Decompiled source of ModIoPlugin v1.1.1

ModIoPlugin.dll

Decompiled 6 months ago
using System;
using System.Diagnostics;
using System.Globalization;
using System.IO;
using System.IO.Compression;
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 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("ModIoPlugin")]
[assembly: AssemblyDescription("")]
[assembly: AssemblyConfiguration("")]
[assembly: AssemblyCompany("")]
[assembly: AssemblyProduct("ModIoPlugin")]
[assembly: AssemblyCopyright("Copyright ©  2024")]
[assembly: AssemblyTrademark("ModIoPlugin")]
[assembly: ComVisible(false)]
[assembly: Guid("c303405d-e66c-4316-9cdb-4e3ca15c6360")]
[assembly: AssemblyFileVersion("1.1.1.0")]
[assembly: TargetFramework(".NETFramework,Version=v4.7.2", FrameworkDisplayName = ".NET Framework 4.7.2")]
[assembly: AssemblyVersion("1.1.1.0")]
namespace LordAshes;

[BepInPlugin("org.lordashes.plugins.modio", "Mod.Io Plugin", "1.1.1.0")]
[BepInDependency(/*Could not decode attribute arguments.*/)]
public class TaleSpireModIoPlugin : 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 GameObject GetBaseLoader(CreatureGuid cid)
		{
			//IL_0004: Unknown result type (might be due to invalid IL or missing references)
			try
			{
				CreatureBoardAsset val = null;
				CreaturePresenter.TryGetAsset(cid, ref val);
				if ((Object)(object)val != (Object)null)
				{
					Transform match = null;
					Traverse(((Component)val).transform, "BaseLoader", 0, 10, ref match);
					if ((Object)(object)match != (Object)null)
					{
						Debug.Log((object)("Mod.Io Plugin: Base Loader '" + ((Object)match.GetChild(0)).name + "' Found"));
						return ((Component)match.GetChild(0)).gameObject;
					}
					Debug.LogWarning((object)"Mod.Io Plugin: Could Not Find Base Loader");
					return null;
				}
				return null;
			}
			catch
			{
				return null;
			}
		}

		public static GameObject GetAssetLoader(CreatureGuid cid)
		{
			//IL_0004: Unknown result type (might be due to invalid IL or missing references)
			try
			{
				CreatureBoardAsset val = null;
				CreaturePresenter.TryGetAsset(cid, ref val);
				if ((Object)(object)val != (Object)null)
				{
					Transform match = null;
					Traverse(((Component)val).transform, "AssetLoader", 0, 10, ref match);
					if ((Object)(object)match != (Object)null)
					{
						Debug.Log((object)("Mod.Io Plugin: Asset Loader '" + ((Object)match.GetChild(0)).name + "' Found"));
						return ((Component)match.GetChild(0)).gameObject;
					}
					Debug.LogWarning((object)"Mod.Io Plugin: Could Not Find Asset Loader");
					return null;
				}
				return null;
			}
			catch
			{
				return null;
			}
		}

		public static void Traverse(Transform root, string seek, int depth, int depthMax, ref Transform match)
		{
			try
			{
				if ((Object)(object)match != (Object)null)
				{
					return;
				}
				if (((Object)root).name == seek)
				{
					match = root;
					return;
				}
				foreach (Transform item in ExtensionMethods.Children(root))
				{
					if (depth < depthMax)
					{
						Traverse(item, seek, depth + 1, depthMax, ref match);
					}
				}
			}
			catch
			{
			}
		}

		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
	}

	public const string Name = "Mod.Io Plugin";

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

	public const string Version = "1.1.1.0";

	public const string Author = "Lord Ashes";

	private bool configOkay = true;

	private ConfigEntry<KeyboardShortcut>[] triggerKey { get; set; } = new ConfigEntry<KeyboardShortcut>[1];


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

	private ConfigEntry<string> sourceFolder { get; set; }

	private ConfigEntry<string> destinationFolder { get; set; }

	private long maxSize { get; set; }

	private void Awake()
	{
		//IL_00c9: Unknown result type (might be due to invalid IL or missing references)
		diagnostics = ((BaseUnityPlugin)this).Config.Bind<DiagnosticLevel>("Settings", "Diagnostic Level", DiagnosticLevel.info, (ConfigDescription)null);
		Debug.Log((object)("Mod.Io Plugin: Active. (Diagnostic Mode = " + diagnostics.Value.ToString() + ")"));
		string text = Environment.GetEnvironmentVariable("USERPROFILE") + "\\Downloads";
		string text2 = Environment.GetEnvironmentVariable("USERPROFILE") + "\\AppData\\LocalLow\\BouncyRock Entertainment\\TaleSpire\\LocalContentPacks";
		maxSize = ((BaseUnityPlugin)this).Config.Bind<int>("Hotkeys", "Maximum Archive Size In MB", 50, (ConfigDescription)null).Value * 1024 * 1024;
		triggerKey[0] = ((BaseUnityPlugin)this).Config.Bind<KeyboardShortcut>("Hotkeys", "Reload Taleweaver Mods", new KeyboardShortcut((KeyCode)279, Array.Empty<KeyCode>()), (ConfigDescription)null);
		sourceFolder = ((BaseUnityPlugin)this).Config.Bind<string>("Setting", "Source Folder (Downloads)", text, (ConfigDescription)null);
		destinationFolder = ((BaseUnityPlugin)this).Config.Bind<string>("Setting", "Destionation Folder (LocalContentPacks)", text2, (ConfigDescription)null);
		if (!sourceFolder.Value.EndsWith("\\") && !sourceFolder.Value.EndsWith("/"))
		{
			sourceFolder.Value += "/";
		}
		if (!destinationFolder.Value.EndsWith("\\") && !destinationFolder.Value.EndsWith("/"))
		{
			destinationFolder.Value += "/";
		}
		if (!Directory.Exists(sourceFolder.Value))
		{
			SystemMessage.DisplayInfoText("Mod.Io PLugin:\r\nMisconfigured Source Folder", 2.5f, 0f);
			configOkay = false;
		}
		if (!Directory.Exists(destinationFolder.Value))
		{
			SystemMessage.DisplayInfoText("Mod.Io PLugin:\r\nMisconfigured Destination Folder", 2.5f, 0f);
			configOkay = false;
		}
		SyncMods();
		Utility.PostOnMainPage(((object)this).GetType());
	}

	private void Update()
	{
		//IL_0013: Unknown result type (might be due to invalid IL or missing references)
		//IL_0018: Unknown result type (might be due to invalid IL or missing references)
		if (Utility.isBoardLoaded())
		{
			KeyboardShortcut value = triggerKey[0].Value;
			if (((KeyboardShortcut)(ref value)).IsUp())
			{
				SystemMessage.DisplayInfoText("Syncing Downloaded Mod.Io Mods", 2.5f, 0f);
				SyncMods();
			}
		}
	}

	private void SyncMods()
	{
		if (configOkay)
		{
			if (diagnostics.Value >= DiagnosticLevel.info)
			{
				Debug.Log((object)"Mod.Io Plugin: Moving Downloaded Mods");
			}
			{
				foreach (string item in Directory.EnumerateFiles(sourceFolder.Value, "*.zip"))
				{
					long length = new FileInfo(item).Length;
					if (diagnostics.Value >= DiagnosticLevel.ultra)
					{
						Debug.Log((object)("Mod.Io Plugin: Found Archived " + item));
					}
					if (length < maxSize)
					{
						ZipArchive val = ZipFile.OpenRead(item);
						try
						{
							foreach (ZipArchiveEntry entry in val.Entries)
							{
								if (diagnostics.Value >= DiagnosticLevel.ultra)
								{
									Debug.Log((object)("Mod.Io Plugin: Found Archived File " + entry.FullName + " (" + Path.GetExtension(entry.FullName).ToUpper() + ")"));
								}
								if (!(Path.GetExtension(entry.FullName).ToUpper() == ".TSMOD"))
								{
									continue;
								}
								if (File.Exists(destinationFolder.Value + Path.GetFileNameWithoutExtension(item) + ".tsmod"))
								{
									if (diagnostics.Value >= DiagnosticLevel.ultra)
									{
										Debug.Log((object)("Mod.Io Plugin: Removing " + destinationFolder.Value + Path.GetFileNameWithoutExtension(item) + ".tsmod"));
									}
									File.Delete(destinationFolder.Value + Path.GetFileNameWithoutExtension(item) + ".tsmod");
								}
								if (diagnostics.Value >= DiagnosticLevel.ultra)
								{
									Debug.Log((object)("Mod.Io Plugin: Extracting " + entry.FullName + " From " + item));
								}
								entry.ExtractToFile(destinationFolder.Value + Path.GetFileNameWithoutExtension(item) + ".tsmod");
							}
						}
						finally
						{
							((IDisposable)val)?.Dispose();
						}
					}
					else if (diagnostics.Value >= DiagnosticLevel.ultra)
					{
						Debug.Log((object)("Mod.Io Plugin: Skipping Large Archived " + item + " (Size = " + length + ")"));
					}
				}
				return;
			}
		}
		SystemMessage.DisplayInfoText("Mod.Io PLugin:\r\nCannot Sync Due To Misconfigured Folders", 2.5f, 0f);
	}
}