Decompiled source of CleanText v1.0.0

CleanText.dll

Decompiled 19 hours 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.Text.Json;
using BepInEx;
using BepInEx.Configuration;
using BepInEx.Core.Logging.Interpolation;
using BepInEx.Logging;
using BepInEx.Unity.IL2CPP;
using HarmonyLib;
using Il2CppInterop.Runtime.InteropTypes.Arrays;
using Microsoft.CodeAnalysis;
using UnityEngine;
using UnityEngine.Localization;
using UnityEngine.Localization.Settings;
using UnityEngine.Localization.Tables;

[assembly: CompilationRelaxations(8)]
[assembly: RuntimeCompatibility(WrapNonExceptionThrows = true)]
[assembly: Debuggable(DebuggableAttribute.DebuggingModes.IgnoreSymbolStoreSequencePoints)]
[assembly: TargetFramework(".NETCoreApp,Version=v6.0", FrameworkDisplayName = ".NET 6.0")]
[assembly: AssemblyCompany("CleanText")]
[assembly: AssemblyConfiguration("Release")]
[assembly: AssemblyFileVersion("1.0.0.0")]
[assembly: AssemblyInformationalVersion("1.0.0")]
[assembly: AssemblyProduct("CleanText")]
[assembly: AssemblyTitle("CleanText")]
[assembly: AssemblyVersion("1.0.0.0")]
[module: RefSafetyRules(11)]
namespace Microsoft.CodeAnalysis
{
	[CompilerGenerated]
	[Embedded]
	internal sealed class EmbeddedAttribute : Attribute
	{
	}
}
namespace System.Runtime.CompilerServices
{
	[CompilerGenerated]
	[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 CleanText
{
	[BepInPlugin("pizzaman.cleantext", "Clean Text", "1.0.0")]
	public class Plugin : BasePlugin
	{
		public const string Guid = "pizzaman.cleantext";

		internal static ManualLogSource Logger;

		internal static ConfigEntry<bool> CleanLanguage;

		internal static ConfigEntry<bool> PlainText;

		internal static ConfigEntry<bool> PlainNames;

		public override void Load()
		{
			//IL_008f: Unknown result type (might be due to invalid IL or missing references)
			//IL_0095: Expected O, but got Unknown
			Logger = ((BasePlugin)this).Log;
			CleanLanguage = ((BasePlugin)this).Config.Bind<bool>("Text", "CleanLanguage", true, "Replace swearing and crude jokes (the F-word, 'smells like shit', 'Slutty Cannon', 'Scorpionussy'...).");
			PlainText = ((BasePlugin)this).Config.Bind<bool>("Text", "PlainText", true, "Rewrite meme-speak lines in plain words ('you ded / maybe him skill issue?', 'You're a smol boi with big dreams', 'chat im locking in'...).");
			PlainNames = ((BasePlugin)this).Config.Bind<bool>("Text", "PlainNames", true, "Give meme names plain ones (Amog -> Hazmat, Megachad -> Strongman, Stonks -> Gold Boost, Borgar -> Burger...).");
			Replacements.Load();
			Harmony.CreateAndPatchAll(typeof(Patches), "pizzaman.cleantext");
			ManualLogSource logger = Logger;
			bool flag = default(bool);
			BepInExInfoLogInterpolatedStringHandler val = new BepInExInfoLogInterpolatedStringHandler(43, 4, ref flag);
			if (flag)
			{
				((BepInExLogInterpolatedStringHandler)val).AppendLiteral("Loaded (");
				((BepInExLogInterpolatedStringHandler)val).AppendFormatted<int>(Replacements.Lines.Count);
				((BepInExLogInterpolatedStringHandler)val).AppendLiteral(" lines known; clean=");
				((BepInExLogInterpolatedStringHandler)val).AppendFormatted<bool>(CleanLanguage.Value);
				((BepInExLogInterpolatedStringHandler)val).AppendLiteral(" plain=");
				((BepInExLogInterpolatedStringHandler)val).AppendFormatted<bool>(PlainText.Value);
				((BepInExLogInterpolatedStringHandler)val).AppendLiteral(" names=");
				((BepInExLogInterpolatedStringHandler)val).AppendFormatted<bool>(PlainNames.Value);
				((BepInExLogInterpolatedStringHandler)val).AppendLiteral(")");
			}
			logger.LogInfo(val);
		}
	}
	internal sealed class Line
	{
		public string Table = "";

		public string Key = "";

		public string Original = "";

		public Dictionary<string, string> ByGroup = new Dictionary<string, string>();
	}
	internal static class Replacements
	{
		public static readonly List<Line> Lines = new List<Line>();

		public static List<string> Priority = new List<string>();

		public static HashSet<string> FunfactsDrop = new HashSet<string>();

		public static void Load()
		{
			using Stream utf8Json = typeof(Replacements).Assembly.GetManifestResourceStream("replacements.json");
			using JsonDocument jsonDocument = JsonDocument.Parse(utf8Json);
			JsonElement rootElement = jsonDocument.RootElement;
			Priority = (from e in rootElement.GetProperty("priority").EnumerateArray()
				select e.GetString()).ToList();
			FunfactsDrop = (from e in rootElement.GetProperty("funfactsDrop").EnumerateArray()
				select e.GetString()).ToHashSet();
			foreach (JsonElement item in rootElement.GetProperty("lines").EnumerateArray())
			{
				Line line = new Line
				{
					Table = item.GetProperty("table").GetString(),
					Key = item.GetProperty("key").GetString(),
					Original = item.GetProperty("original").GetString()
				};
				foreach (string item2 in Priority)
				{
					if (item.TryGetProperty(item2, out var value))
					{
						line.ByGroup[item2] = value.GetString();
					}
				}
				Lines.Add(line);
			}
		}

		private static bool Enabled(string group)
		{
			return group switch
			{
				"clean" => Plugin.CleanLanguage.Value, 
				"plain" => Plugin.PlainText.Value, 
				"names" => Plugin.PlainNames.Value, 
				_ => false, 
			};
		}

		public static string Target(Line line)
		{
			foreach (string item in Priority)
			{
				if (Enabled(item) && line.ByGroup.TryGetValue(item, out var value))
				{
					return value;
				}
			}
			return null;
		}

		private static string Norm(string s)
		{
			return s.Replace("\r\n", "\n").Trim();
		}

		private static bool IsEnglish(Locale locale)
		{
			if ((Object)(object)locale != (Object)null)
			{
				return (locale.Identifier.Code ?? "").StartsWith("en", StringComparison.OrdinalIgnoreCase);
			}
			return false;
		}

		public static void Apply(string why)
		{
			//IL_0018: Unknown result type (might be due to invalid IL or missing references)
			//IL_001f: Expected O, but got Unknown
			//IL_00e0: Unknown result type (might be due to invalid IL or missing references)
			//IL_00e7: Expected O, but got Unknown
			//IL_0333: Unknown result type (might be due to invalid IL or missing references)
			//IL_033a: Expected O, but got Unknown
			//IL_0176: Unknown result type (might be due to invalid IL or missing references)
			//IL_017d: Expected O, but got Unknown
			//IL_0270: Unknown result type (might be due to invalid IL or missing references)
			//IL_0277: Expected O, but got Unknown
			Locale selectedLocale = LocalizationSettings.SelectedLocale;
			bool flag = default(bool);
			BepInExInfoLogInterpolatedStringHandler val;
			if (!IsEnglish(selectedLocale))
			{
				ManualLogSource logger = Plugin.Logger;
				val = new BepInExInfoLogInterpolatedStringHandler(48, 2, ref flag);
				if (flag)
				{
					((BepInExLogInterpolatedStringHandler)val).AppendFormatted<string>(why);
					((BepInExLogInterpolatedStringHandler)val).AppendLiteral(": language is ");
					((BepInExLogInterpolatedStringHandler)val).AppendFormatted<string>(((selectedLocale != null) ? selectedLocale.Identifier.Code : null) ?? "not set");
					((BepInExLogInterpolatedStringHandler)val).AppendLiteral(", English text only, nothing to do");
				}
				logger.LogInfo(val);
				return;
			}
			LocalizedStringDatabase stringDatabase = LocalizationSettings.StringDatabase;
			int num = 0;
			int num2 = 0;
			int num3 = 0;
			foreach (IGrouping<string, Line> item in from l in Lines
				group l by l.Table)
			{
				StringTable table = ((LocalizedDatabase<StringTable, StringTableEntry>)(object)stringDatabase).GetTable(TableReference.op_Implicit(item.Key), selectedLocale);
				if ((Object)(object)table == (Object)null)
				{
					ManualLogSource logger2 = Plugin.Logger;
					BepInExWarningLogInterpolatedStringHandler val2 = new BepInExWarningLogInterpolatedStringHandler(20, 2, ref flag);
					if (flag)
					{
						((BepInExLogInterpolatedStringHandler)val2).AppendFormatted<string>(why);
						((BepInExLogInterpolatedStringHandler)val2).AppendLiteral(": table '");
						((BepInExLogInterpolatedStringHandler)val2).AppendFormatted<string>(item.Key);
						((BepInExLogInterpolatedStringHandler)val2).AppendLiteral("' not found");
					}
					logger2.LogWarning(val2);
					num3 += item.Count();
					continue;
				}
				foreach (Line item2 in item)
				{
					StringTableEntry entry = ((DetailedLocalizationTable<StringTableEntry>)(object)table).GetEntry(item2.Key);
					string text = Target(item2);
					if (entry == null)
					{
						ManualLogSource logger3 = Plugin.Logger;
						BepInExWarningLogInterpolatedStringHandler val2 = new BepInExWarningLogInterpolatedStringHandler(28, 3, ref flag);
						if (flag)
						{
							((BepInExLogInterpolatedStringHandler)val2).AppendFormatted<string>(why);
							((BepInExLogInterpolatedStringHandler)val2).AppendLiteral(": ");
							((BepInExLogInterpolatedStringHandler)val2).AppendFormatted<string>(item2.Table);
							((BepInExLogInterpolatedStringHandler)val2).AppendLiteral("/");
							((BepInExLogInterpolatedStringHandler)val2).AppendFormatted<string>(item2.Key);
							((BepInExLogInterpolatedStringHandler)val2).AppendLiteral(" not in the game any more");
						}
						logger3.LogWarning(val2);
						num3++;
						continue;
					}
					string current3 = entry.Value ?? "";
					string text2 = text ?? item2.Original;
					if (Norm(current3) == Norm(text2))
					{
						num2++;
					}
					else if (!(Norm(current3) == Norm(item2.Original)) && !item2.ByGroup.Values.Any((string t) => Norm(t) == Norm(current3)))
					{
						ManualLogSource logger4 = Plugin.Logger;
						BepInExWarningLogInterpolatedStringHandler val2 = new BepInExWarningLogInterpolatedStringHandler(30, 4, ref flag);
						if (flag)
						{
							((BepInExLogInterpolatedStringHandler)val2).AppendFormatted<string>(why);
							((BepInExLogInterpolatedStringHandler)val2).AppendLiteral(": ");
							((BepInExLogInterpolatedStringHandler)val2).AppendFormatted<string>(item2.Table);
							((BepInExLogInterpolatedStringHandler)val2).AppendLiteral("/");
							((BepInExLogInterpolatedStringHandler)val2).AppendFormatted<string>(item2.Key);
							((BepInExLogInterpolatedStringHandler)val2).AppendLiteral(" has new text, left as is: ");
							((BepInExLogInterpolatedStringHandler)val2).AppendFormatted<string>(current3);
						}
						logger4.LogWarning(val2);
						num3++;
					}
					else
					{
						entry.Value = text2;
						entry.FormatCache = null;
						num++;
					}
				}
			}
			ManualLogSource logger5 = Plugin.Logger;
			val = new BepInExInfoLogInterpolatedStringHandler(42, 4, ref flag);
			if (flag)
			{
				((BepInExLogInterpolatedStringHandler)val).AppendFormatted<string>(why);
				((BepInExLogInterpolatedStringHandler)val).AppendLiteral(": ");
				((BepInExLogInterpolatedStringHandler)val).AppendFormatted<int>(num);
				((BepInExLogInterpolatedStringHandler)val).AppendLiteral(" lines changed, ");
				((BepInExLogInterpolatedStringHandler)val).AppendFormatted<int>(num2);
				((BepInExLogInterpolatedStringHandler)val).AppendLiteral(" already right, ");
				((BepInExLogInterpolatedStringHandler)val).AppendFormatted<int>(num3);
				((BepInExLogInterpolatedStringHandler)val).AppendLiteral(" skipped");
			}
			logger5.LogInfo(val);
		}
	}
	internal static class Patches
	{
		[HarmonyPostfix]
		[HarmonyPatch(typeof(LocalizationPreloader), "LoadMain")]
		private static void AfterPreload()
		{
			Run("tables preloaded");
		}

		[HarmonyPostfix]
		[HarmonyPatch(typeof(LocalizationPrewarmer), "OnLocaleChanged")]
		private static void AfterLocaleChanged()
		{
			Run("language changed");
		}

		[HarmonyPrefix]
		[HarmonyPatch(typeof(Funfact), "Start")]
		private static void BeforeFunfact(Funfact __instance)
		{
			//IL_006d: Unknown result type (might be due to invalid IL or missing references)
			//IL_0073: Expected O, but got Unknown
			//IL_0056: Unknown result type (might be due to invalid IL or missing references)
			//IL_0060: Expected O, but got Unknown
			if (!Plugin.PlainText.Value)
			{
				return;
			}
			try
			{
				Il2CppStringArray facts = __instance.facts;
				if (facts != null)
				{
					string[] array = ((IEnumerable<string>)facts).Where((string f) => !Replacements.FunfactsDrop.Contains(f)).ToArray();
					if (array.Length != ((Il2CppArrayBase<string>)(object)facts).Length && array.Length != 0)
					{
						__instance.facts = new Il2CppStringArray(array);
					}
				}
			}
			catch (Exception ex)
			{
				ManualLogSource logger = Plugin.Logger;
				bool flag = default(bool);
				BepInExErrorLogInterpolatedStringHandler val = new BepInExErrorLogInterpolatedStringHandler(23, 1, ref flag);
				if (flag)
				{
					((BepInExLogInterpolatedStringHandler)val).AppendLiteral("Funfact filter failed: ");
					((BepInExLogInterpolatedStringHandler)val).AppendFormatted<Exception>(ex);
				}
				logger.LogError(val);
			}
		}

		private static void Run(string why)
		{
			//IL_0013: Unknown result type (might be due to invalid IL or missing references)
			//IL_0019: Expected O, but got Unknown
			try
			{
				Replacements.Apply(why);
			}
			catch (Exception ex)
			{
				ManualLogSource logger = Plugin.Logger;
				bool flag = default(bool);
				BepInExErrorLogInterpolatedStringHandler val = new BepInExErrorLogInterpolatedStringHandler(29, 2, ref flag);
				if (flag)
				{
					((BepInExLogInterpolatedStringHandler)val).AppendFormatted<string>(why);
					((BepInExLogInterpolatedStringHandler)val).AppendLiteral(": could not change the text: ");
					((BepInExLogInterpolatedStringHandler)val).AppendFormatted<Exception>(ex);
				}
				logger.LogError(val);
			}
		}
	}
}