Decompiled source of PlantsAlwaysGrowMono v1.1.0

Mods\PlantsAlwaysGrowMono.dll

Decompiled 2 weeks ago
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Reflection;
using System.Resources;
using System.Runtime.CompilerServices;
using System.Runtime.Versioning;
using System.Security;
using System.Security.Permissions;
using HarmonyLib;
using MelonLoader;
using Microsoft.CodeAnalysis;
using PlantsAlwaysGrow;
using ScheduleOne.DevUtilities;
using ScheduleOne.EntityFramework;
using ScheduleOne.GameTime;
using ScheduleOne.Growing;
using ScheduleOne.Lighting;
using UnityEngine;
using UnityEngine.Events;

[assembly: CompilationRelaxations(8)]
[assembly: RuntimeCompatibility(WrapNonExceptionThrows = true)]
[assembly: Debuggable(DebuggableAttribute.DebuggingModes.Default | DebuggableAttribute.DebuggingModes.DisableOptimizations | DebuggableAttribute.DebuggingModes.IgnoreSymbolStoreSequencePoints | DebuggableAttribute.DebuggingModes.EnableEditAndContinue)]
[assembly: MelonInfo(typeof(PlantsAlwaysGrowMod), "PlantsAlwaysGrowMod", "1.1.0", "lasersquid", null)]
[assembly: MelonGame("TVGS", "Schedule I")]
[assembly: TargetFramework(".NETStandard,Version=v2.1", FrameworkDisplayName = ".NET Standard 2.1")]
[assembly: AssemblyCompany("PlantsAlwaysGrowMono")]
[assembly: AssemblyConfiguration("Mono")]
[assembly: AssemblyFileVersion("1.1.0.0")]
[assembly: AssemblyInformationalVersion("1.1.0+36dcdab2e15827ca10cbd440bd4a16fa3ed19124")]
[assembly: AssemblyProduct("PlantsAlwaysGrowMono")]
[assembly: AssemblyTitle("PlantsAlwaysGrowMono")]
[assembly: NeutralResourcesLanguage("en-US")]
[assembly: SecurityPermission(SecurityAction.RequestMinimum, SkipVerification = true)]
[assembly: AssemblyVersion("1.1.0.0")]
[module: UnverifiableCode]
[module: RefSafetyRules(11)]
namespace Microsoft.CodeAnalysis
{
	[CompilerGenerated]
	[Microsoft.CodeAnalysis.Embedded]
	internal sealed class EmbeddedAttribute : Attribute
	{
	}
}
namespace System.Runtime.CompilerServices
{
	[CompilerGenerated]
	[Microsoft.CodeAnalysis.Embedded]
	[AttributeUsage(AttributeTargets.Module, AllowMultiple = false, Inherited = false)]
	internal sealed class RefSafetyRulesAttribute : Attribute
	{
		public readonly int Version;

		public RefSafetyRulesAttribute(int P_0)
		{
			Version = P_0;
		}
	}
}
namespace PlantsAlwaysGrow
{
	public class PlantsAlwaysGrowMod : MelonMod
	{
		private static bool _isPatchApplied;

		private static Harmony _harmonyInstance;

		public override void OnInitializeMelon()
		{
			((MelonEventBase<LemonAction<int, string>>)(object)MelonEvents.OnSceneWasLoaded).Subscribe((LemonAction<int, string>)OnSceneLoaded, 0, false);
			((MelonBase)this).LoggerInstance.Msg("PlantsAlwaysGrow initialized.");
		}

		private void OnSceneLoaded(int buildIndex, string sceneName)
		{
			//IL_0024: Unknown result type (might be due to invalid IL or missing references)
			//IL_002e: Expected O, but got Unknown
			if (sceneName == "Main" && !_isPatchApplied)
			{
				try
				{
					_harmonyInstance = new Harmony("PlantsAlwaysGrowMod");
					_harmonyInstance.PatchAll();
					_isPatchApplied = true;
				}
				catch (Exception ex)
				{
					((MelonBase)this).LoggerInstance.Error("Failed to patch mod: " + ex.Message);
				}
			}
		}
	}
	public static class Utils
	{
		public class UnityObjectComparer : IEqualityComparer<Object>
		{
			public bool Equals(Object a, Object b)
			{
				return a.GetInstanceID() == b.GetInstanceID();
			}

			public int GetHashCode(Object item)
			{
				return item.GetInstanceID();
			}
		}

		public static PlantsAlwaysGrowMod Mod;

		public static void PrintException(Exception e)
		{
			Warn("Exception: " + e.GetType().Name + " - " + e.Message);
			Warn("Source: " + e.Source);
			Warn(e.StackTrace ?? "");
			if (e.InnerException != null)
			{
				Warn("Inner exception: " + e.InnerException.GetType().Name + " - " + e.InnerException.Message);
				Warn("Source: " + e.InnerException.Source);
				Warn(e.InnerException.StackTrace ?? "");
				if (e.InnerException.InnerException != null)
				{
					Warn("Inner inner exception: " + e.InnerException.InnerException.GetType().Name + " - " + e.InnerException.InnerException.Message);
					Warn("Source: " + e.InnerException.InnerException.Source);
					Warn(e.InnerException.InnerException.StackTrace ?? "");
				}
			}
		}

		public static Treturn GetField<Ttarget, Treturn>(string fieldName, object target) where Treturn : class
		{
			return CastTo<Treturn>(GetField<Ttarget>(fieldName, target));
		}

		public static object GetField<Ttarget>(string fieldName, object target)
		{
			return AccessTools.Field(typeof(Ttarget), fieldName).GetValue(target);
		}

		public static void SetField<Ttarget>(string fieldName, object target, object value)
		{
			AccessTools.Field(typeof(Ttarget), fieldName).SetValue(target, value);
		}

