Decompiled source of MusicBox v1.0.2

plugins/MusicBox.dll

Decompiled a month ago
using System;
using System.Collections;
using System.Collections.Generic;
using System.Diagnostics;
using System.IO;
using System.Reflection;
using System.Runtime.CompilerServices;
using System.Runtime.InteropServices;
using System.Runtime.Versioning;
using BepInEx;
using BepInEx.Logging;
using ExitGames.Client.Photon;
using HarmonyLib;
using MusicBox;
using Photon.Pun;
using Photon.Realtime;
using UnityEngine;
using UnityEngine.Networking;

[assembly: CompilationRelaxations(8)]
[assembly: RuntimeCompatibility(WrapNonExceptionThrows = true)]
[assembly: Debuggable(DebuggableAttribute.DebuggingModes.IgnoreSymbolStoreSequencePoints)]
[assembly: AssemblyTitle("MusicBox")]
[assembly: AssemblyDescription("")]
[assembly: AssemblyConfiguration("")]
[assembly: AssemblyCompany("")]
[assembly: AssemblyProduct("MusicBox")]
[assembly: AssemblyCopyright("Copyright ©  2026")]
[assembly: AssemblyTrademark("")]
[assembly: ComVisible(false)]
[assembly: Guid("ceaf9309-9d45-48d0-9fb4-f69e9bfabec7")]
[assembly: AssemblyFileVersion("1.0.0.0")]
[assembly: TargetFramework(".NETFramework,Version=v4.7.2", FrameworkDisplayName = ".NET Framework 4.7.2")]
[assembly: AssemblyVersion("1.0.0.0")]
public class MusicBoxNetworkSpawner : MonoBehaviour, IOnEventCallback
{
	private void OnEnable()
	{
		PhotonNetwork.AddCallbackTarget((object)this);
	}

	private void OnDisable()
	{
		PhotonNetwork.RemoveCallbackTarget((object)this);
	}

	public void OnEvent(EventData photonEvent)
	{
		//IL_0021: Unknown result type (might be due to invalid IL or missing references)
		//IL_0026: 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)
		if (photonEvent.Code == 77)
		{
			object[] obj = (object[])photonEvent.CustomData;
			int num = (int)obj[0];
			Vector3 pos = (Vector3)obj[1];
			MusicBoxPlugin.Log.LogInfo((object)$"Evento 77 recibido! viewID={num}");
			SpawnLocal(pos, num);
		}
	}

