Skip to content

Commit 64d5831

Browse files
committed
Small cleanup, fixed InjectWith signature
1 parent 32f2573 commit 64d5831

File tree

8 files changed

+135
-87
lines changed

8 files changed

+135
-87
lines changed

Mono.Cecil.Inject/ILUtils.cs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -19,9 +19,9 @@ public static Instruction CopyInstruction(Instruction ins)
1919
result = Instruction.Create(ins.OpCode);
2020
else
2121
result =
22-
(Instruction)
23-
typeof(Instruction).GetMethod("Create", new[] {typeof(OpCode), ins.Operand.GetType()})
24-
.Invoke(null, new[] {ins.OpCode, ins.Operand});
22+
(Instruction)
23+
typeof(Instruction).GetMethod("Create", new[] {typeof(OpCode), ins.Operand.GetType()})
24+
.Invoke(null, new[] {ins.OpCode, ins.Operand});
2525
return result;
2626
}
2727

Mono.Cecil.Inject/InjectFlags.cs

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -237,14 +237,16 @@ public InjectValues(InjectFlags flags)
237237
/// <returns>The combination of the properties in a single <see cref="InjectFlags" /> value to use in injection.</returns>
238238
public InjectFlags GetCombinedFlags()
239239
{
240-
return (ModifyReturn ? InjectFlags.ModifyReturn : 0) | (TagType == PassTagType.Int32
240+
return (ModifyReturn ? InjectFlags.ModifyReturn : 0) |
241+
(TagType == PassTagType.Int32
241242
? InjectFlags.PassTag
242243
: TagType == PassTagType.String
243244
? InjectFlags.PassStringTag
244-
: 0)
245-
| (PassInvokingInstance ? InjectFlags.PassInvokingInstance : 0)
246-
| (PassFields ? InjectFlags.PassFields : 0) | (PassLocals ? InjectFlags.PassLocals : 0)
247-
| (ParameterType == PassParametersType.ByValue
245+
: 0) |
246+
(PassInvokingInstance ? InjectFlags.PassInvokingInstance : 0) |
247+
(PassFields ? InjectFlags.PassFields : 0) |
248+
(PassLocals ? InjectFlags.PassLocals : 0) |
249+
(ParameterType == PassParametersType.ByValue
248250
? InjectFlags.PassParametersVal
249251
: (ParameterType == PassParametersType.ByReference ? InjectFlags.PassParametersRef : 0));
250252
}

Mono.Cecil.Inject/InjectionDefinition.cs

Lines changed: 60 additions & 44 deletions
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,10 @@ public InjectionDefinition(MethodDefinition injectTarget,
3737
int[] localVarIDs = null,
3838
params FieldDefinition[] memberReferences)
3939
{
40-
ParameterCount = VerifyInjectionDefinition(injectMethod, injectTarget, flags, localVarIDs,
40+
ParameterCount = VerifyInjectionDefinition(injectMethod,
41+
injectTarget,
42+
flags,
43+
localVarIDs,
4144
memberReferences);
4245
InjectMethod = injectMethod;
4346
InjectTarget = injectTarget;
@@ -140,19 +143,19 @@ internal static InjectionDefinition FindInjectionDefinition(TypeDefinition type,
140143
int parameterCount = 0;
141144

142145
MethodDefinition injection =
143-
type.Methods.FirstOrDefault(m =>
144-
{
145-
try
146-
{
147-
parameterCount = VerifyInjectionDefinition(m, target, flags, localVarIDs, memberReferences);
148-
}
149-
catch (InjectionDefinitionException e)
146+
type.Methods.FirstOrDefault(m =>
150147
{
151-
Logger.LogLine(LogMask.GetInjectionMethod, e.Message);
152-
return false;
153-
}
154-
return true;
155-
});
148+
try
149+
{
150+
parameterCount = VerifyInjectionDefinition(m, target, flags, localVarIDs, memberReferences);
151+
}
152+
catch (InjectionDefinitionException e)
153+
{
154+
Logger.LogLine(LogMask.GetInjectionMethod, e.Message);
155+
return false;
156+
}
157+
return true;
158+
});
156159

157160
if (injection == null)
158161
{
@@ -178,8 +181,10 @@ private static void Assert(bool val, string message)
178181
throw new InjectionDefinitionException(message);
179182
}
180183

181-
private static int VerifyInjectionDefinition(MethodDefinition injectMethod, MethodDefinition injectTarget,
182-
InjectFlags flags, int[] localVarIDs = null,
184+
private static int VerifyInjectionDefinition(MethodDefinition injectMethod,
185+
MethodDefinition injectTarget,
186+
InjectFlags flags,
187+
int[] localVarIDs = null,
183188
params FieldDefinition[] memberReferences)
184189
{
185190
Assert(
@@ -201,22 +206,25 @@ private static int VerifyInjectionDefinition(MethodDefinition injectMethod, Meth
201206
!hFlags.PassFields || memberReferences != null,
202207
$"Supposed to pass member fields, but {nameof(memberReferences)} is empty!");
203208

204-
int prefixCount = Convert.ToInt32(hFlags.PassTag) + Convert.ToInt32(hFlags.PassInvokingInstance)
205-
+ Convert.ToInt32(hFlags.ModifyReturn && !isVoid);
209+
int prefixCount = Convert.ToInt32(hFlags.PassTag) +
210+
Convert.ToInt32(hFlags.PassInvokingInstance) +
211+
Convert.ToInt32(hFlags.ModifyReturn && !isVoid);
206212
int localsCount = hFlags.PassLocals ? localVarIDs.Length : 0;
207213
int memberRefCount = hFlags.PassFields ? memberReferences.Length : 0;
208214
int paramCount = hFlags.PassParameters ? injectTarget.Parameters.Count : 0;
209215
int parameters = injectMethod.Parameters.Count - prefixCount - localsCount - memberRefCount;
210216

211217
Assert(
212-
hFlags.PassParameters && 0 < parameters && parameters <= injectTarget.Parameters.Count
213-
|| !hFlags.PassParameters && parameters == 0,
218+
hFlags.PassParameters && 0 < parameters && parameters <= injectTarget.Parameters.Count ||
219+
!hFlags.PassParameters && parameters == 0,
214220
$@"The injection method has a wrong number of parameters! Check that the provided target method, local variables, member references and injection flags add up to the right number of parameters.
215221
Needed parameters: Prefix: {prefixCount}, Locals: {localsCount}, Members: {memberRefCount}, Parameters: {
216-
(hFlags.PassParameters ? "between 1 and " + paramCount : "0")
217-
}, TOTAL: {
218-
(hFlags.PassParameters ? $"between {prefixCount + localsCount + memberRefCount + 1} and " : "")
219-
}{prefixCount + localsCount + memberRefCount + paramCount}.
222+
(hFlags.PassParameters ? "between 1 and " + paramCount : "0")
223+
}, TOTAL: {
224+
(hFlags.PassParameters
225+
? $"between {prefixCount + localsCount + memberRefCount + 1} and "
226+
: "")
227+
}{prefixCount + localsCount + memberRefCount + paramCount}.
220228
Injection has {injectMethod.Parameters.Count} parameters.");
221229

222230
TypeComparer comparer = TypeComparer.Instance;
@@ -247,11 +255,12 @@ private static int VerifyInjectionDefinition(MethodDefinition injectMethod, Meth
247255
injectMethod.ReturnType.FullName == "System.Boolean",
248256
"The injection method must return a boolean in order to alter the return value.");
249257
Assert(
250-
isVoid
251-
|| comparer.Equals(
258+
isVoid ||
259+
comparer.Equals(
252260
injectMethod
253-
.Parameters[Convert.ToInt32(hFlags.PassTag) + Convert.ToInt32(hFlags.PassInvokingInstance)]
254-
.ParameterType,
261+
.Parameters[Convert.ToInt32(hFlags.PassTag) +
262+
Convert.ToInt32(hFlags.PassInvokingInstance)]
263+
.ParameterType,
255264
new ByReferenceType(injectTarget.ReturnType)),
256265
"Supposed to modify the return value, but the provided return type does not match with the return type of the target method! Also make sure the type is passed by reference (out/ref).");
257266
}
@@ -267,11 +276,15 @@ private static int VerifyInjectionDefinition(MethodDefinition injectMethod, Meth
267276
"Supposed to receive local references, but the provided local variable index/indices do not exist in the target method!");
268277
Assert(
269278
injectMethod.Parameters.Slice(prefixCount, localsCount)
270-
.Select((p, i) => new {param = p, index = localVarIDs[i]})
279+
.Select((p, i) => new
280+
{
281+
param = p,
282+
index = localVarIDs[i]
283+
})
271284
.All(
272285
t =>
273-
t.param.ParameterType.IsByReference
274-
&& comparer.Equals(
286+
t.param.ParameterType.IsByReference &&
287+
comparer.Equals(
275288
t.param.ParameterType,
276289
new ByReferenceType(injectTarget.Body.Variables[t.index].VariableType))),
277290
"Supposed to receive local references, but the types between injection method and target method mismatch. Also make sure they are passed by reference (ref/out).");
@@ -286,13 +299,13 @@ private static int VerifyInjectionDefinition(MethodDefinition injectMethod, Meth
286299
Assert(
287300
memberReferences.All(
288301
m =>
289-
m.DeclaringType.FullName == injectTarget.DeclaringType.FullName
290-
&& m.DeclaringType.BaseType.FullName == injectTarget.DeclaringType.BaseType.FullName),
302+
m.DeclaringType.FullName == injectTarget.DeclaringType.FullName &&
303+
m.DeclaringType.BaseType.FullName == injectTarget.DeclaringType.BaseType.FullName),
291304
$"The provided member fields do not belong to {injectTarget.DeclaringType}");
292305

293306
IEnumerable<TypeReference> paramRefs =
294-
injectMethod.Parameters.Slice(prefixCount + localsCount, memberRefCount)
295-
.Select(p => p.ParameterType);
307+
injectMethod.Parameters.Slice(prefixCount + localsCount, memberRefCount)
308+
.Select(p => p.ParameterType);
296309
IEnumerable<TypeReference> typeReferences = paramRefs as TypeReference[] ?? paramRefs.ToArray();
297310

298311
Assert(
@@ -313,15 +326,16 @@ private static int VerifyInjectionDefinition(MethodDefinition injectMethod, Meth
313326
"The injection and target methods have mismatching specification of generic parameters!");
314327

315328
Assert(
316-
!injectMethod.HasGenericParameters
317-
|| injectMethod.GenericParameters.Count <= injectTarget.GenericParameters.Count +
329+
!injectMethod.HasGenericParameters ||
330+
injectMethod.GenericParameters.Count <=
331+
injectTarget.GenericParameters.Count +
318332
injectTarget.DeclaringType.GenericParameters.Count,
319333
"The injection and target methods have a mismatching number of generic parameters! The injection method must have less or the same number of generic parameters as the target!");
320334

321335
Assert(
322-
!hFlags.PassParametersByRef
323-
|| injectMethod.Parameters.Skip(prefixCount + localsCount + memberRefCount)
324-
.All(p => p.ParameterType.IsByReference),
336+
!hFlags.PassParametersByRef ||
337+
injectMethod.Parameters.Skip(prefixCount + localsCount + memberRefCount)
338+
.All(p => p.ParameterType.IsByReference),
325339
"Supposed to pass target method parameters by reference, but the provided parameters in the injection method are not of a reference type (ref).");
326340

327341
Assert(
@@ -369,7 +383,8 @@ public void Inject(int startCode = 0, object token = null, InjectDirection direc
369383
/// parameter to the injection method.
370384
/// </param>
371385
/// <param name="direction">The direction in which to insert the call: either above the start code or below it.</param>
372-
public void Inject(Instruction startCode, object token = null,
386+
public void Inject(Instruction startCode,
387+
object token = null,
373388
InjectDirection direction = InjectDirection.Before)
374389
{
375390
InjectValues flags = Flags.ToValues();
@@ -397,10 +412,11 @@ public void Inject(Instruction startCode, object token = null,
397412
MethodReference hookRef = InjectTarget.Module.Import(InjectMethod);
398413

399414
// If the hook is generic but not instantiated fully, attempt to fill in the generic arguments with the ones specified in the target method/class
400-
if (hookRef.HasGenericParameters && (!hookRef.IsGenericInstance ||
401-
hookRef.IsGenericInstance &&
402-
((GenericInstanceMethod) hookRef).GenericArguments.Count <
403-
hookRef.GenericParameters.Count))
415+
if (hookRef.HasGenericParameters &&
416+
(!hookRef.IsGenericInstance ||
417+
hookRef.IsGenericInstance &&
418+
((GenericInstanceMethod) hookRef).GenericArguments.Count <
419+
hookRef.GenericParameters.Count))
404420
{
405421
GenericInstanceMethod genericInjectMethod = new GenericInstanceMethod(hookRef);
406422
foreach (GenericParameter genericParameter in InjectMethod.GenericParameters)

Mono.Cecil.Inject/MethodDefinitionExtensions.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -65,7 +65,7 @@ public static InjectionDefinition GetInjector(this MethodDefinition target,
6565
public static void InjectWith(this MethodDefinition method,
6666
MethodDefinition injectionMethod,
6767
int codeOffset = 0,
68-
int tag = 0,
68+
object tag = null,
6969
InjectFlags flags = InjectFlags.None,
7070
InjectDirection dir = InjectDirection.Before,
7171
int[] localsID = null,

Mono.Cecil.Inject/ParamHelper.cs

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -44,7 +44,10 @@ public static Type CreateDummyType(string name)
4444
/// </returns>
4545
public static TypeReference CreateGeneric(string name)
4646
{
47-
return new GenericParameter(new TypeReference(name, null, resolverModule, null)) {Name = name};
47+
return new GenericParameter(new TypeReference(name, null, resolverModule, null))
48+
{
49+
Name = name
50+
};
4851
}
4952

5053
/// <summary>

Mono.Cecil.Inject/Properties/AssemblyInfo.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -35,5 +35,5 @@
3535
// by using the '*' as shown below:
3636
// [assembly: AssemblyVersion("1.0.*")]
3737

38-
[assembly: AssemblyVersion("1.2.5.0")]
39-
[assembly: AssemblyFileVersion("1.2.5.0")]
38+
[assembly: AssemblyVersion("1.2.5.1")]
39+
[assembly: AssemblyFileVersion("1.2.5.1")]

Mono.Cecil.Inject/TypeDefinitionExtensions.cs

Lines changed: 26 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -132,7 +132,12 @@ public static InjectionDefinition GetInjectionMethod(this TypeDefinition type,
132132
params FieldDefinition[] memberReferences)
133133
{
134134
return InjectionDefinition.FindInjectionDefinition(
135-
type, name, target, flags, localVarIDs, memberReferences);
135+
type,
136+
name,
137+
target,
138+
flags,
139+
localVarIDs,
140+
memberReferences);
136141
}
137142

138143

@@ -199,11 +204,11 @@ public static MethodDefinition GetMethod(this TypeDefinition self, string method
199204
public static MethodDefinition GetMethod(this TypeDefinition self, string methodName, params string[] types)
200205
{
201206
return
202-
self.Methods.FirstOrDefault(
203-
m =>
204-
m.Name == methodName
205-
&& types.SequenceEqual(m.Parameters.Select(p => p.ParameterType.FullName),
206-
StringComparer.InvariantCulture));
207+
self.Methods.FirstOrDefault(
208+
m =>
209+
m.Name == methodName &&
210+
types.SequenceEqual(m.Parameters.Select(p => p.ParameterType.FullName),
211+
StringComparer.InvariantCulture));
207212
}
208213

209214
/// <summary>
@@ -263,13 +268,14 @@ public static MethodDefinition[] MatchMethod(this TypeDefinition self,
263268
params string[] paramTypes)
264269
{
265270
return
266-
self.Methods.Where(
267-
m =>
268-
m.Name == methodName && paramTypes.Length <= m.Parameters.Count
269-
&& paramTypes.SequenceEqual(
270-
m.Parameters.Take(paramTypes.Length).Select(p => p.ParameterType.FullName),
271-
StringComparer.InvariantCulture))
272-
.ToArray();
271+
self.Methods.Where(
272+
m =>
273+
m.Name == methodName &&
274+
paramTypes.Length <= m.Parameters.Count &&
275+
paramTypes.SequenceEqual(
276+
m.Parameters.Take(paramTypes.Length).Select(p => p.ParameterType.FullName),
277+
StringComparer.InvariantCulture))
278+
.ToArray();
273279
}
274280
}
275281

@@ -292,8 +298,11 @@ public bool Equals(TypeReference x, TypeReference y)
292298

293299

294300
if (
295-
!(x.Name == y.Name && x.Namespace == y.Namespace && x.IsArray == y.IsArray
296-
&& x.IsGenericInstance == y.IsGenericInstance && x.IsByReference == y.IsByReference))
301+
!(x.Name == y.Name &&
302+
x.Namespace == y.Namespace &&
303+
x.IsArray == y.IsArray &&
304+
x.IsGenericInstance == y.IsGenericInstance &&
305+
x.IsByReference == y.IsByReference))
297306
return false;
298307
if (!x.IsGenericInstance)
299308
return true;
@@ -305,8 +314,8 @@ public bool Equals(TypeReference x, TypeReference y)
305314
$"Generic arg count| x: {gx.GenericArguments.Count}, y: {gy.GenericArguments.Count}");
306315
Logger.LogLine(LogMask.TypeCompare, "Comparing generics");
307316

308-
return gx.GenericArguments.Count == gy.GenericArguments.Count
309-
&& !gx.GenericArguments.Where((t, i) => !Equals(t, gy.GenericArguments[i])).Any();
317+
return gx.GenericArguments.Count == gy.GenericArguments.Count &&
318+
!gx.GenericArguments.Where((t, i) => !Equals(t, gy.GenericArguments[i])).Any();
310319
}
311320

312321
public int GetHashCode(TypeReference obj)

0 commit comments

Comments
 (0)