Decompiled source of ReLocalization v1.1.1

plugins/ReLocalization.dll

Decompiled 2 years ago
using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.Diagnostics;
using System.IO;
using System.Linq;
using System.Reflection;
using System.Runtime.CompilerServices;
using System.Runtime.InteropServices;
using System.Runtime.Versioning;
using BepInEx;
using BepInEx.Logging;
using HarmonyLib;
using PotionCraft.LocalizationSystem;
using ReLocalization.Patches;
using YamlDotNet.RepresentationModel;

[assembly: CompilationRelaxations(8)]
[assembly: RuntimeCompatibility(WrapNonExceptionThrows = true)]
[assembly: Debuggable(DebuggableAttribute.DebuggingModes.IgnoreSymbolStoreSequencePoints)]
[assembly: AssemblyTitle("ReLocalization")]
[assembly: AssemblyDescription("")]
[assembly: AssemblyConfiguration("")]
[assembly: AssemblyCompany("")]
[assembly: AssemblyProduct("ReLocalization")]
[assembly: AssemblyCopyright("Copyright ©  2024")]
[assembly: AssemblyTrademark("")]
[assembly: ComVisible(false)]
[assembly: Guid("8e8be687-07f6-46c9-9298-1611415e4adc")]
[assembly: AssemblyFileVersion("1.1.0.0")]
[assembly: TargetFramework(".NETFramework,Version=v4.7.2", FrameworkDisplayName = ".NET Framework 4.7.2")]
[assembly: AssemblyVersion("1.1.0.0")]
namespace ReLocalization
{
	[BepInPlugin("AirBurn.ReLocalization", "ReLocalization", "1.1.0.0")]
	[BepInProcess("Potion Craft.exe")]
	public class ModInfo : BaseUnityPlugin
	{
		private static ManualLogSource Logger;

		public const string GUID = "AirBurn.ReLocalization";

		public const string MODNAME = "ReLocalization";

		public const string VERSION = "1.1.0.0";

		public void Awake()
		{
			Logger = ((BaseUnityPlugin)this).Logger;
			GlobalConfigs.LazyLoad = ((BaseUnityPlugin)this).Config.Bind<bool>("General", "LazyLoad", false, "Should all localization be lazy-loaded or loaded on startup.").Value;
			GlobalConfigs.LogLoad = ((BaseUnityPlugin)this).Config.Bind<bool>("General", "LogLoad", false, "Show logs about the presence and absence of localization files. Disable LazyLoad to debug more easly.").Value;
			GlobalConfigs.LocalizatorMode = ((BaseUnityPlugin)this).Config.Bind<bool>("General", "LocalizatorMode", false, "Localization keys will be displayed instead of localized text.").Value;
			Localization.AddLocalizationFor((BaseUnityPlugin)(object)this);
			Harmony.CreateAndPatchAll(typeof(LocalizationPatch), (string)null);
			Logger.LogInfo((object)"ReLocalization succesfully loaded");
		}

		public static void Log(object message, LogLevel level = 16)
		{
			//IL_0005: Unknown result type (might be due to invalid IL or missing references)
			Logger.Log(level, message);
		}
	}
	public static class GlobalConfigs
	{
		public static bool LazyLoad { get; set; }

		public static bool LocalizatorMode { get; set; }

		public static bool LogLoad { get; set; }
	}
	public static class Localization
	{
		internal class LocalizationEntry
		{
			internal readonly Dictionary<string, string> localizationData = new Dictionary<string, string>();

			internal void AddLocalization(string key, string value)
			{
				localizationData[key] = value;
			}

			internal string GetLocalization(string key)
			{
				if (localizationData.TryGetValue(key, out var value))
				{
					return value;
				}
				return null;
			}
		}

		private static readonly Dictionary<string, int> localizations = new Dictionary<string, int>();

		private static readonly Dictionary<Locale, LocalizationEntry> localizationData = new Dictionary<Locale, LocalizationEntry>();

		private static readonly string locFolderName = "Localization";

