Decompiled source of DocumentTextFix v2.6.1

LittleMarkoG.DocumentTextFix.dll

Decompiled a week ago
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Linq;
using System.Reflection;
using System.Runtime.CompilerServices;
using System.Runtime.Versioning;
using BepInEx;
using BepInEx.Core.Logging.Interpolation;
using BepInEx.Logging;
using BepInEx.Unity.IL2CPP;
using HarmonyLib;
using Il2CppSystem;
using Il2CppSystem.Collections.Generic;
using Microsoft.CodeAnalysis;
using TMPro;
using UnityEngine;
using UnityEngine.UI;

[assembly: CompilationRelaxations(8)]
[assembly: RuntimeCompatibility(WrapNonExceptionThrows = true)]
[assembly: Debuggable(DebuggableAttribute.DebuggingModes.IgnoreSymbolStoreSequencePoints)]
[assembly: TargetFramework(".NETCoreApp,Version=v6.0", FrameworkDisplayName = ".NET 6.0")]
[assembly: AssemblyCompany("LittleMarkoG.DocumentTextFix")]
[assembly: AssemblyConfiguration("Release")]
[assembly: AssemblyFileVersion("1.0.0.0")]
[assembly: AssemblyInformationalVersion("1.0.0")]
[assembly: AssemblyProduct("LittleMarkoG.DocumentTextFix")]
[assembly: AssemblyTitle("LittleMarkoG.DocumentTextFix")]
[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.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;
		}
	}
	[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 LittleMarkoG.DocumentTextFix
{
	[BepInPlugin("codex.sod.documenttextrepair", "Document Text Fix", "2.6.1")]
	public sealed class Plugin : BasePlugin
	{
		internal static ManualLogSource? Logger;

		public override void Load()
		{
			//IL_0010: Unknown result type (might be due to invalid IL or missing references)
			Logger = ((BasePlugin)this).Log;
			new Harmony("codex.sod.documenttextrepair").PatchAll(typeof(DocumentPatches));
			((BasePlugin)this).Log.LogInfo((object)"Document text repair active.");
		}
	}
	internal static class DocumentPatches
	{
		private sealed class PendingRefresh
		{
			public WindowContentController Content;

			public int InstanceId;

			public int DueFrame;

			public int Attempts;
		}

		private static readonly List<PendingRefresh> Pending = new List<PendingRefresh>();

		private static readonly HashSet<string> ReportedErrors = new HashSet<string>();

		private static bool refreshing;

		private static void Queue(WindowContentController? content)
		{
			if (refreshing || (Object)(object)content == (Object)null)
			{
				return;
			}
			try
			{
				int id = ((Object)content).GetInstanceID();
				PendingRefresh pendingRefresh = Pending.Find((PendingRefresh job) => job.InstanceId == id);
				if (pendingRefresh != null)
				{
					pendingRefresh.DueFrame = Time.frameCount + 2;
					pendingRefresh.Attempts = 0;
					return;
				}
				Pending.Add(new PendingRefresh
				{
					Content = content,
					InstanceId = id,
					DueFrame = Time.frameCount + 2
				});
			}
			catch
			{
			}
		}

		[HarmonyPatch(typeof(WindowContentController), "LoadContent")]
		[HarmonyPostfix]
		private static void AfterLoadContent(WindowContentController __instance)
		{
			HideFallback(__instance);
			Queue(__instance);
		}

		[HarmonyPatch(typeof(WindowContentController), "SetPage")]
		[HarmonyPostfix]
		private static void AfterSetPage(WindowContentController __instance)
		{
			HideFallback(__instance);
			Queue(__instance);
		}

		[HarmonyPatch(typeof(AddressBookController), "OnEnable")]
		[HarmonyPostfix]
		private static void AfterAddressBookEnabled(AddressBookController __instance)
		{
			try
			{
				Queue(__instance.windowContent);
			}
			catch
			{
			}
		}

		[HarmonyPatch(typeof(CityDirectoryController), "OnEnable")]
		[HarmonyPostfix]
		private static void AfterCityDirectoryEnabled(CityDirectoryController __instance)
		{
			try
			{
				Queue(__instance.windowContent);
			}
			catch
			{
			}
		}

		[HarmonyPatch(typeof(TextToImageController), "CaptureTextToImage")]
		[HarmonyPrefix]
		private static void BeforeTextCapture(TextToImageController __instance, TextToImageSettings settings)
		{
			try
			{
				if (settings != null && !string.IsNullOrWhiteSpace(settings.textString))
				{
					TextMeshProUGUI captureText = __instance.captureText;
					if (captureText != null)
					{
						((TMP_Text)captureText).ForceMeshUpdate(true, true);
					}
					Canvas.ForceUpdateCanvases();
				}
			}
			catch
			{
			}
		}

		[HarmonyPatch(typeof(InterfaceController), "Update")]
		[HarmonyPostfix]
		private static void AfterInterfaceUpdate()
		{
			//IL_0124: Unknown result type (might be due to invalid IL or missing references)
			//IL_012b: Expected O, but got Unknown
			int frameCount = Time.frameCount;
			bool flag = default(bool);
			for (int num = Pending.Count - 1; num >= 0; num--)
			{
				PendingRefresh pendingRefresh = Pending[num];
				if (frameCount >= pendingRefresh.DueFrame)
				{
					try
					{
						WindowContentController content = pendingRefresh.Content;
						if ((Object)(object)content == (Object)null || !((Behaviour)content).isActiveAndEnabled || HasPageText(content))
						{
							Pending.RemoveAt(num);
						}
						else
						{
							refreshing = true;
							if (pendingRefresh.Attempts == 0)
							{
								try
								{
									content.UpdateNoteText();
								}
								catch
								{
								}
								try
								{
									PageBasedContent pageBasedContent = content.pageBasedContent;
									if (pageBasedContent != null)
									{
										pageBasedContent.UpdateListDisplay();
									}
								}
								catch
								{
								}
								Canvas.ForceUpdateCanvases();
								ForcePageMeshes(content);
							}
							else
							{
								ManualLogSource? logger = Plugin.Logger;
								if (logger != null)
								{
									logger.LogInfo((object)"Rebuilding a blank document page once.");
								}
								content.LoadContent();
								Canvas.ForceUpdateCanvases();
								ForcePageMeshes(content);
							}
							if (pendingRefresh.Attempts >= 1 && !HasPageText(content))
							{
								DiagnoseAndRecover(content);
							}
							if (pendingRefresh.Attempts >= 1 || HasPageText(content))
							{
								Pending.RemoveAt(num);
							}
							else
							{
								pendingRefresh.Attempts++;
								pendingRefresh.DueFrame = frameCount + 3;
							}
						}
					}
					catch (Exception ex)
					{
						ManualLogSource logger2 = Plugin.Logger;
						if (logger2 != null)
						{
							BepInExErrorLogInterpolatedStringHandler val = new BepInExErrorLogInterpolatedStringHandler(40, 2, ref flag);
							if (flag)
							{
								((BepInExLogInterpolatedStringHandler)val).AppendLiteral("DOCUMENT_TEXT_REPAIR_EXCEPTION window=");
								((BepInExLogInterpolatedStringHandler)val).AppendFormatted<int>(pendingRefresh.InstanceId);
								((BepInExLogInterpolatedStringHandler)val).AppendLiteral(": ");
								((BepInExLogInterpolatedStringHandler)val).AppendFormatted<Exception>(ex);
							}
							logger2.LogError(val);
						}
						Pending.RemoveAt(num);
					}
					finally
					{
						refreshing = false;
					}
				}
			}
		}

		private static bool HasPageText(WindowContentController content)
		{
			RectTransform pageRect = content.pageRect;
			if ((Object)(object)pageRect == (Object)null)
			{
				return true;
			}
			int num = 0;
			foreach (TextMeshProUGUI componentsInChild in ((Component)pageRect).GetComponentsInChildren<TextMeshProUGUI>(true))
			{
				if (!((Object)(object)componentsInChild == (Object)null) && ((Component)componentsInChild).gameObject.activeInHierarchy)
				{
					num += ((TMP_Text)componentsInChild).text?.Count((char ch) => !char.IsWhiteSpace(ch)) ?? 0;
					if (num >= 4)
					{
						return true;
					}
				}
			}
			return false;
		}

		private static void ForcePageMeshes(WindowContentController content)
		{
			RectTransform pageRect = content.pageRect;
			if ((Object)(object)pageRect == (Object)null)
			{
				return;
			}
			foreach (TextMeshProUGUI componentsInChild in ((Component)pageRect).GetComponentsInChildren<TextMeshProUGUI>(true))
			{
				if ((Object)(object)componentsInChild != (Object)null && !string.IsNullOrWhiteSpace(((TMP_Text)componentsInChild).text))
				{
					((TMP_Text)componentsInChild).ForceMeshUpdate(true, true);
				}
			}
		}

		private static void DiagnoseAndRecover(WindowContentController content)
		{
			//IL_01a6: Unknown result type (might be due to invalid IL or missing references)
			//IL_01ad: Expected O, but got Unknown
			//IL_033f: Unknown result type (might be due to invalid IL or missing references)
			//IL_0346: Expected O, but got Unknown
			//IL_03e5: Unknown result type (might be due to invalid IL or missing references)
			//IL_03ec: Expected O, but got Unknown
			//IL_04ec: Unknown result type (might be due to invalid IL or missing references)
			//IL_04f3: Expected O, but got Unknown
			//IL_0818: Unknown result type (might be due to invalid IL or missing references)
			//IL_081f: Expected O, but got Unknown
			//IL_0684: Unknown result type (might be due to invalid IL or missing references)
			//IL_068b: Expected O, but got Unknown
			//IL_0577: Unknown result type (might be due to invalid IL or missing references)
			//IL_057e: Expected O, but got Unknown
			//IL_07ec: Unknown result type (might be due to invalid IL or missing references)
			//IL_07f2: Unknown result type (might be due to invalid IL or missing references)
			//IL_029c: Unknown result type (might be due to invalid IL or missing references)
			//IL_02a3: Expected O, but got Unknown
			//IL_0133: Unknown result type (might be due to invalid IL or missing references)
			//IL_013a: Expected O, but got Unknown
			InfoWindow window = content.window;
			Interactable val = ((window != null) ? window.passedInteractable : null);
			Evidence val2 = ((window != null) ? window.passedEvidence : null);
			DDSTreeSave val3 = content.content;
			string text = FirstValue((val != null) ? val.dds : null, (val2 != null) ? val2.overrideDDS : null, (val3 != null) ? ((DDSComponent)val3).id : null);
			object obj;
			if (window == null)
			{
				obj = null;
			}
			else
			{
				TextMeshProUGUI titleText = window.titleText;
				obj = ((titleText != null) ? ((TMP_Text)titleText).text : null);
			}
			if (obj == null)
			{
				obj = "(untitled)";
			}
			string text2 = (string)obj;
			string text3 = ((val2 != null) ? val2.evID : null) ?? "(none)";
			string text4 = ((val == null) ? "(none)" : val.id.ToString());
			Toolbox instance = Toolbox.Instance;
			bool flag = default(bool);
			if (val3 == null && !string.IsNullOrWhiteSpace(text) && ((instance != null) ? instance.allDDSTrees : null) != null && instance.allDDSTrees.ContainsKey(text))
			{
				try
				{
					content.content = instance.allDDSTrees[text];
					content.LoadContent();
					Canvas.ForceUpdateCanvases();
					ForcePageMeshes(content);
					if (HasPageText(content))
					{
						ManualLogSource logger = Plugin.Logger;
						if (logger != null)
						{
							BepInExInfoLogInterpolatedStringHandler val4 = new BepInExInfoLogInterpolatedStringHandler(45, 3, ref flag);
							if (flag)
							{
								((BepInExLogInterpolatedStringHandler)val4).AppendLiteral("RECOVERED_TREE_BINDING title=");
								((BepInExLogInterpolatedStringHandler)val4).AppendFormatted<string>(text2);
								((BepInExLogInterpolatedStringHandler)val4).AppendLiteral(" tree=");
								((BepInExLogInterpolatedStringHandler)val4).AppendFormatted<string>(text);
								((BepInExLogInterpolatedStringHandler)val4).AppendLiteral(" evidence=");
								((BepInExLogInterpolatedStringHandler)val4).AppendFormatted<string>(text3);
							}
							logger.LogInfo(val4);
						}
						return;
					}
					val3 = content.content;
				}
				catch (Exception ex)
				{
					ManualLogSource logger = Plugin.Logger;
					if (logger != null)
					{
						BepInExWarningLogInterpolatedStringHandler val5 = new BepInExWarningLogInterpolatedStringHandler(26, 2, ref flag);
						if (flag)
						{
							((BepInExLogInterpolatedStringHandler)val5).AppendLiteral("TREE_REBIND_FAILED tree=");
							((BepInExLogInterpolatedStringHandler)val5).AppendFormatted<string>(text);
							((BepInExLogInterpolatedStringHandler)val5).AppendLiteral(": ");
							((BepInExLogInterpolatedStringHandler)val5).AppendFormatted<string>(ex.Message);
						}
						logger.LogWarning(val5);
					}
				}
			}
			if (val3 == null && TryFindTreeByName(instance, val, val2, out DDSTreeSave match))
			{
				string dds = ((val != null) ? val.dds : null) ?? "";
				string text5 = ((val2 != null) ? val2.overrideDDS : null) ?? "";
				try
				{
					if (val != null)
					{
						val.dds = ((DDSComponent)match).id;
					}
					if (val2 != null && !string.IsNullOrWhiteSpace(text5))
					{
						val2.overrideDDS = ((DDSComponent)match).id;
					}
					content.content = match;
					content.LoadContent();
					Canvas.ForceUpdateCanvases();
					ForcePageMeshes(content);
					val3 = content.content ?? match;
					if (HasPageText(content))
					{
						ManualLogSource logger = Plugin.Logger;
						if (logger != null)
						{
							BepInExInfoLogInterpolatedStringHandler val4 = new BepInExInfoLogInterpolatedStringHandler(67, 5, ref flag);
							if (flag)
							{
								((BepInExLogInterpolatedStringHandler)val4).AppendLiteral("RECOVERED_TREE_BY_NAME title=");
								((BepInExLogInterpolatedStringHandler)val4).AppendFormatted<string>(text2);
								((BepInExLogInterpolatedStringHandler)val4).AppendLiteral(" oldTree=");
								((BepInExLogInterpolatedStringHandler)val4).AppendFormatted<string>(text);
								((BepInExLogInterpolatedStringHandler)val4).AppendLiteral(" matchedTree=");
								((BepInExLogInterpolatedStringHandler)val4).AppendFormatted<string>(((DDSComponent)match).id);
								((BepInExLogInterpolatedStringHandler)val4).AppendLiteral(" name=");
								((BepInExLogInterpolatedStringHandler)val4).AppendFormatted<string>(((DDSComponent)match).name);
								((BepInExLogInterpolatedStringHandler)val4).AppendLiteral(" evidence=");
								((BepInExLogInterpolatedStringHandler)val4).AppendFormatted<string>(text3);
							}
							logger.LogInfo(val4);
						}
						return;
					}
				}
				catch (Exception ex2)
				{
					ManualLogSource logger = Plugin.Logger;
					if (logger != null)
					{
						BepInExWarningLogInterpolatedStringHandler val5 = new BepInExWarningLogInterpolatedStringHandler(49, 3, ref flag);
						if (flag)
						{
							((BepInExLogInterpolatedStringHandler)val5).AppendLiteral("TREE_NAME_RECOVERY_FAILED oldTree=");
							((BepInExLogInterpolatedStringHandler)val5).AppendFormatted<string>(text);
							((BepInExLogInterpolatedStringHandler)val5).AppendLiteral(" matchedTree=");
							((BepInExLogInterpolatedStringHandler)val5).AppendFormatted<string>(((DDSComponent)match).id);
							((BepInExLogInterpolatedStringHandler)val5).AppendLiteral(": ");
							((BepInExLogInterpolatedStringHandler)val5).AppendFormatted<string>(ex2.Message);
						}
						logger.LogWarning(val5);
					}
				}
				finally
				{
					if (val != null)
					{
						val.dds = dds;
					}
					if (val2 != null)
					{
						val2.overrideDDS = text5;
					}
				}
			}
			string text6 = "";
			if (val != null)
			{
				try
				{
					text6 = Strings.GetMainTextFromInteractable(val, (LinkSetting)0) ?? "";
				}
				catch (Exception ex3)
				{
					ManualLogSource logger = Plugin.Logger;
					if (logger != null)
					{
						BepInExWarningLogInterpolatedStringHandler val5 = new BepInExWarningLogInterpolatedStringHandler(28, 2, ref flag);
						if (flag)
						{
							((BepInExLogInterpolatedStringHandler)val5).AppendLiteral("SOURCE_LOOKUP_FAILED item=");
							((BepInExLogInterpolatedStringHandler)val5).AppendFormatted<string>(text4);
							((BepInExLogInterpolatedStringHandler)val5).AppendLiteral(": ");
							((BepInExLogInterpolatedStringHandler)val5).AppendFormatted<string>(ex3.Message);
						}
						logger.LogWarning(val5);
					}
				}
			}
			if (text6 == Strings.missingString)
			{
				text6 = "";
			}
			if (string.IsNullOrWhiteSpace(text6) && ((val3 != null) ? val3.messages : null) != null)
			{
				Object val6 = (Object)((val != null) ? ((object)val) : ((object)val2));
				Enumerator<DDSMessageSettings> enumerator = val3.messages.GetEnumerator();
				while (enumerator.MoveNext())
				{
					DDSMessageSettings current = enumerator.Current;
					if (val6 == null || current == null || string.IsNullOrWhiteSpace(current.msgID))
					{
						continue;
					}
					try
					{
						string textForComponent = Strings.GetTextForComponent(current.msgID, val6, (Human)null, (Human)null, "\n", false, (Object)null, (LinkSetting)0, (List<DataKey>)null);
						if (!string.IsNullOrWhiteSpace(textForComponent) && textForComponent != Strings.missingString)
						{
							text6 = textForComponent;
							break;
						}
					}
					catch (Exception ex4)
					{
						ManualLogSource logger = Plugin.Logger;
						if (logger != null)
						{
							BepInExWarningLogInterpolatedStringHandler val5 = new BepInExWarningLogInterpolatedStringHandler(28, 2, ref flag);
							if (flag)
							{
								((BepInExLogInterpolatedStringHandler)val5).AppendLiteral("MESSAGE_LOOKUP_FAILED msg=");
								((BepInExLogInterpolatedStringHandler)val5).AppendFormatted<string>(current.msgID);
								((BepInExLogInterpolatedStringHandler)val5).AppendLiteral(": ");
								((BepInExLogInterpolatedStringHandler)val5).AppendFormatted<string>(ex4.Message);
							}
							logger.LogWarning(val5);
						}
					}
				}
			}
			if (!string.IsNullOrWhiteSpace(text6) && text6 != Strings.missingString)
			{
				ShowFallback(content, text6);
				ManualLogSource logger = Plugin.Logger;
				if (logger != null)
				{
					BepInExInfoLogInterpolatedStringHandler val4 = new BepInExInfoLogInterpolatedStringHandler(50, 4, ref flag);
					if (flag)
					{
						((BepInExLogInterpolatedStringHandler)val4).AppendLiteral("RECOVERED_SOURCE_TEXT title=");
						((BepInExLogInterpolatedStringHandler)val4).AppendFormatted<string>(text2);
						((BepInExLogInterpolatedStringHandler)val4).AppendLiteral(" tree=");
						((BepInExLogInterpolatedStringHandler)val4).AppendFormatted<string>(text);
						((BepInExLogInterpolatedStringHandler)val4).AppendLiteral(" evidence=");
						((BepInExLogInterpolatedStringHandler)val4).AppendFormatted<string>(text3);
						((BepInExLogInterpolatedStringHandler)val4).AppendLiteral(" item=");
						((BepInExLogInterpolatedStringHandler)val4).AppendFormatted<string>(text4);
					}
					logger.LogInfo(val4);
				}
				return;
			}
			string sourceError = GetSourceError(val3, text, instance);
			string item = $"{text4}|{text3}|{text}|{content.page}|{sourceError}";
			if (ReportedErrors.Add(item))
			{
				ManualLogSource logger = Plugin.Logger;
				if (logger != null)
				{
					BepInExErrorLogInterpolatedStringHandler val7 = new BepInExErrorLogInterpolatedStringHandler(87, 9, ref flag);
					if (flag)
					{
						((BepInExLogInterpolatedStringHandler)val7).AppendLiteral("DOCUMENT_TEXT_");
						((BepInExLogInterpolatedStringHandler)val7).AppendFormatted<string>(sourceError);
						((BepInExLogInterpolatedStringHandler)val7).AppendLiteral(" title=");
						((BepInExLogInterpolatedStringHandler)val7).AppendFormatted<string>(text2);
						((BepInExLogInterpolatedStringHandler)val7).AppendLiteral(" tree=");
						((BepInExLogInterpolatedStringHandler)val7).AppendFormatted<string>(text);
						((BepInExLogInterpolatedStringHandler)val7).AppendLiteral(" evidence=");
						((BepInExLogInterpolatedStringHandler)val7).AppendFormatted<string>(text3);
						((BepInExLogInterpolatedStringHandler)val7).AppendLiteral(" evidencePreset=");
						object obj2;
						if (val2 == null)
						{
							obj2 = null;
						}
						else
						{
							EvidencePreset preset = val2.preset;
							obj2 = ((preset != null) ? ((Object)preset).name : null);
						}
						((BepInExLogInterpolatedStringHandler)val7).AppendFormatted<string>((string)obj2);
						((BepInExLogInterpolatedStringHandler)val7).AppendLiteral(" item=");
						((BepInExLogInterpolatedStringHandler)val7).AppendFormatted<string>(text4);
						((BepInExLogInterpolatedStringHandler)val7).AppendLiteral(" itemPreset=");
						object obj3;
						if (val == null)
						{
							obj3 = null;
						}
						else
						{
							InteractablePreset preset2 = val.preset;
							obj3 = ((preset2 != null) ? ((Object)preset2).name : null);
						}
						((BepInExLogInterpolatedStringHandler)val7).AppendFormatted<string>((string)obj3);
						((BepInExLogInterpolatedStringHandler)val7).AppendLiteral(" itemName=");
						((BepInExLogInterpolatedStringHandler)val7).AppendFormatted<string>((val != null) ? val.name : null);
						((BepInExLogInterpolatedStringHandler)val7).AppendLiteral(" page=");
						((BepInExLogInterpolatedStringHandler)val7).AppendFormatted<int>(content.page);
					}
					logger.LogError(val7);
				}
				try
				{
					InterfaceController instance2 = InterfaceController.Instance;
					if (instance2 != null)
					{
						instance2.NewGameMessage((GameMessageType)0, 0, $"Document text error: {sourceError} (tree {text})", (Icon)3, (AudioEvent)null, false, default(Color), -1, 0f, (RectTransform)null, (PingOnComplete)0, (Evidence)null, (List<DataKey>)null, (Sprite)null);
					}
				}
				catch (Exception ex5)
				{
					logger = Plugin.Logger;
					if (logger != null)
					{
						BepInExWarningLogInterpolatedStringHandler val5 = new BepInExWarningLogInterpolatedStringHandler(35, 1, ref flag);
						if (flag)
						{
							((BepInExLogInterpolatedStringHandler)val5).AppendLiteral("DOCUMENT_TEXT_NOTIFICATION_FAILED: ");
							((BepInExLogInterpolatedStringHandler)val5).AppendFormatted<string>(ex5.Message);
						}
						logger.LogWarning(val5);
					}
				}
			}
			try
			{
				if ((Object)(object)((window != null) ? window.titleText : null) != (Object)null)
				{
					((TMP_Text)window.titleText).text = RemoveErrorSuffix(text2) + " [DTF: " + sourceError + "]";
				}
			}
			catch
			{
			}
			ShowFallback(content, $"Document Text Fix: {sourceError}\nTree: {text}\nEvidence: {text3}");
		}

		private static string GetSourceError(DDSTreeSave? tree, string treeId, Toolbox? toolbox)
		{
			if (string.IsNullOrWhiteSpace(treeId))
			{
				return "DOCUMENT_ID_UNKNOWN";
			}
			if (tree == null && (((toolbox != null) ? toolbox.allDDSTrees : null) == null || !toolbox.allDDSTrees.ContainsKey(treeId)))
			{
				return "TREE_NOT_REGISTERED";
			}
			if (tree == null)
			{
				return "TREE_NOT_BOUND_TO_WINDOW";
			}
			if (tree.messages == null || tree.messages.Count == 0)
			{
				return "TREE_HAS_NO_MESSAGES";
			}
			Enumerator<DDSMessageSettings> enumerator = tree.messages.GetEnumerator();
			while (enumerator.MoveNext())
			{
				DDSMessageSettings current = enumerator.Current;
				if (current != null && !string.IsNullOrWhiteSpace(current.msgID) && (((toolbox != null) ? toolbox.allDDSMessages : null) == null || !toolbox.allDDSMessages.ContainsKey(current.msgID)))
				{
					return "MESSAGE_NOT_REGISTERED:" + current.msgID;
				}
			}
			return "SOURCE_TEXT_EMPTY_OR_RENDER_FAILED";
		}

		private static string FirstValue(params string?[] values)
		{
			foreach (string text in values)
			{
				if (!string.IsNullOrWhiteSpace(text))
				{
					return text;
				}
			}
			return "";
		}

		private static bool TryFindTreeByName(Toolbox? toolbox, Interactable? item, Evidence? evidence, out DDSTreeSave? match)
		{
			//IL_012b: Unknown result type (might be due to invalid IL or missing references)
			//IL_0132: Expected O, but got Unknown
			match = null;
			if (((toolbox != null) ? toolbox.allDDSTrees : null) == null)
			{
				return false;
			}
			string[] array = new string[4];
			object obj;
			if (evidence == null)
			{
				obj = null;
			}
			else
			{
				EvidencePreset preset = evidence.preset;
				obj = ((preset != null) ? ((Object)preset).name : null);
			}
			array[0] = (string)obj;
			object obj2;
			if (item == null)
			{
				obj2 = null;
			}
			else
			{
				InteractablePreset preset2 = item.preset;
				obj2 = ((preset2 != null) ? ((Object)preset2).name : null);
			}
			array[1] = (string)obj2;
			object obj3;
			if (evidence == null)
			{
				obj3 = null;
			}
			else
			{
				InteractablePreset interactablePreset = evidence.interactablePreset;
				obj3 = ((interactablePreset != null) ? ((Object)interactablePreset).name : null);
			}
			array[2] = (string)obj3;
			array[3] = ((item != null) ? item.name : null);
			string[] array2 = array;
			bool flag = default(bool);
			foreach (string text in array2)
			{
				string text2 = NormalizeName(text);
				if (text2.Length < 8 || text2 == "document" || text2 == "newspaper")
				{
					continue;
				}
				DDSTreeSave val = null;
				int num = 0;
				Enumerator<string, DDSTreeSave> enumerator = toolbox.allDDSTrees.GetEnumerator();
				while (enumerator.MoveNext())
				{
					DDSTreeSave value = enumerator.Current.Value;
					if (value != null && NormalizeName(((DDSComponent)value).name) == text2)
					{
						val = value;
						num++;
						if (num > 1)
						{
							break;
						}
					}
				}
				if (num == 1)
				{
					match = val;
					return true;
				}
				if (num <= 1)
				{
					continue;
				}
				ManualLogSource logger = Plugin.Logger;
				if (logger != null)
				{
					BepInExWarningLogInterpolatedStringHandler val2 = new BepInExWarningLogInterpolatedStringHandler(32, 2, ref flag);
					if (flag)
					{
						((BepInExLogInterpolatedStringHandler)val2).AppendLiteral("AMBIGUOUS_TREE_NAME name=");
						((BepInExLogInterpolatedStringHandler)val2).AppendFormatted<string>(text);
						((BepInExLogInterpolatedStringHandler)val2).AppendLiteral(" count=");
						((BepInExLogInterpolatedStringHandler)val2).AppendFormatted<int>(num);
					}
					logger.LogWarning(val2);
				}
			}
			return false;
		}

		private static string NormalizeName(string? value)
		{
			if (string.IsNullOrEmpty(value))
			{
				return "";
			}
			List<char> list = new List<char>(value.Length);
			foreach (char c in value)
			{
				if (char.IsLetterOrDigit(c))
				{
					list.Add(char.ToLowerInvariant(c));
				}
			}
			return new string(list.ToArray());
		}

		private static void ShowFallback(WindowContentController content, string value)
		{
			//IL_0139: Unknown result type (might be due to invalid IL or missing references)
			//IL_0140: Expected O, but got Unknown
			//IL_0048: Unknown result type (might be due to invalid IL or missing references)
			//IL_004d: Unknown result type (might be due to invalid IL or missing references)
			//IL_005f: Unknown result type (might be due to invalid IL or missing references)
			//IL_0070: Unknown result type (might be due to invalid IL or missing references)
			//IL_0085: Unknown result type (might be due to invalid IL or missing references)
			//IL_0090: Unknown result type (might be due to invalid IL or missing references)
			//IL_009a: Unknown result type (might be due to invalid IL or missing references)
			//IL_00cf: Unknown result type (might be due to invalid IL or missing references)
			try
			{
				if ((Object)(object)content.pageRect == (Object)null)
				{
					return;
				}
				Transform val = ((Transform)content.pageRect).Find("DocumentTextFixFallback");
				TextMeshProUGUI val2 = (((Object)(object)val == (Object)null) ? null : ((Component)val).GetComponent<TextMeshProUGUI>());
				if ((Object)(object)val2 == (Object)null)
				{
					GameObject val3 = new GameObject("DocumentTextFixFallback");
					val3.transform.SetParent((Transform)(object)content.pageRect, false);
					RectTransform obj = val3.AddComponent<RectTransform>();
					obj.anchorMin = new Vector2(0.06f, 0.06f);
					obj.anchorMax = new Vector2(0.94f, 0.94f);
					obj.offsetMin = Vector2.zero;
					obj.offsetMax = Vector2.zero;
					val2 = val3.AddComponent<TextMeshProUGUI>();
					((TMP_Text)val2).fontSize = 18f;
					((TMP_Text)val2).alignment = (TextAlignmentOptions)257;
					((Graphic)val2).raycastTarget = false;
					((TMP_Text)val2).enableWordWrapping = true;
					((Graphic)val2).color = Color.black;
					TextMeshProUGUI componentInChildren = ((Component)content.pageRect).GetComponentInChildren<TextMeshProUGUI>(true);
					if ((Object)(object)componentInChildren != (Object)null)
					{
						((TMP_Text)val2).font = ((TMP_Text)componentInChildren).font;
					}
				}
				((Component)val2).gameObject.SetActive(true);
				((TMP_Text)val2).text = value;
				((TMP_Text)val2).ForceMeshUpdate(true, true);
				((TMP_Text)val2).transform.SetAsLastSibling();
				Canvas.ForceUpdateCanvases();
			}
			catch (Exception ex)
			{
				ManualLogSource logger = Plugin.Logger;
				if (logger != null)
				{
					bool flag = default(bool);
					BepInExErrorLogInterpolatedStringHandler val4 = new BepInExErrorLogInterpolatedStringHandler(34, 1, ref flag);
					if (flag)
					{
						((BepInExLogInterpolatedStringHandler)val4).AppendLiteral("DOCUMENT_TEXT_FALLBACK_UI_FAILED: ");
						((BepInExLogInterpolatedStringHandler)val4).AppendFormatted<Exception>(ex);
					}
					logger.LogError(val4);
				}
			}
		}

		private static void HideFallback(WindowContentController content)
		{
			try
			{
				RectTransform pageRect = content.pageRect;
				Transform val = ((pageRect != null) ? ((Transform)pageRect).Find("DocumentTextFixFallback") : null);
				if ((Object)(object)val != (Object)null)
				{
					((Component)val).gameObject.SetActive(false);
				}
				InfoWindow window = content.window;
				TextMeshProUGUI val2 = ((window != null) ? window.titleText : null);
				if ((Object)(object)val2 != (Object)null)
				{
					((TMP_Text)val2).text = RemoveErrorSuffix(((TMP_Text)val2).text);
				}
			}
			catch
			{
			}
		}

		private static string RemoveErrorSuffix(string value)
		{
			int num = value.IndexOf(" [DTF: ", StringComparison.Ordinal);
			if (num >= 0)
			{
				return value.Substring(0, num);
			}
			return value;
		}
	}
}