Working transformers.

This commit is contained in:
zaxar163 2019-01-06 16:22:17 +04:00
parent 96d1d853e8
commit 2f4870e96c
No known key found for this signature in database
GPG key ID: CEE900027AE098E0
3 changed files with 16 additions and 110 deletions

View file

@ -48,34 +48,5 @@ public void build() throws IOException {
long time_end = System.currentTimeMillis(); long time_end = System.currentTimeMillis();
IOHelper.move(thisPath, syncBinaryFile); IOHelper.move(thisPath, syncBinaryFile);
LogHelper.info("Build successful from %d millis",time_end - time_start); LogHelper.info("Build successful from %d millis",time_end - time_start);
// ProGuard
/*for (Runnable r : server.buildHookManager.getPostProguardRunHooks())
r.run();
try (ZipInputStream input = new ZipInputStream(IOHelper.newInput(obfJar));
ZipOutputStream output = new ZipOutputStream(IOHelper.newOutput(obfOutJar))) {
ZipEntry e = input.getNextEntry();
while (e != null) {
String filename = e.getName();
output.putNextEntry(IOHelper.newZipEntry(e));
if (filename.endsWith(".class")) {
String classname = filename.replace('/', '.').substring(0, filename.length() - ".class".length());
byte[] bytes;
try (ByteArrayOutputStream outputStream = new ByteArrayOutputStream(2048)) {
IOHelper.transfer(input, outputStream);
bytes = outputStream.toByteArray();
}
//bytes = server.buildHookManager.proGuardClassTransform(bytes, classname, this);
try (ByteArrayInputStream inputStream = new ByteArrayInputStream(bytes)) {
IOHelper.transfer(inputStream, output);
}
} else
IOHelper.transfer(input, output);
e = input.getNextEntry();
}
for (ZipBuildHook h : server.buildHookManager.getProguardBuildHooks())
h.build(output);
}*/
} }
} }

View file

