Closed ghorsington closed 2 years ago
Is there a reason this was closed without full implementation? I am specifically interested in this part:
- [x] Add some chainable error handling logic. Can be
.OnError(Action<string> err)
that handles errors of a previous match. If an error happens,InstructionEnumeration
and other methods return unmodified instructions. In addition, ObsoleteRepotFailure
CodeMatcher
is a really convenient tool to use, the major problem with it is that .ThrowWhenInvalid
would actually break the patch. Returning unchanged instructions like suggested seems like a very good addition. This can't be done with CodeMatcher
, it can only be achieved with classic transpilers.
@Meivyn You can just put it in a try catch and return original list if that's the error action you're looking for.
Yeah, but it would look cleaner if the matcher could provide that functionality. Catching exceptions is also less efficient than just looking for a match fail.
That is true. I don't remember exactly why the original idea wasn't implemented but at this point it's unlikely I think, since the throw methods are what is the most commonly intended, given that it's really hard to recover without knowing what the changed IL is going to be.
Currently HarmonyX contains
CodeMatcher
-- a particularly useful and versatile helper class to write Harmony transpilers as a stream:Originally the helper was part of official Harmony but it was subsequently removed citing it not being ready yet.
We decided to bring it back and did a few fixes to make some broken methods work.
However, there are some changes that can be done to CodeMatcher to make it more usable. As an example, right now it doesn't handle exceptions all that well.
This issue contains a list of some things on how CodeMatcher could be improved. Not all of them have to be done at once, this issue just contains a list of all CodeMatcher-related improvement ideas.
List of CodeMatcher improvements
.OnError(Action<string> err)
that handles errors of a previous match. If an error happens,InstructionEnumeration
and other methods return unmodified instructions. In addition, ObsoleteRepotFailure
Match*
functions use abool
parameter to specify where to move the cursor. This doesn't seem clean and should be changed to something better (maybe an enum?)CodeMatch
. Right now you useCodeMatcher.MatchForward
et al like this:Some of this can be simplified by adding some implicit casts for
CodeMatch
so that you don't have to writenew CodeMatch
in every case.