Decompiled source of SyncUpgrades v2.0.7

SyncUpgrades.dll

Decompiled 2 weeks ago
using System;
using System.Collections.Concurrent;
using System.Collections.Generic;
using System.Diagnostics;
using System.Linq;
using System.Reflection;
using System.Runtime.CompilerServices;
using System.Runtime.Versioning;
using BepInEx;
using BepInEx.Configuration;
using BepInEx.Logging;
using ExitGames.Client.Photon;
using HarmonyLib;
using JetBrains.Annotations;
using Microsoft.CodeAnalysis;
using Photon.Pun;
using Photon.Realtime;
using REPOLib.Modules;
using SyncUpgrades.Core;
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("SyncUpgrades")]
[assembly: AssemblyConfiguration("Release")]
[assembly: AssemblyFileVersion("2.0.7.0")]
[assembly: AssemblyInformationalVersion("2.0.7+7e4c512114c96f11b20b6b4cf0ff8809bc8a3c63")]
[assembly: AssemblyProduct("SyncUpgrades")]
[assembly: AssemblyTitle("SyncUpgrades")]
[assembly: AssemblyVersion("2.0.7.0")]
[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.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]
	[Microsoft.CodeAnalysis.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]
	[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;
		}
	}
	public static class IsExternalInit
	{
	}
	[AttributeUsage(AttributeTargets.Constructor | AttributeTargets.Property, Inherited = false)]
	public sealed class RequiredMemberAttribute : Attribute
	{
	}
	[AttributeUsage(AttributeTargets.Class | AttributeTargets.Struct, Inherited = false)]
	public sealed class CompilerFeatureRequiredAttribute : Attribute
	{
		public CompilerFeatureRequiredAttribute(string featureName)
		{
		}
	}
}
namespace SyncUpgrades
{
	[BepInPlugin("TGO.SyncUpgrades", "Sync Upgrades", "2.0.7")]
	[BepInDependency("REPOLib", "2.1.0")]
	public class Entry : BaseUnityPlugin
	{
		private const string PluginName = "Sync Upgrades";

		private const string PluginVersion = "2.0.7";

		private const string PluginId = "TGO.SyncUpgrades";

		private static readonly Harmony Harmony = new Harmony("TGO.SyncUpgrades");

		internal static readonly ManualLogSource LogSource = Logger.CreateLogSource("Sync Upgrades");

		private static Entry? _instance;

		internal static ConfigFile BepConfig => ((BaseUnityPlugin)_instance).Config;

		private void Awake()
		{
			_instance = this;
			SyncManager.Init();
			Harmony.PatchAll();
			((Object)((Component)this).gameObject).hideFlags = (HideFlags)4;
			LogSource.LogInfo((object)"Sync Upgrades loaded!");
		}

		private void FixedUpdate()
		{
			SyncUtil.RunOneRPC();
		}
	}
}
namespace SyncUpgrades.Patches
{
	[HarmonyPatch(typeof(PunManager))]
	public class PunManagerPatch
	{
		[HarmonyPrefix]
		[HarmonyPatch("UpdateHealthRightAway", new Type[] { typeof(string) })]
		private static void UpdateHealthRightAway(PunManager __instance, PhotonView ___photonView, StatsManager ___statsManager, string playerName)
		{
			if (!SemiFunc.IsNotMasterClient())
			{
				SyncManager.PlayerConsumedUpgrade(new SyncBundle(__instance, ___photonView, ___statsManager, playerName), SyncUtil.HealthId);
			}
		}

		[HarmonyPrefix]
		[HarmonyPatch("UpdateEnergyRightAway", new Type[] { typeof(string) })]
		private static void UpdateEnergyRightAway(PunManager __instance, PhotonView ___photonView, StatsManager ___statsManager, string _steamID)
		{
			if (!SemiFunc.IsNotMasterClient())
			{
				SyncManager.PlayerConsumedUpgrade(new SyncBundle(__instance, ___photonView, ___statsManager, _steamID), SyncUtil.StaminaId);
			}
		}

		[HarmonyPrefix]
		[HarmonyPatch("UpdateTumbleLaunchRightAway", new Type[] { typeof(string) })]
		private static void UpdateTumbleLaunchRightAway(PunManager __instance, PhotonView ___photonView, StatsManager ___statsManager, string _steamID)
		{
			if (!SemiFunc.IsNotMasterClient())
			{
				SyncManager.PlayerConsumedUpgrade(new SyncBundle(__instance, ___photonView, ___statsManager, _steamID), SyncUtil.TumbleLaunchId);
			}
		}

