Decompiled source of VirtualCores v2.0.2

plugins/VirtualCores/VirtualCores.dll

Decompiled 2 months ago
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.IO;
using System.Linq;
using System.Reflection;
using System.Runtime.CompilerServices;
using System.Runtime.InteropServices;
using System.Runtime.Versioning;
using BepInEx;
using BepInEx.Logging;
using HarmonyLib;
using UnityEngine;
using VirtualCores.Patches;

[assembly: CompilationRelaxations(8)]
[assembly: RuntimeCompatibility(WrapNonExceptionThrows = true)]
[assembly: Debuggable(DebuggableAttribute.DebuggingModes.IgnoreSymbolStoreSequencePoints)]
[assembly: AssemblyTitle("VirtualCores")]
[assembly: AssemblyDescription("VirtualCores mod for Techtonica")]
[assembly: AssemblyConfiguration("")]
[assembly: AssemblyCompany("Equinox")]
[assembly: AssemblyProduct("VirtualCores")]
[assembly: AssemblyCopyright("Copyright ©  2023")]
[assembly: AssemblyTrademark("")]
[assembly: ComVisible(false)]
[assembly: Guid("7ed68aa2-d2f3-4e46-8aee-9b17b49bab4e")]
[assembly: AssemblyFileVersion("1.0.0.0")]
[assembly: TargetFramework(".NETFramework,Version=v4.7.2", FrameworkDisplayName = ".NET Framework 4.7.2")]
[assembly: AssemblyVersion("1.0.0.0")]
namespace VirtualCores
{
	[BepInPlugin("com.equinox.VirtualCores", "VirtualCores", "2.0.2")]
	public class VirtualCoresPlugin : BaseUnityPlugin
	{
		private const string MyGUID = "com.equinox.VirtualCores";

		private const string PluginName = "VirtualCores";

		private const string VersionString = "2.0.2";

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

		public static ManualLogSource Log = new ManualLogSource("VirtualCores");

		private static string saveDataFolder = Application.persistentDataPath + "/VirtualCores";

		public static Dictionary<CoreType, int> virtualCoreCounts = new Dictionary<CoreType, int>();

		private void Awake()
		{
			//IL_0052: Unknown result type (might be due to invalid IL or missing references)
			//IL_0057: Unknown result type (might be due to invalid IL or missing references)
			//IL_005d: Unknown result type (might be due to invalid IL or missing references)
			((BaseUnityPlugin)this).Logger.LogInfo((object)"PluginName: VirtualCores, VersionString: 2.0.2 is loading...");
			Harmony.PatchAll();
			((BaseUnityPlugin)this).Logger.LogInfo((object)"PluginName: VirtualCores, VersionString: 2.0.2 is loaded.");
			Log = ((BaseUnityPlugin)this).Logger;
			foreach (CoreType value in Enum.GetValues(typeof(CoreType)))
			{
				virtualCoreCounts.Add(value, 0);
			}
			Directory.CreateDirectory(saveDataFolder);
			Harmony.CreateAndPatchAll(typeof(MemoryTreePatch), (string)null);
			Harmony.CreateAndPatchAll(typeof(TechTreeStatePatch), (string)null);
			Harmony.CreateAndPatchAll(typeof(SaveStatePatch), (string)null);
		}

		public static void saveData(string worldName)
		{
			string path = saveDataFolder + "/" + worldName + ".txt";
			string contents = string.Join(",", virtualCoreCounts.Values);
			File.WriteAllText(path, contents);
		}

		public static void loadData(string worldName)
		{
			//IL_0049: Unknown result type (might be due to invalid IL or missing references)
			//IL_004e: 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)
			string path = saveDataFolder + "/" + worldName + ".txt";
			if (File.Exists(path))
			{
				string[] array = File.ReadAllText(path).Split(new char[1] { ',' });
				for (int i = 0; i < array.Length; i++)
				{
					CoreType key = virtualCoreCounts.Keys.ToList()[i];
					virtualCoreCounts[key] = int.Parse(array[i]);
				}
			}
		}
	}
}
namespace VirtualCores.Patches
{
	public class MemoryTreePatch
	{
		[HarmonyPatch(typeof(MemoryTreeMachineList), "UpdateAll")]
		[HarmonyPostfix]
		private static void addVirtualCore(MemoryTreeMachineList __instance)
		{
			//IL_0026: Unknown result type (might be due to invalid IL or missing references)
			//IL_002c: Invalid comparison between Unknown and I4
			//IL_004b: Unknown result type (might be due to invalid IL or missing references)
			//IL_0050: Unknown result type (might be due to invalid IL or missing references)
			//IL_0053: Unknown result type (might be due to invalid IL or missing references)
			//IL_005e: Unknown result type (might be due to invalid IL or missing references)
			int num = default(int);
			ResearchCoreDefinition val = default(ResearchCoreDefinition);
			int num2 = default(int);
			for (int i = 0; i < ((MachineInstanceList<MemoryTreeInstance, MemoryTreeDefinition>)(object)__instance).curCount; i++)
			{
				if (((MachineInstanceList<MemoryTreeInstance, MemoryTreeDefinition>)(object)__instance).myArray[i].pendingCoreCount == 0 && (int)((MemoryTreeInstance)(ref ((MachineInstanceList<MemoryTreeInstance, MemoryTreeDefinition>)(object)__instance).myArray[i])).GetErrorState() == 1 && ((MemoryTreeInstance)(ref ((MachineInstanceList<MemoryTreeInstance, MemoryTreeDefinition>)(object)__instance).myArray[i])).FindCoreInInventory(ref num, ref val))
				{
					Dictionary<CoreType, int> virtualCoreCounts = VirtualCoresPlugin.virtualCoreCounts;
					CoreType coreType = val.coreType;
					int value = virtualCoreCounts[coreType] + 1;
					virtualCoreCounts[coreType] = value;
					((Inventory)(ref ((MemoryTreeInstance)(ref ((MachineInstanceList<MemoryTreeInstance, MemoryTreeDefinition>)(object)__instance).myArray[i])).GetInputInventory())).RemoveResourcesFromSlot(num, ref num2, 1, false);
				}
			}
		}
	}
	public class SaveStatePatch
	{
		[HarmonyPatch(typeof(SaveState), "LoadFileData", new Type[]
		{
			typeof(SaveMetadata),
			typeof(string)
		})]
		[HarmonyPostfix]
		private static void loadMod(SaveState __instance, SaveMetadata saveMetadata, string replayLocation)
		{
			VirtualCoresPlugin.loadData(saveMetadata.worldName);
		}

