mirror of
https://github.com/xoo0524tw/OpenArcadeGames.git
synced 2026-12-06 02:46:01 +02:00
53 lines
3.0 KiB
Java
53 lines
3.0 KiB
Java
import java.nio.charset.StandardCharsets;
|
|
import java.nio.file.*;
|
|
import java.util.*;
|
|
import java.util.jar.*;
|
|
import java.io.*;
|
|
import org.objectweb.asm.*;
|
|
import org.objectweb.asm.tree.*;
|
|
|
|
/** Deterministic source-compatibility linking for Java-keyword external method aliases (e.g. do_ -> do). */
|
|
public final class FinalizeClasses {
|
|
record Key(String owner,String name,String descriptor){}
|
|
static final Map<Key,String> aliases=new HashMap<>();
|
|
static int count,relinked;
|
|
static byte[] link(byte[] input){
|
|
ClassNode c=new ClassNode();new ClassReader(input).accept(c,0);
|
|
boolean modified=false;
|
|
for(MethodNode method:c.methods){
|
|
for(AbstractInsnNode instruction:method.instructions){
|
|
if(instruction instanceof MethodInsnNode call){String original=aliases.get(new Key(call.owner,call.name,call.desc));if(original!=null){call.name=original;relinked++;modified=true;}}
|
|
else if(instruction instanceof InvokeDynamicInsnNode indy){for(int k=0;k<indy.bsmArgs.length;k++)if(indy.bsmArgs[k] instanceof Handle h){String original=aliases.get(new Key(h.getOwner(),h.getName(),h.getDesc()));if(original!=null){indy.bsmArgs[k]=new Handle(h.getTag(),h.getOwner(),original,h.getDesc(),h.isInterface());relinked++;modified=true;}}}
|
|
}
|
|
}
|
|
count++;
|
|
if(!modified)return input;
|
|
ClassWriter writer=new ClassWriter(0);c.accept(writer);return writer.toByteArray();
|
|
}
|
|
public static void main(String[] args)throws Exception{
|
|
for(String line:Files.readAllLines(Path.of(args[1]),StandardCharsets.UTF_8)){
|
|
if(line.isBlank()||line.startsWith("#")||line.startsWith("owner\t"))continue;String[] p=line.split("\t",-1);aliases.put(new Key(p[0],p[1],p[3]),p[2]);
|
|
}
|
|
Path directory=Path.of(args[2]);
|
|
if(Files.isRegularFile(directory)){
|
|
Path staging=directory.resolveSibling(directory.getFileName()+".linking");
|
|
try(JarFile input=new JarFile(directory.toFile());JarOutputStream output=new JarOutputStream(Files.newOutputStream(staging))){
|
|
for(JarEntry entry:Collections.list(input.entries())){
|
|
JarEntry copy=new JarEntry(entry.getName());copy.setTime(entry.getTime());output.putNextEntry(copy);
|
|
if(!entry.isDirectory()){byte[] bytes=input.getInputStream(entry).readAllBytes();output.write(entry.getName().endsWith(".class")?link(bytes):bytes);}output.closeEntry();
|
|
}
|
|
}
|
|
Files.move(staging,directory,StandardCopyOption.REPLACE_EXISTING,StandardCopyOption.ATOMIC_MOVE);
|
|
System.out.println("Artifact linking: classes="+count+", restoredReservedCalls="+relinked);return;
|
|
}
|
|
if(!Files.isDirectory(directory))return;
|
|
try(var paths=Files.walk(directory)){
|
|
for(Path path:paths.filter(p->p.toString().endsWith(".class")).sorted().toList()){
|
|
byte[] input=Files.readAllBytes(path),output=link(input);
|
|
if(input!=output){Path staging=path.resolveSibling(path.getFileName()+".linking");Files.write(staging,output);Files.move(staging,path,StandardCopyOption.REPLACE_EXISTING,StandardCopyOption.ATOMIC_MOVE);}
|
|
}
|
|
}
|
|
System.out.println("Source linking: classes="+count+", restoredReservedCalls="+relinked);
|
|
}
|
|
}
|