Decompiled source of SpellReplace v1.0.0

SpellReplace.dll

Decompiled 2 days 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 BepInEx;
using BepInEx.Configuration;
using HarmonyLib;
using Microsoft.CodeAnalysis;
using UnityEngine;
using YAPYAP;

[assembly: CompilationRelaxations(8)]
[assembly: RuntimeCompatibility(WrapNonExceptionThrows = true)]
[assembly: Debuggable(DebuggableAttribute.DebuggingModes.Default | DebuggableAttribute.DebuggingModes.DisableOptimizations | DebuggableAttribute.DebuggingModes.IgnoreSymbolStoreSequencePoints | DebuggableAttribute.DebuggingModes.EnableEditAndContinue)]
[assembly: TargetFramework(".NETFramework,Version=v4.8", FrameworkDisplayName = ".NET Framework 4.8")]
[assembly: AssemblyCompany("SpellReplace")]
[assembly: AssemblyConfiguration("Debug")]
[assembly: AssemblyFileVersion("1.0.0.0")]
[assembly: AssemblyInformationalVersion("1.0.0")]
[assembly: AssemblyProduct("SpellReplace")]
[assembly: AssemblyTitle("SpellReplace")]
[assembly: AssemblyVersion("1.0.0.0")]
[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 SpellReplace
{
	[BepInPlugin("com.hyy.spellreplace", "SpellReplace", "1.0.0")]
	public class SpellReplacePlugin : BaseUnityPlugin
	{
		private static SpellReplacePlugin Instance;

		private Harmony harmony;

		private Dictionary<string, string> replacements = new Dictionary<string, string>(StringComparer.OrdinalIgnoreCase);

		private bool configReady = false;

		private float scanInterval = 3f;

		private float nextScanTime = 0f;

		private static readonly Dictionary<string, string> SpellChineseNames = new Dictionary<string, string>(StringComparer.OrdinalIgnoreCase)
		{
			{ "ACHOO", "阿秋" },
			{ "ANA-PORT-TELE", "传送门" },
			{ "ANVIL", "铁砧" },
			{ "ASTRAL-EYES", "星界之眼" },
			{ "AZUL-BRO", "蓝兄弟" },
			{ "BLINK", "闪现" },
			{ "BUB-ANA", "泡泡安娜" },
			{ "BUB-BRO", "泡泡兄弟" },
			{ "BURNING TAG", "燃烧标记" },
			{ "CORPUS-ANIMA", "实体灵魂" },
			{ "DETECT", "侦测" },
			{ "DUB-PISS-YUK", "配音" },
			{ "ECHO-ME", "回声我" },
			{ "EYES-BRO", "眼兄弟" },
			{ "FISH-YUK", "鱼" },
			{ "FLOAT-DOG", "漂浮狗" },
			{ "FOLLOWER", "追随者" },
			{ "FROZEN TAG", "冰冻标记" },
			{ "GLASS TAG", "玻璃标记" },
			{ "GRAB-ANA", "抓取安娜" },
			{ "GUIDANCE", "指引" },
			{ "IGNIS-BALLS", "火球术" },
			{ "IGNIS-DOG", "火狗" },
			{ "IGNIS-LEGS", "火腿" },
			{ "IGNIS-TONGUE", "火舌" },
			{ "ILLUMINATE", "照明" },
			{ "LOCK-FROST", "冰霜锁" },
			{ "LUX-ANA", "光之安娜" },
			{ "MERCI", "谢谢" },
			{ "METAL DUCKY", "金属鸭子" },
			{ "MIND-ANA", "心灵安娜" },
			{ "OCULUS-PISS-TEMPEST", "眼之风暴" },
			{ "OFFER", "献祭" },
			{ "Pan", "平底锅" },
			{ "PISS-YUK", "小便" },
			{ "POOF-ANIMA", "消失的灵魂" },
			{ "POOF-BRO", "消失的兄弟" },
			{ "POOF-ME", "消失的我" },
			{ "SELF-IGNIS", "自燃" },
			{ "SHRINK-ANA", "缩小安娜" },
			{ "SWAP", "交换" },
			{ "TELE-BLAST", "传送爆炸" },
			{ "TELE-FLOAT", "传送漂浮" },
			{ "TELE-LOCK", "传送锁" },
			{ "Tele-Port-Ana", "传送安娜" },
			{ "TELE-PORT-ANA", "传送安娜" },
			{ "TEMPEST", "暴风雨" },
			{ "TORCH", "火炬" },
			{ "UP-DOG", "上狗" },
			{ "VENTO", "风" },
			{ "WHAM-WHAM", "重击" }
		};

		private void Awake()
		{
			//IL_000d: Unknown result type (might be due to invalid IL or missing references)
			//IL_0017: Expected O, but got Unknown
			Instance = this;
			harmony = new Harmony("com.hyy.spellreplace");
			harmony.PatchAll(Assembly.GetExecutingAssembly());
			((BaseUnityPlugin)this).Logger.LogInfo((object)"Plugin SpellReplace v1.0.0 loaded. Will scan spells in a few seconds...");
		}

		private void Update()
		{
			if (configReady || Time.time < nextScanTime)
			{
				return;
			}
			nextScanTime = Time.time + scanInterval;
			SpellData[] array = Resources.FindObjectsOfTypeAll<SpellData>();
			if (array.Length == 0)
			{
				((BaseUnityPlugin)this).Logger.LogInfo((object)"No SpellData found yet, will retry...");
				return;
			}
			List<string> list = (from n in (from sd in array
					select sd.spellName into name
					where !string.IsNullOrEmpty(name)
					select name).Distinct()
				orderby n
				select n).ToList();
			((BaseUnityPlugin)this).Logger.LogInfo((object)$"Found {list.Count} unique spells (excluding empty names).");
			foreach (string item in list)
			{
				string chineseName = GetChineseName(item);
				string text = "Custom voice command for '" + item + "'. Leave empty to use default.";
				text = (string.IsNullOrEmpty(chineseName) ? (text + " (中文名暂缺)") : (text + " (中文名: " + chineseName + ")"));
				ConfigEntry<string> val = ((BaseUnityPlugin)this).Config.Bind<string>("Spells", item, "", text);
				string value = val.Value;
				if (!string.IsNullOrEmpty(value))
				{
					replacements[item] = value;
				}
			}
			configReady = true;
			((BaseUnityPlugin)this).Logger.LogInfo((object)$"Configuration ready. {replacements.Count} spells will be customized.");
		}

		private string GetChineseName(string spellName)
		{
			SpellChineseNames.TryGetValue(spellName, out var value);
			return value;
		}

		public static string GetReplacement(string spellName)
		{
			if (!Instance.configReady)
			{
				return null;
			}
			Instance.replacements.TryGetValue(spellName, out var value);
			return value;
		}
	}
	[HarmonyPatch(typeof(Spell))]
	[HarmonyPatch("Awake")]
	public static class Spell_Awake_Patch
	{
		private static FieldInfo spellDataField = typeof(Spell).GetField("spellData", BindingFlags.Instance | BindingFlags.NonPublic);

		[HarmonyPostfix]
		public static void Postfix(Spell __instance)
		{
			if ((Object)(object)__instance == (Object)null)
			{
				return;
			}
			object? obj = spellDataField?.GetValue(__instance);
			SpellData val = (SpellData)((obj is SpellData) ? obj : null);
			if (!((Object)(object)val == (Object)null))
			{
				string replacement = SpellReplacePlugin.GetReplacement(val.spellName);
				if (replacement != null)
				{
					val.voiceCommandKey = replacement;
				}
			}
		}
	}
	public static class PluginInfo
	{
		public const string PLUGIN_GUID = "com.hyy.spellreplace";

		public const string PLUGIN_NAME = "SpellReplace";

		public const string PLUGIN_VERSION = "1.0.0";
	}
}