Skip to content

Commit

Permalink
Merge pull request #16 from TheNicestGuy/dev
Browse files Browse the repository at this point in the history
Command parsing and rediscover command
  • Loading branch information
TheNicestGuy authored Aug 11, 2019
2 parents 2c0b100 + 156f6d4 commit 7b42eb4
Showing 1 changed file with 88 additions and 12 deletions.
100 changes: 88 additions & 12 deletions Program.cs
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,9 @@ partial class Program : MyGridProgram
private Dictionary<string, Airlock> _allAirlocks;

private MyIni _theIniParser = new MyIni();
private MyCommandLine _theCommandParser = new MyCommandLine();
private readonly IDictionary<string, Action> _knownCommands
= new Dictionary<string, Action>(StringComparer.InvariantCultureIgnoreCase);

#endregion Program / Private Members

Expand All @@ -42,32 +45,105 @@ partial class Program : MyGridProgram

public Program()
{
List<IMyTerminalBlock> allAirlockBlocks = new List<IMyTerminalBlock>();
this.GridTerminalSystem.GetBlocksOfType<IMyTerminalBlock>(
allAirlockBlocks
, block => MyIni.HasSection(block.CustomData, INI_SECTION_NAME)
);
this._allAirlocks =
new Dictionary<string, Airlock>(Airlock.DiscoverAllAirlocks(
allAirlockBlocks
, this._theIniParser
, INI_SECTION_NAME
));
// Map known commands to methods
this._knownCommands["rediscover"] = RediscoverCommand;

// Perform initial discovery
this.RediscoverCommand(true);
}

public void Save()
{
}

public void Main(string argument, UpdateType updateSource)
{
bool defaultRun = true;

if (this._theCommandParser.TryParse(argument))
{
Action commandAction;
if (this._theCommandParser.ArgumentCount > 0)
{
string commandName = this._theCommandParser.Argument(0);
if (null != commandName)
{
defaultRun = false;
if (this._knownCommands.TryGetValue(commandName, out commandAction))
{
commandAction();
}
else
{
throw new Exception(
$"Command '{commandName}' does not exist."
);
}
} // check and map command name
} // if arguments existed
} // if could parse command line

if (defaultRun) {
ReportStatus();
}
} // Main()

private void ReportStatus()
{
Echo(
$"Found {this._allAirlocks.Count().ToString()} airlock(s)."
$"Found {this._allAirlocks.Count.ToString()} airlock(s)."
);
foreach (Airlock a in this._allAirlocks.Values)
{
Echo($"{a.Name} - {(a.IsComplete() ? "valid" : a.BadConfigReport)}");
}
}

#region Command Implementations

private void RediscoverCommand() { this.RediscoverCommand(false); }
private void RediscoverCommand(bool forceAll)
{
List<IMyTerminalBlock> allAirlockBlocks = new List<IMyTerminalBlock>();
this.GridTerminalSystem.GetBlocksOfType<IMyTerminalBlock>(
allAirlockBlocks
, block => MyIni.HasSection(block.CustomData, INI_SECTION_NAME)
);

// The rediscover command takes one optional argument that specifies
// a single airlock to rediscover. forceAll overrides this and
// rediscovers all regardless.
if (this._theCommandParser.ArgumentCount > 1 && !forceAll)
{
// Rediscover one airlock
string airlockName = this._theCommandParser.Argument(1);
IDictionary<string, Airlock> tempAirlocks =
Airlock.DiscoverAllAirlocks(
allAirlockBlocks
, this._theIniParser
, INI_SECTION_NAME
)
;
this._allAirlocks.Remove(airlockName);
if (tempAirlocks.ContainsKey(airlockName))
{
this._allAirlocks[airlockName] = tempAirlocks[airlockName];
}
}
else
{
// Rediscover all airlocks
this._allAirlocks =
new Dictionary<string, Airlock>(Airlock.DiscoverAllAirlocks(
allAirlockBlocks
, this._theIniParser
, INI_SECTION_NAME
));
}

ReportStatus();
} // RediscoverCommand(bool forceAll)

#endregion Command Implemenations
}
}

0 comments on commit 7b42eb4

Please sign in to comment.