Harmony
Harmony copied to clipboard
Deprecate transpiler ILGenerator parameter in favor of some Label/LocalBuilder factory
Currently, ILGenerator must be passed to transpilers in order to define labels and declare locals. However, they must not be used to actually emit any instructions, and there are no guard rails to prevent novice users from misusing ILGenerator as such.
Transpilers are the only place where ILGenerator "leaks" from its effective encapsulation in Harmony. This transpiler parameter should be deprecated in favor of some new factory object parameter that's only for defining labels and declare locals.
For backwards compatibility, the ILGenerator parameter must still be supported. But it could be wrapped, such that calls to methods other than DefineLabel and DeclareLocal generate warnings. That said, there's no warn log functionality in Harmony, and spamming the default harmony.log.txt outside of debug mode isn't a great idea.
A prototype for a replacement that bundles DeclareLocal and opcode-variant-agnostic local variable instruction translations (i.e. doesn't matter if ldloc.1 or ldloc.s with operand, etc): https://github.com/lbmaian/DoorsExpanded/blob/f50bd9b3da99d1a4cac63f030d8244982f5f7196/Source/HarmonyExtensions.cs#L49
It does this be reading local variable info from the MethodBase, so it would be convenient if something like this was injectable.
If this can be generalized to also cover opcode-variant-agnostic method argument translations (i.e. doesn't matter if ldarg.1 or ldarg.s with operand, etc), then it would be reasonable to also bundle a DefineLabel with this resulting "utility" object.