Decompiled source of LeopoldsTavernMusic v1.1.0

plugins\LeopoldsTavernMusic\LeopoldsTavernMusic.dll

Decompiled 12 minutes ago
using System;
using System.Collections;
using System.Diagnostics;
using System.IO;
using System.Reflection;
using System.Runtime.CompilerServices;
using System.Runtime.Versioning;
using BepInEx;
using BepInEx.Configuration;
using FMOD;
using FMODUnity;
using Microsoft.CodeAnalysis;
using UnityEngine;
using UnityEngine.SceneManagement;

[assembly: CompilationRelaxations(8)]
[assembly: RuntimeCompatibility(WrapNonExceptionThrows = true)]
[assembly: Debuggable(DebuggableAttribute.DebuggingModes.IgnoreSymbolStoreSequencePoints)]
[assembly: TargetFramework(".NETStandard,Version=v2.1", FrameworkDisplayName = ".NET Standard 2.1")]
[assembly: AssemblyCompany("LeopoldsTavernMusic")]
[assembly: AssemblyConfiguration("Release")]
[assembly: AssemblyFileVersion("1.0.0.0")]
[assembly: AssemblyInformationalVersion("1.0.0")]
[assembly: AssemblyProduct("LeopoldsTavernMusic")]
[assembly: AssemblyTitle("LeopoldsTavernMusic")]
[assembly: AssemblyVersion("1.0.0.0")]
[module: RefSafetyRules(11)]
namespace Microsoft.CodeAnalysis
{
	[CompilerGenerated]
	[Embedded]
	internal sealed class EmbeddedAttribute : Attribute
	{
	}
}
namespace System.Runtime.CompilerServices
{
	[CompilerGenerated]
	[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;
		}
	}
}
namespace LastPirates.LeopoldsTavernMusic
{
	[BepInPlugin("com.bwadd.lastpirates.leopoldstavernmusic", "Leopold's Tavern Level Music", "1.1.0")]
	public sealed class Plugin : BaseUnityPlugin
	{
		public const string PluginGuid = "com.bwadd.lastpirates.leopoldstavernmusic";

		public const string PluginName = "Leopold's Tavern Level Music";

		public const string PluginVersion = "1.1.0";

		private const string TrackFileName = "LeopoldsTavern.wav";

		private ConfigEntry<float> _volume;

		private ConfigEntry<bool> _loop;

		private ConfigEntry<string> _scenePrefix;

		private Sound _sound;

		private Channel _channel;

		private bool _soundReady;

		private bool _pendingLevelStart;

		private float _lastStartTime = -100f;

		private string _trackPath;

		private void Awake()
		{
			//IL_002f: Unknown result type (might be due to invalid IL or missing references)
			//IL_0039: Expected O, but got Unknown
			_volume = ((BaseUnityPlugin)this).Config.Bind<float>("Audio", "Volume", 0.7f, new ConfigDescription("Playback volume.", (AcceptableValueBase)(object)new AcceptableValueRange<float>(0f, 1f), Array.Empty<object>()));
			_loop = ((BaseUnityPlugin)this).Config.Bind<bool>("Audio", "Loop", false, "Loop the track during a level.");
			_scenePrefix = ((BaseUnityPlugin)this).Config.Bind<string>("Trigger", "SceneNamePrefix", "Level_", "Play when a loaded scene name begins with this text.");
			_trackPath = Path.Combine(Path.GetDirectoryName(((BaseUnityPlugin)this).Info.Location), "LeopoldsTavern.wav");
			SceneManager.sceneLoaded += OnSceneLoaded;
			((MonoBehaviour)this).StartCoroutine(LoadTrackThroughFmod());
		}

		private void OnDestroy()
		{
			//IL_0024: Unknown result type (might be due to invalid IL or missing references)
			//IL_0048: Unknown result type (might be due to invalid IL or missing references)
			SceneManager.sceneLoaded -= OnSceneLoaded;
			if (((Channel)(ref _channel)).hasHandle())
			{
				((Channel)(ref _channel)).stop();
				((Channel)(ref _channel)).clearHandle();
			}
			if (((Sound)(ref _sound)).hasHandle())
			{
				((Sound)(ref _sound)).release();
				((Sound)(ref _sound)).clearHandle();
			}
		}

