Decompiled source of Ordered Custom Boombox v2.0.1

BepInEx/Plugins/Ordered_Custom_Boombox.dll

Decompiled 4 months ago
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Reflection;
using System.Reflection.Emit;
using System.Runtime.CompilerServices;
using System.Runtime.InteropServices;
using System.Runtime.Versioning;
using BepInEx;
using BepInEx.Logging;
using GameNetcodeStuff;
using HarmonyLib;
using Ordered_Custom_Boombox.Patches;
using Unity.Collections;
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("Ordered_Custom_Boombox")]
[assembly: AssemblyDescription("")]
[assembly: AssemblyConfiguration("")]
[assembly: AssemblyCompany("")]
[assembly: AssemblyProduct("Ordered_Custom_Boombox")]
[assembly: AssemblyCopyright("Copyright ©  2024")]
[assembly: AssemblyTrademark("")]
[assembly: ComVisible(false)]
[assembly: Guid("3ed6de34-ecdd-4113-8fb9-d05e7e517fd3")]
[assembly: AssemblyFileVersion("1.0.0.0")]
[assembly: TargetFramework(".NETFramework,Version=v4.8", FrameworkDisplayName = ".NET Framework 4.8")]
[assembly: AssemblyVersion("1.0.0.0")]
namespace Ordered_Custom_Boombox
{
	[BepInPlugin("Hackattack242.Ordered_Custom_Boombox", "Ordered Custom Boombox", "2.0.1")]
	public class Ordered_custom_boombox_base : BaseUnityPlugin
	{
		private const string mod_GUID = "Hackattack242.Ordered_Custom_Boombox";

		private const string mod_name = "Ordered Custom Boombox";

		private const string mod_version = "2.0.1";

		private readonly Harmony harmony = new Harmony("Hackattack242.Ordered_Custom_Boombox");

		private static Ordered_custom_boombox_base Instance;

		private ManualLogSource logger;

		private void Awake()
		{
			if ((Object)(object)Instance == (Object)null)
			{
				Instance = this;
			}
			logger = Logger.CreateLogSource("Ordered Custom Boombox");
			logger.LogInfo((object)"Ordered Custom Boombox has awoken");
			harmony.PatchAll();
			logger.LogInfo((object)"Ordered Custom Boombox Patches Applied");
		}

		internal static void LogDebug(string message)
		{
			Instance.Log(message, (LogLevel)32);
		}

		internal static void LogInfo(string message)
		{
			Instance.Log(message, (LogLevel)16);
		}

		internal static void LogWarning(string message)
		{
			Instance.Log(message, (LogLevel)4);
		}

		internal static void LogError(string message)
		{
			Instance.Log(message, (LogLevel)2);
		}

		internal static void LogError(Exception ex)
		{
			Instance.Log(ex.Message + "\n" + ex.StackTrace, (LogLevel)2);
		}

		private void Log(string message, LogLevel logLevel)
		{
			//IL_0007: Unknown result type (might be due to invalid IL or missing references)
			logger.Log(logLevel, (object)message);
		}
	}
}
namespace Ordered_Custom_Boombox.Patches
{
	[HarmonyPatch(typeof(GrabbableObject))]
	internal class Boombox_Grabbable_Item_patch
	{
		[HarmonyPatch("ItemInteractLeftRight")]
		[HarmonyPostfix]
		private static void Patch_interact_left_right(ref GrabbableObject __instance, object[] __args)
		{
			//IL_005b: Unknown result type (might be due to invalid IL or missing references)
			//IL_006a: Unknown result type (might be due to invalid IL or missing references)
			//IL_002a: Unknown result type (might be due to invalid IL or missing references)
			//IL_0046: Unknown result type (might be due to invalid IL or missing references)
			if (__instance is BoomboxItem)
			{
				if (!(bool)__args[0])
				{
					Boombox_start_music_patch.seek_track(((NetworkBehaviour)__instance).NetworkObjectId, -1, ((BoomboxItem)__instance).musicAudios.Length);
					Boombox_start_music_patch.seek_track(((NetworkBehaviour)__instance).NetworkObjectId, -1, ((BoomboxItem)__instance).musicAudios.Length);
				}
				((GrabbableObject)(BoomboxItem)__instance).ItemActivate(false, true);
				((GrabbableObject)(BoomboxItem)__instance).ItemActivate(true, true);
			}
		}

		[HarmonyPatch("EquipItem")]
		[HarmonyPostfix]
		private static void Patch_equip_item(ref GrabbableObject __instance, ref PlayerControllerB ___playerHeldBy)
		{
			if (__instance is BoomboxItem)
			{
				___playerHeldBy.equippedUsableItemQE = true;
			}
		}

