Skip to content

Commit

Permalink
Use tar.xz for downloading templates
Browse files Browse the repository at this point in the history
  • Loading branch information
ME1312 committed Jun 28, 2021
1 parent d3f3f19 commit 1427ff0
Show file tree
Hide file tree
Showing 10 changed files with 115 additions and 24 deletions.
2 changes: 1 addition & 1 deletion SubServers.Bungee/common/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@
<dependency>
<groupId>net.ME1312.Galaxi</groupId>
<artifactId>GalaxiUtil</artifactId>
<version>21w26b</version>
<version>21w27b</version>
<scope>compile</scope>
</dependency>
</dependencies>
Expand Down
12 changes: 12 additions & 0 deletions SubServers.Bungee/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,18 @@
<version>1.1</version>
<scope>compile</scope>
</dependency>
<dependency>
<groupId>org.kamranzafar</groupId>
<artifactId>jtar</artifactId>
<version>2.3</version>
<scope>compile</scope>
</dependency>
<dependency>
<groupId>org.tukaani</groupId>
<artifactId>xz</artifactId>
<version>1.9</version>
<scope>compile</scope>
</dependency>
</dependencies>

<build>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -82,8 +82,9 @@ public void reload() {
}

if (host.available && !Util.getDespiteException(() -> Util.reflect(SubProxy.class.getDeclaredField("reloading"), host.plugin), false)) {
host.queue(new PacketExUploadTemplates(host.plugin));
if (enableRT == null || enableRT) host.queue(new PacketExDownloadTemplates(host.plugin, host));
host.queue(new PacketExConfigureHost(host.plugin, host), new PacketExUploadTemplates(host.plugin, () -> {
if (enableRT == null || enableRT) host.queue(new PacketExDownloadTemplates(host.plugin, host));
}));
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,12 +19,7 @@ public abstract class FileScanner {
* @param whitelist File Whitelist
*/
protected void scan(File dir, String... whitelist) throws IOException {
List<String> files;
try {
files = Util.reflect(Util.class.getDeclaredMethod("zipsearch", File.class, File.class), null, dir, dir);
} catch (IllegalAccessException | InvocationTargetException | NoSuchMethodException e) {
throw new IllegalStateException("Cannot access zipsearch()", e);
}
List<String> files = Util.searchDirectory(dir);
if (files.size() <= 0 || whitelist.length <= 0)
return;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,10 +8,22 @@
import net.ME1312.SubData.Server.SubDataClient;
import net.ME1312.SubServers.Bungee.Host.External.ExternalHost;
import net.ME1312.SubServers.Bungee.Host.External.ExternalSubCreator;
import net.ME1312.SubServers.Bungee.Host.SubCreator.ServerTemplate;
import net.ME1312.SubServers.Bungee.Library.Compatibility.Logger;
import net.ME1312.SubServers.Bungee.SubProxy;

import org.kamranzafar.jtar.TarEntry;
import org.kamranzafar.jtar.TarOutputStream;
import org.tukaani.xz.LZMA2Options;
import org.tukaani.xz.XZOutputStream;

import java.io.File;
import java.io.FileInputStream;
import java.io.OutputStream;
import java.util.HashMap;

import static org.tukaani.xz.LZMA2Options.MODE_FAST;
import static org.tukaani.xz.XZ.CHECK_SHA256;

/**
* External Host Template Download Packet
Expand Down Expand Up @@ -39,8 +51,27 @@ public PacketExDownloadTemplates(SubProxy plugin, ExternalHost host) {
public void send(SubDataClient client, OutputStream stream) throws Throwable {
try {
if (client.getBlockSize() < DataSize.MBB) client.tempBlockSize(DataSize.MBB);
Util.zip(new UniversalFile(plugin.dir, "SubServers:Templates"), stream);
stream.close();
HashMap<String, ServerTemplate> map = Util.getDespiteException(() -> Util.reflect(ExternalSubCreator.class.getDeclaredField("templates"), ((ExternalHost) client.getHandler()).getCreator()), new HashMap<>());
TarOutputStream tar = new TarOutputStream(new XZOutputStream(stream, new LZMA2Options(MODE_FAST), CHECK_SHA256));
File dir = new UniversalFile(plugin.dir, "SubServers:Templates");

byte[] buffer = new byte[4096];
for (String file : Util.searchDirectory(dir)) {
int index = file.indexOf(File.separatorChar);
if (index != -1 && !map.containsKey(file.substring(0, index).toLowerCase())) {

tar.putNextEntry(new TarEntry(new File(dir, file), file.replace(File.separatorChar, '/')));
FileInputStream in = new FileInputStream(dir.getAbsolutePath() + File.separator + file);

int len;
while ((len = in.read(buffer)) != -1) {
tar.write(buffer, 0, len);
}

in.close();
}
}
tar.close();

Util.isException(() -> Util.reflect(ExternalSubCreator.class.getDeclaredField("enableRT"), host.getCreator(), true));
} catch (Exception e) {
Expand All @@ -58,6 +89,6 @@ public void receive(SubDataClient client) {

@Override
public int version() {
return 0x0001;
return 0x0002;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -12,21 +12,25 @@
import net.ME1312.SubServers.Bungee.SubProxy;

import java.io.File;
import java.util.Arrays;
import java.util.HashMap;
import java.util.LinkedList;
import java.util.Map;
import java.util.logging.Logger;

/**
* External Host Template Upload Packet
*/
public class PacketExUploadTemplates implements PacketObjectIn<Integer>, PacketOut {
private static LinkedList<Runnable> callbacks = new LinkedList<Runnable>();
private SubProxy plugin;

/**
* New PacketExUploadTemplates
*/
public PacketExUploadTemplates(SubProxy plugin) {
public PacketExUploadTemplates(SubProxy plugin, Runnable... callbacks) {
this.plugin = plugin;
PacketExUploadTemplates.callbacks.addAll(Arrays.asList(callbacks));
}

@SuppressWarnings("unchecked")
Expand All @@ -50,6 +54,12 @@ public void receive(SubDataClient client, ObjectMap<Integer> data) {
e.printStackTrace();
}
}

LinkedList<Runnable> callbacks = PacketExUploadTemplates.callbacks;
PacketExUploadTemplates.callbacks = new LinkedList<Runnable>();
for (Runnable r : callbacks) {
r.run();
}
}
}

Expand Down
2 changes: 1 addition & 1 deletion SubServers.Client/Common/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@
<dependency>
<groupId>net.ME1312.Galaxi</groupId>
<artifactId>GalaxiUtil</artifactId>
<version>21w26b</version>
<version>21w27b</version>
<scope>compile</scope>
</dependency>
<dependency>
Expand Down
16 changes: 14 additions & 2 deletions SubServers.Host/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -30,13 +30,13 @@
<dependency>
<groupId>net.ME1312.Galaxi</groupId>
<artifactId>GalaxiEngine</artifactId>
<version>21w26b</version>
<version>21w27b</version>
<scope>compile</scope>
</dependency>
<dependency>
<groupId>net.ME1312.Galaxi</groupId>
<artifactId>GalaxiUI</artifactId>
<version>21w26b</version>
<version>21w27b</version>
<scope>runtime</scope>
</dependency>
<dependency>
Expand All @@ -51,6 +51,18 @@
<version>1.1</version>
<scope>compile</scope>
</dependency>
<dependency>
<groupId>org.kamranzafar</groupId>
<artifactId>jtar</artifactId>
<version>2.3</version>
<scope>compile</scope>
</dependency>
<dependency>
<groupId>org.tukaani</groupId>
<artifactId>xz</artifactId>
<version>1.9</version>
<scope>compile</scope>
</dependency>
</dependencies>

<build>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,12 +23,7 @@ public abstract class FileScanner {
* @param whitelist File Whitelist
*/
protected void scan(File dir, String... whitelist) throws IOException {
List<String> files;
try {
files = Util.reflect(Util.class.getDeclaredMethod("zipsearch", File.class, File.class), null, dir, dir);
} catch (IllegalAccessException | InvocationTargetException | NoSuchMethodException e) {
throw new IllegalStateException("Cannot access zipsearch()", e);
}
List<String> files = Util.searchDirectory(dir);
if (files.size() <= 0 || whitelist.length <= 0)
return;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,12 @@
import net.ME1312.SubServers.Host.ExHost;
import net.ME1312.SubServers.Host.SubAPI;

import org.kamranzafar.jtar.TarEntry;
import org.kamranzafar.jtar.TarInputStream;
import org.tukaani.xz.XZInputStream;

import java.io.File;
import java.io.FileOutputStream;
import java.io.InputStream;

/**
Expand Down Expand Up @@ -42,7 +48,36 @@ public void receive(SubDataSender client, InputStream stream) {

try {
dir.mkdirs();
Util.unzip(stream, dir);
TarInputStream tar = new TarInputStream(new XZInputStream(stream));

byte[] buffer = new byte[4096];
TarEntry entry;
while ((entry = tar.getNextEntry()) != null) {
File newFile = new File(dir + File.separator + entry.getName().replace('/', File.separatorChar));
if (newFile.exists()) {
if (newFile.isDirectory()) {
Util.deleteDirectory(newFile);
} else {
newFile.delete();
}
}

if (entry.isDirectory()) {
newFile.mkdirs();
continue;
} else if (!newFile.getParentFile().exists()) {
newFile.getParentFile().mkdirs();
}

FileOutputStream fos = new FileOutputStream(newFile);
int len;
while ((len = tar.read(buffer)) != -1) {
fos.write(buffer, 0, len);
}
fos.close();
}
tar.close();

host.creator.load(true);
host.log.info.println(((first)?"":"New ") + "Remote Template Files Downloaded");
} catch (Exception e) {
Expand All @@ -54,6 +89,6 @@ public void receive(SubDataSender client, InputStream stream) {

@Override
public int version() {
return 0x0001;
return 0x0002;
}
}

0 comments on commit 1427ff0

Please sign in to comment.