Decompiled source of ThaiCompany v1.0.4

BepInEx/plugins/ThaiCompany/ThaiCompany.dll

Decompiled 11 months ago
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.IO;
using System.Reflection;
using System.Runtime.CompilerServices;
using System.Runtime.InteropServices;
using System.Runtime.Versioning;
using System.Text.RegularExpressions;
using BepInEx;
using HarmonyLib;
using Newtonsoft.Json;
using ThaiCompanyTranslator;
using UnityEngine;

[assembly: CompilationRelaxations(8)]
[assembly: RuntimeCompatibility(WrapNonExceptionThrows = true)]
[assembly: Debuggable(DebuggableAttribute.DebuggingModes.Default | DebuggableAttribute.DebuggingModes.DisableOptimizations | DebuggableAttribute.DebuggingModes.IgnoreSymbolStoreSequencePoints | DebuggableAttribute.DebuggingModes.EnableEditAndContinue)]
[assembly: AssemblyTitle("ThaiCompanyMod")]
[assembly: AssemblyDescription("")]
[assembly: AssemblyConfiguration("")]
[assembly: AssemblyCompany("Reo Nomura")]
[assembly: AssemblyProduct("ThaiCompanyMod")]
[assembly: AssemblyCopyright("Copyright ©  2023")]
[assembly: AssemblyTrademark("")]
[assembly: ComVisible(false)]
[assembly: Guid("03715b83-fe40-4d60-b83b-bbc89f0b84b8")]
[assembly: AssemblyFileVersion("1.0.0.0")]
[assembly: TargetFramework(".NETFramework,Version=v4.8.1", FrameworkDisplayName = ".NET Framework 4.8.1")]
[assembly: AssemblyVersion("1.0.0.0")]
namespace ThaiCompanyTranslator
{
	internal class ThaiCompanyTerminal
	{
		private string translateFile = "mapping.json";

		private string translateKeywordFile = "keyword_default.json";

		private string translateKeywordNonDefaultFile = "keyword_non_default.json";

		private string translatePatternFile = "message_pattern.json";

		private string translateLang = "th";

		private Dictionary<string, Dictionary<string, Dictionary<string, string>>> translateDict;

		private Dictionary<string, Dictionary<string, Dictionary<string, string>>> translateKeyword;

		private Dictionary<string, Dictionary<string, Dictionary<string, Dictionary<string, string>>>> translateKeywordNonDefault;

		private Dictionary<string, string>[] translatePatternArr;

		public ThaiCompanyTerminal()
		{
			string directoryName = Path.GetDirectoryName(Assembly.GetExecutingAssembly().Location);
			string path = Path.Combine(directoryName, translateFile);
			string path2 = Path.Combine(directoryName, translateKeywordFile);
			string path3 = Path.Combine(directoryName, translateKeywordNonDefaultFile);
			string path4 = Path.Combine(directoryName, translatePatternFile);
			string text = File.ReadAllText(path);
			string text2 = File.ReadAllText(path2);
			string text3 = File.ReadAllText(path3);
			string text4 = File.ReadAllText(path4);
			translateDict = JsonConvert.DeserializeObject<Dictionary<string, Dictionary<string, Dictionary<string, string>>>>(text);
			translateKeyword = JsonConvert.DeserializeObject<Dictionary<string, Dictionary<string, Dictionary<string, string>>>>(text2);
			translateKeywordNonDefault = JsonConvert.DeserializeObject<Dictionary<string, Dictionary<string, Dictionary<string, Dictionary<string, string>>>>>(text3);
			translatePatternArr = JsonConvert.DeserializeObject<Dictionary<string, string>[]>(text4);
		}

