Decompiled source of PlasmaSoundAPI v1.0.1

Azim.PlasmaSoundAPI.dll

Decompiled 10 months ago
using System;
using System.Diagnostics;
using System.IO;
using System.Reflection;
using System.Runtime.CompilerServices;
using System.Runtime.InteropServices;
using System.Runtime.Versioning;
using System.Security;
using System.Security.Permissions;
using AOT;
using BepInEx;
using BepInEx.Logging;
using FMOD;
using FMOD.Studio;
using FMODUnity;
using HarmonyLib;
using Microsoft.CodeAnalysis;
using UnityEngine;

[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("Azim.PlasmaSoundAPI")]
[assembly: AssemblyConfiguration("Debug")]
[assembly: AssemblyDescription("PlasmaSoundAPI")]
[assembly: AssemblyFileVersion("1.0.1.0")]
[assembly: AssemblyInformationalVersion("1.0.1")]
[assembly: AssemblyProduct("Azim.PlasmaSoundAPI")]
[assembly: AssemblyTitle("Azim.PlasmaSoundAPI")]
[assembly: SecurityPermission(SecurityAction.RequestMinimum, SkipVerification = true)]
[assembly: AssemblyVersion("1.0.1.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.Module, AllowMultiple = false, Inherited = false)]
	internal sealed class RefSafetyRulesAttribute : Attribute
	{
		public readonly int Version;

		public RefSafetyRulesAttribute(int P_0)
		{
			Version = P_0;
		}
	}
}
namespace Azim.PlasmaSoundAPI
{
	public class BankLoadException : Exception
	{
		public BankLoadException(string message)
			: base(message)
		{
		}
	}
	[BepInPlugin("Azim.PlasmaSoundAPI", "Azim.PlasmaSoundAPI", "1.0.1")]
	public class PlasmaSoundAPI : BaseUnityPlugin
	{
		internal static ManualLogSource mls;

		internal static EVENT_CALLBACK dialogueCallback;

		private static bool loaded = false;

		internal static readonly string sound2d = "{2e2adb70-14df-4219-bb0d-b5c6ffd8fd4f}";

		internal static readonly string sound3d = "{6ced009f-0452-428d-9d94-994ae51660fc}";

		private void Awake()
		{
			//IL_0021: Unknown result type (might be due to invalid IL or missing references)
			//IL_0027: Expected O, but got Unknown
			mls = ((BaseUnityPlugin)this).Logger;
			mls.LogInfo((object)"Starting initialization of PlasmaSoundAPI");
			Harmony val = new Harmony("PlasmaSoundAPI");
			val.PatchAll(Assembly.GetExecutingAssembly());
			mls.LogInfo((object)"PlasmaSoundAPI initialized successfully");
		}

		public static EventInstance PlaySound2D(AudioClip audioclip)
		{
			//IL_0005: Unknown result type (might be due to invalid IL or missing references)
			//IL_000b: Unknown result type (might be due to invalid IL or missing references)
			//IL_000c: Unknown result type (might be due to invalid IL or missing references)
			//IL_0011: Unknown result type (might be due to invalid IL or missing references)
			//IL_0014: Unknown result type (might be due to invalid IL or missing references)
			return PlayClipInFmod(audioclip, is3d: false, default(Vector3));
		}

		public static EventInstance PlaySound3D(AudioClip audioclip, Vector3 position)
		{
			//IL_0003: Unknown result type (might be due to invalid IL or missing references)
			//IL_0004: Unknown result type (might be due to invalid IL or missing references)
			//IL_0009: Unknown result type (might be due to invalid IL or missing references)
			//IL_000c: Unknown result type (might be due to invalid IL or missing references)
			return PlayClipInFmod(audioclip, is3d: true, position);
		}

		[HarmonyPrefix]
		[HarmonyPatch(typeof(AudioController), "Init")]
		internal static void loadDummyBank()
		{
			//IL_0018: Unknown result type (might be due to invalid IL or missing references)
			//IL_0022: Expected O, but got Unknown
			//IL_0037: Unknown result type (might be due to invalid IL or missing references)
			//IL_003c: Unknown result type (might be due to invalid IL or missing references)
			//IL_0044: Unknown result type (might be due to invalid IL or missing references)
			//IL_0049: Unknown result type (might be due to invalid IL or missing references)
			//IL_006c: Unknown result type (might be due to invalid IL or missing references)
			//IL_006e: Invalid comparison between Unknown and I4
			//IL_0096: Unknown result type (might be due to invalid IL or missing references)
			//IL_00a9: Unknown result type (might be due to invalid IL or missing references)
			//IL_00ae: 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)
			//IL_00bf: Unknown result type (might be due to invalid IL or missing references)
			//IL_00c9: Unknown result type (might be due to invalid IL or missing references)
			if (!loaded)
			{
				dialogueCallback = new EVENT_CALLBACK(PlayFileCallBackUsingAudioFile);
				byte[] array = ReadFully(Assembly.GetExecutingAssembly().GetManifestResourceStream("Azim.PlasmaSoundAPI.Modded.bank"));
				System studioSystem = RuntimeManager.StudioSystem;
				Bank val = default(Bank);
				RESULT val2 = ((System)(ref studioSystem)).loadBankMemory(array, (LOAD_BANK_FLAGS)0, ref val);
				mls.LogInfo((object)("Load bank result: " + ((object)(RESULT)(ref val2)).ToString()));
				if ((int)val2 > 0)
				{
					mls.LogError((object)"Dummy sound bank not loaded, aborting");
					throw new BankLoadException("Dummy tank not loaded");
				}
				EventDescription[] array2 = default(EventDescription[]);
				((Bank)(ref val)).getEventList(ref array2);
				EventDescription[] array3 = array2;
				string text = default(string);
				Guid guid = default(Guid);
				bool flag = default(bool);
				for (int i = 0; i < array3.Length; i++)
				{
					EventDescription val3 = array3[i];
					((EventDescription)(ref val3)).getPath(ref text);
					((EventDescription)(ref val3)).getID(ref guid);
					((EventDescription)(ref val3)).is3D(ref flag);
				}
				loaded = true;
			}
		}

		internal static EventInstance PlayClipInFmod(AudioClip audioclip, bool is3d, Vector3 position)
		{
			//IL_0063: 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_0078: Unknown result type (might be due to invalid IL or missing references)
			//IL_009d: Unknown result type (might be due to invalid IL or missing references)
			//IL_00a5: Unknown result type (might be due to invalid IL or missing references)
			//IL_00aa: Unknown result type (might be due to invalid IL or missing references)
			//IL_00ae: Unknown result type (might be due to invalid IL or missing references)
			//IL_00b3: 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)
			//IL_00b6: Unknown result type (might be due to invalid IL or missing references)
			//IL_0088: Unknown result type (might be due to invalid IL or missing references)
			//IL_0089: Unknown result type (might be due to invalid IL or missing references)
			//IL_008e: Unknown result type (might be due to invalid IL or missing references)
			//IL_00ba: Unknown result type (might be due to invalid IL or missing references)
			if (!loaded)
			{
				loadDummyBank();
			}
			float[] array = new float[audioclip.samples * audioclip.channels];
			audioclip.GetData(array, 0);
			SoundRequirements value = new SoundRequirements(((Object)audioclip).name, audioclip.samples, audioclip.channels, (SOUND_FORMAT)5, audioclip.frequency, array, is3d);
			EventInstance result = RuntimeManager.CreateInstance(is3d ? sound3d : sound2d);
			GCHandle value2 = GCHandle.Alloc(value);
			((EventInstance)(ref result)).setUserData(GCHandle.ToIntPtr(value2));
			if (is3d)
			{
				((EventInstance)(ref result)).set3DAttributes(RuntimeUtils.To3DAttributes(position));
			}
			((EventInstance)(ref result)).setCallback(dialogueCallback, (EVENT_CALLBACK_TYPE)(-1));
			RESULT val = ((EventInstance)(ref result)).start();
			val = ((EventInstance)(ref result)).release();
			return result;
		}

		[MonoPInvokeCallback(typeof(EVENT_CALLBACK))]
		internal static RESULT PlayFileCallBackUsingAudioFile(EVENT_CALLBACK_TYPE type, IntPtr instPrt, IntPtr paramsPrt)
		{
			//IL_0027: Unknown result type (might be due to invalid IL or missing references)
			//IL_0041: 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_0044: 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)
			//IL_0048: Unknown result type (might be due to invalid IL or missing references)
			//IL_004b: Invalid comparison between Unknown and I4
			//IL_001c: Unknown result type (might be due to invalid IL or missing references)
			//IL_0290: Unknown result type (might be due to invalid IL or missing references)
			//IL_028c: Unknown result type (might be due to invalid IL or missing references)
			//IL_0052: Unknown result type (might be due to invalid IL or missing references)
			//IL_0059: Invalid comparison between Unknown and I4
			//IL_007f: 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)
			//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_0091: Unknown result type (might be due to invalid IL or missing references)
			//IL_009b: 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_00dc: Unknown result type (might be due to invalid IL or missing references)
			//IL_00fb: Unknown result type (might be due to invalid IL or missing references)
			//IL_0100: 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_0064: Invalid comparison between Unknown and I4
			//IL_0121: Unknown result type (might be due to invalid IL or missing references)
			//IL_012b: Unknown result type (might be due to invalid IL or missing references)
			//IL_0130: Unknown result type (might be due to invalid IL or missing references)
			//IL_0132: Unknown result type (might be due to invalid IL or missing references)
			//IL_0135: Invalid comparison between Unknown and I4
			//IL_0245: Unknown result type (might be due to invalid IL or missing references)
			//IL_024a: Unknown result type (might be due to invalid IL or missing references)
			//IL_024e: Unknown result type (might be due to invalid IL or missing references)
			//IL_025c: Unknown result type (might be due to invalid IL or missing references)
			//IL_0266: Unknown result type (might be due to invalid IL or missing references)
			//IL_026b: Unknown result type (might be due to invalid IL or missing references)
			//IL_026d: Unknown result type (might be due to invalid IL or missing references)
			//IL_0270: Invalid comparison between Unknown and I4
			//IL_0153: Unknown result type (might be due to invalid IL or missing references)
			//IL_015d: Unknown result type (might be due to invalid IL or missing references)
			//IL_0162: Unknown result type (might be due to invalid IL or missing references)
			//IL_0164: Unknown result type (might be due to invalid IL or missing references)
			//IL_0167: Invalid comparison between Unknown and I4
			//IL_013d: Unknown result type (might be due to invalid IL or missing references)
			//IL_013f: Unknown result type (might be due to invalid IL or missing references)
			//IL_0278: Unknown result type (might be due to invalid IL or missing references)
			//IL_027a: Unknown result type (might be due to invalid IL or missing references)
			//IL_016f: Unknown result type (might be due to invalid IL or missing references)
			//IL_0171: Unknown result type (might be due to invalid IL or missing references)
			//IL_01b6: Unknown result type (might be due to invalid IL or missing references)
			//IL_01c0: Unknown result type (might be due to invalid IL or missing references)
			//IL_01c5: Unknown result type (might be due to invalid IL or missing references)
			//IL_01c7: Unknown result type (might be due to invalid IL or missing references)
			//IL_01ca: Invalid comparison between Unknown and I4
			//IL_01d2: Unknown result type (might be due to invalid IL or missing references)
			//IL_01d4: Unknown result type (might be due to invalid IL or missing references)
			//IL_01ec: Unknown result type (might be due to invalid IL or missing references)
			//IL_01f6: Unknown result type (might be due to invalid IL or missing references)
			//IL_01fc: Unknown result type (might be due to invalid IL or missing references)
			//IL_01ff: Invalid comparison between Unknown and I4
			//IL_022b: Unknown result type (might be due to invalid IL or missing references)
			//IL_022d: Unknown result type (might be due to invalid IL or missing references)
			//IL_020a: Unknown result type (might be due to invalid IL or missing references)
			//IL_021e: Unknown result type (might be due to invalid IL or missing references)
			EventInstance val = default(EventInstance);
			((EventInstance)(ref val))..ctor(instPrt);
			if (!((EventInstance)(ref val)).isValid())
			{
				return (RESULT)74;
			}
			IntPtr value = default(IntPtr);
			((EventInstance)(ref val)).getUserData(ref value);
			GCHandle gCHandle = GCHandle.FromIntPtr(value);
			SoundRequirements soundRequirements = gCHandle.Target as SoundRequirements;
			if ((int)type != 2)
			{
				if ((int)type != 128)
				{
					if ((int)type == 256)
					{
						PROGRAMMER_SOUND_PROPERTIES val2 = (PROGRAMMER_SOUND_PROPERTIES)Marshal.PtrToStructure(paramsPrt, typeof(PROGRAMMER_SOUND_PROPERTIES));
						Sound val3 = default(Sound);
						((Sound)(ref val3))..ctor(val2.sound);
						RESULT val4 = ERRCHECK(((Sound)(ref val3)).release(), "Failed to release sound");
						if ((int)val4 > 0)
						{
							return val4;
						}
					}
				}
				else
				{
					PROGRAMMER_SOUND_PROPERTIES structure = (PROGRAMMER_SOUND_PROPERTIES)Marshal.PtrToStructure(paramsPrt, typeof(PROGRAMMER_SOUND_PROPERTIES));
					System coreSystem = RuntimeManager.CoreSystem;
					ChannelGroup val5 = default(ChannelGroup);
					ERRCHECK(((System)(ref coreSystem)).getMasterChannelGroup(ref val5), "Failed to get masterGroup from core system");
					uint num = (uint)(soundRequirements.samples * soundRequirements.channels * 4);
					CREATESOUNDEXINFO val6 = default(CREATESOUNDEXINFO);
					val6.cbsize = Marshal.SizeOf(typeof(CREATESOUNDEXINFO));
					val6.length = num;
					val6.format = (SOUND_FORMAT)5;
					val6.defaultfrequency = soundRequirements.defaultFrequency;
					val6.numchannels = soundRequirements.channels;
					coreSystem = RuntimeManager.CoreSystem;
					Sound val7 = default(Sound);
					RESULT val8 = ERRCHECK(((System)(ref coreSystem)).createSound(soundRequirements.name, (MODE)(0x400 | (soundRequirements.is3D ? 16 : 0)), ref val6, ref val7), "Failed to create sound");
					if ((int)val8 > 0)
					{
						return val8;
					}
					IntPtr intPtr = default(IntPtr);
					IntPtr intPtr2 = default(IntPtr);
					uint num2 = default(uint);
					uint num3 = default(uint);
					val8 = ERRCHECK(((Sound)(ref val7)).@lock(0u, num, ref intPtr, ref intPtr2, ref num2, ref num3), "Failed to lock sound");
					if ((int)val8 > 0)
					{
						return val8;
					}
					Marshal.Copy(soundRequirements.sampleData, 0, intPtr, (int)(num2 / 4));
					if (num3 != 0)
					{
						Marshal.Copy(soundRequirements.sampleData, (int)(num2 / 4), intPtr2, (int)(num3 / 4));
					}
					val8 = ERRCHECK(((Sound)(ref val7)).unlock(intPtr, intPtr2, num2, num3), "Failed to unlock sound");
					if ((int)val8 > 0)
					{
						return val8;
					}
					ERRCHECK(((Sound)(ref val7)).setMode((MODE)(0 | (soundRequirements.is3D ? 16 : 0))), "Failed to set the sound mode");
					if ((int)val8 != 0)
					{
						return val8;
					}
					structure.sound = val7.handle;
					structure.subsoundIndex = -1;
					Marshal.StructureToPtr<PROGRAMMER_SOUND_PROPERTIES>(structure, paramsPrt, fDeleteOld: false);
				}
			}
			else
			{
				gCHandle.Free();
			}
			return (RESULT)0;
		}

		private static RESULT ERRCHECK(RESULT result, string failMsg)
		{
			//IL_0001: Unknown result type (might be due to invalid IL or missing references)
			//IL_0003: Invalid comparison between Unknown and I4
			//IL_002e: Unknown result type (might be due to invalid IL or missing references)
			//IL_002f: Unknown result type (might be due to invalid IL or missing references)
			//IL_0032: Unknown result type (might be due to invalid IL or missing references)
			if ((int)result > 0)
			{
				mls.LogWarning((object)(failMsg + " with result: " + ((object)(RESULT)(ref result)).ToString()));
			}
			return result;
		}

		private static byte[] ReadFully(Stream input)
		{
			if (input == null)
			{
				throw new InvalidDataException("Stream is null");
			}
			using MemoryStream memoryStream = new MemoryStream();
			input.CopyTo(memoryStream);
			return memoryStream.ToArray();
		}
	}
	internal class SoundRequirements
	{
		public string name;

		public int samples;

		public int channels;

		public SOUND_FORMAT format;

		public int defaultFrequency;

		public float[] sampleData;

		public bool is3D;

		public SoundRequirements(string name, int samples, int channel, SOUND_FORMAT format, int defaultFrequency, float[] sampleData, bool is3D)
		{
			//IL_001e: Unknown result type (might be due to invalid IL or missing references)
			//IL_0020: Unknown result type (might be due to invalid IL or missing references)
			this.name = name;
			this.samples = samples;
			channels = channel;
			this.format = format;
			this.defaultFrequency = defaultFrequency;
			this.sampleData = sampleData;
			this.is3D = is3D;
		}
	}
	public static class PluginInfo
	{
		public const string PLUGIN_GUID = "Azim.PlasmaSoundAPI";

		public const string PLUGIN_NAME = "Azim.PlasmaSoundAPI";

		public const string PLUGIN_VERSION = "1.0.1";
	}
}