		[HarmonyPrefix]
		[HarmonyPatch("UpdateSprintSpeedRightAway", new Type[] { typeof(string) })]
		private static void UpdateSprintSpeedRightAway(PunManager __instance, PhotonView ___photonView, StatsManager ___statsManager, string _steamID)
		{
			if (!SemiFunc.IsNotMasterClient())
			{
				SyncManager.PlayerConsumedUpgrade(new SyncBundle(__instance, ___photonView, ___statsManager, _steamID), SyncUtil.SprintSpeedId);
			}
		}

		[HarmonyPrefix]
		[HarmonyPatch("UpdateGrabStrengthRightAway", new Type[] { typeof(string) })]
		private static void UpdateGrabStrengthRightAway(PunManager __instance, PhotonView ___photonView, StatsManager ___statsManager, string _steamID)
		{
			if (!SemiFunc.IsNotMasterClient())
			{
				SyncManager.PlayerConsumedUpgrade(new SyncBundle(__instance, ___photonView, ___statsManager, _steamID), SyncUtil.GrabStrengthId);
			}
		}

		[HarmonyPrefix]
		[HarmonyPatch("UpdateThrowStrengthRightAway", new Type[] { typeof(string) })]
		private static void UpdateThrowStrengthRightAway(PunManager __instance, PhotonView ___photonView, StatsManager ___statsManager, string _steamID)
		{
			if (!SemiFunc.IsNotMasterClient())
			{
				SyncManager.PlayerConsumedUpgrade(new SyncBundle(__instance, ___photonView, ___statsManager, _steamID), SyncUtil.GrabThrowId);
			}
		}

		[HarmonyPrefix]
		[HarmonyPatch("UpdateGrabRangeRightAway", new Type[] { typeof(string) })]
		private static void UpdateGrabRangeRightAway(PunManager __instance, PhotonView ___photonView, StatsManager ___statsManager, string _steamID)
		{
			if (!SemiFunc.IsNotMasterClient())
			{
				SyncManager.PlayerConsumedUpgrade(new SyncBundle(__instance, ___photonView, ___statsManager, _steamID), SyncUtil.GrabRangeId);
			}
		}

		[HarmonyPrefix]
		[HarmonyPatch("UpdateExtraJumpRightAway", new Type[] { typeof(string) })]
		private static void UpdateExtraJumpRightAway(PunManager __instance, PhotonView ___photonView, StatsManager ___statsManager, string _steamID)
		{
			if (!SemiFunc.IsNotMasterClient())
			{
				SyncManager.PlayerConsumedUpgrade(new SyncBundle(__instance, ___photonView, ___statsManager, _steamID), SyncUtil.ExtraJumpId);
			}
		}

		[HarmonyPrefix]
		[HarmonyPatch("UpdateMapPlayerCountRightAway", new Type[] { typeof(string) })]
		private static void UpdateMapPlayerCountRightAway(PunManager __instance, PhotonView ___photonView, StatsManager ___statsManager, string _steamID)
		{
			if (!SemiFunc.IsNotMasterClient())
			{
				SyncManager.PlayerConsumedUpgrade(new SyncBundle(__instance, ___photonView, ___statsManager, _steamID), SyncUtil.MapPlayerCountId);
			}
		}
	}
	[HarmonyPatch(typeof(RunManager))]
	internal class RunManagerPatch
	{
		[HarmonyPostfix]
		[HarmonyPatch("ChangeLevel")]
		private static void ChangeLevel(bool _completedLevel, bool _levelFailed, bool ___restarting)
		{
			if (!(SemiFunc.IsNotMasterClient() || _levelFailed))
			{
				SyncManager.SyncHostToAll();
			}
		}
	}
	[HarmonyPatch(typeof(StatsManager))]
	internal class StatsManagerPatch
	{
		[HarmonyPostfix]
		[HarmonyPatch("PlayerAdd", new Type[]
		{
			typeof(string),
			typeof(string)
		})]
		private static void PlayerAdd(string _steamID, string _playerName)
		{
			if (!SemiFunc.IsNotMasterClient() && !SemiFunc.MenuLevel())
			{
				SyncManager.SyncHostToTarget(_steamID);
			}
		}
	}
	[HarmonyPatch(typeof(Upgrades))]
	public class UpgradesPatch
	{
		[HarmonyPostfix]
		[HarmonyPatch("HandleUpgradeEvent")]
		private static void HandleUpgradeEvent(EventData eventData)
		{
			if (SemiFunc.IsNotMasterClient())
			{
				return;
			}
			object customData = eventData.CustomData;
			Hashtable val = (Hashtable)((customData is Hashtable) ? customData : null);
			if (val != null)
			{
				string text = (string)val[(object)"UpgradeId"];
				string steamId = (string)val[(object)"SteamId"];
				int newLevel = (int)val[(object)"Level"];
				PlayerUpgrade val2 = default(PlayerUpgrade);
				if (Upgrades.TryGetUpgrade(text, ref val2))
				{
					string rawName = SyncUtil.FixKey(text);
					SyncManager.PlayerConsumedUpgrade(steamId, UpgradeId.New(rawName), newLevel);
				}
			}
		}