	private void SpawnLocal(Vector3 pos, int viewID)
	{
		//IL_0026: Unknown result type (might be due to invalid IL or missing references)
		//IL_0027: Unknown result type (might be due to invalid IL or missing references)
		if ((Object)(object)GameObject.Find("MusicBoxObject") != (Object)null)
		{
			return;
		}
		ValuableObject[] array = Resources.FindObjectsOfTypeAll<ValuableObject>();
		if (array.Length != 0)
		{
			GameObject val = Object.Instantiate<GameObject>(((Component)array[0]).gameObject, pos, Quaternion.identity);
			((Object)val).name = "MusicBoxObject";
			ValuableObject component = val.GetComponent<ValuableObject>();
			if ((Object)(object)component != (Object)null)
			{
				Object.Destroy((Object)(object)component);
			}
			PhotonView val2 = val.GetComponent<PhotonView>();
			if ((Object)(object)val2 == (Object)null)
			{
				val2 = val.AddComponent<PhotonView>();
			}
			val2.ViewID = viewID;
			PhotonNetwork.RegisterPhotonView(val2);
			MusicBoxPlugin.Log.LogInfo((object)$"PhotonView registrado con ID: {val2.ViewID}");
			val.AddComponent<MusicBoxItem>().myView = val2;
			MusicBoxPlugin.Log.LogInfo((object)"MusicBoxObject spawneado via evento!");
		}
	}
}
[HarmonyPatch(typeof(LevelGenerator), "GenerateDone")]
public class SpawnPatch
{
	private static void Postfix()
	{
		//IL_001f: Unknown result type (might be due to invalid IL or missing references)
		//IL_0024: Unknown result type (might be due to invalid IL or missing references)
		//IL_0030: Expected O, but got Unknown
		//IL_00ed: Unknown result type (might be due to invalid IL or missing references)
		//IL_00ee: Unknown result type (might be due to invalid IL or missing references)
		//IL_0096: Unknown result type (might be due to invalid IL or missing references)
		//IL_00a1: Unknown result type (might be due to invalid IL or missing references)
		//IL_00a6: Unknown result type (might be due to invalid IL or missing references)
		//IL_00a8: Unknown result type (might be due to invalid IL or missing references)
		//IL_00ad: Unknown result type (might be due to invalid IL or missing references)
		//IL_00af: Unknown result type (might be due to invalid IL or missing references)
		//IL_00b4: Unknown result type (might be due to invalid IL or missing references)
		//IL_00be: Expected O, but got Unknown
		if (!SemiFunc.RunIsLevel())
		{
			return;
		}
		if ((Object)(object)GameObject.Find("MusicBoxNetworkSpawner") == (Object)null)
		{
			GameObject val = new GameObject("MusicBoxNetworkSpawner");
			val.AddComponent<MusicBoxNetworkSpawner>();
			Object.DontDestroyOnLoad((Object)val);
			MusicBoxPlugin.Log.LogInfo((object)"Spawner creado!");
		}
		if ((Object)(object)GameObject.Find("MusicBoxObject") != (Object)null)
		{
			return;
		}
		Vector3 val2 = default(Vector3);
		((Vector3)(ref val2))..ctor(0f, 1f, 3f);
		if (PhotonNetwork.IsConnected && PhotonNetwork.InRoom)
		{
			if (PhotonNetwork.IsMasterClient)
			{
				int num = PhotonNetwork.AllocateViewID(true);
				object[] array = new object[2] { num, val2 };
				PhotonNetwork.RaiseEvent((byte)77, (object)array, new RaiseEventOptions
				{
					Receivers = (ReceiverGroup)1,
					CachingOption = (EventCaching)4
				}, SendOptions.SendReliable);
				MusicBoxPlugin.Log.LogInfo((object)$"Evento 77 enviado con viewID={num}");
			}
			return;
		}
		ValuableObject[] array2 = Resources.FindObjectsOfTypeAll<ValuableObject>();
		if (array2.Length != 0)
		{
			GameObject obj = Object.Instantiate<GameObject>(((Component)array2[0]).gameObject, val2, Quaternion.identity);
			((Object)obj).name = "MusicBoxObject";
			ValuableObject component = obj.GetComponent<ValuableObject>();
			if ((Object)(object)component != (Object)null)
			{
				Object.Destroy((Object)(object)component);
			}
			obj.AddComponent<MusicBoxItem>();
			MusicBoxPlugin.Log.LogInfo((object)"MusicBox spawneado (singleplayer)!");
		}
	}
}
namespace MusicBox;

public class MusicBoxItem : MonoBehaviour
{
	[CompilerGenerated]
	private sealed class <PlaySong>d__18 : IEnumerator<object>, IDisposable, IEnumerator
	{
		private int <>1__state;

		private object <>2__current;

		public MusicBoxItem <>4__this;

		public string path;

		private UnityWebRequest <www>5__2;

		object IEnumerator<object>.Current
		{
			[DebuggerHidden]
			get
			{
				return <>2__current;
			}
		}

		object IEnumerator.Current
		{
			[DebuggerHidden]
			get
			{
				return <>2__current;
			}
		}

		[DebuggerHidden]
		public <PlaySong>d__18(int <>1__state)
		{
			this.<>1__state = <>1__state;
		}

		[DebuggerHidden]
		void IDisposable.Dispose()
		{
			int num = <>1__state;
			if (num == -3 || num == 1)
			{
				try
				{
				}
				finally
				{
					<>m__Finally1();
				}
			}
			<www>5__2 = null;
			<>1__state = -2;
		}

		private bool MoveNext()
		{
			//IL_0093: Unknown result type (might be due to invalid IL or missing references)
			//IL_0099: Invalid comparison between Unknown and I4
			//IL_0041: Unknown result type (might be due to invalid IL or missing references)
			//IL_0053: Unknown result type (might be due to invalid IL or missing references)
			try
			{
				int num = <>1__state;
				MusicBoxItem musicBoxItem = <>4__this;
				switch (num)
				{
				default:
					return false;
				case 0:
				{
					<>1__state = -1;
					musicBoxItem.StopSong();
					AudioType val = (AudioType)(path.EndsWith(".wav") ? 20 : 13);
					<www>5__2 = UnityWebRequestMultimedia.GetAudioClip("file:///" + path, val);
					<>1__state = -3;
					<>2__current = <www>5__2.SendWebRequest();
					<>1__state = 1;
					return true;
				}
				case 1:
					<>1__state = -3;
					if ((int)<www>5__2.result == 1)
					{
						AudioClip content = DownloadHandlerAudioClip.GetContent(<www>5__2);
						musicBoxItem.audioSource.clip = content;
						musicBoxItem.audioSource.Play();
						musicBoxItem.isPlaying = true;
						MusicBoxPlugin.Log.LogInfo((object)("Reproduciendo: " + Path.GetFileNameWithoutExtension(path)));
					}
					else
					{
						MusicBoxPlugin.Log.LogInfo((object)("Error cargando canción: " + <www>5__2.error));
					}
					<>m__Finally1();
					<www>5__2 = null;
					return false;
				}
			}
			catch
			{
				//try-fault
				((IDisposable)this).Dispose();
				throw;
			}
		}

