Decompiled source of LethalPerformance v0.0.5

BepInEx/plugins/LethalPerformance/LethalPerformance.dll

Decompiled 21 hours ago
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.IO;
using System.Linq;
using System.Reflection;
using System.Reflection.Emit;
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 LethalPerformance.API;
using Microsoft.CodeAnalysis;
using Unity.Burst;
using Unity.Burst.LowLevel;
using Unity.Collections;
using Unity.Mathematics;
using UnityEngine;
using UnityEngine.Rendering.HighDefinition;

[assembly: CompilationRelaxations(8)]
[assembly: RuntimeCompatibility(WrapNonExceptionThrows = true)]
[assembly: Debuggable(DebuggableAttribute.DebuggingModes.IgnoreSymbolStoreSequencePoints)]
[assembly: IgnoresAccessChecksTo("Unity.RenderPipelines.HighDefinition.Runtime")]
[assembly: TargetFramework(".NETStandard,Version=v2.1", FrameworkDisplayName = ".NET Standard 2.1")]
[assembly: AssemblyCompany("LethalPerformance")]
[assembly: AssemblyConfiguration("Release")]
[assembly: AssemblyFileVersion("0.0.5.0")]
[assembly: AssemblyInformationalVersion("0.0.5+abb64a346536d5ea2d28c148c6d6066576f03090")]
[assembly: AssemblyProduct("LethalPerformance")]
[assembly: AssemblyTitle("LethalPerformance")]
[assembly: SecurityPermission(SecurityAction.RequestMinimum, SkipVerification = true)]
[assembly: AssemblyVersion("0.0.5.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.Class | AttributeTargets.Property | AttributeTargets.Field | AttributeTargets.Event | AttributeTargets.Parameter | AttributeTargets.ReturnValue | AttributeTargets.GenericParameter, AllowMultiple = false, Inherited = false)]
	internal sealed class NullableAttribute : Attribute
	{
		public readonly byte[] NullableFlags;

		public NullableAttribute(byte P_0)
		{
			NullableFlags = new byte[1] { P_0 };
		}

		public NullableAttribute(byte[] P_0)
		{
			NullableFlags = P_0;
		}
	}
	[CompilerGenerated]
	[Microsoft.CodeAnalysis.Embedded]
	[AttributeUsage(AttributeTargets.Class | AttributeTargets.Struct | AttributeTargets.Method | AttributeTargets.Interface | AttributeTargets.Delegate, AllowMultiple = false, Inherited = false)]
	internal sealed class NullableContextAttribute : Attribute
	{
		public readonly byte Flag;

		public NullableContextAttribute(byte P_0)
		{
			Flag = P_0;
		}
	}
	[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 LethalPerformance
{
	internal class ConfigManager
	{
		private readonly ConfigFile m_Config;

		public ConfigManager(ConfigFile config)
		{
			m_Config = config;
			BindConfig();
		}

		private void BindConfig()
		{
		}
	}
	[BepInPlugin("LethalPerformance", "LethalPerformance", "0.0.5")]
	public class LethalPerformancePlugin : BaseUnityPlugin
	{
		private Harmony? m_Harmony;

		public static LethalPerformancePlugin Instance { get; private set; }

		internal ManualLogSource Logger { get; private set; }

		internal string WorkingDirectory { get; private set; }

		internal ConfigManager Config { get; private set; }

		private void Awake()
		{
			//IL_0050: Unknown result type (might be due to invalid IL or missing references)
			//IL_005a: Expected O, but got Unknown
			Instance = this;
			Logger = ((BaseUnityPlugin)this).Logger;
			WorkingDirectory = new FileInfo(((BaseUnityPlugin)this).Info.Location).DirectoryName;
			Config = new ConfigManager(((BaseUnityPlugin)this).Config);
			LoadGameBurstLib();
			CallInitializeOnAwake();
			m_Harmony = new Harmony("LethalPerformance");
			m_Harmony.PatchAll(typeof(LethalPerformancePlugin).Assembly);
		}

		private void CallInitializeOnAwake()
		{
			foreach (MethodInfo item in from m in typeof(LethalPerformancePlugin).Assembly.GetTypes().SelectMany((Type t) => t.GetMethods(BindingFlags.Static | BindingFlags.Public | BindingFlags.NonPublic))
				where m.GetCustomAttribute<InitializeOnAwakeAttribute>() != null
				select m)
			{
				item.Invoke(null, null);
				Logger.LogInfo((object)("Initialized " + GeneralExtensions.FullDescription((MethodBase)item)));
			}
		}

		private void LoadGameBurstLib()
		{
			string text = Path.Combine(WorkingDirectory, "lib_burst_generated.data");
			if (!File.Exists(text))
			{
				Logger.LogFatal((object)"Failed to find \"lib_burst_generated.data\"");
			}
			else if (!BurstCompilerService.LoadBurstLibrary(text))
			{
				Logger.LogFatal((object)"Failed to load burst library. Probably machine architecture is not x64 or CPU doesn't support AVX2 and SSE2 instructions");
			}
		}
	}
	[BurstCompile]
	public static class Testing
	{
		public unsafe delegate void TestDelegate(in ReadableViewConstants* xrViewConstants, in int viewCount, ReadableShaderVariablesXR* cb);

		public unsafe delegate void UpdateShaderVariablesGlobalCBDelegate(in ReadableShaderVariablesGlobal* cb, in int frameCount, in bool isTaaAntialiasingEnabled, in ViewConstants* mainViewConstant, in float4x4 vectorScreens, in float4x3 vectorScales, in float4x4 vectorParams, in float4* frustumPlaneEquations, in float taaSharpenStrength, in int taaFrameIndex, in float4 taaJitter, in int colorPyramidHistoryMipCount, in float globalMipBias, in float4 timeParams, in int viewCount, in float probeRangeCompressionFactor, in float deExposureMultiplier, in int transparentCameraOnlyMotionVectors, in float4 screenSizeOverride, in float4 screenCoordScaleBias);

		public struct ReadableViewConstants
		{
			public Matrix4x4 viewMatrix;

			public Matrix4x4 invViewMatrix;

			public Matrix4x4 projMatrix;

			public Matrix4x4 invProjMatrix;

			public Matrix4x4 viewProjMatrix;

			public Matrix4x4 invViewProjMatrix;

			public Matrix4x4 nonJitteredViewProjMatrix;

			public Matrix4x4 prevViewMatrix;

			public Matrix4x4 prevViewProjMatrix;

			public Matrix4x4 prevInvViewProjMatrix;

			public Matrix4x4 prevViewProjMatrixNoCameraTrans;

			public Matrix4x4 pixelCoordToViewDirWS;

			public Matrix4x4 viewProjectionNoCameraTrans;

			public Vector3 worldSpaceCameraPos;

			internal float pad0;

			public Vector3 worldSpaceCameraPosViewOffset;

			internal float pad1;

			public Vector3 prevWorldSpaceCameraPos;

			internal float pad2;
		}

		public struct ReadableShaderVariablesXR
		{
			public unsafe fixed float _XRViewMatrix[32];

			public unsafe fixed float _XRInvViewMatrix[32];

			public unsafe fixed float _XRProjMatrix[32];

			public unsafe fixed float _XRInvProjMatrix[32];

			public unsafe fixed float _XRViewProjMatrix[32];

			public unsafe fixed float _XRInvViewProjMatrix[32];

			public unsafe fixed float _XRNonJitteredViewProjMatrix[32];

			public unsafe fixed float _XRPrevViewProjMatrix[32];

			public unsafe fixed float _XRPrevInvViewProjMatrix[32];

			public unsafe fixed float _XRPrevViewProjMatrixNoCameraTrans[32];

			public unsafe fixed float _XRViewProjMatrixNoCameraTrans[32];

			public unsafe fixed float _XRPixelCoordToViewDirWS[32];

			public unsafe fixed float _XRWorldSpaceCameraPos[8];

			public unsafe fixed float _XRWorldSpaceCameraPosViewOffset[8];

			public unsafe fixed float _XRPrevWorldSpaceCameraPos[8];
		}

		public struct ReadableShaderVariablesGlobal
		{
			public Matrix4x4 _ViewMatrix;

			public Matrix4x4 _CameraViewMatrix;

			public Matrix4x4 _InvViewMatrix;

			public Matrix4x4 _ProjMatrix;

			public Matrix4x4 _InvProjMatrix;

			public Matrix4x4 _ViewProjMatrix;

			public Matrix4x4 _CameraViewProjMatrix;

			public Matrix4x4 _InvViewProjMatrix;

			public Matrix4x4 _NonJitteredViewProjMatrix;

			public Matrix4x4 _PrevViewProjMatrix;

			public Matrix4x4 _PrevInvViewProjMatrix;

			public Vector4 _WorldSpaceCameraPos_Internal;

			public Vector4 _PrevCamPosRWS_Internal;

			public Vector4 _ScreenSize;

			public Vector4 _PostProcessScreenSize;

			public Vector4 _RTHandleScale;

			public Vector4 _RTHandleScaleHistory;

			public Vector4 _RTHandlePostProcessScale;

			public Vector4 _RTHandlePostProcessScaleHistory;

			public Vector4 _DynamicResolutionFullscreenScale;

			public Vector4 _ZBufferParams;

			public Vector4 _ProjectionParams;

			public Vector4 unity_OrthoParams;

			public Vector4 _ScreenParams;

			public unsafe fixed float _FrustumPlanes[24];

			public Vector4 _TaaFrameInfo;

			public Vector4 _TaaJitterStrength;

			public Vector4 _Time;

			public Vector4 _SinTime;

			public Vector4 _CosTime;

			public Vector4 unity_DeltaTime;

			public Vector4 _TimeParameters;

			public Vector4 _LastTimeParameters;

			public int _FogEnabled;

			public int _PBRFogEnabled;

			public int _EnableVolumetricFog;

			public float _MaxFogDistance;

			public Vector4 _FogColor;

			public float _FogColorMode;

			public float _GlobalMipBias;

			public float _GlobalMipBiasPow2;

			public float _Pad0;

			public Vector4 _MipFogParameters;

			public Vector4 _HeightFogBaseScattering;

			public float _HeightFogBaseExtinction;

			public float _HeightFogBaseHeight;

			public float _GlobalFogAnisotropy;

			public int _VolumetricFilteringEnabled;

			public Vector2 _HeightFogExponents;

			public int _FogDirectionalOnly;

			public float _FogGIDimmer;

			public Vector4 _VBufferViewportSize;

			public Vector4 _VBufferLightingViewportScale;

			public Vector4 _VBufferLightingViewportLimit;

			public Vector4 _VBufferDistanceEncodingParams;

			public Vector4 _VBufferDistanceDecodingParams;

			public uint _VBufferSliceCount;

			public float _VBufferRcpSliceCount;

			public float _VBufferRcpInstancedViewCount;

			public float _VBufferLastSliceDist;

			public Vector4 _ShadowAtlasSize;

			public Vector4 _CascadeShadowAtlasSize;

			public Vector4 _AreaShadowAtlasSize;

			public Vector4 _CachedShadowAtlasSize;

			public Vector4 _CachedAreaShadowAtlasSize;

			public int _ReflectionsMode;

			public int _UnusedPadding0;

			public int _UnusedPadding1;

			public int _UnusedPadding2;

			public uint _DirectionalLightCount;

			public uint _PunctualLightCount;

			public uint _AreaLightCount;

			public uint _EnvLightCount;

			public int _EnvLightSkyEnabled;

			public uint _CascadeShadowCount;

			public int _DirectionalShadowIndex;

			public uint _EnableLightLayers;

			public uint _EnableSkyReflection;

			public uint _EnableSSRefraction;

			public float _SSRefractionInvScreenWeightDistance;

			public float _ColorPyramidLodCount;

			public float _DirectionalTransmissionMultiplier;

			public float _ProbeExposureScale;

			public float _ContactShadowOpacity;

			public float _ReplaceDiffuseForIndirect;

			public Vector4 _AmbientOcclusionParam;

			public float _IndirectDiffuseLightingMultiplier;

			public uint _IndirectDiffuseLightingLayers;

			public float _ReflectionLightingMultiplier;

			public uint _ReflectionLightingLayers;

			public float _MicroShadowOpacity;

			public uint _EnableProbeVolumes;

			public uint _ProbeVolumeCount;

			public float _SlopeScaleDepthBias;

			public Vector4 _CookieAtlasSize;

			public Vector4 _CookieAtlasData;

			public Vector4 _ReflectionAtlasCubeData;

			public Vector4 _ReflectionAtlasPlanarData;

			public uint _NumTileFtplX;

			public uint _NumTileFtplY;

			public float g_fClustScale;

			public float g_fClustBase;

			public float g_fNearPlane;

			public float g_fFarPlane;

			public int g_iLog2NumClusters;

			public uint g_isLogBaseBufferEnabled;

			public uint _NumTileClusteredX;

			public uint _NumTileClusteredY;

			public int _EnvSliceSize;

			public uint _EnableDecalLayers;

			public unsafe fixed float _ShapeParamsAndMaxScatterDists[64];

			public unsafe fixed float _TransmissionTintsAndFresnel0[64];

			public unsafe fixed float _WorldScalesAndFilterRadiiAndThicknessRemaps[64];

			public unsafe fixed float _DiffusionProfileHashTable[64];

			public uint _EnableSubsurfaceScattering;

			public uint _TexturingModeFlags;

			public uint _TransmissionFlags;

			public uint _DiffusionProfileCount;

			public Vector2 _DecalAtlasResolution;

			public uint _EnableDecals;

			public uint _DecalCount;

			public float _OffScreenDownsampleFactor;

			public uint _OffScreenRendering;

			public uint _XRViewCount;

			public int _FrameCount;

			public Vector4 _CoarseStencilBufferSize;

			public int _IndirectDiffuseMode;

			public int _EnableRayTracedReflections;

			public int _RaytracingFrameIndex;

			public uint _EnableRecursiveRayTracing;

			public int _TransparentCameraOnlyMotionVectors;

			public float _GlobalTessellationFactorMultiplier;

			public float _SpecularOcclusionBlend;

			public float _DeExposureMultiplier;

			public Vector4 _ScreenSizeOverride;

			public Vector4 _ScreenCoordScaleBias;
		}

		[BurstCompile]
		public static void Log(in FixedString128Bytes log)
		{
		}

		[BurstCompile]
		public unsafe static void Test(in ReadableViewConstants* xrViewConstants, in int viewCount, ReadableShaderVariablesXR* cb)
		{
		}

		[BurstCompile]
		public unsafe static void UpdateShaderVariablesGlobalCB(in ReadableShaderVariablesGlobal* cb, in int frameCount, in bool isTaaAntialiasingEnabled, in ViewConstants* mainViewConstant, in float4x4 vectorScreens, in float4x3 vectorScales, in float4x4 vectorParams, in float4* frustumPlaneEquations, in float taaSharpenStrength, in int taaFrameIndex, in float4 taaJitter, in int colorPyramidHistoryMipCount, in float globalMipBias, in float4 timeParams, in int viewCount, in float probeRangeCompressionFactor, in float deExposureMultiplier, in int transparentCameraOnlyMotionVectors, in float4 screenSizeOverride, in float4 screenCoordScaleBias)
		{
		}
	}
	public static class MyPluginInfo
	{
		public const string PLUGIN_GUID = "LethalPerformance";

		public const string PLUGIN_NAME = "LethalPerformance";

		public const string PLUGIN_VERSION = "0.0.5";
	}
}
namespace LethalPerformance.Patches
{
	[HarmonyPatch(typeof(FoliageDetailDistance))]
	internal static class Patch_FoliageDetailDistance
	{
		[HarmonyCleanup]
		public static Exception? Cleanup(Exception exception)
		{
			return HarmonyExceptionHandler.ReportException(exception);
		}

		[HarmonyPatch("Start")]
		[HarmonyTranspiler]
		public static IEnumerable<CodeInstruction> ReplaceFindObjectOfType(IEnumerable<CodeInstruction> codeInstructions)
		{
			//IL_0008: Unknown result type (might be due to invalid IL or missing references)
			//IL_000e: Expected O, but got Unknown
			CodeMatcher val = new CodeMatcher(codeInstructions, (ILGenerator)null);
			MethodInfo findObjectOfType = typeof(Object).GetMethod("FindObjectOfType", 1, AccessTools.all, null, CallingConventions.Any, Array.Empty<Type>(), Array.Empty<ParameterModifier>()).MakeGenericMethod(typeof(StartOfRound));
			val.SearchForward((Func<CodeInstruction, bool>)((CodeInstruction c) => CodeInstructionExtensions.Calls(c, findObjectOfType))).ThrowIfInvalid("Failed to find call of FindObjectOfType<StartOfRound>").Operand = typeof(StartOfRound).GetProperty("Instance", AccessTools.all).GetGetMethod();
			return val.InstructionEnumeration();
		}
	}
	[HarmonyPatch(typeof(HDCamera))]
	internal static class Patch_HDCamera
	{
		private unsafe static readonly Testing.TestDelegate s_TestDelegate = BurstCompiler.CompileFunctionPointer<Testing.TestDelegate>((Testing.TestDelegate)Testing.Test).Invoke;

		[HarmonyCleanup]
		public static Exception? Cleanup(Exception exception)
		{
			return HarmonyExceptionHandler.ReportException(exception);
		}

		[HarmonyPrefix]
		[HarmonyPatch("UpdateShaderVariablesXRCB")]
		public unsafe static bool Prefix(HDCamera __instance, ref ShaderVariablesXR cb)
		{
			fixed (ShaderVariablesXR* ptr = &cb)
			{
				void* cb2 = ptr;
				fixed (ViewConstants* ptr2 = __instance.m_XRViewConstants)
				{
					void* ptr3 = ptr2;
					Testing.TestDelegate testDelegate = s_TestDelegate;
					Testing.ReadableViewConstants* xrViewConstants = (Testing.ReadableViewConstants*)ptr3;
					int viewCount = __instance.viewCount;
					testDelegate(in xrViewConstants, in viewCount, (Testing.ReadableShaderVariablesXR*)cb2);
					return false;
				}
			}
		}
	}
	[HarmonyPatch(typeof(ItemDropship))]
	internal static class Patch_ItemDropship
	{
		private static readonly FieldInfo s_RopesField = typeof(ItemDropship).GetField("ropes", AccessTools.all);

		[HarmonyPrepare]
		public static bool ShouldPatch()
		{
			return s_RopesField != null;
		}

		[HarmonyCleanup]
		public static Exception? Cleanup(Exception exception)
		{
			return HarmonyExceptionHandler.ReportException(exception);
		}

		[HarmonyTranspiler]
		[HarmonyPatch("Update")]
		public static IEnumerable<CodeInstruction> FixAccessingNullArray(IEnumerable<CodeInstruction> codeInstructions)
		{
			//IL_0002: Unknown result type (might be due to invalid IL or missing references)
			//IL_0008: Expected O, but got Unknown
			//IL_0082: Unknown result type (might be due to invalid IL or missing references)
			//IL_0088: Expected O, but got Unknown
			//IL_0094: Unknown result type (might be due to invalid IL or missing references)
			//IL_009a: Expected O, but got Unknown
			//IL_00a7: Unknown result type (might be due to invalid IL or missing references)
			//IL_00ad: Expected O, but got Unknown
			CodeMatcher val = new CodeMatcher(codeInstructions, (ILGenerator)null);
			int pos = val.SearchForward((Func<CodeInstruction, bool>)((CodeInstruction c) => CodeInstructionExtensions.LoadsField(c, s_RopesField, false))).Pos;
			Label label = (Label)val.SearchForward((Func<CodeInstruction, bool>)((CodeInstruction c) => c.operand is Label)).Operand;
			val.Start().Advance(pos - 1);
			val.Insert((CodeInstruction[])(object)new CodeInstruction[3]
			{
				new CodeInstruction(OpCodes.Ldarg_0, (object)null),
				new CodeInstruction(OpCodes.Ldfld, (object)s_RopesField),
				new CodeInstruction(OpCodes.Brfalse_S, (object)label)
			});
			return val.InstructionEnumeration();
		}
	}
}
namespace LethalPerformance.API
{
	internal static class HarmonyExceptionHandler
	{
		public static Exception? ReportException(Exception? exception)
		{
			if (exception != null)
			{
				LethalPerformancePlugin.Instance.Logger.LogWarning((object)exception);
			}
			return null;
		}
	}
	[AttributeUsage(AttributeTargets.Method)]
	internal sealed class InitializeOnAwakeAttribute : Attribute
	{
	}
}
namespace System.Runtime.CompilerServices
{
	[AttributeUsage(AttributeTargets.Assembly, AllowMultiple = true)]
	internal sealed class IgnoresAccessChecksToAttribute : Attribute
	{
		public IgnoresAccessChecksToAttribute(string assemblyName)
		{
		}
	}
}