plugins/tools/HydrationMerge.java
2026-12-06 14:41:51 +01:00

54 lines
5.5 KiB
Java

import java.nio.file.*;
import java.util.*;
import java.util.zip.*;
import org.objectweb.asm.*;
import org.objectweb.asm.tree.*;
/** Adds profile hydration to the retained native SQL paths without replacing their ownership logic. */
public class HydrationMerge {
static final String P="com/example/dunerealms/",H=P+"HydrationProfiles",DB=P+"SharedDatabase";
static MethodInsnNode call(String name,String descriptor){return new MethodInsnNode(Opcodes.INVOKESTATIC,H,name,descriptor,false);}
static ClassNode read(byte[] b){ClassNode c=new ClassNode();new ClassReader(b).accept(c,0);return c;}
static byte[] write(ClassNode c){ClassWriter w=new ClassWriter(ClassWriter.COMPUTE_MAXS);c.accept(w);return w.toByteArray();}
static void need(boolean condition,String message){if(!condition)throw new IllegalStateException(message);}
public static void main(String[] args)throws Exception{
Path out=Path.of(args[1]);Files.createDirectories(out);
try(ZipFile zip=new ZipFile(args[0])){
ClassNode db=read(zip.getInputStream(zip.getEntry(DB+".class")).readAllBytes());
int ctors=0,loads=0,writes=0,loadCalls=0,binds=0,releases=0;
for(MethodNode m:db.methods){
if(m.name.equals("<init>"))for(AbstractInsnNode n:m.instructions.toArray())if(n.getOpcode()==Opcodes.RETURN){InsnList x=new InsnList();x.add(new VarInsnNode(Opcodes.ALOAD,0));x.add(call("initialize","(L"+DB+";)V"));m.instructions.insertBefore(n,x);ctors++;}
if(m.name.equals("loadPlayer")){
for(AbstractInsnNode n:m.instructions.toArray()){
if(n instanceof LdcInsnNode l&&l.cst instanceof String s&&s.startsWith("SELECT faction,inventory")&&s.contains("session_open FROM dune_players")){l.cst=s.replace("session_open FROM","session_open,hydration FROM");loads++;}
if(n instanceof MethodInsnNode i&&i.owner.equals("java/sql/ResultSet")&&i.name.equals("getString")&&n.getPrevious() instanceof LdcInsnNode l&&"faction".equals(l.cst)&&l.getPrevious() instanceof VarInsnNode v&&v.getOpcode()==Opcodes.ALOAD){InsnList x=new InsnList();x.add(new VarInsnNode(Opcodes.ALOAD,1));x.add(new VarInsnNode(Opcodes.ALOAD,v.var));x.add(call("load","(Lorg/bukkit/entity/Player;Ljava/sql/ResultSet;)V"));m.instructions.insertBefore(v,x);loadCalls++;}
}
}
if(m.name.equals("writePlayer"))for(AbstractInsnNode n:m.instructions.toArray()){
if(n instanceof InvokeDynamicInsnNode indy)for(int a=0;a<indy.bsmArgs.length;a++)if(indy.bsmArgs[a] instanceof String s&&s.startsWith("UPDATE dune_players SET faction=")){
need(s.chars().filter(c->c=='?').count()==8&&s.chars().filter(c->c==1).count()==1&&s.endsWith(" WHERE uuid=?")&&!s.contains("hydration"),"Unexpected dynamic profile UPDATE");
indy.bsmArgs[a]=s.replace(" WHERE uuid=?",",hydration=? WHERE uuid=?");writes++;
}
if(n instanceof LdcInsnNode l&&l.cst instanceof String s&&s.startsWith("UPDATE dune_players SET faction=")){
need(s.chars().filter(c->c=='?').count()==9&&!s.contains("hydration"),"Unexpected native profile UPDATE");
need(s.endsWith(" WHERE uuid=?"),"Unexpected native profile WHERE");l.cst=s.replace(" WHERE uuid=?",",hydration=? WHERE uuid=?");writes++;
}
if(n instanceof MethodInsnNode i&&i.owner.equals("java/sql/PreparedStatement")&&i.name.equals("executeUpdate")&&i.desc.equals("()I")&&n.getPrevious() instanceof VarInsnNode v&&v.getOpcode()==Opcodes.ALOAD){InsnList x=new InsnList();x.add(new VarInsnNode(Opcodes.ALOAD,v.var));x.add(new VarInsnNode(Opcodes.ALOAD,1));x.add(call("bind","(Ljava/sql/PreparedStatement;Lorg/bukkit/entity/Player;)V"));m.instructions.insertBefore(v,x);binds++;}
}
if(m.name.equals("releasePlayer"))for(AbstractInsnNode n:m.instructions.toArray())if(n.getOpcode()==Opcodes.RETURN||n.getOpcode()==Opcodes.ATHROW){InsnList x=new InsnList();x.add(new VarInsnNode(Opcodes.ALOAD,1));x.add(call("forget","(Lorg/bukkit/entity/Player;)V"));m.instructions.insertBefore(n,x);releases++;}
}
need(ctors>0&&loads==1&&loadCalls==1&&(writes==1||writes==2)&&binds==1&&releases>0,"Native SQL anchor mismatch: "+List.of(ctors,loads,loadCalls,writes,binds,releases));
Files.write(out.resolve("SharedDatabase.class"),write(db));
ClassNode core=read(zip.getInputStream(zip.getEntry(P+"DuneCore.class")).readAllBytes());
boolean getter=core.methods.stream().anyMatch(m->m.name.equals("sharedDatabase")&&m.desc.equals("()L"+DB+";")&&(m.access&Opcodes.ACC_PUBLIC)!=0);
if(!getter){need(core.fields.stream().anyMatch(f->f.name.equals("sharedDatabase")&&f.desc.equals("L"+DB+";")),"Shared profile field missing");MethodNode m=new MethodNode(Opcodes.ACC_PUBLIC,"sharedDatabase","()L"+DB+";",null,null);m.instructions.add(new VarInsnNode(Opcodes.ALOAD,0));m.instructions.add(new FieldInsnNode(Opcodes.GETFIELD,core.name,"sharedDatabase","L"+DB+";"));m.instructions.add(new InsnNode(Opcodes.ARETURN));core.methods.add(m);}
int enable=0,disable=0;
for(MethodNode m:core.methods)for(AbstractInsnNode n:m.instructions.toArray())if(n.getOpcode()==Opcodes.RETURN){
if(m.name.equals("onEnable")){InsnList x=new InsnList();x.add(new VarInsnNode(Opcodes.ALOAD,0));x.add(call("start","(Lorg/bukkit/plugin/java/JavaPlugin;)V"));m.instructions.insertBefore(n,x);enable++;}
if(m.name.equals("onDisable")){m.instructions.insertBefore(n,call("close","()V"));disable++;}
}
need(enable>0&&disable>0,"Core lifecycle missing");Files.write(out.resolve("DuneCore.class"),write(core));
System.out.println("Preserved retained SQL/session logic; added hydration hooks "+List.of(ctors,loads,loadCalls,writes,binds,releases,enable,disable));
}
}
}