		private static readonly string LocalizationPathBase = Path.GetDirectoryName(Assembly.GetExecutingAssembly().Location);

		public static string modLocalizationFolder(string modid)
		{
			string[] obj = new string[6] { LocalizationPathBase, null, null, null, null, null };
			char directorySeparatorChar = Path.DirectorySeparatorChar;
			obj[1] = directorySeparatorChar.ToString();
			obj[2] = modid;
			directorySeparatorChar = Path.DirectorySeparatorChar;
			obj[3] = directorySeparatorChar.ToString();
			obj[4] = locFolderName;
			directorySeparatorChar = Path.DirectorySeparatorChar;
			obj[5] = directorySeparatorChar.ToString();
			return string.Concat(obj);
		}

		public static ReadOnlyCollection<string> Mods()
		{
			return localizations.Keys.ToList().AsReadOnly();
		}

		public static void AddLocalizationFor(BaseUnityPlugin mod)
		{
			AddLocalizationFor(MetadataHelper.GetMetadata((object)mod).GUID);
		}

		public static void AddLocalizationFor(string modid)
		{
			localizations.Add(modid, 0);
			if (!GlobalConfigs.LazyLoad)
			{
				LoadLocalizationFor(modid);
			}
		}

		internal static void LoadLocalizationFor(string modid)
		{
			//IL_001d: Unknown result type (might be due to invalid IL or missing references)
			//IL_0022: Unknown result type (might be due to invalid IL or missing references)
			//IL_0024: Unknown result type (might be due to invalid IL or missing references)
			foreach (Locale value in Enum.GetValues(typeof(Locale)))
			{
				LoadLocalizationFor(modid, value);
			}
		}

		public static void LoadAllLocalization(bool reload)
		{
			//IL_001d: Unknown result type (might be due to invalid IL or missing references)
			foreach (Locale value in Enum.GetValues(typeof(Locale)))
			{
				LoadLocalization(value, reload);
			}
		}

		public static string GetLocalized(string key)
		{
			//IL_0001: Unknown result type (might be due to invalid IL or missing references)
			return GetLocalized(key, LocalizationManager.CurrentLocale);
		}

		public static string GetLocalized(string key, Locale locale)
		{
			//IL_0001: Unknown result type (might be due to invalid IL or missing references)
			//IL_000b: Unknown result type (might be due to invalid IL or missing references)
			//IL_0013: Unknown result type (might be due to invalid IL or missing references)
			string localizedSilent = GetLocalizedSilent(key, locale);
			if (localizedSilent == null)
			{
				ModInfo.Log((((int)locale == 0) ? "D" : $"Neither '{locale}.yml' nor d") + "efault localization file doesn't contain valid '" + key + "' key.", (LogLevel)2);
				return key;
			}
			return localizedSilent;
		}

		internal static bool IsLocalizationLoaded(string mod, Locale locale)
		{
			//IL_000c: Unknown result type (might be due to invalid IL or missing references)
			//IL_000f: Unknown result type (might be due to invalid IL or missing references)
			return (localizations[mod] & (1 << (int)locale)) != 0;
		}

		internal static void SetLocalizationLoaded(string mod, Locale locale, bool loaded)
		{
			//IL_0001: Unknown result type (might be due to invalid IL or missing references)
			//IL_0004: Unknown result type (might be due to invalid IL or missing references)
			int num = 1 << (int)locale;
			if (loaded)
			{
				localizations[mod] |= num;
			}
			else
			{
				localizations[mod] &= ~num;
			}
		}

		internal static void LoadLocalization(Locale locale, bool force)
		{
			//IL_000e: Unknown result type (might be due to invalid IL or missing references)
			//IL_000f: Unknown result type (might be due to invalid IL or missing references)
			//IL_0050: Unknown result type (might be due to invalid IL or missing references)
			string[] array = localizations.Keys.Where((string mod) => force || !IsLocalizationLoaded(mod, locale)).ToArray();
			for (int i = 0; i < array.Length; i++)
			{
				LoadLocalizationFor(array[i], locale);
			}
		}