		[HarmonyPostfix]
		[HarmonyPatch("RaiseUpgradeEvent")]
		private static void RaiseUpgradeEvent(string upgradeId, string steamId, int level)
		{
			if (!SemiFunc.IsNotMasterClient() && !(steamId != SyncUtil.HostSteamId))
			{
				string rawName = SyncUtil.FixKey(upgradeId);
				SyncManager.PlayerConsumedUpgrade(steamId, UpgradeId.New(rawName), level);
			}
		}
	}
}
namespace SyncUpgrades.Core
{
	public static class Extensions
	{
		public static string SteamId(this PlayerAvatar avatar)
		{
			return SemiFunc.PlayerGetSteamID(avatar);
		}

		public static PhotonView GetView(this PunManager instance)
		{
			return ((Component)instance).GetComponent<PhotonView>();
		}

		public static string ToName(this UpgradeType key)
		{
			return SyncUtil.GetUpgradeName(key);
		}

		public static void LogRPC(this PhotonView view, string methodName, Player target, params object[] parameters)
		{
			SyncUtil.QueueRPC(view, methodName, target, parameters);
		}

		public static void LogRPC(this PhotonView view, string methodName, RpcTarget target, params object[] parameters)
		{
			//IL_0002: Unknown result type (might be due to invalid IL or missing references)
			SyncUtil.QueueRPC(view, methodName, target, parameters);
		}
	}
	public interface ISyncRequest
	{
		void Run();
	}
	public record PunRequest(PhotonView View, string MethodName, object Target, object[] Parameters) : ISyncRequest
	{
		public static PunRequest New(PhotonView view, string methodName, object target, object[] parameters)
		{
			return new PunRequest(view, methodName, target, parameters);
		}

		public void Run()
		{
			//IL_0010: Unknown result type (might be due to invalid IL or missing references)
			//IL_0015: Unknown result type (might be due to invalid IL or missing references)
			//IL_002f: Unknown result type (might be due to invalid IL or missing references)
			object target = Target;
			if (!(target is RpcTarget val))
			{
				Player val2 = (Player)((target is Player) ? target : null);
				if (val2 != null)
				{
					View.RPC(MethodName, val2, Parameters);
				}
			}
			else
			{
				View.RPC(MethodName, val, Parameters);
			}
		}
	}
	public record SyncBundle(PunManager Manager, PhotonView View, StatsManager Stats, string SteamId)
	{
		public static SyncBundle Bundle(string steamId)
		{
			return new SyncBundle(PunManager.instance, StatsManager.instance, steamId);
		}

		public SyncBundle(PunManager mgr, StatsManager sts, string sId)
			: this(mgr, mgr.GetView(), sts, sId)
		{
		}

		public override string ToString()
		{
			return SteamId;
		}

		public static SyncBundle Default(string steamId)
		{
			return new SyncBundle(PunManager.instance, StatsManager.instance, steamId);
		}
	}
	[PublicAPI]
	public static class SyncManager
	{
		private const string Section = "Sync";

		private static ConfigEntry<bool>? _syncHealth;

		private static ConfigEntry<bool>? _syncStamina;

		private static ConfigEntry<bool>? _syncExtraJump;

		private static ConfigEntry<bool>? _syncMapPlayerCount;

		private static ConfigEntry<bool>? _syncGrabRange;

		private static ConfigEntry<bool>? _syncGrabStrength;

		private static ConfigEntry<bool>? _syncGrabThrow;

