Decompiled source of CasperCalc v1.0.0

plugins/CasperCalc/CasperCalc.dll

Decompiled 2 months ago
using System;
using System.Data;
using System.Diagnostics;
using System.Reflection;
using System.Runtime.CompilerServices;
using System.Runtime.InteropServices;
using System.Runtime.Versioning;
using System.Text;
using BepInEx;
using BepInEx.Configuration;
using BepInEx.Logging;
using EquinoxsModUtils;
using HarmonyLib;
using UnityEngine;

[assembly: CompilationRelaxations(8)]
[assembly: RuntimeCompatibility(WrapNonExceptionThrows = true)]
[assembly: Debuggable(DebuggableAttribute.DebuggingModes.Default | DebuggableAttribute.DebuggingModes.DisableOptimizations | DebuggableAttribute.DebuggingModes.IgnoreSymbolStoreSequencePoints | DebuggableAttribute.DebuggingModes.EnableEditAndContinue)]
[assembly: AssemblyTitle("CasperCalc")]
[assembly: AssemblyDescription("")]
[assembly: AssemblyConfiguration("")]
[assembly: AssemblyCompany("")]
[assembly: AssemblyProduct("CasperCalc")]
[assembly: AssemblyCopyright("Copyright ©  2024")]
[assembly: AssemblyTrademark("")]
[assembly: ComVisible(false)]
[assembly: Guid("ffc2e247-ae41-4e78-ba31-2a26157dcdf3")]
[assembly: AssemblyFileVersion("1.0.0.0")]
[assembly: TargetFramework(".NETFramework,Version=v4.7.2", FrameworkDisplayName = ".NET Framework 4.7.2")]
[assembly: AssemblyVersion("1.0.0.0")]
namespace CasperCalc;

[BepInPlugin("com.casper.CasperCalc", "CasperCalc", "1.0.0")]
public class mainPlugin : BaseUnityPlugin
{
	private const string MyGUID = "com.casper.CasperCalc";

	private const string PluginName = "CasperCalc";

	private const string VersionString = "1.0.0";

	private static readonly Harmony Harmony = new Harmony("com.casper.CasperCalc");

	public static ManualLogSource Log;

	private string input = "";

	private string result = "";

	private bool showCalculator = false;

	private bool inputFieldFocused = false;

	private float centeredX;

	private float centeredY;

	private Rect calculatorWindowRect;

	public static ConfigEntry<KeyCode> OpenCalc;

	private void Awake()
	{
		Log = ((BaseUnityPlugin)this).Logger;
		Log.LogInfo((object)"PluginName: CasperCalc, VersionString: 1.0.0 is loading...");
		Harmony.PatchAll();
		CreateConfigEntries();
		Log.LogInfo((object)"PluginName: CasperCalc, VersionString: 1.0.0 is loaded.");
	}

	private void Start()
	{
		//IL_0046: Unknown result type (might be due to invalid IL or missing references)
		//IL_004b: Unknown result type (might be due to invalid IL or missing references)
		centeredX = (Screen.width - 300) / 2;
		centeredY = (Screen.height - 200) / 2 + 300;
		calculatorWindowRect = new Rect(centeredX, centeredY, 400f, 200f);
	}

	private void Update()
	{
		//IL_0006: Unknown result type (might be due to invalid IL or missing references)
		if (Input.GetKeyDown(OpenCalc.Value))
		{
			ToggleCalculator();
		}
	}

	private void CreateConfigEntries()
	{
		//IL_001e: Unknown result type (might be due to invalid IL or missing references)
		//IL_0028: Expected O, but got Unknown
		OpenCalc = ((BaseUnityPlugin)this).Config.Bind<KeyCode>("General", "Calculator Key", (KeyCode)110, new ConfigDescription("Key to open and close the window.", (AcceptableValueBase)null, Array.Empty<object>()));
	}

	private void ToggleCalculator()
	{
		showCalculator = !showCalculator;
		ModUtils.FreeCursor(showCalculator);
		if (showCalculator)
		{
			inputFieldFocused = true;
		}
	}

