Decompiled source of LiberatedTemperature v1.2.0

plugins/LiberatedTemperature.dll

Decompiled a month ago
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Reflection;
using System.Reflection.Emit;
using System.Runtime.CompilerServices;
using System.Runtime.InteropServices;
using System.Text;
using BepInEx;
using BepInEx.Configuration;
using HarmonyLib;

[assembly: CompilationRelaxations(8)]
[assembly: RuntimeCompatibility(WrapNonExceptionThrows = true)]
[assembly: Debuggable(DebuggableAttribute.DebuggingModes.Default | DebuggableAttribute.DebuggingModes.DisableOptimizations | DebuggableAttribute.DebuggingModes.IgnoreSymbolStoreSequencePoints | DebuggableAttribute.DebuggingModes.EnableEditAndContinue)]
[assembly: AssemblyTitle("LiberatedTemperature")]
[assembly: AssemblyDescription("")]
[assembly: AssemblyConfiguration("")]
[assembly: AssemblyCompany("")]
[assembly: AssemblyProduct("LiberatedTemperature")]
[assembly: AssemblyCopyright("Copyright © MangoMangoDev 2024")]
[assembly: AssemblyTrademark("")]
[assembly: ComVisible(false)]
[assembly: Guid("57A849EF-B4ED-40A0-B744-AF788BBAAF50")]
[assembly: AssemblyFileVersion("1.1.0")]
[assembly: AssemblyVersion("1.1.9343.42964")]
namespace LiberatedTemperature;

public static class PluginInfo
{
	public const string PLUGIN_GUID = "LiberatedTemperature";

	public const string PLUGIN_NAME = "Liberated Temperature";

	public const string PLUGIN_VERSION = "1.2.0";
}
[BepInPlugin("LiberatedTemperature", "Liberated Temperature", "1.2.0")]
public class Plugin : BaseUnityPlugin
{
	public static Plugin Instance;

	public static ConfigEntry<bool> SecondaryTemperatureEnabled;

	public static ConfigEntry<string> SecondaryTemperatureUnit;

	public static void LogMessage(string message)
	{
		((BaseUnityPlugin)Instance).Logger.LogError((object)(message ?? ""));
	}

	public static void LogWarning(string message)
	{
		((BaseUnityPlugin)Instance).Logger.LogError((object)(message ?? ""));
	}

	public static void LogError(string message)
	{
		((BaseUnityPlugin)Instance).Logger.LogError((object)(message ?? ""));
	}

	private void Awake()
	{
		Instance = this;
		LogMessage("Reading Config...");
		SecondaryTemperatureEnabled = ((BaseUnityPlugin)this).Config.Bind<bool>("Second Unit", "EnableSecondUnit", false, "Whether a second unit should be used or not.");
		SecondaryTemperatureUnit = ((BaseUnityPlugin)this).Config.Bind<string>("Second Unit", "SelectedUnit", "K", "The unit of the second temperature. Valid options: K, C, F, R.");
		LogMessage("Config read!");
		LogMessage("Patching...");
		Harmony.CreateAndPatchAll(typeof(Plugin), (string)null);
		LogMessage("Patched!");
		LogMessage("Plugin LiberatedTemperature is loaded!");
	}

