Decompiled source of PaintColorSwitch v1.0.0

PaintColorSwitchMod.dll

Decompiled a year ago
using System;
using System.Diagnostics;
using System.Reflection;
using System.Runtime.CompilerServices;
using System.Runtime.InteropServices;
using System.Runtime.Versioning;
using BepInEx;
using GameNetcodeStuff;
using HarmonyLib;
using PaintColorSwitchMod.Patches;
using Unity.Netcode;
using UnityEngine;
using UnityEngine.InputSystem;
using UnityEngine.InputSystem.Controls;

[assembly: CompilationRelaxations(8)]
[assembly: RuntimeCompatibility(WrapNonExceptionThrows = true)]
[assembly: Debuggable(DebuggableAttribute.DebuggingModes.Default | DebuggableAttribute.DebuggingModes.DisableOptimizations | DebuggableAttribute.DebuggingModes.IgnoreSymbolStoreSequencePoints | DebuggableAttribute.DebuggingModes.EnableEditAndContinue)]
[assembly: AssemblyTitle("PaintColorSwitchMod")]
[assembly: AssemblyDescription("")]
[assembly: AssemblyConfiguration("")]
[assembly: AssemblyCompany("")]
[assembly: AssemblyProduct("PaintColorSwitchMod")]
[assembly: AssemblyCopyright("Copyright ©  2023")]
[assembly: AssemblyTrademark("")]
[assembly: ComVisible(false)]
[assembly: Guid("D4BD0462-1D86-4410-BA54-11BDD636AD65")]
[assembly: AssemblyFileVersion("1.0.0.0")]
[assembly: TargetFramework(".NETFramework,Version=v4.8", FrameworkDisplayName = ".NET Framework 4.8")]
[assembly: AssemblyVersion("1.0.0.0")]
internal class <Module>
{
	static <Module>()
	{
	}
}
namespace PaintColorSwitchMod
{
	public class PaintColorSwitchNetworkHandler : NetworkBehaviour
	{
		public static PaintColorSwitchNetworkHandler Instance { get; private set; }

		public static event Action<NetworkObjectReference> ColorChangeEvent;

		public override void OnNetworkSpawn()
		{
			PaintColorSwitchNetworkHandler.ColorChangeEvent = null;
			if ((NetworkManager.Singleton.IsHost || NetworkManager.Singleton.IsServer) && (Object)(object)Instance != (Object)null && (Object)(object)((Component)Instance).gameObject.GetComponent<NetworkObject>() != (Object)null)
			{
				((Component)Instance).gameObject.GetComponent<NetworkObject>().Despawn(true);
			}
			Instance = this;
			((NetworkBehaviour)this).OnNetworkSpawn();
		}

		[ServerRpc(RequireOwnership = false)]
		public void EventServerRpc(NetworkObjectReference sprayPaintItemNOR)
		{
			//IL_0024: Unknown result type (might be due to invalid IL or missing references)
			//IL_002e: Invalid comparison between Unknown and I4
			//IL_00a7: Unknown result type (might be due to invalid IL or missing references)
			//IL_00b1: Invalid comparison between Unknown and I4
			//IL_005f: Unknown result type (might be due to invalid IL or missing references)
			//IL_0068: Unknown result type (might be due to invalid IL or missing references)
			//IL_006d: Unknown result type (might be due to invalid IL or missing references)
			//IL_007d: Unknown result type (might be due to invalid IL or missing references)
			//IL_0083: Unknown result type (might be due to invalid IL or missing references)
			//IL_0097: Unknown result type (might be due to invalid IL or missing references)
			//IL_00e3: Unknown result type (might be due to invalid IL or missing references)
			NetworkManager networkManager = ((NetworkBehaviour)this).NetworkManager;
			if (networkManager != null && networkManager.IsListening)
			{
				if ((int)base.__rpc_exec_stage != 1 && (networkManager.IsClient || networkManager.IsHost))
				{
					ServerRpcParams val = default(ServerRpcParams);
					FastBufferWriter val2 = ((NetworkBehaviour)this).__beginSendServerRpc(1453472041u, val, (RpcDelivery)0);
					((FastBufferWriter)(ref val2)).WriteValueSafe<NetworkObjectReference>(ref sprayPaintItemNOR, default(ForNetworkSerializable));
					((NetworkBehaviour)this).__endSendServerRpc(ref val2, 1453472041u, val, (RpcDelivery)0);
				}
				if ((int)base.__rpc_exec_stage == 1 && (networkManager.IsServer || networkManager.IsHost))
				{
					Debug.Log((object)"Client sent a server RPC... EventServerRPC Fired!");
					EventClientRpc(sprayPaintItemNOR);
				}
			}
		}

