Decompiled source of MoonsListTidy v1.2.1

MoonsListTidy.dll

Decompiled 2 days ago
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Reflection;
using System.Runtime.CompilerServices;
using System.Runtime.Versioning;
using System.Text.RegularExpressions;
using BepInEx;
using BepInEx.Configuration;
using BepInEx.Logging;
using HarmonyLib;
using UnityEngine;

[assembly: CompilationRelaxations(8)]
[assembly: RuntimeCompatibility(WrapNonExceptionThrows = true)]
[assembly: Debuggable(DebuggableAttribute.DebuggingModes.IgnoreSymbolStoreSequencePoints)]
[assembly: TargetFramework(".NETStandard,Version=v2.1", FrameworkDisplayName = ".NET Standard 2.1")]
[assembly: AssemblyCompany("MoonsListTidy")]
[assembly: AssemblyConfiguration("Release")]
[assembly: AssemblyFileVersion("1.2.1.0")]
[assembly: AssemblyInformationalVersion("1.2.1")]
[assembly: AssemblyProduct("MoonsListTidy")]
[assembly: AssemblyTitle("MoonsListTidy")]
[assembly: AssemblyMetadata("AI_Assisted_Creation", "Code generated with Anthropic Claude (Claude Opus 5.5 and other Claude models, via Claude Code); designed and tested by the author")]
[assembly: AssemblyMetadata("AI_Model_Vendor", "Anthropic")]
[assembly: AssemblyVersion("1.2.1.0")]
namespace MoonsListTidy;

[BepInPlugin("jacob.moonslisttidy", "MoonsListTidy", "1.2.1")]
[BepInDependency(/*Could not decode attribute arguments.*/)]
public class Plugin : BaseUnityPlugin
{
	public const string Guid = "jacob.moonslisttidy";

	public const string Name = "MoonsListTidy";

	public const string Version = "1.2.1";

	internal static ConfigEntry<bool> HideNone;

	internal static ConfigEntry<bool> HideFuture;

	internal static ConfigEntry<bool> MarkPending;

	internal static bool Pending;

	internal const string PendingNotice = "Forecast pending: new weather arrives after the day report.";

	private static readonly Regex Divider = new Regex("^_{5,}[ \\t]*\\r?$", RegexOptions.Multiline | RegexOptions.Compiled);

	internal static ManualLogSource Log;

	private const string Sep = "(?<!<[^<>\\r\\n]*)-?>";

	private static readonly Regex ChainedGroups = new Regex("\\)[ \\t]*(?<!<[^<>\\r\\n]*)-?>[ \\t]*\\([^()\\r\\n]*\\)", RegexOptions.Compiled);

	private static readonly Regex Group = new Regex("\\(([^()\\r\\n]*)\\)", RegexOptions.Compiled);

	private static readonly Regex SepInGroup = new Regex("(?<!<[^<>\\r\\n]*)-?>", RegexOptions.Compiled);

	private static readonly Regex Tags = new Regex("<[^<>]*>", RegexOptions.Compiled);

	private static int _errors;

	private static bool _loggedSample;

