Decompiled source of ReusableMetadata v1.0.7

11matt556-ReusableMetadata.dll

Decompiled 10 months ago
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Reflection;
using System.Runtime.CompilerServices;
using System.Runtime.InteropServices;
using System.Runtime.Versioning;
using BepInEx;
using BepInEx.Configuration;
using BepInEx.Logging;
using HarmonyLib;
using UnityEngine.UI;

[assembly: CompilationRelaxations(8)]
[assembly: RuntimeCompatibility(WrapNonExceptionThrows = true)]
[assembly: Debuggable(DebuggableAttribute.DebuggingModes.IgnoreSymbolStoreSequencePoints)]
[assembly: AssemblyTitle("11matt556-ReusableMetadata")]
[assembly: AssemblyDescription("")]
[assembly: AssemblyConfiguration("")]
[assembly: AssemblyCompany("")]
[assembly: AssemblyProduct("11matt556-ReusableMetadata")]
[assembly: AssemblyCopyright("Copyright ©  2022")]
[assembly: AssemblyTrademark("")]
[assembly: ComVisible(false)]
[assembly: Guid("5de677d0-3927-4fb2-b636-7dbedd094f7f")]
[assembly: AssemblyFileVersion("1.0.7")]
[assembly: TargetFramework(".NETFramework,Version=v4.8", FrameworkDisplayName = ".NET Framework 4.8")]
[assembly: AssemblyVersion("1.0.7.0")]
namespace ReusableMetadata;

[BepInPlugin("11matt556.dysonsphereprogram.ReusableMetadata", "Reusable Metadata", "1.0.7")]
public class ReusableMetadataPlugin : BaseUnityPlugin
{
	public const string pluginGuid = "11matt556.dysonsphereprogram.ReusableMetadata";

	public const string pluginName = "Reusable Metadata";

	public const string pluginVersion = "1.0.7";

	public static ManualLogSource logger;

	public static ConfigEntry<bool> useHighestProductionOnly;

	public static ConfigEntry<bool> useVerboseLogging;

	public static ConfigEntry<bool> useSandboxCheat;

	public static ConfigEntry<float> sandboxMultiplier;

	public static IDictionary<int, long> topSeedForItem;

	public void Awake()
	{
		//IL_001a: Unknown result type (might be due to invalid IL or missing references)
		logger = ((BaseUnityPlugin)this).Logger;
		topSeedForItem = new Dictionary<int, long>();
		Harmony val = new Harmony("11matt556.dysonsphereprogram.ReusableMetadata");
		topSeedForItem.Add(6001, -1L);
		topSeedForItem.Add(6002, -1L);
		topSeedForItem.Add(6003, -1L);
		topSeedForItem.Add(6004, -1L);
		topSeedForItem.Add(6005, -1L);
		topSeedForItem.Add(6006, -1L);
		useHighestProductionOnly = ((BaseUnityPlugin)this).Config.Bind<bool>("Behaviour", "useHighestProductionOnly", false, "When True, only metadata contributions from your highest production cluster will be available. Metadata can be thought of as a 'high score' with this setting enabled. When false, Metadata production is unchanged unchanged from Vanilla.");
		useVerboseLogging = ((BaseUnityPlugin)this).Config.Bind<bool>("Debugging", "verboseLogging", false, "For debugging.");
		useSandboxCheat = ((BaseUnityPlugin)this).Config.Bind<bool>("Debugging", "enableSandboxCheat", false, "Sets sandbox metadata multiplier to sandboxMultiplier. Use at your own risk.");
		sandboxMultiplier = ((BaseUnityPlugin)this).Config.Bind<float>("Debugging", "sandboxMultiplier", 1f, "Sets Sandbox Metadata multiplier to the entered value. 1 = 100%");
		val.PatchAll();
		logger.LogInfo((object)"Reusable Metadata 1.0.7 Patch successful");
	}
}
[HarmonyPatch]
public class Patches
{
	[HarmonyPatch(typeof(PropertySystem), "GetItemTotalProperty")]
	[HarmonyPrefix]
	public static bool GetItemTotalProperty_Patch(int itemId, PropertySystem __instance, ref int __result)
	{
		long clusterSeedKey = GameMain.data.GetClusterSeedKey();
		int num = 0;
		int num2 = 0;
		if (ReusableMetadataPlugin.useVerboseLogging.Value)
		{
			ReusableMetadataPlugin.logger.LogInfo((object)("Current Seed " + clusterSeedKey));
		}
		for (int i = 0; i < __instance.propertyDatas.Count; i++)
		{
			ClusterPropertyData val = __instance.propertyDatas[i];
			if (ReusableMetadataPlugin.useVerboseLogging.Value)
			{
				ReusableMetadataPlugin.logger.LogInfo((object)("GetItemTotalProperty_Patch ID=" + itemId + " production=" + val.GetItemProduction(itemId) + " seed=" + val.seedKey));
			}
			int itemProduction = val.GetItemProduction(itemId);
			if (ReusableMetadataPlugin.useHighestProductionOnly.Value)
			{
				if (itemProduction > num)
				{
					num = itemProduction;
					num2 = num;
					ReusableMetadataPlugin.topSeedForItem[itemId] = val.seedKey;
				}
				continue;
			}
			if (ReusableMetadataPlugin.useVerboseLogging.Value)
			{
				ReusableMetadataPlugin.logger.LogInfo((object)("GetItemTotalProperty_Patch ID=" + itemId + " Production=" + itemProduction + " seed=" + val.seedKey));
			}
			num2 += itemProduction;
		}
		if (ReusableMetadataPlugin.useVerboseLogging.Value)
		{
			ReusableMetadataPlugin.logger.LogInfo((object)("GetItemTotalProperty_Patch ID=" + itemId + " Calculated Total=" + num2));
		}
		__result = num2;
		return false;
	}

