Decompiled source of LevelMusicLib v1.0.2

DarthLilo.LevelMusicLib.dll

Decompiled 2 months ago
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Linq;
using System.Reflection;
using System.Runtime.CompilerServices;
using System.Runtime.Versioning;
using System.Security;
using System.Security.Permissions;
using BepInEx;
using BepInEx.Logging;
using HarmonyLib;
using LevelMusicLib.Scripts;
using Microsoft.CodeAnalysis;
using UnityEngine;
using UnityEngine.SceneManagement;

[assembly: CompilationRelaxations(8)]
[assembly: RuntimeCompatibility(WrapNonExceptionThrows = true)]
[assembly: Debuggable(DebuggableAttribute.DebuggingModes.Default | DebuggableAttribute.DebuggingModes.DisableOptimizations | DebuggableAttribute.DebuggingModes.IgnoreSymbolStoreSequencePoints | DebuggableAttribute.DebuggingModes.EnableEditAndContinue)]
[assembly: TargetFramework(".NETStandard,Version=v2.1", FrameworkDisplayName = ".NET Standard 2.1")]
[assembly: AssemblyCompany("DarthLilo.LevelMusicLib")]
[assembly: AssemblyConfiguration("Debug")]
[assembly: AssemblyFileVersion("1.0.2.0")]
[assembly: AssemblyInformationalVersion("1.0.2+59143db8d20e5c01ece66d24a2abcff6e3077e4a")]
[assembly: AssemblyProduct("LevelMusicLib")]
[assembly: AssemblyTitle("DarthLilo.LevelMusicLib")]
[assembly: SecurityPermission(SecurityAction.RequestMinimum, SkipVerification = true)]
[assembly: AssemblyVersion("1.0.2.0")]
[module: UnverifiableCode]
[module: RefSafetyRules(11)]
namespace Microsoft.CodeAnalysis
{
	[CompilerGenerated]
	[Microsoft.CodeAnalysis.Embedded]
	internal sealed class EmbeddedAttribute : Attribute
	{
	}
}
namespace System.Runtime.CompilerServices
{
	[CompilerGenerated]
	[Microsoft.CodeAnalysis.Embedded]
	[AttributeUsage(AttributeTargets.Class | AttributeTargets.Property | AttributeTargets.Field | AttributeTargets.Event | AttributeTargets.Parameter | AttributeTargets.ReturnValue | AttributeTargets.GenericParameter, AllowMultiple = false, Inherited = false)]
	internal sealed class NullableAttribute : Attribute
	{
		public readonly byte[] NullableFlags;

		public NullableAttribute(byte P_0)
		{
			NullableFlags = new byte[1] { P_0 };
		}

		public NullableAttribute(byte[] P_0)
		{
			NullableFlags = P_0;
		}
	}
	[CompilerGenerated]
	[Microsoft.CodeAnalysis.Embedded]
	[AttributeUsage(AttributeTargets.Class | AttributeTargets.Struct | AttributeTargets.Method | AttributeTargets.Interface | AttributeTargets.Delegate, AllowMultiple = false, Inherited = false)]
	internal sealed class NullableContextAttribute : Attribute
	{
		public readonly byte Flag;

		public NullableContextAttribute(byte P_0)
		{
			Flag = P_0;
		}
	}
	[CompilerGenerated]
	[Microsoft.CodeAnalysis.Embedded]
	[AttributeUsage(AttributeTargets.Module, AllowMultiple = false, Inherited = false)]
	internal sealed class RefSafetyRulesAttribute : Attribute
	{
		public readonly int Version;

		public RefSafetyRulesAttribute(int P_0)
		{
			Version = P_0;
		}
	}
}
[Serializable]
public class NewMusicEntry
{
	public AudioClip clip;

	public int weight;
}
namespace LevelMusicLib
{
	[BepInPlugin("DarthLilo.LevelMusicLib", "LevelMusicLib", "1.0.2")]
	public class LevelMusicLib : BaseUnityPlugin
	{
		public static LevelMusicLib Instance { get; private set; }

		internal static ManualLogSource Logger { get; private set; }

		internal static Harmony? Harmony { get; set; }

		private void Awake()
		{
			Logger = ((BaseUnityPlugin)this).Logger;
			Instance = this;
			Patch();
			Logger.LogInfo((object)"DarthLilo.LevelMusicLib v1.0.2 has loaded!");
		}

		internal static void Patch()
		{
			//IL_000d: Unknown result type (might be due to invalid IL or missing references)
			//IL_0012: Unknown result type (might be due to invalid IL or missing references)
			//IL_0018: Expected O, but got Unknown
			if (Harmony == null)
			{
				Harmony = new Harmony("DarthLilo.LevelMusicLib");
			}
			Logger.LogDebug((object)"Patching...");
			Harmony.PatchAll();
			Logger.LogDebug((object)"Finished patching!");
		}
	}
	public static class MyPluginInfo
	{
		public const string PLUGIN_GUID = "DarthLilo.LevelMusicLib";

