Decompiled source of MorePlanters Updated v3.0.7

MorePlanters.dll

Decompiled a day ago
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.IO;
using System.Reflection;
using System.Runtime.CompilerServices;
using System.Runtime.Versioning;
using System.Security;
using System.Security.Permissions;
using BepInEx;
using BepInEx.Configuration;
using BepInEx.Logging;
using EquinoxsModUtils;
using EquinoxsModUtils.Additions;
using HarmonyLib;
using Microsoft.CodeAnalysis;
using UnityEngine;

[assembly: CompilationRelaxations(8)]
[assembly: RuntimeCompatibility(WrapNonExceptionThrows = true)]
[assembly: Debuggable(DebuggableAttribute.DebuggingModes.IgnoreSymbolStoreSequencePoints)]
[assembly: TargetFramework(".NETFramework,Version=v4.7.2", FrameworkDisplayName = ".NET Framework 4.7.2")]
[assembly: AssemblyCompany("MorePlanters")]
[assembly: AssemblyConfiguration("Release")]
[assembly: AssemblyFileVersion("1.0.0.0")]
[assembly: AssemblyInformationalVersion("1.0.0")]
[assembly: AssemblyProduct("MorePlanters")]
[assembly: AssemblyTitle("MorePlanters")]
[assembly: SecurityPermission(SecurityAction.RequestMinimum, SkipVerification = true)]
[assembly: AssemblyVersion("1.0.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 MorePlanters
{
	[BepInPlugin("com.equinox.MorePlanters", "MorePlanters", "3.0.5")]
	[BepInDependency(/*Could not decode attribute arguments.*/)]
	[BepInDependency(/*Could not decode attribute arguments.*/)]
	public class MorePlantersPlugin : BaseUnityPlugin
	{
		private const string MyGUID = "com.equinox.MorePlanters";

		private const string PluginName = "MorePlanters";

		private const string VersionString = "3.0.5";

		private static readonly Harmony Harmony = new Harmony("com.equinox.MorePlanters");

		public static ManualLogSource Log;

		public const string PlanterMk2Name = "Planter MKII";

		public const string PlanterMk3Name = "Planter MKIII";

		public static ConfigEntry<bool> doublePlants;

		public static ConfigEntry<float> speedMultiplier;

		public static Dictionary<uint, PlanterMk3Settings> planterSettings = new Dictionary<uint, PlanterMk3Settings>();

		private static string dataFolder => Application.persistentDataPath + "/MorePlanters";

		private void Awake()
		{
			//IL_0030: Unknown result type (might be due to invalid IL or missing references)
			//IL_0035: Unknown result type (might be due to invalid IL or missing references)
			//IL_0037: Unknown result type (might be due to invalid IL or missing references)
			//IL_003c: Unknown result type (might be due to invalid IL or missing references)
			//IL_003e: Unknown result type (might be due to invalid IL or missing references)
			//IL_0043: Unknown result type (might be due to invalid IL or missing references)
			//IL_004b: Unknown result type (might be due to invalid IL or missing references)
			//IL_0056: Unknown result type (might be due to invalid IL or missing references)
			//IL_0061: Unknown result type (might be due to invalid IL or missing references)
			//IL_0063: Unknown result type (might be due to invalid IL or missing references)
			//IL_0068: Unknown result type (might be due to invalid IL or missing references)
			//IL_0075: Expected O, but got Unknown
			//IL_0075: Unknown result type (might be due to invalid IL or missing references)
			//IL_007a: Unknown result type (might be due to invalid IL or missing references)
			//IL_007c: Unknown result type (might be due to invalid IL or missing references)
			//IL_0081: Unknown result type (might be due to invalid IL or missing references)
			//IL_0083: Unknown result type (might be due to invalid IL or missing references)
			//IL_0088: Unknown result type (might be due to invalid IL or missing references)
			//IL_0093: Unknown result type (might be due to invalid IL or missing references)
			//IL_009e: Unknown result type (might be due to invalid IL or missing references)
			//IL_00a9: Unknown result type (might be due to invalid IL or missing references)
			//IL_00ab: Unknown result type (might be due to invalid IL or missing references)
			//IL_00b0: Unknown result type (might be due to invalid IL or missing references)
			//IL_00bd: Expected O, but got Unknown
			//IL_00c2: Unknown result type (might be due to invalid IL or missing references)
			//IL_00c7: Unknown result type (might be due to invalid IL or missing references)
			//IL_00d2: Unknown result type (might be due to invalid IL or missing references)
			//IL_00dd: Unknown result type (might be due to invalid IL or missing references)
			//IL_00df: Unknown result type (might be due to invalid IL or missing references)
			//IL_00e4: Unknown result type (might be due to invalid IL or missing references)
			//IL_00eb: Unknown result type (might be due to invalid IL or missing references)
			//IL_00f3: Unknown result type (might be due to invalid IL or missing references)
			//IL_00fb: Unknown result type (might be due to invalid IL or missing references)
			//IL_0106: Unknown result type (might be due to invalid IL or missing references)
			//IL_0117: Expected O, but got Unknown
			//IL_011c: Unknown result type (might be due to invalid IL or missing references)
			//IL_0121: Unknown result type (might be due to invalid IL or missing references)
			//IL_012c: Unknown result type (might be due to invalid IL or missing references)
			//IL_0137: Unknown result type (might be due to invalid IL or missing references)
			//IL_0139: Unknown result type (might be due to invalid IL or missing references)
			//IL_013e: Unknown result type (might be due to invalid IL or missing references)
			//IL_0145: Unknown result type (might be due to invalid IL or missing references)
			//IL_014d: Unknown result type (might be due to invalid IL or missing references)
			//IL_0155: Unknown result type (might be due to invalid IL or missing references)
			//IL_0160: Unknown result type (might be due to invalid IL or missing references)
			//IL_0171: Expected O, but got Unknown
			//IL_0171: Unknown result type (might be due to invalid IL or missing references)
			//IL_0176: Unknown result type (might be due to invalid IL or missing references)
			//IL_0181: Unknown result type (might be due to invalid IL or missing references)
			//IL_0183: Unknown result type (might be due to invalid IL or missing references)
			//IL_0188: Unknown result type (might be due to invalid IL or missing references)
			//IL_018f: Unknown result type (might be due to invalid IL or missing references)
			//IL_019a: Unknown result type (might be due to invalid IL or missing references)
			//IL_01a5: Unknown result type (might be due to invalid IL or missing references)
			//IL_01b2: Unknown result type (might be due to invalid IL or missing references)
			//IL_01c3: Unknown result type (might be due to invalid IL or missing references)
			//IL_01d5: Unknown result type (might be due to invalid IL or missing references)
			//IL_01e4: Unknown result type (might be due to invalid IL or missing references)
			//IL_01f1: Unknown result type (might be due to invalid IL or missing references)
			//IL_0200: Unknown result type (might be due to invalid IL or missing references)
			//IL_020e: Expected O, but got Unknown
			//IL_020e: Unknown result type (might be due to invalid IL or missing references)
			//IL_0213: Unknown result type (might be due to invalid IL or missing references)
			//IL_021e: Unknown result type (might be due to invalid IL or missing references)
			//IL_0220: Unknown result type (might be due to invalid IL or missing references)
			//IL_0225: Unknown result type (might be due to invalid IL or missing references)
			//IL_022c: Unknown result type (might be due to invalid IL or missing references)
			//IL_0237: Unknown result type (might be due to invalid IL or missing references)
			//IL_0242: Unknown result type (might be due to invalid IL or missing references)
			//IL_024f: Unknown result type (might be due to invalid IL or missing references)
			//IL_0260: Unknown result type (might be due to invalid IL or missing references)
			//IL_0271: Unknown result type (might be due to invalid IL or missing references)
			//IL_0280: Unknown result type (might be due to invalid IL or missing references)
			//IL_028d: Unknown result type (might be due to invalid IL or missing references)
			//IL_029c: Unknown result type (might be due to invalid IL or missing references)
			//IL_02aa: Expected O, but got Unknown
			Log = ((BaseUnityPlugin)this).Logger;
			Log.LogInfo((object)"PluginName: MorePlanters, VersionString: 3.0.5 is loading...");
			Harmony.PatchAll();
			CreateConfigEntries();
			ApplyPatches();
			EMUAdditions.AddNewUnlock(new NewUnlockDetails
			{
				category = (TechCategory)1,
				coreTypeNeeded = (CoreType)2,
				coreCountNeeded = 100,
				description = "Produces plants at 2x speed. Can produce two plants per seed.",
				displayName = "Planter MKII",
				requiredTier = (ResearchTier)2,
				treePosition = 0
			}, false);
			EMUAdditions.AddNewUnlock(new NewUnlockDetails
			{
				category = (TechCategory)1,
				coreTypeNeeded = (CoreType)2,
				coreCountNeeded = 250,
				description = "Produces plants at 2x speed with integrated Thresher. Outputs processed materials directly.",
				displayName = "Planter MKIII",
				requiredTier = (ResearchTier)2,
				treePosition = 0
			}, false);
			EMUAdditions.AddNewMachine<PlanterInstance, PlanterDefinition>((MachineDefinition<PlanterInstance, PlanterDefinition>)(object)ScriptableObject.CreateInstance<PlanterDefinition>(), new NewResourceDetails
			{
				name = "Planter MKII",
				description = "Produces plants at 2x speed. Can produce two plants per seed.",
				craftingMethod = (CraftingMethod)0,
				craftTierRequired = 0,
				maxStackCount = 50,
				sortPriority = 100,
				unlockName = "Planter MKII",
				parentName = "Planter"
			}, false);
			EMUAdditions.AddNewMachine<PlanterInstance, PlanterDefinition>((MachineDefinition<PlanterInstance, PlanterDefinition>)(object)ScriptableObject.CreateInstance<PlanterDefinition>(), new NewResourceDetails
			{
				name = "Planter MKIII",
				description = "Produces plants at 2x speed with integrated Thresher. Outputs processed materials directly.",
				craftingMethod = (CraftingMethod)0,
				craftTierRequired = 0,
				maxStackCount = 50,
				sortPriority = 101,
				unlockName = "Planter MKIII",
				parentName = "Planter"
			}, false);
			EMUAdditions.AddNewRecipe(new NewRecipeDetails
			{
				GUID = "com.equinox.MorePlanters",
				craftingMethod = (CraftingMethod)0,
				craftTierRequired = 0,
				duration = 5f,
				unlockName = "Planter MKII",
				ingredients = new List<RecipeResourceInfo>
				{
					new RecipeResourceInfo("Planter", 1),
					new RecipeResourceInfo("Mechanical Components", 5),
					new RecipeResourceInfo("Copper Wire", 10)
				},
				outputs = new List<RecipeResourceInfo>
				{
					new RecipeResourceInfo("Planter MKII", 1)
				},
				sortPriority = 100
			}, false);
			EMUAdditions.AddNewRecipe(new NewRecipeDetails
			{
				GUID = "com.equinox.MorePlanters",
				craftingMethod = (CraftingMethod)0,
				craftTierRequired = 0,
				duration = 10f,
				unlockName = "Planter MKIII",
				ingredients = new List<RecipeResourceInfo>
				{
					new RecipeResourceInfo("Planter MKII", 1),
					new RecipeResourceInfo("Thresher", 1),
					new RecipeResourceInfo("Processor Unit", 2)
				},
				outputs = new List<RecipeResourceInfo>
				{
					new RecipeResourceInfo("Planter MKIII", 1)
				},
				sortPriority = 101
			}, false);
			Events.SaveStateLoaded += OnSaveStateLoaded;
			Events.TechTreeStateLoaded += OnTechTreeLoaded;
			Log.LogInfo((object)"PluginName: MorePlanters, VersionString: 3.0.5 is loaded.");
		}

		private void CreateConfigEntries()
		{
			//IL_002e: Unknown result type (might be due to invalid IL or missing references)
			//IL_0038: Expected O, but got Unknown
			//IL_0059: Unknown result type (might be due to invalid IL or missing references)
			//IL_0063: Expected O, but got Unknown
			speedMultiplier = ((BaseUnityPlugin)this).Config.Bind<float>("General", "Speed Multiplier", 2f, new ConfigDescription("Speed multiplier for upgraded planters (default 2x).", (AcceptableValueBase)(object)new AcceptableValueRange<float>(1f, 10f), Array.Empty<object>()));
			doublePlants = ((BaseUnityPlugin)this).Config.Bind<bool>("General", "Double Plants", true, new ConfigDescription("Whether the Planter MKII should produce two plants per seed.", (AcceptableValueBase)null, Array.Empty<object>()));
		}

		private void ApplyPatches()
		{
			Harmony.CreateAndPatchAll(typeof(PlanterInstancePatch), (string)null);
		}

		private void OnSaveStateLoaded(object sender, EventArgs e)
		{
			LoadData(SaveState.instance.metadata.worldName);
		}

		private void OnTechTreeLoaded()
		{
			//IL_004d: Unknown result type (might be due to invalid IL or missing references)
			//IL_0052: Unknown result type (might be due to invalid IL or missing references)
			//IL_008f: Unknown result type (might be due to invalid IL or missing references)
			//IL_0094: Unknown result type (might be due to invalid IL or missing references)
			Unlock unlockByName = Unlocks.GetUnlockByName("Atlantum Ingot", false);
			Unlock unlockByName2 = Unlocks.GetUnlockByName("Thresher MKII", false);
			Unlock unlockByName3 = Unlocks.GetUnlockByName("Assembler MKII", false);
			Unlock unlockByName4 = Unlocks.GetUnlockByName("Planter MKII", false);
			if ((Object)(object)unlockByName4 != (Object)null && (Object)(object)unlockByName != (Object)null && (Object)(object)unlockByName3 != (Object)null)
			{
				unlockByName4.requiredTier = unlockByName.requiredTier;
				unlockByName4.treePosition = unlockByName3.treePosition;
			}
			Unlock unlockByName5 = Unlocks.GetUnlockByName("Planter MKIII", false);
			if ((Object)(object)unlockByName5 != (Object)null && (Object)(object)unlockByName2 != (Object)null && (Object)(object)unlockByName3 != (Object)null)
			{
				unlockByName5.requiredTier = unlockByName2.requiredTier;
				unlockByName5.treePosition = unlockByName3.treePosition;
			}
		}

		public static void SaveData(string worldName)
		{
			Directory.CreateDirectory(dataFolder);
			string path = dataFolder + "/" + worldName + ".txt";
			List<string> list = new List<string>();
			foreach (KeyValuePair<uint, PlanterMk3Settings> planterSetting in planterSettings)
			{
				list.Add(planterSetting.Value.Serialize());
			}
			File.WriteAllLines(path, list);
		}

		public static void LoadData(string worldName)
		{
			string path = dataFolder + "/" + worldName + ".txt";
			if (!File.Exists(path))
			{
				Log.LogInfo((object)("No save file found for world '" + worldName + "'"));
				return;
			}
			planterSettings.Clear();
			string[] array = File.ReadAllLines(path);
			for (int i = 0; i < array.Length; i++)
			{
				PlanterMk3Settings planterMk3Settings = PlanterMk3Settings.Deserialize(array[i]);
				planterSettings[planterMk3Settings.instanceId] = planterMk3Settings;
			}
		}

		public static bool IsPlanterMk2(PlanterInstance planter)
		{
			if ((Object)(object)((PlanterInstance)(ref planter)).myDef != (Object)null)
			{
				return ((ResourceInfo)((PlanterInstance)(ref planter)).myDef).displayName == "Planter MKII";
			}
			return false;
		}

		public static bool IsPlanterMk3(PlanterInstance planter)
		{
			if ((Object)(object)((PlanterInstance)(ref planter)).myDef != (Object)null)
			{
				return ((ResourceInfo)((PlanterInstance)(ref planter)).myDef).displayName == "Planter MKIII";
			}
			return false;
		}

		public static bool IsUpgradedPlanter(PlanterInstance planter)
		{
			//IL_0000: Unknown result type (might be due to invalid IL or missing references)
			//IL_0008: Unknown result type (might be due to invalid IL or missing references)
			if (!IsPlanterMk2(planter))
			{
				return IsPlanterMk3(planter);
			}
			return true;
		}
	}
	public class PlanterMk3Settings
	{
		public uint instanceId;

		public int[] slotOutputMode = new int[4];

		public PlanterMk3Settings(uint id)
		{
			instanceId = id;
		}

		public string Serialize()
		{
			return $"{instanceId}|{slotOutputMode[0]}|{slotOutputMode[1]}|{slotOutputMode[2]}|{slotOutputMode[3]}";
		}

		public static PlanterMk3Settings Deserialize(string input)
		{
			string[] array = input.Split(new char[1] { '|' });
			PlanterMk3Settings planterMk3Settings = new PlanterMk3Settings(uint.Parse(array[0]));
			for (int i = 0; i < 4 && i + 1 < array.Length; i++)
			{
				planterMk3Settings.slotOutputMode[i] = int.Parse(array[i + 1]);
			}
			return planterMk3Settings;
		}
	}
	internal class PlanterInstancePatch
	{
		private static Dictionary<uint, bool[]> wasGrowing = new Dictionary<uint, bool[]>();

		[HarmonyPatch(typeof(PlanterInstance), "SimUpdate")]
		[HarmonyPrefix]
		private static void SimUpdatePrefix(ref PlanterInstance __instance)
		{
			//IL_0001: Unknown result type (might be due to invalid IL or missing references)
			//IL_0054: Unknown result type (might be due to invalid IL or missing references)
			//IL_005a: Invalid comparison between Unknown and I4
			if (!MorePlantersPlugin.IsUpgradedPlanter(__instance))
			{
				return;
			}
			uint instanceId = __instance.commonInfo.instanceId;
			if (!wasGrowing.ContainsKey(instanceId))
			{
				wasGrowing[instanceId] = new bool[4];
			}
			for (int i = 0; i < __instance.plantSlots.Length && i < 4; i++)
			{
				wasGrowing[instanceId][i] = (int)__instance.plantSlots[i].state == 1;
			}
			for (int j = 0; j < __instance.plantSlots.Length; j++)
			{
				ref PlantSlot reference = ref __instance.plantSlots[j];
				if (reference.plantId != -1 && reference.totalGrowthDuration == 120f)
				{
					reference.totalGrowthDuration = 120f / MorePlantersPlugin.speedMultiplier.Value;
				}
			}
		}

		[HarmonyPatch(typeof(PlanterInstance), "SimUpdate")]
		[HarmonyPostfix]
		private static void SimUpdatePostfix(ref PlanterInstance __instance)
		{
			//IL_0001: Unknown result type (might be due to invalid IL or missing references)
			//IL_004c: Unknown result type (might be due to invalid IL or missing references)
			//IL_0052: Invalid comparison between Unknown and I4
			//IL_005d: Unknown result type (might be due to invalid IL or missing references)
			//IL_0093: Unknown result type (might be due to invalid IL or missing references)
			if (!MorePlantersPlugin.IsUpgradedPlanter(__instance))
			{
				return;
			}
			uint instanceId = __instance.commonInfo.instanceId;
			if (!wasGrowing.ContainsKey(instanceId))
			{
				return;
			}
			for (int i = 0; i < __instance.plantSlots.Length && i < 4; i++)
			{
				ref PlantSlot reference = ref __instance.plantSlots[i];
				if (!wasGrowing[instanceId][i] || (int)reference.state != 2)
				{
					continue;
				}
				if (MorePlantersPlugin.IsPlanterMk2(__instance) && MorePlantersPlugin.doublePlants.Value && reference.plantId != -1)
				{
					((Inventory)(ref ((PlanterInstance)(ref __instance)).GetOutputInventory())).AddResources(reference.plantId, 1, true);
				}
				if (!MorePlantersPlugin.IsPlanterMk3(__instance))
				{
					continue;
				}
				ref Inventory outputInventory = ref ((PlanterInstance)(ref __instance)).GetOutputInventory();
				for (int j = 0; j < outputInventory.myStacks.Length; j++)
				{
					ref ResourceStack reference2 = ref outputInventory.myStacks[j];
					if (((ResourceStack)(ref reference2)).isEmpty || (Object)(object)((ResourceStack)(ref reference2)).info == (Object)null)
					{
						continue;
					}
					string processedName = GetProcessedName(((ResourceStack)(ref reference2)).info.displayName);
					if (processedName != null)
					{
						ResourceInfo resourceInfoByName = Resources.GetResourceInfoByName(processedName, false);
						if ((Object)(object)resourceInfoByName != (Object)null)
						{
							int count = reference2.count;
							((Inventory)(ref outputInventory)).TryRemoveResources(((UniqueIdScriptableObject)((ResourceStack)(ref reference2)).info).uniqueId, count);
							((Inventory)(ref outputInventory)).AddResources(((UniqueIdScriptableObject)resourceInfoByName).uniqueId, count, true);
						}
					}
				}
			}
		}

		private static string GetProcessedName(string rawPlantName)
		{
			return rawPlantName switch
			{
				"Kindlevine" => "Kindlevine Stems", 
				"Shiverthorn" => "Shiverthorn Buds", 
				"Plantmatter" => "Plantmatter Fiber", 
				_ => null, 
			};
		}

		[HarmonyPatch(typeof(PlanterInstance), "TakeAll")]
		[HarmonyPostfix]
		private static void TakeAllPostfix(ref PlanterInstance __instance, bool actuallyTake, ref List<ResourceStack> __result)
		{
			//IL_0005: Unknown result type (might be due to invalid IL or missing references)
			//IL_0031: Unknown result type (might be due to invalid IL or missing references)
			//IL_0036: Unknown result type (might be due to invalid IL or missing references)
			//IL_0038: Unknown result type (might be due to invalid IL or missing references)
			//IL_004b: Unknown result type (might be due to invalid IL or missing references)
			//IL_0051: Unknown result type (might be due to invalid IL or missing references)
			if (!actuallyTake || !MorePlantersPlugin.IsPlanterMk2(__instance) || !MorePlantersPlugin.doublePlants.Value)
			{
				return;
			}
			List<ResourceStack> list = new List<ResourceStack>();
			foreach (ResourceStack item in __result)
			{
				ResourceStack current = item;
				list.Add(current);
				list.Add(ResourceStack.CreateSimpleStack(((UniqueIdScriptableObject)((ResourceStack)(ref current)).info).uniqueId, current.count));
			}
			__result.Clear();
			__result.AddRange(list);
		}
	}
	[HarmonyPatch(typeof(SaveState), "SaveToFile")]
	internal class SaveStatePatch
	{
		[HarmonyPostfix]
		private static void SavePlanterData()
		{
			MorePlantersPlugin.SaveData(SaveState.instance.metadata.worldName);
			MorePlantersPlugin.Log.LogInfo((object)"MorePlanters data saved");
		}
	}
}