Decompiled source of ModdedPurchaseKeeper v0.1.1

ModdedPurchaseKeeper.dll

Decompiled a day ago
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Reflection;
using System.Runtime.CompilerServices;
using System.Runtime.Versioning;
using BepInEx;
using BepInEx.Logging;
using HarmonyLib;
using Microsoft.CodeAnalysis;
using UnityEngine;

[assembly: CompilationRelaxations(8)]
[assembly: RuntimeCompatibility(WrapNonExceptionThrows = true)]
[assembly: Debuggable(DebuggableAttribute.DebuggingModes.IgnoreSymbolStoreSequencePoints)]
[assembly: TargetFramework(".NETStandard,Version=v2.1", FrameworkDisplayName = ".NET Standard 2.1")]
[assembly: AssemblyCompany("ModdedPurchaseKeeper")]
[assembly: AssemblyConfiguration("Release")]
[assembly: AssemblyFileVersion("0.1.1.0")]
[assembly: AssemblyInformationalVersion("0.1.1+ecb08dd852e56f65d68682930741c791da279522")]
[assembly: AssemblyProduct("ModdedPurchaseKeeper")]
[assembly: AssemblyTitle("ModdedPurchaseKeeper")]
[assembly: AssemblyVersion("0.1.1.0")]
[module: RefSafetyRules(11)]
namespace Microsoft.CodeAnalysis
{
	[CompilerGenerated]
	[Embedded]
	internal sealed class EmbeddedAttribute : Attribute
	{
	}
}
namespace System.Runtime.CompilerServices
{
	[CompilerGenerated]
	[Embedded]
	[AttributeUsage(AttributeTargets.Class | AttributeTargets.Property | AttributeTargets.Field | AttributeTargets.Event | AttributeTargets.Parameter | AttributeTargets.ReturnValue | AttributeTargets.GenericParameter, AllowMultiple = false, Inherited = false)]
	internal sealed class NullableAttribute : Attribute
	{
		public readonly byte[] NullableFlags;

		public NullableAttribute(byte P_0)
		{
			NullableFlags = new byte[1] { P_0 };
		}

		public NullableAttribute(byte[] P_0)
		{
			NullableFlags = P_0;
		}
	}
	[CompilerGenerated]
	[Embedded]
	[AttributeUsage(AttributeTargets.Class | AttributeTargets.Struct | AttributeTargets.Method | AttributeTargets.Interface | AttributeTargets.Delegate, AllowMultiple = false, Inherited = false)]
	internal sealed class NullableContextAttribute : Attribute
	{
		public readonly byte Flag;

