--- /dev/null
+using Content.Server.Administration;
+using Content.Server.Power.EntitySystems;
+using Content.Shared.Administration;
+using Robust.Shared.Console;
+
+namespace Content.Server.Power.Commands;
+
+[AdminCommand(AdminFlags.Debug)]
+public sealed class PowerValidateCommand : LocalizedEntityCommands
+{
+ [Dependency] private readonly PowerNetSystem _powerNet = null!;
+
+ public override string Command => "power_validate";
+
+ public override void Execute(IConsoleShell shell, string argStr, string[] args)
+ {
+ try
+ {
+ _powerNet.Validate();
+ }
+ catch (Exception e)
+ {
+ shell.WriteLine(LocalizationManager.GetString("cmd-power_validate-error", ("err", e.ToString())));
+ return;
+ }
+
+ shell.WriteLine(LocalizationManager.GetString("cmd-power_validate-success"));
+ }
+}
-using System.Diagnostics;
+using System.Diagnostics.CodeAnalysis;
using Robust.Shared.Utility;
using System.Linq;
+using System.Runtime.CompilerServices;
+using JetBrains.Annotations;
using Robust.Shared.Threading;
using static Content.Server.Power.Pow3r.PowerState;
DebugTools.Assert(state.GroupedNets.Select(x => x.Count).Sum() == state.Networks.Count);
_networkJob.State = state;
_networkJob.FrameTime = frameTime;
+#if DEBUG
ValidateNetworkGroups(state, state.GroupedNets);
+#endif
// Each network height layer can be run in parallel without issues.
foreach (var group in state.GroupedNets)
RecursivelyEstimateNetworkDepth(state, network, groupedNetworks);
}
- ValidateNetworkGroups(state, groupedNetworks);
return groupedNetworks;
}
+ public void Validate(PowerState state)
+ {
+ if (state.GroupedNets == null)
+ throw new InvalidOperationException("We don't have grouped networks cached??");
+
+ ValidateNetworkGroups(state, state.GroupedNets);
+ }
+
/// <summary>
/// Validate that network grouping is up to date. I.e., that it is safe to solve each networking in a given
/// group in parallel. This assumes that batteries are the only device that connects to multiple networks, and
/// is thus the only obstacle to solving everything in parallel.
/// </summary>
- [Conditional("DEBUG")]
private void ValidateNetworkGroups(PowerState state, List<List<Network>> groupedNetworks)
{
HashSet<Network> nets = new();
continue;
}
- DebugTools.Assert(!nets.Contains(subNet));
- DebugTools.Assert(!netIds.Contains(subNet.Id));
- DebugTools.Assert(subNet.Height < net.Height);
+ Check(!nets.Contains(subNet), $"Net {net.Id}, battery {batteryId}");
+ Check(!netIds.Contains(subNet.Id), $"Net {net.Id}, battery {batteryId}");
+ Check(subNet.Height < net.Height, $"Net {net.Id}, battery {batteryId}");
}
foreach (var batteryId in net.BatterySupplies)
continue;
}
- DebugTools.Assert(!nets.Contains(parentNet));
- DebugTools.Assert(!netIds.Contains(parentNet.Id));
- DebugTools.Assert(parentNet.Height > net.Height);
+ Check(!nets.Contains(parentNet), $"Net {net.Id}, battery {batteryId}");
+ Check(!netIds.Contains(parentNet.Id), $"Net {net.Id}, battery {batteryId}");
+ Check(parentNet.Height > net.Height, $"Net {net.Id}, battery {batteryId}");
}
- DebugTools.Assert(nets.Add(net));
- DebugTools.Assert(netIds.Add(net.Id));
+ Check(nets.Add(net), $"Net {net.Id}");
+ Check(netIds.Add(net.Id), $"Net {net.Id}");
}
}
+
+ return;
+
+ // Most readable C# function def.
+ [AssertionMethod]
+ static void Check(
+ [AssertionCondition(AssertionConditionType.IS_TRUE)]
+ [DoesNotReturnIf(false)]
+ bool condition,
+ [InterpolatedStringHandlerArgument("condition")]
+ ref DebugTools.AssertInterpolatedStringHandler handler,
+ [CallerArgumentExpression(nameof(condition))]
+ string check = "")
+ {
+ if (!condition)
+ throw new DebugAssertException($"{handler.ToStringAndClear()}: failed check: {check}");
+ }
}
private static void RecursivelyEstimateNetworkDepth(PowerState state, Network network, List<List<Network>> groupedNetworks)