2020-11-24 22:04:13 +01:00
|
|
|
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) -> {
|
2020-11-25 14:28:29 +01:00
|
|
|
if (hasMetadata(v, "conf.json")) {
|
2020-11-24 22:04:13 +01:00
|
|
|
try {
|
2020-11-25 14:28:29 +01:00
|
|
|
Respackmeta conf = Respackopts.g.fromJson(readMetadata(v, "conf.json", Respackopts.g), Respackmeta.class);
|
|
|
|
if (Respackopts.metaVersion.equals(conf.version)) {
|
|
|
|
Respackopts.resPackMetas.put(v.getDisplayName().asString(), conf);
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
System.err.println(s + " was not loaded as it specifies a different respackopts version than is installed");
|
2020-11-24 22:04:13 +01:00
|
|
|
}
|
|
|
|
} catch (IOException e) {
|
|
|
|
e.printStackTrace();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
});
|
2020-11-25 14:28:29 +01:00
|
|
|
Respackopts.load();
|
|
|
|
Respackopts.save();
|
2020-11-24 22:04:13 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
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();
|
|
|
|
}
|
|
|
|
}
|