extracommands/extracommands/ChainCommandEngine.cs

96 lines
3.5 KiB
C#

using System;
using RobocraftX.GUI.CommandLine;
using RobocraftX.Multiplayer;
using RobocraftX.StateSync;
using RobocraftX.Character;
using Svelto.ECS;
using Unity.Entities;
using UnityEngine;
using uREPL;
using Svelto.Context;
using RobocraftX;
namespace ExtraCommands.Basics
{
[CustomCommand]
class ChainCommandEngine : CustomCommandEngine
{
public ChainCommandEngine(UnityContext<FullGameCompositionRoot> ctxHolder, EnginesRoot enginesRoot, World physW, Action reloadGame, MultiplayerInitParameters mpParams) : base(ctxHolder, enginesRoot, physW, reloadGame, mpParams)
{
}
public override void Ready()
{
CustomCommandUtility.Register<string, string>("Chain", ChainCommand, "Run two commands, one after the other");
CustomCommandUtility.Register<string, string>("ChainNoFail", ChainNoFailCommand, "Run two commands, one after the other even if the first one is invalid");
CustomCommandUtility.Register<string, string>("ChainQuiet", ChainQuietCommand, "Run two commands, one after the other quietly");
}
private void ChainCommand(string command1, string command2)
{
string command1a = decomma(command1);
string command2a = decomma(command2);
bool success1 = uREPL.Evaluator.Evaluate(command1a).type == CompileResult.Type.Success;
if (!success1) {
uREPL.Log.Error("First command was not executed successfully");
return;
}
bool success2 = uREPL.Evaluator.Evaluate(command2a).type == CompileResult.Type.Success;
if (!success2) {
uREPL.Log.Error("Second command was not executed successfully");
}
}
private void ChainNoFailCommand(string command1, string command2)
{
string command1a = decomma(command1);
string command2a = decomma(command2);
bool success1 = uREPL.Evaluator.Evaluate(command1a).type == CompileResult.Type.Success;
if (!success1) {
uREPL.Log.Error("First command was not executed successfully");
}
bool success2 = uREPL.Evaluator.Evaluate(command2a).type == CompileResult.Type.Success;
if (!success2) {
uREPL.Log.Error("Second command was not executed successfully");
}
}
private void ChainQuietCommand(string command1, string command2)
{
string command1a = decomma(command1);
string command2a = decomma(command2);
uREPL.Evaluator.Evaluate(command1a);
uREPL.Evaluator.Evaluate(command2a);
}
private string decomma(string strIn)
{
string strOut = "";
bool wasCommaLast = false;
foreach (char c in strIn)
{
if (wasCommaLast)
{
wasCommaLast = false;
if (c == ' ')
{
strOut = strOut.Substring(0, strOut.Length - 1);
}
}
if (c == ',')
{
wasCommaLast = true;
}
strOut += c;
}
return strOut;
}
public override void Dispose()
{
CustomCommandUtility.Unregister("Chain");
CustomCommandUtility.Unregister("ChainNoFail");
CustomCommandUtility.Unregister("ChainQuiet");
}
}
}