58 lines
2.4 KiB
Java
58 lines
2.4 KiB
Java
package io.gitlab.jfronny.respackopts.mixin;
|
|
|
|
import com.google.gson.Gson;
|
|
import com.google.gson.JsonElement;
|
|
import com.google.gson.JsonObject;
|
|
import io.gitlab.jfronny.respackopts.Respackopts;
|
|
import io.gitlab.jfronny.respackopts.data.Respackmeta;
|
|
import net.minecraft.resource.ResourcePackManager;
|
|
import net.minecraft.resource.ResourcePackProfile;
|
|
import net.minecraft.resource.ResourceType;
|
|
import net.minecraft.util.Identifier;
|
|
import org.spongepowered.asm.mixin.Mixin;
|
|
import org.spongepowered.asm.mixin.Shadow;
|
|
import org.spongepowered.asm.mixin.injection.At;
|
|
import org.spongepowered.asm.mixin.injection.Inject;
|
|
import org.spongepowered.asm.mixin.injection.callback.CallbackInfo;
|
|
|
|
import java.io.ByteArrayOutputStream;
|
|
import java.io.IOException;
|
|
import java.io.InputStream;
|
|
import java.util.Map;
|
|
|
|
@Mixin(ResourcePackManager.class)
|
|
public class ResourcePackManagerMixin {
|
|
@Shadow private Map<String, ResourcePackProfile> profiles;
|
|
|
|
@Inject(at = @At("TAIL"), method = "scanPacks()V")
|
|
private void scanPacks(CallbackInfo info) {
|
|
profiles.forEach((s, v) -> {
|
|
if (hasMetadata(v, "meta.json") && hasMetadata(v, "conf.json")) {
|
|
try {
|
|
Respackmeta.JsonMeta meta = Respackopts.g.fromJson(readMetadata(v, "meta.json", Respackopts.g), Respackmeta.JsonMeta.class);
|
|
if (Respackopts.metaVersion.equals(meta.version)) {
|
|
Respackopts.resPackMetas.put(v.getDisplayName().asString(), new Respackmeta(meta, readMetadata(v, "conf.json", Respackopts.g)));
|
|
}
|
|
} catch (IOException e) {
|
|
e.printStackTrace();
|
|
}
|
|
}
|
|
});
|
|
}
|
|
|
|
private boolean hasMetadata(ResourcePackProfile v, String fname) {
|
|
return v.createResourcePack().contains(ResourceType.CLIENT_RESOURCES, new Identifier(Respackopts.ID, fname));
|
|
}
|
|
|
|
private JsonObject readMetadata(ResourcePackProfile v, String fname, Gson g) throws IOException {
|
|
InputStream is = v.createResourcePack().open(ResourceType.CLIENT_RESOURCES, new Identifier(Respackopts.ID, fname));
|
|
ByteArrayOutputStream bais = new ByteArrayOutputStream();
|
|
byte[] buffer = new byte[1024];
|
|
int length;
|
|
while ((length = is.read(buffer)) != -1) {
|
|
bais.write(buffer, 0, length);
|
|
}
|
|
return g.fromJson(bais.toString(), JsonElement.class).getAsJsonObject();
|
|
}
|
|
}
|