		private static ConfigEntry<bool>? _syncSprintSpeed;

		private static ConfigEntry<bool>? _syncTumbleLaunch;

		private static ConfigEntry<bool>? _moddedUpgrades;

		internal static void Init()
		{
			_syncHealth = Entry.BepConfig.Bind<bool>("Sync", "Health", true, "Sync Max Health");
			_syncStamina = Entry.BepConfig.Bind<bool>("Sync", "Stamina", true, "Sync Max Stamina");
			_syncExtraJump = Entry.BepConfig.Bind<bool>("Sync", "Extra Jump", true, "Sync Extra Jump Count");
			_syncTumbleLaunch = Entry.BepConfig.Bind<bool>("Sync", "Tumble Launch", true, "Sync Tumble Launch Count");
			_syncMapPlayerCount = Entry.BepConfig.Bind<bool>("Sync", "Map Player Count", true, "Sync Map Player Count");
			_syncSprintSpeed = Entry.BepConfig.Bind<bool>("Sync", "Sprint Speed", false, "Sync Sprint Speed");
			_syncGrabStrength = Entry.BepConfig.Bind<bool>("Sync", "Grab Strength", true, "Sync Grab Strength");
			_syncGrabRange = Entry.BepConfig.Bind<bool>("Sync", "Grab Range", true, "Sync Grab Range");
			_syncGrabThrow = Entry.BepConfig.Bind<bool>("Sync", "Grab Throw", true, "Sync Grab Throw");
			_moddedUpgrades = Entry.BepConfig.Bind<bool>("Sync", "Modded Upgrades", true, "Sync Misc Modded Upgrades");
		}

		public static void PlayerConsumedUpgrade(string steamId, UpgradeId upgrade, int newLevel)
		{
			PlayerConsumedUpgrade(SyncBundle.Default(steamId), upgrade, newLevel);
		}

		public static void PlayerConsumedUpgrade(SyncBundle bundle, UpgradeId upgrade, int newLevel = 0)
		{
			if (!ShouldSync(upgrade))
			{
				return;
			}
			if (bundle.SteamId != SyncUtil.HostSteamId)
			{
				if (newLevel > 0)
				{
					int valueOrDefault = SyncUtil.GetUpgrades(bundle.Stats, upgrade).GetValueOrDefault(SyncUtil.HostSteamId, 0);
					int num = newLevel - valueOrDefault;
					for (int i = 0; i < num; i++)
					{
						SyncUtil.CallUpdateFunction(bundle, SyncUtil.HostSteamId, upgrade);
					}
				}
				else
				{
					SyncUtil.CallUpdateFunction(bundle, SyncUtil.HostSteamId, upgrade);
				}
			}
			else
			{
				SyncHostToAll(bundle);
			}
		}

		public static bool SyncHostToTarget(string targetSteamId)
		{
			SyncBundle bundle = SyncBundle.Default(targetSteamId);
			if (!SyncHostToTarget(bundle, SyncUtil.GetPlayer(targetSteamId)))
			{
				return false;
			}
			SyncUtil.SyncStatsDictionaryToAll(bundle);
			return true;
		}

		public static void SyncHostToAll()
		{
			SyncHostToAll(SyncBundle.Default(SyncUtil.HostSteamId));
		}

		public static void SyncHostToAll(SyncBundle bundle)
		{
			SyncBundle bundle2 = bundle;
			if (SemiFunc.PlayerGetAll().Where(NotHost).Aggregate(seed: false, (bool current, PlayerAvatar player) => current || SyncHostToTarget(bundle2, player)))
			{
				SyncUtil.SyncStatsDictionaryToAll(bundle2);
			}
		}

		private static bool SyncHostToTarget(SyncBundle bundle, PlayerAvatar target)
		{
			return SyncFromSourceToTarget(bundle, SyncUtil.Local, target);
		}

