Web server mod for Techblox to run 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.

110 line
3.8KB

  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Reflection;
  5. using System.Threading.Tasks;
  6. using IllusionPlugin;
  7. using TechbloxModdingAPI.App;
  8. using TechbloxModdingAPI.Commands;
  9. using TechbloxModdingAPI.Utility;
  10. using UnityEngine;
  11. namespace TBConsole
  12. {
  13. public class TBConsoleMod : IEnhancedPlugin
  14. {
  15. public override string Name => "TBConsole";
  16. public override string Version { get; } = Assembly.GetExecutingAssembly().GetName().Version.ToString();
  17. private WebServer _server;
  18. private UnityLogHandler _logHandler;
  19. public override void OnApplicationStart()
  20. {
  21. TechbloxModdingAPI.Main.Init();
  22. Game.Enter += async (sender, e) =>
  23. {
  24. while (_server?.Running ?? false)
  25. {
  26. Logging.LogWarning("A previous web server is still running");
  27. _server?.Stop();
  28. await Task.Delay(500);
  29. }
  30. _server = new WebServer(CommandReceived, GetCommandList);
  31. _server.Start();
  32. };
  33. Game.Exit += (sender, e) => _server?.Stop();
  34. }
  35. private string CommandReceived(string command)
  36. {
  37. if (_logHandler == null)
  38. Debug.unityLogger.logHandler = _logHandler = new UnityLogHandler(Debug.unityLogger.logHandler);
  39. _logHandler.StartCollectingLogMessages();
  40. try
  41. {
  42. bool inString = false;
  43. var cmdparts = new List<string>();
  44. command = command.Trim();
  45. int lastIndex = 0;
  46. for (int i = 0; i <= command.Length; i++)
  47. {
  48. if (i < command.Length && command[i] == '"') inString = !inString;
  49. else if (!inString && (i == command.Length || command[i] == ' '))
  50. {
  51. cmdparts.Add(command.Substring(lastIndex, i - lastIndex).Trim('"'));
  52. lastIndex = i + 1;
  53. }
  54. }
  55. switch (cmdparts.Count)
  56. {
  57. case 1:
  58. ExistingCommands.Call(cmdparts[0]);
  59. break;
  60. case 2:
  61. ExistingCommands.Call(cmdparts[0], cmdparts[1]);
  62. break;
  63. case 3:
  64. ExistingCommands.Call(cmdparts[0], cmdparts[1], cmdparts[2]);
  65. break;
  66. case 4:
  67. ExistingCommands.Call(cmdparts[0], cmdparts[1], cmdparts[2], cmdparts[3]);
  68. break;
  69. default:
  70. return "Too many arguments! Maximum for default commands is 3";
  71. }
  72. }
  73. catch (Exception e) when (e is CommandException || e is TargetParameterCountException)
  74. {
  75. Logging.CommandLogWarning(e.Message);
  76. }
  77. catch (Exception e)
  78. {
  79. Logging.CommandLogWarning(e);
  80. }
  81. string result = _logHandler.FinishCollectingLogMessages();
  82. return $"Got it: {command}\n{result}";
  83. }
  84. public string GetCommandList()
  85. {
  86. return ExistingCommands.GetCommandNamesAndDescriptions()
  87. .Select(command => command.Name + " - " + command.Description).Aggregate((a, b) => a + "\n" + b);
  88. }
  89. public override void OnApplicationQuit()
  90. {
  91. _server?.Stop();
  92. TechbloxModdingAPI.Main.Shutdown();
  93. }
  94. public static void Main(string[] args)
  95. {
  96. var mod = new TBConsoleMod();
  97. mod._server = new WebServer(mod.CommandReceived, mod.GetCommandList);
  98. mod._server.Start();
  99. Console.ReadLine();
  100. }
  101. }
  102. }