		[ClientRpc]
		public void EventClientRpc(NetworkObjectReference sprayPaintItemNOR)
		{
			//IL_0024: Unknown result type (might be due to invalid IL or missing references)
			//IL_002e: Invalid comparison between Unknown and I4
			//IL_00a7: Unknown result type (might be due to invalid IL or missing references)
			//IL_00b1: Invalid comparison between Unknown and I4
			//IL_005f: Unknown result type (might be due to invalid IL or missing references)
			//IL_0068: Unknown result type (might be due to invalid IL or missing references)
			//IL_006d: Unknown result type (might be due to invalid IL or missing references)
			//IL_007d: Unknown result type (might be due to invalid IL or missing references)
			//IL_0083: Unknown result type (might be due to invalid IL or missing references)
			//IL_0097: Unknown result type (might be due to invalid IL or missing references)
			//IL_00ed: Unknown result type (might be due to invalid IL or missing references)
			NetworkManager networkManager = ((NetworkBehaviour)this).NetworkManager;
			if (networkManager != null && networkManager.IsListening)
			{
				if ((int)base.__rpc_exec_stage != 2 && (networkManager.IsServer || networkManager.IsHost))
				{
					ClientRpcParams val = default(ClientRpcParams);
					FastBufferWriter val2 = ((NetworkBehaviour)this).__beginSendClientRpc(4149733555u, val, (RpcDelivery)0);
					((FastBufferWriter)(ref val2)).WriteValueSafe<NetworkObjectReference>(ref sprayPaintItemNOR, default(ForNetworkSerializable));
					((NetworkBehaviour)this).__endSendClientRpc(ref val2, 4149733555u, val, (RpcDelivery)0);
				}
				if ((int)base.__rpc_exec_stage == 2 && (networkManager.IsClient || networkManager.IsHost))
				{
					Debug.Log((object)"Server sent a client RPC... EventClientRpc Fired!");
					PaintColorSwitchNetworkHandler.ColorChangeEvent?.Invoke(sprayPaintItemNOR);
				}
			}
		}

		protected override void __initializeVariables()
		{
			((NetworkBehaviour)this).__initializeVariables();
		}

		[RuntimeInitializeOnLoadMethod]
		internal static void InitializeRPCS_PaintColorSwitchNetworkHandler()
		{
			//IL_0011: Unknown result type (might be due to invalid IL or missing references)
			//IL_001b: Expected O, but got Unknown
			//IL_002c: Unknown result type (might be due to invalid IL or missing references)
			//IL_0036: Expected O, but got Unknown
			NetworkManager.__rpc_func_table.Add(1453472041u, new RpcReceiveHandler(__rpc_handler_1453472041));
			NetworkManager.__rpc_func_table.Add(4149733555u, new RpcReceiveHandler(__rpc_handler_4149733555));
		}

		private static void __rpc_handler_1453472041(NetworkBehaviour target, FastBufferReader reader, __RpcParams rpcParams)
		{
			//IL_002f: Unknown result type (might be due to invalid IL or missing references)
			//IL_0035: Unknown result type (might be due to invalid IL or missing references)
			//IL_0044: Unknown result type (might be due to invalid IL or missing references)
			//IL_004f: Unknown result type (might be due to invalid IL or missing references)
			//IL_005e: Unknown result type (might be due to invalid IL or missing references)
			NetworkManager networkManager = target.NetworkManager;
			if (networkManager != null && networkManager.IsListening)
			{
				NetworkObjectReference sprayPaintItemNOR = default(NetworkObjectReference);
				((FastBufferReader)(ref reader)).ReadValueSafe<NetworkObjectReference>(ref sprayPaintItemNOR, default(ForNetworkSerializable));
				target.__rpc_exec_stage = (__RpcExecStage)1;
				((PaintColorSwitchNetworkHandler)(object)target).EventServerRpc(sprayPaintItemNOR);
				target.__rpc_exec_stage = (__RpcExecStage)0;
			}
		}