		public string translate(ref string modifiedDisplayText, ref TerminalNode node)
		{
			string text = node.displayText.ToString();
			string result;
			if (text.Contains("[currentDay]"))
			{
				translateWelcome(ref modifiedDisplayText);
				result = modifiedDisplayText;
			}
			else if (text.Contains("[numberOfItemsOnRoute]"))
			{
				translateMenu(ref modifiedDisplayText);
				result = modifiedDisplayText;
			}
			else if (text.Contains("[planetTime]"))
			{
				translateCommandMoons(ref modifiedDisplayText);
				result = modifiedDisplayText;
			}
			else if (text.Contains("[unlockablesSelectionList]"))
			{
				translateCommandStore(ref modifiedDisplayText);
				result = modifiedDisplayText;
			}
			else if (text.Contains("[currentScannedEnemiesList]"))
			{
				translateCommandBestiary(ref modifiedDisplayText);
				result = modifiedDisplayText;
			}
			else if (text.Contains("[storedUnlockablesList]"))
			{
				translateCommandStorage(ref modifiedDisplayText);
				result = modifiedDisplayText;
			}
			else if (text.Contains("[currentUnlockedLogsList]"))
			{
				translateCommandSigurd(ref modifiedDisplayText);
				result = modifiedDisplayText;
			}
			else
			{
				translateCommandOther(ref modifiedDisplayText);
				translateMisc(ref modifiedDisplayText);
				translateDetailStore(ref modifiedDisplayText);
				if (modifiedDisplayText.Length > 180)
				{
					if (!translateDetailMoons(ref modifiedDisplayText))
					{
						return modifiedDisplayText;
					}
					if (!translateDetailBestiary(ref modifiedDisplayText))
					{
						return modifiedDisplayText;
					}
					if (!translateDetailSigurd(ref modifiedDisplayText))
					{
						return modifiedDisplayText;
					}
				}
				result = modifiedDisplayText;
			}
			return result;
		}

		public void translateWelcome(ref string raw)
		{
			if (!translateDict.TryGetValue(translateLang, out var value) || !value.TryGetValue("welcome", out var value2))
			{
				return;
			}
			foreach (KeyValuePair<string, string> item in value2)
			{
				raw = raw.Replace(item.Key, item.Value);
			}
		}

		public void translateMenu(ref string raw)
		{
			if (!translateDict.TryGetValue(translateLang, out var value) || !value.TryGetValue("menu", out var value2))
			{
				return;
			}
			foreach (KeyValuePair<string, string> item in value2)
			{
				raw = raw.Replace(item.Key, item.Value);
			}
		}

		public void translateCommandMoons(ref string raw)
		{
			if (!translateDict.TryGetValue(translateLang, out var value) || !value.TryGetValue("moonCommands", out var value2))
			{
				return;
			}
			foreach (KeyValuePair<string, string> item in value2)
			{
				raw = raw.Replace(item.Key, item.Value);
			}
		}

		public void translateCommandStore(ref string raw)
		{
			if (!translateDict.TryGetValue(translateLang, out var value) || !value.TryGetValue("storeCommands", out var value2))
			{
				return;
			}
			foreach (KeyValuePair<string, string> item in value2)
			{
				raw = raw.Replace(item.Key, item.Value);
			}
			translateAgainstPattern(ref raw);
		}

		public void translateCommandBestiary(ref string raw)
		{
			if (!translateDict.TryGetValue(translateLang, out var value) || !value.TryGetValue("bestiaryCommands", out var value2))
			{
				return;
			}
			foreach (KeyValuePair<string, string> item in value2)
			{
				raw = raw.Replace(item.Key, item.Value);
			}
		}

		public void translateCommandStorage(ref string raw)
		{
			if (!translateDict.TryGetValue(translateLang, out var value) || !value.TryGetValue("storageCommands", out var value2))
			{
				return;
			}
			foreach (KeyValuePair<string, string> item in value2)
			{
				raw = raw.Replace(item.Key, item.Value);
			}
		}

		public void translateCommandOther(ref string raw)
		{
			if (!translateDict.TryGetValue(translateLang, out var value) || !value.TryGetValue("otherCommands", out var value2))
			{
				return;
			}
			foreach (KeyValuePair<string, string> item in value2)
			{
				raw = raw.Replace(item.Key, item.Value);
			}
		}

		public void translateCommandSigurd(ref string raw)
		{
			if (!translateDict.TryGetValue(translateLang, out var value) || !value.TryGetValue("sigurdCommands", out var value2))
			{
				return;
			}
			foreach (KeyValuePair<string, string> item in value2)
			{
				raw = raw.Replace(item.Key, item.Value);
			}
		}

		public bool translateDetailMoons(ref string raw)
		{
			string text = raw;
			if (translateDict.TryGetValue(translateLang, out var value) && value.TryGetValue("moonDetails", out var value2))
			{
				foreach (KeyValuePair<string, string> item in value2)
				{
					raw = raw.Replace(item.Key, item.Value);
				}
			}
			return raw == text;
		}

		public void translateDetailStore(ref string raw)
		{
			if (!translateDict.TryGetValue(translateLang, out var value) || !value.TryGetValue("storeDetails", out var value2))
			{
				return;
			}
			foreach (KeyValuePair<string, string> item in value2)
			{
				raw = raw.Replace(item.Key, item.Value);
			}
		}

		public bool translateDetailBestiary(ref string raw)
		{
			string text = raw;
			return raw == text;
		}

