Moar Gamecraft commands!
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

99 lines
3.7KB

  1. using System;
  2. using RobocraftX.GUI.CommandLine;
  3. using RobocraftX.Multiplayer;
  4. using RobocraftX.StateSync;
  5. using RobocraftX.Character;
  6. using Svelto.ECS;
  7. using Unity.Entities;
  8. using UnityEngine;
  9. using uREPL;
  10. using Svelto.Context;
  11. using RobocraftX;
  12. namespace ExtraCommands.Basics
  13. {
  14. [CustomCommand("Chain", "Run two commands, one after the other")]
  15. [CustomCommand("ChainNoFail", "Run two commands, one after the other even if the first one is invalid")]
  16. [CustomCommand("ChainQuiet", "Run two commands, one after the other quietly")]
  17. class ChainCommandEngine : CustomCommandEngine
  18. {
  19. public ChainCommandEngine(UnityContext<FullGameCompositionRoot> ctxHolder, EnginesRoot enginesRoot, World physW, Action reloadGame, MultiplayerInitParameters mpParams) : base(ctxHolder, enginesRoot, physW, reloadGame, mpParams)
  20. {
  21. }
  22. public override void Ready()
  23. {
  24. uREPL.RuntimeCommands.Register<string, string>("Chain", ChainCommand, "Run two commands, one after the other");
  25. uREPL.RuntimeCommands.Register<string, string>("ChainNoFail", ChainNoFailCommand, "Run two commands, one after the other even if the first one is invalid");
  26. uREPL.RuntimeCommands.Register<string, string>("ChainQuiet", ChainQuietCommand, "Run two commands, one after the other quietly");
  27. }
  28. private void ChainCommand(string command1, string command2)
  29. {
  30. string command1a = decomma(command1);
  31. string command2a = decomma(command2);
  32. bool success1 = uREPL.Evaluator.Evaluate(command1a).type == CompileResult.Type.Success;
  33. if (!success1) {
  34. uREPL.Log.Error("First command was not executed successfully");
  35. return;
  36. }
  37. bool success2 = uREPL.Evaluator.Evaluate(command2a).type == CompileResult.Type.Success;
  38. if (!success2) {
  39. uREPL.Log.Error("Second command was not executed successfully");
  40. }
  41. }
  42. private void ChainNoFailCommand(string command1, string command2)
  43. {
  44. string command1a = decomma(command1);
  45. string command2a = decomma(command2);
  46. bool success1 = uREPL.Evaluator.Evaluate(command1a).type == CompileResult.Type.Success;
  47. if (!success1) {
  48. uREPL.Log.Error("First command was not executed successfully");
  49. }
  50. bool success2 = uREPL.Evaluator.Evaluate(command2a).type == CompileResult.Type.Success;
  51. if (!success2) {
  52. uREPL.Log.Error("Second command was not executed successfully");
  53. }
  54. }
  55. private void ChainQuietCommand(string command1, string command2)
  56. {
  57. string command1a = decomma(command1);
  58. string command2a = decomma(command2);
  59. uREPL.Evaluator.Evaluate(command1a);
  60. uREPL.Evaluator.Evaluate(command2a);
  61. }
  62. private string decomma(string strIn)
  63. {
  64. string strOut = "";
  65. bool wasCommaLast = false;
  66. foreach (char c in strIn)
  67. {
  68. if (wasCommaLast)
  69. {
  70. wasCommaLast = false;
  71. if (c == ' ')
  72. {
  73. strOut = strOut.Substring(0, strOut.Length - 1);
  74. }
  75. }
  76. if (c == ',')
  77. {
  78. wasCommaLast = true;
  79. }
  80. strOut += c;
  81. }
  82. return strOut;
  83. }
  84. public override void Dispose()
  85. {
  86. uREPL.RuntimeCommands.Unregister("Chain");
  87. uREPL.RuntimeCommands.Unregister("ChainNoFail");
  88. uREPL.RuntimeCommands.Unregister("ChainQuiet");
  89. }
  90. }
  91. }