Decompiled source of fraud optimizer v1.5.0

FraudOptimizer.dll

Decompiled 2 days ago
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Reflection;
using System.Reflection.Emit;
using System.Runtime.CompilerServices;
using System.Runtime.Versioning;
using BepInEx;
using HarmonyLib;
using Microsoft.CodeAnalysis;
using PluginConfig.API;
using PluginConfig.API.Fields;
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("FraudOptimizer")]
[assembly: AssemblyConfiguration("Release")]
[assembly: AssemblyFileVersion("1.0.0.0")]
[assembly: AssemblyInformationalVersion("1.0.0+dc6a743b254298d04ebf982cd47acbbca38d61e4")]
[assembly: AssemblyProduct("FraudOptimizer")]
[assembly: AssemblyTitle("FraudOptimizer")]
[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.Module, AllowMultiple = false, Inherited = false)]
	internal sealed class RefSafetyRulesAttribute : Attribute
	{
		public readonly int Version;

		public RefSafetyRulesAttribute(int P_0)
		{
			Version = P_0;
		}
	}
}
[BepInPlugin("Dungeon.FraudOptimizer", "Fraud Optimizer", "1.5.0")]
[BepInDependency(/*Could not decode attribute arguments.*/)]
public class FraudOptimizer : BaseUnityPlugin
{
	public static FraudOptimizer Instance;

	private const int VanillaMaxDepth = 4;

	private static int activeMaxDepth = 4;

	private static float activeMaxDistance = float.MaxValue;

	private static float activeMinPixelArea;

	private static bool reportPending = true;

	private int maxRecursionDepth = 1;

	private float maxPortalDistance = 50f;

	private float minPortalPixelArea = 1000f;

	private Harmony harmony;

	private Type portalRenderType;

	private void Awake()
	{
		//IL_0074: Unknown result type (might be due to invalid IL or missing references)
		//IL_007e: Expected O, but got Unknown
		//IL_0097: Unknown result type (might be due to invalid IL or missing references)
		//IL_00a3: Expected O, but got Unknown
		Instance = this;
		BuildConfigMenu();
		Refresh();
		portalRenderType = AccessTools.TypeByName("ULTRAKILL.Portal.PortalRenderV2");
		if (portalRenderType == null)
		{
			((BaseUnityPlugin)this).Logger.LogError((object)"Fraud Optimizer: PortalRenderV2 was not found.");
			return;
		}
		MethodInfo methodInfo = AccessTools.Method(portalRenderType, "Setup", (Type[])null, (Type[])null);
		if (methodInfo == null)
		{
			((BaseUnityPlugin)this).Logger.LogError((object)"Fraud Optimizer: PortalRenderV2.Setup was not found.");
			return;
		}
		harmony = new Harmony("Dungeon.FraudOptimizer");
		try
		{
			harmony.Patch((MethodBase)methodInfo, (HarmonyMethod)null, (HarmonyMethod)null, new HarmonyMethod(typeof(FraudOptimizer), "SetupTranspiler", (Type[])null), (HarmonyMethod)null, (HarmonyMethod)null);
		}
		catch (Exception ex)
		{
			((BaseUnityPlugin)this).Logger.LogError((object)("Fraud Optimizer: could not patch Setup: " + ex));
		}
	}

	private void BuildConfigMenu()
	{
		//IL_0027: Unknown result type (might be due to invalid IL or missing references)
		//IL_002d: 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_0072: Unknown result type (might be due to invalid IL or missing references)
		//IL_0078: Expected O, but got Unknown
		//IL_00a4: Unknown result type (might be due to invalid IL or missing references)
		//IL_00ae: Expected O, but got Unknown
		//IL_00b6: Unknown result type (might be due to invalid IL or missing references)
		//IL_00c0: Expected O, but got Unknown
		//IL_00c8: Unknown result type (might be due to invalid IL or missing references)
		//IL_00d2: Expected O, but got Unknown
		PluginConfigurator obj = PluginConfigurator.Create("Fraud Optimizer", "fraud.optimizer.guid");
		IntField val = new IntField(obj.rootPanel, "Max recursion depth (vanilla 4)", "max_recursion_depth", maxRecursionDepth, 1, 4);
		FloatField val2 = new FloatField(obj.rootPanel, "Max portal distance", "max_portal_distance", maxPortalDistance, 1f, 1000f);
		FloatField val3 = new FloatField(obj.rootPanel, "Min portal pixel area", "min_portal_pixel_area", minPortalPixelArea, 0f, 100000f);
		maxRecursionDepth = val.value;
		maxPortalDistance = val2.value;
		minPortalPixelArea = val3.value;
		val.onValueChange += (IntValueChangeEventDelegate)delegate(IntValueChangeEvent e)
		{
			maxRecursionDepth = e.value;
			Refresh();
		};
		val2.onValueChange += (FloatValueChangeEventDelegate)delegate(FloatValueChangeEvent e)
		{
			maxPortalDistance = e.value;
			Refresh();
		};
		val3.onValueChange += (FloatValueChangeEventDelegate)delegate(FloatValueChangeEvent e)
		{
			minPortalPixelArea = e.value;
			Refresh();
		};
	}