		[HarmonyPatch(typeof(SaveState), "SaveToFile")]
		[HarmonyPostfix]
		private static void saveMod(SaveState __instance, string saveLocation, bool saveToPersistent = true)
		{
			VirtualCoresPlugin.saveData(__instance.metadata.worldName);
		}
	}
	public class TechTreeStatePatch
	{
		[HarmonyPatch(typeof(TechTreeState), "NumCoresAvailable")]
		[HarmonyPrefix]
		private static bool addNumVirtualCores(TechTreeState __instance, CoreType type, ref int __result)
		{
			//IL_0019: Unknown result type (might be due to invalid IL or missing references)
			//IL_001a: Invalid comparison between I4 and Unknown
			//IL_001d: Unknown result type (might be due to invalid IL or missing references)
			//IL_0023: Expected I4, but got Unknown
			//IL_002e: Unknown result type (might be due to invalid IL or missing references)
			//IL_002f: Invalid comparison between I4 and Unknown
			//IL_004b: Unknown result type (might be due to invalid IL or missing references)
			//IL_004c: Invalid comparison between I4 and Unknown
			//IL_0037: Unknown result type (might be due to invalid IL or missing references)
			//IL_0062: Unknown result type (might be due to invalid IL or missing references)
			//IL_0055: Unknown result type (might be due to invalid IL or missing references)
			//IL_005b: Expected I4, but got Unknown
			//IL_0070: Unknown result type (might be due to invalid IL or missing references)
			while (__instance.usedResearchCores.Count <= (int)type)
			{
				__instance.usedResearchCores.Add(0);
			}
			int num = __instance.NumCoresOfTypePlaced((int)type);
			if (TechTreeState.coreEffiencyMultipliers.Count() < (int)type)
			{
				num *= FHG_Utils.FloorToInt(TechTreeState.coreEffiencyMultipliers[type]);
			}
			if (__instance.usedResearchCores.Count > (int)type)
			{
				num -= __instance.usedResearchCores[(int)type];
			}
			if (VirtualCoresPlugin.virtualCoreCounts.ContainsKey(type))
			{
				num += VirtualCoresPlugin.virtualCoreCounts[type];
			}
			__result = num;
			return false;
		}

		[HarmonyPatch(typeof(TechTreeState), "HandleEndOfFrame")]
		[HarmonyPostfix]
		private static void addVirtualCoresToClusters(TechTreeState __instance)
		{
			//IL_0028: Unknown result type (might be due to invalid IL or missing references)
			//IL_002a: 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)
			__instance.freeCores = 0;
			int num = Math.Max(__instance.totalResearchCores.Count, __instance.usedResearchCores.Count);
			ResearchCoreDefinition val2 = default(ResearchCoreDefinition);
			for (int i = 0; i < num; i++)
			{
				CoreType val = (CoreType)i;
				int num2 = __instance.NumCoresAvailable(val);
				if (num2 > 0 && GameDefines.instance.TryGetCoreDefinitionForType(val, ref val2) && val2.clusterSize > 0f)
				{
					__instance.freeCores += Mathf.FloorToInt((float)num2 / val2.clusterSize);
				}
			}
			ref UpgradeState[] upgradeStates = ref GameState.instance.upgradeStates;
			__instance.freeCoresAssembling = (float)__instance.freeCores * upgradeStates[22].floatVal * 0.01f;
			__instance.freeCoresMining = (float)__instance.freeCores * upgradeStates[19].floatVal * 0.01f;
			__instance.freeCoresPowerOutput = (float)__instance.freeCores * upgradeStates[23].floatVal * 0.01f;
			__instance.freeCoresSmelting = (float)__instance.freeCores * upgradeStates[20].floatVal * 0.01f;
			__instance.freeCoresThreshing = (float)__instance.freeCores * upgradeStates[21].floatVal * 0.01f;
		}
	}
}