Decompiled source of DSPSwarmDrawFix v1.1.0

DSPSwarmDrawFix.dll

Decompiled 3 days ago
using System;
using System.Diagnostics;
using System.Reflection;
using System.Runtime.CompilerServices;
using System.Runtime.Versioning;
using BepInEx;
using BepInEx.Logging;
using HarmonyLib;
using UnityEngine;
using UnityEngine.Rendering;

[assembly: CompilationRelaxations(8)]
[assembly: RuntimeCompatibility(WrapNonExceptionThrows = true)]
[assembly: Debuggable(DebuggableAttribute.DebuggingModes.IgnoreSymbolStoreSequencePoints)]
[assembly: TargetFramework(".NETFramework,Version=v4.7.2", FrameworkDisplayName = ".NET Framework 4.7.2")]
[assembly: AssemblyVersion("0.0.0.0")]
namespace DSPSwarmDrawFix;

[BepInPlugin("me.gp.dspswarmdrawfix", "DSPSwarmDrawFix", "1.1.0")]
public class SwarmDrawFixPlugin : BaseUnityPlugin
{
	internal static ManualLogSource Log;

	internal static readonly FieldInfo SwarmBufferField = AccessTools.Field(typeof(DysonSwarm), "swarmBuffer");

	internal static readonly FieldInfo SwarmInfoBufferField = AccessTools.Field(typeof(DysonSwarm), "swarmInfoBuffer");

	internal static readonly FieldInfo OrbitColorsBufferField = AccessTools.Field(typeof(DysonSwarm), "sailOrbitColorsBuffer");

	internal static readonly FieldInfo BulletBufferField = AccessTools.Field(typeof(DysonSwarm), "bulletBuffer");

	internal static readonly FieldInfo SizeInMatField = AccessTools.Field(typeof(DysonSwarm), "sizeInMat");

	private void Awake()
	{
		//IL_0010: Unknown result type (might be due to invalid IL or missing references)
		//IL_0015: Unknown result type (might be due to invalid IL or missing references)
		Log = ((BaseUnityPlugin)this).Logger;
		Harmony val = new Harmony("me.gp.dspswarmdrawfix");
		val.PatchAll(typeof(SwarmDrawPatch));
		val.PatchAll(typeof(NearDrawPatch));
		Log.LogInfo((object)"DSPSwarmDrawFix активен: дальние паруса — индексированные треугольники, ближние — instanced без indirect");
	}
}
internal static class SwarmDrawPatch
{
	private const int ChunkSails = 5460;

	private static GraphicsBuffer quadIndexBuffer;

	private static bool failedOnce;

	private static GraphicsBuffer GetIndexBuffer()
	{
		//IL_0072: Unknown result type (might be due to invalid IL or missing references)
		//IL_007c: Expected O, but got Unknown
		if (quadIndexBuffer != null && quadIndexBuffer.IsValid())
		{
			return quadIndexBuffer;
		}
		int num = 16380;
		int[] array = new int[num * 6];
		for (int i = 0; i < num; i++)
		{
			int num2 = i * 4;
			int num3 = i * 6;
			array[num3] = num2;
			array[num3 + 1] = num2 + 1;
			array[num3 + 2] = num2 + 2;
			array[num3 + 3] = num2;
			array[num3 + 4] = num2 + 2;
			array[num3 + 5] = num2 + 3;
		}
		quadIndexBuffer = new GraphicsBuffer((Target)2, array.Length, 4);
		quadIndexBuffer.SetData((Array)array);
		return quadIndexBuffer;
	}

	[HarmonyPrefix]
	[HarmonyPatch(typeof(DysonSwarm), "DrawPost")]
	private static bool DrawPostPrefix(DysonSwarm __instance)
	{
		try
		{
			ReplacementDrawPost(__instance);
			return false;
		}
		catch (Exception arg)
		{
			if (!failedOnce)
			{
				failedOnce = true;
				SwarmDrawFixPlugin.Log.LogError((object)$"замена DrawPost упала, откат на оригинал: {arg}");
			}
			return true;
		}
	}