		private static void __rpc_handler_4149733555(NetworkBehaviour target, FastBufferReader reader, __RpcParams rpcParams)
		{
			//IL_002f: Unknown result type (might be due to invalid IL or missing references)
			//IL_0035: Unknown result type (might be due to invalid IL or missing references)
			//IL_0044: Unknown result type (might be due to invalid IL or missing references)
			//IL_004f: Unknown result type (might be due to invalid IL or missing references)
			//IL_005e: Unknown result type (might be due to invalid IL or missing references)
			NetworkManager networkManager = target.NetworkManager;
			if (networkManager != null && networkManager.IsListening)
			{
				NetworkObjectReference sprayPaintItemNOR = default(NetworkObjectReference);
				((FastBufferReader)(ref reader)).ReadValueSafe<NetworkObjectReference>(ref sprayPaintItemNOR, default(ForNetworkSerializable));
				target.__rpc_exec_stage = (__RpcExecStage)2;
				((PaintColorSwitchNetworkHandler)(object)target).EventClientRpc(sprayPaintItemNOR);
				target.__rpc_exec_stage = (__RpcExecStage)0;
			}
		}

		protected internal override string __getTypeName()
		{
			return "PaintColorSwitchNetworkHandler";
		}
	}
	[BepInPlugin("Silvision.PaintColorSwitchMod", "Paint Color Switch Mod", "1.0.0")]
	public class PaintColorSwitchModBase : BaseUnityPlugin
	{
		private const string PLUGIN_GUID = "Silvision.PaintColorSwitchMod";

		private const string PLUGIN_NAME = "Paint Color Switch Mod";

		private const string PLUGIN_VERSION = "1.0.0";

		public static PaintColorSwitchModBase Instance;

		public static AssetBundle MainAssetBundle;

		private readonly Harmony harmony = new Harmony("Silvision.PaintColorSwitchMod");

		private static void NetCodeWeaver()
		{
			Type[] types = Assembly.GetExecutingAssembly().GetTypes();
			Type[] array = types;
			foreach (Type type in array)
			{
				MethodInfo[] methods = type.GetMethods(BindingFlags.Instance | BindingFlags.Static | BindingFlags.NonPublic);
				MethodInfo[] array2 = methods;
				foreach (MethodInfo methodInfo in array2)
				{
					object[] customAttributes = methodInfo.GetCustomAttributes(typeof(RuntimeInitializeOnLoadMethodAttribute), inherit: false);
					if (customAttributes.Length != 0)
					{
						methodInfo.Invoke(null, null);
					}
				}
			}
		}

		private void Awake()
		{
			((BaseUnityPlugin)this).Logger.LogInfo((object)"Plugin Silvision.PaintColorSwitchMod is loaded!");
			MainAssetBundle = AssetBundle.LoadFromStream(Assembly.GetExecutingAssembly().GetManifestResourceStream("PaintColorSwitchMod.Properties.Resources.asset"));
			NetCodeWeaver();
			if ((Object)(object)Instance == (Object)null)
			{
				Instance = this;
			}
			harmony.PatchAll(typeof(PaintColorSwitchModBase));
			harmony.PatchAll(typeof(NetworkObjectManagerPatch));
			harmony.PatchAll(typeof(PlayerControllerBPatch));
			harmony.PatchAll(typeof(SprayPaintItemPatch));
		}
	}
}
namespace PaintColorSwitchMod.Patches
{
	[HarmonyPatch]
	public class NetworkObjectManagerPatch
	{
		private static GameObject networkPrefab;

		[HarmonyPostfix]
		[HarmonyPatch(typeof(GameNetworkManager), "Start")]
		public static void Init()
		{
			if (!((Object)(object)networkPrefab != (Object)null))
			{
				networkPrefab = PaintColorSwitchModBase.MainAssetBundle.LoadAsset<GameObject>("PaintColorSwitchNetworkHandler");
				networkPrefab.AddComponent<PaintColorSwitchNetworkHandler>();
				NetworkManager.Singleton.AddNetworkPrefab(networkPrefab);
			}
		}

		[HarmonyPostfix]
		[HarmonyPatch(typeof(StartOfRound), "Awake")]
		private static void SpawnNetworkHandler()
		{
			//IL_0024: Unknown result type (might be due to invalid IL or missing references)
			//IL_0029: Unknown result type (might be due to invalid IL or missing references)
			if (NetworkManager.Singleton.IsHost || NetworkManager.Singleton.IsServer)
			{
				GameObject val = Object.Instantiate<GameObject>(networkPrefab, Vector3.zero, Quaternion.identity);
				val.GetComponent<NetworkObject>().Spawn(false);
			}
		}
	}
	[HarmonyPatch(typeof(PlayerControllerB))]
	public class PlayerControllerBPatch
	{
		private static GrabbableObject currentItem;

