From 8d9e568cac7c8c1fd1fceb4405d43dfcfdc3c804 Mon Sep 17 00:00:00 2001
From: NorbiPeti <szatmari.norbert.peter@gmail.com>
Date: Sat, 24 Apr 2021 01:58:13 +0200
Subject: [PATCH] Call builtin commands then read log for 1 second

---
 TBConsole/TBConsole.csproj   |  6 ++++++
 TBConsole/TBConsoleMod.cs    | 31 +++++++++++++++++++++++++++++--
 TBConsole/UnityLogHandler.cs | 35 +++++++++++++++++++++++++++++++++++
 TBConsole/WebServer.cs       |  7 ++++---
 4 files changed, 74 insertions(+), 5 deletions(-)
 create mode 100644 TBConsole/UnityLogHandler.cs

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 @@
 <Project Sdk="Microsoft.NET.Sdk">
 
     <PropertyGroup>
+        <!--
         <TargetFramework>netcoreapp3.1</TargetFramework>
         <OutputType>Exe</OutputType>
+        -->
+        <TargetFramework>netstandard2.0</TargetFramework>
     </PropertyGroup>
 
     <ItemGroup>
@@ -16,6 +19,9 @@
       <Reference Include="IllusionPlugin">
         <HintPath>..\..\GamecraftModdingAPI\GamecraftModdingAPI\bin\Debug\net472\IllusionPlugin.dll</HintPath>
       </Reference>
+      <Reference Include="UnityEngine.CoreModule, Version=0.0.0.0, Culture=neutral, PublicKeyToken=null">
+        <HintPath>..\..\ref\TechbloxPreview_Data\Managed\UnityEngine.CoreModule.dll</HintPath>
+      </Reference>
     </ItemGroup>
 
 </Project>
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<string> 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<string> 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<string, string> _receiver;
-        public WebServer(Func<string, string> receiver) => _receiver = receiver;
+        private Func<string, Task<string>> _receiver;
+        public WebServer(Func<string, Task<string>> 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();