	private static void ReplacementDrawPost(DysonSwarm sw)
	{
		//IL_000a: Unknown result type (might be due to invalid IL or missing references)
		//IL_000f: Unknown result type (might be due to invalid IL or missing references)
		//IL_002b: Unknown result type (might be due to invalid IL or missing references)
		//IL_0030: Unknown result type (might be due to invalid IL or missing references)
		//IL_0130: Unknown result type (might be due to invalid IL or missing references)
		//IL_0136: Invalid comparison between Unknown and I4
		//IL_015e: Unknown result type (might be due to invalid IL or missing references)
		//IL_0164: Invalid comparison between Unknown and I4
		//IL_0067: Unknown result type (might be due to invalid IL or missing references)
		//IL_006c: Unknown result type (might be due to invalid IL or missing references)
		//IL_00e0: Unknown result type (might be due to invalid IL or missing references)
		//IL_00e4: Unknown result type (might be due to invalid IL or missing references)
		//IL_00e9: Unknown result type (might be due to invalid IL or missing references)
		//IL_00ee: Unknown result type (might be due to invalid IL or missing references)
		//IL_0072: Unknown result type (might be due to invalid IL or missing references)
		//IL_0076: Unknown result type (might be due to invalid IL or missing references)
		//IL_007b: Unknown result type (might be due to invalid IL or missing references)
		//IL_0080: Unknown result type (might be due to invalid IL or missing references)
		//IL_0084: Unknown result type (might be due to invalid IL or missing references)
		//IL_0089: Unknown result type (might be due to invalid IL or missing references)
		//IL_008b: Unknown result type (might be due to invalid IL or missing references)
		//IL_0090: Unknown result type (might be due to invalid IL or missing references)
		//IL_0092: Unknown result type (might be due to invalid IL or missing references)
		//IL_0096: Unknown result type (might be due to invalid IL or missing references)
		//IL_009b: Unknown result type (might be due to invalid IL or missing references)
		//IL_00a0: Unknown result type (might be due to invalid IL or missing references)
		//IL_00a5: Unknown result type (might be due to invalid IL or missing references)
		//IL_01f3: Unknown result type (might be due to invalid IL or missing references)
		//IL_01fa: Expected O, but got Unknown
		//IL_0205: Unknown result type (might be due to invalid IL or missing references)
		//IL_020c: Expected O, but got Unknown
		//IL_0217: Unknown result type (might be due to invalid IL or missing references)
		//IL_021e: Expected O, but got Unknown
		//IL_0229: Unknown result type (might be due to invalid IL or missing references)
		//IL_0230: Expected O, but got Unknown
		//IL_0289: Unknown result type (might be due to invalid IL or missing references)
		//IL_028a: Unknown result type (might be due to invalid IL or missing references)
		//IL_029a: Unknown result type (might be due to invalid IL or missing references)
		//IL_029b: Unknown result type (might be due to invalid IL or missing references)
		//IL_02ab: Unknown result type (might be due to invalid IL or missing references)
		//IL_00f0: Unknown result type (might be due to invalid IL or missing references)
		//IL_00f2: Unknown result type (might be due to invalid IL or missing references)
		//IL_00f7: Unknown result type (might be due to invalid IL or missing references)
		//IL_00f8: Unknown result type (might be due to invalid IL or missing references)
		//IL_00fe: Invalid comparison between Unknown and I4
		//IL_0106: Unknown result type (might be due to invalid IL or missing references)
		//IL_010b: Unknown result type (might be due to invalid IL or missing references)
		//IL_0110: Unknown result type (might be due to invalid IL or missing references)
		//IL_011e: Unknown result type (might be due to invalid IL or missing references)
		//IL_0123: Unknown result type (might be due to invalid IL or missing references)
		//IL_0128: Unknown result type (might be due to invalid IL or missing references)
		uint num = (uint)(GameMain.gameTick & 0xFFFFFFFFu);
		Vector3 val = Vector3.zero;
		Vector4 val2 = default(Vector4);
		((Vector4)(ref val2))..ctor(0f, 0f, 0f, 1f);
		Vector3 val3 = Vector3.zero;
		if (sw.starData != null && sw.gameData != null)
		{
			PlanetData localPlanet = sw.gameData.localPlanet;
			Player mainPlayer = sw.gameData.mainPlayer;
			VectorLF3 uPosition = sw.starData.uPosition;
			if (localPlanet != null)
			{
				uPosition -= mainPlayer.uPosition;
				uPosition = Maths.QInvRotateLF(localPlanet.runtimeRotation, uPosition);
				uPosition += VectorLF3.op_Implicit(mainPlayer.position);
				((Vector4)(ref val2))..ctor(localPlanet.runtimeRotation.x, localPlanet.runtimeRotation.y, localPlanet.runtimeRotation.z, localPlanet.runtimeRotation.w);
			}
			else
			{
				uPosition -= mainPlayer.uPosition;
			}
			val = VectorLF3.op_Implicit(uPosition);
			if ((int)DysonSphere.renderPlace == 1)
			{
				val3 = VectorLF3.op_Implicit((sw.starData.uPosition - UIStarmap.viewTargetStatic) * 0.00025);
			}
		}
		Camera val4 = Camera.main;
		if ((int)DysonSphere.renderPlace == 1)
		{
			UIStarmap starmap = UIRoot.instance.uiGame.starmap;
			if ((Object)(object)starmap != (Object)null)
			{
				val4 = starmap.screenCamera;
			}
		}
		else if ((int)DysonSphere.renderPlace == 2)
		{
			UIDysonEditor dysonEditor = UIRoot.instance.uiGame.dysonEditor;
			if ((Object)(object)dysonEditor != (Object)null)
			{
				val4 = dysonEditor.screenCamera;
			}
		}
		if ((Object)(object)val4 != (Object)null)
		{
			float num2 = (float)SwarmDrawFixPlugin.SizeInMatField.GetValue(sw) * (float)Screen.height * 0.4f;
			float num3 = 1f / Mathf.Tan(val4.fieldOfView * ((float)Math.PI / 180f) * 0.5f) * num2;
			sw.sailFarMaterial.SetFloat("_DistScalePoint", num3);
		}
		ComputeBuffer val5 = (ComputeBuffer)SwarmDrawFixPlugin.SwarmBufferField.GetValue(sw);
		ComputeBuffer val6 = (ComputeBuffer)SwarmDrawFixPlugin.SwarmInfoBufferField.GetValue(sw);
		ComputeBuffer val7 = (ComputeBuffer)SwarmDrawFixPlugin.OrbitColorsBufferField.GetValue(sw);
		ComputeBuffer val8 = (ComputeBuffer)SwarmDrawFixPlugin.BulletBufferField.GetValue(sw);
		Material sailFarMaterial = sw.sailFarMaterial;
		sailFarMaterial.SetInt("_Stride", 5460);
		sailFarMaterial.SetBuffer("_SwarmBuffer", val5);
		sailFarMaterial.SetBuffer("_SwarmInfoBuffer", val6);
		sailFarMaterial.SetBuffer("_NodeBuffer", sw.dysonSphere.nrdBuffer);
		sailFarMaterial.SetBuffer("_OrbitColor", val7);
		sailFarMaterial.SetVector("_SunPosition", Vector4.op_Implicit(val));
		sailFarMaterial.SetVector("_SunPosition_Map", Vector4.op_Implicit(val3));
		sailFarMaterial.SetVector("_LocalRot", val2);
		sailFarMaterial.SetInt("_GameTick", (int)num);
		sailFarMaterial.SetPass(0);
		if (sw.sailCursor > 0 && val5 != null)
		{
			int num4 = (sw.sailCursor + 5460 - 1) / 5460;
			Graphics.DrawProceduralNow((MeshTopology)0, GetIndexBuffer(), 98280, num4);
		}
		if (val8 != null)
		{
			sw.bulletMaterial.SetBuffer("_BulletBuffer", val8);
			sw.bulletMaterial.SetPass(0);
			Graphics.DrawProceduralNow((MeshTopology)2, sw.bulletCursor * 8, 1);
		}
	}
}
internal static class NearDrawPatch
{
	private static ComputeBuffer identityBuffer;