		private IEnumerator LoadTrackThroughFmod()
		{
			if (!File.Exists(_trackPath))
			{
				((BaseUnityPlugin)this).Logger.LogError((object)("Music file not found: " + _trackPath));
				yield break;
			}
			float deadline = Time.realtimeSinceStartup + 30f;
			string lastError = "FMOD has not initialized yet.";
			while (Time.realtimeSinceStartup < deadline)
			{
				if (TryCreateFmodSound(out lastError))
				{
					_soundReady = true;
					((BaseUnityPlugin)this).Logger.LogInfo((object)"Loaded LeopoldsTavern.wav through the game's FMOD Core system.");
					if (_pendingLevelStart)
					{
						_pendingLevelStart = false;
						PlayTrack();
					}
					yield break;
				}
				yield return (object)new WaitForSecondsRealtime(0.5f);
			}
			((BaseUnityPlugin)this).Logger.LogError((object)("Could not load LeopoldsTavern.wav through FMOD within 30 seconds: " + lastError));
		}

		private bool TryCreateFmodSound(out string error)
		{
			//IL_0003: Unknown result type (might be due to invalid IL or missing references)
			//IL_0008: Unknown result type (might be due to invalid IL or missing references)
			//IL_0033: Unknown result type (might be due to invalid IL or missing references)
			//IL_0038: Unknown result type (might be due to invalid IL or missing references)
			//IL_0039: Unknown result type (might be due to invalid IL or missing references)
			//IL_0061: Unknown result type (might be due to invalid IL or missing references)
			//IL_0066: Unknown result type (might be due to invalid IL or missing references)
			//IL_0067: 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_0048: Unknown result type (might be due to invalid IL or missing references)
			error = null;
			try
			{
				System coreSystem = RuntimeManager.CoreSystem;
				if (!((System)(ref coreSystem)).hasHandle())
				{
					error = "FMOD Core system has no handle.";
					return false;
				}
				RESULT val = ((System)(ref coreSystem)).createSound(_trackPath, (MODE)137, ref _sound);
				if ((int)val != 0)
				{
					error = $"createSound returned {val}: {Error.String(val)}";
					return false;
				}
				uint num = default(uint);
				val = ((Sound)(ref _sound)).getLength(ref num, (TIMEUNIT)1);
				if ((int)val == 0)
				{
					((BaseUnityPlugin)this).Logger.LogInfo((object)$"FMOD track duration: {(float)num / 1000f:F1} seconds.");
				}
				return true;
			}
			catch (Exception ex)
			{
				error = ex.ToString();
				return false;
			}
		}

		private void OnSceneLoaded(Scene scene, LoadSceneMode mode)
		{
			if (((Scene)(ref scene)).name.StartsWith(_scenePrefix.Value, StringComparison.OrdinalIgnoreCase))
			{
				((BaseUnityPlugin)this).Logger.LogInfo((object)("Matched level scene '" + ((Scene)(ref scene)).name + "'."));
				if (!_soundReady)
				{
					_pendingLevelStart = true;
					((BaseUnityPlugin)this).Logger.LogInfo((object)"FMOD audio is not ready yet; playback is queued.");
				}
				else
				{
					PlayTrack();
				}
			}
		}

		private void PlayTrack()
		{
			//IL_002f: 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_005d: 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)
			//IL_005f: Unknown result type (might be due to invalid IL or missing references)
			//IL_0064: Unknown result type (might be due to invalid IL or missing references)
			//IL_0065: Unknown result type (might be due to invalid IL or missing references)
			//IL_008c: 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_0078: Unknown result type (might be due to invalid IL or missing references)
			//IL_007e: Unknown result type (might be due to invalid IL or missing references)
			//IL_0086: Unknown result type (might be due to invalid IL or missing references)
			//IL_008b: Unknown result type (might be due to invalid IL or missing references)
			//IL_00c2: Unknown result type (might be due to invalid IL or missing references)
			//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)
			if (_soundReady && !(Time.realtimeSinceStartup - _lastStartTime < 3f))
			{
				if (((Channel)(ref _channel)).hasHandle())
				{
					((Channel)(ref _channel)).stop();
					((Channel)(ref _channel)).clearHandle();
				}
				MODE val = (MODE)((!_loop.Value) ? 1 : 2);
				RESULT val2 = ((Sound)(ref _sound)).setMode((MODE)(0x88 | val));
				if ((int)val2 == 0)
				{
					System coreSystem = RuntimeManager.CoreSystem;
					val2 = ((System)(ref coreSystem)).playSound(_sound, default(ChannelGroup), false, ref _channel);
				}
				if ((int)val2 != 0)
				{
					((BaseUnityPlugin)this).Logger.LogError((object)$"FMOD playback failed: {val2}: {Error.String(val2)}");
					return;
				}
				((Channel)(ref _channel)).setVolume(_volume.Value);
				_lastStartTime = Time.realtimeSinceStartup;
				((BaseUnityPlugin)this).Logger.LogInfo((object)"Playing Leopold's Tavern through FMOD for level start.");
			}
		}
	}
}