Respackopts/src/main/java/io/gitlab/jfronny/respackopts/mixin/ResourcePackManagerMixin.java

70 lines
3.1 KiB
Java
Raw Normal View History

2020-11-24 22:04:13 +01:00
package io.gitlab.jfronny.respackopts.mixin;
2021-06-10 16:11:19 +02:00
import com.google.gson.Gson;
import com.google.gson.JsonElement;
import com.google.gson.JsonObject;
2020-11-24 22:04:13 +01:00
import io.gitlab.jfronny.respackopts.Respackopts;
2021-06-10 16:07:22 +02:00
import io.gitlab.jfronny.respackopts.data.Respackmeta;
2021-06-10 16:11:19 +02:00
import io.gitlab.jfronny.respackopts.data.entry.SyncMode;
2020-11-24 22:04:13 +01:00
import net.minecraft.resource.ResourcePackManager;
import net.minecraft.resource.ResourcePackProfile;
import net.minecraft.resource.ResourceType;
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) {
2021-06-10 19:26:59 +02:00
Respackopts.NAME_LOOKUP.clear();
2020-11-24 22:04:13 +01:00
profiles.forEach((s, v) -> {
2021-06-10 13:10:12 +02:00
if (rpo$hasMetadata(v)) {
2020-11-24 22:04:13 +01:00
try {
2021-06-10 13:10:12 +02:00
Respackmeta conf = Respackopts.GSON.fromJson(rpo$readMetadata(v, Respackopts.GSON), Respackmeta.class);
if (Respackopts.META_VERSION < conf.version) {
Respackopts.LOGGER.error(s + " was not loaded as it specifies a newer respackopts version than is installed");
return;
}
if (Respackopts.META_VERSION > conf.version) {
Respackopts.LOGGER.warn(s + " uses an outdated RPO format (" + conf.version + "). Although this is supported, using the latest version (" + Respackopts.META_VERSION + ") is recommended");
}
conf.conf.setVersion(conf.version);
2021-06-10 13:10:12 +02:00
if (!Respackopts.CONFIG_BRANCH.containsKey(conf.id))
Respackopts.CONFIG_BRANCH.put(conf.id, conf.conf);
else
2021-06-10 14:55:03 +02:00
Respackopts.CONFIG_BRANCH.get(conf.id).sync(conf.conf, SyncMode.RESPACK_LOAD);
2021-06-10 19:26:59 +02:00
Respackopts.NAME_LOOKUP.put(v.getDisplayName().asString(), conf.id);
Respackopts.load(conf.id);
} catch (Throwable e) {
Respackopts.LOGGER.error("Could not initialize pack meta for " + s, e);
2020-11-24 22:04:13 +01:00
}
}
});
}
2021-06-10 13:10:12 +02:00
private boolean rpo$hasMetadata(ResourcePackProfile v) {
return v.createResourcePack().contains(ResourceType.CLIENT_RESOURCES, Respackopts.CONF_ID);
2020-11-24 22:04:13 +01:00
}
2021-06-10 13:10:12 +02:00
private JsonObject rpo$readMetadata(ResourcePackProfile v, Gson g) throws IOException {
InputStream is = v.createResourcePack().open(ResourceType.CLIENT_RESOURCES, Respackopts.CONF_ID);
ByteArrayOutputStream os = new ByteArrayOutputStream();
2020-11-24 22:04:13 +01:00
byte[] buffer = new byte[1024];
int length;
while ((length = is.read(buffer)) != -1) {
os.write(buffer, 0, length);
2020-11-24 22:04:13 +01:00
}
return g.fromJson(os.toString(), JsonElement.class).getAsJsonObject();
2020-11-24 22:04:13 +01:00
}
}