		private static bool SyncFromSourceToTarget(SyncBundle bundle, PlayerAvatar source, PlayerAvatar target)
		{
			string text = source.SteamId();
			string text2 = target.SteamId();
			if (text2 == text)
			{
				return false;
			}
			bool flag = false;
			foreach (UpgradeId item in SyncUtil.GetUpgradeTypes(bundle).Where(ShouldSync))
			{
				Dictionary<string, int> upgrades = SyncUtil.GetUpgrades(bundle.Stats, item);
				int valueOrDefault = upgrades.GetValueOrDefault(text2, 0);
				int valueOrDefault2 = upgrades.GetValueOrDefault(text, 0);
				if (valueOrDefault2 <= valueOrDefault)
				{
					continue;
				}
				int num = valueOrDefault2 - valueOrDefault;
				if (!(flag = flag || num > 0))
				{
					continue;
				}
				if (item.Type != 0)
				{
					for (int i = 0; i < num; i++)
					{
						SyncUtil.CallRPCOnePlayer(bundle, target, item);
					}
				}
				else
				{
					SyncUtil.UpgradeModded(bundle, target, item, num);
				}
				Entry.LogSource.LogInfo((object)string.Format("[{0}] Synchronized upgrade for player {1}: {2} ({3}), from {4} to {5}", "SyncFromSourceToTarget", text2, item.RawName, item.Type, valueOrDefault, valueOrDefault2));
			}
			return flag;
		}

		private static bool NotHost(PlayerAvatar avatar)
		{
			return avatar.SteamId() != SyncUtil.HostSteamId;
		}

		private static bool ShouldSync(UpgradeId key)
		{
			return key.Type switch
			{
				UpgradeType.Health => _syncHealth?.Value ?? false, 
				UpgradeType.Stamina => _syncStamina?.Value ?? false, 
				UpgradeType.ExtraJump => _syncExtraJump?.Value ?? false, 
				UpgradeType.TumbleLaunch => _syncTumbleLaunch?.Value ?? false, 
				UpgradeType.MapPlayerCount => _syncMapPlayerCount?.Value ?? false, 
				UpgradeType.SprintSpeed => _syncSprintSpeed?.Value ?? false, 
				UpgradeType.GrabStrength => _syncGrabStrength?.Value ?? false, 
				UpgradeType.GrabRange => _syncGrabRange?.Value ?? false, 
				UpgradeType.GrabThrow => _syncGrabThrow?.Value ?? false, 
				_ => _moddedUpgrades?.Value ?? false, 
			};
		}
	}
	public record SyncRequest(SyncBundle Bundle) : ISyncRequest
	{
		public void Run()
		{
			Bundle.Manager.SyncAllDictionaries();
		}

		public static ISyncRequest New(SyncBundle bundle)
		{
			return new SyncRequest(bundle);
		}
	}
	[PublicAPI]
	public static class SyncUtil
	{
		private const string PlayerUpgrade = "playerUpgrade";

		private const string AppliedPlayerUpgrade = "appliedPlayerUpgrade";

		private const RpcTarget Others = 1;

		public static readonly UpgradeId HealthId = new UpgradeId(UpgradeType.Health);

		public static readonly UpgradeId StaminaId = new UpgradeId(UpgradeType.Stamina);

		public static readonly UpgradeId ExtraJumpId = new UpgradeId(UpgradeType.ExtraJump);

		public static readonly UpgradeId TumbleLaunchId = new UpgradeId(UpgradeType.TumbleLaunch);

		public static readonly UpgradeId MapPlayerCountId = new UpgradeId(UpgradeType.MapPlayerCount);

		public static readonly UpgradeId SprintSpeedId = new UpgradeId(UpgradeType.SprintSpeed);

		public static readonly UpgradeId GrabStrengthId = new UpgradeId(UpgradeType.GrabStrength);

		public static readonly UpgradeId GrabRangeId = new UpgradeId(UpgradeType.GrabRange);

		public static readonly UpgradeId GrabThrowId = new UpgradeId(UpgradeType.GrabThrow);

		private static readonly ConcurrentQueue<ISyncRequest> SyncQueue = new ConcurrentQueue<ISyncRequest>();

		public static string HostSteamId => Local.SteamId();

		public static PlayerAvatar Local => SemiFunc.PlayerAvatarLocal();

		public static string TrimKey(string? key)
		{
			if (string.IsNullOrEmpty(key))
			{
				return string.Empty;
			}
			string text;
			if (key.StartsWith("appliedPlayerUpgrade"))
			{
				text = key;
				return text.Substring(20, text.Length - 20);
			}
			if (!key.StartsWith("playerUpgrade"))
			{
				return key;
			}
			text = key;
			return text.Substring(13, text.Length - 13);
		}

		public static string FixKey(string? key)
		{
			if (string.IsNullOrEmpty(key))
			{
				return string.Empty;
			}
			if (!key.StartsWith("playerUpgrade"))
			{
				return "playerUpgrade" + key;
			}
			return key;
		}

