diff --git a/TBConsole/TBConsole.csproj b/TBConsole/TBConsole.csproj index 25a29f4..c7a8ece 100644 --- a/TBConsole/TBConsole.csproj +++ b/TBConsole/TBConsole.csproj @@ -1,8 +1,11 @@ + + netstandard2.0 @@ -16,6 +19,9 @@ ..\..\GamecraftModdingAPI\GamecraftModdingAPI\bin\Debug\net472\IllusionPlugin.dll + + ..\..\ref\TechbloxPreview_Data\Managed\UnityEngine.CoreModule.dll + diff --git a/TBConsole/TBConsoleMod.cs b/TBConsole/TBConsoleMod.cs index be0c7e4..a7f3e8b 100644 --- a/TBConsole/TBConsoleMod.cs +++ b/TBConsole/TBConsoleMod.cs @@ -2,7 +2,10 @@ using System.IO; using System.Net; using System.Reflection; +using System.Threading.Tasks; +using GamecraftModdingAPI.Commands; using IllusionPlugin; +using UnityEngine; namespace TBConsole { @@ -12,6 +15,7 @@ namespace TBConsole public override string Version { get; } = Assembly.GetExecutingAssembly().GetName().Version.ToString(); private WebServer _server; + private UnityLogHandler _logHandler; public override void OnApplicationStart() { GamecraftModdingAPI.Main.Init(); @@ -19,9 +23,32 @@ namespace TBConsole _server.Start(); } - public string CommandReceived(string command) + private async Task CommandReceived(string command) { - return $"Got it: {command}"; + if (_logHandler == null) + Debug.unityLogger.logHandler = _logHandler = new UnityLogHandler(Debug.unityLogger.logHandler); + var logTask = _logHandler.CollectLogMessages(); + var cmdparts = command.Split(' '); + switch (cmdparts.Length) + { + case 1: + ExistingCommands.Call(cmdparts[0]); + break; + case 2: + ExistingCommands.Call(cmdparts[0], cmdparts[1]); + break; + case 3: + ExistingCommands.Call(cmdparts[0], cmdparts[1], cmdparts[2]); + break; + case 4: + ExistingCommands.Call(cmdparts[0], cmdparts[1], cmdparts[2], cmdparts[3]); + break; + default: + return "Too many arguments! Maximum for default commands is 3"; + } + + string result = await logTask; + return $"Got it: {command}\n{result}"; } public override void OnApplicationQuit() diff --git a/TBConsole/UnityLogHandler.cs b/TBConsole/UnityLogHandler.cs new file mode 100644 index 0000000..21555c3 --- /dev/null +++ b/TBConsole/UnityLogHandler.cs @@ -0,0 +1,35 @@ +using System; +using System.Threading.Tasks; +using UnityEngine; +using Object = UnityEngine.Object; + +namespace TBConsole +{ + public class UnityLogHandler : ILogHandler + { + private readonly ILogHandler _original; + private string _collectedLog = null; + public UnityLogHandler(ILogHandler original) => _original = original; + + public void LogFormat(LogType logType, Object context, string format, params object[] args) + { + if (_collectedLog != null) + _collectedLog += $"{logType} - {string.Format(format, args)} - {context}\n"; + _original.LogFormat(logType, context, format, args); + } + + public void LogException(Exception exception, Object context) + { + _original.LogException(exception, context); + } + + public async Task CollectLogMessages() + { + _collectedLog = ""; + await Task.Delay(1000); + string ret = _collectedLog; + _collectedLog = null; + return ret; + } + } +} \ No newline at end of file diff --git a/TBConsole/WebServer.cs b/TBConsole/WebServer.cs index edba93a..63dff34 100644 --- a/TBConsole/WebServer.cs +++ b/TBConsole/WebServer.cs @@ -1,6 +1,7 @@ using System; using System.IO; using System.Net; +using System.Threading.Tasks; namespace TBConsole { @@ -8,8 +9,8 @@ namespace TBConsole { private bool _running; private readonly HttpListener _listener = new HttpListener(); - private Func _receiver; - public WebServer(Func receiver) => _receiver = receiver; + private Func> _receiver; + public WebServer(Func> receiver) => _receiver = receiver; public void Start() { @@ -30,7 +31,7 @@ namespace TBConsole while (_running) { var context = await _listener.GetContextAsync(); - string resp = _receiver(await new StreamReader(context.Request.InputStream).ReadToEndAsync()); + string resp = await _receiver(await new StreamReader(context.Request.InputStream).ReadToEndAsync()); var sw = new StreamWriter(context.Response.OutputStream); await sw.WriteLineAsync(resp); sw.Close();