Fix async command responses (#393)
Fix logs not being written for invalid IL
This commit is contained in:
@@ -18,7 +18,7 @@ namespace Torch.API.Managers
|
||||
/// <summary>
|
||||
/// Represents a scripted or user chat message.
|
||||
/// </summary>
|
||||
public struct TorchChatMessage
|
||||
public readonly struct TorchChatMessage
|
||||
{
|
||||
private const string DEFAULT_FONT = MyFontEnum.Blue;
|
||||
|
||||
|
@@ -177,15 +177,11 @@ namespace Torch.Server
|
||||
InsertMessage(new TorchChatMessage(TorchBase.Instance.Config.ChatName, text, TorchBase.Instance.Config.ChatColor));
|
||||
_server.Invoke(() =>
|
||||
{
|
||||
var responses = commands.HandleCommandFromServer(text);
|
||||
if (responses == null)
|
||||
if (!commands.HandleCommandFromServer(text, InsertMessage))
|
||||
{
|
||||
InsertMessage(new TorchChatMessage(TorchBase.Instance.Config.ChatName, "Invalid command.", TorchBase.Instance.Config.ChatColor));
|
||||
return;
|
||||
}
|
||||
|
||||
foreach (var response in responses)
|
||||
InsertMessage(response);
|
||||
});
|
||||
}
|
||||
else
|
||||
|
@@ -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 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();
|
||||
_log.Trace($"Invoking {cmdPath} for server.");
|
||||
var context = new ConsoleCommandContext(Torch, command.Plugin, Sync.MyId, argText, splitArgs);
|
||||
if (subscriber != null)
|
||||
context.OnResponse += subscriber;
|
||||
if (command.TryInvoke(context))
|
||||
_log.Info($"Server ran command '{message}'");
|
||||
else
|
||||
@@ -100,6 +102,20 @@ namespace Torch.Commands
|
||||
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)
|
||||
{
|
||||
if (msg.AuthorSteamId.HasValue)
|
||||
|
@@ -1,3 +1,4 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Text;
|
||||
using Torch.API;
|
||||
@@ -9,6 +10,8 @@ namespace Torch.Commands
|
||||
public class ConsoleCommandContext : CommandContext
|
||||
{
|
||||
public List<TorchChatMessage> Responses = new List<TorchChatMessage>();
|
||||
public event Action<TorchChatMessage> OnResponse;
|
||||
|
||||
private bool _flag;
|
||||
|
||||
/// <inheritdoc />
|
||||
@@ -23,8 +26,10 @@ namespace Torch.Commands
|
||||
sender = 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);
|
||||
}
|
||||
}
|
||||
}
|
@@ -173,6 +173,8 @@ namespace Torch.Managers.PatchManager
|
||||
LogTarget(PrintModeEnum.Emitted, false, "========== Desired method ==========");
|
||||
MethodTranspiler.IntegrityAnalysis((a, b) => LogTarget(PrintModeEnum.Emitted, a, b), il);
|
||||
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);
|
||||
}
|
||||
catch
|
||||
catch (Exception failure)
|
||||
{
|
||||
lock (_log)
|
||||
{
|
||||
_log.Error(failure, $"Failed to patch method {_method}");
|
||||
var ctx = new MethodContext(method);
|
||||
ctx.Read();
|
||||
MethodTranspiler.IntegrityAnalysis((err, msg) => _log.Warn(msg), ctx.Instructions);
|
||||
LogManager.Flush();
|
||||
}
|
||||
|
||||
throw;
|
||||
}
|
||||
|
||||
|
Reference in New Issue
Block a user