From 074dd68a66b5362720c05f1a8478f16520aa9e4d Mon Sep 17 00:00:00 2001 From: "SNDST00M: M.U.N.I.N" <82655227+SNDST00M@users.noreply.github.com> Date: Sat, 9 Oct 2021 15:03:46 +0100 Subject: [PATCH] Add JAR extraction logic, basic dir cleaning https://www.youtube.com/watch?v=AXwGVXD7qEQ --- .../java/io/github/jlyne/LiveAtlasMain.java | 67 +++++++++++++++---- package.json | 2 +- 2 files changed, 56 insertions(+), 13 deletions(-) diff --git a/java/src/main/java/io/github/jlyne/LiveAtlasMain.java b/java/src/main/java/io/github/jlyne/LiveAtlasMain.java index d61f761..0f328a2 100644 --- a/java/src/main/java/io/github/jlyne/LiveAtlasMain.java +++ b/java/src/main/java/io/github/jlyne/LiveAtlasMain.java @@ -6,11 +6,13 @@ import java.io.FileOutputStream; import java.io.IOException; import java.io.InputStream; import java.io.OutputStream; -import java.nio.file.Path; import java.nio.file.Paths; +import java.util.Enumeration; import java.util.HashMap; import java.util.logging.Level; import java.util.logging.Logger; +import java.util.zip.ZipEntry; +import java.util.zip.ZipFile; import org.bukkit.configuration.file.FileConfiguration; import org.bukkit.event.EventHandler; @@ -78,10 +80,11 @@ public class LiveAtlasMain extends JavaPlugin { String version = this.getDescription().getVersion(); try { - Path dynmapWebFolderPath = Paths.get(dynmap.getDataFolder().toURI()).resolve("web"); - File sourceDirectory = Paths.get(this.getDataFolder().toURI()).toFile(); - File destinationDirectory = dynmapWebFolderPath.toFile(); - copyDirectory(sourceDirectory, destinationDirectory); + File liveAtlasFolderPath = this.getDataFolder(); + deleteDir(liveAtlasFolderPath); + copyJarFile(this.getFile().getAbsoluteFile().toString()); + File dynmapWebFolderPath = Paths.get(dynmap.getDataFolder().toURI()).resolve("web").toFile(); + copyDirectory(liveAtlasFolderPath, dynmapWebFolderPath); } catch (IOException e) { throw new RuntimeException("LiveAtlas v" + version + " installation failed", e); } @@ -92,6 +95,53 @@ public class LiveAtlasMain extends JavaPlugin { public void onDisable() { } + public void deleteDir(File file) { + File[] list = file.listFiles(); + if (list != null) { + for (File child : list) { + deleteDir(child); + } + } + file.delete(); + } + + public void copyJarFile(String jarfile) throws IOException { /* Open JAR as ZIP */ + ZipFile zf = null; + FileOutputStream fos = null; + InputStream ins = null; + byte[] buf = new byte[2048]; + String n = null; + + File f; + zf = new ZipFile(jarfile); + Enumeration e = zf.entries(); + while (e.hasMoreElements()) { + ZipEntry ze = e.nextElement(); + n = ze.getName(); + f = new File(this.getDataFolder(), n); + if (n.startsWith("io/")) continue; + if (n.startsWith("META-INF/")) continue; + if (n == "plugin.yml") continue; + if (ze.isDirectory()) { + f.mkdirs(); + } + else { + f.getParentFile().mkdirs(); + fos = new FileOutputStream(f); + ins = zf.getInputStream(ze); + int len; + while ((len = ins.read(buf)) >= 0) { + fos.write(buf, 0, len); + } + ins.close(); + ins = null; + fos.close(); + fos = null; + } + } + zf.close(); + } + private static void copyDirectory(File sourceDirectory, File destinationDirectory) throws IOException { if (!destinationDirectory.exists()) { destinationDirectory.mkdir(); @@ -102,9 +152,6 @@ public class LiveAtlasMain extends JavaPlugin { } public static void copyDirectoryCompatibityMode(File sourceDirectory, File destinationDirectory) throws IOException { - if (sourceDirectory.getName() == "io" || sourceDirectory.getName() == "META-INF") { - return; - } info(sourceDirectory.getName()); if (sourceDirectory.isDirectory()) { copyDirectory(sourceDirectory, destinationDirectory); @@ -114,9 +161,6 @@ public class LiveAtlasMain extends JavaPlugin { } private static void copyFile(File sourceFile, File destinationFile) throws IOException { - if (sourceFile.getName() == "plugin.yml") { - return; - } try (InputStream in = new FileInputStream(sourceFile); OutputStream out = new FileOutputStream(destinationFile)) { byte[] buf = new byte[1024]; @@ -126,5 +170,4 @@ public class LiveAtlasMain extends JavaPlugin { } } } - } diff --git a/package.json b/package.json index f2c2e68..2da8453 100644 --- a/package.json +++ b/package.json @@ -7,7 +7,7 @@ "preview": "vite preview --port 8082", "test": "jest", "clean": "rimraf dist java/target/resources", - "copy": "cpy plugin.yml dist java/target/resources --parents", + "copy": "cpy plugin.yml dist java/target/resources --parents && rimraf java/target/resources/assets/*.svg", "build": "npm run clean && vue-tsc --noEmit && vite build --out-dir dist && npm-run-all copy mvn", "lint": "eslint --ext .ts,.vue src", "lint:fix": "eslint -ext .ts,.vue src --fix",