Browse Source

Call builtin commands then read log for 1 second

tags/v1.0.0
NorbiPeti 3 years ago
parent
commit
8d9e568cac
Signed by: NorbiPeti <szatmari.norbert.peter@gmail.com> GPG Key ID: DBA4C4549A927E56
4 changed files with 74 additions and 5 deletions
  1. +6
    -0
      TBConsole/TBConsole.csproj
  2. +29
    -2
      TBConsole/TBConsoleMod.cs
  3. +35
    -0
      TBConsole/UnityLogHandler.cs
  4. +4
    -3
      TBConsole/WebServer.cs

+ 6
- 0
TBConsole/TBConsole.csproj View File

@@ -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>

+ 29
- 2
TBConsole/TBConsoleMod.cs View File

@@ -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()


+ 35
- 0
TBConsole/UnityLogHandler.cs View File

@@ -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;
}
}
}

+ 4
- 3
TBConsole/WebServer.cs View File

@@ -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();


Loading…
Cancel
Save