		public static Treturn GetProperty<Ttarget, Treturn>(string fieldName, object target) where Treturn : class
		{
			return CastTo<Treturn>(GetProperty<Ttarget>(fieldName, target));
		}

		public static object GetProperty<Ttarget>(string fieldName, object target)
		{
			return AccessTools.Property(typeof(Ttarget), fieldName).GetValue(target);
		}

		public static void SetProperty<Ttarget>(string fieldName, object target, object value)
		{
			AccessTools.Property(typeof(Ttarget), fieldName).SetValue(target, value);
		}

		public static Treturn CallMethod<Ttarget, Treturn>(string methodName, object target) where Treturn : class
		{
			return CastTo<Treturn>(CallMethod<Ttarget>(methodName, target, Array.Empty<object>()));
		}

		public static Treturn CallMethod<Ttarget, Treturn>(string methodName, object target, object[] args) where Treturn : class
		{
			return CastTo<Treturn>(CallMethod<Ttarget>(methodName, target, args));
		}

		public static Treturn CallMethod<Ttarget, Treturn>(string methodName, Type[] argTypes, object target, object[] args) where Treturn : class
		{
			return CastTo<Treturn>(CallMethod<Ttarget>(methodName, argTypes, target, args));
		}

		public static object CallMethod<Ttarget>(string methodName, object target)
		{
			return AccessTools.Method(typeof(Ttarget), methodName, (Type[])null, (Type[])null).Invoke(target, Array.Empty<object>());
		}

		public static object CallMethod<Ttarget>(string methodName, object target, object[] args)
		{
			return AccessTools.Method(typeof(Ttarget), methodName, (Type[])null, (Type[])null).Invoke(target, args);
		}

		public static object CallMethod<Ttarget>(string methodName, Type[] argTypes, object target, object[] args)
		{
			return AccessTools.Method(typeof(Ttarget), methodName, argTypes, (Type[])null).Invoke(target, args);
		}

		public static void SetMod(PlantsAlwaysGrowMod mod)
		{
			Mod = mod;
		}

		public static T CastTo<T>(object o) where T : class
		{
			if (!(o is T result))
			{
				return null;
			}
			return result;
		}

		public static bool Is<T>(object o)
		{
			return o is T;
		}

		public static UnityAction ToUnityAction(Action action)
		{
			//IL_0008: Unknown result type (might be due to invalid IL or missing references)
			//IL_000e: Expected O, but got Unknown
			return new UnityAction(action.Invoke);
		}

		public static UnityAction<T> ToUnityAction<T>(Action<T> action)
		{
			return action.Invoke;
		}

		public static T ToInterface<T>(object o)
		{
			return (T)o;
		}

		public static void Log(string message)
		{
			((MelonBase)Mod).LoggerInstance.Msg(message);
		}

		public static void Warn(string message)
		{
			((MelonBase)Mod).LoggerInstance.Warning(message);
		}
	}
	[HarmonyPatch]
	public static class AlwaysGrowPatches
	{
		[HarmonyPatch(typeof(Plant), "MinPass")]
		[HarmonyPostfix]
		public static void PlantMinPassPostfix(Plant __instance, int mins)
		{
			if (!(__instance.NormalizedGrowthProgress >= 1f) && NetworkSingleton<TimeManager>.Instance.IsEndOfDay)
			{
				float num = 1f / ((float)__instance.GrowthTime * 60f * (float)mins);
				num *= ((GrowContainer)__instance.Pot).GetTemperatureGrowthMultiplier();
				num *= GetAverageLightExposure((GrowContainer)(object)__instance.Pot, out var growSpeedMultiplier);
				num *= __instance.Pot.GrowSpeedMultiplier;
				num *= growSpeedMultiplier;
				if (GameManager.IS_TUTORIAL)
				{
					num *= 0.3f;
				}
				if (((GrowContainer)__instance.Pot).NormalizedMoistureAmount <= 0f)
				{
					num *= 0f;
				}
				__instance.SetNormalizedGrowthProgress(__instance.NormalizedGrowthProgress + num);
			}
		}

		[HarmonyPatch(typeof(ShroomColony), "OnMinPass")]
		[HarmonyPostfix]
		public static void ShroomOnMinPassPostfix(ShroomColony __instance)
		{
			if (NetworkSingleton<TimeManager>.Instance.IsEndOfDay)
			{
				int num = (int)Utils.GetField<ShroomColony>("_growTime", __instance);
				float num2 = (float)Utils.CallMethod<ShroomColony>("GetCurrentGrowthRate", __instance);
				float num3 = num2 / ((float)num * 60f);
				Utils.CallMethod<ShroomColony>("ChangeGrowthPercentage", __instance, new object[1] { num3 });
			}
		}

		private static float GetAverageLightExposure(GrowContainer container, out float growSpeedMultiplier)
		{
			growSpeedMultiplier = 1f;
			UsableLightSource field = Utils.GetField<GrowContainer, UsableLightSource>("_lightSourceOverride", container);
			if ((Object)(object)field != (Object)null)
			{
				return field.GrowSpeedMultiplier;
			}
			float num = 0f;
			float num2 = default(float);
			for (int i = 0; i < ((GridItem)container).CoordinatePairs.Count; i++)
			{
				num += ((GridItem)container).OwnerGrid.GetTile(((GridItem)container).CoordinatePairs[i].coord2).LightExposureNode.GetTotalExposure(ref num2);
				growSpeedMultiplier += num2;
			}
			growSpeedMultiplier /= ((GridItem)container).CoordinatePairs.Count;
			return num / (float)((GridItem)container).CoordinatePairs.Count;
		}
	}
}