Launcher/LaunchServer/src/main/java/pro/gravit/launchserver/binary/ProguardConf.java

101 lines
4.1 KiB
Java
Raw Normal View History

package pro.gravit.launchserver.binary;
2018-09-17 10:07:32 +03:00
2019-10-19 19:46:04 +03:00
import pro.gravit.launchserver.LaunchServer;
2020-02-21 07:17:16 +03:00
import pro.gravit.utils.helper.*;
2019-10-19 19:46:04 +03:00
2019-04-03 16:27:40 +03:00
import java.io.IOException;
import java.io.OutputStreamWriter;
import java.io.PrintWriter;
2018-09-17 10:07:32 +03:00
import java.nio.file.Files;
import java.nio.file.Path;
import java.security.SecureRandom;
2018-09-21 14:38:16 +03:00
import java.util.ArrayList;
import java.util.Collections;
2019-01-08 16:36:05 +03:00
import java.util.List;
2018-09-17 10:07:32 +03:00
public class ProguardConf {
2020-04-05 10:27:04 +03:00
public static final String[] JAVA9_OPTS = new String[]{
"-libraryjars '<java.home>/jmods/'"
};
2020-04-05 10:27:04 +03:00
public static final String[] JAVA8_OPTS = new String[]{
"-libraryjars '<java.home>/lib/rt.jar'",
"-libraryjars '<java.home>/lib/jce.jar'",
"-libraryjars '<java.home>/lib/ext/nashorn.jar'",
"-libraryjars '<java.home>/lib/ext/jfxrt.jar'"
};
2020-04-05 10:27:04 +03:00
private static final char[] chars = "1aAbBcC2dDeEfF3gGhHiI4jJkKl5mMnNoO6pPqQrR7sStT8uUvV9wWxX0yYzZ".toCharArray();
2018-09-22 17:33:00 +03:00
public final Path proguard;
public final Path config;
public final Path mappings;
public final Path words;
public transient final LaunchServer srv;
2018-09-22 17:33:00 +03:00
public ProguardConf(LaunchServer srv) {
2018-11-26 11:00:58 +03:00
proguard = srv.dir.resolve("proguard");
2018-09-22 17:33:00 +03:00
config = proguard.resolve("proguard.config");
mappings = proguard.resolve("mappings.pro");
words = proguard.resolve("random.pro");
this.srv = srv;
}
2019-01-15 06:35:39 +03:00
2020-04-05 10:27:04 +03:00
private static String generateString(SecureRandom rand, String lowString, String upString, int il) {
StringBuilder sb = new StringBuilder(Math.max(il, lowString.length()));
for (int i = 0; i < lowString.length(); ++i) {
sb.append(rand.nextBoolean() ? lowString.charAt(i) : upString.charAt(i));
}
int toI = il - lowString.length();
for (int i = 0; i < toI; i++) sb.append(chars[rand.nextInt(chars.length)]);
return sb.toString();
}
2019-01-15 06:35:39 +03:00
public String[] buildConfig(Path inputJar, Path outputJar) {
List<String> confStrs = new ArrayList<>();
2018-09-22 17:33:00 +03:00
prepare(false);
2019-10-19 19:46:04 +03:00
if (srv.config.launcher.proguardGenMappings)
2020-09-25 18:44:39 +03:00
confStrs.add("-printmapping '" + mappings.toFile().getName() + "'");
confStrs.add("-obfuscationdictionary '" + words.toFile().getName() + "'");
confStrs.add("-injar '" + inputJar.toAbsolutePath() + "'");
confStrs.add("-outjar '" + outputJar.toAbsolutePath() + "'");
2020-04-05 10:27:04 +03:00
Collections.addAll(confStrs, JVMHelper.JVM_VERSION >= 9 ? JAVA9_OPTS : JAVA8_OPTS);
2019-01-09 12:08:43 +03:00
srv.launcherBinary.coreLibs.stream()
2020-09-25 18:44:39 +03:00
.map(e -> "-libraryjars '" + e.toAbsolutePath().toString() + "'")
2019-01-15 06:35:39 +03:00
.forEach(confStrs::add);
2020-04-05 10:27:04 +03:00
srv.launcherBinary.addonLibs.stream()
2020-09-25 18:44:39 +03:00
.map(e -> "-libraryjars '" + e.toAbsolutePath().toString() + "'")
2019-04-03 16:27:40 +03:00
.forEach(confStrs::add);
2020-09-25 18:44:39 +03:00
confStrs.add("-classobfuscationdictionary '" + words.toFile().getName() + "'");
2019-11-28 20:01:04 +03:00
confStrs.add("@".concat(config.toFile().getName()));
2019-01-08 16:36:05 +03:00
return confStrs.toArray(new String[0]);
2018-09-22 17:33:00 +03:00
}
2018-09-17 10:07:32 +03:00
2018-09-22 17:33:00 +03:00
private void genConfig(boolean force) throws IOException {
if (IOHelper.exists(config) && !force) return;
Files.deleteIfExists(config);
UnpackHelper.unpack(IOHelper.getResourceURL("pro/gravit/launchserver/defaults/proguard.cfg"), config);
2018-09-22 17:33:00 +03:00
}
public void genWords(boolean force) throws IOException {
if (IOHelper.exists(words) && !force) return;
Files.deleteIfExists(words);
SecureRandom rand = SecurityHelper.newRandom();
rand.setSeed(SecureRandom.getSeed(32));
try (PrintWriter out = new PrintWriter(new OutputStreamWriter(IOHelper.newOutput(words), IOHelper.UNICODE_CHARSET))) {
String projectName = srv.config.projectName.replaceAll("\\W", "");
String lowName = projectName.toLowerCase();
String upName = projectName.toUpperCase();
for (int i = 0; i < Short.MAX_VALUE; i++) out.println(generateString(rand, lowName, upName, 14));
2018-09-22 17:33:00 +03:00
}
}
public void prepare(boolean force) {
try {
IOHelper.createParentDirs(config);
genWords(force);
genConfig(force);
} catch (IOException e) {
LogHelper.error(e);
}
}
2018-09-17 10:07:32 +03:00
}