	private void Awake()
	{
		//IL_0070: Unknown result type (might be due to invalid IL or missing references)
		//IL_0076: Expected O, but got Unknown
		//IL_009f: Unknown result type (might be due to invalid IL or missing references)
		//IL_00ad: Expected O, but got Unknown
		//IL_00f6: Unknown result type (might be due to invalid IL or missing references)
		//IL_00fb: Unknown result type (might be due to invalid IL or missing references)
		//IL_0104: Expected O, but got Unknown
		//IL_0183: Unknown result type (might be due to invalid IL or missing references)
		//IL_0190: Expected O, but got Unknown
		Log = ((BaseUnityPlugin)this).Logger;
		HideNone = ((BaseUnityPlugin)this).Config.Bind<bool>("General", "HideNoneWeather", true, "Remove \"(None)\" after clear moons in the terminal's moons list.");
		HideFuture = ((BaseUnityPlugin)this).Config.Bind<bool>("General", "HideFutureWeather", true, "Only show the landing weather for WeatherTweaks' progressing weathers (e.g. \"None > Stormy\" shows as clear).");
		MarkPending = ((BaseUnityPlugin)this).Config.Bind<bool>("General", "ForecastPending", true, "Between leaving a moon and the ship being ready in orbit, show \"(pending)\" instead of yesterday's weather.");
		Harmony val = new Harmony("jacob.moonslisttidy");
		try
		{
			MethodInfo methodInfo = AccessTools.Method(typeof(StartOfRound), "EndOfGame", (Type[])null, (Type[])null);
			val.Patch((MethodBase)methodInfo, new HarmonyMethod(typeof(Plugin), "EndOfGamePrefix", (Type[])null), (HarmonyMethod)null, (HarmonyMethod)null, (HarmonyMethod)null, (HarmonyMethod)null);
		}
		catch (Exception ex)
		{
			((BaseUnityPlugin)this).Logger.LogError((object)("Couldn't hook StartOfRound.EndOfGame, ForecastPending inactive: " + ex.Message));
		}
		try
		{
			MethodInfo methodInfo2 = AccessTools.Method(typeof(Terminal), "TextPostProcess", (Type[])null, (Type[])null);
			HarmonyMethod val2 = new HarmonyMethod(typeof(Plugin), "TextPostProcessPostfix", (Type[])null)
			{
				priority = 0
			};
			val.Patch((MethodBase)methodInfo2, (HarmonyMethod)null, val2, (HarmonyMethod)null, (HarmonyMethod)null, (HarmonyMethod)null);
		}
		catch (Exception ex2)
		{
			((BaseUnityPlugin)this).Logger.LogError((object)("Couldn't hook Terminal.TextPostProcess, moons list cleaning inactive: " + ex2));
		}
		try
		{
			Type type = QuietTypes.Find("WeatherRegistry.Patches.SetMapScreenInfoToCurrentLevelPatch");
			MethodInfo methodInfo3 = ((type == null) ? null : AccessTools.Method(type, "GetDisplayWeatherString", (Type[])null, (Type[])null));
			if (methodInfo3 != null)
			{
				val.Patch((MethodBase)methodInfo3, (HarmonyMethod)null, new HarmonyMethod(typeof(Plugin), "MapScreenPostfix", (Type[])null), (HarmonyMethod)null, (HarmonyMethod)null, (HarmonyMethod)null);
				((BaseUnityPlugin)this).Logger.LogInfo((object)"HideFutureWeather applied to the map screen");
			}
		}
		catch (Exception ex3)
		{
			((BaseUnityPlugin)this).Logger.LogWarning((object)("Couldn't hook the map screen weather: " + ex3.Message));
		}
		((BaseUnityPlugin)this).Logger.LogInfo((object)("MoonsListTidy 1.2.1 loaded. HideNoneWeather=" + HideNone.Value + ", HideFutureWeather=" + HideFuture.Value));
	}

	internal static bool LooksLikeMoonsList(string text)
	{
		if (text.IndexOf("exomoons catalogue", StringComparison.OrdinalIgnoreCase) < 0)
		{
			return text.IndexOf("PREVIEW:", StringComparison.Ordinal) >= 0;
		}
		return true;
	}

	internal static string FirstWeather(string s)
	{
		if (string.IsNullOrEmpty(s))
		{
			return s;
		}
		Match match = SepInGroup.Match(s);
		if (!match.Success)
		{
			return s;
		}
		string text = s.Substring(0, match.Index).TrimEnd(' ', '\t');
		int count = Regex.Matches(text, "<color", RegexOptions.IgnoreCase).Count;
		for (int i = Regex.Matches(text, "</color>", RegexOptions.IgnoreCase).Count; i < count; i++)
		{
			text += "</color>";
		}
		return text;
	}

	private static void EndOfGamePrefix()
	{
		Pending = true;
	}

	internal static bool IsPending()
	{
		if (!Pending || MarkPending == null || !MarkPending.Value)
		{
			return false;
		}
		StartOfRound instance = StartOfRound.Instance;
		if ((Object)(object)instance == (Object)null || instance.inShipPhase)
		{
			Pending = false;
			return false;
		}
		return true;
	}