		private static SprayPaintItem sprayPaintItem;

		[HarmonyPatch("Update")]
		[HarmonyPostfix]
		private static void BindColorChangeKeyPatch(PlayerControllerB __instance)
		{
			//IL_00b0: Unknown result type (might be due to invalid IL or missing references)
			//IL_00b5: Unknown result type (might be due to invalid IL or missing references)
			//IL_011a: Unknown result type (might be due to invalid IL or missing references)
			//IL_00ff: Unknown result type (might be due to invalid IL or missing references)
			if ((!((NetworkBehaviour)__instance).IsOwner || !__instance.isPlayerControlled || (((NetworkBehaviour)__instance).IsServer && !__instance.isHostPlayerObject)) && !__instance.isTestingPlayer)
			{
				return;
			}
			currentItem = __instance.ItemSlots[__instance.currentItemSlot];
			if ((Object)(object)currentItem == (Object)null)
			{
				return;
			}
			sprayPaintItem = (SprayPaintItem)((currentItem is SprayPaintItem) ? /*isinst with value type is only supported in some contexts*/: null);
			if ((Object)(object)sprayPaintItem == (Object)null || !Object.op_Implicit((Object)(object)((NetworkBehaviour)sprayPaintItem).NetworkObject))
			{
				return;
			}
			NetworkObjectReference sprayPaintItemNOR = NetworkObjectReference.op_Implicit(((NetworkBehaviour)sprayPaintItem).NetworkObject);
			if (((ButtonControl)Keyboard.current.tKey).wasPressedThisFrame)
			{
				Debug.Log((object)"T Key was Pressed");
				if (((NetworkBehaviour)__instance).IsHost || ((NetworkBehaviour)__instance).IsServer)
				{
					Debug.Log((object)"Does it get here x1");
					PaintColorSwitchNetworkHandler.Instance.EventClientRpc(sprayPaintItemNOR);
				}
				else
				{
					Debug.Log((object)"Does it get here x2");
					PaintColorSwitchNetworkHandler.Instance.EventServerRpc(sprayPaintItemNOR);
				}
			}
		}
	}
	[HarmonyPatch]
	public class SprayPaintItemPatch
	{
		[HarmonyPatch(typeof(RoundManager), "GenerateNewLevelClientRpc")]
		[HarmonyPostfix]
		private static void SubscribeToHandler()
		{
			PaintColorSwitchNetworkHandler.ColorChangeEvent -= ReceivedEventFromServer;
			PaintColorSwitchNetworkHandler.ColorChangeEvent += ReceivedEventFromServer;
		}

		[HarmonyPatch(typeof(RoundManager), "DespawnPropsAtEndOfRound")]
		[HarmonyPostfix]
		private static void UnsubscribeFromHandler()
		{
			PaintColorSwitchNetworkHandler.ColorChangeEvent -= ReceivedEventFromServer;
		}

		[HarmonyPatch(typeof(SprayPaintItem))]
		public static void ReceivedEventFromServer(NetworkObjectReference sprayPaintItemNOR)
		{
			Debug.Log((object)"ReceivedEventFromServer");
			NetworkObject val = default(NetworkObject);
			if (((NetworkObjectReference)(ref sprayPaintItemNOR)).TryGet(ref val, (NetworkManager)null))
			{
				SprayPaintItem component = ((Component)val).GetComponent<SprayPaintItem>();
				FieldInfo fieldInfo = AccessTools.Field(typeof(SprayPaintItem), "sprayCanMatsIndex");
				int num = (int)fieldInfo.GetValue(component);
				num = ((num < component.sprayCanMats.Length - 1) ? (num + 1) : 0);
				fieldInfo.SetValue(component, num);
				((Renderer)((Component)component.sprayParticle).GetComponent<ParticleSystemRenderer>()).material = component.particleMats[num];
				((Renderer)((Component)component.sprayCanNeedsShakingParticle).GetComponent<ParticleSystemRenderer>()).material = component.particleMats[num];
			}
		}
	}
}