Decompiled source of An0n AmbientMusicSync v1.0.2

BepInEx/plugins/An0n_AmbientMusicSync.dll

Decompiled 9 months ago
using System;
using System.Diagnostics;
using System.Reflection;
using System.Runtime.CompilerServices;
using System.Runtime.InteropServices;
using System.Runtime.Versioning;
using An0n_Patches.Patches;
using BepInEx;
using BepInEx.Configuration;
using BepInEx.Logging;
using GameNetcodeStuff;
using HarmonyLib;
using LC_API.ServerAPI;
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("An0n_MusicSync")]
[assembly: AssemblyDescription("")]
[assembly: AssemblyConfiguration("")]
[assembly: AssemblyCompany("")]
[assembly: AssemblyProduct("An0n_MusicSync")]
[assembly: AssemblyCopyright("Copyright ©  2023")]
[assembly: AssemblyTrademark("")]
[assembly: ComVisible(false)]
[assembly: Guid("415f18a2-c213-4036-b3f0-7673c0a38c58")]
[assembly: AssemblyFileVersion("1.0.0.0")]
[assembly: TargetFramework(".NETFramework,Version=v4.6", FrameworkDisplayName = ".NET Framework 4.6")]
[assembly: AssemblyVersion("1.0.0.0")]
namespace An0n_Patches
{
	[BepInPlugin("com.an0n.ambientmusicsync", "An0n AmbientMusicSync", "1.0.2")]
	[BepInDependency(/*Could not decode attribute arguments.*/)]
	public class An0n_Patch_Plugin : BaseUnityPlugin
	{
		public const string pluginGUID = "com.an0n.ambientmusicsync";

		private const string pluginName = "An0n AmbientMusicSync";

		private const string pluginVersion = "1.0.2";

		public static ManualLogSource mls = Logger.CreateLogSource("com.an0n.ambientmusicsync");

		private Harmony harmony = new Harmony("com.an0n.ambientmusicsync");

		public static ConfigEntry<float> ambientChance;

		private Harmony patcher;

		private void Awake()
		{
			//IL_0017: Unknown result type (might be due to invalid IL or missing references)
			//IL_0021: Expected O, but got Unknown
			mls.LogInfo((object)"An0n AmbientMusicSync Plugin Loaded");
			patcher = new Harmony("com.an0n.ambientmusicsync");
			patcher.PatchAll(typeof(SoundManagerPatch));
			ambientChance = ((BaseUnityPlugin)this).Config.Bind<float>("General", "ambientChance", 4f, "the 1/x chance ambient music will play upon exiting the ship on a new day. Default is 1/4.");
			Networking.GetInt = (Action<int, string>)Delegate.Combine(Networking.GetInt, new Action<int, string>(syncPatch.GetNetworkIntBroadcast));
		}
	}
}
namespace An0n_Patches.Patches
{
	internal class syncPatch
	{
		public static void GetNetworkIntBroadcast(int data, string signature)
		{
			//IL_0050: Unknown result type (might be due to invalid IL or missing references)
			//IL_0056: Invalid comparison between Unknown and I4
			if (!(signature == "com.an0n.ambientmusicsync"))
			{
				return;
			}
			SoundManager instance = SoundManager.Instance;
			float num = (float)Traverse.Create((object)instance).Field("timeSincePlayingLastMusic").GetValue();
			if (num < 200f)
			{
				return;
			}
			if ((int)TimeOfDay.Instance.dayMode >= 2)
			{
				if (instance.EveningMusic.Length == 0)
				{
					return;
				}
				instance.musicSource.clip = instance.EveningMusic[data];
			}
			else
			{
				instance.musicSource.clip = instance.DaytimeMusic[data];
			}
			instance.musicSource.Play();
			instance.playingOutsideMusic = true;
			Traverse.Create((object)instance).Field("timeSincePlayingLastMusic").SetValue((object)0f);
		}
	}
	[HarmonyPatch(typeof(SoundManager))]
	internal class SoundManagerPatch
	{
		private static bool firstWalkOut = false;

		private static bool hasLanded = false;

		private static bool newLobby = true;

		private static bool isCompany = false;

		public static bool anyPlayerExit()
		{
			PlayerControllerB[] allPlayerScripts = GameNetworkManager.Instance.localPlayerController.playersManager.allPlayerScripts;
			for (int i = 0; i < allPlayerScripts.Length; i++)
			{
				if (!allPlayerScripts[i].isInHangarShipRoom)
				{
					return true;
				}
			}
			return false;
		}

		[HarmonyPatch(typeof(SoundManager), "PlayRandomOutsideMusic")]
		[HarmonyPrefix]
		public static bool hijackRandomOutsideMusic()
		{
			if (((NetworkBehaviour)GameNetworkManager.Instance.localPlayerController).IsHost)
			{
				Debug.Log((object)"[An0nPatch] Host rand music play, broadcasting");
				int num = Random.Range(0, SoundManager.Instance.DaytimeMusic.Length);
				Networking.Broadcast(num, "com.an0n.ambientmusicsync");
				syncPatch.GetNetworkIntBroadcast(num, "com.an0n.ambientmusicsync");
			}
			return false;
		}

		[HarmonyPatch(typeof(SoundManager), "Update")]
		[HarmonyPrefix]
		public static void MusicSync()
		{
			//IL_0095: Unknown result type (might be due to invalid IL or missing references)
			if (!StartOfRound.Instance.shipDoorsEnabled)
			{
				hasLanded = false;
				firstWalkOut = false;
			}
			else
			{
				hasLanded = true;
			}
			if (StartOfRound.Instance.currentLevel.PlanetName == "71 Gordion")
			{
				isCompany = true;
			}
			else
			{
				isCompany = false;
			}
			if (!((Object)(object)GameNetworkManager.Instance.localPlayerController != (Object)null))
			{
				return;
			}
			if (!isCompany && !firstWalkOut && hasLanded && ((Component)GameNetworkManager.Instance.localPlayerController).transform.position.y < 3f && anyPlayerExit())
			{
				firstWalkOut = true;
				Debug.Log((object)"[An0nPatch] Any player exited");
				if (Random.Range(0f, An0n_Patch_Plugin.ambientChance.Value) == 0f || newLobby)
				{
					newLobby = false;
					if (((NetworkBehaviour)GameNetworkManager.Instance.localPlayerController).IsHost)
					{
						Debug.Log((object)"[An0nPatch] Host walked out of ship, playing synced song");
						int num = Random.Range(0, SoundManager.Instance.DaytimeMusic.Length);
						Networking.Broadcast(num, "com.an0n.ambientmusicsync");
						syncPatch.GetNetworkIntBroadcast(num, "com.an0n.ambientmusicsync");
					}
				}
			}
			if (!SoundManager.Instance.musicSource.isPlaying)
			{
				SoundManager.Instance.playingOutsideMusic = false;
			}
		}

		[HarmonyPatch(typeof(TimeOfDay), "playSoundDelayed")]
		[HarmonyPrefix]
		public static bool stopDayFXifMusic()
		{
			if (SoundManager.Instance.musicSource.isPlaying)
			{
				return false;
			}
			return true;
		}

		[HarmonyPatch(typeof(GameNetworkManager), "Disconnect")]
		[HarmonyPostfix]
		public static void resetNewLobbyVar()
		{
			newLobby = true;
		}
	}
}