Add some error handling and update to newer C#

This commit is contained in:
Norbi Peti 2022-04-13 00:27:32 +02:00
parent 5b56d7fd43
commit b0d59a1e47
3 changed files with 49 additions and 43 deletions

View file

@ -5,8 +5,9 @@
<TargetFramework>netcoreapp3.1</TargetFramework>
<OutputType>Exe</OutputType>
-->
<TargetFramework>netstandard2.0</TargetFramework>
<TargetFramework>netstandard2.1</TargetFramework>
<Authors>NorbiPeti</Authors>
<LangVersion>8</LangVersion>
</PropertyGroup>
<ItemGroup>
@ -18,10 +19,10 @@
<HintPath>..\..\ref\Plugins\TechbloxModdingAPI.dll</HintPath>
</Reference>
<Reference Include="IllusionPlugin">
<HintPath>..\..\ref\TechbloxPreview_Data\Managed\IllusionPlugin.dll</HintPath>
<HintPath>..\..\ref\Techblox_Data\Managed\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>
<HintPath>..\..\ref\Techblox_Data\Managed\UnityEngine.CoreModule.dll</HintPath>
</Reference>
</ItemGroup>

View file

@ -41,6 +41,8 @@ namespace TBConsole
if (_logHandler == null)
Debug.unityLogger.logHandler = _logHandler = new UnityLogHandler(Debug.unityLogger.logHandler);
_logHandler.StartCollectingLogMessages();
try
{
bool inString = false;
var cmdparts = new List<string>();
command = command.Trim();
@ -72,7 +74,15 @@ namespace TBConsole
default:
return "Too many arguments! Maximum for default commands is 3";
}
}
catch (Exception e) when (e is CommandException || e is TargetParameterCountException)
{
Logging.CommandLogWarning(e.Message);
}
catch (Exception e)
{
Logging.CommandLogWarning(e);
}
string result = _logHandler.FinishCollectingLogMessages();
return $"Got it: {command}\n{result}";
}

View file

@ -40,25 +40,20 @@ namespace TBConsole
{
var context = await _listener.GetContextAsync();
string request = await new StreamReader(context.Request.InputStream).ReadToEndAsync();
string resp;
switch (context.Request.Url.AbsolutePath)
string resp = context.Request.Url.AbsolutePath switch
{
case "/command":
resp = _commandReceiver(request);
break;
case "/commands":
resp = _commandsListSender();
break;
default:
resp = "<img src=\"https://http.cat/404\">";
break;
}
"/command" => _commandReceiver(request),
"/commands" => _commandsListSender(),
_ => "<img src=\"https://http.cat/404\">"
};
string origin = context.Request.Headers["Origin"];
if (origin == "http://localhost:4200" || origin == "https://tb-console.web.app")
context.Response.AddHeader("Access-Control-Allow-Origin", origin);
var sw = new StreamWriter(context.Response.OutputStream);
await using var sw = new StreamWriter(context.Response.OutputStream);
await sw.WriteLineAsync(resp);
sw.Close();
}
catch (ObjectDisposedException)
{
}
catch (Exception e)
{