Decompiled source of SharedUpgrades v2.1.3

SharedUpgrades.dll

Decompiled 2 weeks 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 Photon.Pun;
using SharedUpgrades.Core;

[assembly: CompilationRelaxations(8)]
[assembly: RuntimeCompatibility(WrapNonExceptionThrows = true)]
[assembly: Debuggable(DebuggableAttribute.DebuggingModes.Default | DebuggableAttribute.DebuggingModes.DisableOptimizations | DebuggableAttribute.DebuggingModes.IgnoreSymbolStoreSequencePoints | DebuggableAttribute.DebuggingModes.EnableEditAndContinue)]
[assembly: TargetFramework(".NETFramework,Version=v4.8", FrameworkDisplayName = ".NET Framework 4.8")]
[assembly: AssemblyCompany("SharedUpgrades")]
[assembly: AssemblyConfiguration("Debug")]
[assembly: AssemblyFileVersion("1.0.0.0")]
[assembly: AssemblyInformationalVersion("1.0.0")]
[assembly: AssemblyProduct("Shared Upgrades")]
[assembly: AssemblyTitle("SharedUpgrades")]
[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 SharedUpgrades
{
	[BepInPlugin("Traktool.SharedUpgrades", "Shared Upgrades", "2.1.3")]
	public class SharedUpgrades : BaseUnityPlugin
	{
		public const string PluginGUID = "Traktool.SharedUpgrades";

		public const string PluginName = "Shared Upgrades";

		public const string PluginVersion = "2.1.3";

		internal static ManualLogSource Logger;

		private void Awake()
		{
			//IL_0021: Unknown result type (might be due to invalid IL or missing references)
			//IL_0027: Expected O, but got Unknown
			Logger = ((BaseUnityPlugin)this).Logger;
			Logger.LogInfo((object)"Plugin Shared Upgrades v2.1.3 is loaded!");
			Harmony val = new Harmony("Traktool.SharedUpgrades");
			val.PatchAll();
			LogAllPatches();
		}

		private void LogAllPatches()
		{
			List<MethodBase> list = Harmony.GetAllPatchedMethods().ToList();
			if (!list.Any())
			{
				Logger.LogDebug((object)"No patches were found.");
				return;
			}
			foreach (MethodBase item in list)
			{
				Patches patchInfo = Harmony.GetPatchInfo(item);
				if (patchInfo == null)
				{
					continue;
				}
				List<Patch> list2 = patchInfo.Prefixes.Where((Patch p) => p.owner == "Traktool.SharedUpgrades").ToList();
				List<Patch> list3 = patchInfo.Postfixes.Where((Patch p) => p.owner == "Traktool.SharedUpgrades").ToList();
				List<Patch> list4 = patchInfo.Transpilers.Where((Patch p) => p.owner == "Traktool.SharedUpgrades").ToList();
				List<Patch> list5 = patchInfo.Finalizers.Where((Patch p) => p.owner == "Traktool.SharedUpgrades").ToList();
				if (list2.Count == 0 && list3.Count == 0 && list4.Count == 0 && list5.Count == 0)
				{
					continue;
				}
				string text = item.DeclaringType?.FullName + ":" + item.Name;
				Logger.LogDebug((object)("Method patched by Traktool.SharedUpgrades: " + text));
				foreach (Patch item2 in list2)
				{
					Logger.LogDebug((object)("  -> [Prefix] (patch method: " + item2.PatchMethod.Name + ")"));
				}
				foreach (Patch item3 in list3)
				{
					Logger.LogDebug((object)("  -> [Postfix] (patch method: " + item3.PatchMethod.Name + ")"));
				}
				foreach (Patch item4 in list4)
				{
					Logger.LogDebug((object)("  -> [Transpiler] (patch method: " + item4.PatchMethod.Name + ")"));
				}
				foreach (Patch item5 in list5)
				{
					Logger.LogDebug((object)("  -> [Finalizer] (patch method: " + item5.PatchMethod.Name + ")"));
				}
			}
		}
	}
	public static class MyPluginInfo
	{
		public const string PLUGIN_GUID = "SharedUpgrades";

		public const string PLUGIN_NAME = "Shared Upgrades";

		public const string PLUGIN_VERSION = "1.0.0";
	}
}
namespace SharedUpgrades.Patches
{
	[HarmonyPatch(typeof(RunManager))]
	public static class RunManagerChangeLevelPatch
	{
		[HarmonyPatch("ChangeLevel")]
		[HarmonyPrefix]
		private static void ChangeLevel_Prefix()
		{
			UpgradesSynchronizer.SynchronizeUpgrades(forced: true);
		}
	}
	[HarmonyPatch(typeof(StatsManager))]
	public static class RunManagerResetProgressPatch
	{
		[HarmonyPatch("ResetAllStats")]
		[HarmonyPostfix]
		private static void ResetAllStats_Postfix()
		{
			UpgradesSynchronizer.ResetPreviousMaxValues();
		}
	}
	[HarmonyPatch(typeof(StatsManager))]
	public static class StatsManagerUpdatePatch
	{
		[HarmonyPatch("Update")]
		[HarmonyPrefix]
		private static void Update_Prefix()
		{
			UpgradesSynchronizer.SynchronizeUpgrades();
		}
	}
}
namespace SharedUpgrades.Core
{
	internal static class UpgradesSynchronizer
	{
		private static Dictionary<string, int> previousMaxValues = new Dictionary<string, int>();

		internal static void SynchronizeUpgrades(bool forced = false)
		{
			try
			{
				if (!LevelGenerator.Instance.Generated || SemiFunc.MenuLevel() || !PhotonNetwork.IsMasterClient)
				{
					return;
				}
				StatsManager instance = StatsManager.instance;
				Dictionary<string, Dictionary<string, int>> dictionaryOfDictionaries = instance.dictionaryOfDictionaries;
				List<string> list = dictionaryOfDictionaries.Keys.Where((string key) => key.StartsWith("playerUpgrade")).ToList();
				SharedUpgrades.Logger.LogDebug((object)("Found upgrade keys: " + string.Join(", ", list)));
				Dictionary<string, int> dictionary = new Dictionary<string, int>();
				foreach (string item in list)
				{
					if (dictionaryOfDictionaries.TryGetValue(item, out var value) && value.Count > 0)
					{
						int num2 = (dictionary[item] = value.Values.Max());
						SharedUpgrades.Logger.LogDebug((object)$"Calculated max value for {item}: {num2}");
					}
					else
					{
						SharedUpgrades.Logger.LogDebug((object)("No upgrades found for key " + item + "."));
					}
				}
				bool flag = false;
				if (!forced)
				{
					SharedUpgrades.Logger.LogDebug((object)("Current max values: " + string.Join(", ", dictionary)));
					SharedUpgrades.Logger.LogDebug((object)("Previous max values: " + string.Join(", ", previousMaxValues)));
					if (dictionary.Any((KeyValuePair<string, int> kv) => !previousMaxValues.TryGetValue(kv.Key, out var value2) || value2 != kv.Value))
					{
						SharedUpgrades.Logger.LogDebug((object)"Upgrade values have changed, applying updates.");
						flag = true;
					}
					else
					{
						SharedUpgrades.Logger.LogDebug((object)"No upgrade values changed, skipping update.");
					}
				}
				else
				{
					SharedUpgrades.Logger.LogDebug((object)"Forced upgrades synchronization due to level change.");
					flag = true;
				}
				if (!flag)
				{
					return;
				}
				foreach (KeyValuePair<string, int> item2 in dictionary)
				{
					SharedUpgrades.Logger.LogDebug((object)$"Updating upgrade {item2.Key} with value {item2.Value}.");
					instance.DictionaryFill(item2.Key, item2.Value);
				}
				if (!forced)
				{
					SemiFunc.StatSyncAll();
					SharedUpgrades.Logger.LogDebug((object)"StatSyncAll called.");
				}
				previousMaxValues = new Dictionary<string, int>(dictionary);
				SharedUpgrades.Logger.LogDebug((object)"Updated previousMaxValues for next iteration.");
				SharedUpgrades.Logger.LogInfo((object)"Upgrades synced for all players.");
			}
			catch (Exception ex)
			{
				LogException(ex);
				throw;
			}
		}

		internal static void ResetPreviousMaxValues()
		{
			if (PhotonNetwork.IsMasterClient)
			{
				previousMaxValues.Clear();
				SharedUpgrades.Logger.LogInfo((object)"Previous max upgrades values reseted.");
			}
		}

		private static void LogException(Exception ex)
		{
			if (ex != null)
			{
				SharedUpgrades.Logger.LogDebug((object)("Exception caught: " + ex.GetType().FullName + " - " + ex.Message + "\nStackTrace: " + ex.StackTrace));
				if (ex.InnerException != null)
				{
					SharedUpgrades.Logger.LogDebug((object)"Inner exception:");
					LogException(ex.InnerException);
				}
			}
		}
	}
}