		bool IEnumerator.MoveNext()
		{
			//ILSpy generated this explicit interface implementation from .override directive in MoveNext
			return this.MoveNext();
		}

		private void <>m__Finally1()
		{
			<>1__state = -1;
			if (<www>5__2 != null)
			{
				((IDisposable)<www>5__2).Dispose();
			}
		}

		[DebuggerHidden]
		void IEnumerator.Reset()
		{
			throw new NotSupportedException();
		}
	}

	private AudioSource audioSource;

	private List<string> songPaths = new List<string>();

	private int currentIndex;

	private bool showMenu;

	private bool isPlaying;

	private Vector2 scrollPos = Vector2.zero;

	public PhotonView myView;

	private float volume = 0.8f;

	private void Awake()
	{
		MusicBoxPlugin.Log.LogInfo((object)"MusicBoxItem Awake llamado!");
	}

	private void Start()
	{
		//IL_0081: 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_00b5: Unknown result type (might be due to invalid IL or missing references)
		MusicBoxPlugin.Log.LogInfo((object)"MusicBoxItem Start llamado!");
		ManualLogSource log = MusicBoxPlugin.Log;
		PhotonView obj = myView;
		log.LogInfo((object)$"MusicBoxItem Start - PhotonView ID: {((obj != null) ? new int?(obj.ViewID) : null)}");
		Renderer[] componentsInChildren = ((Component)this).GetComponentsInChildren<Renderer>();
		for (int i = 0; i < componentsInChildren.Length; i++)
		{
			Object.Destroy((Object)(object)componentsInChildren[i]);
		}
		GameObject obj2 = GameObject.CreatePrimitive((PrimitiveType)3);
		obj2.transform.SetParent(((Component)this).transform);
		obj2.transform.localPosition = Vector3.zero;
		obj2.transform.localScale = new Vector3(0.3f, 0.3f, 0.3f);
		obj2.GetComponent<Renderer>().material.color = Color.red;
		Object.Destroy((Object)(object)obj2.GetComponent<Collider>());
		audioSource = ((Component)this).gameObject.AddComponent<AudioSource>();
		audioSource.spatialBlend = 1f;
		audioSource.volume = volume;
		LoadSongs();
	}

	private void Update()
	{
		if (Time.frameCount % 300 == 0)
		{
			MusicBoxPlugin.Log.LogInfo((object)$"[UPDATE] Vivo! showMenu={showMenu}");
		}
		if (Input.GetKeyDown((KeyCode)109))
		{
			showMenu = !showMenu;
			MusicBoxPlugin.Log.LogInfo((object)$"[UPDATE] M PULSADA! showMenu={showMenu}");
			if (showMenu)
			{
				Cursor.lockState = (CursorLockMode)0;
				Cursor.visible = true;
			}
			else
			{
				Cursor.lockState = (CursorLockMode)1;
				Cursor.visible = false;
			}
		}
	}

	private void LoadSongs()
	{
		songPaths.Clear();
		songPaths.AddRange(Directory.GetFiles(MusicBoxPlugin.SongsFolder, "*.mp3"));
		songPaths.AddRange(Directory.GetFiles(MusicBoxPlugin.SongsFolder, "*.wav"));
		MusicBoxPlugin.Log.LogInfo((object)$"MusicBox: {songPaths.Count} canciones encontradas.");
	}

	private void PlayRequest(string songName)
	{
		if (PhotonNetwork.IsConnected && PhotonNetwork.CurrentRoom != null)
		{
			myView.RPC("RPC_Play", (RpcTarget)0, new object[1] { songName });
		}
		else
		{
			RPC_Play(songName);
		}
	}

