|
@@ -0,0 +1,104 @@
|
|
|
|
+package xyz.tbvns;
|
|
|
|
+
|
|
|
|
+import org.apache.commons.codec.binary.Base64;
|
|
|
|
+import org.apache.commons.io.FileUtils;
|
|
|
|
+import xyz.tbvns.Objects.Level;
|
|
|
|
+
|
|
|
|
+import java.io.ByteArrayInputStream;
|
|
|
|
+import java.io.ByteArrayOutputStream;
|
|
|
|
+import java.io.File;
|
|
|
|
+import java.io.IOException;
|
|
|
|
+import java.net.URISyntaxException;
|
|
|
|
+import java.nio.charset.StandardCharsets;
|
|
|
|
+import java.util.zip.GZIPInputStream;
|
|
|
|
+import java.util.zip.GZIPOutputStream;
|
|
|
|
+
|
|
|
|
+public class Decoder {
|
|
|
|
+
|
|
|
|
+ public static final String defaultLevelSettings = "kA13,0,kA15,0,kA16,0,kA14,,kA6,0,kA7,0,kA25,0,kA17,0,kA18,0,kS39,0,kA2,0,kA3,0,kA8,0,kA4,0,kA9,0,kA10,0,kA22,1,kA23,0,kA24,0,kA27,1,kA40,1,kA41,1,kA42,1,kA28,0,kA29,0,kA31,1,kA32,1,kA36,0,kA43,0,kA44,0,kA45,1,kA33,1,kA34,1,kA35,0,kA37,1,kA38,1,kA39,1,kA19,0,kA26,0,kA20,0,kA21,0,kA11,0;";
|
|
|
|
+
|
|
|
|
+ public static String decodeLevel(String levelStr) throws IOException {
|
|
|
|
+ byte[] levelData = Base64.decodeBase64(levelStr.getBytes(StandardCharsets.UTF_8));
|
|
|
|
+ ByteArrayInputStream inputStream = new ByteArrayInputStream(levelData);
|
|
|
|
+ GZIPInputStream gzipInputStream = new GZIPInputStream(inputStream);
|
|
|
|
+
|
|
|
|
+ return new String(gzipInputStream.readAllBytes());
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+ public static String encodeLevel(String levelStr) throws IOException {
|
|
|
|
+ ByteArrayOutputStream outputStream = new ByteArrayOutputStream();
|
|
|
|
+ GZIPOutputStream gzipOutputStream = new GZIPOutputStream(outputStream);
|
|
|
|
+ gzipOutputStream.write(levelStr.getBytes(StandardCharsets.UTF_8));
|
|
|
|
+ gzipOutputStream.flush();
|
|
|
|
+ gzipOutputStream.finish();
|
|
|
|
+ gzipOutputStream.close();
|
|
|
|
+
|
|
|
|
+ byte[] levelData = java.util.Base64.getUrlEncoder().encode(outputStream.toByteArray());
|
|
|
|
+
|
|
|
|
+ return new String(levelData);
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+ public static String encodeLevel(String levelStr, UIProgressBar progressBar, float factor) throws IOException {
|
|
|
|
+ ByteArrayOutputStream outputStream = new ByteArrayOutputStream();
|
|
|
|
+ GZIPOutputStream gzipOutputStream = new GZIPOutputStream(outputStream);
|
|
|
|
+ gzipOutputStream.write(levelStr.getBytes(StandardCharsets.UTF_8));
|
|
|
|
+ gzipOutputStream.flush();
|
|
|
|
+ gzipOutputStream.finish();
|
|
|
|
+ gzipOutputStream.close();
|
|
|
|
+ progressBar.updateBar(progressBar.getProgressBar().getProgress() + 0.5 * factor);
|
|
|
|
+
|
|
|
|
+ byte[] levelData = java.util.Base64.getUrlEncoder().encode(outputStream.toByteArray());
|
|
|
|
+ progressBar.updateBar(progressBar.getProgressBar().getProgress() + 0.5 * factor);
|
|
|
|
+
|
|
|
|
+ return new String(levelData);
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+ public static byte[] decodeFile(File file, int key) throws IOException {
|
|
|
|
+
|
|
|
|
+ byte[] levelData = xor(FileUtils.readFileToByteArray(file), key);
|
|
|
|
+ levelData = Base64.decodeBase64(levelData);
|
|
|
|
+
|
|
|
|
+ ByteArrayInputStream inputStream = new ByteArrayInputStream(levelData);
|
|
|
|
+ GZIPInputStream gzipInputStream = new GZIPInputStream(inputStream);
|
|
|
|
+
|
|
|
|
+ return gzipInputStream.readAllBytes();
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+ public static byte[] encodeFile(File file, int key) throws IOException {
|
|
|
|
+ ByteArrayOutputStream outputStream = new ByteArrayOutputStream();
|
|
|
|
+ GZIPOutputStream gzipOutputStream = new GZIPOutputStream(outputStream);
|
|
|
|
+ gzipOutputStream.write(FileUtils.readFileToByteArray(file));
|
|
|
|
+ gzipOutputStream.flush();
|
|
|
|
+ gzipOutputStream.finish();
|
|
|
|
+ gzipOutputStream.close();
|
|
|
|
+
|
|
|
|
+ byte[] fileData = java.util.Base64.getUrlEncoder().encode(outputStream.toByteArray());
|
|
|
|
+ fileData = xor(fileData, key);
|
|
|
|
+ FileUtils.writeByteArrayToFile(file, fileData, false);
|
|
|
|
+
|
|
|
|
+ return fileData;
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+ public static void exportGMD(String name, String data, String path) throws IOException, URISyntaxException {
|
|
|
|
+ String template = new String(Decoder.class.getResourceAsStream("/template.gmd").readAllBytes());
|
|
|
|
+ template = template.replace("%name%", name).replace("%levelData%", encodeLevel(data));
|
|
|
|
+ FileUtils.writeStringToFile(new File(path), template, StandardCharsets.UTF_8);
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+ public static void exportGMD(Level level, String path, UIProgressBar progressBar, float factor) throws IOException, URISyntaxException {
|
|
|
|
+ String template = new String(Decoder.class.getResourceAsStream("/template.gmd").readAllBytes());
|
|
|
|
+ progressBar.updateBar(progressBar.getProgressBar().getProgress() + 0.2 * factor);
|
|
|
|
+ template = template.replace("%name%", level.getName()).replace("%levelData%", encodeLevel(level.getEncodedLevelString(), progressBar, factor*0.8f));
|
|
|
|
+ FileUtils.writeStringToFile(new File(path), template, StandardCharsets.UTF_8);
|
|
|
|
+ progressBar.updateBar(progressBar.getProgressBar().getProgress() + 0.2 * factor);
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+ public static byte[] xor(byte[] in, int key) {
|
|
|
|
+ byte[] out = new byte[in.length];
|
|
|
|
+ for (int i = 0; i < in.length; i++) {
|
|
|
|
+ out[i] = (byte)(in[i] ^ key);
|
|
|
|
+ }
|
|
|
|
+ return out;
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+}
|