Decompiled source of SimpleWeatherDisplay v1.0.1

SimpleWeatherDisplay.dll

Decompiled 10 months 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 System.Security;
using System.Security.Permissions;
using BepInEx;
using BepInEx.Configuration;
using BepInEx.Logging;
using HarmonyLib;
using Microsoft.CodeAnalysis;
using Mono.Cecil.Cil;
using MonoMod.Cil;

[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("SimpleWeatherDisplay")]
[assembly: AssemblyConfiguration("Debug")]
[assembly: AssemblyDescription("A mod for Lethal Company")]
[assembly: AssemblyFileVersion("1.0.0.0")]
[assembly: AssemblyInformationalVersion("1.0.0+3463d0d6a691615512fe9398491320cfc174e8e4")]
[assembly: AssemblyProduct("SimpleWeatherDisplay")]
[assembly: AssemblyTitle("SimpleWeatherDisplay")]
[assembly: SecurityPermission(SecurityAction.RequestMinimum, SkipVerification = true)]
[assembly: AssemblyVersion("1.0.0.0")]
[module: UnverifiableCode]
[module: RefSafetyRules(11)]
namespace Microsoft.CodeAnalysis
{
	[CompilerGenerated]
	[Microsoft.CodeAnalysis.Embedded]
	internal sealed class EmbeddedAttribute : Attribute
	{
	}
}
namespace System.Runtime.CompilerServices
{
	[CompilerGenerated]
	[Microsoft.CodeAnalysis.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 SimpleWeatherDisplay
{
	public class Config
	{
		public static ConfigEntry<bool> ApplyToShipScreen { get; private set; }

		public static ConfigEntry<bool> ApplyToLandingSequence { get; private set; }

		public static ConfigEntry<bool> DisplayClearWeather { get; private set; }

		public static ConfigEntry<bool> CapitalizeTitle { get; private set; }

		public static ConfigEntry<bool> MoveToTop { get; private set; }

		public Config(ConfigFile cfg)
		{
			ApplyToShipScreen = cfg.Bind<bool>("General", "ApplyToShipScreen", true, "Whether to apply the mod's changes to the ship screen.");
			ApplyToLandingSequence = cfg.Bind<bool>("General", "ApplyToLandingSequence", true, "Whether to apply the mod's changes to the moon info popup on landing.");
			DisplayClearWeather = cfg.Bind<bool>("Tweaks", "DisplayClearWeather", true, "Still displays weather when it's clear.");
			CapitalizeTitle = cfg.Bind<bool>("Tweaks", "CapitalizeTitle", true, "Capitalizes the word 'WEATHER' like the rest of the moon info.");
			MoveToTop = cfg.Bind<bool>("Tweaks", "MoveToTop", true, "Moves the weather display to the top, making it always visible on the screen.");
		}

		public static string GetWeatherTitle()
		{
			return CapitalizeTitle.Value ? "WEATHER" : "Weather";
		}
	}
	[BepInPlugin("SylviBlossom.SimpleWeatherDisplay", "SimpleWeatherDisplay", "1.0.1")]
	public class Plugin : BaseUnityPlugin
	{
		public static Plugin Instance { get; private set; }

		public static Config Config { get; private set; }

		public static ManualLogSource Logger { get; private set; }

		private void Awake()
		{
			Instance = this;
			Config = new Config(((BaseUnityPlugin)this).Config);
			Logger = ((BaseUnityPlugin)this).Logger;
			Harmony.CreateAndPatchAll(typeof(Plugin), "SylviBlossom.SimpleWeatherDisplay");
			Logger.LogInfo((object)"Plugin SylviBlossom.SimpleWeatherDisplay is loaded!");
		}

		[HarmonyILManipulator]
		[HarmonyPatch(typeof(StartOfRound), "SetMapScreenInfoToCurrentLevel")]
		private static void StartOfRound_SetMapScreenInfoToCurrentLevel(ILContext il)
		{
			//IL_0002: Unknown result type (might be due to invalid IL or missing references)
			//IL_0008: Expected O, but got Unknown
			ILCursor val = new ILCursor(il);
			if (!val.TryGotoNext((MoveType)2, new Func<Instruction, bool>[1]
			{
				(Instruction instr) => ILPatternMatchingExt.MatchLdstr(instr, "Weather: ")
			}))
			{
				Logger.LogError((object)"Failed IL hook for StartOfRound.SetMapScreenInfoToCurrentLevel @ Non-clear weather string");
				return;
			}
			val.EmitDelegate<Func<string, string>>((Func<string, string>)((string str) => (!Config.ApplyToShipScreen.Value) ? str : (Config.GetWeatherTitle() + ": ")));
			if (!val.TryGotoNext((MoveType)2, new Func<Instruction, bool>[1]
			{
				(Instruction instr) => ILPatternMatchingExt.MatchLdstr(instr, "")
			}))
			{
				Logger.LogError((object)"Failed IL hook for StartOfRound.SetMapScreenInfoToCurrentLevel @ Clear weather string");
				return;
			}
			val.EmitDelegate<Func<string, string>>((Func<string, string>)((string str) => (!Config.ApplyToShipScreen.Value || !Config.DisplayClearWeather.Value) ? str : (Config.GetWeatherTitle() + ": Clear")));
			int num = 0;
			while (val.TryGotoNext((MoveType)2, new Func<Instruction, bool>[1]
			{
				(Instruction instr) => ILPatternMatchingExt.MatchLdstr(instr, "Orbiting: ")
			}) && val.TryGotoNext((MoveType)1, new Func<Instruction, bool>[1]
			{
				(Instruction instr) => ILPatternMatchingExt.MatchCall<string>(instr, "Concat")
			}))
			{
				val.EmitDelegate<Func<string[], string[]>>((Func<string[], string[]>)delegate(string[] text)
				{
					if (!Config.ApplyToShipScreen.Value || !Config.MoveToTop.Value)
					{
						return text;
					}
					List<string> list = text.ToList();
					int num2 = list.FindIndex((string line) => line.StartsWith("weather: ", StringComparison.InvariantCultureIgnoreCase));
					if (num2 == -1)
					{
						if (Config.DisplayClearWeather.Value)
						{
							Logger.LogWarning((object)"Found no weather display on screen");
						}
						return text;
					}
					string item = list[num2];
					list.RemoveAt(num2);
					list.RemoveAt(num2 - 1);
					int num3 = list.IndexOf("\n");
					if (num3 == -1)
					{
						num3 = list.Count;
					}
					list.Insert(num3, "\n");
					list.Insert(num3 + 1, item);
					return list.ToArray();
				});
				num++;
			}
			if (num < 2)
			{
				Logger.LogError((object)$"Failed IL hook for StartOfRound.SetMapScreenInfoToCurrentLevel @ Screen text (Replaced {num}/2)");
			}
		}

		[HarmonyILManipulator]
		[HarmonyPatch(/*Could not decode attribute arguments.*/)]
		private static void StartOfRound_openingDoorsSequence(ILContext il)
		{
			//IL_0002: Unknown result type (might be due to invalid IL or missing references)
			//IL_0008: Expected O, but got Unknown
			//IL_0052: Unknown result type (might be due to invalid IL or missing references)
			ILCursor val = new ILCursor(il);
			if (!val.TryGotoNext((MoveType)2, new Func<Instruction, bool>[1]
			{
				(Instruction instr) => ILPatternMatchingExt.MatchLdfld<SelectableLevel>(instr, "LevelDescription")
			}))
			{
				Logger.LogError((object)"Failed IL hook for StartOfRound.openingDoorsSequence");
				return;
			}
			val.Emit(OpCodes.Ldloc_1);
			val.EmitDelegate<Func<string, StartOfRound, string>>((Func<string, StartOfRound, string>)delegate(string desc, StartOfRound self)
			{
				//IL_002c: Unknown result type (might be due to invalid IL or missing references)
				//IL_0032: Invalid comparison between Unknown and I4
				//IL_0048: Unknown result type (might be due to invalid IL or missing references)
				//IL_004e: Invalid comparison between Unknown and I4
				if (!Config.ApplyToLandingSequence.Value)
				{
					return desc;
				}
				if (!Config.DisplayClearWeather.Value && (int)self.currentLevel.currentWeather == -1)
				{
					return desc;
				}
				string text = (((int)self.currentLevel.currentWeather != -1) ? ((object)(LevelWeatherType)(ref self.currentLevel.currentWeather)).ToString() : "Clear");
				string text2 = Config.GetWeatherTitle() + ": " + text;
				return Config.MoveToTop.Value ? (text2 + "\n" + desc) : (desc + "\n" + text2);
			});
		}
	}
	public static class PluginInfo
	{
		public const string PLUGIN_GUID = "SylviBlossom.SimpleWeatherDisplay";

		public const string PLUGIN_NAME = "SimpleWeatherDisplay";

		public const string PLUGIN_VERSION = "1.0.1";
	}
}