	internal static string ApplyPending(string text)
	{
		string[] array = text.Split('\n');
		for (int i = 0; i < array.Length; i++)
		{
			string text2 = array[i];
			if (text2.TrimStart().StartsWith("*", StringComparison.Ordinal) && text2.IndexOf("Company", StringComparison.OrdinalIgnoreCase) < 0)
			{
				bool flag = text2.EndsWith("\r", StringComparison.Ordinal);
				int num = text2.IndexOf('(');
				if (num >= 0)
				{
					text2 = text2.Substring(0, num);
				}
				text2 = text2.TrimEnd(' ', '\t', '\r') + " (pending)";
				array[i] = (flag ? (text2 + "\r") : text2);
			}
		}
		text = string.Join("\n", array);
		Match match = Divider.Match(text);
		if (match.Success)
		{
			return text.Substring(0, match.Index + match.Length) + "\n\nForecast pending: new weather arrives after the day report." + text.Substring(match.Index + match.Length);
		}
		return "Forecast pending: new weather arrives after the day report.\n" + text;
	}

	internal static string Clean(string text)
	{
		return Clean(text, IsPending());
	}

	internal static string Clean(string text, bool pending)
	{
		if (string.IsNullOrEmpty(text) || !LooksLikeMoonsList(text))
		{
			return text;
		}
		if (pending)
		{
			return ApplyPending(text);
		}
		if (HideFuture != null && HideFuture.Value)
		{
			string text2;
			do
			{
				text2 = text;
				text = ChainedGroups.Replace(text, ")");
			}
			while (text != text2);
			text = Group.Replace(text, (Match g) => "(" + FirstWeather(g.Groups[1].Value) + ")");
		}
		if (HideNone == null || HideNone.Value)
		{
			text = Regex.Replace(text, "[ \\t]*\\(([^()\\r\\n]*)\\)", (Match g) => (!Tags.Replace(g.Groups[1].Value, "").Trim().Equals("None", StringComparison.OrdinalIgnoreCase)) ? g.Value : "");
		}
		return text;
	}

	private static void TextPostProcessPostfix(ref string __result)
	{
		if (_errors >= 5)
		{
			return;
		}
		try
		{
			string text = __result;
			__result = Clean(__result);
			if (!_loggedSample && text != null && LooksLikeMoonsList(text) && text != __result)
			{
				_loggedSample = true;
				Log.LogInfo((object)("Moons list cleaned. Before:\n" + Cut(text) + "\nAfter:\n" + Cut(__result)));
			}
		}
		catch (Exception ex)
		{
			_errors++;
			Log.LogWarning((object)("Cleaning the moons list failed (" + _errors + "/5, gives up after 5): " + ex.Message));
		}
	}

	private static void MapScreenPostfix(ref string __result)
	{
		try
		{
			if (HideFuture.Value)
			{
				__result = FirstWeather(__result);
			}
		}
		catch
		{
		}
	}

	private static string Cut(string s)
	{
		if (s.Length <= 2500)
		{
			return s;
		}
		return s.Substring(0, 2500) + "...";
	}
}
internal static class QuietTypes
{
	private static readonly Dictionary<string, Type> _hits;

	private static readonly Dictionary<string, int> _misses;

	private static readonly object _lock;

	private static int _generation;

	static QuietTypes()
	{
		_hits = new Dictionary<string, Type>(StringComparer.Ordinal);
		_misses = new Dictionary<string, int>(StringComparer.Ordinal);
		_lock = new object();
		try
		{
			AppDomain.CurrentDomain.AssemblyLoad += delegate
			{
				lock (_lock)
				{
					_generation++;
				}
			};
		}
		catch
		{
		}
	}

	internal static Type Find(string fullName)
	{
		if (string.IsNullOrEmpty(fullName))
		{
			return null;
		}
		int generation;
		lock (_lock)
		{
			if (_hits.TryGetValue(fullName, out var value))
			{
				return value;
			}
			if (_misses.TryGetValue(fullName, out var value2) && value2 == _generation)
			{
				return null;
			}
			generation = _generation;
		}
		Type type = Scan(fullName);
		lock (_lock)
		{
			if (type != null)
			{
				_hits[fullName] = type;
				_misses.Remove(fullName);
			}
			else
			{
				_misses[fullName] = generation;
			}
		}
		return type;
	}

	private static Type Scan(string fullName)
	{
		Assembly[] assemblies;
		try
		{
			assemblies = AppDomain.CurrentDomain.GetAssemblies();
		}
		catch
		{
			return null;
		}
		Assembly[] array = assemblies;
		foreach (Assembly assembly in array)
		{
			Type type = null;
			try
			{
				type = assembly.GetType(fullName, throwOnError: false);
			}
			catch
			{
			}
			if (type != null)
			{
				return type;
			}
		}
		return null;
	}
}