		public static PlayerAvatar GetPlayer(string targetSteamId)
		{
			return SemiFunc.PlayerAvatarGetFromSteamID(targetSteamId);
		}

		public static IEnumerable<UpgradeId> GetUpgradeTypes(SyncBundle bundle)
		{
			return bundle.Stats.dictionaryOfDictionaries.Where((KeyValuePair<string, Dictionary<string, int>> kvp) => kvp.Key.StartsWith("playerUpgrade") || kvp.Key.StartsWith("appliedPlayerUpgrade")).Select(UpgradeId.New);
		}

		public static UpgradeType GetUpgradeType(string? key)
		{
			return TrimKey(key) switch
			{
				"Health" => UpgradeType.Health, 
				"Stamina" => UpgradeType.Stamina, 
				"ExtraJump" => UpgradeType.ExtraJump, 
				"Launch" => UpgradeType.TumbleLaunch, 
				"MapPlayerCount" => UpgradeType.MapPlayerCount, 
				"Speed" => UpgradeType.SprintSpeed, 
				"Strength" => UpgradeType.GrabStrength, 
				"Range" => UpgradeType.GrabRange, 
				"Throw" => UpgradeType.GrabThrow, 
				_ => UpgradeType.Modded, 
			};
		}

		public static string GetUpgradeName(UpgradeType key)
		{
			return key switch
			{
				UpgradeType.Health => "Health", 
				UpgradeType.Stamina => "Stamina", 
				UpgradeType.ExtraJump => "ExtraJump", 
				UpgradeType.TumbleLaunch => "Launch", 
				UpgradeType.MapPlayerCount => "MapPlayerCount", 
				UpgradeType.SprintSpeed => "Speed", 
				UpgradeType.GrabStrength => "Strength", 
				UpgradeType.GrabRange => "Range", 
				UpgradeType.GrabThrow => "Throw", 
				UpgradeType.Modded => "Modded: Unknown", 
				_ => throw new ArgumentException("Invalid UpgradeType for GetUpgradeName"), 
			};
		}

		public static Dictionary<string, int> GetUpgrades(StatsManager stats, UpgradeId id)
		{
			return id.Type switch
			{
				UpgradeType.Health => stats.playerUpgradeHealth, 
				UpgradeType.Stamina => stats.playerUpgradeStamina, 
				UpgradeType.ExtraJump => stats.playerUpgradeExtraJump, 
				UpgradeType.TumbleLaunch => stats.playerUpgradeLaunch, 
				UpgradeType.MapPlayerCount => stats.playerUpgradeMapPlayerCount, 
				UpgradeType.SprintSpeed => stats.playerUpgradeSpeed, 
				UpgradeType.GrabStrength => stats.playerUpgradeStrength, 
				UpgradeType.GrabRange => stats.playerUpgradeRange, 
				UpgradeType.GrabThrow => stats.playerUpgradeThrow, 
				UpgradeType.Modded => stats.dictionaryOfDictionaries[id.RawName], 
				_ => throw new ArgumentException("Invalid UpgradeType for GetUpgrades"), 
			};
		}

		public static string GetRPCFunctionName(UpgradeType key)
		{
			return key switch
			{
				UpgradeType.Health => "UpgradePlayerHealthRPC", 
				UpgradeType.Stamina => "UpgradePlayerEnergyRPC", 
				UpgradeType.ExtraJump => "UpgradePlayerExtraJumpRPC", 
				UpgradeType.TumbleLaunch => "UpgradePlayerTumbleLaunchRPC", 
				UpgradeType.MapPlayerCount => "UpgradeMapPlayerCountRPC", 
				UpgradeType.SprintSpeed => "UpgradePlayerSprintSpeedRPC", 
				UpgradeType.GrabStrength => "UpgradePlayerGrabStrengthRPC", 
				UpgradeType.GrabRange => "UpgradePlayerGrabRangeRPC", 
				UpgradeType.GrabThrow => "UpgradePlayerThrowStrengthRPC", 
				_ => throw new ArgumentException("Invalid UpgradeType for GetRPCFunctionName"), 
			};
		}

		public static void CallRPCOnePlayer(SyncBundle bundle, PlayerAvatar workingPlayer, UpgradeId key)
		{
			CallRPCOnePlayer(bundle, workingPlayer.SteamId(), key, workingPlayer.photonView.Owner);
		}

