Decompiled source of MoreTranslations Korean SoU v0.9.5

MoreTranslations.dll

Decompiled 2 weeks ago
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.IO;
using System.Linq;
using System.Reflection;
using System.Runtime.CompilerServices;
using System.Runtime.Versioning;
using System.Security;
using System.Security.Permissions;
using System.Text;
using System.Text.RegularExpressions;
using BepInEx;
using BepInEx.Core.Logging.Interpolation;
using BepInEx.Logging;
using HarmonyLib;
using Microsoft.CodeAnalysis;
using TMPro;
using UnityEngine;
using UnityEngine.Events;

[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.6.2", FrameworkDisplayName = ".NET Framework 4.6.2")]
[assembly: AssemblyCompany("ATOKoreanMod")]
[assembly: AssemblyConfiguration("Debug")]
[assembly: AssemblyDescription("Across the Obelisk Korean language mod")]
[assembly: AssemblyFileVersion("1.0.0.0")]
[assembly: AssemblyInformationalVersion("1.0.0")]
[assembly: AssemblyProduct("ATOKoreanMod")]
[assembly: AssemblyTitle("ATOKoreanMod")]
[assembly: SecurityPermission(SecurityAction.RequestMinimum, SkipVerification = true)]
[assembly: AssemblyVersion("1.0.0.0")]
[module: UnverifiableCode]
namespace ATOKoreanMod
{
	[BepInPlugin("ATOKoreanMod", "ATOKoreanMod", "1.0.0")]
	public class Plugin : BaseUnityPlugin
	{
		[HarmonyPatch(typeof(Resources), "Load", new Type[]
		{
			typeof(string),
			typeof(Type)
		})]
		private static class TextAssetLoadHook
		{
			private static void Postfix(string path, ref object __result)
			{
				//IL_0118: Unknown result type (might be due to invalid IL or missing references)
				//IL_011f: Expected O, but got Unknown
				//IL_00c1: Unknown result type (might be due to invalid IL or missing references)
				//IL_00c7: Expected O, but got Unknown
				//IL_00d8: Unknown result type (might be due to invalid IL or missing references)
				//IL_00df: Expected O, but got Unknown
				if (!path.StartsWith("Lang/"))
				{
					return;
				}
				string fileName = Path.GetFileName(path);
				object obj = __result;
				TextAsset val = (TextAsset)((obj is TextAsset) ? obj : null);
				string path2 = Path.Combine(ModDirectory, "source", fileName + ".txt");
				if (!File.Exists(path2))
				{
					Directory.CreateDirectory(Path.GetDirectoryName(path2));
					File.WriteAllText(path2, val.text);
					((BaseUnityPlugin)Instance).Logger.LogInfo((object)("    dumped to source/" + fileName + ".txt"));
				}
				string path3 = Path.Combine(ModDirectory, "translation", fileName + ".txt");
				bool flag = default(bool);
				if (File.Exists(path3))
				{
					string text = File.ReadAllText(path3);
					__result = (object)new TextAsset(text);
					ManualLogSource logger = ((BaseUnityPlugin)Instance).Logger;
					BepInExInfoLogInterpolatedStringHandler val2 = new BepInExInfoLogInterpolatedStringHandler(20, 1, ref flag);
					if (flag)
					{
						((BepInExLogInterpolatedStringHandler)val2).AppendFormatted<string>(fileName);
						((BepInExLogInterpolatedStringHandler)val2).AppendLiteral(": loaded translation");
					}
					logger.LogInfo(val2);
				}
				else
				{
					ManualLogSource logger2 = ((BaseUnityPlugin)Instance).Logger;
					BepInExInfoLogInterpolatedStringHandler val2 = new BepInExInfoLogInterpolatedStringHandler(17, 1, ref flag);
					if (flag)
					{
						((BepInExLogInterpolatedStringHandler)val2).AppendFormatted<string>(fileName);
						((BepInExLogInterpolatedStringHandler)val2).AppendLiteral(": loaded original");
					}
					logger2.LogInfo(val2);
				}
			}
		}

		public static Plugin Instance { get; private set; }

		public static string ModDirectory { get; private set; }

		public void Awake()
		{
			//IL_0033: Unknown result type (might be due to invalid IL or missing references)
			//IL_0039: Expected O, but got Unknown
			//IL_0073: Unknown result type (might be due to invalid IL or missing references)
			//IL_0079: Expected O, but got Unknown
			Instance = this;
			ModDirectory = Path.GetDirectoryName(((object)Instance).GetType().Assembly.Location);
			ManualLogSource logger = ((BaseUnityPlugin)this).Logger;
			bool flag = default(bool);
			BepInExInfoLogInterpolatedStringHandler val = new BepInExInfoLogInterpolatedStringHandler(18, 1, ref flag);
			if (flag)
			{
				((BepInExLogInterpolatedStringHandler)val).AppendLiteral("Plugin ");
				((BepInExLogInterpolatedStringHandler)val).AppendFormatted<string>("ATOKoreanMod");
				((BepInExLogInterpolatedStringHandler)val).AppendLiteral(" is loaded!");
			}
			logger.LogInfo(val);
			ManualLogSource logger2 = ((BaseUnityPlugin)this).Logger;
			val = new BepInExInfoLogInterpolatedStringHandler(0, 1, ref flag);
			if (flag)
			{
				((BepInExLogInterpolatedStringHandler)val).AppendFormatted<string>(ModDirectory);
			}
			logger2.LogInfo(val);
			Directory.CreateDirectory(Path.Combine(ModDirectory, "translation"));
			try
			{
				Harmony.CreateAndPatchAll(((object)this).GetType().Assembly, "ATOKoreanMod");
			}
			catch (Exception ex)
			{
				((BaseUnityPlugin)this).Logger.LogError((object)ex);
			}
		}

		public void Start()
		{
			try
			{
				LoadFont();
			}
			catch (Exception ex)
			{
				((BaseUnityPlugin)this).Logger.LogError((object)ex.ToString());
				((BaseUnityPlugin)this).Logger.LogError((object)ex.StackTrace);
			}
		}

		private void LoadFont()
		{
			string text = Path.Combine(ModDirectory, "font", "samliphopang");
			((BaseUnityPlugin)this).Logger.LogInfo((object)("Font bundle path: " + text));
			AssetBundle val = AssetBundle.LoadFromFile(text);
			TMP_FontAsset item = val.LoadAllAssets<TMP_FontAsset>().First();
			((BaseUnityPlugin)this).Logger.LogInfo((object)"loaded font bundle");
			TMP_FontAsset val2 = Resources.FindObjectsOfTypeAll<TMP_FontAsset>().First((TMP_FontAsset x) => ((Object)x).name == "AlbertTextBold SDF");
			val2.fallbackFontAssetTable.Insert(0, item);
			TMP_FontAsset val3 = Resources.FindObjectsOfTypeAll<TMP_FontAsset>().First((TMP_FontAsset x) => ((Object)x).name == "CantoraOne-Regular SDF");
			val3.fallbackFontAssetTable.Insert(0, item);
			((BaseUnityPlugin)this).Logger.LogInfo((object)"Patched fonts");
		}
	}
	public static class PluginInfo
	{
		public const string PLUGIN_GUID = "ATOKoreanMod";

		public const string PLUGIN_NAME = "ATOKoreanMod";

		public const string PLUGIN_VERSION = "1.0.0";
	}
}
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 MoreTranslations
{
	[BepInPlugin("MoreTranslations_DontTouchFranky", "MoreTranslations", "1.1.0")]
	public class Plugin : BaseUnityPlugin
	{
		private static Dictionary<string, Dictionary<string, string>> TextStrings;

		private static Dictionary<string, Dictionary<string, string>> TextKeynotes;

		private static TMP_Dropdown languagesDropdown = null;

		private static string selectedLanguage = null;

		private static List<string> languages = new List<string>();

		private static List<string> tips = new List<string>();

		private static TMP_FontAsset alternativeFont = null;

		private void Awake()
		{
			Harmony.CreateAndPatchAll(typeof(Plugin), (string)null);
		}

		[HarmonyPatch(typeof(TextMeshPro), "OnEnable")]
		[HarmonyPostfix]
		public static void FontPatchTMPText(TextMeshPro __instance)
		{
			if ((Object)(object)alternativeFont != (Object)null)
			{
				((TMP_Text)__instance).font.fallbackFontAssetTable = new List<TMP_FontAsset> { alternativeFont };
			}
		}

		[HarmonyPatch(typeof(GameManager), "Start")]
		[HarmonyPrefix]
		private static void Start()
		{
			//IL_006f: Unknown result type (might be due to invalid IL or missing references)
			//IL_0076: Expected O, but got Unknown
			//IL_017b: Unknown result type (might be due to invalid IL or missing references)
			//IL_0185: Expected O, but got Unknown
			//IL_01b5: Unknown result type (might be due to invalid IL or missing references)
			//IL_01bf: Expected O, but got Unknown
			string pluginPath = Paths.PluginPath;
			selectedLanguage = PlayerPrefs.GetString("linguaSelezionata");
			string text = pluginPath;
			text = ((!(selectedLanguage.ToLower() == "jp") && !(selectedLanguage.ToLower() == "japanese")) ? (text + "/CantoraOne-Regular Fix.ttf") : (text + "/NotoSerifJP-Regular.otf"));
			if (File.Exists(text))
			{
				Font val = new Font(text);
				TMP_FontAsset val2 = TMP_FontAsset.CreateFontAsset(val);
				if ((Object)(object)val2 != (Object)null)
				{
					alternativeFont = val2;
				}
			}
			TextStrings = new Dictionary<string, Dictionary<string, string>>(StringComparer.OrdinalIgnoreCase);
			TextKeynotes = new Dictionary<string, Dictionary<string, string>>(StringComparer.OrdinalIgnoreCase);
			if (selectedLanguage != "")
			{
				Debug.Log((object)("MoreTranslations | Selected -> " + selectedLanguage));
				TextStrings[selectedLanguage] = new Dictionary<string, string>();
				TextKeynotes[selectedLanguage] = new Dictionary<string, string>();
			}
			else
			{
				Debug.Log((object)"MoreTranslations | Selected -> Default");
			}
			CreateLanguageDropdown();
			GetTranslationLanguages();
			if (selectedLanguage != "" && !languages.Contains(selectedLanguage))
			{
				selectedLanguage = "";
				PlayerPrefs.SetString("linguaSelezionata", selectedLanguage);
			}
			languagesDropdown.options.Add(new OptionData("Default"));
			foreach (string language in languages)
			{
				languagesDropdown.options.Add(new OptionData(Capitalize(language.ToLower())));
			}
			if (selectedLanguage == "")
			{
				languagesDropdown.value = 0;
			}
			else
			{
				languagesDropdown.value = languagesDropdown.options.FindIndex((OptionData x) => x.text.ToLower() == selectedLanguage.ToLower());
			}
			((UnityEvent<int>)(object)languagesDropdown.onValueChanged).AddListener((UnityAction<int>)delegate(int value)
			{
				string text2 = languagesDropdown.options[value].text;
				if (text2 == "Default")
				{
					text2 = "";
				}
				PlayerPrefs.SetString("linguaSelezionata", text2.ToLower());
				AlertManager.Instance.AlertConfirm(Texts.Instance.GetText("selectLanguageChanged", ""), "");
			});
		}

		private static void CreateLanguageDropdown()
		{
			//IL_0050: Unknown result type (might be due to invalid IL or missing references)
			//IL_005a: Expected O, but got Unknown
			//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_0075: 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_0098: Unknown result type (might be due to invalid IL or missing references)
			TMP_Dropdown languageDropdown = SettingsManager.Instance.languageDropdown;
			TMP_Dropdown val = Object.Instantiate<TMP_Dropdown>(languageDropdown, ((Component)languageDropdown).transform.parent);
			((Component)val).transform.SetSiblingIndex(((Component)languageDropdown).transform.GetSiblingIndex() + 1);
			((Object)val).name = "languageDropdown2";
			((UnityEventBase)val.onValueChanged).RemoveAllListeners();
			val.onValueChanged = new DropdownEvent();
			RectTransform component = ((Component)val).GetComponent<RectTransform>();
			Rect rect = component.rect;
			double num = ((Rect)(ref rect)).width;
			component.anchoredPosition = new Vector2((float)((double)component.anchoredPosition.x + num + 10.0), component.anchoredPosition.y);
			val.ClearOptions();
			languagesDropdown = val;
		}

		private static void GetTranslationLanguages()
		{
			string directoryName = Path.GetDirectoryName(Assembly.GetExecutingAssembly().Location);
			string text = directoryName + "/../";
			string[] directories = Directory.GetDirectories(Paths.PluginPath);
			string[] array = directories;
			foreach (string path in array)
			{
				string path2 = Path.Combine(path, "moretranslations.txt");
				if (!File.Exists(path2))
				{
					continue;
				}
				string[] array2 = File.ReadAllLines(path2);
				string[] array3 = array2;
				foreach (string text2 in array3)
				{
					if (!text2.StartsWith("#"))
					{
						string item = text2.Trim().ToLower();
						if (!languages.Contains(item))
						{
							languages.Add(item);
						}
					}
				}
			}
			if (languages.Count > 0)
			{
				Debug.Log((object)"MoreTranslations | Languages found:");
				{
					foreach (string language in languages)
					{
						Debug.Log((object)("MoreTranslations | - " + language));
					}
					return;
				}
			}
			Debug.LogWarning((object)"MoreTranslations | No languages found. Please install a language pack to use this plugin.");
		}

		private static string Capitalize(string str)
		{
			if (str == null || str.Length < 1)
			{
				return str;
			}
			return char.ToUpper(str[0]) + str.Substring(1);
		}

		[HarmonyPatch(typeof(Texts), "GetText")]
		[HarmonyPrefix]
		private static bool GetTextPrefix(string _id, string _type, ref string __result)
		{
			__result = "";
			if ((Object)(object)Globals.Instance == (Object)null || !GameManager.Instance.PrefsLoaded)
			{
				__result = "";
				return false;
			}
			string text = _id.Replace(" ", "").ToLower();
			if (!(text != ""))
			{
				__result = "";
				return false;
			}
			if (TextStrings != null && TextStrings.ContainsKey(selectedLanguage))
			{
				if (_type != "")
				{
					text = _type.ToLower() + "_" + text.ToLower();
				}
				if (TextStrings[selectedLanguage].ContainsKey(text))
				{
					string text2 = TextStrings[selectedLanguage][text];
					if (text2 != "")
					{
						__result = text2;
						return false;
					}
				}
			}
			return true;
		}

		[HarmonyPatch(typeof(Texts), "LoadTranslationText")]
		[HarmonyPostfix]
		private static void LoadTranslationTextPostfix(string type)
		{
			if (tips.Count > 0)
			{
				Texts.Instance.TipsList.Clear();
				Texts.Instance.TipsList.AddRange(tips);
			}
		}

		[HarmonyPatch(typeof(Texts), "LoadTranslationText")]
		[HarmonyPrefix]
		private static void LoadTranslationTextPrefix(string type)
		{
			if (!(selectedLanguage != ""))
			{
				return;
			}
			string pluginPath = Paths.PluginPath;
			string text = "";
			string[] directories = Directory.GetDirectories(pluginPath);
			string[] array = directories;
			foreach (string text2 in array)
			{
				string path = text2 + "/moretranslations.txt";
				if (!File.Exists(path))
				{
					continue;
				}
				string[] array2 = File.ReadAllLines(path);
				string[] array3 = array2;
				foreach (string text3 in array3)
				{
					if (!text3.StartsWith("#"))
					{
						string text4 = text3.Trim().ToLower();
						if (text4 == selectedLanguage)
						{
							text = text2 + "/";
						}
					}
				}
			}
			if (!(text != ""))
			{
				return;
			}
			string text5 = "";
			string[] array4 = null;
			type = type.ToLower();
			switch (type)
			{
			case "":
				text5 = text + "/" + selectedLanguage + ".txt";
				if (File.Exists(text5))
				{
					array4 = File.ReadAllLines(text5);
				}
				break;
			case "keynotes":
				text5 = text + "/" + selectedLanguage + "_keynotes.txt";
				if (File.Exists(text5))
				{
					array4 = File.ReadAllLines(text5);
				}
				break;
			case "traits":
				text5 = text + "/" + selectedLanguage + "_traits.txt";
				if (File.Exists(text5))
				{
					array4 = File.ReadAllLines(text5);
				}
				break;
			case "auracurse":
				text5 = text + "/" + selectedLanguage + "_auracurse.txt";
				if (File.Exists(text5))
				{
					array4 = File.ReadAllLines(text5);
				}
				break;
			case "events":
				text5 = text + "/" + selectedLanguage + "_events.txt";
				if (File.Exists(text5))
				{
					array4 = File.ReadAllLines(text5);
				}
				break;
			case "nodes":
				text5 = text + "/" + selectedLanguage + "_nodes.txt";
				if (File.Exists(text5))
				{
					array4 = File.ReadAllLines(text5);
				}
				break;
			case "cards":
				text5 = text + "/" + selectedLanguage + "_cards.txt";
				if (File.Exists(text5))
				{
					array4 = File.ReadAllLines(text5);
				}
				break;
			case "fluff":
				text5 = text + "/" + selectedLanguage + "_cardsfluff.txt";
				if (File.Exists(text5))
				{
					array4 = File.ReadAllLines(text5);
				}
				break;
			case "class":
				text5 = text + "/" + selectedLanguage + "_class.txt";
				if (File.Exists(text5))
				{
					array4 = File.ReadAllLines(text5);
				}
				break;
			case "monsters":
				text5 = text + "/" + selectedLanguage + "_monsters.txt";
				if (File.Exists(text5))
				{
					array4 = File.ReadAllLines(text5);
				}
				break;
			case "requirements":
				text5 = text + "/" + selectedLanguage + "_requirements.txt";
				if (File.Exists(text5))
				{
					array4 = File.ReadAllLines(text5);
				}
				break;
			case "tips":
				text5 = text + "/" + selectedLanguage + "_tips.txt";
				if (File.Exists(text5))
				{
					array4 = File.ReadAllLines(text5);
				}
				break;
			}
			if (array4 == null)
			{
				return;
			}
			List<string> list = new List<string>(array4);
			int num = 0;
			StringBuilder stringBuilder = new StringBuilder();
			StringBuilder stringBuilder2 = new StringBuilder();
			for (int k = 0; k < list.Count; k++)
			{
				string text6 = list[k];
				if (text6 == "" || text6[0] == '#')
				{
					continue;
				}
				string[] array5 = text6.Trim().Split(new char[1] { '=' }, 2);
				if (array5 == null || array5.Length < 2)
				{
					continue;
				}
				array5[0] = array5[0].Trim().ToLower();
				array5[1] = Functions.SplitString("//", array5[1])[0].Trim();
				switch (type)
				{
				case "keynotes":
					stringBuilder.Append("keynotes_");
					break;
				case "traits":
					stringBuilder.Append("traits_");
					break;
				case "auracurse":
					stringBuilder.Append("auracurse_");
					break;
				case "events":
					stringBuilder.Append("events_");
					break;
				case "nodes":
					stringBuilder.Append("nodes_");
					break;
				case "cards":
				case "fluff":
					stringBuilder.Append("cards_");
					break;
				case "class":
					stringBuilder.Append("class_");
					break;
				case "monsters":
					stringBuilder.Append("monsters_");
					break;
				case "requirements":
					stringBuilder.Append("requirements_");
					break;
				case "tips":
					stringBuilder.Append("tips_");
					break;
				}
				stringBuilder.Append(array5[0]);
				if (TextStrings[selectedLanguage].ContainsKey(stringBuilder.ToString()))
				{
					TextStrings[selectedLanguage][stringBuilder.ToString()] = array5[1];
				}
				else
				{
					TextStrings[selectedLanguage].Add(stringBuilder.ToString(), array5[1]);
				}
				if (type == "tips")
				{
					tips.Add(array5[1]);
				}
				bool flag = true;
				switch (type)
				{
				case "":
					if (array5[1].StartsWith("rptd_", StringComparison.OrdinalIgnoreCase))
					{
						stringBuilder2.Append(array5[1].Substring(5).ToLower());
						TextStrings[selectedLanguage][stringBuilder.ToString()] = TextStrings[selectedLanguage][stringBuilder2.ToString()];
						flag = false;
						stringBuilder2.Clear();
					}
					break;
				case "events":
					if (array5[1].StartsWith("rptd_", StringComparison.OrdinalIgnoreCase))
					{
						stringBuilder2.Append("events_");
						stringBuilder2.Append(array5[1].Substring(5).ToLower());
						TextStrings[selectedLanguage][stringBuilder.ToString()] = TextStrings[selectedLanguage][stringBuilder2.ToString()];
						flag = false;
						stringBuilder2.Clear();
					}
					break;
				case "cards":
					if (array5[1].StartsWith("rptd_", StringComparison.OrdinalIgnoreCase))
					{
						stringBuilder2.Append("cards_");
						stringBuilder2.Append(array5[1].Substring(5).ToLower());
						TextStrings[selectedLanguage][stringBuilder.ToString()] = TextStrings[selectedLanguage][stringBuilder2.ToString()];
						flag = false;
						stringBuilder2.Clear();
					}
					break;
				case "monsters":
					if (array5[1].StartsWith("rptd_", StringComparison.OrdinalIgnoreCase))
					{
						stringBuilder2.Append("monsters_");
						stringBuilder2.Append(array5[1].Substring(5).ToLower());
						TextStrings[selectedLanguage][stringBuilder.ToString()] = TextStrings[selectedLanguage][stringBuilder2.ToString()];
						flag = false;
						stringBuilder2.Clear();
					}
					break;
				}
				if (flag)
				{
					string text7 = Regex.Replace(Regex.Replace(array5[1], "<(.*?)>", ""), "\\s+", " ");
					num += text7.Split(' ').Length;
				}
				stringBuilder.Clear();
			}
		}

		public static void ExportEnglishTextForTranslation()
		{
			ActualExport("en");
			ActualExport("en_keynotes");
			ActualExport("en_traits");
			ActualExport("en_auracurse");
			ActualExport("en_events");
			ActualExport("en_nodes");
			ActualExport("en_cards");
			ActualExport("en_cardsfluff");
			ActualExport("en_class");
			ActualExport("en_monsters");
			ActualExport("en_requirements");
			ActualExport("en_tips");
			File.WriteAllText(Path.Combine(Paths.PluginPath, "MoreTranslations_Export", "moretranslations.txt"), "English");
		}

		public static void ActualExport(string fileName)
		{
			Debug.Log((object)("Exporting " + fileName));
			Object obj = Resources.Load("Lang/" + fileName);
			TextAsset val = (TextAsset)(object)((obj is TextAsset) ? obj : null);
			DirectoryInfo directoryInfo = new DirectoryInfo(Path.Combine(Paths.PluginPath, "MoreTranslations_Export"));
			if (!directoryInfo.Exists)
			{
				directoryInfo.Create();
			}
			File.WriteAllText(Path.Combine(Paths.PluginPath, "MoreTranslations_Export", ((fileName == "en") ? "English" : fileName.Replace("en_", "English_")) + ".txt"), ((object)val).ToString());
		}
	}
	public static class PluginInfo
	{
		public const string PLUGIN_GUID = "MoreTranslations";

		public const string PLUGIN_NAME = "MoreTranslations";

		public const string PLUGIN_VERSION = "1.1.0";
	}
}