	[HarmonyPatch(typeof(GUIOptions), "Init")]
	[HarmonyTranspiler]
	public static IEnumerable<CodeInstruction> PatchGUIOptions_AddEntry(IEnumerable<CodeInstruction> instructions)
	{
		//IL_0004: Unknown result type (might be due to invalid IL or missing references)
		//IL_000a: Expected O, but got Unknown
		//IL_0021: Unknown result type (might be due to invalid IL or missing references)
		//IL_0027: Expected O, but got Unknown
		//IL_0035: Unknown result type (might be due to invalid IL or missing references)
		//IL_003b: Expected O, but got Unknown
		//IL_004d: Unknown result type (might be due to invalid IL or missing references)
		//IL_0053: Expected O, but got Unknown
		//IL_0061: Unknown result type (might be due to invalid IL or missing references)
		//IL_0067: Expected O, but got Unknown
		//IL_0075: Unknown result type (might be due to invalid IL or missing references)
		//IL_007b: Expected O, but got Unknown
		//IL_0089: Unknown result type (might be due to invalid IL or missing references)
		//IL_008f: Expected O, but got Unknown
		//IL_009d: Unknown result type (might be due to invalid IL or missing references)
		//IL_00a3: Expected O, but got Unknown
		//IL_00b1: Unknown result type (might be due to invalid IL or missing references)
		//IL_00b7: Expected O, but got Unknown
		//IL_00c5: Unknown result type (might be due to invalid IL or missing references)
		//IL_00cb: Expected O, but got Unknown
		//IL_0132: Unknown result type (might be due to invalid IL or missing references)
		//IL_0138: Expected O, but got Unknown
		//IL_0144: Unknown result type (might be due to invalid IL or missing references)
		//IL_014a: Expected O, but got Unknown
		//IL_018a: Unknown result type (might be due to invalid IL or missing references)
		//IL_0190: Expected O, but got Unknown
		CodeMatcher val;
		try
		{
			val = new CodeMatcher(instructions, (ILGenerator)null);
			val.MatchForward(false, (CodeMatch[])(object)new CodeMatch[9]
			{
				new CodeMatch((OpCode?)OpCodes.Ldarg_0, (object)null, (string)null),
				new CodeMatch((OpCode?)OpCodes.Ldfld, (object)null, (string)null),
				new CodeMatch((OpCode?)OpCodes.Ldstr, (object)"Kelvin", (string)null),
				new CodeMatch((OpCode?)OpCodes.Ldc_I4_0, (object)null, (string)null),
				new CodeMatch((OpCode?)OpCodes.Stloc_S, (object)null, (string)null),
				new CodeMatch((OpCode?)OpCodes.Ldloca_S, (object)null, (string)null),
				new CodeMatch((OpCode?)OpCodes.Constrained, (object)null, (string)null),
				new CodeMatch((OpCode?)OpCodes.Callvirt, (object)null, (string)null),
				new CodeMatch((OpCode?)OpCodes.Callvirt, (object)null, (string)null)
			});
			if (val.Remaining == 0)
			{
				throw new Exception("Could not find the branch destination label.");
			}
		}
		catch (Exception arg)
		{
			LogError($"Exception during transpilation instruction matching: {arg}");
			return instructions;
		}
		CodeInstruction[] array;
		try
		{
			array = (CodeInstruction[])(object)new CodeInstruction[5]
			{
				val.InstructionAt(0),
				val.InstructionAt(1),
				new CodeInstruction(OpCodes.Ldstr, (object)"Fahrenheit"),
				new CodeInstruction(OpCodes.Ldstr, (object)"F"),
				val.InstructionAt(8)
			};
		}
		catch (Exception arg2)
		{
			LogError($"Exception while collecting Fahrenheit transpilation instructions: {arg2}");
			return instructions;
		}
		CodeInstruction[] array2;
		try
		{
			array2 = (CodeInstruction[])(object)new CodeInstruction[5];
			for (int i = 0; i < array.Length; i++)
			{
				array2[i] = new CodeInstruction(array[i]);
			}
			array2[2].operand = "Rankine";
			array2[3].operand = "R";
		}
		catch (Exception arg3)
		{
			LogError($"Exception while collecting Rankine transpilation instructions: {arg3}");
			return instructions;
		}
		try
		{
			val.InsertAndAdvance(array);
		}
		catch (Exception arg4)
		{
			LogError($"Exception during Fahrenheit transpilation instruction insertion: {arg4}");
			return instructions;
		}
		try
		{
			val.InsertAndAdvance(array2);
		}
		catch (Exception arg5)
		{
			LogError($"Exception during Rankine transpilation instruction insertion: {arg5}");
			return instructions;
		}
		return val.InstructionEnumeration();
	}

