Decompiled source of ModLocalizer v1.0.1

ModLocalizer.dll

Decompiled 9 months ago
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.IO;
using System.Reflection;
using System.Runtime.CompilerServices;
using System.Runtime.Versioning;
using System.Security;
using System.Security.Permissions;
using BepInEx;
using BepInEx.Logging;
using HarmonyLib;
using Microsoft.CodeAnalysis;
using Reptile;
using UnityEngine;

[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.7.2", FrameworkDisplayName = ".NET Framework 4.7.2")]
[assembly: AssemblyCompany("ModLocalizer")]
[assembly: AssemblyConfiguration("Debug")]
[assembly: AssemblyDescription("My first plugin")]
[assembly: AssemblyFileVersion("1.0.0.0")]
[assembly: AssemblyInformationalVersion("1.0.0+f80ec1ebdd0507cf22080b3e0d913793ffa69148")]
[assembly: AssemblyProduct("ModLocalizer")]
[assembly: AssemblyTitle("ModLocalizer")]
[assembly: SecurityPermission(SecurityAction.RequestMinimum, SkipVerification = true)]
[assembly: AssemblyVersion("1.0.0.0")]
[module: UnverifiableCode]
[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 ModLocalizer
{
	[BepInPlugin("trpg.brc.modlocalizer", "ModLocalizer", "1.0.1")]
	public class Core : BaseUnityPlugin
	{
		public const string PluginGUID = "trpg.brc.modlocalizer";

		public const string PluginName = "ModLocalizer";

		public const string PluginVersion = "1.0.1";

		internal static ManualLogSource Logger = Logger.CreateLogSource("ModLocalizer");

		internal List<PluginLocalizer> Localizers = new List<PluginLocalizer>();

		internal static Core Instance { get; private set; }

		private void Awake()
		{
			//IL_0023: Unknown result type (might be due to invalid IL or missing references)
			//IL_0029: Expected O, but got Unknown
			if ((Object)(object)Instance != (Object)null)
			{
				Object.Destroy((Object)(object)this);
			}
			Instance = this;
			Harmony val = new Harmony("ModLocalizer");
			val.PatchAll();
		}
	}
	[HarmonyPatch(typeof(UIManager), "InstantiateMainMenuSceneUI")]
	internal class UIManager_InstantiateMainMenuSceneUI_Patch
	{
		public static void Postfix()
		{
			for (int i = 0; i < Core.Instance.Localizers.Count; i++)
			{
				if (Core.Instance.Localizers[i] == null)
				{
					Core.Instance.Localizers.RemoveAt(i);
				}
				Core.Instance.Localizers[i].Initialize();
			}
		}
	}
	[HarmonyPatch(typeof(OptionsMenuGameTab), "ApplyLanguage")]
	internal class OptionsMenuGameTab_ApplyLanguage_Patch
	{
		public static void Postfix()
		{
			//IL_0048: Unknown result type (might be due to invalid IL or missing references)
			for (int i = 0; i < Core.Instance.Localizers.Count; i++)
			{
				if (Core.Instance.Localizers[i] == null)
				{
					Core.Instance.Localizers.RemoveAt(i);
				}
				Core.Instance.Localizers[i].UpdateLocalization(Core.Instance.Localizer.Language);
			}
		}
	}
	public class PluginLocalizer
	{
		public delegate void OnLanguageChangedDelegate(SystemLanguage language);

		public delegate void OnInitializationFinishedDelegate();

		private string pluginName;

		private SystemLanguage defaultLanguage;

		private string languageFilePath;

		private SystemLanguage localizationLanguage;

		private PlatformLanguages platformLanguages;

		private LocalizationLookupTable localizationLookupTable;

		private LocalizationData localizationData;

		public bool Initialized { get; private set; } = false;


		public event OnLanguageChangedDelegate OnLanguageChanged;

		public event OnInitializationFinishedDelegate OnInitializationFinished;

		public PluginLocalizer(string pluginName, string languageFilePath, SystemLanguage defaultLanguage = 10)
		{
			//IL_0017: 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)
			this.pluginName = pluginName;
			this.defaultLanguage = defaultLanguage;
			this.languageFilePath = languageFilePath;
			platformLanguages = CheckAvailableLanguages();
			Core.Instance.Localizers.Add(this);
		}

		private PlatformLanguages CheckAvailableLanguages()
		{
			//IL_0068: Unknown result type (might be due to invalid IL or missing references)
			//IL_0081: Unknown result type (might be due to invalid IL or missing references)
			PlatformLanguages val = ScriptableObject.CreateInstance<PlatformLanguages>();
			string[] files = Directory.GetFiles(languageFilePath, "*");
			List<SystemLanguage> list = new List<SystemLanguage>();
			string[] array = files;
			foreach (string path in array)
			{
				if (Path.GetExtension(path) == ".fods")
				{
					string fileNameWithoutExtension = Path.GetFileNameWithoutExtension(path);
					if (Enum.TryParse<SystemLanguage>(fileNameWithoutExtension, out SystemLanguage result))
					{
						Core.Logger.LogInfo((object)$"Found language {result} for {pluginName}");
						list.Add(result);
					}
				}
			}
			val.availableLanguages = list.ToArray();
			return val;
		}

		internal void Initialize()
		{
			//IL_002f: Unknown result type (might be due to invalid IL or missing references)
			//IL_0034: 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_0065: Unknown result type (might be due to invalid IL or missing references)
			//IL_005a: Unknown result type (might be due to invalid IL or missing references)
			if (!Initialized)
			{
				Initialized = true;
				localizationData = Core.Instance.localizerData;
				SystemLanguage language = Core.Instance.Localizer.Language;
				if (this.OnInitializationFinished != null)
				{
					this.OnInitializationFinished();
				}
				if (IsLanguageAvailable(language))
				{
					UpdateLocalization(language);
				}
				else
				{
					UpdateLocalization(defaultLanguage);
				}
				Core.Logger.LogInfo((object)("Finished initialization for " + pluginName));
			}
		}

		private bool IsLanguageAvailable(SystemLanguage language)
		{
			//IL_0045: Unknown result type (might be due to invalid IL or missing references)
			//IL_0046: Invalid comparison between I4 and Unknown
			if (!Initialized)
			{
				Core.Logger.LogWarning((object)("Localizer with name " + pluginName + " has not been initialized yet!"));
				return false;
			}
			for (int i = 0; i < platformLanguages.availableLanguages.Length; i++)
			{
				if ((int)platformLanguages.availableLanguages[i] == (int)language)
				{
					return true;
				}
			}
			return false;
		}

		private LocalizationLookupTable GenerateLocalizationTable(SystemLanguage language)
		{
			//IL_0024: Unknown result type (might be due to invalid IL or missing references)
			//IL_002a: Expected O, but got Unknown
			//IL_0032: Unknown result type (might be due to invalid IL or missing references)
			string text = Path.Combine(languageFilePath, ((object)(SystemLanguage)(ref language)).ToString() + ".fods");
			LocalizationTableGenerator val = new LocalizationTableGenerator();
			return val.GetLocalizationLookupTable(text, localizationData, language);
		}

		private LocalizationLookupTable HandleNoLocalizationFoundForLanguage(SystemLanguage language)
		{
			return CreateDefaultLocalizationTable();
		}

		private LocalizationLookupTable CreateDefaultLocalizationTable()
		{
			//IL_0003: Unknown result type (might be due to invalid IL or missing references)
			return GenerateLocalizationTable(defaultLanguage);
		}

		private void GenerateLocalization()
		{
			//IL_0004: Unknown result type (might be due to invalid IL or missing references)
			//IL_0023: Unknown result type (might be due to invalid IL or missing references)
			localizationLookupTable = GenerateLocalizationTable(localizationLanguage);
			if (localizationLookupTable == null)
			{
				localizationLookupTable = HandleNoLocalizationFoundForLanguage(localizationLanguage);
			}
		}

		private void DisposeCurrentLocalizationLookupTable()
		{
			if (localizationLookupTable != null)
			{
				localizationLookupTable.Dispose();
			}
			localizationLookupTable = null;
		}

		internal void UpdateLocalization(SystemLanguage language)
		{
			//IL_0002: Unknown result type (might be due to invalid IL or missing references)
			//IL_0007: 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)
			//IL_0019: Unknown result type (might be due to invalid IL or missing references)
			//IL_0038: Unknown result type (might be due to invalid IL or missing references)
			if (localizationLanguage != language)
			{
				DisposeCurrentLocalizationLookupTable();
				localizationLanguage = language;
				GenerateLocalization();
				if (this.OnLanguageChanged != null)
				{
					this.OnLanguageChanged(language);
				}
			}
		}

		public string GetRawTextValue(Subgroups subgroup, string localizationKey)
		{
			if (!Initialized)
			{
				Core.Logger.LogWarning((object)("Localizer with name " + pluginName + " has not been initialized yet!"));
				return localizationKey;
			}
			return localizationLookupTable.GetLocalizationValueFromSubgroup(subgroup.ToString(), localizationKey);
		}

		public GameFontType LoadFontType(GameFontTypes fontType)
		{
			return Core.Instance.Assets.LoadAssetFromBundle<GameFontType>("coreassets", $"assets/gamemanagement/localization/scriptableobjects/fonts/gamefonttypes/{fontType}.asset");
		}

		public GameFontType LoadFontTypeAndRemoveUnusedFonts(GameFontTypes fontType)
		{
			//IL_006a: Unknown result type (might be due to invalid IL or missing references)
			GameFontType val = LoadFontType(fontType);
			if (!Initialized)
			{
				Core.Logger.LogWarning((object)("Localizer with name " + pluginName + " has not been initialized yet!"));
				return val;
			}
			Traverse val2 = Traverse.Create((object)val);
			TextMeshProFont[] value = val2.Field<TextMeshProFont[]>("fonts").Value;
			List<TextMeshProFont> list = new List<TextMeshProFont>();
			for (int i = 0; i < value.Length; i++)
			{
				if (IsLanguageAvailable(value[i].Language))
				{
					list.Add(value[i]);
				}
			}
			if (list.Count < 1)
			{
				throw new Exception("No valid fonts found for this GameFontType. This should never happen.");
			}
			val2.Field<TextMeshProFont[]>("fonts").Value = list.ToArray();
			return val;
		}

		public GameFontType LoadFontTypeEnglishOnly(GameFontTypes fontType)
		{
			//IL_0069: Unknown result type (might be due to invalid IL or missing references)
			//IL_0070: Invalid comparison between Unknown and I4
			GameFontType val = LoadFontType(fontType);
			if (!Initialized)
			{
				Core.Logger.LogWarning((object)("Localizer with name " + pluginName + " has not been initialized yet!"));
				return val;
			}
			Traverse val2 = Traverse.Create((object)val);
			TextMeshProFont[] value = val2.Field<TextMeshProFont[]>("fonts").Value;
			List<TextMeshProFont> list = new List<TextMeshProFont>();
			for (int i = 0; i < value.Length; i++)
			{
				if ((int)value[i].Language == 10)
				{
					list.Add(value[i]);
				}
			}
			if (list.Count < 1)
			{
				throw new Exception("No valid fonts found for this GameFontType. This should never happen.");
			}
			val2.Field<TextMeshProFont[]>("fonts").Value = list.ToArray();
			return val;
		}
	}
	public enum Subgroups
	{
		Text,
		Sizes,
		Dialogue,
		Examples,
		Credits,
		CharacterNames,
		StageNames,
		ObjectiveText,
		EmailMessages,
		Notifications,
		SkinText
	}
	public enum GameFontTypes
	{
		CharacterNameText,
		CreditsNameText,
		CreditsTitleText,
		DefaultText,
		DefaultTextBold,
		DefaultTextHighlight,
		DefaultTextThin,
		DottedText,
		FlavorText,
		GameplayHudCrewText,
		GameplayHudGraffitiText,
		GameplayHudHeaderText,
		GameplayHudLocalizedGraffitiText,
		GameplayHudStatusText,
		LoadingText,
		MainButtonText,
		MainButtonTextBackdrop,
		MainButtonTextHighlight,
		MainButtonTextPressed,
		MenuTimelineSelectedText,
		MenuTimelineText,
		MenuTitlesText,
		MicroBoyText,
		MicroBoyTextWithoutOutline,
		MicroBoyTitle,
		MicroBoyTitleWithoutOutline,
		PhoneClockText,
		PhoneFortuneText,
		PhoneGraffitiSizeText,
		PhoneLocalizedNotifications,
		PhoneMainText,
		PhoneMessageText,
		PhoneMsgSubjectText,
		PhoneNotificationText,
		PhoneOnScreenNotificationText,
		PhoneSubtext,
		YesNoButtonBackdropText,
		YesNoButtonText
	}
	public static class PluginInfo
	{
		public const string PLUGIN_GUID = "ModLocalizer";

		public const string PLUGIN_NAME = "ModLocalizer";

		public const string PLUGIN_VERSION = "1.0.0";
	}
}