Decompiled source of BoomboxControllerVolumeSync v1.0.1

BoomboxControllerVolumeSync.dll

Decompiled 6 months ago
using System;
using System.CodeDom.Compiler;
using System.ComponentModel;
using System.Diagnostics;
using System.Globalization;
using System.Reflection;
using System.Resources;
using System.Runtime.CompilerServices;
using System.Runtime.InteropServices;
using System.Runtime.Versioning;
using BepInEx;
using BepInEx.Logging;
using BoomboxController;
using BoomboxControllerVolumeSync.NetcodePatcher;
using BoomboxControllerVolumeSync.Properties;
using HarmonyLib;
using Unity.Netcode;
using UnityEngine;

[assembly: CompilationRelaxations(8)]
[assembly: RuntimeCompatibility(WrapNonExceptionThrows = true)]
[assembly: Debuggable(DebuggableAttribute.DebuggingModes.Default | DebuggableAttribute.DebuggingModes.DisableOptimizations | DebuggableAttribute.DebuggingModes.IgnoreSymbolStoreSequencePoints | DebuggableAttribute.DebuggingModes.EnableEditAndContinue)]
[assembly: AssemblyTitle("BoomboxControllerVolumeSync")]
[assembly: AssemblyDescription("")]
[assembly: AssemblyConfiguration("")]
[assembly: AssemblyCompany("")]
[assembly: AssemblyProduct("BoomboxControllerVolumeSync")]
[assembly: AssemblyCopyright("Copyright ©  2024")]
[assembly: AssemblyTrademark("")]
[assembly: ComVisible(false)]
[assembly: Guid("7f076abc-a160-4d29-8bb9-8a40e4eb7bf0")]
[assembly: AssemblyFileVersion("1.0.0.0")]
[assembly: TargetFramework(".NETFramework,Version=v4.8", FrameworkDisplayName = ".NET Framework 4.8")]
[assembly: AssemblyVersion("1.0.0.0")]
[module: NetcodePatchedAssembly]
internal class <Module>
{
	static <Module>()
	{
	}
}
namespace BoomboxControllerVolumeSync
{
	internal class BoomboxPatch
	{
		[HarmonyPatch(typeof(BoomboxController), "SaveCache")]
		[HarmonyPostfix]
		private static void SaveCachePostFix(float vol)
		{
			//IL_0009: Unknown result type (might be due to invalid IL or missing references)
			//IL_000f: Unknown result type (might be due to invalid IL or missing references)
			VolumeSync.Instance.ActivateVolumeSyncServerRPC(vol);
		}
	}
	internal class GameNetworkManagerPatch
	{
		private static GameObject networkPrefab;

		[HarmonyPatch(typeof(GameNetworkManager), "Start")]
		[HarmonyPostfix]
		private static void Init()
		{
			if (!((Object)(object)networkPrefab != (Object)null))
			{
				AssetBundle val = AssetBundle.LoadFromMemory(Resources.boomboxcontrollervolumesync);
				networkPrefab = val.LoadAsset<GameObject>("Assets/BoomboxVolumeSyncHandler.prefab");
				networkPrefab.AddComponent<VolumeSync>();
				NetworkManager.Singleton.AddNetworkPrefab(networkPrefab);
			}
		}