	[HarmonyPatch(typeof(MathUtils), "GetTemperatureString")]
	[HarmonyPrefix]
	public static bool PatchMathUtils_AddCases(double dfAmount, ref string __result)
	{
		//IL_00a8: Unknown result type (might be due to invalid IL or missing references)
		//IL_00ad: Unknown result type (might be due to invalid IL or missing references)
		//IL_00af: Unknown result type (might be due to invalid IL or missing references)
		//IL_00b1: Unknown result type (might be due to invalid IL or missing references)
		//IL_00b3: Unknown result type (might be due to invalid IL or missing references)
		//IL_00ca: Expected I4, but got Unknown
		Traverse val = Traverse.Create(typeof(MathUtils)).Field("fTempLast");
		Traverse val2 = Traverse.Create(typeof(MathUtils)).Field("strTemp");
		if (dfAmount == val.GetValue<double>() && !string.IsNullOrEmpty(val2.GetValue<string>()))
		{
			__result = val2.GetValue<string>();
			return false;
		}
		Traverse val3 = Traverse.Create(typeof(MathUtils)).Field("sb");
		val3.GetValue<StringBuilder>().Length = 0;
		double num = dfAmount - 273.15;
		TemperatureUnit val4 = DataHandler.dictSettings["UserSettings"].TemperatureUnit();
		TemperatureUnit val5 = val4;
		switch ((int)val5)
		{
		case 0:
			val3.GetValue<StringBuilder>().Append(dfAmount.ToString("n2"));
			val3.GetValue<StringBuilder>().Append("K");
			break;
		case 1:
			val3.GetValue<StringBuilder>().Append(num.ToString("n2"));
			val3.GetValue<StringBuilder>().Append("C");
			break;
		case 2:
		{
			double num3 = num * 9.0 / 5.0 + 32.0;
			val3.GetValue<StringBuilder>().Append(num3.ToString("n2"));
			val3.GetValue<StringBuilder>().Append("F");
			break;
		}
		case 3:
		{
			double num2 = dfAmount * 1.7999999523162842;
			val3.GetValue<StringBuilder>().Append(num2.ToString("n2"));
			val3.GetValue<StringBuilder>().Append("R");
			break;
		}
		default:
			LogWarning("Unknown temperature unit, defaulting to Kelvin.");
			val3.GetValue<StringBuilder>().Append(dfAmount.ToString("n2"));
			val3.GetValue<StringBuilder>().Append("K");
			break;
		}
		if (SecondaryTemperatureEnabled.Value)
		{
			val3.GetValue<StringBuilder>().Append(" | ");
			switch (SecondaryTemperatureUnit.Value)
			{
			case "K":
				val3.GetValue<StringBuilder>().Append(dfAmount.ToString("n2"));
				val3.GetValue<StringBuilder>().Append("K");
				break;
			case "C":
				val3.GetValue<StringBuilder>().Append(num.ToString("n2"));
				val3.GetValue<StringBuilder>().Append("C");
				break;
			case "R":
			{
				double num5 = dfAmount * 1.7999999523162842;
				val3.GetValue<StringBuilder>().Append(num5.ToString("n2"));
				val3.GetValue<StringBuilder>().Append("R");
				break;
			}
			case "F":
			{
				double num4 = num * 9.0 / 5.0 + 32.0;
				val3.GetValue<StringBuilder>().Append(num4.ToString("n2"));
				val3.GetValue<StringBuilder>().Append("F");
				break;
			}
			}
		}
		val.SetValue((object)dfAmount);
		val2.SetValue((object)val3.GetValue<StringBuilder>().ToString());
		__result = val2.GetValue<string>();
		return false;
	}

	[HarmonyPatch(typeof(JsonUserSettings), "TemperatureUnit")]
	[HarmonyPrefix]
	public static bool PatchJsonUserSettings_AddConditions(JsonUserSettings __instance, ref TemperatureUnit __result)
	{
		switch (__instance.strTemperatureUnit)
		{
		default:
			__result = (TemperatureUnit)0;
			break;
		case "C":
			__result = (TemperatureUnit)1;
			break;
		case "F":
			__result = (TemperatureUnit)2;
			break;
		case "R":
			__result = (TemperatureUnit)3;
			break;
		}
		return false;
	}
}