	private void StopRequest()
	{
		if (PhotonNetwork.IsConnected && PhotonNetwork.CurrentRoom != null)
		{
			myView.RPC("RPC_Stop", (RpcTarget)0, Array.Empty<object>());
		}
		else
		{
			RPC_Stop();
		}
	}

	[PunRPC]
	private void RPC_Play(string songName)
	{
		string text = FindSongPath(songName);
		if (text != null)
		{
			((MonoBehaviour)this).StartCoroutine(PlaySong(text));
		}
	}

	[PunRPC]
	private void RPC_Stop()
	{
		StopSong();
	}

	private string FindSongPath(string songName)
	{
		foreach (string songPath in songPaths)
		{
			if (Path.GetFileNameWithoutExtension(songPath) == songName)
			{
				return songPath;
			}
		}
		return null;
	}

	private void OnGUI()
	{
		//IL_002f: 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_0080: Unknown result type (might be due to invalid IL or missing references)
		//IL_0085: Unknown result type (might be due to invalid IL or missing references)
		if (!showMenu)
		{
			return;
		}
		GUILayout.BeginArea(new Rect((float)(Screen.width / 2 - 175), (float)(Screen.height / 2 - 250), 350f, 480f));
		GUILayout.BeginVertical(GUIStyle.op_Implicit("box"), Array.Empty<GUILayoutOption>());
		GUILayout.Label("\ud83c\udfb5 MusicBox", Array.Empty<GUILayoutOption>());
		GUILayout.Space(10f);
		scrollPos = GUILayout.BeginScrollView(scrollPos, (GUILayoutOption[])(object)new GUILayoutOption[1] { GUILayout.Height(300f) });
		for (int i = 0; i < songPaths.Count; i++)
		{
			string fileNameWithoutExtension = Path.GetFileNameWithoutExtension(songPaths[i]);
			if (GUILayout.Button(((i == currentIndex && isPlaying) ? "▶ " : "   ") + fileNameWithoutExtension, Array.Empty<GUILayoutOption>()))
			{
				currentIndex = i;
				PlayRequest(fileNameWithoutExtension);
			}
		}
		GUILayout.EndScrollView();
		GUILayout.Label($"\ud83d\udd0a Volumen: {Mathf.RoundToInt(volume * 100f)}%", Array.Empty<GUILayoutOption>());
		float num = GUILayout.HorizontalSlider(volume, 0f, 1f, Array.Empty<GUILayoutOption>());
		if (num != volume)
		{
			volume = num;
			audioSource.volume = volume;
		}
		GUILayout.Space(5f);
		if (GUILayout.Button(isPlaying ? "⏹ Stop" : "▶ Play", Array.Empty<GUILayoutOption>()))
		{
			if (isPlaying)
			{
				StopRequest();
			}
			else if (songPaths.Count > 0)
			{
				PlayRequest(Path.GetFileNameWithoutExtension(songPaths[currentIndex]));
			}
		}
		if (GUILayout.Button("✕ Cerrar", Array.Empty<GUILayoutOption>()))
		{
			showMenu = false;
			Cursor.lockState = (CursorLockMode)1;
			Cursor.visible = false;
		}
		GUILayout.EndVertical();
		GUILayout.EndArea();
	}

	[IteratorStateMachine(typeof(<PlaySong>d__18))]
	private IEnumerator PlaySong(string path)
	{
		//yield-return decompiler failed: Unexpected instruction in Iterator.Dispose()
		return new <PlaySong>d__18(0)
		{
			<>4__this = this,
			path = path
		};
	}

	private void StopSong()
	{
		audioSource.Stop();
		isPlaying = false;
	}
}
[BepInPlugin("com.ErMichos.musicbox", "MusicBox", "1.0.2")]
public class MusicBoxPlugin : BaseUnityPlugin
{
	internal static ManualLogSource Log;

	public static string SongsFolder;

	private void Awake()
	{
		//IL_004d: Unknown result type (might be due to invalid IL or missing references)
		Log = ((BaseUnityPlugin)this).Logger;
		SongsFolder = Path.Combine(Paths.PluginPath, "ErMichos-MusicBox", "songs");
		Directory.CreateDirectory(SongsFolder);
		Log.LogInfo((object)("MusicBox cargado. Carpeta de canciones: " + SongsFolder));
		new Harmony("com.ErMichos.musicbox").PatchAll();
	}
}