	private void OnGUI()
	{
		//IL_000c: Unknown result type (might be due to invalid IL or missing references)
		//IL_001a: Unknown result type (might be due to invalid IL or missing references)
		//IL_0026: Unknown result type (might be due to invalid IL or missing references)
		//IL_003a: Expected O, but got Unknown
		//IL_0035: Unknown result type (might be due to invalid IL or missing references)
		//IL_003a: Unknown result type (might be due to invalid IL or missing references)
		if (showCalculator)
		{
			GUI.color = Color.white;
			calculatorWindowRect = GUILayout.Window(0, calculatorWindowRect, new WindowFunction(CalculatorWindow), "CasperCalc", Array.Empty<GUILayoutOption>());
		}
	}

	private void CalculatorWindow(int windowID)
	{
		//IL_000b: Unknown result type (might be due to invalid IL or missing references)
		//IL_0010: Unknown result type (might be due to invalid IL or missing references)
		//IL_001a: Expected O, but got Unknown
		//IL_0024: Unknown result type (might be due to invalid IL or missing references)
		//IL_0029: Unknown result type (might be due to invalid IL or missing references)
		//IL_0033: Expected O, but got Unknown
		//IL_0038: Unknown result type (might be due to invalid IL or missing references)
		//IL_003e: Invalid comparison between Unknown and I4
		//IL_0045: Unknown result type (might be due to invalid IL or missing references)
		//IL_004f: Unknown result type (might be due to invalid IL or missing references)
		//IL_0091: Unknown result type (might be due to invalid IL or missing references)
		//IL_011f: Unknown result type (might be due to invalid IL or missing references)
		//IL_0125: Invalid comparison between Unknown and I4
		//IL_0194: Unknown result type (might be due to invalid IL or missing references)
		GUIStyle val = new GUIStyle(GUI.skin.textField)
		{
			fontSize = 24
		};
		GUIStyle val2 = new GUIStyle(GUI.skin.label)
		{
			fontSize = 24
		};
		if ((int)Event.current.type == 4 && Event.current.keyCode == OpenCalc.Value)
		{
			ToggleCalculator();
			Event.current.Use();
		}
		if (GUI.Button(new Rect(((Rect)(ref calculatorWindowRect)).width - 35f, 5f, 30f, 30f), "X"))
		{
			ToggleCalculator();
		}
		GUILayout.Space(40f);
		GUILayout.BeginVertical(Array.Empty<GUILayoutOption>());
		GUI.SetNextControlName("InputField");
		string text = GUILayout.TextField(input, val, (GUILayoutOption[])(object)new GUILayoutOption[1] { GUILayout.Height(50f) });
		text = ValidateInput(text);
		if (text != input)
		{
			input = text;
			CalculateResult();
		}
		if ((int)Event.current.type == 7 && inputFieldFocused)
		{
			GUI.FocusControl("InputField");
			inputFieldFocused = false;
		}
		GUILayout.Label("Result: " + result, val2, (GUILayoutOption[])(object)new GUILayoutOption[1] { GUILayout.Height(50f) });
		GUILayout.EndVertical();
		GUI.DragWindow(new Rect(0f, 0f, ((Rect)(ref calculatorWindowRect)).width, 20f));
	}

	private void CalculateResult()
	{
		try
		{
			result = EvaluateExpression(input).ToString();
		}
		catch
		{
			result = "Error";
		}
	}

	private double EvaluateExpression(string expression)
	{
		DataTable dataTable = new DataTable();
		try
		{
			return double.Parse(dataTable.Compute(expression, "").ToString());
		}
		finally
		{
			((IDisposable)dataTable)?.Dispose();
		}
	}

	private string ValidateInput(string input)
	{
		string text = "0123456789+-*/().";
		StringBuilder stringBuilder = new StringBuilder();
		for (int i = 0; i < input.Length; i++)
		{
			char value = input[i];
			if (text.Contains(value.ToString()))
			{
				stringBuilder.Append(value);
			}
		}
		return stringBuilder.ToString();
	}
}