Decompiled source of AudioSyncPlugin v1.1.0

AudioSyncPlugin.dll

Decompiled 7 months ago
using System;
using System.Collections;
using System.Collections.Generic;
using System.Diagnostics;
using System.Globalization;
using System.IO;
using System.Reflection;
using System.Runtime.CompilerServices;
using System.Runtime.InteropServices;
using System.Runtime.Versioning;
using System.Security.Cryptography;
using System.Text;
using BepInEx;
using BepInEx.Configuration;
using Bounce.Singletons;
using Bounce.TaleSpire.AssetManagement;
using HarmonyLib;
using Newtonsoft.Json;
using TMPro;
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: AssemblyTitle("AudioSyncPlugin")]
[assembly: AssemblyDescription("")]
[assembly: AssemblyConfiguration("")]
[assembly: AssemblyCompany("Nth Dimension")]
[assembly: AssemblyProduct("AudioSyncPlugin")]
[assembly: AssemblyCopyright("Copyright ©  2024")]
[assembly: AssemblyTrademark("AudioSyncPlugin")]
[assembly: ComVisible(false)]
[assembly: Guid("c303405d-e66c-4316-9cdb-4e3ca15c6360")]
[assembly: AssemblyFileVersion("1.1.0.0")]
[assembly: TargetFramework(".NETFramework,Version=v4.8", FrameworkDisplayName = ".NET Framework 4.8")]
[assembly: AssemblyVersion("1.1.0.0")]
namespace LordAshes;

[BepInPlugin("org.lordashes.plugins.audiosync", "Audio Plugin Sync", "1.1.0.0")]
[BepInDependency(/*Could not decode attribute arguments.*/)]
[BepInDependency(/*Could not decode attribute arguments.*/)]
[BepInDependency(/*Could not decode attribute arguments.*/)]
public class AudioPluginSync : BaseUnityPlugin
{
	public static class Utility
	{
		public static bool isBoardLoaded()
		{
			return SimpleSingletonBehaviour<CameraController>.HasInstance && SingletonStateMBehaviour<BoardSessionManager, State<BoardSessionManager>>.HasInstance && !BoardSessionManager.IsLoading;
		}

		public static Guid GuidFromString(string input)
		{
			using MD5 mD = MD5.Create();
			byte[] b = mD.ComputeHash(Encoding.Default.GetBytes(input));
			return new Guid(b);
		}

		public static float ParseFloat(string value)
		{
			return float.Parse(value, CultureInfo.InvariantCulture);
		}

		public static string GetCreatureName(string nameBlock)
		{
			if (nameBlock == null)
			{
				return "(Unknown)";
			}
			if (!nameBlock.Contains("<size=0>"))
			{
				return nameBlock;
			}
			return nameBlock.Substring(0, nameBlock.IndexOf("<size=0>")).Trim();
		}

		public static void PostOnMainPage(MemberInfo plugin)
		{
			SceneManager.sceneLoaded += delegate(Scene scene, LoadSceneMode mode)
			{
				//IL_0072: Unknown result type (might be due to invalid IL or missing references)
				//IL_0079: Expected O, but got Unknown
				try
				{
					if (((Scene)(ref scene)).name == "UI")
					{
						TextMeshProUGUI uITextByName = GetUITextByName("BETA");
						if (Object.op_Implicit((Object)(object)uITextByName))
						{
							((TMP_Text)uITextByName).text = "INJECTED BUILD - unstable mods";
						}
					}
					else
					{
						TextMeshProUGUI uITextByName2 = GetUITextByName("TextMeshPro Text");
						if (Object.op_Implicit((Object)(object)uITextByName2))
						{
							BepInPlugin val = (BepInPlugin)Attribute.GetCustomAttribute(plugin, typeof(BepInPlugin));
							if (((TMP_Text)uITextByName2).text.EndsWith("</size>"))
							{
								((TMP_Text)uITextByName2).text = ((TMP_Text)uITextByName2).text + "\n\nMods Currently Installed:\n";
							}
							TextMeshProUGUI val2 = uITextByName2;
							((TMP_Text)val2).text = ((TMP_Text)val2).text + "\nLord Ashes' " + val.Name + " - " + val.Version;
						}
					}
				}
				catch (Exception ex)
				{
					Debug.Log((object)ex);
				}
			};
		}

		private static TextMeshProUGUI GetUITextByName(string name)
		{
			TextMeshProUGUI[] array = Object.FindObjectsOfType<TextMeshProUGUI>();
			for (int i = 0; i < array.Length; i++)
			{
				if (((Object)array[i]).name == name)
				{
					return array[i];
				}
			}
			return null;
		}
	}