	[HarmonyPatch(typeof(PropertySystem), "GetItemAvaliableProperty", new Type[]
	{
		typeof(long),
		typeof(int)
	})]
	[HarmonyPrefix]
	public static bool GetItemAvaliableProperty_Patch(long seedKey, int itemId, PropertySystem __instance, ref int __result)
	{
		int num = __instance.GetItemTotalProperty(itemId);
		if (ReusableMetadataPlugin.useVerboseLogging.Value)
		{
			ReusableMetadataPlugin.logger.LogInfo((object)$"GetItemAvaliableProperty_Patch_Start={num} ID={itemId} ");
		}
		if (GameMain.data.GetClusterSeedKey() == seedKey)
		{
			num -= GameMain.history.GetPropertyItemComsumption(itemId);
			if (ReusableMetadataPlugin.useVerboseLogging.Value)
			{
				ReusableMetadataPlugin.logger.LogInfo((object)$"GetItemAvaliableProperty_Patch ID={itemId} PropertySystem.GetItemConsumption={__instance.GetItemConsumption(seedKey, itemId)} seed={seedKey}");
				ReusableMetadataPlugin.logger.LogInfo((object)$"GetItemAvaliableProperty_Patch ID={itemId} GameMain.history.GetPropertyItemComsumption={GameMain.history.GetPropertyItemComsumption(itemId)} seed={seedKey}");
			}
			if (!ReusableMetadataPlugin.useHighestProductionOnly.Value || seedKey == ReusableMetadataPlugin.topSeedForItem[itemId])
			{
				num -= __instance.GetItemProduction(seedKey, itemId);
			}
		}
		if (ReusableMetadataPlugin.useVerboseLogging.Value)
		{
			ReusableMetadataPlugin.logger.LogInfo((object)$"GetItemAvaliableProperty_Patch_Result={__result} ID={itemId} ");
		}
		__result = num;
		return false;
	}

	[HarmonyPatch(typeof(GameScenarioLogic))]
	[HarmonyPatch("GameTick")]
	[HarmonyPostfix]
	public static void GameScenarioLogic_GameTick_Patch(long time, GameScenarioLogic __instance)
	{
		if (__instance.gameData.gameDesc.isSandboxMode && ReusableMetadataPlugin.useSandboxCheat.Value)
		{
			__instance.propertyLogic.GameTick(time);
		}
	}