@ -10,6 +10,7 @@
import java.nio.file.attribute.BasicFileAttributes; import java.nio.file.attribute.BasicFileAttributes;
import java.util.HashMap; import java.util.HashMap;
import java.util.Map; import java.util.Map;
import java.util.jar.JarFile;
import java.util.zip.ZipEntry; import java.util.zip.ZipEntry;
import java.util.zip.ZipException; import java.util.zip.ZipException;
import java.util.zip.ZipInputStream; import java.util.zip.ZipInputStream;
@ -22,6 +23,7 @@
import ru.gravit.launcher.LauncherConfig; import ru.gravit.launcher.LauncherConfig;
import ru.gravit.launcher.serialize.HOutput; import ru.gravit.launcher.serialize.HOutput;
import ru.gravit.launchserver.LaunchServer; import ru.gravit.launchserver.LaunchServer;
import ru.gravit.launchserver.asm.ClassMetadataReader;
import ru.gravit.launchserver.binary.BuildContext; import ru.gravit.launchserver.binary.BuildContext;
import ru.gravit.launchserver.binary.JAConfigurator; import ru.gravit.launchserver.binary.JAConfigurator;
import ru.gravit.utils.helper.IOHelper; import ru.gravit.utils.helper.IOHelper;
@ -35,6 +37,7 @@ public class MainBuildTask implements LauncherBuildTask {
public final Path binaryFile; public final Path binaryFile;
public Path cleanJar; public Path cleanJar;
private final LaunchServer server; private final LaunchServer server;
public final ClassMetadataReader reader;
private final class RuntimeDirVisitor extends SimpleFileVisitor<Path> { private final class RuntimeDirVisitor extends SimpleFileVisitor<Path> {
private final ZipOutputStream output; private final ZipOutputStream output;
private final Map<String, byte[]> runtime; private final Map<String, byte[]> runtime;
@ -66,7 +69,6 @@ public FileVisitResult visitFile(Path file, BasicFileAttributes attrs) throws IO
} }
// TODO: new native security wrapper and library... // TODO: new native security wrapper and library...
@SuppressWarnings("unused")
private final class GuardDirVisitor extends SimpleFileVisitor<Path> { private final class GuardDirVisitor extends SimpleFileVisitor<Path> {
private final ZipOutputStream output; private final ZipOutputStream output;
private final Map<String, byte[]> guard; private final Map<String, byte[]> guard;
@ -109,6 +111,7 @@ public MainBuildTask(LaunchServer srv) {
runtimeDir = server.dir.resolve(Launcher.RUNTIME_DIR); runtimeDir = server.dir.resolve(Launcher.RUNTIME_DIR);
guardDir = server.dir.resolve(Launcher.GUARD_DIR); guardDir = server.dir.resolve(Launcher.GUARD_DIR);
binaryFile = server.dir.resolve(server.config.binaryName + "-main.jar"); binaryFile = server.dir.resolve(server.config.binaryName + "-main.jar");
reader = new ClassMetadataReader();
} }
@Override @Override
@ -123,7 +126,7 @@ public Path process(Path cleanJar) throws IOException {
JAConfigurator jaConfigurator = new JAConfigurator(AutogenConfig.class.getName(), this)) { JAConfigurator jaConfigurator = new JAConfigurator(AutogenConfig.class.getName(), this)) {
jaConfigurator.pool.insertClassPath(cleanJar.toFile().getAbsolutePath()); jaConfigurator.pool.insertClassPath(cleanJar.toFile().getAbsolutePath());
BuildContext context = new BuildContext(output, jaConfigurator, this); BuildContext context = new BuildContext(output, jaConfigurator, this);
server.buildHookManager.preHook(context); server.buildHookManager.hook(context);
jaConfigurator.setAddress(server.config.getAddress()); jaConfigurator.setAddress(server.config.getAddress());
jaConfigurator.setPort(server.config.port); jaConfigurator.setPort(server.config.port);
jaConfigurator.setProjectName(server.config.projectName); jaConfigurator.setProjectName(server.config.projectName);
@ -133,6 +136,7 @@ public Path process(Path cleanJar) throws IOException {
jaConfigurator.setDownloadJava(server.config.isDownloadJava); jaConfigurator.setDownloadJava(server.config.isDownloadJava);
jaConfigurator.setEnv(server.config.env); jaConfigurator.setEnv(server.config.env);
server.buildHookManager.registerAllClientModuleClass(jaConfigurator); server.buildHookManager.registerAllClientModuleClass(jaConfigurator);
reader.getCp().add(new JarFile(cleanJar.toFile()));
try (ZipInputStream input = new ZipInputStream(IOHelper.newInput(cleanJar))) { try (ZipInputStream input = new ZipInputStream(IOHelper.newInput(cleanJar))) {
ZipEntry e = input.getNextEntry(); ZipEntry e = input.getNextEntry();
while (e != null) { while (e != null) {
@ -148,7 +152,7 @@ public Path process(Path cleanJar) throws IOException {
e = input.getNextEntry(); e = input.getNextEntry();
continue; continue;
} }
/*if (filename.endsWith(".class")) { if (filename.endsWith(".class")) {
String classname = filename.replace('/', '.').substring(0, String classname = filename.replace('/', '.').substring(0,
filename.length() - ".class".length()); filename.length() - ".class".length());
byte[] bytes; byte[] bytes;
@ -157,10 +161,8 @@ public Path process(Path cleanJar) throws IOException {
bytes = outputStream.toByteArray(); bytes = outputStream.toByteArray();
} }
bytes = server.buildHookManager.classTransform(bytes, classname, this); bytes = server.buildHookManager.classTransform(bytes, classname, this);
try (ByteArrayInputStream inputStream = new ByteArrayInputStream(bytes)) { output.write(bytes);
IOHelper.transfer(inputStream, output); } else
}
} else */
IOHelper.transfer(input, output); IOHelper.transfer(input, output);
context.fileList.add(filename); context.fileList.add(filename);
e = input.getNextEntry(); e = input.getNextEntry();
@ -176,7 +178,7 @@ public Path process(Path cleanJar) throws IOException {
if (server.buildHookManager.buildRuntime()) { if (server.buildHookManager.buildRuntime()) {
// Write launcher guard dir // Write launcher guard dir
IOHelper.walk(runtimeDir, new RuntimeDirVisitor(output, runtime), false); IOHelper.walk(runtimeDir, new RuntimeDirVisitor(output, runtime), false);
// IOHelper.walk(guardDir, new GuardDirVisitor(output, runtime), false); IOHelper.walk(guardDir, new GuardDirVisitor(output, runtime), false);
} }
// Create launcher config file // Create launcher config file
byte[] launcherConfigBytes; byte[] launcherConfigBytes;
@ -195,7 +197,6 @@ public Path process(Path cleanJar) throws IOException {
output.putNextEntry(e); output.putNextEntry(e);
jaConfigurator.compile(); jaConfigurator.compile();
output.write(jaConfigurator.getBytecode()); output.write(jaConfigurator.getBytecode());
server.buildHookManager.postHook(context);
} catch (CannotCompileException | NotFoundException e) { } catch (CannotCompileException | NotFoundException e) {
LogHelper.error(e); LogHelper.error(e);
} }

View file

@ -4,20 +4,14 @@
import java.util.HashSet; import java.util.HashSet;
import java.util.Map; import java.util.Map;
import java.util.Set; import java.util.Set;
import java.util.zip.ZipOutputStream;
import ru.gravit.launcher.AutogenConfig; import ru.gravit.launcher.AutogenConfig;
import ru.gravit.launcher.modules.TestClientModule; import ru.gravit.launcher.modules.TestClientModule;
import ru.gravit.launchserver.binary.BuildContext; import ru.gravit.launchserver.binary.BuildContext;
import ru.gravit.launchserver.binary.JAConfigurator; import ru.gravit.launchserver.binary.JAConfigurator;
import ru.gravit.launchserver.binary.tasks.MainBuildTask; import ru.gravit.launchserver.binary.tasks.MainBuildTask;
import ru.gravit.launchserver.manangers.NodeTransformer;
public class BuildHookManager { public class BuildHookManager {
@FunctionalInterface
public interface ZipBuildHook {
void build(ZipOutputStream context);
}
@FunctionalInterface @FunctionalInterface
public interface BuildHook { public interface BuildHook {
@ -30,24 +24,14 @@ public interface Transformer {
} }
private boolean BUILDRUNTIME; private boolean BUILDRUNTIME;
private final Set<BuildHook> POST_HOOKS; private final Set<BuildHook> HOOKS;
private final Set<Runnable> POST_PROGUARDRUN_HOOKS;
private final Set<Transformer> POST_PROGUARD_HOOKS;
private final Set<BuildHook> PRE_HOOKS;
private final Set<ZipBuildHook> POST_PROGUARD_BUILDHOOKS;
private final Set<Transformer> CLASS_TRANSFORMER; private final Set<Transformer> CLASS_TRANSFORMER;
private final Set<String> CLASS_BLACKLIST; private final Set<String> CLASS_BLACKLIST;
private final Set<String> MODULE_CLASS; private final Set<String> MODULE_CLASS;
private final Map<String, byte[]> INCLUDE_CLASS; private final Map<String, byte[]> INCLUDE_CLASS;
private final NodeTransformer noder;
private final NodeTransformer proguardNoder;
public BuildHookManager() { public BuildHookManager() {
POST_HOOKS = new HashSet<>(4); HOOKS = new HashSet<>(4);
POST_PROGUARDRUN_HOOKS = new HashSet<>(4);
POST_PROGUARD_HOOKS = new HashSet<>(4);
PRE_HOOKS = new HashSet<>(4);
POST_PROGUARD_BUILDHOOKS = new HashSet<>(4);
CLASS_BLACKLIST = new HashSet<>(4); CLASS_BLACKLIST = new HashSet<>(4);
MODULE_CLASS = new HashSet<>(4); MODULE_CLASS = new HashSet<>(4);
INCLUDE_CLASS = new HashMap<>(4); INCLUDE_CLASS = new HashMap<>(4);
@ -58,34 +42,6 @@ public BuildHookManager() {
registerIgnoredClass("META-INF/LICENSE"); registerIgnoredClass("META-INF/LICENSE");
registerIgnoredClass("META-INF/NOTICE"); registerIgnoredClass("META-INF/NOTICE");
registerClientModuleClass(TestClientModule.class.getName()); registerClientModuleClass(TestClientModule.class.getName());
noder = new NodeTransformer();
registerClassTransformer(noder);
proguardNoder = new NodeTransformer();
registerProGuardHook(proguardNoder);
}
public NodeTransformer getProguardNoder() {
return proguardNoder;
}
public NodeTransformer getNoder() {
return noder;
}
public Set<ZipBuildHook> getProguardBuildHooks() {
return POST_PROGUARD_BUILDHOOKS;
}
public Set<Runnable> getPostProguardRunHooks() {
return POST_PROGUARDRUN_HOOKS;
}
public void addPostProguardRunHook(Runnable hook) {
POST_PROGUARDRUN_HOOKS.add(hook);
}
public void addPostProguardRunHook(ZipBuildHook hook) {
POST_PROGUARD_BUILDHOOKS.add(hook);
} }
public void autoRegisterIgnoredClass(String clazz) { public void autoRegisterIgnoredClass(String clazz) {
@ -102,12 +58,6 @@ public byte[] classTransform(byte[] clazz, String classname, MainBuildTask reade
return result; return result;
} }
public byte[] proGuardClassTransform(byte[] clazz, String classname, MainBuildTask reader) {
byte[] result = clazz;
for (Transformer transformer : POST_PROGUARD_HOOKS) result = transformer.transform(result, classname, reader);
return result;
}
public void registerIncludeClass(String classname, byte[] classdata) { public void registerIncludeClass(String classname, byte[] classdata) {
INCLUDE_CLASS.put(classname, classdata); INCLUDE_CLASS.put(classname, classdata);
} }
@ -123,12 +73,8 @@ public boolean isContainsBlacklist(String clazz) {
return false; return false;
} }
public void postHook(BuildContext context) { public void hook(BuildContext context) {
for (BuildHook hook : POST_HOOKS) hook.build(context); for (BuildHook hook : HOOKS) hook.build(context);
}
public void preHook(BuildContext context) {
for (BuildHook hook : PRE_HOOKS) hook.build(context);
} }
public void registerAllClientModuleClass(JAConfigurator cfg) { public void registerAllClientModuleClass(JAConfigurator cfg) {
@ -147,20 +93,8 @@ public void registerIgnoredClass(String clazz) {
CLASS_BLACKLIST.add(clazz); CLASS_BLACKLIST.add(clazz);
} }
public void registerPostHook(BuildHook hook) { public void registerHook(BuildHook hook) {
POST_HOOKS.add(hook); HOOKS.add(hook);
}
public void registerProGuardHook(Transformer hook) {
POST_PROGUARD_HOOKS.add(hook);
}
public boolean isNeedPostProguardHook() {
return POST_PROGUARD_HOOKS.size() > 1 || !POST_PROGUARDRUN_HOOKS.isEmpty() || !POST_PROGUARD_BUILDHOOKS.isEmpty() || !proguardNoder.getTransLst().isEmpty();
}
public void registerPreHook(BuildHook hook) {
PRE_HOOKS.add(hook);
} }
public void setBuildRuntime(boolean runtime) { public void setBuildRuntime(boolean runtime) {