Posted September 24, 201213 yr Through a lot of determination and code sleuthing, I've managed to get a grasp on setting up a core mod and using it to employ class transformers. Just as a preliminary test, I made a transformer that would add || var8 == Block.arbitraryBlock.blockId to BlockFarmland in its isCropsNearby method to expand what it considered a crop. The transformer works well enough, but it got me thinking about the best ways to go about transforming classes. It seems like injecting code at arbitrary points would be fragile in that slight modification to the class by other transformers would mess up the bytecode "heuristics" I used to do it. Furthermore, just adding code to an arbitrary location in the class seems not much better than actually editing the class. So, my question is: What kind of programming practices do you consider to be effective or proper when dealing with arbitrary code insertion? I've seen patterns where you just insert a redirect at the beginning of the method and go from there, and I've also seen stuff on the internet about the proxy class design pattern (though I don't understand it too well right now). Is there another pattern that would be effective? I'm looking for advice/opinions on this mostly, how the modders on these boards do/would do it, rather than some concrete answer. P.S. After learning some ASM and going through a LOT of the code, I started to realize just how complex and powerful Forge is, so bravo cpw and LexManos, this project is definitely an achievement. EDIT: I just found out that the latest build of Forge actually has a new interface related to what I was sort of accomplishing here, but my question still stands!
September 30, 201213 yr In general you want to minimize your footprint so as to not step on other mods trying to do the same thing. Pick a good point, like a call and cancel hook at the start or something, rather than just mass editing the code.
September 30, 201213 yr Author Thanks for the advice, Overmind! I had kind of given up on this topic. I leaning in the direction of what you said. I was mostly worried about screwing up the variable indexes and possibly labels further on in a method's bytecode if I inserted sufficiently complex logic. I think that if other people are smart though, they will use simple heuristics like I did to get around that problem.
October 1, 201213 yr I was away for a while, but am back and catching up. Insert variable after the last in the class and so forth, no worry if you do that.
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.
Note: Your post will require moderator approval before it will be visible.