use separately generated hashes

This commit is contained in:
EOT3000 2020-04-28 10:45:10 -04:00
parent 1a194dbce8
commit 8ac199b027
2 changed files with 49 additions and 43 deletions

View File

@ -25,6 +25,7 @@
package org.geysermc.connector.utils; package org.geysermc.connector.utils;
import com.fasterxml.jackson.core.JsonFactory;
import com.fasterxml.jackson.core.JsonParser; import com.fasterxml.jackson.core.JsonParser;
import com.fasterxml.jackson.databind.DeserializationFeature; import com.fasterxml.jackson.databind.DeserializationFeature;
import com.fasterxml.jackson.databind.ObjectMapper; import com.fasterxml.jackson.databind.ObjectMapper;
@ -42,11 +43,16 @@ public class FileUtils {
return objectMapper.readValue(src, valueType); return objectMapper.readValue(src, valueType);
} }
public static <T> T loadJson(InputStream src, Class<T> valueType) throws IOException { public static <T> T loadYaml(InputStream src, Class<T> valueType) throws IOException {
ObjectMapper objectMapper = new ObjectMapper(new YAMLFactory()).enable(JsonParser.Feature.IGNORE_UNDEFINED).disable(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES); ObjectMapper objectMapper = new ObjectMapper(new YAMLFactory()).enable(JsonParser.Feature.IGNORE_UNDEFINED).disable(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES);
return objectMapper.readValue(src, valueType); return objectMapper.readValue(src, valueType);
} }
public static <T> T loadJson(InputStream src, Class<T> valueType) throws IOException {
ObjectMapper objectMapper = new ObjectMapper(new JsonFactory()).enable(JsonParser.Feature.IGNORE_UNDEFINED).disable(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES);
return objectMapper.readValue(src, valueType);
}
public static File fileOrCopiedFromResource(String name, Function<String, String> s) throws IOException { public static File fileOrCopiedFromResource(String name, Function<String, String> s) throws IOException {
return fileOrCopiedFromResource(new File(name), name, s); return fileOrCopiedFromResource(new File(name), name, s);
} }
@ -73,7 +79,7 @@ public class FileUtils {
return file; return file;
} }
public static void writeFile(File file, char[] data) throws IOException { private static void writeFile(File file, char[] data) throws IOException {
if (!file.exists()) { if (!file.exists()) {
file.createNewFile(); file.createNewFile();
} }

View File

@ -26,66 +26,66 @@ public class ResourcePack {
public static final NativeCode<VoxelwindHash> HASH = new NativeCode<>("native-hash", JavaHash.class, NativeHash.class); public static final NativeCode<VoxelwindHash> HASH = new NativeCode<>("native-hash", JavaHash.class, NativeHash.class);
public static final int CHUNK_SIZE = 1048576; public static final int CHUNK_SIZE = 1048576;
private boolean hashed;
private byte[] sha256; private byte[] sha256;
private File file; private File file;
private ResourcePackManifest manifest; private ResourcePackManifest manifest;
private ResourcePackManifest.Version version; private ResourcePackManifest.Version version;
public static void loadPacks() { public static void loadPacks() {
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) {
//
}
File directory = new File("packs"); File directory = new File("packs");
for(File file : directory.listFiles()) { for(File file : directory.listFiles()) {
try { if(file.getName().endsWith(".zip")) {
ZipFile zip = new ZipFile(file); ResourcePack pack = new ResourcePack();
zip.stream().forEach((x) -> { pack.sha256 = getBytes(hashes.get(file.getName()));
if(x.getName().contains("manifest.json")) {
try {
ResourcePackManifest manifest = FileUtils.loadJson(zip.getInputStream(x), ResourcePackManifest.class);
ResourcePack pack = new ResourcePack(); try {
ZipFile zip = new ZipFile(file);
pack.file = file; zip.stream().forEach((x) -> {
pack.manifest = manifest; if (x.getName().contains("manifest.json")) {
pack.version = ResourcePackManifest.Version.fromArray(manifest.getHeader().getVersion()); try {
ResourcePackManifest manifest = FileUtils.loadJson(zip.getInputStream(x), ResourcePackManifest.class);
PACKS.put(pack.getManifest().getHeader().getUuid().toString(), pack); pack.file = file;
} catch (Exception e) { pack.manifest = manifest;
e.printStackTrace(); pack.version = ResourcePackManifest.Version.fromArray(manifest.getHeader().getVersion());
PACKS.put(pack.getManifest().getHeader().getUuid().toString(), pack);
} catch (Exception e) {
e.printStackTrace();
}
} }
} });
}); } catch (Exception e) {
} catch (Exception e) { GeyserConnector.getInstance().getLogger().error(file.getName() + " " + "is broken!");
GeyserConnector.getInstance().getLogger().error(file.getName() + " " + "is broken!"); e.printStackTrace();
e.printStackTrace(); }
} }
} }
} }
/** private static byte[] getBytes(String string) {
* author: NukkitX String[] strings = string.replace("]", "").replace("[", "").replaceAll(" ", "").split(",");
* Nukkit Project byte[] bytes = new byte[strings.length];
*/
//TODO: calculate this separately for(int i = 0; i < strings.length; i++) {
public byte[] getSha256() { bytes[i] = Byte.parseByte(strings[i]);
if (!hashed) {
VoxelwindHash hash = HASH.newInstance();
ByteBuf bytes = null;
try {
bytes = PooledByteBufAllocator.DEFAULT.directBuffer(Math.toIntExact(Files.size(file.toPath()))); // Hopefully there is not a resource pack big enough to need a long...
bytes.writeBytes(Files.readAllBytes(file.toPath()));
hash.update(bytes);
sha256 = hash.digest();
hashed = true;
} catch (Exception e) {
throw new RuntimeException("Could not calculate pack hash", e);
} finally {
if (bytes != null) {
bytes.release();
}
}
} }
return bytes;
}
public byte[] getSha256() {
return sha256; return sha256;
} }