		public const string PLUGIN_NAME = "LevelMusicLib";

		public const string PLUGIN_VERSION = "1.0.2";
	}
}
namespace LevelMusicLib.Scripts
{
	public class CustomLevelMusic : MonoBehaviour
	{
		[Range(0f, 100f)]
		[Tooltip("Every time you land what is the chance of music playing?")]
		public int MusicChance = 20;

		public bool DisableTimeOfDayStingers;

		public NewMusicEntry[] DaytimeMusics;

		public NewMusicEntry[] EveningMusics;
	}
}
namespace LevelMusicLib.Patches
{
	[HarmonyPatch(typeof(TimeOfDay))]
	public class CustomMusicTrigger
	{
		public static int CustomSongOffset;

		[HarmonyPatch("PlayTimeMusicDelayed")]
		[HarmonyPrefix]
		private static bool PlayTimeMusicDelayedPatch(TimeOfDay __instance)
		{
			//IL_0036: Unknown result type (might be due to invalid IL or missing references)
			//IL_003b: Unknown result type (might be due to invalid IL or missing references)
			//IL_00d4: Unknown result type (might be due to invalid IL or missing references)
			//IL_00d9: Unknown result type (might be due to invalid IL or missing references)
			//IL_00a2: Unknown result type (might be due to invalid IL or missing references)
			//IL_00a8: Invalid comparison between Unknown and I4
			CustomLevelMusic customLevelMusic = Object.FindObjectOfType<CustomLevelMusic>();
			GameObject val = GameObject.Find("OutsideMusic");
			if ((Object)(object)customLevelMusic == (Object)null)
			{
				LevelMusicLib.Logger.LogInfo((object)"CustomMusicObject was null, skipping!");
				return true;
			}
			Scene scene = val.scene;
			if (((Scene)(ref scene)).name == "SampleSceneRelay")
			{
				AudioSource component = val.GetComponent<AudioSource>();
				if (!component.isPlaying && (Random.Range(0, 100) < customLevelMusic.MusicChance || ES3.Load<int>("TimesLanded", "LCGeneralSaveData", 0) <= 1))
				{
					PlayCustomMusic(customLevelMusic, component, (int)__instance.dayMode >= 2);
					if (customLevelMusic.DisableTimeOfDayStingers)
					{
						return false;
					}
				}
			}
			else
			{
				ManualLogSource logger = LevelMusicLib.Logger;
				scene = val.scene;
				logger.LogError((object)("The OutsideMusic gameobject found was of another scene [" + ((Scene)(ref scene)).name + "] please verify that the moon doesn't have another identically named object!"));
			}
			return true;
		}

		[HarmonyPatch("OnDayChanged")]
		[HarmonyPostfix]
		private static void ResetCustomSongOffset(TimeOfDay __instance)
		{
			CustomSongOffset = 0;
		}

		public static void PlayCustomMusic(CustomLevelMusic CustomMusicObject, AudioSource MusicSource, bool EveningMusic)
		{
			if (!(SoundManager.Instance.timeSincePlayingLastMusic < 200f))
			{
				if (EveningMusic && CustomMusicObject.EveningMusics.Count() >= 1)
				{
					MusicSource.clip = ChooseWeightedClip(CustomMusicObject.EveningMusics);
				}
				if (!EveningMusic && CustomMusicObject.DaytimeMusics.Count() >= 1)
				{
					MusicSource.clip = ChooseWeightedClip(CustomMusicObject.DaytimeMusics);
				}
				MusicSource.Play();
				SoundManager.Instance.playingOutsideMusic = true;
				SoundManager.Instance.timeSincePlayingLastMusic = 0f;
			}
		}

		public static AudioClip ChooseWeightedClip(NewMusicEntry[] Musics)
		{
			List<AudioClip> list = new List<AudioClip>();
			Random random = new Random(StartOfRound.Instance.randomMapSeed + CustomSongOffset);
			foreach (NewMusicEntry newMusicEntry in Musics)
			{
				for (int j = 0; j < newMusicEntry.weight; j++)
				{
					list.Add(newMusicEntry.clip);
				}
			}
			CustomSongOffset++;
			return list[random.Next(list.Count)];
		}
	}
	[HarmonyPatch(typeof(SoundManager))]
	public class VanillaMusicPatch
	{
		[HarmonyPatch("PlayRandomOutsideMusic")]
		[HarmonyPrefix]
		private static bool VanillaMusicRemovePatch(SoundManager __instance)
		{
			CustomLevelMusic customLevelMusic = Object.FindObjectOfType<CustomLevelMusic>();
			if ((Object)(object)customLevelMusic != (Object)null)
			{
				LevelMusicLib.Logger.LogInfo((object)"Cancelled vanilla music call since custom music object was found!");
				return false;
			}
			return true;
		}
	}
}