using System;
using System.Collections.Concurrent;
using System.Collections.Generic;
using System.Diagnostics;
using System.IO;
using System.Reflection;
using System.Runtime.CompilerServices;
using System.Runtime.Versioning;
using System.Threading;
using System.Threading.Tasks;
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: AssemblyCompany("DysonShellOptimizer")]
[assembly: AssemblyConfiguration("Release")]
[assembly: AssemblyFileVersion("1.0.1.0")]
[assembly: AssemblyInformationalVersion("1.0.1+1b2fa40e34501e56239314f27a0dbee8fcd8ae1f")]
[assembly: AssemblyProduct("DysonShellOptimizer")]
[assembly: AssemblyTitle("DysonShellOptimizer")]
[assembly: AssemblyVersion("1.0.1.0")]
namespace DysonShellOptimizer;
[HarmonyPatch]
internal static class BlueprintImportPatch
{
[HarmonyPostfix]
[HarmonyPatch(typeof(DysonBlueprintData), "Import")]
private static void Import_Postfix(DysonBlueprintData __instance, DysonSphere sphere)
{
if (sphere == null || DysonShellOptimizerPlugin.FreeGeometryAfterPaste == null || !DysonShellOptimizerPlugin.FreeGeometryAfterPaste.Value)
{
return;
}
try
{
FreeGeometry.ReleaseAll(sphere);
}
catch (Exception ex)
{
DysonShellOptimizerPlugin.Log.LogWarning((object)("[Geometry] free after paste failed: " + ex.Message));
}
}
[HarmonyPrefix]
[HarmonyPatch(typeof(DysonSphereSegmentRenderer), "DrawModels")]
private static void DrawModels_Prefix(DysonSphereSegmentRenderer __instance)
{
if (DysonShellOptimizerPlugin.FreeGeometryAfterPaste == null || !DysonShellOptimizerPlugin.FreeGeometryAfterPaste.Value)
{
return;
}
try
{
DysonSphere val = __instance?.dysonSphere;
if (val != null && FreeGeometry.IsReleased(val) && (val.inEditorRenderMaskL != 0 || val.inGameRenderMaskL != 0))
{
FreeGeometry.Regenerate(val);
}
}
catch (Exception ex)
{
DysonShellOptimizerPlugin.Log.LogWarning((object)("[Geometry] regenerate on view failed: " + ex.Message));
}
}
}
[BepInPlugin("sinthome358.dsp.dysonshelloptimizer", "DysonShellOptimizer", "1.0.1")]
[BepInDependency(/*Could not decode attribute arguments.*/)]
public sealed class DysonShellOptimizerPlugin : BaseUnityPlugin
{
public const string Guid = "sinthome358.dsp.dysonshelloptimizer";
public const string Name = "DysonShellOptimizer";
public const string Version = "1.0.1";
public const string LossyCompressionGuid = "starfi5h.plugin.LossyCompression";
internal static ConfigEntry<bool> Enabled;
internal static ConfigEntry<int> MaxDegreeOfParallelism;
internal static ConfigEntry<bool> ParallelAcrossShells;
internal static ConfigEntry<bool> FreeGeometryAfterPaste;
internal static ConfigEntry<int> GeometryBatchMultiplier;
internal static ConfigEntry<bool> BatchNodeBufferUpload;
internal static ConfigEntry<bool> LogTimings;
internal static ManualLogSource Log;
private Harmony _harmony;
private void Awake()
{
//IL_00f1: Unknown result type (might be due to invalid IL or missing references)
//IL_00fb: Expected O, but got Unknown
Log = ((BaseUnityPlugin)this).Logger;
Enabled = ((BaseUnityPlugin)this).Config.Bind<bool>("General", "Enabled", true, "Replace DysonShell.GenerateGeometry with a parallel implementation. Geometry output is unchanged.");
MaxDegreeOfParallelism = ((BaseUnityPlugin)this).Config.Bind<int>("General", "MaxDegreeOfParallelism", 0, "Worker threads for the parallel loops. 0 uses the CPU core count.");
ParallelAcrossShells = ((BaseUnityPlugin)this).Config.Bind<bool>("General", "ParallelAcrossShells", true, "Generate geometry for multiple shells in parallel during blueprint paste. Disable to compare.");
FreeGeometryAfterPaste = ((BaseUnityPlugin)this).Config.Bind<bool>("General", "FreeGeometryAfterPaste", true, "Drop vertex data after pasting to release RAM. Models are regenerated when the sphere is viewed.");
GeometryBatchMultiplier = ((BaseUnityPlugin)this).Config.Bind<int>("General", "GeometryBatchMultiplier", 4, "Shells per batch = CPU cores * this value. Smaller batches lower peak memory, larger batches reduce scheduling overhead.");
BatchNodeBufferUpload = ((BaseUnityPlugin)this).Config.Bind<bool>("General", "BatchNodeBufferUpload", true, "Upload the dyson node compute buffer once per layer import instead of once per node.");
LogTimings = ((BaseUnityPlugin)this).Config.Bind<bool>("General", "LogTimings", false, "Log per-shell geometry generation timings so the speedup can be verified.");
_harmony = new Harmony("sinthome358.dsp.dysonshelloptimizer");
_harmony.PatchAll(typeof(DysonShellOptimizerPlugin).Assembly);
((BaseUnityPlugin)this).Logger.LogInfo((object)("DysonShellOptimizer 1.0.1 loaded. cores=" + Environment.ProcessorCount));
}
private void OnDestroy()
{
if (_harmony != null)
{
_harmony.UnpatchSelf();
_harmony = null;
}
}
}
internal static class FreeGeometry
{
private static readonly List<DysonNode> EmptyNodes = new List<DysonNode>(0);
private static readonly Dictionary<int, int> EmptyNodeIndexMap = new Dictionary<int, int>(0);
private static readonly List<DysonFrame> EmptyFrames = new List<DysonFrame>(0);
private static readonly List<VectorLF3> EmptyPolygon = new List<VectorLF3>(0);
private static readonly FieldRef<DysonShell, VectorLF3[]> RefPolyn = AccessTools.FieldRefAccess<DysonShell, VectorLF3[]>("polyn");
private static readonly FieldRef<DysonShell, double[]> RefPolynu = AccessTools.FieldRefAccess<DysonShell, double[]>("polynu");
private static readonly HashSet<DysonSphere> Released = new HashSet<DysonSphere>();
internal static bool IsReleased(DysonSphere sphere)
{
if (sphere == null)
{
return false;
}
lock (Released)
{
return Released.Contains(sphere);
}
}
internal static void MarkReleased(DysonSphere sphere)
{
if (sphere == null)
{
return;
}
lock (Released)
{
Released.Add(sphere);
}
}
internal static void ReleaseAll(DysonSphere sphere)
{
if (sphere == null || sphere.layersIdBased == null || IsReleased(sphere))
{
return;
}
int num = 0;
for (int i = 1; i < sphere.layersIdBased.Length; i++)
{
DysonSphereLayer val = sphere.layersIdBased[i];
if (val == null || val.id != i)
{
continue;
}
for (int j = 1; j < val.shellCursor; j++)
{
DysonShell val2 = val.shellPool[j];
if (val2 != null && val2.id == j && val2.verts != null)
{
Release(val2);
num++;
}
}
}
if (num > 0)
{
lock (Released)
{
Released.Add(sphere);
}
long totalMemory = GC.GetTotalMemory(forceFullCollection: false);
GC.Collect();
GC.WaitForPendingFinalizers();
GC.Collect();
long totalMemory2 = GC.GetTotalMemory(forceFullCollection: true);
DysonShellOptimizerPlugin.Log.LogInfo((object)("[Geometry] released " + num + " shells, managed heap " + totalMemory / 1048576 + " MB -> " + totalMemory2 / 1048576 + " MB"));
}
}
internal static void ReleaseVerticesOnly(DysonShell shell)
{
if (shell != null && shell.verts != null)
{
shell.verts = null;
shell.uvs = null;
shell.uv2s = null;
shell.vkeys = null;
shell.indices = null;
shell.vAdjs = null;
shell.vertAttr = null;
shell.vertsq = null;
if ((Object)(object)shell.mesh != (Object)null)
{
Object.Destroy((Object)(object)shell.mesh);
shell.mesh = null;
}
if ((Object)(object)shell.material != (Object)null)
{
Object.Destroy((Object)(object)shell.material);
shell.material = null;
}
}
}
internal static void Regenerate(DysonSphere sphere)
{
if (sphere == null || sphere.layersIdBased == null)
{
return;
}
lock (Released)
{
if (!Released.Remove(sphere))
{
return;
}
}
int num = 0;
for (int i = 1; i < sphere.layersIdBased.Length; i++)
{
DysonSphereLayer val = sphere.layersIdBased[i];
if (val == null || val.id != i)
{
continue;
}
List<DysonShell> list = new List<DysonShell>();
for (int j = 1; j < val.shellCursor; j++)
{
DysonShell val2 = val.shellPool[j];
if (val2 != null && val2.id == j && val2.verts == null && val2.polygon != null && val2.polygon.Count > 0)
{
list.Add(val2);
}
}
if (list.Count == 0)
{
continue;
}
GeometryCore.Slot slot = GeometryCore.RentSlot();
try
{
for (int k = 0; k < list.Count; k++)
{
GeometryCore.Generate(list[k], slot);
}
}
finally
{
GeometryCore.ReturnSlot(slot);
}
for (int l = 0; l < list.Count; l++)
{
list[l].GenerateModelObjects();
num++;
}
}
if (num > 0)
{
DysonShellOptimizerPlugin.Log.LogInfo((object)("[Geometry] regenerated " + num + " shells on demand."));
}
}
private static void Release(DysonShell shell)
{
//IL_001d: Unknown result type (might be due to invalid IL or missing references)
//IL_0022: Unknown result type (might be due to invalid IL or missing references)
//IL_00df: Unknown result type (might be due to invalid IL or missing references)
//IL_00e1: Unknown result type (might be due to invalid IL or missing references)
int layerId = shell.layerId;
int id = shell.id;
int protoId = shell.protoId;
int randSeed = shell.randSeed;
Color32 color = shell.color;
int[] nodecps = shell.nodecps;
int[] vertsqOffset = shell.vertsqOffset;
int vertexCount = shell.vertexCount;
int cpPerVertex = shell.cpPerVertex;
float gridSize = shell.gridSize;
int gridScale = shell.gridScale;
List<DysonNode> nodes = shell.nodes;
Dictionary<int, int> nodeIndexMap = shell.nodeIndexMap;
List<DysonFrame> frames = shell.frames;
List<VectorLF3> polygon = shell.polygon;
VectorLF3[] array = RefPolyn.Invoke(shell);
double[] array2 = RefPolynu.Invoke(shell);
shell.nodes = EmptyNodes;
shell.nodeIndexMap = EmptyNodeIndexMap;
shell.frames = EmptyFrames;
shell.polygon = EmptyPolygon;
shell.SetEmpty();
shell.layerId = layerId;
shell.id = id;
shell.protoId = protoId;
shell.randSeed = randSeed;
shell.color = color;
shell.nodecps = nodecps;
shell.vertsqOffset = vertsqOffset;
shell.vertexCount = vertexCount;
shell.cpPerVertex = cpPerVertex;
shell.gridSize = gridSize;
shell.gridScale = gridScale;
shell.nodes = nodes;
shell.nodeIndexMap = nodeIndexMap;
shell.frames = frames;
shell.polygon = polygon;
RefPolyn.Invoke(shell) = array;
RefPolynu.Invoke(shell) = array2;
}
}
[HarmonyPatch]
internal static class GenerateGeometryPatch
{
[HarmonyPrefix]
[HarmonyPatch(typeof(DysonShell), "GenerateGeometry")]
private static bool GenerateGeometry_Prefix(DysonShell __instance)
{
if (DysonShellOptimizerPlugin.Enabled == null || !DysonShellOptimizerPlugin.Enabled.Value)
{
return true;
}
if (GeometryCore.BatchImporting)
{
return false;
}
try
{
GeometryCore.Slot slot = GeometryCore.RentSlot();
try
{
GeometryCore.Generate(__instance, slot);
}
finally
{
GeometryCore.ReturnSlot(slot);
}
}
catch (Exception ex)
{
DysonShellOptimizerPlugin.Log.LogError((object)("[Geometry] shell=" + (__instance?.id ?? (-1)) + " failed: " + ex));
throw;
}
return false;
}
[HarmonyPrefix]
[HarmonyPatch(typeof(DysonShell), "GenerateModelObjects")]
private static bool GenerateModelObjects_Prefix(DysonShell __instance)
{
if (GeometryCore.BatchImporting && __instance != null && __instance.verts == null)
{
return false;
}
return true;
}
}
internal static class GeometryCore
{
internal sealed class Slot
{
public Dictionary<int, Vector3> Vmap = new Dictionary<int, Vector3>(16384);
public Dictionary<int, Vector3> OutVmap = new Dictionary<int, Vector3>(16384);
public Dictionary<int, int> Ivmap = new Dictionary<int, int>(16384);
public byte[] Kind = new byte[0];
public float[] Pos = new float[0];
public int[] RowCount = new int[0];
public int[] RowCursor = new int[0];
public int[] BestEdge = new int[0];
public int[] VertAttr = new int[0];
public int[] PerNode = new int[0];
public int[] OpenList = new int[0];
public bool[] Opened = new bool[0];
public bool[] Marked = new bool[0];
}
private const double Sqrt3 = 0.8660254037844386;
private const double RayX = 0.915662593339561;
private const double RayY = 0.40194777665596015;
private const double InvSqrt3 = 0.5773502691896258;
private static readonly FieldRef<DysonShell, double> RefRadius = AccessTools.FieldRefAccess<DysonShell, double>("radius");
private static readonly FieldRef<DysonShell, VectorLF3> RefCenter = AccessTools.FieldRefAccess<DysonShell, VectorLF3>("center");
private static readonly FieldRef<DysonShell, VectorLF3> RefXAxis = AccessTools.FieldRefAccess<DysonShell, VectorLF3>("xaxis");
private static readonly FieldRef<DysonShell, VectorLF3> RefYAxis = AccessTools.FieldRefAccess<DysonShell, VectorLF3>("yaxis");
private static readonly FieldRef<DysonShell, VectorLF3> RefW0Axis = AccessTools.FieldRefAccess<DysonShell, VectorLF3>("w0axis");
private static readonly FieldRef<DysonShell, VectorLF3> RefW1Axis = AccessTools.FieldRefAccess<DysonShell, VectorLF3>("w1axis");
private static readonly FieldRef<DysonShell, VectorLF3> RefW2Axis = AccessTools.FieldRefAccess<DysonShell, VectorLF3>("w2axis");
private static readonly FieldRef<DysonShell, VectorLF3> RefT0Axis = AccessTools.FieldRefAccess<DysonShell, VectorLF3>("t0axis");
private static readonly FieldRef<DysonShell, VectorLF3> RefT1Axis = AccessTools.FieldRefAccess<DysonShell, VectorLF3>("t1axis");
private static readonly FieldRef<DysonShell, VectorLF3> RefT2Axis = AccessTools.FieldRefAccess<DysonShell, VectorLF3>("t2axis");
private static readonly FieldRef<DysonShell, VectorLF3> RefRayDir = AccessTools.FieldRefAccess<DysonShell, VectorLF3>("raydir");
private static readonly FieldRef<DysonShell, VectorLF3[]> RefPolyn = AccessTools.FieldRefAccess<DysonShell, VectorLF3[]>("polyn");
private static readonly FieldRef<DysonShell, double[]> RefPolynu = AccessTools.FieldRefAccess<DysonShell, double[]>("polynu");
private static readonly FieldRef<DysonShell, bool> RefClockwise = AccessTools.FieldRefAccess<DysonShell, bool>("clockwise");
private static readonly ConcurrentBag<Slot> SlotPool = new ConcurrentBag<Slot>();
internal static volatile bool BatchImporting;
private static int DegreeOfParallelism
{
get
{
int num = ((DysonShellOptimizerPlugin.MaxDegreeOfParallelism != null) ? DysonShellOptimizerPlugin.MaxDegreeOfParallelism.Value : 0);
if (num <= 0)
{
return Environment.ProcessorCount;
}
return num;
}
}
internal static Slot RentSlot()
{
if (!SlotPool.TryTake(out var result))
{
return new Slot();
}
return result;
}
internal static void ReturnSlot(Slot slot)
{
if (slot != null)
{
SlotPool.Add(slot);
}
}
internal static void Generate(DysonShell shell, Slot slot)
{
//IL_0041: Unknown result type (might be due to invalid IL or missing references)
//IL_0046: Unknown result type (might be due to invalid IL or missing references)
//IL_006e: Unknown result type (might be due to invalid IL or missing references)
//IL_007a: Unknown result type (might be due to invalid IL or missing references)
//IL_008d: Unknown result type (might be due to invalid IL or missing references)
//IL_0099: Unknown result type (might be due to invalid IL or missing references)
//IL_009e: Unknown result type (might be due to invalid IL or missing references)
//IL_00a3: Unknown result type (might be due to invalid IL or missing references)
//IL_00b1: Unknown result type (might be due to invalid IL or missing references)
//IL_00b6: Unknown result type (might be due to invalid IL or missing references)
//IL_00b8: Unknown result type (might be due to invalid IL or missing references)
//IL_00b9: Unknown result type (might be due to invalid IL or missing references)
//IL_00be: Unknown result type (might be due to invalid IL or missing references)
//IL_00c3: Unknown result type (might be due to invalid IL or missing references)
//IL_00c8: Unknown result type (might be due to invalid IL or missing references)
//IL_00fe: Unknown result type (might be due to invalid IL or missing references)
//IL_0103: Unknown result type (might be due to invalid IL or missing references)
//IL_0146: Unknown result type (might be due to invalid IL or missing references)
//IL_014b: Unknown result type (might be due to invalid IL or missing references)
//IL_014f: Unknown result type (might be due to invalid IL or missing references)
//IL_015a: Unknown result type (might be due to invalid IL or missing references)
//IL_0175: Unknown result type (might be due to invalid IL or missing references)
//IL_0177: Unknown result type (might be due to invalid IL or missing references)
//IL_017c: Unknown result type (might be due to invalid IL or missing references)
//IL_0187: Unknown result type (might be due to invalid IL or missing references)
//IL_018c: Unknown result type (might be due to invalid IL or missing references)
//IL_018f: Unknown result type (might be due to invalid IL or missing references)
//IL_0197: Unknown result type (might be due to invalid IL or missing references)
//IL_019c: Unknown result type (might be due to invalid IL or missing references)
//IL_01ae: Unknown result type (might be due to invalid IL or missing references)
//IL_01b3: Unknown result type (might be due to invalid IL or missing references)
//IL_01c0: Unknown result type (might be due to invalid IL or missing references)
//IL_01c5: Unknown result type (might be due to invalid IL or missing references)
//IL_0254: Unknown result type (might be due to invalid IL or missing references)
//IL_0256: Unknown result type (might be due to invalid IL or missing references)
//IL_025b: Unknown result type (might be due to invalid IL or missing references)
//IL_0260: Unknown result type (might be due to invalid IL or missing references)
//IL_0265: Unknown result type (might be due to invalid IL or missing references)
//IL_0269: Unknown result type (might be due to invalid IL or missing references)
//IL_026e: Unknown result type (might be due to invalid IL or missing references)
//IL_0298: 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_029c: Unknown result type (might be due to invalid IL or missing references)
//IL_02a1: Unknown result type (might be due to invalid IL or missing references)
//IL_02a5: Unknown result type (might be due to invalid IL or missing references)
//IL_02aa: Unknown result type (might be due to invalid IL or missing references)
//IL_02ad: Unknown result type (might be due to invalid IL or missing references)
//IL_02b8: Unknown result type (might be due to invalid IL or missing references)
//IL_02bd: Unknown result type (might be due to invalid IL or missing references)
//IL_02c8: Unknown result type (might be due to invalid IL or missing references)
//IL_02cd: Unknown result type (might be due to invalid IL or missing references)
//IL_02d2: Unknown result type (might be due to invalid IL or missing references)
//IL_02d8: Unknown result type (might be due to invalid IL or missing references)
//IL_02e7: Unknown result type (might be due to invalid IL or missing references)
//IL_02ec: Unknown result type (might be due to invalid IL or missing references)
//IL_02fb: Unknown result type (might be due to invalid IL or missing references)
//IL_0300: Unknown result type (might be due to invalid IL or missing references)
//IL_0305: Unknown result type (might be due to invalid IL or missing references)
//IL_030a: Unknown result type (might be due to invalid IL or missing references)
//IL_0319: Unknown result type (might be due to invalid IL or missing references)
//IL_031e: Unknown result type (might be due to invalid IL or missing references)
//IL_032d: Unknown result type (might be due to invalid IL or missing references)
//IL_0332: Unknown result type (might be due to invalid IL or missing references)
//IL_0337: Unknown result type (might be due to invalid IL or missing references)
//IL_033a: Unknown result type (might be due to invalid IL or missing references)
//IL_033f: Unknown result type (might be due to invalid IL or missing references)
//IL_0344: Unknown result type (might be due to invalid IL or missing references)
//IL_0349: Unknown result type (might be due to invalid IL or missing references)
//IL_0362: Unknown result type (might be due to invalid IL or missing references)
//IL_0367: Unknown result type (might be due to invalid IL or missing references)
//IL_0376: Unknown result type (might be due to invalid IL or missing references)
//IL_037b: Unknown result type (might be due to invalid IL or missing references)
//IL_0380: Unknown result type (might be due to invalid IL or missing references)
//IL_0382: Unknown result type (might be due to invalid IL or missing references)
//IL_039b: Unknown result type (might be due to invalid IL or missing references)
//IL_03a0: Unknown result type (might be due to invalid IL or missing references)
//IL_03af: Unknown result type (might be due to invalid IL or missing references)
//IL_03b4: Unknown result type (might be due to invalid IL or missing references)
//IL_03b9: Unknown result type (might be due to invalid IL or missing references)
//IL_03bb: Unknown result type (might be due to invalid IL or missing references)
//IL_03d4: Unknown result type (might be due to invalid IL or missing references)
//IL_03d9: Unknown result type (might be due to invalid IL or missing references)
//IL_040d: Unknown result type (might be due to invalid IL or missing references)
//IL_0412: Unknown result type (might be due to invalid IL or missing references)
//IL_0425: Unknown result type (might be due to invalid IL or missing references)
//IL_042a: Unknown result type (might be due to invalid IL or missing references)
//IL_0434: Unknown result type (might be due to invalid IL or missing references)
//IL_0436: Unknown result type (might be due to invalid IL or missing references)
//IL_0438: Unknown result type (might be due to invalid IL or missing references)
//IL_043d: Unknown result type (might be due to invalid IL or missing references)
//IL_0441: Unknown result type (might be due to invalid IL or missing references)
//IL_0446: Unknown result type (might be due to invalid IL or missing references)
//IL_04e1: Unknown result type (might be due to invalid IL or missing references)
//IL_04e6: Unknown result type (might be due to invalid IL or missing references)
//IL_04f6: Unknown result type (might be due to invalid IL or missing references)
//IL_04f8: Unknown result type (might be due to invalid IL or missing references)
//IL_0508: Unknown result type (might be due to invalid IL or missing references)
//IL_050a: Unknown result type (might be due to invalid IL or missing references)
//IL_051b: Unknown result type (might be due to invalid IL or missing references)
//IL_0520: Unknown result type (might be due to invalid IL or missing references)
//IL_0531: Unknown result type (might be due to invalid IL or missing references)
//IL_0536: Unknown result type (might be due to invalid IL or missing references)
//IL_0546: Unknown result type (might be due to invalid IL or missing references)
//IL_0548: Unknown result type (might be due to invalid IL or missing references)
//IL_0558: Unknown result type (might be due to invalid IL or missing references)
//IL_055a: Unknown result type (might be due to invalid IL or missing references)
//IL_056a: Unknown result type (might be due to invalid IL or missing references)
//IL_056c: Unknown result type (might be due to invalid IL or missing references)
//IL_057c: Unknown result type (might be due to invalid IL or missing references)
//IL_057e: Unknown result type (might be due to invalid IL or missing references)
//IL_058f: Unknown result type (might be due to invalid IL or missing references)
//IL_0594: Unknown result type (might be due to invalid IL or missing references)
//IL_080e: Unknown result type (might be due to invalid IL or missing references)
//IL_07bc: Unknown result type (might be due to invalid IL or missing references)
//IL_07cc: Unknown result type (might be due to invalid IL or missing references)
List<VectorLF3> polygon = shell.polygon;
int polyCount = ((polygon != null) ? polygon.Count : 0);
if (polyCount == 0)
{
return;
}
if (slot == null)
{
slot = new Slot();
}
VectorLF3 val = VectorLF3.zero;
double num = 0.0;
List<DysonFrame> frames = shell.frames;
for (int i = 0; i < frames.Count; i++)
{
DysonFrame val2 = frames[i];
float num2 = Vector3.Distance(val2.nodeA.pos, val2.nodeB.pos);
VectorLF3 val3 = VectorLF3.op_Implicit(val2.nodeA.pos + val2.nodeB.pos) * 0.5;
val += val3 * (double)num2;
num += (double)num2;
}
double radius = 1.0;
VectorLF3 val4;
for (int j = 0; j < polyCount; j++)
{
val4 = polygon[0];
radius = ((VectorLF3)(ref val4)).magnitude;
radius = Math.Round(radius * 10.0) / 10.0;
List<VectorLF3> list = polygon;
int index = j;
val4 = polygon[j];
list[index] = ((VectorLF3)(ref val4)).normalized * radius;
}
VectorLF3 center = val / num;
VectorLF3 normalized = ((VectorLF3)(ref center)).normalized;
center = normalized * radius;
float num3 = 0f;
for (int k = 0; k < polyCount; k++)
{
float num4 = Vector3.Distance(VectorLF3.op_Implicit(center), VectorLF3.op_Implicit(polygon[k]));
if (num4 > num3)
{
num3 = num4;
}
}
int num5 = (int)(Math.Pow(radius / 4000.0, 0.75) + 0.5);
if (num5 < 1)
{
num5 = 1;
}
float num6 = (float)num5 * 80f;
int cpPerVertex = num5 * num5 * 2;
int half = (int)((double)num3 / 0.8660254037844386 / (double)num6 + 2.5);
val4 = VectorLF3.Cross(normalized, VectorLF3.op_Implicit(Vector3.up));
VectorLF3 normalized2 = ((VectorLF3)(ref val4)).normalized;
if (((VectorLF3)(ref normalized2)).magnitude < 0.1)
{
((VectorLF3)(ref normalized2))..ctor(0f, 0f, 1f);
}
val4 = VectorLF3.Cross(normalized2, normalized);
VectorLF3 normalized3 = ((VectorLF3)(ref val4)).normalized;
VectorLF3 raydir = normalized2 * 0.915662593339561 + normalized3 * 0.40194777665596015;
VectorLF3 w1axis = normalized2 * (0.5 * (double)num6) - normalized3 * (0.8660254037844386 * (double)num6);
VectorLF3 val5 = normalized2 * (0.5 * (double)num6) + normalized3 * (0.8660254037844386 * (double)num6);
VectorLF3 w0axis = normalized2 * (double)num6;
VectorLF3 val6 = normalized3 * ((double)num6 * 0.5773502691896258 * 0.5) - normalized2 * ((double)num6 * 0.5);
VectorLF3 val7 = normalized3 * ((double)num6 * 0.5773502691896258 * 0.5) + normalized2 * ((double)num6 * 0.5);
VectorLF3 val8 = normalized3 * ((double)num6 / 0.8660254037844386 * 0.5);
VectorLF3[] polyn = (VectorLF3[])(object)new VectorLF3[polyCount];
double[] polynu = new double[polyCount];
for (int l = 0; l < polyCount; l++)
{
VectorLF3 val9 = polygon[l];
VectorLF3 val10 = polygon[(l + 1) % polyCount];
VectorLF3[] array = polyn;
int num7 = l;
val4 = VectorLF3.Cross(val9, val10);
array[num7] = ((VectorLF3)(ref val4)).normalized;
polynu[l] = polyn[l].x * raydir.x + polyn[l].y * raydir.y + polyn[l].z * raydir.z;
}
RefRadius.Invoke(shell) = radius;
RefCenter.Invoke(shell) = center;
RefXAxis.Invoke(shell) = normalized2;
RefYAxis.Invoke(shell) = normalized3;
RefW0Axis.Invoke(shell) = w0axis;
RefW1Axis.Invoke(shell) = w1axis;
RefW2Axis.Invoke(shell) = val5;
RefT0Axis.Invoke(shell) = val8;
RefT1Axis.Invoke(shell) = val6;
RefT2Axis.Invoke(shell) = val7;
RefRayDir.Invoke(shell) = raydir;
RefPolyn.Invoke(shell) = polyn;
RefPolynu.Invoke(shell) = polynu;
shell.gridScale = num5;
shell.gridSize = num6;
shell.cpPerVertex = cpPerVertex;
int side = 2 * half + 1;
int num8 = side * side;
EnsureBuffers(slot, num8, side);
byte[] kind = slot.Kind;
float[] pos = slot.Pos;
int[] rowCount = slot.RowCount;
double halfGrid = (double)num6 * 0.5;
ParallelOptions parallelOptions = new ParallelOptions
{
MaxDegreeOfParallelism = DegreeOfParallelism
};
Array.Clear(kind, 0, num8);
Array.Clear(rowCount, 0, side + 1);
Parallel.For(0, side, parallelOptions, delegate(int mi)
{
//IL_00d1: Unknown result type (might be due to invalid IL or missing references)
//IL_00d8: 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_00e7: 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_00f7: Unknown result type (might be due to invalid IL or missing references)
//IL_014c: Unknown result type (might be due to invalid IL or missing references)
//IL_0166: Unknown result type (might be due to invalid IL or missing references)
//IL_0181: Unknown result type (might be due to invalid IL or missing references)
//IL_01a7: Unknown result type (might be due to invalid IL or missing references)
//IL_01bd: Unknown result type (might be due to invalid IL or missing references)
//IL_01d3: Unknown result type (might be due to invalid IL or missing references)
//IL_01e9: Unknown result type (might be due to invalid IL or missing references)
//IL_01ee: 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_01f7: Unknown result type (might be due to invalid IL or missing references)
//IL_0234: 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_023b: Unknown result type (might be due to invalid IL or missing references)
//IL_0240: Unknown result type (might be due to invalid IL or missing references)
//IL_0253: 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_025f: 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_0268: Unknown result type (might be due to invalid IL or missing references)
//IL_0270: Unknown result type (might be due to invalid IL or missing references)
//IL_0277: Unknown result type (might be due to invalid IL or missing references)
//IL_0280: Unknown result type (might be due to invalid IL or missing references)
//IL_0287: Unknown result type (might be due to invalid IL or missing references)
//IL_02ac: Unknown result type (might be due to invalid IL or missing references)
//IL_0331: Unknown result type (might be due to invalid IL or missing references)
//IL_0336: Unknown result type (might be due to invalid IL or missing references)
//IL_0340: Unknown result type (might be due to invalid IL or missing references)
//IL_0345: Unknown result type (might be due to invalid IL or missing references)
//IL_0347: Unknown result type (might be due to invalid IL or missing references)
//IL_0349: Unknown result type (might be due to invalid IL or missing references)
//IL_034b: Unknown result type (might be due to invalid IL or missing references)
//IL_0350: Unknown result type (might be due to invalid IL or missing references)
//IL_0352: Unknown result type (might be due to invalid IL or missing references)
//IL_0359: Unknown result type (might be due to invalid IL or missing references)
//IL_0361: Unknown result type (might be due to invalid IL or missing references)
//IL_0368: Unknown result type (might be due to invalid IL or missing references)
//IL_0371: Unknown result type (might be due to invalid IL or missing references)
//IL_0378: Unknown result type (might be due to invalid IL or missing references)
//IL_02be: Unknown result type (might be due to invalid IL or missing references)
//IL_03aa: Unknown result type (might be due to invalid IL or missing references)
//IL_03af: Unknown result type (might be due to invalid IL or missing references)
//IL_03b1: Unknown result type (might be due to invalid IL or missing references)
//IL_03b3: Unknown result type (might be due to invalid IL or missing references)
//IL_03b7: Unknown result type (might be due to invalid IL or missing references)
//IL_03bc: Unknown result type (might be due to invalid IL or missing references)
//IL_03c1: Unknown result type (might be due to invalid IL or missing references)
//IL_03c3: Unknown result type (might be due to invalid IL or missing references)
//IL_03c5: Unknown result type (might be due to invalid IL or missing references)
//IL_03ca: Unknown result type (might be due to invalid IL or missing references)
//IL_03d5: Unknown result type (might be due to invalid IL or missing references)
//IL_03d9: Unknown result type (might be due to invalid IL or missing references)
//IL_03de: Unknown result type (might be due to invalid IL or missing references)
//IL_03e0: Unknown result type (might be due to invalid IL or missing references)
//IL_03e2: Unknown result type (might be due to invalid IL or missing references)
//IL_03e7: Unknown result type (might be due to invalid IL or missing references)
//IL_03e9: Unknown result type (might be due to invalid IL or missing references)
//IL_03f0: Unknown result type (might be due to invalid IL or missing references)
//IL_03f8: Unknown result type (might be due to invalid IL or missing references)
//IL_03ff: Unknown result type (might be due to invalid IL or missing references)
//IL_0408: Unknown result type (might be due to invalid IL or missing references)
//IL_040f: Unknown result type (might be due to invalid IL or missing references)
//IL_02d0: Unknown result type (might be due to invalid IL or missing references)
//IL_0438: Unknown result type (might be due to invalid IL or missing references)
//IL_043a: Unknown result type (might be due to invalid IL or missing references)
//IL_043c: Unknown result type (might be due to invalid IL or missing references)
//IL_0441: Unknown result type (might be due to invalid IL or missing references)
int num16 = mi - half;
int num17 = 0;
VectorLF3 val11 = default(VectorLF3);
for (int m = 0; m < side; m++)
{
int num18 = m - half;
int num19 = num16 - num18;
if (num19 <= half && num19 >= -half)
{
val11.x = center.x + w0axis.x * (double)num16 - w1axis.x * (double)num18;
val11.y = center.y + w0axis.y * (double)num16 - w1axis.y * (double)num18;
val11.z = center.z + w0axis.z * (double)num16 - w1axis.z * (double)num18;
double num20 = radius / Math.Sqrt(val11.x * val11.x + val11.y * val11.y + val11.z * val11.z);
val11.x *= num20;
val11.y *= num20;
val11.z *= num20;
int num21 = 0;
VectorLF3 val12;
for (int n = 0; n < polyCount; n++)
{
double num22 = (0.0 - (polyn[n].x * val11.x + polyn[n].y * val11.y + polyn[n].z * val11.z)) / polynu[n];
if (num22 >= 0.0)
{
val12 = new VectorLF3(val11.x + num22 * raydir.x, val11.y + num22 * raydir.y, val11.z + num22 * raydir.z);
VectorLF3 normalized4 = ((VectorLF3)(ref val12)).normalized;
normalized4.x *= radius;
normalized4.y *= radius;
normalized4.z *= radius;
VectorLF3 val13 = polygon[n] - normalized4;
VectorLF3 val14 = polygon[(n + 1) % polyCount] - normalized4;
double num23 = val13.x * val14.x + val13.y * val14.y + val13.z * val14.z;
if (num23 < 0.0 || (num23 == 0.0 && val13.x == 0.0 && val13.y == 0.0 && val13.z == 0.0))
{
num21++;
}
}
}
int num24 = mi * side + m;
if ((num21 & 1) == 1)
{
kind[num24] = 1;
num17++;
}
else
{
for (int num25 = 0; num25 < polyCount; num25++)
{
VectorLF3 val15 = polygon[num25];
VectorLF3 val16 = polyn[num25];
VectorLF3 val17 = val11 - val15;
double num26 = val16.x * val17.x + val16.y * val17.y + val16.z * val17.z;
double num27 = Math.Abs(num26);
if (!(num27 > halfGrid))
{
VectorLF3 val18 = polygon[(num25 + 1) % polyCount];
VectorLF3 val19 = val11 - val16 * num26;
VectorLF3 val20 = val18 - val15;
double magnitude = ((VectorLF3)(ref val20)).magnitude;
VectorLF3 val21 = val20 / magnitude;
VectorLF3 val22 = val19 - val15;
double num28 = val21.x * val22.x + val21.y * val22.y + val21.z * val22.z;
double num29;
if (num28 < 0.0)
{
num29 = ((VectorLF3)(ref val17)).magnitude;
}
else if (num28 > magnitude)
{
val12 = val11 - val18;
num29 = ((VectorLF3)(ref val12)).magnitude;
}
else
{
num29 = num27;
}
if (num29 <= halfGrid)
{
kind[num24] = 2;
num17++;
break;
}
}
}
}
}
}
rowCount[mi + 1] = num17;
});
for (int num9 = 0; num9 < side; num9++)
{
rowCount[num9 + 1] += rowCount[num9];
}
EnsureRowCursor(slot, side);
int[] rowCursor = slot.RowCursor;
Array.Copy(rowCount, rowCursor, side);
Parallel.For(0, side, parallelOptions, delegate(int mi)
{
//IL_00d6: Unknown result type (might be due to invalid IL or missing references)
//IL_00dd: Unknown result type (might be due to invalid IL or missing references)
//IL_00e5: Unknown result type (might be due to invalid IL or missing references)
//IL_00ec: Unknown result type (might be due to invalid IL or missing references)
//IL_00f5: Unknown result type (might be due to invalid IL or missing references)
//IL_00fc: Unknown result type (might be due to invalid IL or missing references)
//IL_0114: Unknown result type (might be due to invalid IL or missing references)
//IL_0129: Unknown result type (might be due to invalid IL or missing references)
//IL_013e: Unknown result type (might be due to invalid IL or missing references)
int num16 = mi - half;
int num17 = rowCursor[mi] * 3;
VectorLF3 val11 = default(VectorLF3);
for (int m = 0; m < side; m++)
{
int num18 = mi * side + m;
if (kind[num18] != 0)
{
int num19 = m - half;
val11.x = center.x + w0axis.x * (double)num16 - w1axis.x * (double)num19;
val11.y = center.y + w0axis.y * (double)num16 - w1axis.y * (double)num19;
val11.z = center.z + w0axis.z * (double)num16 - w1axis.z * (double)num19;
double num20 = radius / Math.Sqrt(val11.x * val11.x + val11.y * val11.y + val11.z * val11.z);
pos[num17] = (float)(val11.x * num20);
pos[num17 + 1] = (float)(val11.y * num20);
pos[num17 + 2] = (float)(val11.z * num20);
num17 += 3;
}
}
});
Dictionary<int, Vector3> vmap = slot.Vmap;
Dictionary<int, Vector3> outVmap = slot.OutVmap;
Dictionary<int, int> ivmap = slot.Ivmap;
vmap.Clear();
outVmap.Clear();
ivmap.Clear();
int num10 = 0;
Vector3 value = default(Vector3);
for (int num11 = 0; num11 < side; num11++)
{
int num12 = num11 - half;
for (int num13 = 0; num13 < side; num13++)
{
int num14 = num11 * side + num13;
byte b = kind[num14];
if (b != 0)
{
int num15 = num13 - half;
int key = (num12 + 10000 << 16) | (num15 + 10000);
((Vector3)(ref value))..ctor(pos[num10], pos[num10 + 1], pos[num10 + 2]);
num10 += 3;
vmap[key] = value;
if (b == 2)
{
outVmap[key] = value;
}
}
}
}
BuildVertices(shell, slot, polygon, polyn, polynu, raydir, radius, vmap, outVmap, ivmap, parallelOptions);
}
private static void EnsureBuffers(Slot slot, int cells, int side)
{
if (slot.Kind.Length < cells)
{
slot.Kind = new byte[cells];
}
if (slot.Pos.Length < cells * 3)
{
slot.Pos = new float[cells * 3];
}
if (slot.RowCount.Length < side + 1)
{
slot.RowCount = new int[side + 1];
}
}
private static void EnsureRowCursor(Slot slot, int side)
{
if (slot.RowCursor.Length < side)
{
slot.RowCursor = new int[side];
}
}
private static void BuildVertices(DysonShell shell, Slot slot, List<VectorLF3> polygon, VectorLF3[] polyn, double[] polynu, VectorLF3 raydir, double radius, Dictionary<int, Vector3> vmap, Dictionary<int, Vector3> outvmap, Dictionary<int, int> ivmap, ParallelOptions options)
{
//IL_00ba: Unknown result type (might be due to invalid IL or missing references)
//IL_00bf: Unknown result type (might be due to invalid IL or missing references)
//IL_0298: Unknown result type (might be due to invalid IL or missing references)
//IL_029d: Unknown result type (might be due to invalid IL or missing references)
//IL_02b0: Unknown result type (might be due to invalid IL or missing references)
//IL_02e3: Unknown result type (might be due to invalid IL or missing references)
//IL_02e8: Unknown result type (might be due to invalid IL or missing references)
//IL_02f5: Unknown result type (might be due to invalid IL or missing references)
//IL_02fa: Unknown result type (might be due to invalid IL or missing references)
//IL_02ff: Unknown result type (might be due to invalid IL or missing references)
//IL_0301: Unknown result type (might be due to invalid IL or missing references)
//IL_031b: Unknown result type (might be due to invalid IL or missing references)
//IL_0336: Unknown result type (might be due to invalid IL or missing references)
//IL_056f: Unknown result type (might be due to invalid IL or missing references)
//IL_0574: Unknown result type (might be due to invalid IL or missing references)
//IL_05fc: Unknown result type (might be due to invalid IL or missing references)
//IL_0601: Unknown result type (might be due to invalid IL or missing references)
//IL_0603: Unknown result type (might be due to invalid IL or missing references)
//IL_0608: Unknown result type (might be due to invalid IL or missing references)
int polyCount = polygon.Count;
int count = vmap.Count;
Vector3[] verts = (Vector3[])(object)new Vector3[count];
Vector2[] array = (Vector2[])(object)new Vector2[count];
Vector2[] array2 = (Vector2[])(object)new Vector2[count];
int[] array3 = new int[count];
int[] array4 = new int[count];
short[] array5 = new short[count * 6];
for (int i = 0; i < array5.Length; i++)
{
array5[i] = -1;
}
int num = 0;
foreach (KeyValuePair<int, Vector3> item in vmap)
{
ivmap[item.Key] = num;
verts[num] = item.Value;
array2[num].x = (outvmap.ContainsKey(item.Key) ? 0f : 1f);
array3[num] = item.Key;
array4[num] = num;
num++;
}
foreach (KeyValuePair<int, int> item2 in ivmap)
{
int key = item2.Key;
int value = item2.Value;
array5[value * 6] = (short)(ivmap.TryGetValue(key + 65536, out var value2) ? ((short)value2) : (-1));
array5[value * 6 + 1] = (short)(ivmap.TryGetValue(key - 1, out value2) ? ((short)value2) : (-1));
array5[value * 6 + 2] = (short)(ivmap.TryGetValue(key - 65537, out value2) ? ((short)value2) : (-1));
array5[value * 6 + 3] = (short)(ivmap.TryGetValue(key - 65536, out value2) ? ((short)value2) : (-1));
array5[value * 6 + 4] = (short)(ivmap.TryGetValue(key + 1, out value2) ? ((short)value2) : (-1));
array5[value * 6 + 5] = (short)(ivmap.TryGetValue(key + 65537, out value2) ? ((short)value2) : (-1));
}
if (slot.BestEdge.Length < count)
{
slot.BestEdge = new int[count];
}
int[] bestEdge = slot.BestEdge;
Parallel.For(0, count, options, delegate(int num23)
{
//IL_001a: Unknown result type (might be due to invalid IL or missing references)
//IL_001f: Unknown result type (might be due to invalid IL or missing references)
//IL_0024: Unknown result type (might be due to invalid IL or missing references)
//IL_0033: Unknown result type (might be due to invalid IL or missing references)
//IL_0038: 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_004f: Unknown result type (might be due to invalid IL or missing references)
//IL_0058: Unknown result type (might be due to invalid IL or missing references)
//IL_005d: Unknown result type (might be due to invalid IL or missing references)
//IL_005f: Unknown result type (might be due to invalid IL or missing references)
//IL_0060: Unknown result type (might be due to invalid IL or missing references)
//IL_0062: Unknown result type (might be due to invalid IL or missing references)
//IL_0067: Unknown result type (might be due to invalid IL or missing references)
//IL_0069: Unknown result type (might be due to invalid IL or missing references)
//IL_0070: Unknown result type (might be due to invalid IL or missing references)
//IL_0078: Unknown result type (might be due to invalid IL or missing references)
//IL_007f: Unknown result type (might be due to invalid IL or missing references)
//IL_0088: Unknown result type (might be due to invalid IL or missing references)
//IL_008f: Unknown result type (might be due to invalid IL or missing references)
//IL_009a: 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_009f: Unknown result type (might be due to invalid IL or missing references)
//IL_00a4: 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_00ab: Unknown result type (might be due to invalid IL or missing references)
//IL_00ac: 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_00b3: 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_00bc: Unknown result type (might be due to invalid IL or missing references)
//IL_00c4: Unknown result type (might be due to invalid IL or missing references)
//IL_00cb: Unknown result type (might be due to invalid IL or missing references)
//IL_00d4: Unknown result type (might be due to invalid IL or missing references)
//IL_00db: Unknown result type (might be due to invalid IL or missing references)
//IL_00e6: Unknown result type (might be due to invalid IL or missing references)
//IL_00e8: Unknown result type (might be due to invalid IL or missing references)
//IL_00ea: Unknown result type (might be due to invalid IL or missing references)
//IL_00ef: Unknown result type (might be due to invalid IL or missing references)
//IL_00fa: Unknown result type (might be due to invalid IL or missing references)
//IL_00fe: Unknown result type (might be due to invalid IL or missing references)
//IL_0103: 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_0107: 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_0110: 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_011f: Unknown result type (might be due to invalid IL or missing references)
//IL_0126: Unknown result type (might be due to invalid IL or missing references)
//IL_012f: Unknown result type (might be due to invalid IL or missing references)
//IL_0136: Unknown result type (might be due to invalid IL or missing references)
//IL_014e: Unknown result type (might be due to invalid IL or missing references)
//IL_014f: Unknown result type (might be due to invalid IL or missing references)
//IL_0151: Unknown result type (might be due to invalid IL or missing references)
//IL_0156: Unknown result type (might be due to invalid IL or missing references)
//IL_017f: Unknown result type (might be due to invalid IL or missing references)
//IL_0180: Unknown result type (might be due to invalid IL or missing references)
//IL_0182: Unknown result type (might be due to invalid IL or missing references)
//IL_0187: Unknown result type (might be due to invalid IL or missing references)
double num21 = radius * 2.0;
int num22 = -1;
VectorLF3 val3 = VectorLF3.op_Implicit(verts[num23]);
for (int j = 0; j < polyCount; j++)
{
VectorLF3 val4 = polygon[j];
VectorLF3 val5 = polygon[(j + 1) % polyCount];
VectorLF3 val6 = polyn[j];
VectorLF3 val7 = val3 - val4;
double num24 = val6.x * val7.x + val6.y * val7.y + val6.z * val7.z;
VectorLF3 val8 = val3 - val6 * num24;
VectorLF3 val9 = val3 - val5;
double value3 = val6.x * val9.x + val6.y * val9.y + val6.z * val9.z;
VectorLF3 val10 = val5 - val4;
double magnitude = ((VectorLF3)(ref val10)).magnitude;
VectorLF3 val11 = val10 / magnitude;
VectorLF3 val12 = val8 - val4;
double num25 = val11.x * val12.x + val11.y * val12.y + val11.z * val12.z;
VectorLF3 val13;
double magnitude2;
if (num25 < 0.0)
{
val13 = val3 - val4;
magnitude2 = ((VectorLF3)(ref val13)).magnitude;
magnitude2 -= Math.Abs(num24) * 0.001;
}
else if (num25 > magnitude)
{
val13 = val3 - val5;
magnitude2 = ((VectorLF3)(ref val13)).magnitude;
magnitude2 -= Math.Abs(value3) * 0.001;
}
else
{
magnitude2 = Math.Abs(num24);
magnitude2 -= Math.Abs(num24) * 0.001;
}
if (magnitude2 < num21)
{
num21 = magnitude2;
num22 = j;
}
}
bestEdge[num23] = num22;
});
int num2 = 0;
int num3 = 0;
for (int num4 = 0; num4 < count; num4++)
{
array2[num4].y = bestEdge[num4];
if (num2 + num3 >= 49 || !IsPointInShell(VectorLF3.op_Implicit(verts[num4]), polygon, polyn, polynu, raydir, radius, polyCount))
{
continue;
}
int num5 = bestEdge[num4];
if (num5 >= 0)
{
VectorLF3 val = VectorLF3.op_Implicit(verts[num4]) - polygon[num5];
double num6 = val.x * polyn[num5].x + val.y * polyn[num5].y + val.z * polyn[num5].z;
if (num6 > 0.1)
{
num2++;
}
else if (num6 < -0.1)
{
num3++;
}
}
}
RefClockwise.Invoke(shell) = num2 > num3;
int nodeCount = shell.nodes.Count;
if (slot.VertAttr.Length < count)
{
slot.VertAttr = new int[count];
}
if (slot.PerNode.Length < nodeCount + 1)
{
slot.PerNode = new int[nodeCount + 1];
}
int[] vertAttr = slot.VertAttr;
int[] perNode = slot.PerNode;
Array.Clear(perNode, 0, nodeCount + 1);
Array.Clear(vertAttr, 0, count);
int wrap = nodeCount / 2;
Parallel.For(0, count, options, delegate(int num21)
{
//IL_0007: Unknown result type (might be due to invalid IL or missing references)
//IL_000c: Unknown result type (might be due to invalid IL or missing references)
//IL_0059: 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_0071: 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)
Vector3 val3 = verts[num21];
double num22 = double.MaxValue;
int num23 = 0;
int num24 = 0;
int num25 = num21 + 479001600;
for (int j = 0; j < nodeCount; j++)
{
int num26 = num25 % nodeCount - j;
if (num26 < 0)
{
num26 = -num26;
}
if (num26 > wrap)
{
num26 = nodeCount - num26;
}
Vector3 val4 = val3 - shell.nodes[j].pos;
double num27 = (double)((Vector3)(ref val4)).sqrMagnitude + (double)num26;
if (num27 < num22)
{
num22 = num27;
num23 = shell.nodes[j].id;
num24 = j;
}
}
vertAttr[num21] = num23;
Interlocked.Increment(ref perNode[num24]);
});
int[] array6 = new int[nodeCount + 1];
for (int num7 = 0; num7 <= nodeCount; num7++)
{
array6[num7] = perNode[num7];
}
int num8 = 0;
for (int num9 = 0; num9 < array6.Length; num9++)
{
num8 += array6[num9];
}
for (int num10 = array6.Length - 1; num10 >= 0; num10--)
{
array6[num10] = num8;
if (num10 > 0)
{
num8 -= array6[num10 - 1];
}
}
short[] array7 = new short[count];
if (slot.OpenList.Length < count)
{
slot.OpenList = new int[count];
}
if (slot.Opened.Length < count)
{
slot.Opened = new bool[count];
}
if (slot.Marked.Length < count)
{
slot.Marked = new bool[count];
}
int[] openList = slot.OpenList;
bool[] opened = slot.Opened;
bool[] marked = slot.Marked;
Array.Clear(opened, 0, count);
Array.Clear(marked, 0, count);
int openCount = 0;
int seed = shell.randSeed;
for (int num11 = 0; num11 < nodeCount; num11++)
{
Vector3 pos = shell.nodes[num11].pos;
int id = shell.nodes[num11].id;
float num12 = float.MaxValue;
int num13 = -1;
for (int num14 = 0; num14 < count; num14++)
{
if (vertAttr[num14] == id && (array5[num14 * 6] >= 0 || array5[num14 * 6 + 1] >= 0 || array5[num14 * 6 + 2] >= 0 || array5[num14 * 6 + 3] >= 0 || array5[num14 * 6 + 4] >= 0 || array5[num14 * 6 + 5] >= 0))
{
Vector3 val2 = verts[num14] - pos;
float sqrMagnitude = ((Vector3)(ref val2)).sqrMagnitude;
if (sqrMagnitude < num12)
{
num12 = sqrMagnitude;
num13 = num14;
}
}
}
if (num13 >= 0 && !opened[num13])
{
openList[openCount++] = num13;
opened[num13] = true;
}
int num15 = array6[num11];
int num16 = array6[num11 + 1];
int num17 = num16 - num15;
double num18 = 0.0;
bool flag = false;
while (true)
{
int num19 = TraverseRandomVertex(id, ref seed, openList, opened, marked, vertAttr, array5, ref openCount);
if (num19 < 0)
{
break;
}
array7[num15++] = (short)num19;
num18 += 1.0;
array[num19].x = num11;
array[num19].y = (float)(num18 / (double)num17);
if (num15 == num16)
{
flag = true;
break;
}
}
if (flag)
{
openCount = 0;
}
if (num15 < num16)
{
for (int num20 = 0; num20 < count; num20++)
{
if (num15 >= num16)
{
break;
}
if (vertAttr[num20] == id && !marked[num20])
{
array7[num15++] = (short)num20;
num18 += 1.0;
array[num20].x = num11;
array[num20].y = (float)(num18 / (double)num17);
}
}
}
Array.Clear(openList, 0, openList.Length);
Array.Clear(opened, 0, opened.Length);
openCount = 0;
}
shell.verts = verts;
shell.uvs = array;
shell.uv2s = array2;
shell.vkeys = array3;
shell.indices = array4;
shell.vAdjs = array5;
shell.vertAttr = vertAttr;
shell.vertsq = array7;
shell.vertsqOffset = array6;
shell.vertexCount = count;
}
private static bool IsPointInShell(VectorLF3 vertex, List<VectorLF3> polygon, VectorLF3[] polyn, double[] polynu, VectorLF3 raydir, double radius, int count)
{
//IL_0015: Unknown result type (might be due to invalid IL or missing references)
//IL_0028: Unknown result type (might be due to invalid IL or missing references)
//IL_003c: Unknown result type (might be due to invalid IL or missing references)
//IL_0059: Unknown result type (might be due to invalid IL or missing references)
//IL_0060: Unknown result type (might be due to invalid IL or missing references)
//IL_0069: Unknown result type (might be due to invalid IL or missing references)
//IL_0070: Unknown result type (might be due to invalid IL or missing references)
//IL_0079: 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_0089: Unknown result type (might be due to invalid IL or missing references)
//IL_008e: 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_0097: Unknown result type (might be due to invalid IL or missing references)
//IL_00c1: Unknown result type (might be due to invalid IL or missing references)
//IL_00c6: Unknown result type (might be due to invalid IL or missing references)
//IL_00c7: Unknown result type (might be due to invalid IL or missing references)
//IL_00cc: Unknown result type (might be due to invalid IL or missing references)
//IL_00d5: Unknown result type (might be due to invalid IL or missing references)
//IL_00da: Unknown result type (might be due to invalid IL or missing references)
//IL_00db: 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_00e2: 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_00f1: 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_0101: Unknown result type (might be due to invalid IL or missing references)
//IL_0108: Unknown result type (might be due to invalid IL or missing references)
//IL_012d: Unknown result type (might be due to invalid IL or missing references)
//IL_013f: Unknown result type (might be due to invalid IL or missing references)
//IL_0151: Unknown result type (might be due to invalid IL or missing references)
int num = 0;
for (int i = 0; i < count; i++)
{
double num2 = (0.0 - (polyn[i].x * vertex.x + polyn[i].y * vertex.y + polyn[i].z * vertex.z)) / polynu[i];
if (!(num2 < 0.0))
{
VectorLF3 val = new VectorLF3(vertex.x + num2 * raydir.x, vertex.y + num2 * raydir.y, vertex.z + num2 * raydir.z);
VectorLF3 normalized = ((VectorLF3)(ref val)).normalized;
normalized.x *= radius;
normalized.y *= radius;
normalized.z *= radius;
VectorLF3 val2 = polygon[i] - normalized;
VectorLF3 val3 = polygon[(i + 1) % count] - normalized;
double num3 = val2.x * val3.x + val2.y * val3.y + val2.z * val3.z;
if (num3 < 0.0 || (num3 == 0.0 && val2.x == 0.0 && val2.y == 0.0 && val2.z == 0.0))
{
num++;
}
}
}
return (num & 1) == 1;
}
private static int TraverseRandomVertex(int attr, ref int seed, int[] openList, bool[] opened, bool[] marked, int[] vertAttr, short[] vAdjs, ref int openCount)
{
if (openCount <= 0)
{
return -1;
}
int num = RandomTable.Integer(ref seed, openCount);
int num2 = openList[num];
opened[num2] = false;
if (num < openCount - 1)
{
Array.Copy(openList, num + 1, openList, num, openCount - num - 1);
}
openCount--;
marked[num2] = true;
for (int i = 0; i < 6; i++)
{
int num3 = vAdjs[num2 * 6 + i];
if (num3 >= 0 && !marked[num3] && !opened[num3] && vertAttr[num3] == attr)
{
openList[openCount++] = num3;
opened[num3] = true;
}
}
return num2;
}
}
[HarmonyPatch]
internal static class LayerImportPatch
{
private static readonly FieldRef<DysonSphereLayer, int> RefNodeCursor = AccessTools.FieldRefAccess<DysonSphereLayer, int>("nodeCursor");
private static readonly FieldRef<DysonSphereLayer, int> RefNodeRecycleCursor = AccessTools.FieldRefAccess<DysonSphereLayer, int>("nodeRecycleCursor");
private static readonly FieldRef<DysonSphereLayer, int> RefFrameCursor = AccessTools.FieldRefAccess<DysonSphereLayer, int>("frameCursor");
private static readonly FieldRef<DysonSphereLayer, int> RefFrameRecycleCursor = AccessTools.FieldRefAccess<DysonSphereLayer, int>("frameRecycleCursor");
private static readonly FieldRef<DysonSphereLayer, int> RefShellCursor = AccessTools.FieldRefAccess<DysonSphereLayer, int>("shellCursor");
private static readonly FieldRef<DysonSphereLayer, int> RefShellRecycleCursor = AccessTools.FieldRefAccess<DysonSphereLayer, int>("shellRecycleCursor");
private static readonly MethodInfo SetNodeCapacity = AccessTools.Method(typeof(DysonSphereLayer), "SetNodeCapacity", (Type[])null, (Type[])null);
private static readonly MethodInfo SetFrameCapacity = AccessTools.Method(typeof(DysonSphereLayer), "SetFrameCapacity", (Type[])null, (Type[])null);
private static readonly MethodInfo SetShellCapacity = AccessTools.Method(typeof(DysonSphereLayer), "SetShellCapacity", (Type[])null, (Type[])null);
private static readonly FieldRef<DysonSphere, DysonNodeRData[]> RefPool = AccessTools.FieldRefAccess<DysonSphere, DysonNodeRData[]>("nrdPool");
private static readonly FieldRef<DysonSphere, ComputeBuffer> RefBuffer = AccessTools.FieldRefAccess<DysonSphere, ComputeBuffer>("nrdBuffer");
[HarmonyPrefix]
[HarmonyPatch(typeof(DysonSphereLayer), "ImportFromBlueprint")]
private static bool ImportFromBlueprint_Prefix(DysonSphereLayer __instance, BinaryReader r, DysonSphere sphere)
{
if (!DysonShellOptimizerPlugin.Enabled.Value)
{
return true;
}
try
{
Import(__instance, r, sphere);
}
catch (Exception ex)
{
DysonShellOptimizerPlugin.Log.LogError((object)("[Geometry] ImportFromBlueprint failed: " + ex));
throw;
}
return false;
}
private static void Import(DysonSphereLayer layer, BinaryReader r, DysonSphere sphere)
{
//IL_0088: Unknown result type (might be due to invalid IL or missing references)
//IL_008f: Expected O, but got Unknown
//IL_017a: Unknown result type (might be due to invalid IL or missing references)
//IL_0181: Expected O, but got Unknown
//IL_026f: Unknown result type (might be due to invalid IL or missing references)
//IL_0276: Expected O, but got Unknown
//IL_033c: Unknown result type (might be due to invalid IL or missing references)
//IL_0341: Unknown result type (might be due to invalid IL or missing references)
//IL_0709: Unknown result type (might be due to invalid IL or missing references)
//IL_071f: Unknown result type (might be due to invalid IL or missing references)
//IL_0724: Unknown result type (might be due to invalid IL or missing references)
//IL_0729: Unknown result type (might be due to invalid IL or missing references)
//IL_0730: Unknown result type (might be due to invalid IL or missing references)
//IL_0753: Unknown result type (might be due to invalid IL or missing references)
//IL_0758: Unknown result type (might be due to invalid IL or missing references)
//IL_075d: Unknown result type (might be due to invalid IL or missing references)
Stopwatch stopwatch = Stopwatch.StartNew();
int num = r.ReadInt32();
layer.ResetNew();
PerformanceMonitor.BeginData((ESaveDataEntry)35);
int num2 = r.ReadInt32();
SetNodeCapacity.Invoke(layer, new object[1] { num2 });
RefNodeCursor.Invoke(layer) = r.ReadInt32();
RefNodeRecycleCursor.Invoke(layer) = r.ReadInt32();
for (int i = 1; i < layer.nodeCursor; i++)
{
int num3 = r.ReadInt32();
if (num3 != 0)
{
if (num3 != i)
{
throw new Exception("node id doesn't match!");
}
DysonNode val = new DysonNode();
val.ImportAsBlueprint(r, sphere, layer.id);
if (val.id != i)
{
throw new Exception("node id doesn't match!");
}
layer.nodePool[i] = val;
sphere.AddDysonNodeRData(layer.nodePool[i], false);
}
}
for (int j = 0; j < layer.nodeRecycleCursor; j++)
{
layer.nodeRecycle[j] = r.ReadInt32();
}
PerformanceMonitor.EndData((ESaveDataEntry)35);
PerformanceMonitor.BeginData((ESaveDataEntry)36);
num2 = r.ReadInt32();
SetFrameCapacity.Invoke(layer, new object[1] { num2 });
RefFrameCursor.Invoke(layer) = r.ReadInt32();
RefFrameRecycleCursor.Invoke(layer) = r.ReadInt32();
for (int k = 1; k < layer.frameCursor; k++)
{
int num4 = r.ReadInt32();
if (num4 != 0)
{
if (num4 != k)
{
throw new Exception("frame id doesn't match!");
}
DysonFrame val2 = new DysonFrame();
val2.ImportFromBlueprint(r, sphere, layer.id);
if (val2.id != k)
{
throw new Exception("frame id doesn't match!");
}
layer.framePool[k] = val2;
}
}
for (int l = 0; l < layer.frameRecycleCursor; l++)
{
layer.frameRecycle[l] = r.ReadInt32();
}
PerformanceMonitor.EndData((ESaveDataEntry)36);
PerformanceMonitor.BeginData((ESaveDataEntry)37);
num2 = r.ReadInt32();
SetShellCapacity.Invoke(layer, new object[1] { num2 });
RefShellCursor.Invoke(layer) = r.ReadInt32();
RefShellRecycleCursor.Invoke(layer) = r.ReadInt32();
List<DysonShell> shells = new List<DysonShell>();
GeometryCore.BatchImporting = true;
try
{
for (int m = 1; m < layer.shellCursor; m++)
{
int num5 = r.ReadInt32();
if (num5 != 0)
{
if (num5 != m)
{
throw new Exception("shell id doesn't match!");
}
DysonShell val3 = new DysonShell(layer);
val3.ImportFromBlueprint(r, sphere, layer.id);
if (val3.id != m)
{
throw new Exception("shell id doesn't match!");
}
layer.shellPool[m] = val3;
shells.Add(val3);
}
}
}
finally
{
GeometryCore.BatchImporting = false;
}
for (int n = 0; n < layer.shellRecycleCursor; n++)
{
layer.shellRecycle[n] = r.ReadInt32();
}
if (num >= 1)
{
layer.paintingGridMode = r.ReadInt32();
if (r.ReadBoolean())
{
int num6 = r.ReadInt32();
Color32[] array = (Color32[])(object)new Color32[num6];
for (int num7 = 0; num7 < num6; num7++)
{
array[num7] = new Color32(r.ReadByte(), r.ReadByte(), r.ReadByte(), r.ReadByte());
}
layer.SetPaintingData(array);
}
}
PerformanceMonitor.EndData((ESaveDataEntry)37);
long elapsedMilliseconds = stopwatch.ElapsedMilliseconds;
FlushNodeBuffer(sphere);
long num8 = 0L;
long num9 = 0L;
long num10 = 0L;
bool flag = DysonShellOptimizerPlugin.FreeGeometryAfterPaste != null && DysonShellOptimizerPlugin.FreeGeometryAfterPaste.Value;
if (shells.Count > 0)
{
int num11 = ((DysonShellOptimizerPlugin.GeometryBatchMultiplier == null) ? 4 : Math.Max(1, DysonShellOptimizerPlugin.GeometryBatchMultiplier.Value));
int num12 = Math.Max(1, Environment.ProcessorCount * num11);
bool flag2 = DysonShellOptimizerPlugin.ParallelAcrossShells.Value && shells.Count >= 8;
ParallelOptions parallelOptions = new ParallelOptions
{
MaxDegreeOfParallelism = Math.Min(Environment.ProcessorCount, Math.Max(1, num12))
};
int start;
for (start = 0; start < shells.Count; start += num12)
{
int num13 = Math.Min(num12, shells.Count - start);
long timestamp = Stopwatch.GetTimestamp();
if (flag2)
{
Parallel.For(0, num13, parallelOptions, delegate(int offset)
{
GeometryCore.Slot slot2 = GeometryCore.RentSlot();
try
{
GeometryCore.Generate(shells[start + offset], slot2);
}
finally
{
GeometryCore.ReturnSlot(slot2);
}
});
}
else
{
GeometryCore.Slot slot = GeometryCore.RentSlot();
try
{
for (int num14 = 0; num14 < num13; num14++)
{
GeometryCore.Generate(shells[start + num14], slot);
}
}
finally
{
GeometryCore.ReturnSlot(slot);
}
}
long timestamp2 = Stopwatch.GetTimestamp();
for (int num15 = 0; num15 < num13; num15++)
{
shells[start + num15].GenerateModelObjects();
}
long timestamp3 = Stopwatch.GetTimestamp();
if (flag)
{
for (int num16 = 0; num16 < num13; num16++)
{
FreeGeometry.ReleaseVerticesOnly(shells[start + num16]);
}
}
long timestamp4 = Stopwatch.GetTimestamp();
num8 += (long)Math.Round((double)(timestamp2 - timestamp) * 1000.0 / (double)Stopwatch.Frequency);
num9 += (long)Math.Round((double)(timestamp3 - timestamp2) * 1000.0 / (double)Stopwatch.Frequency);
num10 += (long)Math.Round((double)(timestamp4 - timestamp3) * 1000.0 / (double)Stopwatch.Frequency);
if (DysonShellOptimizerPlugin.LogTimings.Value)
{
DysonShellOptimizerPlugin.Log.LogInfo((object)("[Geometry] batch layer=" + layer.id + " done=" + (start + num13) + "/" + shells.Count + " gen=" + Math.Round((double)(timestamp2 - timestamp) * 1000.0 / (double)Stopwatch.Frequency) + " model=" + Math.Round((double)(timestamp3 - timestamp2) * 1000.0 / (double)Stopwatch.Frequency) + " heap=" + GC.GetTotalMemory(forceFullCollection: false) / 1048576 + " MB"));
}
}
if (flag)
{
FreeGeometry.MarkReleased(sphere);
}
}
_ = stopwatch.ElapsedMilliseconds;
for (int num17 = 1; num17 < layer.nodeCursor; num17++)
{
if (layer.nodePool[num17] != null && layer.nodePool[num17].id == num17)
{
layer.nodePool[num17].RecalcSpReq();
layer.nodePool[num17].RecalcCpReq();
}
}
layer.currentRotation = layer.orbitRotation * Quaternion.Euler(0f, 0f - layer.currentAngle, 0f);
layer.nextRotation = layer.orbitRotation * Quaternion.Euler(0f, 0f - layer.currentAngle - layer.orbitAngularSpeed * (1f / 60f), 0f);
if (!layer.CheckLayerNodeCount())
{
Debug.LogError((object)"Blueprint data corrupted");
}
if (DysonShellOptimizerPlugin.LogTimings.Value)
{
DysonShellOptimizerPlugin.Log.LogInfo((object)("[Geometry] paste layer=" + layer.id + " shells=" + shells.Count + " parallel=" + (DysonShellOptimizerPlugin.ParallelAcrossShells.Value && shells.Count >= 8) + " parse=" + elapsedMilliseconds + " geom=" + num8 + " model=" + num9 + " free=" + num10 + " cpu=" + CpuShare(num8, num9) + " total=" + stopwatch.ElapsedMilliseconds + "ms"));
}
}
private static string CpuShare(long geometryMs, long modelMs)
{
long num = geometryMs + modelMs;
if (num <= 0)
{
return "n/a";
}
double num2 = geometryMs * Environment.ProcessorCount + modelMs;
double num3 = (double)num * (double)Environment.ProcessorCount;
return (num2 / num3).ToString("0.0%");
}
private static void FlushNodeBuffer(DysonSphere sphere)
{
if (DysonShellOptimizerPlugin.BatchNodeBufferUpload == null || !DysonShellOptimizerPlugin.BatchNodeBufferUpload.Value)
{
return;
}
try
{
ComputeBuffer val = RefBuffer.Invoke(sphere);
DysonNodeRData[] array = RefPool.Invoke(sphere);
if (val != null && array != null)
{
val.SetData((Array)array);
}
}
catch (Exception ex)
{
DysonShellOptimizerPlugin.Log.LogWarning((object)("[Geometry] node buffer flush failed: " + ex.Message));
}
}
}