Decompiled source of LanguageFileGenerator v1.0.2

plugins/LanguageFileGenerator.dll

Decompiled 6 months 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 BepInEx;
using BepInEx.Bootstrap;
using BepInEx.Logging;
using LanguageFileGenerator.Modules;
using MonoMod.RuntimeDetour;
using R2API;
using R2API.Utils;
using RoR2;

[assembly: CompilationRelaxations(8)]
[assembly: RuntimeCompatibility(WrapNonExceptionThrows = true)]
[assembly: Debuggable(DebuggableAttribute.DebuggingModes.Default | DebuggableAttribute.DebuggingModes.DisableOptimizations | DebuggableAttribute.DebuggingModes.IgnoreSymbolStoreSequencePoints | DebuggableAttribute.DebuggingModes.EnableEditAndContinue)]
[assembly: TargetFramework(".NETStandard,Version=v2.0", FrameworkDisplayName = "")]
[assembly: AssemblyCompany("LanguageFileGenerator")]
[assembly: AssemblyConfiguration("Debug")]
[assembly: AssemblyFileVersion("1.0.0.0")]
[assembly: AssemblyInformationalVersion("1.0.0")]
[assembly: AssemblyProduct("LanguageFileGenerator")]
[assembly: AssemblyTitle("LanguageFileGenerator")]
[assembly: SecurityPermission(SecurityAction.RequestMinimum, SkipVerification = true)]
[assembly: AssemblyVersion("1.0.0.0")]
[module: UnverifiableCode]
namespace LanguageFileGenerator
{
	[BepInDependency(/*Could not decode attribute arguments.*/)]
	[BepInPlugin("AAA.TheTimeSweeper.LanguageFileGenerator", "LanguageFileGenerator", "1.0.2")]
	public class LanguageFileGeneratorPlugin : BaseUnityPlugin
	{
		private Hook languageHook;

		public static LanguageFileGeneratorPlugin instance;

		public static ManualLogSource Log;

		public static Dictionary<Assembly, string> assemblyModNames = new Dictionary<Assembly, string>();

		public const string nullMod = "UNKNOWN";

		private void Awake()
		{
			//IL_0068: Unknown result type (might be due to invalid IL or missing references)
			//IL_0072: Expected O, but got Unknown
			instance = this;
			Log = ((BaseUnityPlugin)this).Logger;
			languageHook = new Hook((MethodBase)typeof(LanguageAPI).GetMethod("Add", new Type[3]
			{
				typeof(string),
				typeof(string),
				typeof(string)
			}), typeof(LanguageFileGeneratorPlugin).GetMethod("OnLanguageHook"));
			RoR2Application.onLoad = (Action)Delegate.Combine(RoR2Application.onLoad, new Action(RoR2Application_OnLoad));
		}

		private void RoR2Application_OnLoad()
		{
			Language.PrintAll();
		}

		public static void OnLanguageHook(Action<string, string, string> orig, string token, string text, string language)
		{
			Language.Add(token, text, GetModName(GetAssembly()));
			orig(token, text, language);
		}

		private static Assembly GetAssembly()
		{
			Assembly assembly = new StackFrame(3, needFileInfo: false).GetMethod()?.DeclaringType.Assembly;
			if (assembly != null && assembly.FullName.Contains("R2API.Language"))
			{
				assembly = new StackFrame(4, needFileInfo: false).GetMethod()?.DeclaringType.Assembly;
			}
			return assembly;
		}

		private static string GetModName(Assembly assembly)
		{
			if (assembly == null)
			{
				return "UNKNOWN";
			}
			if (!assemblyModNames.TryGetValue(assembly, out var value))
			{
				string location = assembly.Location;
				value = Chainloader.PluginInfos.FirstOrDefault((KeyValuePair<string, PluginInfo> x) => location == x.Value.Location).Key;
				if (value == null)
				{
					((BaseUnityPlugin)instance).Logger.LogWarning((object)("The assembly " + assembly.FullName + " is not a loaded BepInEx plugin, falling back to looking for attribute in assembly"));
					try
					{
						Type[] source = default(Type[]);
						Reflection.GetTypesSafe(assembly, ref source);
						BepInPlugin val = source.Select((Type x) => ((MemberInfo)x).GetCustomAttribute<BepInPlugin>()).First((BepInPlugin x) => x != null);
						value = val.GUID;
					}
					catch
					{
						((BaseUnityPlugin)instance).Logger.LogWarning((object)"Assembly did not have a BepInPlugin attribute or couldn't load its types, falling back to assembly name");
						value = assembly.GetName().Name;
					}
				}
				assemblyModNames[assembly] = value;
			}
			return assemblyModNames[assembly];
		}
	}
}
namespace LanguageFileGenerator.Modules
{
	internal static class Language
	{
		public static Dictionary<string, string> tokensOutputs = new Dictionary<string, string>();

		public static void Add(string token, string text, string modName)
		{
			if (!tokensOutputs.ContainsKey(modName))
			{
				tokensOutputs[modName] = "";
			}
			Dictionary<string, string> dictionary = tokensOutputs;
			dictionary[modName] = dictionary[modName] + "\n        \"" + token + "\" : \"" + text.Replace(Environment.NewLine, "\\n").Replace("\n", "\\n") + "\",";
		}

		public static void PrintAll()
		{
			foreach (KeyValuePair<string, string> tokensOutput in tokensOutputs)
			{
				PrintOutput(tokensOutput.Value, tokensOutput.Key);
			}
		}

		private static void PrintOutput(string tokensOutput, string fileName = "")
		{
			string contents = "{\r\n    \"en\": {" + tokensOutput + "\r\n    }\r\n}";
			fileName += ".language";
			string text = Path.Combine(Directory.GetParent(((BaseUnityPlugin)LanguageFileGeneratorPlugin.instance).Info.Location).FullName, fileName);
			File.WriteAllText(text, contents);
			LanguageFileGeneratorPlugin.Log.LogMessage((object)("writing file to " + text));
		}
	}
}