	private static bool failedOnce;

	private static ComputeBuffer GetIdentityBuffer(int minCount)
	{
		//IL_004a: Unknown result type (might be due to invalid IL or missing references)
		//IL_0054: Expected O, but got Unknown
		if (identityBuffer != null && identityBuffer.IsValid() && identityBuffer.count >= minCount)
		{
			return identityBuffer;
		}
		int num = Mathf.NextPowerOfTwo(Mathf.Max(minCount, 65536));
		ComputeBuffer obj = identityBuffer;
		if (obj != null)
		{
			obj.Release();
		}
		identityBuffer = new ComputeBuffer(num, 4, (ComputeBufferType)0);
		int[] array = new int[num];
		for (int i = 0; i < num; i++)
		{
			array[i] = i;
		}
		identityBuffer.SetData((Array)array);
		return identityBuffer;
	}

	[HarmonyPrefix]
	[HarmonyPatch(typeof(DysonSwarm), "DrawModel")]
	private static bool DrawModelPrefix(DysonSwarm __instance, ERenderPlace place, int editorMask, int gameMask)
	{
		try
		{
			ReplacementDrawModel(__instance, editorMask, gameMask);
			return false;
		}
		catch (Exception arg)
		{
			if (!failedOnce)
			{
				failedOnce = true;
				SwarmDrawFixPlugin.Log.LogError((object)$"замена DrawModel упала, откат на оригинал: {arg}");
			}
			return true;
		}
	}

