Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Pass through postfixes not excuting when have Finalizer #254

Closed
pengweiqhca opened this issue Feb 20, 2020 · 0 comments
Closed

Pass through postfixes not excuting when have Finalizer #254

pengweiqhca opened this issue Feb 20, 2020 · 0 comments
Assignees

Comments

@pengweiqhca
Copy link
Contributor

Describe the bug
Pass through postfixes not excuting when have Finalizer

Expected behavior

Transpiler
Prefix
Finalizer
GetNumbers
Postfix
0, 20, 30, 99

Screenshots / Code

   public class Annotations
    {
        private readonly bool _isRunning;

        public IEnumerable<int> GetNumbers()
        {
            Console.WriteLine(nameof(GetNumbers));

            yield return 1;
            yield return 2;
            yield return 3;
        }
    }

    [HarmonyPatch(typeof(Annotations))]
    [HarmonyPatch(nameof(Annotations.GetNumbers))]
    public class AnnotationsPatcher
    {
        static AccessTools.FieldRef<Annotations, bool> isRunningRef =
            AccessTools.FieldRefAccess<Annotations, bool>("_isRunning");

        public static void Patch()
        {
            var harmony = new Harmony(nameof(AnnotationsPatcher));

            harmony.PatchAll();

            Console.WriteLine(string.Join(", ", new Annotations().GetNumbers()));
        }

        static bool Prefix(Annotations __instance)
        {
            Console.WriteLine("Prefix");

            return true;
        }

        /// <summary>Not working</summary>
        static IEnumerable<int> Postfix(IEnumerable<int> values)
        {
            yield return 0;
            foreach (var value in values)
                if (value > 1)
                    yield return value * 10;
            yield return 99;
            Console.WriteLine(nameof(Postfix));
        }

        // looks for STDFLD someField and inserts CALL MyExtraMethod before it
        public static IEnumerable<CodeInstruction> Transpiler(IEnumerable<CodeInstruction> instructions)
        {
            Console.WriteLine(nameof(Transpiler));
            //var found = false;
            foreach (var instruction in instructions)
            {
                //if (instruction.opcode == OpCodes.Stfld && instruction.operand == f_someField)
                //{
                //    yield return new CodeInstruction(OpCodes.Call, m_MyExtraMethod);
                //    found = true;
                //}
                yield return instruction;
            }
            //if (found == false)
            //    ReportError("Cannot find <Stdfld someField> in OriginalType.OriginalMethod");
        }

        public static void Finalizer(Exception __exception)
        {
            Console.WriteLine($"{nameof(Finalizer)} {__exception}");
        }
    }

Runtime environment (please complete the following information):

  • OS: Windows 10, 64bit
  • netcoreapp3.1
  • Harmony version 2.0.0.4
  • Console
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants