mirror of
https://github.com/GravitLauncher/Launcher
synced 2024-11-15 11:39:11 +03:00
Binary logic changes.
This commit is contained in:
parent
669ca83bec
commit
494d83ae92
13 changed files with 194 additions and 30 deletions
|
@ -4,6 +4,8 @@
|
|||
import java.nio.file.Files;
|
||||
import java.nio.file.Path;
|
||||
import java.util.ArrayList;
|
||||
import java.util.Locale;
|
||||
import java.util.concurrent.atomic.AtomicLong;
|
||||
|
||||
import ru.gravit.launcher.Launcher;
|
||||
import ru.gravit.launchserver.LaunchServer;
|
||||
|
@ -12,13 +14,16 @@
|
|||
import ru.gravit.launchserver.binary.tasks.ProGuardBuildTask;
|
||||
import ru.gravit.launchserver.binary.tasks.StripLineNumbersTask;
|
||||
import ru.gravit.launchserver.binary.tasks.UnpackBuildTask;
|
||||
import ru.gravit.utils.helper.CommonHelper;
|
||||
import ru.gravit.utils.helper.IOHelper;
|
||||
import ru.gravit.utils.helper.LogHelper;
|
||||
|
||||
public final class JARLauncherBinary extends LauncherBinary {
|
||||
public ArrayList<LauncherBuildTask> tasks;
|
||||
public final AtomicLong count;
|
||||
public final Path runtimeDir;
|
||||
public final Path guardDir;
|
||||
public final Path buildDir;
|
||||
|
||||
public JARLauncherBinary(LaunchServer server) throws IOException {
|
||||
super(server);
|
||||
|
@ -27,14 +32,17 @@ public JARLauncherBinary(LaunchServer server) throws IOException {
|
|||
tasks.add(new MainBuildTask(server));
|
||||
if(server.config.enabledProGuard) tasks.add(new ProGuardBuildTask(server));
|
||||
if(server.config.stripLineNumbers) tasks.add(new StripLineNumbersTask(server));
|
||||
count = new AtomicLong(0);
|
||||
syncBinaryFile = server.dir.resolve(server.config.binaryName + ".jar");
|
||||
runtimeDir = server.dir.resolve(Launcher.RUNTIME_DIR);
|
||||
guardDir = server.dir.resolve(Launcher.GUARD_DIR);
|
||||
buildDir = server.dir.resolve("build");
|
||||
}
|
||||
|
||||
@Override
|
||||
public void build() throws IOException {
|
||||
LogHelper.info("Building launcher binary file");
|
||||
count.set(0);
|
||||
Path thisPath = null;
|
||||
boolean isNeedDelete = false;
|
||||
long time_start = System.currentTimeMillis();
|
||||
|
@ -55,4 +63,20 @@ public void build() throws IOException {
|
|||
IOHelper.move(thisPath, syncBinaryFile);
|
||||
LogHelper.info("Build successful from %d millis",time_end - time_start);
|
||||
}
|
||||
|
||||
public String nextName(String taskName) {
|
||||
return String.format("%s-%s-%d.jar", server.config.projectName, taskName, count.getAndIncrement());
|
||||
}
|
||||
|
||||
public Path nextPath(String taskName) {
|
||||
return buildDir.resolve(nextName(taskName));
|
||||
}
|
||||
|
||||
public Path nextPath(LauncherBuildTask task) {
|
||||
return nextPath(task.getName());
|
||||
}
|
||||
|
||||
public Path nextLowerPath(LauncherBuildTask task) {
|
||||
return nextPath(CommonHelper.low(task.getName()));
|
||||
}
|
||||
}
|
||||
|
|
|
@ -9,6 +9,7 @@
|
|||
import java.nio.file.Path;
|
||||
import java.security.SecureRandom;
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
|
||||
import ru.gravit.launchserver.LaunchServer;
|
||||
import ru.gravit.utils.helper.IOHelper;
|
||||
|
@ -30,8 +31,6 @@ private static String generateString(SecureRandom rand, int il) {
|
|||
public final Path config;
|
||||
public final Path mappings;
|
||||
public final Path words;
|
||||
public final Path outputJar;
|
||||
public final ArrayList<String> confStrs;
|
||||
public transient final LaunchServer srv;
|
||||
|
||||
public ProguardConf(LaunchServer srv) {
|
||||
|
@ -39,13 +38,11 @@ public ProguardConf(LaunchServer srv) {
|
|||
config = proguard.resolve("proguard.config");
|
||||
mappings = proguard.resolve("mappings.pro");
|
||||
words = proguard.resolve("random.pro");
|
||||
outputJar = srv.dir.resolve(srv.config.binaryName + "-obfPre.jar");
|
||||
confStrs = new ArrayList<>();
|
||||
this.srv = srv;
|
||||
}
|
||||
public void buildConfig(Path inputJar)
|
||||
public String[] buildConfig(Path inputJar, Path outputJar)
|
||||
{
|
||||
confStrs.clear();
|
||||
List<String> confStrs = new ArrayList<>();
|
||||
prepare(false);
|
||||
if (srv.config.genMappings) confStrs.add("-printmapping \'" + mappings.toFile().getName() + "\'");
|
||||
confStrs.add("-obfuscationdictionary \'" + words.toFile().getName() + "\'");
|
||||
|
@ -53,6 +50,7 @@ public void buildConfig(Path inputJar)
|
|||
confStrs.add("-outjar \'" + outputJar.toAbsolutePath() + "\'");
|
||||
confStrs.add("-classobfuscationdictionary \'" + words.toFile().getName() + "\'");
|
||||
confStrs.add(readConf());
|
||||
return confStrs.toArray(new String[0]);
|
||||
}
|
||||
|
||||
private void genConfig(boolean force) throws IOException {
|
||||
|
|
|
@ -31,8 +31,6 @@
|
|||
import ru.gravit.utils.helper.SecurityHelper;
|
||||
|
||||
public class MainBuildTask implements LauncherBuildTask {
|
||||
public final Path binaryFile;
|
||||
public Path cleanJar;
|
||||
private final LaunchServer server;
|
||||
public final ClassMetadataReader reader;
|
||||
private final class RuntimeDirVisitor extends SimpleFileVisitor<Path> {
|
||||
|
@ -105,7 +103,6 @@ private static ZipEntry newGuardEntry(String fileName) {
|
|||
|
||||
public MainBuildTask(LaunchServer srv) {
|
||||
server = srv;
|
||||
binaryFile = server.dir.resolve(server.config.binaryName + "-main.jar");
|
||||
reader = new ClassMetadataReader();
|
||||
}
|
||||
|
||||
|
@ -115,11 +112,11 @@ public String getName() {
|
|||
}
|
||||
|
||||
@Override
|
||||
public Path process(Path cleanJar) throws IOException {
|
||||
this.cleanJar = cleanJar;
|
||||
try (ZipOutputStream output = new ZipOutputStream(IOHelper.newOutput(binaryFile));
|
||||
public Path process(Path inputJar) throws IOException {
|
||||
Path outputJar = server.launcherBinary.nextPath("main");
|
||||
try (ZipOutputStream output = new ZipOutputStream(IOHelper.newOutput(outputJar));
|
||||
JAConfigurator jaConfigurator = new JAConfigurator(AutogenConfig.class.getName(), this)) {
|
||||
jaConfigurator.pool.insertClassPath(cleanJar.toFile().getAbsolutePath());
|
||||
jaConfigurator.pool.insertClassPath(inputJar.toFile().getAbsolutePath());
|
||||
BuildContext context = new BuildContext(output, jaConfigurator, this);
|
||||
server.buildHookManager.hook(context);
|
||||
jaConfigurator.setAddress(server.config.getAddress());
|
||||
|
@ -131,8 +128,8 @@ public Path process(Path cleanJar) throws IOException {
|
|||
jaConfigurator.setDownloadJava(server.config.isDownloadJava);
|
||||
jaConfigurator.setEnv(server.config.env);
|
||||
server.buildHookManager.registerAllClientModuleClass(jaConfigurator);
|
||||
reader.getCp().add(new JarFile(cleanJar.toFile()));
|
||||
try (ZipInputStream input = new ZipInputStream(IOHelper.newInput(cleanJar))) {
|
||||
reader.getCp().add(new JarFile(inputJar.toFile()));
|
||||
try (ZipInputStream input = new ZipInputStream(IOHelper.newInput(inputJar))) {
|
||||
ZipEntry e = input.getNextEntry();
|
||||
while (e != null) {
|
||||
String filename = e.getName();
|
||||
|
@ -196,7 +193,7 @@ public Path process(Path cleanJar) throws IOException {
|
|||
LogHelper.error(e);
|
||||
}
|
||||
reader.close();
|
||||
return binaryFile;
|
||||
return outputJar;
|
||||
}
|
||||
|
||||
@Override
|
||||
|
|
|
@ -8,6 +8,7 @@
|
|||
import proguard.ParseException;
|
||||
import proguard.ProGuard;
|
||||
import ru.gravit.launchserver.LaunchServer;
|
||||
import ru.gravit.utils.helper.LogHelper;
|
||||
|
||||
public class ProGuardBuildTask implements LauncherBuildTask {
|
||||
private final LaunchServer server;
|
||||
|
@ -23,18 +24,18 @@ public String getName() {
|
|||
|
||||
@Override
|
||||
public Path process(Path inputFile) throws IOException {
|
||||
Path outputJar = server.launcherBinary.nextLowerPath(this);
|
||||
Configuration proguard_cfg = new Configuration();
|
||||
server.proguardConf.buildConfig(inputFile);
|
||||
ConfigurationParser parser = new ConfigurationParser(server.proguardConf.confStrs.toArray(new String[0]),
|
||||
ConfigurationParser parser = new ConfigurationParser(server.proguardConf.buildConfig(inputFile, outputJar),
|
||||
server.proguardConf.proguard.toFile(), System.getProperties());
|
||||
try {
|
||||
parser.parse(proguard_cfg);
|
||||
ProGuard proGuard = new ProGuard(proguard_cfg);
|
||||
proGuard.execute();
|
||||
} catch (ParseException e1) {
|
||||
e1.printStackTrace();
|
||||
} catch (ParseException e) {
|
||||
LogHelper.error(e);
|
||||
}
|
||||
return server.proguardConf.outputJar;
|
||||
return outputJar;
|
||||
}
|
||||
|
||||
@Override
|
||||
|
|
|
@ -25,12 +25,12 @@ public StripLineNumbersTask(LaunchServer server) {
|
|||
|
||||
@Override
|
||||
public String getName() {
|
||||
return "Strip debug task";
|
||||
return "StripDebug";
|
||||
}
|
||||
|
||||
@Override
|
||||
public Path process(Path inputFile) throws IOException {
|
||||
Path out = server.dir.resolve(server.config.projectName + "-stripped.jar");
|
||||
Path out = server.launcherBinary.nextPath("stripped");
|
||||
try (ClassMetadataReader reader = new ClassMetadataReader()) {
|
||||
reader.getCp().add(new JarFile(inputFile.toFile()));
|
||||
try (ZipInputStream input = IOHelper.newZipInput(inputFile);
|
||||
|
|
|
@ -0,0 +1,78 @@
|
|||
package ru.gravit.launchserver.binary.tasks.api;
|
||||
|
||||
import java.io.ByteArrayOutputStream;
|
||||
import java.io.IOException;
|
||||
import java.nio.file.Path;
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
import java.util.jar.JarFile;
|
||||
import java.util.zip.ZipEntry;
|
||||
import java.util.zip.ZipInputStream;
|
||||
import java.util.zip.ZipOutputStream;
|
||||
|
||||
import ru.gravit.launchserver.LaunchServer;
|
||||
import ru.gravit.launchserver.binary.tasks.LauncherBuildTask;
|
||||
import ru.gravit.utils.helper.IOHelper;
|
||||
|
||||
public class AttachJarsTask implements LauncherBuildTask {
|
||||
|
||||
private final LaunchServer srv;
|
||||
private final List<Path> jars;
|
||||
private final List<String> exclusions;
|
||||
|
||||
public AttachJarsTask(LaunchServer srv) {
|
||||
this.srv = srv;
|
||||
jars = new ArrayList<>();
|
||||
exclusions = new ArrayList<>();
|
||||
}
|
||||
|
||||
@Override
|
||||
public String getName() {
|
||||
return "AttachJars";
|
||||
}
|
||||
|
||||
@Override
|
||||
public Path process(Path inputFile) throws IOException {
|
||||
Path outputFile = srv.launcherBinary.nextPath("attached");
|
||||
try (ZipInputStream input = IOHelper.newZipInput(inputFile);
|
||||
ZipOutputStream output = new ZipOutputStream(IOHelper.newOutput(outputFile))) {
|
||||
ZipEntry e = input.getNextEntry();
|
||||
while (e != null) {
|
||||
output.putNextEntry(IOHelper.newZipEntry(e));
|
||||
IOHelper.transfer(input, output);
|
||||
e = input.getNextEntry();
|
||||
}
|
||||
attach(output);
|
||||
}
|
||||
return outputFile;
|
||||
}
|
||||
|
||||
private void attach(ZipOutputStream output) throws IOException {
|
||||
for (Path p : jars) {
|
||||
try (ZipInputStream input = IOHelper.newZipInput(p)) {
|
||||
ZipEntry e = input.getNextEntry();
|
||||
while (e != null) {
|
||||
String filename = e.getName();
|
||||
if (exclusions.stream().noneMatch(exc -> filename.startsWith(exc))) {
|
||||
output.putNextEntry(IOHelper.newZipEntry(e));
|
||||
IOHelper.transfer(input, output);
|
||||
}
|
||||
e = input.getNextEntry();
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean allowDelete() {
|
||||
return true;
|
||||
}
|
||||
|
||||
public List<Path> getJars() {
|
||||
return jars;
|
||||
}
|
||||
|
||||
public List<String> getExclusions() {
|
||||
return exclusions;
|
||||
}
|
||||
}
|
|
@ -0,0 +1,62 @@
|
|||
package ru.gravit.launchserver.binary.tasks.api;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
import java.util.Optional;
|
||||
import java.util.function.Predicate;
|
||||
import java.util.stream.Collectors;
|
||||
|
||||
import ru.gravit.launchserver.binary.tasks.LauncherBuildTask;
|
||||
|
||||
public final class TaskUtil {
|
||||
public static void addCounted(List<LauncherBuildTask> tasks, int count, Predicate<LauncherBuildTask> pred, LauncherBuildTask taskAdd) {
|
||||
List<Integer> indexes = new ArrayList<>();
|
||||
tasks.stream().filter(pred).forEach(e -> {
|
||||
indexes.add(tasks.indexOf(e)+count);
|
||||
});
|
||||
indexes.forEach(e -> {
|
||||
tasks.add(e, taskAdd);
|
||||
});
|
||||
}
|
||||
|
||||
public static void replaceCounted(List<LauncherBuildTask> tasks, int count, Predicate<LauncherBuildTask> pred, LauncherBuildTask taskRep) {
|
||||
List<Integer> indexes = new ArrayList<>();
|
||||
tasks.stream().filter(pred).forEach(e -> {
|
||||
indexes.add(tasks.indexOf(e)+count);
|
||||
});
|
||||
indexes.forEach(e -> {
|
||||
tasks.set(e, taskRep);
|
||||
});
|
||||
}
|
||||
|
||||
public static void addPre(List<LauncherBuildTask> tasks, Predicate<LauncherBuildTask> pred, LauncherBuildTask taskAdd) {
|
||||
addCounted(tasks, -1, pred, taskAdd);
|
||||
}
|
||||
|
||||
public static void add(List<LauncherBuildTask> tasks, Predicate<LauncherBuildTask> pred, LauncherBuildTask taskAdd) {
|
||||
addCounted(tasks, 0, pred, taskAdd);
|
||||
}
|
||||
|
||||
public static void addAfter(List<LauncherBuildTask> tasks, Predicate<LauncherBuildTask> pred, LauncherBuildTask taskAdd) {
|
||||
addCounted(tasks, 1, pred, taskAdd);
|
||||
}
|
||||
|
||||
public static void replacePre(List<LauncherBuildTask> tasks, Predicate<LauncherBuildTask> pred, LauncherBuildTask taskRep) {
|
||||
replaceCounted(tasks, -1, pred, taskRep);
|
||||
}
|
||||
|
||||
public static void replace(List<LauncherBuildTask> tasks, Predicate<LauncherBuildTask> pred, LauncherBuildTask taskRep) {
|
||||
replaceCounted(tasks, 0, pred, taskRep);
|
||||
}
|
||||
|
||||
public static void replaceAfter(List<LauncherBuildTask> tasks, Predicate<LauncherBuildTask> pred, LauncherBuildTask taskRep) {
|
||||
replaceCounted(tasks, 1, pred, taskRep);
|
||||
}
|
||||
|
||||
public static <T extends LauncherBuildTask> List<T> getTaskByClass(List<LauncherBuildTask> tasks, Class<T> taskClass) {
|
||||
return tasks.stream().filter(taskClass::isInstance).map(taskClass::cast).collect(Collectors.toList());
|
||||
}
|
||||
|
||||
private TaskUtil() {
|
||||
}
|
||||
}
|
|
@ -13,7 +13,7 @@
|
|||
import java.nio.charset.Charset;
|
||||
import java.nio.charset.StandardCharsets;
|
||||
|
||||
public class HTTPRequest {
|
||||
public final class HTTPRequest {
|
||||
private static final int TIMEOUT = 10000;
|
||||
private static final JsonParser parser = new JsonParser();
|
||||
|
||||
|
@ -61,4 +61,8 @@ public static JsonElement jsonRequest(JsonElement request, URL url) throws IOExc
|
|||
JsonElement content = parser.parse(reader);
|
||||
return content;
|
||||
}
|
||||
|
||||
private HTTPRequest() {
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
|
@ -13,7 +13,7 @@
|
|||
import java.util.zip.ZipEntry;
|
||||
import java.util.zip.ZipInputStream;
|
||||
|
||||
public class HttpDownloader extends Observable {
|
||||
public final class HttpDownloader extends Observable {
|
||||
public static final int BUFER_SIZE = 8192;
|
||||
public static final int INTERVAL = 300;
|
||||
public AtomicInteger writed = new AtomicInteger(0);
|
||||
|
|
|
@ -2,7 +2,7 @@
|
|||
|
||||
import ru.gravit.utils.helper.LogHelper;
|
||||
|
||||
public class NativeJVMHalt {
|
||||
public final class NativeJVMHalt {
|
||||
public NativeJVMHalt(int haltCode) {
|
||||
this.haltCode = haltCode;
|
||||
LogHelper.error("JVM exit code %d", haltCode);
|
||||
|
|
|
@ -4,7 +4,7 @@
|
|||
|
||||
import java.util.Objects;
|
||||
|
||||
public class Version {
|
||||
public final class Version {
|
||||
@LauncherAPI
|
||||
public final int major;
|
||||
@LauncherAPI
|
||||
|
|
|
@ -3,7 +3,7 @@
|
|||
import java.util.Locale;
|
||||
import java.util.Map;
|
||||
|
||||
public class EnvHelper {
|
||||
public final class EnvHelper {
|
||||
public static final String[] toTest = {"_JAVA_OPTIONS", "_JAVA_OPTS", "JAVA_OPTS", "JAVA_OPTIONS"};
|
||||
|
||||
public static void addEnv(ProcessBuilder builder) {
|
||||
|
|
|
@ -10,7 +10,7 @@
|
|||
import java.util.zip.ZipEntry;
|
||||
import java.util.zip.ZipInputStream;
|
||||
|
||||
public class UnpackHelper {
|
||||
public final class UnpackHelper {
|
||||
public static boolean unpack(URL resource, Path target) throws IOException {
|
||||
if (IOHelper.isFile(target)) {
|
||||
if (matches(target, resource)) return false;
|
||||
|
|
Loading…
Reference in a new issue