		public static void CallRPCOnePlayer(SyncBundle bundle, string steamId, UpgradeId key, Player player)
		{
			bundle.View.LogRPC(GetRPCFunctionName(key.Type), player, steamId, ++GetUpgrades(bundle.Stats, key)[steamId]);
		}

		public static void CallRPC(SyncBundle bundle, string steamId, UpgradeId key)
		{
			bundle.View.LogRPC(GetRPCFunctionName(key.Type), (RpcTarget)1, steamId, ++GetUpgrades(bundle.Stats, key)[steamId]);
		}

		public static void SyncStatsDictionaryToAll(SyncBundle bundle)
		{
			SyncQueue.Enqueue(SyncRequest.New(bundle));
		}

		public static int CallUpdateFunction(SyncBundle bundle, string steamId, UpgradeId key)
		{
			return key.Type switch
			{
				UpgradeType.Health => bundle.Manager.UpgradePlayerHealth(steamId), 
				UpgradeType.Stamina => bundle.Manager.UpgradePlayerEnergy(steamId), 
				UpgradeType.ExtraJump => bundle.Manager.UpgradePlayerExtraJump(steamId), 
				UpgradeType.TumbleLaunch => bundle.Manager.UpgradePlayerTumbleLaunch(steamId), 
				UpgradeType.MapPlayerCount => bundle.Manager.UpgradeMapPlayerCount(steamId), 
				UpgradeType.SprintSpeed => bundle.Manager.UpgradePlayerSprintSpeed(steamId), 
				UpgradeType.GrabStrength => bundle.Manager.UpgradePlayerGrabStrength(steamId), 
				UpgradeType.GrabRange => bundle.Manager.UpgradePlayerGrabRange(steamId), 
				UpgradeType.GrabThrow => bundle.Manager.UpgradePlayerThrowStrength(steamId), 
				UpgradeType.Modded => UpgradeModded(bundle, SemiFunc.PlayerAvatarGetFromSteamID(steamId), key, 1), 
				_ => throw new ArgumentException("Invalid UpgradeType for CallUpdateFunction"), 
			};
		}

		public static int UpgradeModded(SyncBundle bundle, PlayerAvatar workingPlayer, UpgradeId key, int amount)
		{
			string steamId = workingPlayer.SteamId();
			int num = AddToStatsDictionary(bundle, steamId, key, amount);
			PlayerUpgrade val = default(PlayerUpgrade);
			if (Upgrades.TryGetUpgrade(TrimKey(key.RawName), ref val))
			{
				val.SetLevel(workingPlayer, num);
			}
			return num;
		}

		public static void AddToStatsDictionaryAndSync(SyncBundle bundle, string steamId, UpgradeId key, int amount)
		{
			AddToStatsDictionary(bundle, steamId, key, amount);
			SyncStatsDictionaryToAll(bundle);
		}

		public static int AddToStatsDictionary(SyncBundle bundle, string steamId, UpgradeId key, int amount)
		{
			return bundle.Stats.dictionaryOfDictionaries[key.RawName][steamId] += amount;
		}

		public static void QueueRPC(PhotonView view, string methodName, object target, object[] parameters)
		{
			SyncQueue.Enqueue(PunRequest.New(view, methodName, target, parameters));
		}

		public static void RunOneRPC()
		{
			if (SyncQueue.TryDequeue(out ISyncRequest result))
			{
				result.Run();
			}
		}
	}
	public class UpgradeId
	{
		public UpgradeType Type { get; } = SyncUtil.GetUpgradeType(rawName);


		public string RawName { get; }

		public UpgradeId(string rawName)
		{
			RawName = rawName;
			base..ctor();
		}

		public UpgradeId(UpgradeType upgradeType)
			: this(SyncUtil.GetUpgradeName(upgradeType))
		{
			Type = upgradeType;
		}

		public override string ToString()
		{
			return "{ Type = \"" + Type.ToName() + "\", RawName = \"" + RawName + "\" }";
		}

		public static UpgradeId New(string rawName)
		{
			return new UpgradeId(rawName);
		}

		public static UpgradeId New<T>(KeyValuePair<string, T> item)
		{
			return new UpgradeId(item.Key);
		}
	}
	public enum UpgradeType
	{
		Modded,
		Health,
		Stamina,
		ExtraJump,
		TumbleLaunch,
		MapPlayerCount,
		SprintSpeed,
		GrabStrength,
		GrabRange,
		GrabThrow
	}
}