Fix async command responses (#393)

Fix logs not being written for invalid IL
This commit is contained in:
Equinox
2020-07-09 18:06:35 -07:00
committed by GitHub
parent b53194184c
commit e9e446f8ab
5 changed files with 31 additions and 11 deletions

View File

@@ -18,7 +18,7 @@ namespace Torch.API.Managers
/// <summary> /// <summary>
/// Represents a scripted or user chat message. /// Represents a scripted or user chat message.
/// </summary> /// </summary>
public struct TorchChatMessage public readonly struct TorchChatMessage
{ {
private const string DEFAULT_FONT = MyFontEnum.Blue; private const string DEFAULT_FONT = MyFontEnum.Blue;

View File

@@ -177,15 +177,11 @@ namespace Torch.Server
InsertMessage(new TorchChatMessage(TorchBase.Instance.Config.ChatName, text, TorchBase.Instance.Config.ChatColor)); InsertMessage(new TorchChatMessage(TorchBase.Instance.Config.ChatName, text, TorchBase.Instance.Config.ChatColor));
_server.Invoke(() => _server.Invoke(() =>
{ {
var responses = commands.HandleCommandFromServer(text); if (!commands.HandleCommandFromServer(text, InsertMessage))
if (responses == null)
{ {
InsertMessage(new TorchChatMessage(TorchBase.Instance.Config.ChatName, "Invalid command.", TorchBase.Instance.Config.ChatColor)); InsertMessage(new TorchChatMessage(TorchBase.Instance.Config.ChatName, "Invalid command.", TorchBase.Instance.Config.ChatColor));
return; return;
} }
foreach (var response in responses)
InsertMessage(response);
}); });
} }
else else

View File

@@ -82,7 +82,7 @@ namespace Torch.Commands
} }
} }
public List<TorchChatMessage> HandleCommandFromServer(string message) private List<TorchChatMessage> HandleCommandFromServerInternal(string message, Action<TorchChatMessage> subscriber = null)
{ {
var cmdText = new string(message.Skip(1).ToArray()); var cmdText = new string(message.Skip(1).ToArray());
var command = Commands.GetCommand(cmdText, out string argText); var command = Commands.GetCommand(cmdText, out string argText);
@@ -93,6 +93,8 @@ namespace Torch.Commands
var splitArgs = Regex.Matches(argText, "(\"[^\"]+\"|\\S+)").Cast<Match>().Select(x => x.ToString().Replace("\"", "")).ToList(); var splitArgs = Regex.Matches(argText, "(\"[^\"]+\"|\\S+)").Cast<Match>().Select(x => x.ToString().Replace("\"", "")).ToList();
_log.Trace($"Invoking {cmdPath} for server."); _log.Trace($"Invoking {cmdPath} for server.");
var context = new ConsoleCommandContext(Torch, command.Plugin, Sync.MyId, argText, splitArgs); var context = new ConsoleCommandContext(Torch, command.Plugin, Sync.MyId, argText, splitArgs);
if (subscriber != null)
context.OnResponse += subscriber;
if (command.TryInvoke(context)) if (command.TryInvoke(context))
_log.Info($"Server ran command '{message}'"); _log.Info($"Server ran command '{message}'");
else else
@@ -100,6 +102,20 @@ namespace Torch.Commands
return context.Responses; return context.Responses;
} }
/// <summary>
/// Invokes the given command string as the server, subscribing to responses using the given callback.
/// </summary>
/// <returns>true if the command was run, false if not</returns>
public bool HandleCommandFromServer(string message, Action<TorchChatMessage> subscriber)
{
return HandleCommandFromServerInternal(message, subscriber) != null;
}
public List<TorchChatMessage> HandleCommandFromServer(string message)
{
return HandleCommandFromServerInternal(message, null);
}
public void HandleCommand(TorchChatMessage msg, ref bool consumed) public void HandleCommand(TorchChatMessage msg, ref bool consumed)
{ {
if (msg.AuthorSteamId.HasValue) if (msg.AuthorSteamId.HasValue)

View File

@@ -1,3 +1,4 @@
using System;
using System.Collections.Generic; using System.Collections.Generic;
using System.Text; using System.Text;
using Torch.API; using Torch.API;
@@ -9,6 +10,8 @@ namespace Torch.Commands
public class ConsoleCommandContext : CommandContext public class ConsoleCommandContext : CommandContext
{ {
public List<TorchChatMessage> Responses = new List<TorchChatMessage>(); public List<TorchChatMessage> Responses = new List<TorchChatMessage>();
public event Action<TorchChatMessage> OnResponse;
private bool _flag; private bool _flag;
/// <inheritdoc /> /// <inheritdoc />
@@ -24,7 +27,9 @@ namespace Torch.Commands
font = null; font = null;
} }
Responses.Add(new TorchChatMessage(sender ?? TorchBase.Instance.Config.ChatName, message, font ?? TorchBase.Instance.Config.ChatColor)); var msg = new TorchChatMessage(sender ?? TorchBase.Instance.Config.ChatName, message, font ?? TorchBase.Instance.Config.ChatColor);
Responses.Add(msg);
OnResponse?.Invoke(msg);
} }
} }
} }

View File

@@ -173,6 +173,8 @@ namespace Torch.Managers.PatchManager
LogTarget(PrintModeEnum.Emitted, false, "========== Desired method =========="); LogTarget(PrintModeEnum.Emitted, false, "========== Desired method ==========");
MethodTranspiler.IntegrityAnalysis((a, b) => LogTarget(PrintModeEnum.Emitted, a, b), il); MethodTranspiler.IntegrityAnalysis((a, b) => LogTarget(PrintModeEnum.Emitted, a, b), il);
LogTarget(PrintModeEnum.Emitted, false, gap); LogTarget(PrintModeEnum.Emitted, false, gap);
// If the method is invalid the program is likely to hard crash in EmitMethod or Compile, so flush the log
LogManager.Flush();
} }
} }
@@ -182,15 +184,16 @@ namespace Torch.Managers.PatchManager
{ {
PatchUtilities.Compile(method); PatchUtilities.Compile(method);
} }
catch catch (Exception failure)
{ {
lock (_log) lock (_log)
{ {
_log.Error(failure, $"Failed to patch method {_method}");
var ctx = new MethodContext(method); var ctx = new MethodContext(method);
ctx.Read(); ctx.Read();
MethodTranspiler.IntegrityAnalysis((err, msg) => _log.Warn(msg), ctx.Instructions); MethodTranspiler.IntegrityAnalysis((err, msg) => _log.Warn(msg), ctx.Instructions);
LogManager.Flush();
} }
throw; throw;
} }