← 输入框|github.com|刷新
Skip to content

Patches may fail because of inlined methods #205

Open

Description

@Meivyn

Description

Depending on the order of patches and the length of the patched methods, some may fail to apply due to inlining.

Say for example, I'm patching method A which uses a method B that is later patched by another assembly:

  • Patch runs, completed method A IL is generated, resulting dynamic method is compiled.
  • Problematic method B is inlined because it's small and not marked NoInlining.
  • Method B is patched, dynamic method compiled, redirected, etc., but by this point it's already been inlined.

Because of this, method A won't be calling the patched method B, but the original. So the patch applied to method B will never run.

Related: BepInEx/HarmonyX#71

Example

// Assembly1 patches Method A
[HarmonyPatch(typeof(BeatmapDataLoaderVersion2_6_0AndEarlier.BeatmapDataLoader.ObstacleConverter), nameof(BeatmapDataLoaderVersion2_6_0AndEarlier.BeatmapDataLoader.ObstacleConverter.GetHeightForObstacleType))]
internal static class GetHeightForObstacleTypePatch
{
    private static void Postfix()
    {
        // This is never printed.
        Plugin.Log.Notice("GetHeightForObstacleType");
    }
}

// Method B original
private static int GetHeightForObstacleType(ObstacleType obstacleType)
{
    if (obstacleType != ObstacleType.Top)
    {
	return 5;
    }
    return 3;
}

// Assembly2 patches Method B
[HarmonyPatch(typeof(BeatmapDataLoaderVersion2_6_0AndEarlier.BeatmapDataLoader.ObstacleConverter), nameof(BeatmapDataLoaderVersion2_6_0AndEarlier.BeatmapDataLoader.ObstacleConverter.Convert))]
internal static class AllowNegativeObstacleDurationPatch
{
    private static IEnumerable<CodeInstruction> Transpiler(IEnumerable<CodeInstruction> instructions)
    {
        return instructions;
    }
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions