Skip to content

Commit e81b3a4

Browse files
authored
Merge 2f7c0b5 into 830bf36
2 parents 830bf36 + 2f7c0b5 commit e81b3a4

File tree

1 file changed

+59
-42
lines changed

1 file changed

+59
-42
lines changed

Confuser.Core/Helpers/InjectHelper.cs

Lines changed: 59 additions & 42 deletions
Original file line numberDiff line numberDiff line change
@@ -99,64 +99,81 @@ 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];
152+
newMethodDef.Body.Instructions.Add(newInstr);
153+
bodyMap[instr] = newInstr;
154+
}
143155

144-
else if (instr.Operand is Instruction[])
145-
instr.Operand = ((Instruction[])instr.Operand).Select(target => (Instruction)bodyMap[target]).ToArray();
146-
}
156+
foreach (Instruction instr in newMethodDef.Body.Instructions)
157+
{
158+
if (instr.Operand != null && bodyMap.ContainsKey(instr.Operand))
159+
instr.Operand = bodyMap[instr.Operand];
147160

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);
161+
else if (instr.Operand is Instruction[] instructions)
162+
instr.Operand = instructions.Select(target => (Instruction) bodyMap[target]).ToArray();
159163
}
164+
165+
foreach (ExceptionHandler eh in methodDef.Body.ExceptionHandlers)
166+
newMethodDef.Body.ExceptionHandlers.Add(new ExceptionHandler(eh.HandlerType)
167+
{
168+
CatchType = eh.CatchType == null ? null : ctx.Importer.Import(eh.CatchType),
169+
TryStart = (Instruction) bodyMap[eh.TryStart],
170+
TryEnd = (Instruction) bodyMap[eh.TryEnd],
171+
HandlerStart = (Instruction) bodyMap[eh.HandlerStart],
172+
HandlerEnd = (Instruction) bodyMap[eh.HandlerEnd],
173+
FilterStart = eh.FilterStart == null ? null : (Instruction) bodyMap[eh.FilterStart]
174+
});
175+
176+
newMethodDef.Body.SimplifyMacros(newMethodDef.Parameters);
160177
}
161178

162179
/// <summary>

0 commit comments

Comments
 (0)