129 lines
7.2 KiB
Java
129 lines
7.2 KiB
Java
import java.nio.file.Files;
|
|
import java.nio.file.Path;
|
|
import java.util.HashSet;
|
|
import java.util.Set;
|
|
import org.objectweb.asm.*;
|
|
import org.objectweb.asm.tree.*;
|
|
|
|
class RegionalCookingMerge implements Opcodes {
|
|
private static final String JARVIS="com/dunerealms/jarvis/";
|
|
private static final String TERRAIN="com/example/duneterrain/";
|
|
private static final String PLUGIN="Lorg/bukkit/plugin/Plugin;";
|
|
public static void main(String[] args) throws Exception {
|
|
if(args.length!=3)throw new IllegalArgumentException("Expected retained, donor and output classes");
|
|
var reader=new ClassReader(Files.readAllBytes(Path.of(args[0])));
|
|
var old=parse(reader);
|
|
var donor=parse(new ClassReader(Files.readAllBytes(Path.of(args[1]))));
|
|
if(!old.name.equals(donor.name))throw new IllegalArgumentException("Class mismatch");
|
|
switch(old.name){
|
|
case JARVIS+"JarvisTranslator" -> copy(old,donor,"hydrationRestoration");
|
|
case JARVIS+"SurvivalFoodRecipes" -> addRegionalStartup(old);
|
|
case "com/dunerealms/gui/OrganisationMenus" -> copy(old,donor,
|
|
"cookbook","regionalCookbook","regionalRecipe","applyRegionalIcon");
|
|
case TERRAIN+"NativePlants" -> copy(old,donor,"allowed");
|
|
case TERRAIN+"PlanetMobs" -> {
|
|
if(args[2].endsWith("giedi.class"))copy(old,donor,"allowed");
|
|
else if(args[2].endsWith("kaitain.class"))addLivestock(old);
|
|
else throw new IllegalArgumentException("Unknown PlanetMobs profile");
|
|
}
|
|
case TERRAIN+"DuneTerrain$PlanetChunkGenerator" -> copy(old,donor,"placeOasisTree");
|
|
default -> throw new IllegalArgumentException("Unexpected target "+old.name);
|
|
}
|
|
var writer=new ClassWriter(reader,0);
|
|
old.accept(writer);
|
|
Files.write(Path.of(args[2]),writer.toByteArray());
|
|
}
|
|
private static void addRegionalStartup(ClassNode owner){
|
|
var method=unique(owner,"loaded");
|
|
AbstractInsnNode anchor=null;
|
|
for(var instruction:method.instructions){
|
|
if(instruction instanceof LdcInsnNode constant && constant.cst instanceof String text
|
|
&& text.startsWith("Survival food smoker recipes registered;")){
|
|
var next=instruction.getNext();
|
|
if(anchor!=null || !(next instanceof MethodInsnNode call) || !call.owner.equals("java/util/logging/Logger")
|
|
|| !call.name.equals("info"))throw new IllegalArgumentException("Unexpected food startup anchor");
|
|
anchor=next;
|
|
}
|
|
}
|
|
if(anchor==null)throw new IllegalArgumentException("Food startup anchor absent");
|
|
var hook=new InsnList();
|
|
hook.add(new TypeInsnNode(NEW,JARVIS+"NativeLeafDecay"));
|
|
hook.add(new InsnNode(DUP));
|
|
hook.add(new VarInsnNode(ALOAD,0));
|
|
hook.add(new FieldInsnNode(GETFIELD,owner.name,"plugin",PLUGIN));
|
|
hook.add(new MethodInsnNode(INVOKESPECIAL,JARVIS+"NativeLeafDecay","<init>","("+PLUGIN+")V",false));
|
|
hook.add(new InsnNode(POP));
|
|
hook.add(new VarInsnNode(ALOAD,0));
|
|
hook.add(new FieldInsnNode(GETFIELD,owner.name,"plugin",PLUGIN));
|
|
hook.add(new VarInsnNode(ALOAD,0));
|
|
hook.add(new FieldInsnNode(GETFIELD,owner.name,"metadata","L"+JARVIS+"ItemMetadataOverlay;"));
|
|
hook.add(new VarInsnNode(ALOAD,0));
|
|
hook.add(new MethodInsnNode(INVOKESTATIC,JARVIS+"RegionalCooking","start",
|
|
"("+PLUGIN+"L"+JARVIS+"ItemMetadataOverlay;L"+owner.name+";)V",false));
|
|
method.instructions.insert(anchor,hook);
|
|
method.maxStack=Math.max(method.maxStack,4);
|
|
}
|
|
private static void addLivestock(ClassNode owner){
|
|
var method=unique(owner,"start");
|
|
if(!method.desc.equals("("+PLUGIN+")V") || (method.access&ACC_STATIC)==0)
|
|
throw new IllegalArgumentException("Unexpected Terrain startup signature");
|
|
var hook=new InsnList();
|
|
hook.add(new MethodInsnNode(INVOKESTATIC,"org/bukkit/Bukkit","getPluginManager","()Lorg/bukkit/plugin/PluginManager;",false));
|
|
hook.add(new TypeInsnNode(NEW,TERRAIN+"KaitainLivestock"));
|
|
hook.add(new InsnNode(DUP));
|
|
hook.add(new VarInsnNode(ALOAD,0));
|
|
hook.add(new MethodInsnNode(INVOKESPECIAL,TERRAIN+"KaitainLivestock","<init>","("+PLUGIN+")V",false));
|
|
hook.add(new VarInsnNode(ALOAD,0));
|
|
hook.add(new MethodInsnNode(INVOKEINTERFACE,"org/bukkit/plugin/PluginManager","registerEvents",
|
|
"(Lorg/bukkit/event/Listener;"+PLUGIN+")V",true));
|
|
method.instructions.insert(hook);
|
|
method.maxStack=Math.max(method.maxStack,4);
|
|
}
|
|
private static void copy(ClassNode old, ClassNode donor, String... names) {
|
|
var copied = new HashSet<String>();
|
|
for (String name : names) {
|
|
var method = unique(donor, name);
|
|
var prior = old.methods.stream().filter(m -> m.name.equals(name) && m.desc.equals(method.desc)).findFirst();
|
|
if (prior.isEmpty() && !Set.of("hydrationRestoration", "regionalCookbook", "regionalRecipe", "applyRegionalIcon").contains(name)) {
|
|
throw new IllegalArgumentException("Missing retained method " + name);
|
|
}
|
|
prior.ifPresent(old.methods::remove);
|
|
copyMethod(old, donor, method, copied);
|
|
}
|
|
}
|
|
|
|
private static void copyMethod(ClassNode old, ClassNode donor, MethodNode method, Set<String> copied) {
|
|
if (!copied.add(method.name + method.desc)) return;
|
|
var clone = new MethodNode(method.access, method.name, method.desc, method.signature, method.exceptions.toArray(String[]::new));
|
|
method.accept(clone);
|
|
if (clone.name.startsWith("lambda$")) clone.name = "regionalCooking$" + clone.name;
|
|
if (old.methods.stream().anyMatch(m -> m.name.equals(clone.name) && m.desc.equals(clone.desc))) {
|
|
throw new IllegalArgumentException("Duplicate method " + clone.name);
|
|
}
|
|
for (var instruction : clone.instructions) {
|
|
if (!(instruction instanceof InvokeDynamicInsnNode dynamic)) continue;
|
|
for (int i = 0; i < dynamic.bsmArgs.length; i++) {
|
|
if (!(dynamic.bsmArgs[i] instanceof Handle handle) || !handle.getOwner().equals(donor.name)
|
|
|| !handle.getName().startsWith("lambda$")) continue;
|
|
var target = donor.methods.stream().filter(m -> m.name.equals(handle.getName())
|
|
&& m.desc.equals(handle.getDesc())).findFirst().orElseThrow();
|
|
copyMethod(old, donor, target, copied);
|
|
dynamic.bsmArgs[i] = new Handle(handle.getTag(), handle.getOwner(),
|
|
"regionalCooking$" + handle.getName(), handle.getDesc(), handle.isInterface());
|
|
}
|
|
}
|
|
old.methods.add(clone);
|
|
}
|
|
|
|
private static MethodNode unique(ClassNode node, String name) {
|
|
var methods = node.methods.stream().filter(m -> m.name.equals(name)).toList();
|
|
if (methods.size() != 1) throw new IllegalArgumentException("Expected one " + node.name + "." + name);
|
|
return methods.getFirst();
|
|
}
|
|
|
|
private static ClassNode parse(ClassReader reader) {
|
|
var node = new ClassNode();
|
|
reader.accept(node, 0);
|
|
return node;
|
|
}
|
|
}
|

Staff