Skip to content

Commit 7bccaeb

Browse files
authored
Merge b3ede30 into 830bf36
2 parents 830bf36 + b3ede30 commit 7bccaeb

File tree

1 file changed

+59
-43
lines changed

1 file changed

+59
-43
lines changed

Confuser.Core/Helpers/InjectHelper.cs

Lines changed: 59 additions & 43 deletions
Original file line numberDiff line numberDiff line change
@@ -99,64 +99,80 @@ static void CopyMethodDef(MethodDef methodDef, InjectContext ctx) {
9999

100100
newMethodDef.Signature = ctx.Importer.Import(methodDef.Signature);
101101
newMethodDef.Parameters.UpdateParameterTypes();
102+
103+
foreach (var paramDef in methodDef.ParamDefs)
104+
newMethodDef.ParamDefs.Add(new ParamDefUser(paramDef.Name, paramDef.Sequence, paramDef.Attributes));
102105

103106
if (methodDef.ImplMap != null)
104107
newMethodDef.ImplMap = new ImplMapUser(new ModuleRefUser(ctx.TargetModule, methodDef.ImplMap.Module.Name), methodDef.ImplMap.Name, methodDef.ImplMap.Attributes);
105108

106109
foreach (CustomAttribute ca in methodDef.CustomAttributes)
107110
newMethodDef.CustomAttributes.Add(new CustomAttribute((ICustomAttributeType)ctx.Importer.Import(ca.Constructor)));
108111

109-
if (methodDef.HasBody) {
110-
newMethodDef.Body = new CilBody(methodDef.Body.InitLocals, new List<Instruction>(), new List<ExceptionHandler>(), new List<Local>());
111-
newMethodDef.Body.MaxStack = methodDef.Body.MaxStack;
112-
113-
var bodyMap = new Dictionary<object, object>();
114-
115-
foreach (Local local in methodDef.Body.Variables) {
116-
var newLocal = new Local(ctx.Importer.Import(local.Type));
117-
newMethodDef.Body.Variables.Add(newLocal);
118-
newLocal.Name = local.Name;
119-
120-
bodyMap[local] = newLocal;
121-
}
122-
123-
foreach (Instruction instr in methodDef.Body.Instructions) {
124-
var newInstr = new Instruction(instr.OpCode, instr.Operand);
125-
newInstr.SequencePoint = instr.SequencePoint;
112+
if (methodDef.HasBody)
113+
CopyMethodBody(methodDef, ctx, newMethodDef);
114+
}
115+
116+
static void CopyMethodBody(MethodDef methodDef, InjectContext ctx, MethodDef newMethodDef)
117+
{
118+
newMethodDef.Body = new CilBody(methodDef.Body.InitLocals, new List<Instruction>(),
119+
new List<ExceptionHandler>(), new List<Local>()) {MaxStack = methodDef.Body.MaxStack};
126120

127-
if (newInstr.Operand is IType)
128-
newInstr.Operand = ctx.Importer.Import((IType)newInstr.Operand);
121+
var bodyMap = new Dictionary<object, object>();
129122

130-
else if (newInstr.Operand is IMethod)
131-
newInstr.Operand = ctx.Importer.Import((IMethod)newInstr.Operand);
123+
foreach (Local local in methodDef.Body.Variables)
124+
{
125+
var newLocal = new Local(ctx.Importer.Import(local.Type));
126+
newMethodDef.Body.Variables.Add(newLocal);
127+
newLocal.Name = local.Name;
132128

133-
else if (newInstr.Operand is IField)
134-
newInstr.Operand = ctx.Importer.Import((IField)newInstr.Operand);
129+
bodyMap[local] = newLocal;
130+
}
135131

136-
newMethodDef.Body.Instructions.Add(newInstr);
137-
bodyMap[instr] = newInstr;
132+
foreach (Instruction instr in methodDef.Body.Instructions)
133+
{
134+
var newInstr = new Instruction(instr.OpCode, instr.Operand)
135+
{
136+
SequencePoint = instr.SequencePoint
137+
};
138+
139+
switch (newInstr.Operand)
140+
{
141+
case IType type:
142+
newInstr.Operand = ctx.Importer.Import(type);
143+
break;
144+
case IMethod method:
145+
newInstr.Operand = ctx.Importer.Import(method);
146+
break;
147+
case IField field:
148+
newInstr.Operand = ctx.Importer.Import(field);
149+
break;
138150
}
139151

140-
foreach (Instruction instr in newMethodDef.Body.Instructions) {
141-
if (instr.Operand != null && bodyMap.ContainsKey(instr.Operand))
142-
instr.Operand = bodyMap[instr.Operand];
143-
144-
else if (instr.Operand is Instruction[])
145-
instr.Operand = ((Instruction[])instr.Operand).Select(target => (Instruction)bodyMap[target]).ToArray();
146-
}
152+
newMethodDef.Body.Instructions.Add(newInstr);
153+
bodyMap[instr] = newInstr;
154+
}
147155

148-
foreach (ExceptionHandler eh in methodDef.Body.ExceptionHandlers)
149-
newMethodDef.Body.ExceptionHandlers.Add(new ExceptionHandler(eh.HandlerType) {
150-
CatchType = eh.CatchType == null ? null : (ITypeDefOrRef)ctx.Importer.Import(eh.CatchType),
151-
TryStart = (Instruction)bodyMap[eh.TryStart],
152-
TryEnd = (Instruction)bodyMap[eh.TryEnd],
153-
HandlerStart = (Instruction)bodyMap[eh.HandlerStart],
154-
HandlerEnd = (Instruction)bodyMap[eh.HandlerEnd],
155-
FilterStart = eh.FilterStart == null ? null : (Instruction)bodyMap[eh.FilterStart]
156-
});
157-
158-
newMethodDef.Body.SimplifyMacros(newMethodDef.Parameters);
156+
foreach (Instruction instr in newMethodDef.Body.Instructions)
157+
{
158+
if (instr.Operand != null && bodyMap.ContainsKey(instr.Operand))
159+
instr.Operand = bodyMap[instr.Operand];
160+
else if (instr.Operand is Instruction[] instructions)
161+
instr.Operand = instructions.Select(target => (Instruction) bodyMap[target]).ToArray();
159162
}
163+
164+
foreach (ExceptionHandler eh in methodDef.Body.ExceptionHandlers)
165+
newMethodDef.Body.ExceptionHandlers.Add(new ExceptionHandler(eh.HandlerType)
166+
{
167+
CatchType = eh.CatchType == null ? null : ctx.Importer.Import(eh.CatchType),
168+
TryStart = (Instruction) bodyMap[eh.TryStart],
169+
TryEnd = (Instruction) bodyMap[eh.TryEnd],
170+
HandlerStart = (Instruction) bodyMap[eh.HandlerStart],
171+
HandlerEnd = (Instruction) bodyMap[eh.HandlerEnd],
172+
FilterStart = eh.FilterStart == null ? null : (Instruction) bodyMap[eh.FilterStart]
173+
});
174+
175+
newMethodDef.Body.SimplifyMacros(newMethodDef.Parameters);
160176
}
161177

162178
/// <summary>

0 commit comments

Comments
 (0)