Decompiled source of PoorlySols v1.3.0

TranslatedSols.dll

Decompiled 2 weeks ago
using System;
using System.Collections;
using System.Collections.Generic;
using System.Diagnostics;
using System.IO;
using System.Reflection;
using System.Runtime.CompilerServices;
using System.Runtime.Versioning;
using System.Text.RegularExpressions;
using BepInEx;
using BepInEx.Configuration;
using HarmonyLib;
using I2.Loc;
using Microsoft.CodeAnalysis;
using TMPro;
using UnityEngine;
using UnityEngine.SceneManagement;

[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.1", FrameworkDisplayName = ".NET Standard 2.1")]
[assembly: AssemblyCompany("TranslatedSols")]
[assembly: AssemblyConfiguration("Debug")]
[assembly: AssemblyFileVersion("1.0.0.0")]
[assembly: AssemblyInformationalVersion("1.0.0")]
[assembly: AssemblyProduct("TranslatedSols")]
[assembly: AssemblyTitle("TranslatedSols")]
[assembly: AssemblyVersion("1.0.0.0")]
namespace Microsoft.CodeAnalysis
{
	[CompilerGenerated]
	[Embedded]
	internal sealed class EmbeddedAttribute : Attribute
	{
	}
}
namespace System.Runtime.CompilerServices
{
	[CompilerGenerated]
	[Embedded]
	[AttributeUsage(AttributeTargets.Class | AttributeTargets.Property | AttributeTargets.Field | AttributeTargets.Event | AttributeTargets.Parameter | AttributeTargets.ReturnValue | AttributeTargets.GenericParameter, AllowMultiple = false, Inherited = false)]
	internal sealed class NullableAttribute : Attribute
	{
		public readonly byte[] NullableFlags;

		public NullableAttribute(byte P_0)
		{
			NullableFlags = new byte[1] { P_0 };
		}

		public NullableAttribute(byte[] P_0)
		{
			NullableFlags = P_0;
		}
	}
	[CompilerGenerated]
	[Embedded]
	[AttributeUsage(AttributeTargets.Class | AttributeTargets.Struct | AttributeTargets.Method | AttributeTargets.Interface | AttributeTargets.Delegate, AllowMultiple = false, Inherited = false)]
	internal sealed class NullableContextAttribute : Attribute
	{
		public readonly byte Flag;

		public NullableContextAttribute(byte P_0)
		{
			Flag = P_0;
		}
	}
}
namespace PoorlySols
{
	[BepInPlugin("Rekku484.PoorlySols", "PoorlySols", "1.3.0")]
	public class PoorlySols : BaseUnityPlugin
	{
		private bool translationInitialized = false;

		private Harmony harmony;

		private bool gameLevelPatched = false;

		private ConfigEntry<bool> solarianApplying;

		private Dictionary<string, string> solarianLoaded = new Dictionary<string, string>();

		public static PoorlySols Instance { get; private set; }

		private void Awake()
		{
			//IL_003f: Unknown result type (might be due to invalid IL or missing references)
			//IL_0049: Expected O, but got Unknown
			Instance = this;
			Object.DontDestroyOnLoad((Object)(object)((Component)this).gameObject);
			((BaseUnityPlugin)this).Logger.LogMessage((object)"Poor Sols are waking up");
			SetupConfig();
			((MonoBehaviour)this).StartCoroutine(WaitAndInitialize());
			harmony = new Harmony("Rekku484.PoorlySols");
			SceneManager.sceneLoaded += OnSceneLoaded;
		}

		private void SetupConfig()
		{
			solarianApplying = ((BaseUnityPlugin)this).Config.Bind<bool>("Translation", "Apply PoorlySols", true, "Check to apply Poorly Sols, uncheck to revert to Default.");
			solarianApplying.SettingChanged += delegate
			{
				poorSolsReloading();
			};
		}

		private void poorSolsReloading()
		{
			translationInitialized = false;
			if (LocalizationManager.Sources.Count == 0)
			{
				((MonoBehaviour)this).StartCoroutine(WaitAndInitialize());
				return;
			}
			initializePoorSols(LocalizationManager.Sources[0]);
			translationInitialized = true;
		}

		private IEnumerator WaitAndInitialize()
		{
			while (LocalizationManager.Sources.Count == 0)
			{
				yield return null;
			}
			if (!translationInitialized)
			{
				initializePoorSols(LocalizationManager.Sources[0]);
				translationInitialized = true;
			}
		}

		private void initializePoorSols(LanguageSourceData data)
		{
			int num = data.mLanguages.FindIndex((LanguageData x) => x.Code == "en-US");
			if (num < 0)
			{
				((BaseUnityPlugin)this).Logger.LogWarning((object)"english not real!!!!!!!!!!!!!!");
				return;
			}
			Dictionary<string, string> dictionary = Poor("English");
			foreach (TermData mTerm in data.mTerms)
			{
				if (dictionary.TryGetValue(mTerm.Term, out var value))
				{
					mTerm.Languages[num] = value;
				}
			}
			if (solarianApplying.Value)
			{
				solarianLoaded = Poor("PoorlySols");
				HashSet<string> hashSet = new HashSet<string>();
				foreach (TermData mTerm2 in data.mTerms)
				{
					if (solarianLoaded.TryGetValue(mTerm2.Term, out string value2))
					{
						mTerm2.Languages[num] = value2;
						string text = (mTerm2.Term.Contains("/") ? mTerm2.Term.Split('/')[0] : mTerm2.Term);
						if (hashSet.Add(text))
						{
							((BaseUnityPlugin)this).Logger.LogInfo((object)("\"" + text + "\" is now translated by the Poor Sols"));
						}
					}
				}
			}
			LocalizationManager.LocalizeAll(true);
			((BaseUnityPlugin)this).Logger.LogMessage((object)(solarianApplying.Value ? "The Poor Sols has applied the PoorlySols translation" : "The Poor Sols has applied the Official English Language"));
		}

		private string poorSolsFindTranslation()
		{
			string[] directories = Directory.GetDirectories(Paths.PluginPath, "*PoorlySols");
			return (directories.Length != 0) ? directories[0] : Paths.PluginPath;
		}

		private Dictionary<string, string> Poor(string fileName)
		{
			string text = Path.Combine(poorSolsFindTranslation(), fileName + ".json");
			if (!File.Exists(text))
			{
				((BaseUnityPlugin)this).Logger.LogWarning((object)("The Poor Sols didn't find the file: " + text));
				return new Dictionary<string, string>();
			}
			try
			{
				Dictionary<string, string> dictionary = new Dictionary<string, string>();
				string text2 = File.ReadAllText(text);
				text2 = text2.Trim().TrimStart('{').TrimEnd('}');
				string[] array = text2.Split('\n');
				foreach (string text3 in array)
				{
					string text4 = text3.Trim().TrimEnd(',');
					if (text4.StartsWith("\""))
					{
						int num = text4.IndexOf("\": \"");
						if (num >= 0)
						{
							string str = text4.Substring(1, num - 1);
							string text5 = text4;
							int num2 = num + 4;
							string str2 = text5.Substring(num2, text5.Length - num2).TrimEnd('"');
							str = Regex.Unescape(str);
							str2 = Regex.Unescape(str2);
							dictionary[str] = str2;
						}
					}
				}
				return dictionary;
			}
			catch (Exception ex)
			{
				((BaseUnityPlugin)this).Logger.LogError((object)("Failed to parse " + fileName + ".json: " + ex.Message));
				return new Dictionary<string, string>();
			}
		}

		private void OnSceneLoaded(Scene scene, LoadSceneMode mode)
		{
			//IL_008d: Unknown result type (might be due to invalid IL or missing references)
			//IL_009a: Expected O, but got Unknown
			if (gameLevelPatched)
			{
				return;
			}
			Type type = AccessTools.TypeByName("GameLevel");
			if (type == null)
			{
				((BaseUnityPlugin)this).Logger.LogWarning((object)("GameLevel not found in scene: " + ((Scene)(ref scene)).name));
				return;
			}
			MethodInfo methodInfo = AccessTools.Method(type, "Awake", (Type[])null, (Type[])null);
			if (methodInfo == null)
			{
				((BaseUnityPlugin)this).Logger.LogWarning((object)"GameLevel.Awake method not found");
				return;
			}
			harmony.Patch((MethodBase)methodInfo, (HarmonyMethod)null, new HarmonyMethod(typeof(PoorlySols), "Patch_GameLevel_Awake", (Type[])null), (HarmonyMethod)null, (HarmonyMethod)null, (HarmonyMethod)null);
			gameLevelPatched = true;
			((BaseUnityPlugin)this).Logger.LogInfo((object)("GameLevel.Awake patched on scene: " + ((Scene)(ref scene)).name));
		}

		private static void Patch_GameLevel_Awake(Component __instance)
		{
			if ((Object)(object)Instance != (Object)null)
			{
				((MonoBehaviour)Instance).StartCoroutine(Instance.ReplaceBOssNames(__instance.gameObject));
			}
			else
			{
				Debug.LogWarning((object)"[PoorlySols] Instance is null in Patch_GameLevel_Awake");
			}
		}

		private IEnumerator ReplaceBOssNames(GameObject go)
		{
			float timeout = Time.time + 5f;
			List<Transform> bossIntros = new List<Transform>();
			while (bossIntros.Count == 0 && Time.time < timeout)
			{
				Transform[] componentsInChildren = go.GetComponentsInChildren<Transform>(true);
				foreach (Transform t in componentsInChildren)
				{
					if (((Object)t).name.Contains("Boss Intro Acting"))
					{
						bossIntros.Add(t);
					}
				}
				if (bossIntros.Count == 0)
				{
					yield return (object)new WaitForSeconds(1f);
				}
			}
			if (bossIntros.Count == 0)
			{
				((BaseUnityPlugin)this).Logger.LogWarning((object)"No Boss Intro Acting Found");
				yield break;
			}
			foreach (Transform intro in bossIntros)
			{
				Transform view = intro.Find("View");
				if ((Object)(object)view == (Object)null)
				{
					continue;
				}
				for (int j = 0; j < view.childCount; j++)
				{
					Transform child = view.GetChild(j);
					Transform titleTransform = child.Find("Subtitle_Text/Title");
					if (!((Object)(object)titleTransform == (Object)null))
					{
						if (TryGetBossName(((Object)child).name, out string translatedName))
						{
							((Component)titleTransform).GetComponent<TMP_Text>().text = translatedName.ToUpper();
							((BaseUnityPlugin)this).Logger.LogInfo((object)("Boss name replaced: " + ((Object)child).name + " -> " + translatedName));
						}
						else if (((Object)child).name != "Intro Template")
						{
							((BaseUnityPlugin)this).Logger.LogWarning((object)("No translation found for: " + ((Object)child).name));
						}
						yield return null;
						translatedName = null;
					}
				}
			}
		}

		private static bool TryGetBossName(string bossObjectName, out string name)
		{
			switch (bossObjectName)
			{
			case "Boss_SpearHorseMan":
				name = "END OF SERVICE";
				return true;
			case "Boss_MrX":
				name = "COMPUTER";
				return true;
			case "XingTen":
				name = "FOR MORE INFORMATION, CLICK HERE";
				return true;
			case "XingTen_HeadLess":
				name = "FOR MORE INFORMATION, CLICK HERE";
				return true;
			case "Boss_GouMang":
				name = "RED LIGHT, RED LIGHT, RED LIGHT";
				return true;
			case "Boss_DaoChang":
				name = "I LIKE OLD THINGS";
				return true;
			case "Boss_JeiChuan":
				name = "THIS IS CHECHNYA";
				return true;
			case "Boss_Ji":
				name = "WANG ZIYE";
				return true;
			case "Boss_FuDie":
				name = "RED COW";
				return true;
			case "Boss_YiGung":
				name = "HELLO";
				return true;
			case "Boss_FuXi NuWa":
				name = "AFRICAN-AMERICANS LIVING IN NEW YORK, OTHER WORKS ARE IN PERSIAN";
				return true;
			case "Boss_Skyrending Claw":
				name = "PRODUCT QUALITY IS THE MOST IMPORTANT THING";
				return true;
			default:
				name = "";
				return false;
			}
		}
	}
}