		[HarmonyPatch("DiscardItem")]
		[HarmonyPrefix]
		private static void Patch_discard_item(ref GrabbableObject __instance, ref PlayerControllerB ___playerHeldBy)
		{
			if (__instance is BoomboxItem)
			{
				___playerHeldBy.equippedUsableItemQE = false;
			}
		}
	}
	[HarmonyPatch(typeof(BoomboxItem))]
	internal class Boombox_start_music_patch
	{
		internal static Dictionary<ulong, int> track_num_map = new Dictionary<ulong, int>();

		public static int seek_track(ulong bbox_id, int seek_amount, int length)
		{
			if (!track_num_map.ContainsKey(bbox_id))
			{
				track_num_map.Add(bbox_id, 10000000);
			}
			track_num_map[bbox_id] += seek_amount;
			if (track_num_map[bbox_id] >= length)
			{
				track_num_map[bbox_id] = 0;
			}
			if (track_num_map[bbox_id] < 0)
			{
				track_num_map[bbox_id] = length - 1;
			}
			return track_num_map[bbox_id];
		}

		public static IEnumerable<ulong> get_all_bbox_ids()
		{
			foreach (ulong key in track_num_map.Keys)
			{
				Ordered_custom_boombox_base.LogInfo("key: " + key);
			}
			return track_num_map.Keys;
		}

		[HarmonyPatch("Start")]
		[HarmonyPostfix]
		private static void patch_boombox_start(ref BoomboxItem __instance)
		{
			((GrabbableObject)__instance).itemProperties.syncInteractLRFunction = true;
			string[] array = new string[((GrabbableObject)__instance).itemProperties.toolTips.Length + 2];
			for (int i = 0; i < ((GrabbableObject)__instance).itemProperties.toolTips.Length; i++)
			{
				array[i] = ((GrabbableObject)__instance).itemProperties.toolTips[i];
			}
			array[((GrabbableObject)__instance).itemProperties.toolTips.Length] = "Prev Song: [Q]";
			array[((GrabbableObject)__instance).itemProperties.toolTips.Length + 1] = "Next Song: [E]";
			((GrabbableObject)__instance).itemProperties.toolTips = array;
		}

		[HarmonyPatch("StartMusic")]
		[HarmonyTranspiler]
		private static IEnumerable<CodeInstruction> patch_start_music(IEnumerable<CodeInstruction> instructions)
		{
			//IL_007a: Unknown result type (might be due to invalid IL or missing references)
			//IL_0084: Expected O, but got Unknown
			//IL_008f: Unknown result type (might be due to invalid IL or missing references)
			//IL_0099: Expected O, but got Unknown
			//IL_0136: Unknown result type (might be due to invalid IL or missing references)
			//IL_0140: Expected O, but got Unknown
			List<CodeInstruction> list = new List<CodeInstruction>(instructions);
			for (int i = 0; i < list.Count; i++)
			{
				if (list[i].opcode == OpCodes.Ldfld && (FieldInfo)list[i].operand == AccessTools.Field(typeof(BoomboxItem), "musicRandomizer"))
				{
					list[i] = new CodeInstruction(OpCodes.Call, (object)AccessTools.Property(typeof(NetworkBehaviour), "NetworkObjectId").GetGetMethod());
					list[i + 1] = new CodeInstruction(OpCodes.Ldc_I4_1, (object)null);
				}
				else if (CodeInstructionExtensions.Calls(list[i], typeof(Random).GetMethod("Next", new Type[2]
				{
					typeof(int),
					typeof(int)
				})))
				{
					Type typeFromHandle = typeof(Boombox_start_music_patch);
					string text = "seek_track";
					list[i] = new CodeInstruction(OpCodes.Call, (object)AccessTools.Method(typeFromHandle, text, new Type[3]
					{
						typeof(ulong),
						typeof(int),
						typeof(int)
					}, (Type[])null));
				}
			}
			return list;
		}
	}
}
namespace Ordered_Custom_Boombox.Netcode
{
	[HarmonyPatch]
	public static class Sync_track_num
	{
		internal static IEnumerable<ulong> get_bbox_ids()
		{
			return Boombox_start_music_patch.get_all_bbox_ids();
		}

		internal static int get_track_num(ulong bbox_id)
		{
			return Boombox_start_music_patch.track_num_map[bbox_id];
		}

		internal static void set_track_num(ulong bbox_id, int new_track_num)
		{
			Boombox_start_music_patch.track_num_map[bbox_id] = new_track_num;
		}

