LoliKingdom / Bansoukou

Bansōkō [絆創膏] - a simple coremod that streamlines patching of (most) mods
9 stars 3 forks source link

[Feature Request] Bytecode splice #3

Open Krutoy242 opened 1 year ago

Krutoy242 commented 1 year ago

Please add feature to splice bytecode.

Problem

Sometimes, i need to modify only one integer or string in .class files.
Some mods with "All Rights Reserved" license forbid to distribute their .class files, so i can't use Bansoukou.
Other devs using mixins and coremods for this, but i have lack of Java knowledge.

Suggestion

Please add feature to change binary file content without providing whole binary file.
For example, it could be .json with commands that will replace bytes.

Manual example

I have ImmersiveEngineering Silo that i want to change capacity stored in int

public class TileEntitySilo
extends TileEntityMultiblockPart<TileEntitySilo>
implements IEBlockInterfaces.IComparatorOverride {
    public ItemStack identStack = ItemStack.field_190927_a;
    public int storageAmount = 0;
    static int maxStorage = 41472; // Value i want to change
    boolean lockItem = false;
    private int[] oldComps = new int[6];

When i used Bansoukou for the first time, i opened this .class file with Hex editor, found 00 00 A2 and replace it to 00 FF FF. So now my code should looks like:

static int maxStorage = 16776960;

I saved this file into bansoukou/ folder and its work.

Feature suggestion example

What i want is to do same actions in steps above, but on MC launch.

For example, it could be .json file with arrays of instructions.
In addition to file path it could have special characters that instruct to read file as instructions instead of inject in .jar as is. In my example, file called TileEntitySilo.class and instructions file called TileEntitySilo.class$$.json

bansoukou/ImmersiveEngineering-0.12-98/blusunrize/immersiveengineering/common/blocks/metal/TileEntitySilo.class$$.json

Instruction file could have arrays of JS-like .splice() parameters:

[
  {
    "index": 6574, // The position to add/remove bytes
    "remove": 2, // Number of bytes removed, optional
    "insert": [255, 255] // New byte(s) to be added
  }
]

When Bansoukou run, it would open .class file in binary format, and replace bytes 6574 and 6575 to FF FF and then save -patched .jar as usual.

Note:
It would be nice if "index" and "insert" values could accept strings, so i could write:

"index": "19AE",
"remove": 2,
"insert": "FF FF"
Rongmario commented 1 month ago

I will introduce a binary patching system for this.