Major refactor and cleanup as we look towards reuse.

This commit is contained in:
2025-02-11 11:03:48 -05:00
parent 5db025bb47
commit af97dbf6e8
6 changed files with 138 additions and 128 deletions

View File

@ -86,11 +86,11 @@ namespace IngameScript
private const int CooldownTicks = 120;
private const int SealTimeoutTicks = 30;
public Airlock(MyIni ini, Console console, string name)
public Airlock(string name, IConsoleProgram _program)
{
_ini = ini;
_ini = _program.Ini;
_name = name;
_console = console.CreatePrefixedConsole(_name);
_console = new PrefixedConsole(_program.Console, _name);
_lights = new List<IMyLightingBlock>();
// _displays = new List<IMyTextSurface>();
}

View File

@ -5,20 +5,19 @@ using VRage.Game.ModAPI.Ingame.Utilities;
namespace IngameScript
{
public partial class Program : MyGridProgram
public partial class Program : MyGridProgram, IConsoleProgram
{
public IConsole Console { get; private set; }
public MyIni Ini { get; private set; }
private Dictionary<string, Airlock> _airlocks;
private List<IEnumerator<bool>> _jobs;
private int _tickCount = 0;
private MyCommandLine _cli;
private Console _console;
private MyIni _ini;
public Program()
{
_ini = new MyIni();
_console = new Console(this, _ini);
Ini = new MyIni();
Console = new MainConsole(this, "Airlock Controller");
_cli = new MyCommandLine();
_jobs = new List<IEnumerator<bool>>();
_airlocks = new Dictionary<string, Airlock>();
@ -28,25 +27,26 @@ namespace IngameScript
IMyAirVent referenceVent = null;
foreach (IMyTerminalBlock block in airlockBlocks)
{
_ini.TryParse(block.CustomData, "airlock");
Ini.TryParse(block.CustomData, "airlock");
// TODO: redundant reference vents would be awesome. Everyone loves redundancy
if (block is IMyAirVent && _ini.Get("airlock", "reference").ToBoolean())
if (block is IMyAirVent && Ini.Get("airlock", "reference").ToBoolean())
{
if (referenceVent != null) {
_console.Print("Found multiple reference vents. Only the first one will be used.");
if (referenceVent != null)
{
Console.Print("Found multiple reference vents. Only the first one will be used.");
continue;
}
referenceVent = block as IMyAirVent;
_console.Print($"Found reference vent {block.CustomName}.");
Console.Print($"Found reference vent {block.CustomName}.");
continue;
}
string airlockName = _ini.Get("airlock", "id").ToString();
string airlockName = Ini.Get("airlock", "id").ToString();
if (!_airlocks.ContainsKey(airlockName))
{
_airlocks[airlockName] = new Airlock(_ini, _console, airlockName);
_airlocks[airlockName] = new Airlock(airlockName, this);
}
_airlocks[airlockName].AddBlock(block);
@ -54,23 +54,23 @@ namespace IngameScript
if (referenceVent != null) foreach (Airlock airlock in _airlocks.Values) { airlock.ReferenceVent = referenceVent; }
_console.Print($"Found {_airlocks.Count} airlocks.");
_console.PrintLower($"Airlock Controller\nTotal Ticks: 0");
Console.Print($"Found {_airlocks.Count} airlocks.");
Console.PrintLower($"Airlock Controller\nTotal Ticks: 0");
}
public void Main(string argument, UpdateType updateSource)
{
_console.PrintLower($"Airlock Controller\nTotal Ticks: {++_tickCount}");
Console.UpdateTickCount();
if (updateSource == UpdateType.Trigger || updateSource == UpdateType.Terminal)
{
_cli.TryParse(argument);
if (_cli.ArgumentCount == 0) { _console.Print("You must provide an airlock ID."); }
if (_cli.ArgumentCount == 0) { Console.Print("You must provide an airlock ID."); }
else
{
string airlockName = _cli.Argument(0);
if (!_airlocks.ContainsKey(airlockName)) _console.Print($"Airlock ID '{airlockName}' not found.");
else if (!_airlocks[airlockName].Functional) _console.Print($"Airlock '{airlockName}' is not functional.");
if (!_airlocks.ContainsKey(airlockName)) Console.Print($"Airlock ID '{airlockName}' not found.");
else if (!_airlocks[airlockName].Functional) Console.Print($"Airlock '{airlockName}' is not functional.");
else
{
_jobs.Add(_airlocks[airlockName].CycleAirlock());
@ -86,7 +86,7 @@ namespace IngameScript
job.Dispose();
_jobs.Remove(job);
i--;
_console.Print("Job Removed From Queue.");
Console.Print("Job Removed From Queue.");
}
if (_jobs.Count == 0) Runtime.UpdateFrequency = UpdateFrequency.None;