Decompiled source of UpgradeLib v0.0.1

UpgradeLib.dll

Decompiled 2 months ago
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Linq;
using System.Reflection;
using System.Runtime.CompilerServices;
using System.Runtime.Versioning;
using System.Security;
using System.Security.Permissions;
using BepInEx;
using BepInEx.Logging;
using HarmonyLib;
using Microsoft.CodeAnalysis;
using UnityEngine;
using UpgradeLib;

[assembly: CompilationRelaxations(8)]
[assembly: RuntimeCompatibility(WrapNonExceptionThrows = true)]
[assembly: Debuggable(DebuggableAttribute.DebuggingModes.Default | DebuggableAttribute.DebuggingModes.DisableOptimizations | DebuggableAttribute.DebuggingModes.IgnoreSymbolStoreSequencePoints | DebuggableAttribute.DebuggingModes.EnableEditAndContinue)]
[assembly: TargetFramework(".NETStandard,Version=v2.1", FrameworkDisplayName = ".NET Standard 2.1")]
[assembly: AssemblyCompany("UpgradeLib")]
[assembly: AssemblyConfiguration("Debug")]
[assembly: AssemblyFileVersion("0.0.1.0")]
[assembly: AssemblyInformationalVersion("0.0.1")]
[assembly: AssemblyProduct("UpgradeLib")]
[assembly: AssemblyTitle("UpgradeLib")]
[assembly: SecurityPermission(SecurityAction.RequestMinimum, SkipVerification = true)]
[assembly: AssemblyVersion("0.0.1.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;
		}
	}
}
[BepInPlugin("eu.xaru.upgradelib", "UpgradeLib", "0.0.1")]
public class UpgradeLibPlugin : BaseUnityPlugin
{
	private const string PluginGuid = "eu.xaru.upgradelib";

	private const string PluginName = "UpgradeLib";

	private const string PluginVersion = "0.0.1";

	private static ManualLogSource _sLOG;

	private void Awake()
	{
		_sLOG = ((BaseUnityPlugin)this).Logger;
		_sLOG.LogInfo((object)"UpgradeLib v0.0.1 is loading...");
		Object.DontDestroyOnLoad((Object)(object)((Component)this).gameObject);
		UpgradeLibManager.Init("eu.xaru.upgradeLib");
		_sLOG.LogInfo((object)"UpgradeLib Awoke.");
	}

	private void OnDestroy()
	{
		_sLOG.LogInfo((object)"UpgradeLib is unloading... Goodbye!");
	}
}
namespace UpgradeLib
{
	public interface ICustomItem
	{
		int Id { get; }

		string InternalName { get; }

		string Title { get; }

		string Description { get; }

		int Price { get; }

		bool Stackable { get; }
	}
	public class UpgradeDefinition : ICustomItem
	{
		public int Id { get; }

		public string InternalName { get; }

		public string Title { get; }

		public string Description { get; }

		public int Price { get; }

		public bool Stackable { get; }

		public Action<bool> OnPurchased { get; }

		public Action OnLockedAllUpgrades { get; }

		public UpgradeDefinition(int id, string internalName, string title, string description, int price, bool stackable = false, Action<bool> onPurchased = null, Action onLockedAllUpgrades = null)
		{
			Id = id;
			InternalName = internalName;
			Title = title;
			Description = description;
			Price = price;
			Stackable = stackable;
			OnPurchased = onPurchased;
			OnLockedAllUpgrades = onLockedAllUpgrades;
		}
	}
	public static class UpgradeLibManager
	{
		private static bool s_initialized;

		private static Harmony s_harmonyInstance;

		private static bool s_patchesApplied;

		private static readonly Dictionary<int, UpgradeDefinition> s_upgradeDefs = new Dictionary<int, UpgradeDefinition>();

