using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Reflection;
using System.Runtime.CompilerServices;
using System.Runtime.Versioning;
using BepInEx;
using HarmonyLib;
using Microsoft.CodeAnalysis;
using PowerNetworkStructures;
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")]
[module: RefSafetyRules(11)]
namespace Microsoft.CodeAnalysis
{
[CompilerGenerated]
[Embedded]
internal sealed class EmbeddedAttribute : Attribute
{
}
}
namespace System.Runtime.CompilerServices
{
[CompilerGenerated]
[Embedded]
[AttributeUsage(AttributeTargets.Module, AllowMultiple = false, Inherited = false)]
internal sealed class RefSafetyRulesAttribute : Attribute
{
public readonly int Version;
public RefSafetyRulesAttribute(int P_0)
{
Version = P_0;
}
}
}
internal static class PluginInfo
{
public const string PLUGIN_GUID = "dspdev.gridone";
public const string PLUGIN_NAME = "GridOne";
public const string PLUGIN_VERSION = "1.0.0";
}
namespace GridOne
{
[BepInPlugin("dspdev.gridone", "GridOne", "1.0.0")]
public sealed class Plugin : BaseUnityPlugin
{
private void Awake()
{
//IL_0005: Unknown result type (might be due to invalid IL or missing references)
new Harmony("dspdev.gridone").PatchAll(typeof(Plugin).Assembly);
}
}
[HarmonyPatch]
internal static class GridPatches
{
private sealed class GridState
{
public Node ConsumerOwner;
}
private static readonly ConditionalWeakTable<PowerSystem, GridState> States = new ConditionalWeakTable<PowerSystem, GridState>();
private static readonly IComparer<Node> NodeOrder = Comparer<Node>.Create((Node left, Node right) => left.id.CompareTo(right.id));
[HarmonyPrefix]
[HarmonyPatch(typeof(PowerSystem), "OnNodeAdded")]
private static bool AddNode(PowerSystem __instance, int nodeId, int[] ___netRecycle, int ___netRecycleCursor)
{
//IL_003e: Unknown result type (might be due to invalid IL or missing references)
//IL_0043: 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_0055: Unknown result type (might be due to invalid IL or missing references)
//IL_005a: Unknown result type (might be due to invalid IL or missing references)
//IL_005c: Unknown result type (might be due to invalid IL or missing references)
//IL_005e: Unknown result type (might be due to invalid IL or missing references)
//IL_0063: Unknown result type (might be due to invalid IL or missing references)
//IL_0081: Unknown result type (might be due to invalid IL or missing references)
//IL_0086: 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_008f: 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_009c: Unknown result type (might be due to invalid IL or missing references)
//IL_00a8: 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_00b5: 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_00bc: 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_00c9: Unknown result type (might be due to invalid IL or missing references)
//IL_00cf: 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_00dc: 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_00e9: 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_00f6: Unknown result type (might be due to invalid IL or missing references)
//IL_0104: Expected O, but got Unknown
GridState value = States.GetValue(__instance, (PowerSystem _) => new GridState());
PowerNetwork val = EnsureGrid(__instance, value, ___netRecycle, ___netRecycleCursor);
PowerNodeComponent val2 = __instance.nodePool[nodeId];
EntityData val3 = __instance.factory.entityPool[val2.entityId];
Vector3 val4 = val3.pos / (val3.alt / (__instance.factory.planet.realRadius + 0.2f));
Node val5 = new Node(nodeId)
{
x = val4.x,
y = val4.y,
z = val4.z,
connDistance2 = val2.connectDistance * val2.connectDistance,
coverRadius2 = val2.coverRadius * val2.coverRadius,
genId = val3.powerGenId,
accId = val3.powerAccId,
excId = val3.powerExcId
};
val.nodes.Insert(~val.nodes.BinarySearch(val5, NodeOrder), val5);
__instance.nodePool[nodeId].networkId = 1;
if (val5.genId > 0)
{
Insert(val.generators, val5.genId);
__instance.genPool[val5.genId].networkId = 1;
}
if (val5.accId > 0)
{
Insert(val.accumulators, val5.accId);
__instance.accPool[val5.accId].networkId = 1;
}
if (val5.excId > 0)
{
Insert(val.exchangers, val5.excId);
__instance.excPool[val5.excId].networkId = 1;
}
if (value.ConsumerOwner == null)
{
value.ConsumerOwner = val5;
val5.consumers.AddRange(val.consumers);
}
return false;
}
[HarmonyPrefix]
[HarmonyPatch(typeof(PowerSystem), "OnConsumerAdded")]
private static bool AddConsumer(PowerSystem __instance, int consumerId)
{
if (!States.TryGetValue(__instance, out var value) || value.ConsumerOwner == null)
{
return true;
}
Insert(__instance.netPool[1].consumers, consumerId);
Insert(value.ConsumerOwner.consumers, consumerId);
__instance.consumerPool[consumerId].networkId = 1;
return false;
}
[HarmonyPrefix]
[HarmonyPatch(typeof(PowerSystem), "OnConsumerRemoving")]
private static bool RemoveConsumer(PowerSystem __instance, int consumerId)
{
if (!States.TryGetValue(__instance, out var value) || value.ConsumerOwner == null)
{
return true;
}
Remove(__instance.netPool[1].consumers, consumerId);
Remove(value.ConsumerOwner.consumers, consumerId);
return false;
}
[HarmonyPrefix]
[HarmonyPatch(typeof(PowerSystem), "OnNodeRemoving")]
private static bool RemoveNode(PowerSystem __instance, int nodeId, int[] ___netRecycle, int ___netRecycleCursor)
{
//IL_003d: Unknown result type (might be due to invalid IL or missing references)
//IL_0042: Unknown result type (might be due to invalid IL or missing references)
//IL_0053: Expected O, but got Unknown
GridState value = States.GetValue(__instance, (PowerSystem _) => new GridState());
PowerNetwork val = EnsureGrid(__instance, value, ___netRecycle, ___netRecycleCursor);
int index = val.nodes.BinarySearch(new Node
{
id = nodeId
}, NodeOrder);
Node val2 = val.nodes[index];
val.nodes.RemoveAt(index);
Remove(val.generators, val2.genId);
Remove(val.accumulators, val2.accId);
Remove(val.exchangers, val2.excId);
if (val.nodes.Count == 0)
{
List<int> consumers = __instance.netPool[0].consumers;
foreach (int consumer in val.consumers)
{
__instance.consumerPool[consumer].networkId = 0;
}
consumers.AddRange(val.consumers);
consumers.Sort();
value.ConsumerOwner = null;
__instance.RemoveNetwork(1);
}
else if (value.ConsumerOwner == val2)
{
value.ConsumerOwner = val.nodes[0];
value.ConsumerOwner.consumers = val2.consumers;
}
return false;
}
private static PowerNetwork EnsureGrid(PowerSystem system, GridState state, int[] recycled, int recycleCount)
{
if (state.ConsumerOwner != null)
{
return system.netPool[1];
}
if (system.netCursor == 1)
{
system.NewNetwork();
}
else
{
PowerNetwork obj = system.netPool[1];
if (obj == null || obj.id != 1)
{
int num = Array.IndexOf(recycled, 1, 0, recycleCount);
if (num < 0)
{
throw new InvalidOperationException("Power network slot 1 is missing from the recycle stack.");
}
recycled[num] = recycled[recycleCount - 1];
recycled[recycleCount - 1] = 1;
system.NewNetwork();
}
}
PowerNetwork val = system.netPool[1];
val.consumers.AddRange(system.netPool[0].consumers);
system.netPool[0].consumers.Clear();
for (int i = 2; i < system.netCursor; i++)
{
PowerNetwork val2 = system.netPool[i];
if (val2 != null && val2.id == i)
{
val.nodes.AddRange(val2.nodes);
val.consumers.AddRange(val2.consumers);
val.generators.AddRange(val2.generators);
val.accumulators.AddRange(val2.accumulators);
val.exchangers.AddRange(val2.exchangers);
system.RemoveNetwork(i);
}
}
val.nodes.Sort(NodeOrder);
val.consumers.Sort();
val.generators.Sort();
val.accumulators.Sort();
val.exchangers.Sort();
foreach (Node node in val.nodes)
{
node.conns.Clear();
node.lines.Clear();
node.consumers.Clear();
system.nodePool[node.id].networkId = 1;
}
foreach (int consumer in val.consumers)
{
system.consumerPool[consumer].networkId = 1;
}
foreach (int generator in val.generators)
{
system.genPool[generator].networkId = 1;
}
foreach (int accumulator in val.accumulators)
{
system.accPool[accumulator].networkId = 1;
}
foreach (int exchanger in val.exchangers)
{
system.excPool[exchanger].networkId = 1;
}
if (val.nodes.Count > 0)
{
state.ConsumerOwner = val.nodes[0];
state.ConsumerOwner.consumers.AddRange(val.consumers);
}
return val;
}
private static void Insert(List<int> members, int id)
{
int num = members.BinarySearch(id);
if (num < 0)
{
members.Insert(~num, id);
}
}
private static void Remove(List<int> members, int id)
{
int num = members.BinarySearch(id);
if (num >= 0)
{
members.RemoveAt(num);
}
}
}
}