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

View file

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

View file

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