		public static void Init(string harmonyId)
		{
			//IL_0020: Unknown result type (might be due to invalid IL or missing references)
			//IL_002a: Expected O, but got Unknown
			if (s_initialized)
			{
				Debug.Log((object)"[UpgradeLib] Already initialized. Skipping re-init.");
				return;
			}
			s_initialized = true;
			try
			{
				s_harmonyInstance = new Harmony(harmonyId);
				s_harmonyInstance.PatchAll(Assembly.GetExecutingAssembly());
				s_patchesApplied = true;
				Debug.Log((object)("[UpgradeLib] Harmony patches applied. ID=" + harmonyId));
			}
			catch (Exception arg)
			{
				Debug.LogError((object)$"[UpgradeLib] Patch failure: {arg}");
			}
		}

		public static void RegisterUpgrade(UpgradeDefinition def)
		{
			if (def == null)
			{
				Debug.LogWarning((object)"[UpgradeLib] Tried to register null UpgradeDefinition.");
				return;
			}
			if (s_upgradeDefs.ContainsKey(def.Id))
			{
				Debug.LogWarning((object)$"[UpgradeLib] Upgrade ID={def.Id} already exists. Skipping.");
				return;
			}
			s_upgradeDefs[def.Id] = def;
			Debug.Log((object)$"[UpgradeLib] Registered upgrade: {def.InternalName} (ID={def.Id}).");
			InjectIntoUpgradeManager(def);
		}

		public static UpgradeDefinition AttemptPurchaseUpgrade(string internalName)
		{
			UpgradeDefinition upgradeDefinition = s_upgradeDefs.Values.FirstOrDefault((UpgradeDefinition u) => u.InternalName.Equals(internalName, StringComparison.OrdinalIgnoreCase));
			if (upgradeDefinition == null)
			{
				Debug.LogWarning((object)("[UpgradeLib] No such upgrade: " + internalName));
				return null;
			}
			if (!upgradeDefinition.Stackable && IsUpgradeUnlocked((Upgrades)upgradeDefinition.Id))
			{
				Debug.Log((object)("[UpgradeLib] " + internalName + " is already unlocked. Skipping re-purchase."));
				return null;
			}
			int num = (Object.op_Implicit((Object)(object)GameController.instance) ? GameController.instance.balance : 0);
			if (num < upgradeDefinition.Price)
			{
				Debug.LogWarning((object)"[UpgradeLib] Not enough money to buy upgrade.");
				return null;
			}
			GameController.instance.ChangeBalance(-upgradeDefinition.Price, false);
			Debug.Log((object)$"[UpgradeLib] Purchased {upgradeDefinition.InternalName} for {upgradeDefinition.Price}.");
			UpgradeManager.instance.UnlockLocal((Upgrades)upgradeDefinition.Id);
			return upgradeDefinition;
		}

		public static void OnUpgradeManagerReady()
		{
			foreach (UpgradeDefinition value in s_upgradeDefs.Values)
			{
				InjectIntoUpgradeManager(value);
			}
		}

		public static void OnAllUpgradesLocked()
		{
			foreach (UpgradeDefinition value in s_upgradeDefs.Values)
			{
				value.OnLockedAllUpgrades?.Invoke();
			}
		}

		internal static UpgradeDefinition GetUpgradeById(int id)
		{
			s_upgradeDefs.TryGetValue(id, out var value);
			return value;
		}

		private static bool IsUpgradeUnlocked(Upgrades upg)
		{
			//IL_001e: Unknown result type (might be due to invalid IL or missing references)
			//IL_0032: Unknown result type (might be due to invalid IL or missing references)
			UpgradeManager instance = UpgradeManager.instance;
			if (!Object.op_Implicit((Object)(object)instance))
			{
				return false;
			}
			if (instance.unlockedUpgrades.Contains(upg))
			{
				return true;
			}
			if (instance.stackedUpgrades.ContainsKey(upg))
			{
				return true;
			}
			return false;
		}

