Please disclose if any significant portion of your mod was created using AI tools by adding the 'AI Generated' category. Failing to do so may result in the mod being removed from Thunderstore.
Decompiled source of MurphysSharedUpgrades v1.1.3
plugins/MurphysSharedUpgrades.dll
Decompiled 24 minutes agousing System; using System.Collections; using System.Collections.Generic; using System.Diagnostics; using System.Reflection; using System.Runtime.CompilerServices; using System.Runtime.Versioning; using BepInEx; using BepInEx.Configuration; 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("MurphysSharedUpgrades")] [assembly: AssemblyConfiguration("Release")] [assembly: AssemblyFileVersion("1.1.3.0")] [assembly: AssemblyInformationalVersion("1.1.3")] [assembly: AssemblyProduct("MurphysSharedUpgrades")] [assembly: AssemblyTitle("MurphysSharedUpgrades")] [assembly: AssemblyVersion("1.1.3.0")] [module: RefSafetyRules(11)] namespace Microsoft.CodeAnalysis { [CompilerGenerated] [Embedded] internal sealed class EmbeddedAttribute : Attribute { } } namespace System.Runtime.CompilerServices { [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 MurphyDev.Coop { internal sealed class NativeUpgradeDispatch { private const string UpgradeRpc = "TesterUpgradeCommandRPC"; private readonly object view; private readonly MethodInfo rpc; private readonly object others; private NativeUpgradeDispatch(object view, MethodInfo rpc, object others) { this.view = view; this.rpc = rpc; this.others = others; } internal static bool HasNetworkView(object pun) { return Game.Read<int>(Game.Read(pun, "photonView"), "ViewID") > 0; } internal static NativeUpgradeDispatch For(object pun) { if (!HasNetworkView(pun)) { return null; } Type type = Game.Type("Photon.Pun.RpcTarget"); Type type2 = Game.Type("Photon.Pun.PhotonMessageInfo"); if (type == null || type2 == null || Game.Method("PunManager", "TesterUpgradeCommandRPC", typeof(string), typeof(string), typeof(int), type2) == null) { return null; } MethodInfo methodInfo = Game.Method("Photon.Pun.PhotonView", "RPC", typeof(string), type, typeof(object[])); if (!(methodInfo == null)) { return new NativeUpgradeDispatch(Game.Read(pun, "photonView"), methodInfo, Enum.Parse(type, "Others")); } return null; } internal void Send(string steamId, string upgrade, int amount) { rpc.Invoke(view, new object[3] { "TesterUpgradeCommandRPC", others, new object[3] { steamId, upgrade, amount } }); } } [BepInPlugin("murphydev.repo.sharedupgrades", "Murphy's Shared Upgrades", "1.1.3")] public sealed class SharedUpgradesPlugin : BaseUnityPlugin { internal const string PluginId = "murphydev.repo.sharedupgrades"; internal ConfigEntry<bool> Shared; private ConfigEntry<KeyCode> menuKey; private readonly HashSet<string> reported = new HashSet<string>(); private Harmony harmony; private GUIStyle statusStyle; private float nextRefresh; private bool menuOpen; private bool previousCursorVisibility; private CursorLockMode previousCursorLock; internal static SharedUpgradesPlugin Instance { get; private set; } private void Awake() { //IL_0052: Unknown result type (might be due to invalid IL or missing references) //IL_005c: Expected O, but got Unknown //IL_0073: Unknown result type (might be due to invalid IL or missing references) //IL_0078: Unknown result type (might be due to invalid IL or missing references) Instance = this; Shared = ((BaseUnityPlugin)this).Config.Bind<bool>("Shared Upgrades", "Enabled", true, "Share upgrades when hosting."); menuKey = ((BaseUnityPlugin)this).Config.Bind<KeyCode>("Controls", "SettingsKey", (KeyCode)292, "Open Shared Upgrades settings."); harmony = new Harmony("murphydev.repo.sharedupgrades"); SharedUpgrades.Install(this); ((BaseUnityPlugin)this).Logger.LogInfo((object)("Murphy's Shared Upgrades loaded. Settings key is " + ((object)menuKey.Value/*cast due to .constrained prefix*/).ToString() + ".")); } internal void Patch(string type, string method, Type source, string prefix = null, string postfix = null, Type[] args = null) { Refresh(type + "." + method, delegate { //IL_0077: Unknown result type (might be due to invalid IL or missing references) //IL_0094: Unknown result type (might be due to invalid IL or missing references) MethodInfo methodInfo = Game.Method(type, method, args ?? Type.EmptyTypes); if (methodInfo == null) { ((BaseUnityPlugin)this).Logger.LogWarning((object)(type + "." + method + " is unavailable on this game build.")); } else { harmony.Patch((MethodBase)methodInfo, (prefix == null) ? ((HarmonyMethod)null) : new HarmonyMethod(source, prefix, (Type[])null), (postfix == null) ? ((HarmonyMethod)null) : new HarmonyMethod(source, postfix, (Type[])null), (HarmonyMethod)null, (HarmonyMethod)null, (HarmonyMethod)null); } }); } internal static void Refresh(string name, Action action) { SharedUpgradesPlugin instance = Instance; if ((Object)(object)instance == (Object)null) { return; } try { action(); } catch (Exception ex) { if (instance.reported.Add(name)) { ((BaseUnityPlugin)instance).Logger.LogWarning((object)(name + ": " + (ex.InnerException ?? ex).Message)); } } } internal void ReportCatchUp(int actor) { ((BaseUnityPlugin)this).Logger.LogInfo((object)("Sent shared upgrade sync to lobby player " + actor + ".")); } private void Update() { //IL_0006: Unknown result type (might be due to invalid IL or missing references) if (Input.GetKeyDown(menuKey.Value)) { SetMenu(!menuOpen); } if (menuOpen && Input.GetKeyDown((KeyCode)27)) { SetMenu(open: false); } if (!(Time.unscaledTime < nextRefresh)) { nextRefresh = Time.unscaledTime + 0.35f; Refresh("Shared Upgrades", SharedUpgrades.Refresh); } } private void SetMenu(bool open) { //IL_0031: 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_001a: Unknown result type (might be due to invalid IL or missing references) if (menuOpen != open) { menuOpen = open; if (open) { previousCursorLock = Cursor.lockState; previousCursorVisibility = Cursor.visible; UnlockCursor(); } else { Cursor.lockState = previousCursorLock; Cursor.visible = previousCursorVisibility; } } } private static void UnlockCursor() { Cursor.lockState = (CursorLockMode)0; Cursor.visible = true; } private void LateUpdate() { if (menuOpen) { UnlockCursor(); } } private void OnGUI() { //IL_0005: Unknown result type (might be due to invalid IL or missing references) //IL_000b: Invalid comparison between Unknown and I4 //IL_006c: Unknown result type (might be due to invalid IL or missing references) //IL_0078: Unknown result type (might be due to invalid IL or missing references) //IL_0087: Expected O, but got Unknown //IL_0082: Unknown result type (might be due to invalid IL or missing references) if ((int)Event.current.type == 7 && (Game.Room == null || Game.Host)) { DrawStatus(); } if (menuOpen) { float num = Mathf.Min(380f, (float)Screen.width - 20f); GUI.ModalWindow(726137, new Rect(((float)Screen.width - num) / 2f, ((float)Screen.height - 110f) / 2f, num, 110f), new WindowFunction(DrawMenu), "Murphy's Shared Upgrades"); } } private void DrawStatus() { //IL_006a: Unknown result type (might be due to invalid IL or missing references) //IL_0013: Unknown result type (might be due to invalid IL or missing references) //IL_0018: Unknown result type (might be due to invalid IL or missing references) //IL_0024: Expected O, but got Unknown //IL_00cd: Unknown result type (might be due to invalid IL or missing references) //IL_00b7: Unknown result type (might be due to invalid IL or missing references) //IL_010f: Unknown result type (might be due to invalid IL or missing references) if (statusStyle == null) { statusStyle = new GUIStyle(GUI.skin.label) { alignment = (TextAnchor)3 }; } float num = Mathf.Clamp((float)Screen.height / 1080f, 0.9f, 1.8f); float num2 = 12f * num; float num3 = 25f * num; Rect val = default(Rect); ((Rect)(ref val))..ctor(num2, (float)Screen.height - num2 - num3, 190f * num, num3); GUI.Box(val, GUIContent.none); statusStyle.fontSize = Mathf.RoundToInt(13f * num); bool value = Shared.Value; statusStyle.normal.textColor = (value ? new Color(0.55f, 1f, 0.74f) : new Color(1f, 0.73f, 0.55f)); string text = (value ? "Shared Upgrades: Enabled" : "Shared Upgrades: Disabled"); GUI.Label(new Rect(((Rect)(ref val)).x + 8f * num, ((Rect)(ref val)).y, ((Rect)(ref val)).width - 16f * num, num3), text, statusStyle); } private void DrawMenu(int _) { GUILayout.BeginHorizontal(Array.Empty<GUILayoutOption>()); GUILayout.Label(Shared.Value ? "Host setting enabled" : "Host setting disabled", Array.Empty<GUILayoutOption>()); GUILayout.FlexibleSpace(); GUILayout.Label("created by Murphy.Dev", Array.Empty<GUILayoutOption>()); GUILayout.EndHorizontal(); GUILayout.BeginHorizontal(Array.Empty<GUILayoutOption>()); if (GUILayout.Button("Enable", Array.Empty<GUILayoutOption>())) { SetSharing(enabled: true); } if (GUILayout.Button("Disable", Array.Empty<GUILayoutOption>())) { SetSharing(enabled: false); } GUILayout.EndHorizontal(); if (GUILayout.Button("Close", Array.Empty<GUILayoutOption>())) { SetMenu(open: false); } } private void SetSharing(bool enabled) { Shared.Value = enabled; Refresh("Shared Upgrades", SharedUpgrades.Refresh); } private void OnDestroy() { SetMenu(open: false); if (!((Object)(object)Instance != (Object)(object)this)) { Harmony obj = harmony; if (obj != null) { obj.UnpatchSelf(); } Instance = null; } } } internal static class Game { private static readonly Dictionary<string, Type> Types = new Dictionary<string, Type>(); private static readonly Dictionary<(Type, string), FieldInfo> Fields = new Dictionary<(Type, string), FieldInfo>(); private static readonly Dictionary<(Type, string), PropertyInfo> Properties = new Dictionary<(Type, string), PropertyInfo>(); private static readonly Dictionary<string, MethodInfo> Calls = new Dictionary<string, MethodInfo>(); internal static bool Host { get { if (Alive(Instance("GameManager"))) { return Semi("IsMasterClientOrSingleplayer"); } return false; } } internal static bool HasLevel => Alive(Read(Instance("RunManager"), "levelCurrent")); internal static object Room => Instance("Photon.Pun.PhotonNetwork", "CurrentRoom"); internal static object LocalPlayer => Instance("Photon.Pun.PhotonNetwork", "LocalPlayer"); internal static Type Type(string name) { if (!Types.TryGetValue(name, out var value)) { value = AccessTools.TypeByName(name); if (value != null) { Types.Add(name, value); } } return value; } internal static MethodInfo Method(string type, string method, params Type[] parameters) { string key = type + "." + method + "(" + string.Join(",", Array.ConvertAll(parameters, (Type p) => p.FullName)) + ")"; if (!Calls.TryGetValue(key, out var value)) { Type type2 = Type(type); value = ((type2 == null) ? null : AccessTools.Method(type2, method, parameters, (Type[])null)); if (value != null) { Calls.Add(key, value); } } return value; } internal static FieldInfo Field(Type type, string name) { (Type, string) key = (type, name); if (!Fields.TryGetValue(key, out var value)) { value = AccessTools.Field(type, name); Fields[key] = value; } return value; } internal static object Read(object owner, string name) { if (!Alive(owner)) { return null; } Type obj = owner as Type; Type type = obj ?? owner.GetType(); object obj2 = ((obj == null) ? owner : null); FieldInfo fieldInfo = Field(type, name); if (fieldInfo != null) { return fieldInfo.GetValue(obj2); } if (!Properties.TryGetValue((type, name), out var value)) { value = AccessTools.Property(type, name); Properties[(type, name)] = value; } return value?.GetValue(obj2); } internal static bool Alive(object obj) { if (obj != null) { Object val = (Object)((obj is Object) ? obj : null); if (val != null) { return Object.op_Implicit(val); } return true; } return false; } internal static T Read<T>(object owner, string name) { object obj = Read(owner, name); if (obj is T) { return (T)obj; } return default(T); } internal static object Instance(string name, string field = "instance") { return Read(Type(name), field); } internal static bool Semi(string method) { object obj = Method("SemiFunc", method)?.Invoke(null, null); bool flag = default(bool); int num; if (obj is bool) { flag = (bool)obj; num = 1; } else { num = 0; } return (byte)((uint)num & (flag ? 1u : 0u)) != 0; } internal static object NetworkProperty(object owner, string key) { object obj = Read(owner, "CustomProperties"); if (obj == null) { return null; } MethodInfo methodInfo = AccessTools.Method(obj.GetType(), "TryGetValue", new Type[2] { typeof(object), typeof(object).MakeByRefType() }, (Type[])null); if (methodInfo == null) { return null; } object[] array = new object[2] { key, null }; object obj2 = methodInfo.Invoke(obj, array); bool flag = default(bool); int num; if (obj2 is bool) { flag = (bool)obj2; num = 1; } else { num = 0; } if (((uint)num & (flag ? 1u : 0u)) == 0) { return null; } return array[1]; } internal static bool SetNetworkProperty(object owner, string key, object value) { object obj = Read(owner, "CustomProperties"); if (obj == null) { return false; } Type type = obj.GetType(); object obj2 = Activator.CreateInstance(type); AccessTools.Property(type, "Item")?.SetValue(obj2, value, new object[1] { key }); MethodInfo methodInfo = AccessTools.Method(owner.GetType(), "SetCustomProperties", (Type[])null, (Type[])null); if (methodInfo == null) { return false; } object[] array = new object[methodInfo.GetParameters().Length]; array[0] = obj2; object obj3 = methodInfo.Invoke(owner, array); bool flag = default(bool); int num; if (obj3 is bool) { flag = (bool)obj3; num = 1; } else { num = 0; } return (byte)((uint)num & (flag ? 1u : 0u)) != 0; } internal static void Set(object owner, string name, object value) { Field(owner.GetType(), name)?.SetValue(owner, value); } } internal static class SharedUpgrades { private static readonly (string method, string stat, string apply)[] Types = new(string, string, string)[13] { ("UpgradePlayerHealth", "playerUpgradeHealth", "UpdateHealthRightAway"), ("UpgradePlayerEnergy", "playerUpgradeStamina", "UpdateEnergyRightAway"), ("UpgradePlayerExtraJump", "playerUpgradeExtraJump", "UpdateExtraJumpRightAway"), ("UpgradeMapPlayerCount", "playerUpgradeMapPlayerCount", "UpdateMapPlayerCountRightAway"), ("UpgradePlayerTumbleLaunch", "playerUpgradeLaunch", "UpdateTumbleLaunchRightAway"), ("UpgradePlayerTumbleClimb", "playerUpgradeTumbleClimb", "UpdateTumbleClimbRightAway"), ("UpgradeDeathHeadBattery", "playerUpgradeDeathHeadBattery", "UpdateDeathHeadBatteryRightAway"), ("UpgradePlayerTumbleWings", "playerUpgradeTumbleWings", "UpdateTumbleWingsRightAway"), ("UpgradePlayerSprintSpeed", "playerUpgradeSpeed", "UpdateSprintSpeedRightAway"), ("UpgradePlayerCrouchRest", "playerUpgradeCrouchRest", "UpdateCrouchRestRightAway"), ("UpgradePlayerGrabStrength", "playerUpgradeStrength", "UpdateGrabStrengthRightAway"), ("UpgradePlayerThrowStrength", "playerUpgradeThrow", "UpdateThrowStrengthRightAway"), ("UpgradePlayerGrabRange", "playerUpgradeRange", "UpdateGrabRangeRightAway") }; private static readonly HashSet<int> JoiningActors = new HashSet<int>(); private static object trackedRoom; private static readonly HashSet<int> SeenActors = new HashSet<int>(); private static readonly Dictionary<int, (object avatar, long since)> ReadySince = new Dictionary<int, (object, long)>(); internal static void Install(SharedUpgradesPlugin suite) { Type type = Game.Type("Photon.Realtime.Player"); if (type != null) { suite.Patch("NetworkManager", "OnPlayerEnteredRoom", typeof(SharedUpgrades), null, "PlayerJoined", new Type[1] { type }); suite.Patch("NetworkManager", "OnPlayerLeftRoom", typeof(SharedUpgrades), null, "PlayerLeft", new Type[1] { type }); } suite.Patch("PlayerAvatar", "AddToStatsManagerForced", typeof(SharedUpgrades), null, "AfterAvatarRegistered", new Type[2] { typeof(string), typeof(string) }); } private static void TrackRoom(object room) { if (trackedRoom != room) { trackedRoom = room; JoiningActors.Clear(); SeenActors.Clear(); ReadySince.Clear(); } } private static void PlayerJoined(object __0) { if (!Game.Host || Game.Room == null) { return; } TrackRoom(Game.Room); int num = Game.Read<int>(__0, "ActorNumber"); if (num > 0) { JoiningActors.Add(num); SeenActors.Add(num); ReadySince.Remove(num); object obj = Game.Method("SemiFunc", "PlayerAvatarGetFromPhotonPlayer", __0.GetType())?.Invoke(null, new object[1] { __0 }); if (!string.IsNullOrEmpty(Game.Read<string>(obj, "steamID"))) { AfterAvatarRegistered(obj); } } } private static void PlayerLeft(object __0) { int num = Game.Read<int>(__0, "ActorNumber"); JoiningActors.Remove(num); SeenActors.Remove(num); ReadySince.Remove(num); } private static bool AvatarReady(object avatar) { if (Game.Alive(avatar) && Game.Read<bool>(avatar, "spawned")) { return Game.Read<bool>(avatar, "levelAnimationCompleted"); } return false; } private static int ActorOf(object avatar) { return Game.Read<int>(Game.Read(Game.Read(avatar, "photonView"), "Owner"), "ActorNumber"); } private static void AfterAvatarRegistered(object __instance) { if (!SharedUpgradesPlugin.Instance.Shared.Value || !Game.Host || Game.Room == null) { return; } TrackRoom(Game.Room); object owner = Game.Read(Game.Read(__instance, "photonView"), "Owner"); int num = Game.Read<int>(owner, "ActorNumber"); if (num <= 0 || Game.Read<bool>(owner, "IsLocal") || !Game.Read<bool>(Game.Instance("LevelGenerator", "Instance"), "Generated")) { return; } if (SeenActors.Add(num)) { JoiningActors.Add(num); } if (!JoiningActors.Contains(num)) { return; } SharedUpgradesPlugin.Refresh("Sync arriving player's upgrades", delegate { object obj = Game.Instance("PunManager"); if (NativeUpgradeDispatch.HasNetworkView(obj)) { Game.Method("PunManager", "SyncAllDictionaries")?.Invoke(obj, null); } }); } private static bool ArrivalReady(object avatar, object generator, int actor, long now) { if (!Game.Read<bool>(avatar, "spawned")) { return false; } if (AvatarReady(avatar)) { return true; } object value = Game.Read(Game.Read(avatar, "photonView"), "Owner"); if ((!(Game.Read(generator, "ModulesReadyPlayerList") is IList list) || !list.Contains(value)) && !Game.Read<bool>(avatar, "clientCameraStateValid")) { return false; } if (!ReadySince.TryGetValue(actor, out (object, long) value2) || value2.Item1 != avatar) { ReadySince[actor] = (avatar, now); return false; } return now - value2.Item2 >= Stopwatch.Frequency; } internal static void Refresh() { TrackRoom(Game.Room); if (SharedUpgradesPlugin.Instance.Shared.Value) { Sync(null); } } private static void Sync(string onlyMethod) { if (!Game.HasLevel || !Game.Host || (!Game.Semi("RunIsLevel") && !Game.Semi("RunIsShop") && !Game.Semi("RunIsLobby"))) { return; } object owner = Game.Instance("StatsManager"); object owner2 = Game.Instance("GameDirector"); object obj = Game.Instance("PunManager"); object obj2 = Game.Instance("LevelGenerator", "Instance"); if (!Game.Alive(obj) || !Game.Read<bool>(obj2, "Generated") || !Game.Read<bool>(owner, "statsSynced") || !(Game.Read(owner2, "PlayerList") is IList { Count: not 0 } list)) { return; } NativeUpgradeDispatch nativeUpgradeDispatch = ((Game.Room != null) ? NativeUpgradeDispatch.For(obj) : null); if (Game.Room != null && nativeUpgradeDispatch == null) { return; } object owner3 = Game.Instance("RunManager"); if (Game.Read<bool>(owner3, "restarting") || Game.Read<bool>(owner3, "SceneHandoffActive")) { return; } List<int> list2 = new List<int>(); long timestamp = Stopwatch.GetTimestamp(); if (Game.Room != null) { foreach (object item2 in list) { object owner4 = Game.Read(Game.Read(item2, "photonView"), "Owner"); int num = Game.Read<int>(owner4, "ActorNumber"); if (num > 0 && !Game.Read<bool>(owner4, "IsLocal")) { if (SeenActors.Add(num)) { JoiningActors.Add(num); } if (JoiningActors.Contains(num) && !string.IsNullOrEmpty(Game.Read<string>(item2, "steamID")) && ArrivalReady(item2, obj2, num, timestamp)) { list2.Add(num); } } } } if (list.Count < 2 && list2.Count == 0) { return; } bool flag = false; (string, string, string)[] types = Types; for (int i = 0; i < types.Length; i++) { (string, string, string) tuple = types[i]; if ((onlyMethod != null && tuple.Item1 != onlyMethod) || !(Game.Read(owner, tuple.Item2) is IDictionary dictionary)) { continue; } MethodInfo methodInfo = Game.Method("PunManager", tuple.Item1, typeof(string), typeof(int)); if (methodInfo == null) { continue; } int num2 = 0; foreach (DictionaryEntry item3 in dictionary) { if (item3.Value is int val) { num2 = Math.Max(num2, val); } } foreach (object item4 in list) { string text = Game.Read<string>(item4, "steamID"); int item = ActorOf(item4); if (!string.IsNullOrEmpty(text) && (!JoiningActors.Contains(item) || list2.Contains(item))) { int num3 = ((dictionary[text] is int num4) ? num4 : 0); if (num3 < num2) { int num5 = num2 - num3; methodInfo.Invoke(obj, new object[2] { text, num5 }); nativeUpgradeDispatch?.Send(text, tuple.Item2.Substring("playerUpgrade".Length), num5); flag = true; } } } } if (Game.Room == null) { return; } if (list2.Count > 0) { Game.Method("PunManager", "SyncAllDictionaries")?.Invoke(obj, null); { foreach (int item5 in list2) { JoiningActors.Remove(item5); ReadySince.Remove(item5); SharedUpgradesPlugin.Instance.ReportCatchUp(item5); } return; } } if (flag) { Game.Method("PunManager", "SyncAllDictionariesDelta")?.Invoke(obj, null); } } } }