Skip to content

Commit 373c476

Browse files
committed
Better guessing on the generic operand type
1 parent b1145c8 commit 373c476

4 files changed

Lines changed: 26 additions & 4 deletions

File tree

‎NLog.config‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,6 @@
1212
<rules>
1313
<logger name="*" minlevel="Info" writeTo="main, console" />
1414
<logger name="Chat" minlevel="Info" writeTo="chat" />
15-
<!--<logger name="Torch.Managers.PatchManager.*" minlevel="Trace" writeTo="patch"/>-->
15+
<logger name="Torch.Managers.PatchManager.*" minlevel="Trace" writeTo="patch"/>
1616
</rules>
1717
</nlog>

‎Torch/Managers/PatchManager/MSIL/MsilInstruction.cs‎

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
using System;
2+
using System.Collections.Concurrent;
23
using System.Collections.Generic;
4+
using System.Diagnostics;
35
using System.Reflection;
46
using System.Reflection.Emit;
57
using System.Text;
@@ -103,6 +105,9 @@ public MsilInstruction(OpCode opcode)
103105
/// </summary>
104106
public HashSet<MsilLabel> Labels { get; } = new HashSet<MsilLabel>();
105107

108+
109+
private static readonly ConcurrentDictionary<Type, PropertyInfo> _setterInfoForInlines = new ConcurrentDictionary<Type, PropertyInfo>();
110+
106111
/// <summary>
107112
/// Sets the inline value for this instruction.
108113
/// </summary>
@@ -111,6 +116,23 @@ public MsilInstruction(OpCode opcode)
111116
/// <returns>This instruction</returns>
112117
public MsilInstruction InlineValue<T>(T o)
113118
{
119+
Type type = typeof(T);
120+
while (type != null)
121+
{
122+
if (!_setterInfoForInlines.TryGetValue(type, out PropertyInfo target))
123+
{
124+
Type genType = typeof(MsilOperandInline<>).MakeGenericType(type);
125+
target = genType.GetProperty(nameof(MsilOperandInline<int>.Value));
126+
_setterInfoForInlines[type] = target;
127+
}
128+
Debug.Assert(target?.DeclaringType != null);
129+
if (target.DeclaringType.IsInstanceOfType(Operand))
130+
{
131+
target.SetValue(Operand, o);
132+
return this;
133+
}
134+
type = type.BaseType;
135+
}
114136
((MsilOperandInline<T>)Operand).Value = o;
115137
return this;
116138
}

‎Torch/Managers/PatchManager/Transpile/MethodContext.cs‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -143,7 +143,7 @@ public void CheckIntegrity()
143143

144144
public string ToHumanMsil()
145145
{
146-
return string.Join("\n", _instructions.Select(x => $"IL_{x.Offset:X4}: {x.StackChange()} {x}"));
146+
return string.Join("\n", _instructions.Select(x => $"IL_{x.Offset:X4}: {x.StackChange():+0;-#} {x}"));
147147
}
148148

149149
private static readonly Dictionary<short, OpCode> OpCodeLookup;

‎Torch/Managers/PatchManager/Transpile/MethodTranspiler.cs‎

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -16,8 +16,8 @@ internal static void Transpile(MethodBase baseMethod, IEnumerable<MethodInfo> tr
1616
var context = new MethodContext(baseMethod);
1717
context.Read();
1818
context.CheckIntegrity();
19-
_log.Trace("Input Method:");
20-
_log.Trace(context.ToHumanMsil);
19+
// _log.Trace("Input Method:");
20+
// _log.Trace(context.ToHumanMsil);
2121

2222
var methodContent = (IEnumerable<MsilInstruction>)context.Instructions;
2323
foreach (var transpiler in transpilers)

0 commit comments

Comments
 (0)