	private static void ReplacementDrawModel(DysonSwarm sw, int editorMask, int gameMask)
	{
		//IL_0045: Unknown result type (might be due to invalid IL or missing references)
		//IL_004a: Unknown result type (might be due to invalid IL or missing references)
		//IL_0066: Unknown result type (might be due to invalid IL or missing references)
		//IL_006b: Unknown result type (might be due to invalid IL or missing references)
		//IL_008c: 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_0105: Unknown result type (might be due to invalid IL or missing references)
		//IL_0109: Unknown result type (might be due to invalid IL or missing references)
		//IL_010e: Unknown result type (might be due to invalid IL or missing references)
		//IL_0113: Unknown result type (might be due to invalid IL or missing references)
		//IL_0097: Unknown result type (might be due to invalid IL or missing references)
		//IL_009b: Unknown result type (might be due to invalid IL or missing references)
		//IL_00a0: Unknown result type (might be due to invalid IL or missing references)
		//IL_00a5: Unknown result type (might be due to invalid IL or missing references)
		//IL_00a9: Unknown result type (might be due to invalid IL or missing references)
		//IL_00ae: Unknown result type (might be due to invalid IL or missing references)
		//IL_00b0: Unknown result type (might be due to invalid IL or missing references)
		//IL_00b5: Unknown result type (might be due to invalid IL or missing references)
		//IL_00b7: Unknown result type (might be due to invalid IL or missing references)
		//IL_00bb: Unknown result type (might be due to invalid IL or missing references)
		//IL_00c0: Unknown result type (might be due to invalid IL or missing references)
		//IL_00c5: Unknown result type (might be due to invalid IL or missing references)
		//IL_00ca: Unknown result type (might be due to invalid IL or missing references)
		//IL_0115: Unknown result type (might be due to invalid IL or missing references)
		//IL_0117: Unknown result type (might be due to invalid IL or missing references)
		//IL_011c: Unknown result type (might be due to invalid IL or missing references)
		//IL_011d: Unknown result type (might be due to invalid IL or missing references)
		//IL_0123: Invalid comparison between Unknown and I4
		//IL_0159: Unknown result type (might be due to invalid IL or missing references)
		//IL_0160: Expected O, but got Unknown
		//IL_016b: Unknown result type (might be due to invalid IL or missing references)
		//IL_0172: Expected O, but got Unknown
		//IL_017d: Unknown result type (might be due to invalid IL or missing references)
		//IL_0184: Expected O, but got Unknown
		//IL_012b: Unknown result type (might be due to invalid IL or missing references)
		//IL_0130: Unknown result type (might be due to invalid IL or missing references)
		//IL_0135: Unknown result type (might be due to invalid IL or missing references)
		//IL_0143: Unknown result type (might be due to invalid IL or missing references)
		//IL_0148: Unknown result type (might be due to invalid IL or missing references)
		//IL_014d: Unknown result type (might be due to invalid IL or missing references)
		//IL_01f0: Unknown result type (might be due to invalid IL or missing references)
		//IL_01f1: Unknown result type (might be due to invalid IL or missing references)
		//IL_0202: Unknown result type (might be due to invalid IL or missing references)
		//IL_0203: Unknown result type (might be due to invalid IL or missing references)
		//IL_0214: Unknown result type (might be due to invalid IL or missing references)
		//IL_022e: Unknown result type (might be due to invalid IL or missing references)
		//IL_0238: Expected I4, but got Unknown
		//IL_0256: Unknown result type (might be due to invalid IL or missing references)
		//IL_025c: Invalid comparison between Unknown and I4
		//IL_0264: Unknown result type (might be due to invalid IL or missing references)
		//IL_026a: Invalid comparison between Unknown and I4
		//IL_027d: Unknown result type (might be due to invalid IL or missing references)
		//IL_0291: Unknown result type (might be due to invalid IL or missing references)
		//IL_0296: Unknown result type (might be due to invalid IL or missing references)
		if ((Object)(object)Configs.builtin.solarSailMesh == (Object)null || sw.starData == null || sw.gameData == null || (Object)(object)Camera.main == (Object)null || sw.sailCursor <= 1)
		{
			return;
		}
		uint num = (uint)(GameMain.gameTick & 0xFFFFFFFFu);
		Vector3 zero = Vector3.zero;
		Vector4 val = default(Vector4);
		((Vector4)(ref val))..ctor(0f, 0f, 0f, 1f);
		Vector3 val2 = Vector3.zero;
		PlanetData localPlanet = sw.gameData.localPlanet;
		Player mainPlayer = sw.gameData.mainPlayer;
		VectorLF3 uPosition = sw.starData.uPosition;
		if (localPlanet != null)
		{
			uPosition -= mainPlayer.uPosition;
			uPosition = Maths.QInvRotateLF(localPlanet.runtimeRotation, uPosition);
			uPosition += VectorLF3.op_Implicit(mainPlayer.position);
			((Vector4)(ref val))..ctor(localPlanet.runtimeRotation.x, localPlanet.runtimeRotation.y, localPlanet.runtimeRotation.z, localPlanet.runtimeRotation.w);
		}
		else
		{
			uPosition -= mainPlayer.uPosition;
		}
		zero = VectorLF3.op_Implicit(uPosition);
		if ((int)DysonSphere.renderPlace == 1)
		{
			val2 = VectorLF3.op_Implicit((sw.starData.uPosition - UIStarmap.viewTargetStatic) * 0.00025);
		}
		ComputeBuffer val3 = (ComputeBuffer)SwarmDrawFixPlugin.SwarmBufferField.GetValue(sw);
		ComputeBuffer val4 = (ComputeBuffer)SwarmDrawFixPlugin.SwarmInfoBufferField.GetValue(sw);
		ComputeBuffer val5 = (ComputeBuffer)SwarmDrawFixPlugin.OrbitColorsBufferField.GetValue(sw);
		if (val3 != null)
		{
			Material sailNearMaterial = sw.sailNearMaterial;
			sailNearMaterial.SetBuffer("_NearIdBuffer", GetIdentityBuffer(sw.sailCursor));
			sailNearMaterial.SetBuffer("_SwarmBuffer", val3);
			sailNearMaterial.SetBuffer("_SwarmInfoBuffer", val4);
			sailNearMaterial.SetBuffer("_NodeBuffer", sw.dysonSphere.nrdBuffer);
			sailNearMaterial.SetBuffer("_OrbitColor", val5);
			sailNearMaterial.SetVector("_SunPosition", Vector4.op_Implicit(zero));
			sailNearMaterial.SetVector("_SunPosition_Map", Vector4.op_Implicit(val2));
			sailNearMaterial.SetVector("_LocalRot", val);
			sailNearMaterial.SetInt("_GameTick", (int)num);
			sailNearMaterial.SetInt("_Global_DS_RenderPlace", (int)DysonSphere.renderPlace);
			sailNearMaterial.SetInt("_Global_DS_EditorMaskS", editorMask);
			sailNearMaterial.SetInt("_Global_DS_GameMaskS", gameMask);
			int num2 = 16;
			if ((int)DysonSphere.renderPlace == 1)
			{
				num2 = 20;
			}
			else if ((int)DysonSphere.renderPlace == 2)
			{
				num2 = 21;
			}
			Graphics.DrawMeshInstancedProcedural(Configs.builtin.solarSailMesh, 0, sailNearMaterial, new Bounds(Vector3.zero, new Vector3(100000f, 100000f, 100000f)), sw.sailCursor, (MaterialPropertyBlock)null, (ShadowCastingMode)0, false, num2, (Camera)null, (LightProbeUsage)1, (LightProbeProxyVolume)null);
		}
	}
}