		public bool translateDetailSigurd(ref string raw)
		{
			string text = raw;
			if (translateDict.TryGetValue(translateLang, out var value) && value.TryGetValue("sigurdDetails", out var value2))
			{
				foreach (KeyValuePair<string, string> item in value2)
				{
					raw = raw.Replace(item.Key, item.Value);
				}
			}
			return raw == text;
		}

		public void translateMisc(ref string raw)
		{
			if (translateDict.TryGetValue(translateLang, out var value) && value.TryGetValue("misc", out var value2))
			{
				foreach (KeyValuePair<string, string> item in value2)
				{
					raw = raw.Replace(item.Key, item.Value);
				}
			}
			translateAgainstPattern(ref raw);
		}

		private void translateAgainstPattern(ref string raw)
		{
			Dictionary<string, string>[] array = translatePatternArr;
			foreach (Dictionary<string, string> dictionary in array)
			{
				Regex regex = new Regex(dictionary["pattern"]);
				Match match = regex.Match(raw);
				if (match.Success)
				{
					string[] array2 = new string[match.Groups.Count];
					for (int j = 1; j < match.Groups.Count; j++)
					{
						string raw2 = match.Groups[j].Value;
						array2[j - 1] = translateItemName(ref raw2);
					}
					string input = raw;
					string format = dictionary[translateLang];
					object[] array3 = array2;
					object[] array4 = array3;
					object[] args = array4;
					raw = regex.Replace(input, string.Format(format, args));
				}
			}
		}

		private string translateItemName(ref string raw)
		{
			if (translateDict.TryGetValue(translateLang, out var value) && value.TryGetValue("itemName", out var value2))
			{
				foreach (KeyValuePair<string, string> item in value2)
				{
					raw = raw.Replace(item.Key, item.Value);
				}
			}
			return raw;
		}

		public void initThaiCompanyTerminalNodes()
		{
			//IL_023d: Unknown result type (might be due to invalid IL or missing references)
			//IL_0244: Expected O, but got Unknown
			//IL_0124: Unknown result type (might be due to invalid IL or missing references)
			//IL_012b: Expected O, but got Unknown
			//IL_037f: Unknown result type (might be due to invalid IL or missing references)
			//IL_0386: Expected O, but got Unknown
			Dictionary<string, TerminalKeyword> dictionary = new Dictionary<string, TerminalKeyword>();
			TerminalKeyword[] array = (TerminalKeyword[])(object)Resources.FindObjectsOfTypeAll(typeof(TerminalKeyword));
			if (translateKeyword.TryGetValue(translateLang, out var value))
			{
				for (int i = 0; i < array.Length; i++)
				{
					if (!value.TryGetValue(((Object)array[i]).name, out var value2))
					{
						continue;
					}
					TerminalKeyword defaultVerb = array[i].defaultVerb;
					CompatibleNoun[] array2 = null;
					dictionary.Add(((Object)array[i]).name, array[i]);
					if ((Object)(object)defaultVerb != (Object)null)
					{
						array2 = (CompatibleNoun[])(object)new CompatibleNoun[defaultVerb.compatibleNouns.Length + value2.Keys.Count];
						Array.Copy(defaultVerb.compatibleNouns, array2, defaultVerb.compatibleNouns.Length);
					}
					int num = 0;
					foreach (KeyValuePair<string, string> item in value2)
					{
						TerminalKeyword val = Object.Instantiate<TerminalKeyword>(array[i]);
						((Object)val).name = item.Key;
						val.word = item.Key;
						dictionary.Add(((Object)val).name, val);
						if (array2 != null)
						{
							CompatibleNoun val2 = new CompatibleNoun();
							val2.noun = val;
							val2.result = getResultNode(item.Value);
							if ((Object)(object)array[i].defaultVerb != (Object)null)
							{
								array2[defaultVerb.compatibleNouns.Length + num++] = val2;
							}
						}
					}
					if ((Object)(object)defaultVerb != (Object)null)
					{
						array[i].defaultVerb.compatibleNouns = array2;
					}
				}
				dictionary["ซ\u0e37\u0e49อ"].compatibleNouns = dictionary["Buy"].compatibleNouns;
				dictionary["ไป"].compatibleNouns = dictionary["Route"].compatibleNouns;
				dictionary["ไปดาว"].compatibleNouns = dictionary["Route"].compatibleNouns;
				TerminalNodesList val3 = (TerminalNodesList)Resources.FindObjectsOfTypeAll(typeof(TerminalNodesList))[0];
				val3.allKeywords = (TerminalKeyword[])(object)Resources.FindObjectsOfTypeAll(typeof(TerminalKeyword));
			}
			if (!translateKeywordNonDefault.TryGetValue(translateLang, out var value3))
			{
				return;
			}
			for (int j = 0; j < array.Length; j++)
			{
				value3.TryGetValue("Info", out var value4);
				if (!value4.TryGetValue(((Object)array[j]).name, out var value5))
				{
					continue;
				}
				TerminalKeyword val4 = dictionary["Info"];
				CompatibleNoun[] array3 = null;
				if ((Object)(object)val4 != (Object)null)
				{
					array3 = (CompatibleNoun[])(object)new CompatibleNoun[val4.compatibleNouns.Length + value5.Keys.Count];
					Array.Copy(val4.compatibleNouns, array3, val4.compatibleNouns.Length);
				}
				int num2 = 0;
				foreach (KeyValuePair<string, string> item2 in value5)
				{
					if (!dictionary.TryGetValue(item2.Key, out var _))
					{
						TerminalKeyword val5 = Object.Instantiate<TerminalKeyword>(array[j]);
						((Object)val5).name = item2.Key;
						val5.word = item2.Key;
						dictionary.Add(item2.Key, val5);
					}
					if (array3 != null)
					{
						CompatibleNoun val6 = new CompatibleNoun();
						val6.noun = dictionary[item2.Key];
						val6.result = getResultNode(item2.Value);
						array3[val4.compatibleNouns.Length + num2++] = val6;
					}
				}
				if ((Object)(object)val4 != (Object)null)
				{
					val4.compatibleNouns = array3;
				}
			}
			dictionary["ข\u0e49อม\u0e39ล"].compatibleNouns = dictionary["Info"].compatibleNouns;
			dictionary["รายละเอ\u0e35ยด"].compatibleNouns = dictionary["Info"].compatibleNouns;
		}