		internal static void LoadLocalizationFor(string mod, Locale locale)
		{
			//IL_0001: Unknown result type (might be due to invalid IL or missing references)
			//IL_000a: Unknown result type (might be due to invalid IL or missing references)
			//IL_0086: Unknown result type (might be due to invalid IL or missing references)
			//IL_0093: Unknown result type (might be due to invalid IL or missing references)
			//IL_006f: Unknown result type (might be due to invalid IL or missing references)
			//IL_0050: Unknown result type (might be due to invalid IL or missing references)
			if (IsLocalizationLoaded(mod, locale))
			{
				SetLocalizationLoaded(mod, locale, loaded: false);
			}
			string path = modLocalizationFolder(mod) + ((object)(Locale)(ref locale)).ToString() + ".yml";
			if (!File.Exists(path))
			{
				if (GlobalConfigs.LogLoad && mod != "AirBurn.ReLocalization")
				{
					ModInfo.Log($"'{locale}.yml' localization file doesn't found for mod '{mod}'. Please consider helping mod author to provide translation.", (LogLevel)4);
				}
				return;
			}
			if (GlobalConfigs.LogLoad)
			{
				ModInfo.Log($"'{locale}.yml' found for mod '{mod}'.", (LogLevel)8);
			}
			localizationData[locale] = ReadLocalizationByPath(path);
			SetLocalizationLoaded(mod, locale, loaded: true);
		}

		internal static void TryLoadLocale(Locale locale)
		{
			//IL_0005: Unknown result type (might be due to invalid IL or missing references)
			//IL_000d: Unknown result type (might be due to invalid IL or missing references)
			if (!localizationData.ContainsKey(locale))
			{
				LoadLocalization(locale, force: false);
			}
		}

		internal static string GetLocalizedSilent(string key, Locale locale)
		{
			//IL_0000: Unknown result type (might be due to invalid IL or missing references)
			//IL_000d: Unknown result type (might be due to invalid IL or missing references)
			//IL_0024: Unknown result type (might be due to invalid IL or missing references)
			TryLoadLocale(locale);
			string text = null;
			if (localizationData.TryGetValue(locale, out var value))
			{
				text = value.GetLocalization(key);
			}
			string text2 = text;
			if (text2 == null)
			{
				if ((int)locale == 0)
				{
					return null;
				}
				text2 = GetLocalizedSilent(key, (Locale)0);
			}
			return text2;
		}

		private static LocalizationEntry ReadLocalizationByPath(string path)
		{
			//IL_000d: Unknown result type (might be due to invalid IL or missing references)
			//IL_0013: Expected O, but got Unknown
			LocalizationEntry localizationEntry = new LocalizationEntry();
			using StreamReader streamReader = new StreamReader(path);
			YamlStream val = new YamlStream();
			val.Load((TextReader)streamReader);
			if (val.Documents.Count == 0)
			{
				ModInfo.Log("Empty localization file on path " + path + "!", (LogLevel)2);
				return localizationEntry;
			}
			YamlNode rootNode = val.Documents[0].RootNode;
			foreach (KeyValuePair<YamlNode, YamlNode> item in (YamlMappingNode)((rootNode is YamlMappingNode) ? rootNode : null))
			{
				localizationEntry.AddLocalization((string)item.Key, (string)item.Value);
			}
			return localizationEntry;
		}
	}
}
namespace ReLocalization.Patches
{
	[HarmonyPatch(typeof(LocalizationManager))]
	internal class LocalizationPatch
	{
		[HarmonyPatch("GetText", new Type[]
		{
			typeof(string),
			typeof(Locale)
		})]
		[HarmonyPrefix]
		public static bool GetText(string key, Locale locale, ref string __result)
		{
			//IL_000a: Unknown result type (might be due to invalid IL or missing references)
			if (GlobalConfigs.LocalizatorMode)
			{
				return false;
			}
			string localizedSilent = Localization.GetLocalizedSilent(key, locale);
			if (localizedSilent == null)
			{
				return true;
			}
			__result = localizedSilent;
			return false;
		}
	}
}