		[HarmonyPatch(typeof(StartOfRound), "Awake")]
		[HarmonyPostfix]
		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);
			}
		}
	}
	[BepInPlugin("TobanCZ.BoomboxControllerVolumeSync", "Boombox Controller Volume Sync", "1.0.0")]
	public class Plugin : BaseUnityPlugin
	{
		private const string modGUID = "TobanCZ.BoomboxControllerVolumeSync";

		private const string modName = "Boombox Controller Volume Sync";

		private const string modVersion = "1.0.0";

		private readonly Harmony harmony = new Harmony("TobanCZ.BoomboxControllerVolumeSync");

		private static Plugin Instance;

		public static ManualLogSource mls;

		public static GameObject volumeSync;

		private void Awake()
		{
			if ((Object)(object)Instance == (Object)null)
			{
				Instance = this;
			}
			NetcodeWeaver();
			mls = Logger.CreateLogSource("TobanCZ.BoomboxControllerVolumeSync");
			mls.LogInfo((object)"The Boombox Controller Volume Sync has been loaded.");
			harmony.PatchAll(typeof(Plugin));
			harmony.PatchAll(typeof(BoomboxPatch));
			harmony.PatchAll(typeof(GameNetworkManagerPatch));
		}

		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);
					}
				}
			}
		}
	}
	internal class VolumeSync : NetworkBehaviour
	{
		private float syncedVolume = 0.5f;

		internal static VolumeSync Instance { get; private set; }

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

		private void OnPlayerJoin(ulong playerId)
		{
			if (((NetworkBehaviour)this).IsServer)
			{
				ActivateVolumeSyncOnJoinClientRpc(playerId, syncedVolume);
			}
		}

		[ServerRpc(RequireOwnership = false)]
		public void ActivateVolumeSyncServerRPC(float newVolume, ServerRpcParams serverRpcParams = default(ServerRpcParams))
		{
			//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)
			NetworkManager networkManager = ((NetworkBehaviour)this).NetworkManager;
			if (networkManager != null && networkManager.IsListening)
			{
				if ((int)base.__rpc_exec_stage != 1 && (networkManager.IsClient || networkManager.IsHost))
				{
					FastBufferWriter val = ((NetworkBehaviour)this).__beginSendServerRpc(3519616167u, serverRpcParams, (RpcDelivery)0);
					((FastBufferWriter)(ref val)).WriteValueSafe<float>(ref newVolume, default(ForPrimitives));
					((NetworkBehaviour)this).__endSendServerRpc(ref val, 3519616167u, serverRpcParams, (RpcDelivery)0);
				}
				if ((int)base.__rpc_exec_stage == 1 && (networkManager.IsServer || networkManager.IsHost))
				{
					ActivateVolumeSyncClientRpc(newVolume);
				}
			}
		}

		[ClientRpc]
		public void ActivateVolumeSyncClientRpc(float newVolume)
		{
			//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)
			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(1915358677u, val, (RpcDelivery)0);
					((FastBufferWriter)(ref val2)).WriteValueSafe<float>(ref newVolume, default(ForPrimitives));
					((NetworkBehaviour)this).__endSendClientRpc(ref val2, 1915358677u, val, (RpcDelivery)0);
				}
				if ((int)base.__rpc_exec_stage == 2 && (networkManager.IsClient || networkManager.IsHost))
				{
					syncedVolume = newVolume;
					BoomboxController.boomboxItem.boomboxAudio.volume = syncedVolume;
				}
			}
		}

		[ClientRpc]
		public void ActivateVolumeSyncOnJoinClientRpc(ulong targetPlayerId, float newVolume)
		{
			//IL_0024: Unknown result type (might be due to invalid IL or missing references)
			//IL_002e: Invalid comparison between Unknown and I4
			//IL_00b4: Unknown result type (might be due to invalid IL or missing references)
			//IL_00be: 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_0071: Unknown result type (might be due to invalid IL or missing references)
			//IL_008a: Unknown result type (might be due to invalid IL or missing references)
			//IL_0090: Unknown result type (might be due to invalid IL or missing references)
			//IL_00a4: 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(925581769u, val, (RpcDelivery)0);
					BytePacker.WriteValueBitPacked(val2, targetPlayerId);
					((FastBufferWriter)(ref val2)).WriteValueSafe<float>(ref newVolume, default(ForPrimitives));
					((NetworkBehaviour)this).__endSendClientRpc(ref val2, 925581769u, val, (RpcDelivery)0);
				}
				if ((int)base.__rpc_exec_stage == 2 && (networkManager.IsClient || networkManager.IsHost) && ((NetworkBehaviour)this).IsClient && NetworkManager.Singleton.LocalClientId == targetPlayerId)
				{
					syncedVolume = newVolume;
					BoomboxController.boomboxItem.boomboxAudio.volume = syncedVolume;
				}
			}
		}

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

		[RuntimeInitializeOnLoadMethod]
		internal static void InitializeRPCS_VolumeSync()
		{
			//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
			//IL_0047: Unknown result type (might be due to invalid IL or missing references)
			//IL_0051: Expected O, but got Unknown
			NetworkManager.__rpc_func_table.Add(3519616167u, new RpcReceiveHandler(__rpc_handler_3519616167));
			NetworkManager.__rpc_func_table.Add(1915358677u, new RpcReceiveHandler(__rpc_handler_1915358677));
			NetworkManager.__rpc_func_table.Add(925581769u, new RpcReceiveHandler(__rpc_handler_925581769));
		}

		private static void __rpc_handler_3519616167(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_003e: 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_0044: Unknown result type (might be due to invalid IL or missing references)
			//IL_004e: Unknown result type (might be due to invalid IL or missing references)
			//IL_005d: Unknown result type (might be due to invalid IL or missing references)
			//IL_006c: Unknown result type (might be due to invalid IL or missing references)
			NetworkManager networkManager = target.NetworkManager;
			if (networkManager != null && networkManager.IsListening)
			{
				float newVolume = default(float);
				((FastBufferReader)(ref reader)).ReadValueSafe<float>(ref newVolume, default(ForPrimitives));
				ServerRpcParams server = rpcParams.Server;
				target.__rpc_exec_stage = (__RpcExecStage)1;
				((VolumeSync)(object)target).ActivateVolumeSyncServerRPC(newVolume, server);
				target.__rpc_exec_stage = (__RpcExecStage)0;
			}
		}

		private static void __rpc_handler_1915358677(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_005e: Unknown result type (might be due to invalid IL or missing references)
			NetworkManager networkManager = target.NetworkManager;
			if (networkManager != null && networkManager.IsListening)
			{
				float newVolume = default(float);
				((FastBufferReader)(ref reader)).ReadValueSafe<float>(ref newVolume, default(ForPrimitives));
				target.__rpc_exec_stage = (__RpcExecStage)2;
				((VolumeSync)(object)target).ActivateVolumeSyncClientRpc(newVolume);
				target.__rpc_exec_stage = (__RpcExecStage)0;
			}
		}

		private static void __rpc_handler_925581769(NetworkBehaviour target, FastBufferReader reader, __RpcParams rpcParams)
		{
			//IL_0023: Unknown result type (might be due to invalid IL or missing references)
			//IL_003c: Unknown result type (might be due to invalid IL or missing references)
			//IL_0042: Unknown result type (might be due to invalid IL or missing references)
			//IL_0051: Unknown result type (might be due to invalid IL or missing references)
			//IL_006f: Unknown result type (might be due to invalid IL or missing references)
			NetworkManager networkManager = target.NetworkManager;
			if (networkManager != null && networkManager.IsListening)
			{
				ulong targetPlayerId = default(ulong);
				ByteUnpacker.ReadValueBitPacked(reader, ref targetPlayerId);
				float newVolume = default(float);
				((FastBufferReader)(ref reader)).ReadValueSafe<float>(ref newVolume, default(ForPrimitives));
				target.__rpc_exec_stage = (__RpcExecStage)2;
				((VolumeSync)(object)target).ActivateVolumeSyncOnJoinClientRpc(targetPlayerId, newVolume);
				target.__rpc_exec_stage = (__RpcExecStage)0;
			}
		}

		protected internal override string __getTypeName()
		{
			return "VolumeSync";
		}
	}
}
namespace BoomboxControllerVolumeSync.Properties
{
	[GeneratedCode("System.Resources.Tools.StronglyTypedResourceBuilder", "17.0.0.0")]
	[DebuggerNonUserCode]
	[CompilerGenerated]
	internal class Resources
	{
		private static ResourceManager resourceMan;

		private static CultureInfo resourceCulture;

		[EditorBrowsable(EditorBrowsableState.Advanced)]
		internal static ResourceManager ResourceManager
		{
			get
			{
				if (resourceMan == null)
				{
					ResourceManager resourceManager = new ResourceManager("BoomboxControllerVolumeSync.Properties.Resources", typeof(Resources).Assembly);
					resourceMan = resourceManager;
				}
				return resourceMan;
			}
		}

		[EditorBrowsable(EditorBrowsableState.Advanced)]
		internal static CultureInfo Culture
		{
			get
			{
				return resourceCulture;
			}
			set
			{
				resourceCulture = value;
			}
		}

		internal static byte[] boomboxcontrollervolumesync
		{
			get
			{
				object @object = ResourceManager.GetObject("boomboxcontrollervolumesync", resourceCulture);
				return (byte[])@object;
			}
		}

		internal Resources()
		{
		}
	}
}
namespace BoomboxControllerVolumeSync.NetcodePatcher
{
	[AttributeUsage(AttributeTargets.Module)]
	internal class NetcodePatchedAssemblyAttribute : Attribute
	{
	}
}