		private static void InjectIntoUpgradeManager(UpgradeDefinition def)
		{
			//IL_011e: Unknown result type (might be due to invalid IL or missing references)
			UpgradeManager instance = UpgradeManager.instance;
			if (!Object.op_Implicit((Object)(object)instance) || (Object)(object)instance.catalog == (Object)null || instance.catalog.items == null)
			{
				Debug.Log((object)("[UpgradeLib] Manager not ready. Delaying: " + def.InternalName));
				return;
			}
			if (instance.catalog.items.Any((UpgradeShopItem u) => u.upgradeName.Equals(def.InternalName, StringComparison.OrdinalIgnoreCase)))
			{
				Debug.Log((object)("[UpgradeLib] " + def.InternalName + " is already in catalog. Skipping add."));
				return;
			}
			UpgradeShopItem val = ScriptableObject.CreateInstance<UpgradeShopItem>();
			val.upgradeName = def.InternalName;
			val.title = def.Title;
			val.description = def.Description;
			val.price = def.Price;
			val.stackable = def.Stackable;
			val.stackPriceMultiplier = 1f;
			val.upgrade = (Upgrades)def.Id;
			List<UpgradeShopItem> list = instance.catalog.items.ToList();
			list.Add(val);
			instance.catalog.items = list.ToArray();
			Debug.Log((object)$"[UpgradeLib] Injected {def.InternalName} => (ID={def.Id}).");
		}
	}
	[HarmonyPatch(typeof(GameController))]
	public static class PatchGameController
	{
		[HarmonyPostfix]
		[HarmonyPatch("Start")]
		public static void Postfix_Start()
		{
			Debug.Log((object)"[UpgradeLib] Checking injection after GameController.Start...");
			UpgradeLibManager.OnUpgradeManagerReady();
		}
	}
	[HarmonyPatch(typeof(UpgradeManager))]
	public static class PatchUpgradeManager
	{
		[HarmonyPrefix]
		[HarmonyPatch("UnlockMethod", new Type[]
		{
			typeof(int),
			typeof(bool)
		})]
		public static bool Prefix_UnlockMethod(UpgradeManager __instance, int upgrade, bool fromSave)
		{
			//IL_003c: Unknown result type (might be due to invalid IL or missing references)
			//IL_003f: Unknown result type (might be due to invalid IL or missing references)
			//IL_0048: Unknown result type (might be due to invalid IL or missing references)
			UpgradeDefinition upgradeById = UpgradeLibManager.GetUpgradeById(upgrade);
			if (upgradeById == null)
			{
				return true;
			}
			if (upgradeById.Stackable)
			{
				if (__instance.stackedUpgrades.ContainsKey((Upgrades)upgrade))
				{
					__instance.stackedUpgrades[(Upgrades)upgrade]++;
				}
				else
				{
					__instance.stackedUpgrades.Add((Upgrades)upgrade, 1);
				}
			}
			else
			{
				__instance.unlockedUpgrades.Add((Upgrades)upgrade);
			}
			if (!fromSave && Object.op_Implicit((Object)(object)UIRewardPanel.instance))
			{
				UIRewardPanel.instance.Show(upgradeById.Title, upgradeById.Description, 1);
			}
			Debug.Log((object)$"[UpgradeLib] Intercepting custom ID={upgrade} => {upgradeById.InternalName}. Skipping original.");
			upgradeById.OnPurchased?.Invoke(fromSave);
			return false;
		}
	}
	[HarmonyPatch(typeof(UpgradeManager))]
	public static class PatchLockAll
	{
		[HarmonyPostfix]
		[HarmonyPatch("LockAllUpgrades")]
		public static void Postfix_LockAllUpgrades()
		{
			Debug.Log((object)"[UpgradeLib] LockAllUpgrades => calling OnAllUpgradesLocked.");
			UpgradeLibManager.OnAllUpgradesLocked();
		}
	}
	public static class MyPluginInfo
	{
		public const string PLUGIN_GUID = "UpgradeLib";

		public const string PLUGIN_NAME = "UpgradeLib";

		public const string PLUGIN_VERSION = "0.0.1";
	}
}