Skip to content

CodeMatch.Calls(MethodInfo) does not match constructors despite XML docs/summary claiming it does #758

@SokyranTheDragon

Description

@SokyranTheDragon

Describe the bug
CodeMatch.Calls(MethodInfo) does not accept a ConstructorInfo/MethodBase, nor does it check for the Newobj opcode.

This contradicts the XML docs/summary, which states that it tests for a constructor.

/// <summary>Tests if the code instruction calls the method/constructor</summary>
/// <param name="method">The method</param>
/// <returns>A new code match</returns>
///
public static CodeMatch Calls(MethodInfo method) => WithOpcodes(CodeInstructionExtensions.opcodesCalling, method);

CodeMatch.Calls uses opcodesCalling, which only includes Call and Callvirt opcodes.

To Reproduce
Situation 1 (matching any call):

  1. Create a CodeMatcher
  2. Call one of the matching methods while passing CodeMatch.Calls((MethodInfo)null)
  3. The matcher will only match Call and Callvirt opcodes, ignoring any Newobj opcodes

Situation 2 (matching specific constructor):

  1. Create a CodeMatcher
  2. Get a ConstructorInfo object
  3. Call one of the matching methods while passing CodeMatch.Calls(constructorInfo)
  4. Code cannot compile as there's no matching method signature

Expected behavior
I've expected that either of those 2 would be true:

  • CodeMatch.Calls summary does not mention testing for a constructor
  • CodeMatch.Calls accepts MethodBase/ConstructorInfo (possibly as an overload) and matches Newobj opcode

Screenshots / Code
The following code will go through all instructions and log all matches for CodeMatch.Calls (with null as argument). It'll log every instruction using Call and Callvirt opcodes, but none using Newobj opcodes.

var matcher = new CodeMatcher(instr);
while (true)
{
    matcher.MatchEndForward(CodeMatch.Calls((MethodInfo)null));
    if (matcher.IsValid)
        Console.WriteLine($"{matcher.Instruction}");
    else
        break;
    matcher.Advance();
}

Runtime environment (please complete the following information):

  • OS: Windows 10, 64bit
  • .NET Framework 4.8
  • Harmony version 2.4.1
  • Name of game or host application: RimWorld

Metadata

Metadata

Labels

No labels
No labels

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions