2018-09-17 10:07:32 +03:00
|
|
|
package ru.gravit.launchserver.binary;
|
|
|
|
|
2018-12-26 15:33:49 +03:00
|
|
|
import static ru.gravit.utils.helper.IOHelper.newZipEntry;
|
2018-09-17 10:07:32 +03:00
|
|
|
|
|
|
|
import java.io.ByteArrayInputStream;
|
|
|
|
import java.io.ByteArrayOutputStream;
|
|
|
|
import java.io.IOException;
|
2018-12-06 05:29:34 +03:00
|
|
|
import java.nio.file.FileVisitResult;
|
2019-01-05 18:28:25 +03:00
|
|
|
import java.nio.file.Files;
|
2018-12-06 05:29:34 +03:00
|
|
|
import java.nio.file.Path;
|
|
|
|
import java.nio.file.SimpleFileVisitor;
|
2018-09-17 10:07:32 +03:00
|
|
|
import java.nio.file.attribute.BasicFileAttributes;
|
2018-12-02 15:21:27 +03:00
|
|
|
import java.util.ArrayList;
|
2018-09-17 10:07:32 +03:00
|
|
|
import java.util.HashMap;
|
2018-12-02 15:21:27 +03:00
|
|
|
import java.util.List;
|
2018-09-17 10:07:32 +03:00
|
|
|
import java.util.Map;
|
|
|
|
import java.util.Map.Entry;
|
2018-12-06 05:29:34 +03:00
|
|
|
import java.util.jar.JarFile;
|
2018-09-17 10:07:32 +03:00
|
|
|
import java.util.zip.ZipEntry;
|
|
|
|
import java.util.zip.ZipException;
|
|
|
|
import java.util.zip.ZipInputStream;
|
|
|
|
import java.util.zip.ZipOutputStream;
|
|
|
|
|
2018-12-26 15:33:49 +03:00
|
|
|
import javassist.CannotCompileException;
|
|
|
|
import javassist.NotFoundException;
|
|
|
|
import proguard.Configuration;
|
|
|
|
import proguard.ConfigurationParser;
|
|
|
|
import proguard.ParseException;
|
|
|
|
import proguard.ProGuard;
|
|
|
|
import ru.gravit.launcher.AutogenConfig;
|
|
|
|
import ru.gravit.launcher.Launcher;
|
|
|
|
import ru.gravit.launcher.LauncherConfig;
|
|
|
|
import ru.gravit.launcher.serialize.HOutput;
|
|
|
|
import ru.gravit.launchserver.LaunchServer;
|
|
|
|
import ru.gravit.launchserver.asm.ClassMetadataReader;
|
2019-01-05 18:15:19 +03:00
|
|
|
import ru.gravit.launchserver.binary.tasks.LauncherBuildTask;
|
|
|
|
import ru.gravit.launchserver.binary.tasks.MainBuildTask;
|
|
|
|
import ru.gravit.launchserver.binary.tasks.ProGuardBuildTask;
|
|
|
|
import ru.gravit.launchserver.binary.tasks.UnpackBuildTask;
|
2018-12-31 10:53:07 +03:00
|
|
|
import ru.gravit.launchserver.manangers.hook.BuildHookManager.ZipBuildHook;
|
2018-12-26 15:33:49 +03:00
|
|
|
import ru.gravit.utils.helper.CommonHelper;
|
|
|
|
import ru.gravit.utils.helper.IOHelper;
|
|
|
|
import ru.gravit.utils.helper.LogHelper;
|
|
|
|
import ru.gravit.utils.helper.SecurityHelper;
|
|
|
|
import ru.gravit.utils.helper.SecurityHelper.DigestAlgorithm;
|
|
|
|
import ru.gravit.utils.helper.UnpackHelper;
|
2018-09-17 10:07:32 +03:00
|
|
|
|
|
|
|
public final class JARLauncherBinary extends LauncherBinary {
|
2019-01-05 18:15:19 +03:00
|
|
|
//public ClassMetadataReader reader;
|
|
|
|
public ArrayList<LauncherBuildTask> tasks;
|
2018-12-20 18:45:01 +03:00
|
|
|
public JARLauncherBinary(LaunchServer server) throws IOException {
|
2019-01-05 18:15:19 +03:00
|
|
|
super(server);
|
|
|
|
tasks = new ArrayList<>();
|
|
|
|
tasks.add(new UnpackBuildTask());
|
|
|
|
tasks.add(new MainBuildTask());
|
|
|
|
tasks.add(new ProGuardBuildTask());
|
2019-01-05 18:28:25 +03:00
|
|
|
syncBinaryFile = server.dir.resolve(server.config.binaryName + ".jar");
|
2019-01-05 18:15:19 +03:00
|
|
|
/*runtimeDir = server.dir.resolve(Launcher.RUNTIME_DIR);
|
2018-12-20 18:45:01 +03:00
|
|
|
guardDir = server.dir.resolve(Launcher.GUARD_DIR);
|
|
|
|
initScriptFile = runtimeDir.resolve(Launcher.INIT_SCRIPT_FILE);
|
2018-12-26 16:17:47 +03:00
|
|
|
obfJar = server.dir.resolve(server.config.binaryName + "-obfPre.jar");
|
2018-12-20 18:45:01 +03:00
|
|
|
obfOutJar = server.config.buildPostTransform.enabled ? server.dir.resolve(server.config.binaryName + "-obf.jar")
|
|
|
|
: syncBinaryFile;
|
|
|
|
cleanJar = server.dir.resolve(server.config.binaryName + "-clean.jar");
|
|
|
|
reader = new ClassMetadataReader();
|
|
|
|
UnpackHelper.unpack(IOHelper.getResourceURL("Launcher.jar"), cleanJar);
|
2019-01-05 18:15:19 +03:00
|
|
|
reader.getCp().add(new JarFile(cleanJar.toFile()));*/
|
2018-12-20 18:45:01 +03:00
|
|
|
}
|
2018-12-06 05:29:34 +03:00
|
|
|
|
2018-12-20 18:45:01 +03:00
|
|
|
@Override
|
|
|
|
public void build() throws IOException {
|
|
|
|
// Build launcher binary
|
|
|
|
LogHelper.info("Building launcher binary file");
|
2019-01-05 18:15:19 +03:00
|
|
|
Path thisPath = null;
|
2019-01-05 18:28:25 +03:00
|
|
|
boolean isNeedDelete = false;
|
2019-01-05 18:15:19 +03:00
|
|
|
for(LauncherBuildTask task : tasks)
|
|
|
|
{
|
|
|
|
LogHelper.subInfo("Task %s",task.getName());
|
2019-01-05 18:28:25 +03:00
|
|
|
Path oldPath = thisPath;
|
|
|
|
thisPath = task.process(oldPath);
|
|
|
|
if(isNeedDelete) Files.delete(oldPath);
|
|
|
|
isNeedDelete = task.allowDelete();
|
2019-01-05 18:15:19 +03:00
|
|
|
LogHelper.subInfo("Task %s processed",task.getName());
|
|
|
|
}
|
2019-01-05 18:28:25 +03:00
|
|
|
IOHelper.move(thisPath, syncBinaryFile);
|
2019-01-05 18:15:19 +03:00
|
|
|
LogHelper.info("Build successful");
|
2018-12-06 05:29:34 +03:00
|
|
|
|
2018-12-20 18:45:01 +03:00
|
|
|
// ProGuard
|
2019-01-05 18:15:19 +03:00
|
|
|
|
|
|
|
/*for (Runnable r : server.buildHookManager.getPostProguardRunHooks())
|
2018-12-20 18:45:01 +03:00
|
|
|
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();
|
2018-12-26 16:17:47 +03:00
|
|
|
output.putNextEntry(IOHelper.newZipEntry(e));
|
2018-12-20 18:45:01 +03:00
|
|
|
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();
|
|
|
|
}
|
2019-01-05 18:15:19 +03:00
|
|
|
//bytes = server.buildHookManager.proGuardClassTransform(bytes, classname, this);
|
2018-12-20 18:45:01 +03:00
|
|
|
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);
|
|
|
|
}
|
|
|
|
if (server.config.buildPostTransform.enabled)
|
2019-01-05 18:15:19 +03:00
|
|
|
transformedBuild();*/
|
2018-12-20 18:45:01 +03:00
|
|
|
}
|
2018-12-06 05:29:34 +03:00
|
|
|
|
2018-12-20 18:45:01 +03:00
|
|
|
private void transformedBuild() throws IOException {
|
2019-01-05 18:15:19 +03:00
|
|
|
/*List<String> cmd = new ArrayList<>(1);
|
2018-12-20 18:45:01 +03:00
|
|
|
for (String v : server.config.buildPostTransform.script)
|
|
|
|
cmd.add(CommonHelper.replace(v, "launcher-output", IOHelper.toAbsPathString(syncBinaryFile), "launcher-obf",
|
|
|
|
IOHelper.toAbsPathString(obfJar), "launcher-nonObf", IOHelper.toAbsPathString(binaryFile)));
|
|
|
|
ProcessBuilder builder = new ProcessBuilder();
|
|
|
|
builder.directory(IOHelper.toAbsPath(server.dir).toFile());
|
|
|
|
builder.inheritIO();
|
|
|
|
builder.command(cmd);
|
|
|
|
Process proc = builder.start();
|
|
|
|
try {
|
|
|
|
LogHelper.debug("Transformer process return code: " + proc.waitFor());
|
|
|
|
} catch (InterruptedException e) {
|
|
|
|
LogHelper.error(e);
|
2019-01-05 18:15:19 +03:00
|
|
|
}*/
|
2018-12-20 18:45:01 +03:00
|
|
|
}
|
2018-09-17 10:07:32 +03:00
|
|
|
}
|