2020-04-13 22:16:12 +00:00
|
|
|
package org.geysermc.connector.utils;
|
|
|
|
|
|
|
|
import com.voxelwind.server.jni.hash.JavaHash;
|
|
|
|
import com.voxelwind.server.jni.hash.NativeHash;
|
|
|
|
import com.voxelwind.server.jni.hash.VoxelwindHash;
|
|
|
|
import net.md_5.bungee.jni.NativeCode;
|
|
|
|
import org.geysermc.connector.GeyserConnector;
|
|
|
|
|
|
|
|
import java.io.File;
|
|
|
|
import java.nio.file.Files;
|
|
|
|
import java.util.HashMap;
|
|
|
|
import java.util.Map;
|
|
|
|
import java.util.zip.ZipFile;
|
|
|
|
|
|
|
|
public class ResourcePack {
|
|
|
|
public static final Map<String, ResourcePack> PACKS = new HashMap<>();
|
|
|
|
public static final NativeCode<VoxelwindHash> HASH = new NativeCode<>("native-hash", JavaHash.class, NativeHash.class);
|
2020-06-03 00:07:15 +00:00
|
|
|
public static final int CHUNK_SIZE = 102400;
|
2020-04-13 22:16:12 +00:00
|
|
|
|
|
|
|
private byte[] sha256;
|
|
|
|
private File file;
|
|
|
|
private ResourcePackManifest manifest;
|
|
|
|
private ResourcePackManifest.Version version;
|
|
|
|
|
|
|
|
public static void loadPacks() {
|
2020-04-28 14:45:10 +00:00
|
|
|
Map<String, String> hashes = new HashMap<>();
|
|
|
|
|
|
|
|
try {
|
|
|
|
Files.lines(new File("packs/hashes.txt").toPath()).forEach((x) -> hashes.put(x.split("=")[0], x.split("=")[1]));
|
|
|
|
} catch (Exception e) {
|
|
|
|
//
|
|
|
|
}
|
|
|
|
|
2020-04-13 22:16:12 +00:00
|
|
|
File directory = new File("packs");
|
2020-05-05 17:49:38 +00:00
|
|
|
|
|
|
|
if (!directory.exists()) {
|
|
|
|
directory.mkdir();
|
|
|
|
}
|
|
|
|
|
2020-04-13 22:16:12 +00:00
|
|
|
for(File file : directory.listFiles()) {
|
2020-04-28 20:17:29 +00:00
|
|
|
if(file.getName().endsWith(".zip") || file.getName().endsWith(".mcpack")) {
|
2020-04-28 14:45:10 +00:00
|
|
|
ResourcePack pack = new ResourcePack();
|
|
|
|
|
|
|
|
pack.sha256 = getBytes(hashes.get(file.getName()));
|
2020-04-13 22:16:12 +00:00
|
|
|
|
2020-04-28 14:45:10 +00:00
|
|
|
try {
|
|
|
|
ZipFile zip = new ZipFile(file);
|
2020-04-13 22:16:12 +00:00
|
|
|
|
2020-04-28 14:45:10 +00:00
|
|
|
zip.stream().forEach((x) -> {
|
|
|
|
if (x.getName().contains("manifest.json")) {
|
|
|
|
try {
|
|
|
|
ResourcePackManifest manifest = FileUtils.loadJson(zip.getInputStream(x), ResourcePackManifest.class);
|
2020-04-13 22:16:12 +00:00
|
|
|
|
2020-04-28 14:45:10 +00:00
|
|
|
pack.file = file;
|
|
|
|
pack.manifest = manifest;
|
|
|
|
pack.version = ResourcePackManifest.Version.fromArray(manifest.getHeader().getVersion());
|
2020-04-13 22:16:12 +00:00
|
|
|
|
2020-04-28 14:45:10 +00:00
|
|
|
PACKS.put(pack.getManifest().getHeader().getUuid().toString(), pack);
|
|
|
|
} catch (Exception e) {
|
|
|
|
e.printStackTrace();
|
|
|
|
}
|
2020-04-13 22:16:12 +00:00
|
|
|
}
|
2020-04-28 14:45:10 +00:00
|
|
|
});
|
|
|
|
} catch (Exception e) {
|
|
|
|
GeyserConnector.getInstance().getLogger().error(file.getName() + " " + "is broken!");
|
|
|
|
e.printStackTrace();
|
|
|
|
}
|
2020-04-13 22:16:12 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-04-28 14:45:10 +00:00
|
|
|
private static byte[] getBytes(String string) {
|
|
|
|
String[] strings = string.replace("]", "").replace("[", "").replaceAll(" ", "").split(",");
|
|
|
|
byte[] bytes = new byte[strings.length];
|
|
|
|
|
|
|
|
for(int i = 0; i < strings.length; i++) {
|
|
|
|
bytes[i] = Byte.parseByte(strings[i]);
|
2020-04-13 22:16:12 +00:00
|
|
|
}
|
2020-04-28 14:45:10 +00:00
|
|
|
|
|
|
|
return bytes;
|
|
|
|
}
|
|
|
|
|
|
|
|
public byte[] getSha256() {
|
2020-04-13 22:16:12 +00:00
|
|
|
return sha256;
|
|
|
|
}
|
|
|
|
|
|
|
|
public File getFile() {
|
|
|
|
return file;
|
|
|
|
}
|
|
|
|
|
|
|
|
public ResourcePackManifest getManifest() {
|
|
|
|
return manifest;
|
|
|
|
}
|
|
|
|
|
|
|
|
public ResourcePackManifest.Version getVersion() {
|
|
|
|
return version;
|
|
|
|
}
|
|
|
|
}
|