	public enum DiagnosticLevel
	{
		none,
		error,
		warning,
		info,
		debug,
		ultra
	}

	private class Library
	{
		public List<Tuple<string, string>> content = new List<Tuple<string, string>>();
	}

	public const string Name = "Audio Plugin Sync";

	public const string Guid = "org.lordashes.plugins.audiosync";

	public const string Version = "1.1.0.0";

	public const string Author = "Lord Ashes";

	private static AudioPluginSync _self;

	private static ConfigEntry<DiagnosticLevel> diagnostics { get; set; }

	private static ConfigEntry<KeyboardShortcut> trigger { get; set; }

	private static ConfigEntry<string> wwwPath { get; set; }

	private void Awake()
	{
		//IL_0071: Unknown result type (might be due to invalid IL or missing references)
		//IL_00cb: Unknown result type (might be due to invalid IL or missing references)
		//IL_00d0: Unknown result type (might be due to invalid IL or missing references)
		//IL_0199: Unknown result type (might be due to invalid IL or missing references)
		//IL_019f: Expected O, but got Unknown
		_self = this;
		diagnostics = ((BaseUnityPlugin)this).Config.Bind<DiagnosticLevel>("Settings", "Diagnostic Level", DiagnosticLevel.info, (ConfigDescription)null);
		Debug.Log((object)("Audio Plugin Sync: Active. (Diagnostic Mode = " + diagnostics.Value.ToString() + ")"));
		trigger = ((BaseUnityPlugin)this).Config.Bind<KeyboardShortcut>("Hotkeys", "Manual Tigger Of WWW Library Exchange", new KeyboardShortcut((KeyCode)97, (KeyCode[])(object)new KeyCode[1] { (KeyCode)305 }), (ConfigDescription)null);
		wwwPath = ((BaseUnityPlugin)this).Config.Bind<string>("Settings", "Virtual GM WWW Location", Paths.PluginPath + "/.AudioCache/CustomData/Audio/*/WWW", (ConfigDescription)null);
		foreach (MusicKind value in Enum.GetValues(typeof(MusicKind)))
		{
			MusicKind val = value;
			if (!Directory.Exists(wwwPath.Value.Replace("*", ((object)(MusicKind)(ref val)).ToString())))
			{
				Directory.CreateDirectory(wwwPath.Value.Replace("*", ((object)(MusicKind)(ref val)).ToString()));
			}
		}
		BoardSessionManager.OnBoardInfoChanged += delegate(BoardInfo info)
		{
			//IL_002a: Unknown result type (might be due to invalid IL or missing references)
			//IL_002b: Unknown result type (might be due to invalid IL or missing references)
			//IL_0030: 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_004a: Unknown result type (might be due to invalid IL or missing references)
			//IL_004f: 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_0079: Unknown result type (might be due to invalid IL or missing references)
			//IL_00a4: Unknown result type (might be due to invalid IL or missing references)
			if (diagnostics.Value >= DiagnosticLevel.ultra)
			{
				string[] obj = new string[12]
				{
					"Audio Plugin Sync: BoardChange: Id: ", null, null, null, null, null, null, null, null, null,
					null, null
				};
				BoardGuid id = info.Id;
				obj[1] = ((object)(BoardGuid)(ref id)).ToString();
				obj[2] = ", Campaign: ";
				CampaignGuid campaignId = info.CampaignId;
				obj[3] = ((object)(CampaignGuid)(ref campaignId)).ToString();
				obj[4] = ", Board: ";
				obj[5] = info.BoardName;
				obj[6] = ", Description: ";
				obj[7] = info.Description;
				obj[8] = ", Version: ";
				obj[9] = info.Version.ToString();
				obj[10] = ", Folder: ";
				obj[11] = info.FolderPath;
				Debug.Log((object)string.Concat(obj));
			}
			if (LocalClient.IsInGmMode)
			{
				((MonoBehaviour)_self).StartCoroutine(SendLibrary(0f));
			}
			if (!LocalClient.IsInGmMode)
			{
				((MonoBehaviour)_self).StartCoroutine(ReadLibrary(0f));
			}
			else
			{
				((MonoBehaviour)_self).StartCoroutine(ReadLibrary(5f));
			}
		};
		BoardSessionManager.OnClientListChange += delegate
		{
			if (diagnostics.Value >= DiagnosticLevel.ultra)
			{
				Debug.Log((object)"Audio Plugin Sync: Client Change");
			}
			if (LocalClient.IsInGmMode)
			{
				((MonoBehaviour)_self).StartCoroutine(SendLibrary(5f));
			}
		};
		Harmony val2 = new Harmony("org.lordashes.plugins.audiosync");
		val2.PatchAll();
		Utility.PostOnMainPage(((object)this).GetType());
	}

