using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Reflection;
using System.Runtime.CompilerServices;
using System.Runtime.Versioning;
using BepInEx;
using BepInEx.Configuration;
using BepInEx.Logging;
using HarmonyLib;
using UnityEngine;
[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 DspOptiMod;
[BepInPlugin("byboy.dspopti", "DspOptiMod", "1.0.0")]
public class DspOptiModPlugin : BaseUnityPlugin
{
internal static ManualLogSource L;
internal static ConfigEntry<bool> OptimizeSwarm;
internal static ConfigEntry<bool> DisableNearPass;
internal static ConfigEntry<bool> FramesFirst;
internal static ConfigEntry<int> RingSegments;
internal static ConfigEntry<float> RingWidthFactor;
internal static ConfigEntry<float> RingMinAlpha;
internal static ConfigEntry<float> RingAlphaScale;
private void Awake()
{
//IL_0120: Unknown result type (might be due to invalid IL or missing references)
L = ((BaseUnityPlugin)this).Logger;
OptimizeSwarm = ((BaseUnityPlugin)this).Config.Bind<bool>("Swarm", "OptimizeSwarm", true, "优化戴森云渲染:远景不再逐帆绘制,每条轨道只画一条环带");
DisableNearPass = ((BaseUnityPlugin)this).Config.Bind<bool>("Swarm", "DisableNearPass", false, "同时禁用近处帆的精细网格渲染(2500m 内本来也只画近处的帆)");
RingSegments = ((BaseUnityPlugin)this).Config.Bind<int>("Swarm", "RingSegments", 128, "环带分段数(64~256)");
RingWidthFactor = ((BaseUnityPlugin)this).Config.Bind<float>("Swarm", "RingWidthFactor", 0.025f, "环带宽度 = 轨道半径 * 该系数");
RingMinAlpha = ((BaseUnityPlugin)this).Config.Bind<float>("Swarm", "RingMinAlpha", 0.25f, "环带最低透明度");
RingAlphaScale = ((BaseUnityPlugin)this).Config.Bind<float>("Swarm", "RingAlphaScale", 0.6f, "环带基础透明度");
FramesFirst = ((BaseUnityPlugin)this).Config.Bind<bool>("Rocket", "FramesFirst", true, "小火箭:所有节点本体全部建成后才开始建造框架");
RingSegments.Value = Mathf.Clamp(RingSegments.Value, 32, 512);
new Harmony("byboy.dspopti").PatchAll(Assembly.GetExecutingAssembly());
L.LogInfo((object)"DspOptiMod 已加载");
}
private void Update()
{
if (Input.GetKeyDown((KeyCode)290))
{
OptimizeSwarm.Value = !OptimizeSwarm.Value;
L.LogInfo((object)("戴森云渲染优化: " + (OptimizeSwarm.Value ? "开" : "关")));
}
}
}
internal static class DysonSphereUtil
{
private static readonly HashSet<DysonSphere> FramesPhase = new HashSet<DysonSphere>();
public static DysonSphere FindSphere(DysonNode node)
{
DysonSphere[] array = GameMain.data?.dysonSpheres;
if (array == null)
{
return null;
}
foreach (DysonSphere val in array)
{
if (val != null)
{
DysonSphereLayer val2 = val.layersIdBased[node.layerId];
if (val2 != null && val2.nodePool[node.id] == node)
{
return val;
}
}
}
return null;
}
public static bool AllNodesBuilt(DysonSphere sphere)
{
DysonSphereLayer[] layersIdBased = sphere.layersIdBased;
for (int i = 1; i < layersIdBased.Length; i++)
{
DysonSphereLayer val = layersIdBased[i];
if (val == null || val.id != i)
{
continue;
}
DysonNode[] nodePool = val.nodePool;
for (int j = 1; j < val.nodeCursor; j++)
{
DysonNode val2 = nodePool[j];
if (val2 != null && val2.id == j && val2.sp < val2.spMax)
{
FramesPhase.Remove(sphere);
return false;
}
}
}
return true;
}
public static void EnsureFramesPhaseRecalc(DysonSphere sphere)
{
if (!AllNodesBuilt(sphere) || FramesPhase.Contains(sphere))
{
return;
}
FramesPhase.Add(sphere);
DysonSphereLayer[] layersIdBased = sphere.layersIdBased;
for (int i = 1; i < layersIdBased.Length; i++)
{
DysonSphereLayer val = layersIdBased[i];
if (val == null || val.id != i)
{
continue;
}
DysonNode[] nodePool = val.nodePool;
for (int j = 1; j < val.nodeCursor; j++)
{
DysonNode val2 = nodePool[j];
if (val2 != null && val2.id == j)
{
val2.RecalcSpReq();
}
}
}
for (int k = sphere.GetAutoNodeCount(); k < 8; k++)
{
sphere.PickAutoNode();
}
}
}
[HarmonyPatch(typeof(DysonNode), "RecalcSpReq")]
internal static class NodeRecalcSpReqPatch
{
private static readonly FieldInfo SpReqField = AccessTools.Field(typeof(DysonNode), "_spReq");
private static bool Prefix(DysonNode __instance)
{
if (!DspOptiModPlugin.FramesFirst.Value)
{
return true;
}
DysonSphere val = DysonSphereUtil.FindSphere(__instance);
if (val != null && !DysonSphereUtil.AllNodesBuilt(val))
{
lock (__instance)
{
SpReqField.SetValue(__instance, __instance.spMax - __instance.sp);
}
return false;
}
return true;
}
}
[HarmonyPatch(typeof(DysonNode), "ConstructSp")]
internal static class NodeConstructSpPatch
{
private static bool Prefix(DysonNode __instance, ref object __result)
{
if (!DspOptiModPlugin.FramesFirst.Value)
{
return true;
}
lock (__instance)
{
object obj = null;
bool flag = false;
if (__instance.sp >= __instance.spMax)
{
DysonSphere val = DysonSphereUtil.FindSphere(__instance);
flag = val != null && DysonSphereUtil.AllNodesBuilt(val);
}
if (flag)
{
List<DysonFrame> frames = __instance.frames;
if (frames != null)
{
for (int i = __instance.frameTurn; i < __instance.frameTurn + frames.Count; i++)
{
int num = i % frames.Count;
DysonFrame val2 = frames[num];
int num2 = val2.spMax >> 1;
if (val2.nodeA == __instance && val2.spA < num2)
{
val2.spA++;
__instance.frameTurn = num + 1;
obj = val2;
break;
}
if (val2.nodeB == __instance && val2.spB < num2)
{
val2.spB++;
__instance.frameTurn = num + 1;
obj = val2;
break;
}
}
}
}
if (obj == null && __instance.sp < __instance.spMax)
{
__instance.sp++;
obj = __instance;
}
if (obj == null)
{
DysonSphere val3 = DysonSphereUtil.FindSphere(__instance);
if (val3 != null)
{
DysonSphereLayer[] layersIdBased = val3.layersIdBased;
for (int j = 1; j < layersIdBased.Length; j++)
{
if (obj != null)
{
break;
}
DysonSphereLayer val4 = layersIdBased[j];
if (val4 == null || val4.id != j)
{
continue;
}
DysonNode[] nodePool = val4.nodePool;
for (int k = 1; k < val4.nodeCursor; k++)
{
DysonNode val5 = nodePool[k];
if (val5 == null || val5.id != k || val5.sp >= val5.spMax)
{
continue;
}
lock (val5)
{
if (val5.sp < val5.spMax)
{
val5.sp++;
obj = val5;
}
}
if (obj != null)
{
break;
}
}
}
DysonNode val6 = (DysonNode)((obj is DysonNode) ? obj : null);
if (val6 != null)
{
val6.RecalcSpReq();
}
}
}
__instance.spOrdered--;
if (__instance.spOrdered < 0)
{
__instance.spOrdered = 0;
}
__result = obj;
}
__instance.RecalcSpReq();
DysonSphere val7 = DysonSphereUtil.FindSphere(__instance);
if (val7 != null)
{
DysonSphereUtil.EnsureFramesPhaseRecalc(val7);
}
return false;
}
}
[HarmonyPatch(typeof(DysonSwarm), "DrawPost")]
internal static class SwarmDrawPostPatch
{
private static Material _ringMat;
private static FieldInfo _bulletBufferField;
private static Material RingMaterial()
{
//IL_0033: Unknown result type (might be due to invalid IL or missing references)
//IL_003d: Expected O, but got Unknown
if ((Object)(object)_ringMat == (Object)null)
{
Shader val = Shader.Find("Hidden/Internal-Colored");
if ((Object)(object)val == (Object)null)
{
DspOptiModPlugin.L.LogWarning((object)"找不到 Hidden/Internal-Colored,回退原版渲染");
return null;
}
_ringMat = new Material(val);
_ringMat.SetInt("_SrcBlend", 5);
_ringMat.SetInt("_DstBlend", 10);
_ringMat.SetInt("_Cull", 0);
_ringMat.SetInt("_ZWrite", 0);
}
return _ringMat;
}
private static bool Prefix(DysonSwarm __instance)
{
//IL_0033: Unknown result type (might be due to invalid IL or missing references)
//IL_0039: Invalid comparison between Unknown and I4
//IL_006e: Unknown result type (might be due to invalid IL or missing references)
//IL_0073: Unknown result type (might be due to invalid IL or missing references)
//IL_02e5: Unknown result type (might be due to invalid IL or missing references)
//IL_02eb: Expected O, but got Unknown
//IL_00e6: Unknown result type (might be due to invalid IL or missing references)
//IL_00eb: 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_01a4: Unknown result type (might be due to invalid IL or missing references)
//IL_01bf: Unknown result type (might be due to invalid IL or missing references)
//IL_01c4: Unknown result type (might be due to invalid IL or missing references)
//IL_01c9: Unknown result type (might be due to invalid IL or missing references)
//IL_01cb: Unknown result type (might be due to invalid IL or missing references)
//IL_01e6: Unknown result type (might be due to invalid IL or missing references)
//IL_01eb: 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_01f2: Unknown result type (might be due to invalid IL or missing references)
//IL_01f4: Unknown result type (might be due to invalid IL or missing references)
//IL_01f9: Unknown result type (might be due to invalid IL or missing references)
//IL_01fe: Unknown result type (might be due to invalid IL or missing references)
//IL_0200: 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_0207: Unknown result type (might be due to invalid IL or missing references)
//IL_020b: Unknown result type (might be due to invalid IL or missing references)
//IL_0210: Unknown result type (might be due to invalid IL or missing references)
//IL_0215: Unknown result type (might be due to invalid IL or missing references)
//IL_0219: Unknown result type (might be due to invalid IL or missing references)
//IL_0220: Unknown result type (might be due to invalid IL or missing references)
//IL_0225: Unknown result type (might be due to invalid IL or missing references)
//IL_0227: 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_0230: Unknown result type (might be due to invalid IL or missing references)
//IL_0232: Unknown result type (might be due to invalid IL or missing references)
//IL_0237: Unknown result type (might be due to invalid IL or missing references)
//IL_0239: Unknown result type (might be due to invalid IL or missing references)
//IL_0243: Unknown result type (might be due to invalid IL or missing references)
//IL_0245: Unknown result type (might be due to invalid IL or missing references)
//IL_0247: Unknown result type (might be due to invalid IL or missing references)
//IL_024c: Unknown result type (might be due to invalid IL or missing references)
//IL_024e: Unknown result type (might be due to invalid IL or missing references)
//IL_0258: Unknown result type (might be due to invalid IL or missing references)
//IL_025a: Unknown result type (might be due to invalid IL or missing references)
//IL_025c: Unknown result type (might be due to invalid IL or missing references)
//IL_0261: Unknown result type (might be due to invalid IL or missing references)
//IL_0263: Unknown result type (might be due to invalid IL or missing references)
//IL_026d: Unknown result type (might be due to invalid IL or missing references)
//IL_026f: Unknown result type (might be due to invalid IL or missing references)
//IL_0271: Unknown result type (might be due to invalid IL or missing references)
//IL_0276: Unknown result type (might be due to invalid IL or missing references)
//IL_0278: Unknown result type (might be due to invalid IL or missing references)
if (!DspOptiModPlugin.OptimizeSwarm.Value)
{
return true;
}
Material val = RingMaterial();
if ((Object)(object)val == (Object)null)
{
return true;
}
StarData starData = __instance.starData;
GameData gameData = __instance.gameData;
if (starData == null || gameData == null)
{
return false;
}
bool num = (int)DysonSphere.renderPlace == 2;
float num2 = 0.00025f;
if (num)
{
int value = DspOptiModPlugin.RingSegments.Value;
float value2 = DspOptiModPlugin.RingWidthFactor.Value;
SailOrbit[] orbits = __instance.orbits;
Vector4[] orbitColorsHSVA = __instance.orbitColorsHSVA;
Vector3 zero = Vector3.zero;
val.SetPass(0);
GL.Begin(7);
try
{
for (int i = 1; i < __instance.orbitCursor && i < orbits.Length; i++)
{
if (orbits[i].id == i && orbits[i].count > 0 && orbits[i].enabled)
{
float num3 = orbits[i].radius * num2;
Quaternion rotation = __instance.orbits[i].rotation;
Color val2 = Color.HSVToRGB(orbitColorsHSVA[i].x, orbitColorsHSVA[i].y, orbitColorsHSVA[i].z);
float num4 = Mathf.Clamp01((float)orbits[i].count / 6000f);
val2.a = Mathf.Max(DspOptiModPlugin.RingMinAlpha.Value, DspOptiModPlugin.RingAlphaScale.Value * (0.35f + 0.65f * num4));
float num5 = num3 * value2;
for (int j = 0; j < value; j++)
{
float num6 = (float)j / (float)value * (float)Math.PI * 2f;
float num7 = (float)(j + 1) / (float)value * (float)Math.PI * 2f;
Vector3 val3 = rotation * new Vector3(Mathf.Cos(num6) * num3, 0f, Mathf.Sin(num6) * num3);
Vector3 val4 = rotation * new Vector3(Mathf.Cos(num7) * num3, 0f, Mathf.Sin(num7) * num3);
Vector3 val5 = rotation * Vector3.up;
Vector3 val6 = val4 - val3;
val6 = Vector3.Cross(val5, ((Vector3)(ref val6)).normalized);
Vector3 val7 = ((Vector3)(ref val6)).normalized * num5;
GL.Color(val2);
GL.Vertex(zero + val3 - val7);
GL.Vertex(zero + val3 + val7);
GL.Vertex(zero + val4 + val7);
GL.Vertex(zero + val4 - val7);
}
}
}
}
finally
{
GL.End();
}
}
if (_bulletBufferField == null)
{
_bulletBufferField = AccessTools.Field(typeof(DysonSwarm), "bulletBuffer");
}
ComputeBuffer val8 = (ComputeBuffer)_bulletBufferField.GetValue(__instance);
Material bulletMaterial = __instance.bulletMaterial;
if ((Object)(object)bulletMaterial != (Object)null && val8 != null && __instance.bulletCursor > 1)
{
bulletMaterial.SetBuffer("_BulletBuffer", val8);
bulletMaterial.SetPass(0);
Graphics.DrawProceduralNow((MeshTopology)2, __instance.bulletCursor * 8, 1);
}
return false;
}
}
[HarmonyPatch(typeof(DysonSwarm), "DrawModel")]
internal static class SwarmDrawModelPatch
{
private static bool Prefix()
{
if (DspOptiModPlugin.OptimizeSwarm.Value && DspOptiModPlugin.DisableNearPass.Value)
{
return false;
}
return true;
}
}