		public NullableContextAttribute(byte P_0)
		{
			Flag = P_0;
		}
	}
	[CompilerGenerated]
	[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 ModdedPurchaseKeeper
{
	public static class LedgerLogic
	{
		public static void RecordCount(Dictionary<string, int> ledger, string itemName, int count)
		{
			ledger[itemName] = count;
		}

		public static void SyncConsumption(Dictionary<string, int> ledger, string itemName, int? gameCountAfter)
		{
			if (!ledger.ContainsKey(itemName))
			{
				return;
			}
			if (gameCountAfter.HasValue)
			{
				int valueOrDefault = gameCountAfter.GetValueOrDefault();
				if (valueOrDefault > 0)
				{
					ledger[itemName] = valueOrDefault;
					return;
				}
			}
			ledger.Remove(itemName);
		}

		public static int? ReassertValue(int ledgerCount, bool isRegistered, int currentCount)
		{
			if (ledgerCount <= 0 || !isRegistered || currentCount >= ledgerCount)
			{
				return null;
			}
			return ledgerCount;
		}

		public static string PurchaseKeyFromInstanceName(string instanceName)
		{
			return instanceName.Split('/')[0];
		}
	}
	[BepInPlugin("darkharasho.ModdedPurchaseKeeper", "ModdedPurchaseKeeper", "0.1.1")]
	[BepInDependency(/*Could not decode attribute arguments.*/)]
	public class Plugin : BaseUnityPlugin
	{
		internal static ManualLogSource Log = null;

		internal static readonly Dictionary<string, int> Ledger = new Dictionary<string, int>();

		private void Awake()
		{
			//IL_0010: Unknown result type (might be due to invalid IL or missing references)
			Log = ((BaseUnityPlugin)this).Logger;
			new Harmony("darkharasho.ModdedPurchaseKeeper").PatchAll();
			Log.LogInfo((object)"ModdedPurchaseKeeper v0.1.1 loaded — protecting modded purchases.");
		}
	}
	[HarmonyPatch(typeof(StatsManager), "ItemPurchase")]
	internal static class Track_ItemPurchase
	{
		private static void Postfix(string itemName)
		{
			StatsManager instance = StatsManager.instance;
			if (!((Object)(object)instance == (Object)null))
			{
				LedgerLogic.RecordCount(Plugin.Ledger, itemName, instance.itemsPurchased.TryGetValue(itemName, out var value) ? value : 0);
			}
		}
	}
	[HarmonyPatch(typeof(StatsManager), "SetItemPurchase")]
	internal static class Track_SetItemPurchase
	{
		private static void Postfix(Item _item, int value)
		{
			if ((Object)(object)_item != (Object)null)
			{
				LedgerLogic.RecordCount(Plugin.Ledger, ((Object)_item).name, value);
			}
		}
	}
	[HarmonyPatch(typeof(ItemUpgrade), "PlayerUpgrade")]
	internal static class Sync_PlayerUpgradeConsumption
	{
		private static readonly FieldRef<ItemUpgrade, ItemAttributes> ItemAttributesRef = AccessTools.FieldRefAccess<ItemUpgrade, ItemAttributes>("itemAttributes");

		private static string? KeyOf(ItemUpgrade upgrade)
		{
			ItemAttributes val = ItemAttributesRef.Invoke(upgrade);
			Item val2 = (((Object)(object)val != (Object)null) ? val.item : null);
			if (!((Object)(object)val2 != (Object)null))
			{
				return null;
			}
			return ((Object)val2).name;
		}

		private static int? Count(string key)
		{
			StatsManager instance = StatsManager.instance;
			if ((Object)(object)instance == (Object)null)
			{
				return null;
			}
			if (!instance.itemsPurchased.TryGetValue(key, out var value))
			{
				return null;
			}
			return value;
		}

		private static void Prefix(ItemUpgrade __instance, ref int? __state)
		{
			string text = KeyOf(__instance);
			__state = ((text != null) ? Count(text) : ((int?)null));
		}

		private static void Postfix(ItemUpgrade __instance, int? __state)
		{
			string text = KeyOf(__instance);
			if (text != null && !((Object)(object)StatsManager.instance == (Object)null))
			{
				int? num = Count(text);
				if (num != __state)
				{
					LedgerLogic.SyncConsumption(Plugin.Ledger, text, num);
				}
			}
		}
	}
	[HarmonyPatch(typeof(StatsManager), "ItemRemove")]
	internal static class Sync_ItemRemoveConsumption
	{
		private static int? Count(StatsManager sm, string key)
		{
			if (!sm.itemsPurchased.TryGetValue(key, out var value))
			{
				return null;
			}
			return value;
		}

		private static void Prefix(StatsManager __instance, string instanceName, ref int? __state)
		{
			__state = Count(__instance, LedgerLogic.PurchaseKeyFromInstanceName(instanceName));
		}

		private static void Postfix(StatsManager __instance, string instanceName, int? __state)
		{
			string text = LedgerLogic.PurchaseKeyFromInstanceName(instanceName);
			int? num = Count(__instance, text);
			if (num != __state)
			{
				LedgerLogic.SyncConsumption(Plugin.Ledger, text, num);
			}
		}
	}
	[HarmonyPatch(typeof(StatsManager), "ResetAllStats")]
	internal static class Clear_OnReset
	{
		private static void Postfix()
		{
			if (Plugin.Ledger.Count > 0)
			{
				Plugin.Ledger.Clear();
			}
		}
	}
	[HarmonyPatch(typeof(ItemManager), "GetPurchasedItems")]
	internal static class Reassert_BeforeTruck
	{
		private static void Prefix()
		{
			StatsManager instance = StatsManager.instance;
			if ((Object)(object)instance == (Object)null || Plugin.Ledger.Count == 0)
			{
				return;
			}
			foreach (KeyValuePair<string, int> item in Plugin.Ledger)
			{
				int value;
				int currentCount = (instance.itemsPurchased.TryGetValue(item.Key, out value) ? value : 0);
				int? num = LedgerLogic.ReassertValue(item.Value, instance.itemDictionary.ContainsKey(item.Key), currentCount);
				if (num.HasValue)
				{
					int valueOrDefault = num.GetValueOrDefault();
					instance.itemsPurchased[item.Key] = valueOrDefault;
				}
			}
		}
	}
	public static class PluginInfo
	{
		public const string PLUGIN_GUID = "darkharasho.ModdedPurchaseKeeper";

		public const string PLUGIN_NAME = "ModdedPurchaseKeeper";

		public const string PLUGIN_VERSION = "0.1.1";
	}
}