	public void Update()
	{
		//IL_0006: 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)
		KeyboardShortcut value = trigger.Value;
		if (((KeyboardShortcut)(ref value)).IsUp())
		{
			if (LocalClient.IsInGmMode)
			{
				SystemMessage.DisplayInfoText("Generating WWW Library List", 2.5f, 0f);
				((MonoBehaviour)_self).StartCoroutine(SendLibrary(0f));
			}
			if (!LocalClient.IsInGmMode)
			{
				SystemMessage.DisplayInfoText("Downloading WWW Library List", 2.5f, 0f);
				((MonoBehaviour)_self).StartCoroutine(ReadLibrary(0f));
			}
			else
			{
				SystemMessage.DisplayInfoText("Downloading WWW Library List", 2.5f, 0f);
				((MonoBehaviour)_self).StartCoroutine(ReadLibrary(5f));
			}
		}
	}

	public static IEnumerator SendLibrary(float delay)
	{
		yield return (object)new WaitForSeconds(delay);
		if (diagnostics.Value >= DiagnosticLevel.info)
		{
			Debug.Log((object)"Audio Plugin Sync: GM Is Obtaining WWW Audio Library");
		}
		Library library = new Library();
		string[] array = File.Find(".WWW", (CacheType)999);
		foreach (string entry in array)
		{
			foreach (MusicKind value in Enum.GetValues(typeof(MusicKind)))
			{
				MusicKind kind = value;
				if (entry.ToUpper().Replace("\\", "/").Contains("/" + ((object)(MusicKind)(ref kind)).ToString().ToUpper() + "/"))
				{
					if (diagnostics.Value >= DiagnosticLevel.ultra)
					{
						Debug.Log((object)("Audio Plugin Sync: Adding /" + ((object)(MusicKind)(ref kind)).ToString() + "/" + Path.GetFileNameWithoutExtension(entry) + " => " + File.ReadAllText(entry, (CacheType)999)));
					}
					library.content.Add(new Tuple<string, string>("/" + ((object)(MusicKind)(ref kind)).ToString() + "/" + Path.GetFileNameWithoutExtension(entry), File.ReadAllText(entry, (CacheType)999)));
				}
			}
		}
		if (diagnostics.Value >= DiagnosticLevel.debug)
		{
			Debug.Log((object)"Audio Plugin Sync: Setting WWW Audio Library");
		}
		CampaignGuid id = CampaignSessionManager.Id;
		string? text = ((object)(CampaignGuid)(ref id)).ToString();
		BoardGuid id2 = BoardSessionManager.CurrentBoardInfo.Id;
		AssetDataPlugin.SetInfo("GMAudioLibrary", text + "." + ((object)(BoardGuid)(ref id2)).ToString() + ":org.lordashes.plugins.audiosync", JsonConvert.SerializeObject((object)library), false);
	}

	public static IEnumerator ReadLibrary(float delay)
	{
		yield return (object)new WaitForSeconds(delay);
		if (diagnostics.Value >= DiagnosticLevel.info)
		{
			Debug.Log((object)"Audio Plugin Sync: Player Is Reading WWW Audio Library");
		}
		Library library = AssetDataPlugin.ReadInfo<Library>("GMAudioLibrary", "org.lordashes.plugins.audiosync");
		if (diagnostics.Value >= DiagnosticLevel.ultra)
		{
			Debug.Log((object)("Audio Plugin Sync: Library =\r\n" + JsonConvert.SerializeObject((object)library)));
		}
		foreach (Tuple<string, string> audioFile in library.content)
		{
			string path = audioFile.Item1.Substring(0, audioFile.Item1.LastIndexOf("/"));
			path = wwwPath.Value.Replace("*", path);
			string file = audioFile.Item1.Substring(audioFile.Item1.LastIndexOf("/") + 1);
			if (diagnostics.Value >= DiagnosticLevel.ultra)
			{
				Debug.Log((object)("Audio Plugin Sync: Writing " + path + "/" + file + ".WWW => " + audioFile.Item2));
			}
			File.WriteAllText(path + "/" + file + ".WWW", audioFile.Item2, (CacheType)999);
		}
		if (diagnostics.Value >= DiagnosticLevel.debug)
		{
			Debug.Log((object)"Audio Plugin Sync: Requesting Audio Plugin Reload");
		}
		AssetDataPlugin.SetInfo("GMAudioLibrary", "org.pluginmasters.plugins.audio", "RELOAD(" + DateTime.Now.ToString() + ")", false);
	}
}