		private TerminalNode getResultNode(string nodeName)
		{
			TerminalNode[] array = (TerminalNode[])(object)Resources.FindObjectsOfTypeAll(typeof(TerminalNode));
			TerminalNode result = null;
			for (int i = 0; i < array.Length; i++)
			{
				if (string.Equals(((Object)array[i]).name, nodeName))
				{
					result = array[i];
					break;
				}
			}
			return result;
		}

		private TerminalKeyword getResultKeyword(string keywordName)
		{
			TerminalKeyword[] array = (TerminalKeyword[])(object)Resources.FindObjectsOfTypeAll(typeof(TerminalKeyword));
			TerminalKeyword result = null;
			for (int i = 0; i < array.Length; i++)
			{
				if (string.Equals(((Object)array[i]).name, keywordName))
				{
					result = array[i];
					break;
				}
			}
			return result;
		}
	}
}
namespace ThaiCompany
{
	[BepInPlugin("com.reonomura.thaicompany", "ThaiCompany", "1.0.4")]
	public class ThaiCompanyPlugin : BaseUnityPlugin
	{
		private static readonly Harmony harmony = new Harmony("com.reonomura.thaicompany");

		private const string MyGUID = "com.reonomura.thaicompany";

		private const string PluginName = "ThaiCompany";

		private const string VersionString = "1.0.4";

		private void Awake()
		{
			((BaseUnityPlugin)this).Logger.LogDebug((object)"Plugin com.reonomura.thaicompany (ThaiCompany) v1.0.4 is loaded.");
			PatchHarmony();
		}

		private void PatchHarmony()
		{
			try
			{
				harmony.PatchAll();
			}
			catch (Exception ex)
			{
				((BaseUnityPlugin)this).Logger.LogError((object)("Error applying Harmony patches: " + ex.Message));
			}
		}
	}
}
namespace ThaiCompany.utils
{
	internal static class ThaiCompanyUtils
	{
	}
}
namespace ThaiCompany.Patches
{
	[HarmonyPatch(typeof(Terminal), "TextPostProcess")]
	internal class TerminalPatch
	{
		internal static readonly ThaiCompanyTerminal terminalThai = new ThaiCompanyTerminal();

		private static string Postfix(string modifiedDisplayText, TerminalNode node)
		{
			return terminalThai.translate(ref modifiedDisplayText, ref node);
		}
	}
	[HarmonyPatch(typeof(Terminal), "BeginUsingTerminal")]
	internal class TerminalNodesPatch
	{
		private static bool patchedTerminalNodes;

		private static bool Prefix()
		{
			if (!patchedTerminalNodes)
			{
				patchedTerminalNodes = true;
				TerminalPatch.terminalThai.initThaiCompanyTerminalNodes();
			}
			return true;
		}
	}
}