Add Categories feature to Patch Container Classes #498
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Summary
This change adds the following features:
[HarmonyPatchCategory(string category)]
attribute, applied to patch container classes to specify one category for a patchHarmony.PatchCategories(string category)
method, called to apply all patch container classes marked with a specific categoryHarmony.PatchAllUncategorized()
method, called to apply any patch container classes not marked with a specific categoryThis functionality allows applying different sets of patches at different times when using annotated patch classes. This allows for easy conditional patch application, allowing things like delaying a patch until after a static initializer has run, or performing patches based on user configuration.
Notes
For simplicity,
[HarmonyPatchCategory]
can only be applied to patch container classes, not individual patch methods within a container. This avoids having to make a library-level choice about how individual patch method categorization overrides container categorization, which I believe would be an awkward design. Instead, users can control their categorization as needed to create conditional logic for what patches to apply and when.Only one category can be applied to a patch. This simplifies the implementation and means that patching multiple different categories will never cause a patch container to be applied more than once.Users can use specific
This change does not change the behavior of
Unpatch()
orUnpatchAll()
. Because of how those methods work, they act based on the methods that were patched, not what patches were used, so it would require extra data to be added to identify what patch categories were used to patch something. Additionally, unpatching seems to unpatch as a whole, so if you only wanted to unpatch a method's patches from one category, it would also unpatch any changes from another category. Future work could maybe change how Unpatching logic is done to allow partial unpatching (or unpatching and re-patching based on categories).