Add JAR extraction logic, basic dir cleaning

https://www.youtube.com/watch?v=AXwGVXD7qEQ
This commit is contained in:
SNDST00M: M.U.N.I.N 2021-10-09 15:03:46 +01:00
parent 35ac2244c3
commit 074dd68a66
No known key found for this signature in database
GPG Key ID: 0C7CE6F01FC333C5
2 changed files with 56 additions and 13 deletions

View File

@ -6,11 +6,13 @@ import java.io.FileOutputStream;
import java.io.IOException; import java.io.IOException;
import java.io.InputStream; import java.io.InputStream;
import java.io.OutputStream; import java.io.OutputStream;
import java.nio.file.Path;
import java.nio.file.Paths; import java.nio.file.Paths;
import java.util.Enumeration;
import java.util.HashMap; import java.util.HashMap;
import java.util.logging.Level; import java.util.logging.Level;
import java.util.logging.Logger; import java.util.logging.Logger;
import java.util.zip.ZipEntry;
import java.util.zip.ZipFile;
import org.bukkit.configuration.file.FileConfiguration; import org.bukkit.configuration.file.FileConfiguration;
import org.bukkit.event.EventHandler; import org.bukkit.event.EventHandler;
@ -78,10 +80,11 @@ public class LiveAtlasMain extends JavaPlugin {
String version = this.getDescription().getVersion(); String version = this.getDescription().getVersion();
try { try {
Path dynmapWebFolderPath = Paths.get(dynmap.getDataFolder().toURI()).resolve("web"); File liveAtlasFolderPath = this.getDataFolder();
File sourceDirectory = Paths.get(this.getDataFolder().toURI()).toFile(); deleteDir(liveAtlasFolderPath);
File destinationDirectory = dynmapWebFolderPath.toFile(); copyJarFile(this.getFile().getAbsoluteFile().toString());
copyDirectory(sourceDirectory, destinationDirectory); File dynmapWebFolderPath = Paths.get(dynmap.getDataFolder().toURI()).resolve("web").toFile();
copyDirectory(liveAtlasFolderPath, dynmapWebFolderPath);
} catch (IOException e) { } catch (IOException e) {
throw new RuntimeException("LiveAtlas v" + version + " installation failed", e); throw new RuntimeException("LiveAtlas v" + version + " installation failed", e);
} }
@ -92,6 +95,53 @@ public class LiveAtlasMain extends JavaPlugin {
public void onDisable() { 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<? extends ZipEntry> 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 { private static void copyDirectory(File sourceDirectory, File destinationDirectory) throws IOException {
if (!destinationDirectory.exists()) { if (!destinationDirectory.exists()) {
destinationDirectory.mkdir(); destinationDirectory.mkdir();
@ -102,9 +152,6 @@ public class LiveAtlasMain extends JavaPlugin {
} }
public static void copyDirectoryCompatibityMode(File sourceDirectory, File destinationDirectory) throws IOException { public static void copyDirectoryCompatibityMode(File sourceDirectory, File destinationDirectory) throws IOException {
if (sourceDirectory.getName() == "io" || sourceDirectory.getName() == "META-INF") {
return;
}
info(sourceDirectory.getName()); info(sourceDirectory.getName());
if (sourceDirectory.isDirectory()) { if (sourceDirectory.isDirectory()) {
copyDirectory(sourceDirectory, destinationDirectory); copyDirectory(sourceDirectory, destinationDirectory);
@ -114,9 +161,6 @@ public class LiveAtlasMain extends JavaPlugin {
} }
private static void copyFile(File sourceFile, File destinationFile) throws IOException { private static void copyFile(File sourceFile, File destinationFile) throws IOException {
if (sourceFile.getName() == "plugin.yml") {
return;
}
try (InputStream in = new FileInputStream(sourceFile); try (InputStream in = new FileInputStream(sourceFile);
OutputStream out = new FileOutputStream(destinationFile)) { OutputStream out = new FileOutputStream(destinationFile)) {
byte[] buf = new byte[1024]; byte[] buf = new byte[1024];
@ -126,5 +170,4 @@ public class LiveAtlasMain extends JavaPlugin {
} }
} }
} }
} }

View File

@ -7,7 +7,7 @@
"preview": "vite preview --port 8082", "preview": "vite preview --port 8082",
"test": "jest", "test": "jest",
"clean": "rimraf dist java/target/resources", "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", "build": "npm run clean && vue-tsc --noEmit && vite build --out-dir dist && npm-run-all copy mvn",
"lint": "eslint --ext .ts,.vue src", "lint": "eslint --ext .ts,.vue src",
"lint:fix": "eslint -ext .ts,.vue src --fix", "lint:fix": "eslint -ext .ts,.vue src --fix",