	private void Refresh()
	{
		activeMaxDepth = Mathf.Clamp(maxRecursionDepth, 1, 4);
		activeMaxDistance = Mathf.Max(1f, maxPortalDistance);
		activeMinPixelArea = Mathf.Max(0f, minPortalPixelArea);
		reportPending = true;
	}

	public static float ClampDistance(float original)
	{
		float result = Mathf.Min(original, activeMaxDistance);
		if (reportPending)
		{
			reportPending = false;
			((BaseUnityPlugin)Instance).Logger.LogInfo((object)("Setup running: maxDistance " + original + " -> " + result + ", depth " + activeMaxDepth + ", minPixelArea " + activeMinPixelArea));
		}
		return result;
	}

	public static int GetMaxDepth()
	{
		return activeMaxDepth;
	}

	public static float AdjustMinPixelArea(float original)
	{
		return Mathf.Max(original, activeMinPixelArea);
	}

	public static IEnumerable<CodeInstruction> SetupTranspiler(IEnumerable<CodeInstruction> instructions)
	{
		//IL_00b8: Unknown result type (might be due to invalid IL or missing references)
		//IL_00c2: Expected O, but got Unknown
		//IL_00cd: Unknown result type (might be due to invalid IL or missing references)
		//IL_00d7: Expected O, but got Unknown
		//IL_00e3: Unknown result type (might be due to invalid IL or missing references)
		//IL_00ed: Expected O, but got Unknown
		//IL_00f8: Unknown result type (might be due to invalid IL or missing references)
		//IL_0102: Expected O, but got Unknown
		//IL_010e: Unknown result type (might be due to invalid IL or missing references)
		//IL_0118: Expected O, but got Unknown
		//IL_01d8: Unknown result type (might be due to invalid IL or missing references)
		//IL_01e2: Expected O, but got Unknown
		List<CodeInstruction> list = new List<CodeInstruction>(instructions);
		MethodInfo methodInfo = AccessTools.Method(typeof(FraudOptimizer), "ClampDistance", (Type[])null, (Type[])null);
		MethodInfo operand = AccessTools.Method(typeof(FraudOptimizer), "GetMaxDepth", (Type[])null, (Type[])null);
		MethodInfo methodInfo2 = AccessTools.Method(typeof(FraudOptimizer), "AdjustMinPixelArea", (Type[])null, (Type[])null);
		int num = 0;
		int num2 = 0;
		int num3 = 0;
		for (int i = 0; i < list.Count; i++)
		{
			FieldInfo fieldInfo = list[i].operand as FieldInfo;
			if (!(fieldInfo == null))
			{
				if (list[i].opcode == OpCodes.Stfld && fieldInfo.Name == "maxDistance")
				{
					list.Insert(i + 1, new CodeInstruction(OpCodes.Ldarg_0, (object)null));
					list.Insert(i + 2, new CodeInstruction(OpCodes.Ldarg_0, (object)null));
					list.Insert(i + 3, new CodeInstruction(OpCodes.Ldfld, (object)fieldInfo));
					list.Insert(i + 4, new CodeInstruction(OpCodes.Call, (object)methodInfo));
					list.Insert(i + 5, new CodeInstruction(OpCodes.Stfld, (object)fieldInfo));
					i += 5;
					num++;
				}
				else if (list[i].opcode == OpCodes.Stfld && fieldInfo.Name == "maxDepth" && i > 0 && list[i - 1].opcode == OpCodes.Ldc_I4_4)
				{
					list[i - 1].opcode = OpCodes.Call;
					list[i - 1].operand = operand;
					num2++;
				}
				else if (list[i].opcode == OpCodes.Ldfld && fieldInfo.Name == "minPortalPixelArea")
				{
					list.Insert(i + 1, new CodeInstruction(OpCodes.Call, (object)methodInfo2));
					i++;
					num3++;
				}
			}
		}
		((BaseUnityPlugin)Instance).Logger.LogInfo((object)("Patched Setup: distance=" + num + ", depth=" + num2 + ", pixelArea=" + num3));
		if (num == 0 || num2 == 0 || num3 == 0)
		{
			((BaseUnityPlugin)Instance).Logger.LogWarning((object)"Some portal optimizations were not applied. The game may have been updated.");
		}
		return list;
	}

	private void OnDestroy()
	{
		if (harmony != null)
		{
			harmony.UnpatchSelf();
		}
	}
}