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,38 +41,48 @@ namespace TBConsole
if (_logHandler == null)
Debug.unityLogger.logHandler = _logHandler = new UnityLogHandler(Debug.unityLogger.logHandler);
_logHandler.StartCollectingLogMessages();
bool inString = false;
var cmdparts = new List<string>();
command = command.Trim();
int lastIndex = 0;
for (int i = 0; i <= command.Length; i++)
try
{
if (i < command.Length && command[i] == '"') inString = !inString;
else if (!inString && (i == command.Length || command[i] == ' '))
bool inString = false;
var cmdparts = new List<string>();
command = command.Trim();
int lastIndex = 0;
for (int i = 0; i <= command.Length; i++)
{
cmdparts.Add(command.Substring(lastIndex, i - lastIndex).Trim('"'));
lastIndex = i + 1;
if (i < command.Length && command[i] == '"') inString = !inString;
else if (!inString && (i == command.Length || command[i] == ' '))
{
cmdparts.Add(command.Substring(lastIndex, i - lastIndex).Trim('"'));
lastIndex = i + 1;
}
}
switch (cmdparts.Count)
{
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";
}
}
switch (cmdparts.Count)
catch (Exception e) when (e is CommandException || e is TargetParameterCountException)
{
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";
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)
{