		[HarmonyPatch(typeof(PlayerControllerB), "ConnectClientToPlayerObject")]
		[HarmonyPostfix]
		public static void Init()
		{
			//IL_0031: Unknown result type (might be due to invalid IL or missing references)
			//IL_003b: Expected O, but got Unknown
			//IL_006f: Unknown result type (might be due to invalid IL or missing references)
			//IL_0079: Expected O, but got Unknown
			if (NetworkManager.Singleton.IsServer)
			{
				Ordered_custom_boombox_base.LogInfo("Set up track number sync in server");
				NetworkManager.Singleton.CustomMessagingManager.RegisterNamedMessageHandler("Ordered_Custom_Boombox.Receive_server_set_track_num_rpc", new HandleNamedMessageDelegate(Receive_server_set_track_num_rpc));
			}
			else if (NetworkManager.Singleton.IsClient)
			{
				Ordered_custom_boombox_base.LogInfo("Set up track number sync in client");
				NetworkManager.Singleton.CustomMessagingManager.RegisterNamedMessageHandler("Ordered_Custom_Boombox.Receive_client_set_track_num_rpc", new HandleNamedMessageDelegate(Receive_client_set_track_num_rpc));
			}
			Client_send_sync_request_to_server();
		}

		public static void Client_send_sync_request_to_server()
		{
			//IL_009a: Unknown result type (might be due to invalid IL or missing references)
			//IL_00a0: Unknown result type (might be due to invalid IL or missing references)
			//IL_00b9: Unknown result type (might be due to invalid IL or missing references)
			if (!NetworkManager.Singleton.IsClient)
			{
				Ordered_custom_boombox_base.LogWarning("[Client_send_sync_request_to_server] Not a client?");
				return;
			}
			if (NetworkManager.Singleton.IsServer)
			{
				IEnumerable<ulong> bbox_ids = get_bbox_ids();
				{
					foreach (ulong item in bbox_ids)
					{
						Server_send_track_num_sync(item, get_track_num(item));
					}
					return;
				}
			}
			Ordered_custom_boombox_base.LogInfo("Requesting track num sync from server");
			FastBufferWriter val = default(FastBufferWriter);
			((FastBufferWriter)(ref val))..ctor(4, (Allocator)2, -1);
			int num = 1337;
			((FastBufferWriter)(ref val)).WriteValue<int>(ref num, default(ForPrimitives));
			NetworkManager.Singleton.CustomMessagingManager.SendNamedMessage("Ordered_Custom_Boombox.Receive_server_set_track_num_rpc", 0uL, val, (NetworkDelivery)3);
		}

		private static void Receive_server_set_track_num_rpc(ulong clientId, FastBufferReader reader)
		{
			if (!NetworkManager.Singleton.IsServer)
			{
				return;
			}
			IEnumerable<ulong> bbox_ids = get_bbox_ids();
			foreach (ulong item in bbox_ids)
			{
				Server_send_track_num_sync(item, get_track_num(item));
			}
		}

		public static void Server_send_track_num_sync(ulong bbox_id, int server_track_num)
		{
			//IL_0055: Unknown result type (might be due to invalid IL or missing references)
			//IL_005b: 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_006e: Unknown result type (might be due to invalid IL or missing references)
			//IL_0084: Unknown result type (might be due to invalid IL or missing references)
			if (!NetworkManager.Singleton.IsServer)
			{
				Ordered_custom_boombox_base.LogWarning("[Server_send_track_num_sync] Only the server can call this method!");
				return;
			}
			Ordered_custom_boombox_base.LogInfo("Syncing track num of " + server_track_num + " with clients for id " + bbox_id);
			FastBufferWriter val = default(FastBufferWriter);
			((FastBufferWriter)(ref val))..ctor(12, (Allocator)2, -1);
			((FastBufferWriter)(ref val)).WriteValue<ulong>(ref bbox_id, default(ForPrimitives));
			((FastBufferWriter)(ref val)).WriteValue<int>(ref server_track_num, default(ForPrimitives));
			NetworkManager.Singleton.CustomMessagingManager.SendNamedMessageToAll("Ordered_Custom_Boombox.Receive_client_set_track_num_rpc", val, (NetworkDelivery)3);
		}

		private static void Receive_client_set_track_num_rpc(ulong clientId, FastBufferReader reader)
		{
			//IL_0027: Unknown result type (might be due to invalid IL or missing references)
			//IL_002d: Unknown result type (might be due to invalid IL or missing references)
			//IL_003a: Unknown result type (might be due to invalid IL or missing references)
			//IL_0040: Unknown result type (might be due to invalid IL or missing references)
			if (NetworkManager.Singleton.IsClient && !NetworkManager.Singleton.IsServer)
			{
				ulong bbox_id = default(ulong);
				((FastBufferReader)(ref reader)).ReadValue<ulong>(ref bbox_id, default(ForPrimitives));
				int new_track_num = default(int);
				((FastBufferReader)(ref reader)).ReadValue<int>(ref new_track_num, default(ForPrimitives));
				Ordered_custom_boombox_base.LogInfo("setting track number for id " + bbox_id + " client via rpc: " + new_track_num);
				set_track_num(bbox_id, new_track_num);
			}
		}
	}
}