Compare commits
4 Commits
v1.0.32-ma
...
v1.0.35-ma
Author | SHA1 | Date | |
---|---|---|---|
![]() |
67f25ab20b | ||
![]() |
ad19a7dc9e | ||
![]() |
dd854a159a | ||
![]() |
879a373e6a |
@@ -62,9 +62,6 @@ namespace Torch.Server
|
||||
if (File.Exists(oldTorchCfg))
|
||||
File.Move(oldTorchCfg, torchCfg, true);
|
||||
|
||||
Target.Register<LogViewerTarget>(nameof(LogViewerTarget));
|
||||
TorchLogManager.SetConfiguration(new XmlLoggingConfiguration(newNlog));
|
||||
|
||||
var config = Persistent<TorchConfig>.Load(torchCfg);
|
||||
config.Data.InstanceName = instanceName;
|
||||
config.Data.InstancePath = instancePath;
|
||||
@@ -77,14 +74,17 @@ namespace Torch.Server
|
||||
var handler = new UnhandledExceptionHandler(config.Data, isService);
|
||||
AppDomain.CurrentDomain.UnhandledException += handler.OnUnhandledException;
|
||||
|
||||
Target.Register<LogViewerTarget>(nameof(LogViewerTarget));
|
||||
TorchLogManager.RegisterTargets(Environment.GetEnvironmentVariable("TORCH_LOG_EXTENSIONS_PATH") ??
|
||||
Path.Combine(instancePath, "LoggingExtensions"));
|
||||
|
||||
TorchLogManager.SetConfiguration(new XmlLoggingConfiguration(newNlog));
|
||||
|
||||
var initializer = new Initializer(workingDir, config);
|
||||
if (!initializer.Initialize(args))
|
||||
Environment.Exit(1);
|
||||
|
||||
TorchLauncher.Launch(workingDir, binDir);
|
||||
TorchLogManager.SetConfiguration(TorchLogManager.Configuration,
|
||||
Environment.GetEnvironmentVariable("TORCH_LOG_EXTENSIONS_PATH") ??
|
||||
Path.Combine(instancePath, "LoggingExtensions"));
|
||||
|
||||
CopyNative(binDir);
|
||||
initializer.Run(isService, instanceName, instancePath);
|
||||
|
@@ -197,7 +197,7 @@ namespace Torch.Managers.PatchManager
|
||||
lock (_log)
|
||||
{
|
||||
var instructions = context.Body.Instructions
|
||||
.Select(b => new MsilInstruction(b)).ToList();
|
||||
.Select(b => b.ToMsilInstruction()).ToList();
|
||||
LogTarget(PrintModeEnum.Patched, false, "========== Patched method ==========");
|
||||
MethodTranspiler.IntegrityAnalysis((a, b) => LogTarget(PrintModeEnum.Patched, a, b), instructions, true);
|
||||
LogTarget(PrintModeEnum.Patched, false, gap);
|
||||
|
136
Torch/Managers/PatchManager/MSIL/InstructionExtensions.cs
Normal file
136
Torch/Managers/PatchManager/MSIL/InstructionExtensions.cs
Normal file
@@ -0,0 +1,136 @@
|
||||
using System;
|
||||
using System.Linq;
|
||||
using System.Reflection;
|
||||
using Mono.Cecil;
|
||||
using Mono.Cecil.Cil;
|
||||
using MonoMod.Utils;
|
||||
using Torch.Managers.PatchManager.Transpile;
|
||||
using OperandType = System.Reflection.Emit.OperandType;
|
||||
|
||||
namespace Torch.Managers.PatchManager.MSIL;
|
||||
|
||||
internal static class InstructionExtensions
|
||||
{
|
||||
public static MsilInstruction ToMsilInstruction(this Instruction instruction)
|
||||
{
|
||||
static System.Reflection.Emit.Label CreateLabel(int pos)
|
||||
{
|
||||
var instance = Activator.CreateInstance(typeof(System.Reflection.Emit.Label),
|
||||
BindingFlags.CreateInstance | BindingFlags.Instance | BindingFlags.NonPublic, null,
|
||||
new object[] {pos}, null);
|
||||
if (instance == null)
|
||||
return default;
|
||||
return (System.Reflection.Emit.Label) instance;
|
||||
}
|
||||
|
||||
var systemOpCode = MethodContext.OpCodeLookup[instruction.OpCode.Value];
|
||||
var msil = new MsilInstruction(systemOpCode);
|
||||
if (instruction.Operand is null || instruction.OpCode.OperandType == Mono.Cecil.Cil.OperandType.InlineNone)
|
||||
return msil;
|
||||
|
||||
var opType = systemOpCode.OperandType;
|
||||
|
||||
switch (instruction.Operand)
|
||||
{
|
||||
case Instruction targetInstruction when opType is OperandType.InlineBrTarget or OperandType.ShortInlineBrTarget:
|
||||
msil.Operand = new MsilOperandBrTarget(msil)
|
||||
{
|
||||
Target = new MsilLabel(CreateLabel(targetInstruction.Offset))
|
||||
};
|
||||
break;
|
||||
case FieldReference reference when opType == OperandType.InlineField:
|
||||
msil.Operand = new MsilOperandInline.MsilOperandReflected<FieldInfo>(msil)
|
||||
{
|
||||
Value = reference.ResolveReflection()
|
||||
};
|
||||
break;
|
||||
case int int32 when opType is OperandType.InlineI or OperandType.ShortInlineI:
|
||||
msil.Operand = new MsilOperandInline.MsilOperandInt32(msil)
|
||||
{
|
||||
Value = int32
|
||||
};
|
||||
break;
|
||||
case long int64 when opType is OperandType.InlineI8:
|
||||
msil.Operand = new MsilOperandInline.MsilOperandInt64(msil)
|
||||
{
|
||||
Value = int64
|
||||
};
|
||||
break;
|
||||
case MethodReference methodReference when opType is OperandType.InlineMethod:
|
||||
msil.Operand = new MsilOperandInline.MsilOperandReflected<MethodBase>(msil)
|
||||
{
|
||||
Value = methodReference.ResolveReflection()
|
||||
};
|
||||
break;
|
||||
case double @double when opType is OperandType.InlineR:
|
||||
msil.Operand = new MsilOperandInline.MsilOperandDouble(msil)
|
||||
{
|
||||
Value = @double
|
||||
};
|
||||
break;
|
||||
case null when opType is OperandType.InlineSig:
|
||||
throw new NotSupportedException("InlineSignature is not supported by instruction converter");
|
||||
case string @string when opType == OperandType.InlineString:
|
||||
msil.Operand = new MsilOperandInline.MsilOperandString(msil)
|
||||
{
|
||||
Value = @string
|
||||
};
|
||||
break;
|
||||
case Instruction[] targetInstructions when opType is OperandType.InlineSwitch:
|
||||
msil.Operand = new MsilOperandSwitch(msil)
|
||||
{
|
||||
Labels = targetInstructions.Select(b => new MsilLabel(CreateLabel(b.Offset))).ToArray()
|
||||
};
|
||||
break;
|
||||
case MemberReference memberReference when opType is OperandType.InlineTok:
|
||||
msil.Operand = new MsilOperandInline.MsilOperandReflected<MemberInfo>(msil)
|
||||
{
|
||||
Value = memberReference.ResolveReflection()
|
||||
};
|
||||
break;
|
||||
case TypeReference typeReference when opType is OperandType.InlineType:
|
||||
msil.Operand = new MsilOperandInline.MsilOperandReflected<Type>(msil)
|
||||
{
|
||||
Value = typeReference.ResolveReflection()
|
||||
};
|
||||
break;
|
||||
case VariableDefinition variableDefinition when opType is OperandType.InlineVar or OperandType.ShortInlineVar:
|
||||
if (systemOpCode.IsLocalStore() || systemOpCode.IsLocalLoad() || systemOpCode.IsLocalLoadByRef())
|
||||
msil.Operand = new MsilOperandInline.MsilOperandLocal(msil)
|
||||
{
|
||||
Value = new MsilLocal(variableDefinition.Index)
|
||||
};
|
||||
else
|
||||
msil.Operand = new MsilOperandInline.MsilOperandArgument(msil)
|
||||
{
|
||||
Value = new MsilArgument(variableDefinition.Index)
|
||||
};
|
||||
break;
|
||||
case ParameterDefinition parameterDefinition when opType is OperandType.InlineVar or OperandType.ShortInlineVar:
|
||||
if (systemOpCode.IsLocalStore() || systemOpCode.IsLocalLoad() || systemOpCode.IsLocalLoadByRef())
|
||||
msil.Operand = new MsilOperandInline.MsilOperandLocal(msil)
|
||||
{
|
||||
Value = new MsilLocal(parameterDefinition.Index)
|
||||
};
|
||||
else
|
||||
msil.Operand = new MsilOperandInline.MsilOperandArgument(msil)
|
||||
{
|
||||
Value = new MsilArgument(parameterDefinition.Index)
|
||||
};
|
||||
break;
|
||||
case float @float when opType == OperandType.ShortInlineR:
|
||||
msil.Operand = new MsilOperandInline.MsilOperandSingle(msil)
|
||||
{
|
||||
Value = @float
|
||||
};
|
||||
break;
|
||||
#pragma warning disable 618
|
||||
case null when opType == OperandType.InlinePhi:
|
||||
#pragma warning restore 618
|
||||
default:
|
||||
throw new ArgumentOutOfRangeException(nameof(instruction.Operand), instruction.Operand, "Invalid operand type");
|
||||
}
|
||||
|
||||
return msil;
|
||||
}
|
||||
}
|
@@ -12,6 +12,7 @@ using Mono.Cecil.Cil;
|
||||
using MonoMod.Utils;
|
||||
using Torch.Managers.PatchManager.Transpile;
|
||||
using Torch.Utils;
|
||||
using VRage.Game.VisualScripting;
|
||||
using OpCode = System.Reflection.Emit.OpCode;
|
||||
using OpCodes = System.Reflection.Emit.OpCodes;
|
||||
using OperandType = System.Reflection.Emit.OperandType;
|
||||
@@ -88,132 +89,6 @@ namespace Torch.Managers.PatchManager.MSIL
|
||||
}
|
||||
}
|
||||
|
||||
public MsilInstruction(Instruction instruction)
|
||||
{
|
||||
Label CreateLabel(int pos)
|
||||
{
|
||||
var instance = Activator.CreateInstance(typeof(Label),
|
||||
BindingFlags.CreateInstance | BindingFlags.Instance | BindingFlags.NonPublic, null,
|
||||
new object[] {pos}, null);
|
||||
if (instance == null)
|
||||
return default;
|
||||
return (Label) instance;
|
||||
}
|
||||
|
||||
if (!MethodContext.OpCodeLookup.TryGetValue(instruction.OpCode.Value, out var opCode))
|
||||
return;
|
||||
OpCode = opCode;
|
||||
|
||||
var opType = opCode.OperandType;
|
||||
if (opType == OperandType.InlineNone)
|
||||
{
|
||||
Operand = null;
|
||||
return;
|
||||
}
|
||||
switch (instruction.Operand)
|
||||
{
|
||||
case OperandType.InlineNone:
|
||||
break;
|
||||
case Instruction targetInstruction when opType == OperandType.InlineBrTarget || opType == OperandType.ShortInlineBrTarget:
|
||||
Operand = new MsilOperandBrTarget(this)
|
||||
{
|
||||
Target = new MsilLabel(CreateLabel(targetInstruction.Offset))
|
||||
};
|
||||
break;
|
||||
case FieldReference reference when opType == OperandType.InlineField:
|
||||
Operand = new MsilOperandInline.MsilOperandReflected<FieldInfo>(this)
|
||||
{
|
||||
Value = reference.ResolveReflection()
|
||||
};
|
||||
break;
|
||||
case int int32 when opType == OperandType.InlineI || opType == OperandType.ShortInlineI:
|
||||
Operand = new MsilOperandInline.MsilOperandInt32(this)
|
||||
{
|
||||
Value = int32
|
||||
};
|
||||
break;
|
||||
case long int64 when opType == OperandType.InlineI8:
|
||||
Operand = new MsilOperandInline.MsilOperandInt64(this)
|
||||
{
|
||||
Value = int64
|
||||
};
|
||||
break;
|
||||
case MethodReference methodReference when opType == OperandType.InlineMethod:
|
||||
Operand = new MsilOperandInline.MsilOperandReflected<MethodBase>(this)
|
||||
{
|
||||
Value = methodReference.ResolveReflection()
|
||||
};
|
||||
break;
|
||||
case double @double when opType == OperandType.InlineR:
|
||||
Operand = new MsilOperandInline.MsilOperandDouble(this)
|
||||
{
|
||||
Value = @double
|
||||
};
|
||||
break;
|
||||
case null when opType == OperandType.InlineSig:
|
||||
throw new NotSupportedException("InlineSignature is not supported by instruction converter");
|
||||
case string @string when opType == OperandType.InlineString:
|
||||
Operand = new MsilOperandInline.MsilOperandString(this)
|
||||
{
|
||||
Value = @string
|
||||
};
|
||||
break;
|
||||
case Instruction[] targetInstructions when opType == OperandType.InlineSwitch:
|
||||
Operand = new MsilOperandSwitch(this)
|
||||
{
|
||||
Labels = targetInstructions.Select(b => new MsilLabel(CreateLabel(b.Offset))).ToArray()
|
||||
};
|
||||
break;
|
||||
case MemberReference memberReference when opType == OperandType.InlineTok:
|
||||
Operand = new MsilOperandInline.MsilOperandReflected<MemberInfo>(this)
|
||||
{
|
||||
Value = memberReference.ResolveReflection()
|
||||
};
|
||||
break;
|
||||
case TypeReference typeReference when opType == OperandType.InlineType:
|
||||
Operand = new MsilOperandInline.MsilOperandReflected<Type>(this)
|
||||
{
|
||||
Value = typeReference.ResolveReflection()
|
||||
};
|
||||
break;
|
||||
case VariableDefinition variableDefinition when opType == OperandType.InlineVar || opType == OperandType.ShortInlineVar:
|
||||
if (OpCode.IsLocalStore() || OpCode.IsLocalLoad() || OpCode.IsLocalLoadByRef())
|
||||
Operand = new MsilOperandInline.MsilOperandLocal(this)
|
||||
{
|
||||
Value = new MsilLocal(variableDefinition.Index)
|
||||
};
|
||||
else
|
||||
Operand = new MsilOperandInline.MsilOperandArgument(this)
|
||||
{
|
||||
Value = new MsilArgument(variableDefinition.Index)
|
||||
};
|
||||
break;
|
||||
case ParameterDefinition parameterDefinition when opType == OperandType.InlineVar || opType == OperandType.ShortInlineVar:
|
||||
if (OpCode.IsLocalStore() || OpCode.IsLocalLoad() || OpCode.IsLocalLoadByRef())
|
||||
Operand = new MsilOperandInline.MsilOperandLocal(this)
|
||||
{
|
||||
Value = new MsilLocal(parameterDefinition.Index)
|
||||
};
|
||||
else
|
||||
Operand = new MsilOperandInline.MsilOperandArgument(this)
|
||||
{
|
||||
Value = new MsilArgument(parameterDefinition.Index)
|
||||
};
|
||||
break;
|
||||
case float @float when opType == OperandType.ShortInlineR:
|
||||
Operand = new MsilOperandInline.MsilOperandSingle(this)
|
||||
{
|
||||
Value = @float
|
||||
};
|
||||
break;
|
||||
#pragma warning disable 618
|
||||
case null when opType == OperandType.InlinePhi:
|
||||
#pragma warning restore 618
|
||||
default:
|
||||
throw new ArgumentOutOfRangeException(nameof(instruction.Operand), instruction.Operand, "Invalid operand type");
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Opcode of this instruction
|
||||
/// </summary>
|
||||
@@ -227,7 +102,7 @@ namespace Torch.Managers.PatchManager.MSIL
|
||||
/// <summary>
|
||||
/// The operand for this instruction, or null.
|
||||
/// </summary>
|
||||
public MsilOperand Operand { get; }
|
||||
public MsilOperand Operand { get; internal set; }
|
||||
|
||||
/// <summary>
|
||||
/// Labels pointing to this instruction.
|
||||
@@ -284,7 +159,11 @@ namespace Torch.Managers.PatchManager.MSIL
|
||||
type = type.BaseType;
|
||||
}
|
||||
|
||||
((MsilOperandInline<T>) Operand).Value = o;
|
||||
if (Operand is not MsilOperandInline<T> operandInline)
|
||||
throw new InvalidOperationException(
|
||||
$"Type {typeof(T).FullName} is not valid operand for {Operand?.GetType().Signature()}");
|
||||
|
||||
operandInline.Value = o;
|
||||
return this;
|
||||
}
|
||||
|
||||
|
@@ -1,38 +1,41 @@
|
||||
using System;
|
||||
using System.IO;
|
||||
using System.Linq;
|
||||
using System.Reflection;
|
||||
using System.Runtime.Loader;
|
||||
using NLog;
|
||||
using NLog.Config;
|
||||
using NLog.Targets;
|
||||
|
||||
namespace Torch.Utils;
|
||||
|
||||
public static class TorchLogManager
|
||||
{
|
||||
private static readonly AssemblyLoadContext LoadContext = new("TorchLog");
|
||||
private static AssemblyLoadContext LoadContext = new("TorchLog");
|
||||
|
||||
public static LoggingConfiguration Configuration { get; private set; }
|
||||
|
||||
public static void SetConfiguration(LoggingConfiguration configuration, string extensionsDir = null)
|
||||
public static void SetConfiguration(LoggingConfiguration configuration)
|
||||
{
|
||||
Configuration = configuration;
|
||||
LogManager.Setup()
|
||||
.SetupExtensions(builder =>
|
||||
{
|
||||
if (extensionsDir is null || !Directory.Exists(extensionsDir))
|
||||
return;
|
||||
foreach (var file in Directory.EnumerateFiles(extensionsDir, "*.dll", SearchOption.AllDirectories))
|
||||
{
|
||||
builder.RegisterAssembly(LoadContext.LoadFromAssemblyPath(file));
|
||||
}
|
||||
})
|
||||
.SetupLogFactory(builder => builder.SetThrowConfigExceptions(true))
|
||||
.LoadConfiguration(configuration);
|
||||
LogManager.Configuration = configuration;
|
||||
LogManager.ReconfigExistingLoggers();
|
||||
}
|
||||
|
||||
public static void RegisterTargets(string dir)
|
||||
{
|
||||
if (!Directory.Exists(dir)) return;
|
||||
|
||||
foreach (var type in Directory.EnumerateFiles(dir, "*.dll").Select(LoadContext.LoadFromAssemblyPath)
|
||||
.SelectMany(b => b.ExportedTypes)
|
||||
.Where(b => b.GetCustomAttribute<TargetAttribute>() is { }))
|
||||
{
|
||||
Target.Register(type.GetCustomAttribute<TargetAttribute>()!.Name, type);
|
||||
}
|
||||
}
|
||||
|
||||
public static void RestoreGlobalConfiguration()
|
||||
{
|
||||
LogManager.Configuration = Configuration;
|
||||
LogManager.ReconfigExistingLoggers();
|
||||
SetConfiguration(Configuration);
|
||||
}
|
||||
}
|
Reference in New Issue
Block a user