	[HarmonyPatch(typeof(GameMain))]
	[HarmonyPatch("Begin")]
	[HarmonyPostfix]
	public static void GameMain_Begin_Patch(GameMain __instance)
	{
		if (DSPGame.GameDesc.isSandboxMode && !ReusableMetadataPlugin.useSandboxCheat.Value)
		{
			for (int i = 6001; i <= 6006; i++)
			{
				GameMain.history.SetPropertyItemProduction(i, 0);
				DSPGame.propertySystem.SetItemProduction(DSPGame.GameDesc.seedKey64, i, 0);
			}
		}
	}

	[HarmonyPatch(typeof(PropertySystem))]
	[HarmonyPatch("GetItemProduction")]
	[HarmonyPostfix]
	public static void PropertySystem_GetItemProduction_Patch(long seedKey, int itemId, PropertySystem __instance, ref int __result)
	{
		if (GameMain.gameScenario != null && seedKey == GameMain.data.GetClusterSeedKey())
		{
			int propertyItemProduction = GameMain.history.GetPropertyItemProduction(itemId);
			if (__result < propertyItemProduction)
			{
				__instance.SetItemProduction(seedKey, itemId, propertyItemProduction);
				GameMain.history.SetPropertyItemProduction(itemId, propertyItemProduction);
				__result = propertyItemProduction;
			}
		}
	}

	[HarmonyPatch(typeof(UIPropertyEntry))]
	[HarmonyPatch("UpdateUIElements")]
	[HarmonyPostfix]
	public static void UIPropertyEntry_UpdateUIElements_Patch(UIPropertyEntry __instance)
	{
		if (DSPGame.GameDesc.isSandboxMode && ReusableMetadataPlugin.useSandboxCheat.Value)
		{
			int itemAvaliableProperty = DSPGame.propertySystem.GetItemAvaliableProperty(GameMain.data.GetClusterSeedKey(), __instance.itemId);
			((Selectable)__instance.realizeButton.button).interactable = GameMain.mainPlayer.isAlive && itemAvaliableProperty > 0;
			Text productionRateText = __instance.productionRateText1;
			string text2 = (__instance.productionRateText0.text = $"( x {ReusableMetadataPlugin.sandboxMultiplier.Value:0%} )");
			productionRateText.text = text2;
		}
	}

	[HarmonyPatch(typeof(PropertyLogic), "UpdateProduction")]
	[HarmonyPrefix]
	public static bool PropertyLogic_UpdateProduction_Patch(PropertyLogic __instance)
	{
		FactoryProductionStat[] factoryStatPool = __instance.gameData.statistics.production.factoryStatPool;
		int factoryCount = __instance.gameData.factoryCount;
		ClusterPropertyData clusterData = __instance.propertySystem.GetClusterData(__instance.gameData.GetClusterSeedKey());
		ClusterPropertyData propertyData = __instance.gameData.history.propertyData;
		int[] productIds = PropertySystem.productIds;
		foreach (int num in productIds)
		{
			int itemProduction = propertyData.GetItemProduction(num);
			int itemProduction2 = clusterData.GetItemProduction(num);
			long num2 = 0L;
			for (int j = 0; j < factoryCount; j++)
			{
				int num3 = factoryStatPool[j].productIndices[num];
				if (num3 > 0)
				{
					num2 += factoryStatPool[j].productPool[num3].total[3];
				}
			}
			float num4 = 0f;
			num4 = ((!ReusableMetadataPlugin.useSandboxCheat.Value || !DSPGame.GameDesc.isSandboxMode) ? __instance.gameData.history.minimalPropertyMultiplier : ReusableMetadataPlugin.sandboxMultiplier.Value);
			int num5 = (int)((double)num2 * (double)num4 / 60.0 + 0.001);
			if (num5 > itemProduction)
			{
				propertyData.SetItemProduction(num, num5);
			}
			if (num5 > itemProduction2)
			{
				clusterData.SetItemProduction(num, num5);
			}
			if (ReusableMetadataPlugin.useVerboseLogging.Value)
			{
				ReusableMetadataPlugin.logger.LogInfo((object)$"PropertyLogic_UpdateProduction_Patch count={num5}");
				ReusableMetadataPlugin.logger.LogInfo((object)$"PropertyLogic_UpdateProduction_Patch num={num2}");
				ReusableMetadataPlugin.logger.LogInfo((object)$"PropertyLogic_UpdateProduction_Patch minimalPropertyMultiplier={num4}");
			}
		}
		return false;
	}
}