aboutsummaryrefslogtreecommitdiff
path: root/src/main
diff options
context:
space:
mode:
authorSHsuperCM <shsupercm@gmail.com>2021-08-30 17:18:18 +0300
committerSHsuperCM <shsupercm@gmail.com>2021-08-30 17:18:18 +0300
commit4d3a6afd39fbd3a2e48d9ac3f3af5a58b80bb6e7 (patch)
treeb6dc342c9402ca654256843fb0e74ca1d5e228a4 /src/main
parenta7dc9b7c96178a5646d3451902730cddf28b3a60 (diff)
downloadCITResewn-4d3a6afd39fbd3a2e48d9ac3f3af5a58b80bb6e7.tar.gz
CITResewn-4d3a6afd39fbd3a2e48d9ac3f3af5a58b80bb6e7.tar.bz2
CITResewn-4d3a6afd39fbd3a2e48d9ac3f3af5a58b80bb6e7.zip
v0.4.2: Made model jsons able to reference relative paths
For the love of glob please do not look at this commit or its code, this is really disgusting..
Diffstat (limited to 'src/main')
-rw-r--r--src/main/java/shcm/shsupercm/fabric/citresewn/mixin/JsonUnbakedModelAccessor.java7
-rw-r--r--src/main/java/shcm/shsupercm/fabric/citresewn/mixin/ModelLoaderMixin.java53
-rw-r--r--src/main/java/shcm/shsupercm/fabric/citresewn/pack/ResewnItemModelIdentifier.java4
-rw-r--r--src/main/java/shcm/shsupercm/fabric/citresewn/pack/cits/CIT.java6
-rw-r--r--src/main/java/shcm/shsupercm/fabric/citresewn/pack/cits/CITItem.java19
5 files changed, 86 insertions, 3 deletions
diff --git a/src/main/java/shcm/shsupercm/fabric/citresewn/mixin/JsonUnbakedModelAccessor.java b/src/main/java/shcm/shsupercm/fabric/citresewn/mixin/JsonUnbakedModelAccessor.java
index 84a0f43..70a3815 100644
--- a/src/main/java/shcm/shsupercm/fabric/citresewn/mixin/JsonUnbakedModelAccessor.java
+++ b/src/main/java/shcm/shsupercm/fabric/citresewn/mixin/JsonUnbakedModelAccessor.java
@@ -3,6 +3,7 @@ package shcm.shsupercm.fabric.citresewn.mixin;
import com.mojang.datafixers.util.Either;
import net.minecraft.client.render.model.json.JsonUnbakedModel;
import net.minecraft.client.util.SpriteIdentifier;
+import net.minecraft.util.Identifier;
import org.spongepowered.asm.mixin.Mixin;
import org.spongepowered.asm.mixin.gen.Accessor;
@@ -12,4 +13,10 @@ import java.util.Map;
public interface JsonUnbakedModelAccessor {
@Accessor
Map<String, Either<SpriteIdentifier, String>> getTextureMap();
+
+ @Accessor
+ Identifier getParentId();
+
+ @Accessor
+ void setParentId(Identifier parentId);
}
diff --git a/src/main/java/shcm/shsupercm/fabric/citresewn/mixin/ModelLoaderMixin.java b/src/main/java/shcm/shsupercm/fabric/citresewn/mixin/ModelLoaderMixin.java
index a5e6a30..07f08fd 100644
--- a/src/main/java/shcm/shsupercm/fabric/citresewn/mixin/ModelLoaderMixin.java
+++ b/src/main/java/shcm/shsupercm/fabric/citresewn/mixin/ModelLoaderMixin.java
@@ -1,17 +1,22 @@
package shcm.shsupercm.fabric.citresewn.mixin;
+import com.mojang.datafixers.util.Either;
import net.minecraft.client.render.model.*;
import net.minecraft.client.render.model.json.JsonUnbakedModel;
import net.minecraft.client.texture.TextureManager;
import net.minecraft.client.util.ModelIdentifier;
+import net.minecraft.client.util.SpriteIdentifier;
+import net.minecraft.resource.Resource;
import net.minecraft.resource.ResourceManager;
import net.minecraft.util.Identifier;
import net.minecraft.util.profiler.Profiler;
+import org.apache.commons.io.IOUtils;
import org.spongepowered.asm.mixin.Final;
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.Redirect;
import org.spongepowered.asm.mixin.injection.callback.CallbackInfo;
import org.spongepowered.asm.mixin.injection.callback.CallbackInfoReturnable;
import shcm.shsupercm.fabric.citresewn.ActiveCITs;
@@ -19,9 +24,13 @@ import shcm.shsupercm.fabric.citresewn.CITResewn;
import shcm.shsupercm.fabric.citresewn.config.CITResewnConfig;
import shcm.shsupercm.fabric.citresewn.pack.CITParser;
import shcm.shsupercm.fabric.citresewn.pack.ResewnItemModelIdentifier;
+import shcm.shsupercm.fabric.citresewn.pack.ResewnTextureIdentifier;
import shcm.shsupercm.fabric.citresewn.pack.cits.CIT;
import shcm.shsupercm.fabric.citresewn.pack.cits.CITItem;
+import java.io.IOException;
+import java.io.InputStream;
+import java.nio.charset.StandardCharsets;
import java.util.*;
import java.util.stream.Collectors;
@@ -32,6 +41,7 @@ public abstract class ModelLoaderMixin {
@Shadow @Final private Map<Identifier, UnbakedModel> unbakedModels;
@Shadow @Final private Map<Identifier, UnbakedModel> modelsToBake;
@Shadow @Final private Map<Identifier, BakedModel> bakedModels;
+ @Shadow protected abstract JsonUnbakedModel loadModelFromJson(Identifier id) throws IOException;
private Map<Identifier, BakedModel> citOverrideCacheMap = new HashMap<>();
@@ -111,4 +121,47 @@ public abstract class ModelLoaderMixin {
profiler.pop();
}
+
+
+ @Inject(method = "loadModelFromJson", cancellable = true, at = @At("HEAD"))
+ public void forceLiteralResewnModelIdentifier(Identifier id, CallbackInfoReturnable<JsonUnbakedModel> cir) {
+ if (id instanceof ResewnItemModelIdentifier) {
+ InputStream is = null;
+ Resource resource = null;
+ try {
+ JsonUnbakedModel json = JsonUnbakedModel.deserialize(IOUtils.toString(is = (resource = resourceManager.getResource(id)).getInputStream(), StandardCharsets.UTF_8));
+ json.id = id.toString();
+ json.id = json.id.substring(0, json.id.length() - 5);
+
+ ((JsonUnbakedModelAccessor) json).getTextureMap().replaceAll((layer, original) -> {
+ Optional<SpriteIdentifier> left = original.left();
+ if (left.isPresent() && left.get().getTextureId().getPath().startsWith("./")) {
+ Identifier resolvedIdentifier = CIT.resolvePath(id, left.get().getTextureId().getPath(), ".png", null);
+ if (resolvedIdentifier != null)
+ return Either.left(new SpriteIdentifier(left.get().getAtlasId(), new ResewnTextureIdentifier(resolvedIdentifier)));
+ }
+ return original;
+ });
+
+ Identifier parentId = ((JsonUnbakedModelAccessor) json).getParentId();
+ if (parentId.getPath().startsWith("./")) {
+ parentId = CIT.resolvePath(id, parentId.getPath(), ".json", null);
+ if (parentId != null)
+ ((JsonUnbakedModelAccessor) json).setParentId(new ResewnItemModelIdentifier(parentId));
+ }
+
+ cir.setReturnValue(json);
+ } catch (Exception ignored) {
+ } finally {
+ IOUtils.closeQuietly(is, resource);
+ }
+ }
+ }
+
+ @Redirect(method = "loadModelFromJson", at = @At(value = "INVOKE", target = "Lnet/minecraft/resource/ResourceManager;getResource(Lnet/minecraft/util/Identifier;)Lnet/minecraft/resource/Resource;"))
+ public Resource getResource(ResourceManager resourceManager, Identifier id) throws IOException {
+ if (id.getPath().endsWith(".json.json") && id.getPath().contains("cit"))
+ return resourceManager.getResource(new Identifier(id.getNamespace(), id.getPath().substring(7, id.getPath().length() - 5)));
+ return resourceManager.getResource(id);
+ }
}
diff --git a/src/main/java/shcm/shsupercm/fabric/citresewn/pack/ResewnItemModelIdentifier.java b/src/main/java/shcm/shsupercm/fabric/citresewn/pack/ResewnItemModelIdentifier.java
index 2313ba4..baa1018 100644
--- a/src/main/java/shcm/shsupercm/fabric/citresewn/pack/ResewnItemModelIdentifier.java
+++ b/src/main/java/shcm/shsupercm/fabric/citresewn/pack/ResewnItemModelIdentifier.java
@@ -10,4 +10,8 @@ public class ResewnItemModelIdentifier extends Identifier {
public ResewnItemModelIdentifier(String id) {
super(id);
}
+
+ public ResewnItemModelIdentifier(Identifier identifier) {
+ super(identifier.getNamespace(), identifier.getPath());
+ }
}
diff --git a/src/main/java/shcm/shsupercm/fabric/citresewn/pack/cits/CIT.java b/src/main/java/shcm/shsupercm/fabric/citresewn/pack/cits/CIT.java
index 34f7155..3df4006 100644
--- a/src/main/java/shcm/shsupercm/fabric/citresewn/pack/cits/CIT.java
+++ b/src/main/java/shcm/shsupercm/fabric/citresewn/pack/cits/CIT.java
@@ -318,7 +318,7 @@ public abstract class CIT {
public static Identifier resolvePath(Identifier propertyIdentifier, String path, String extension, ResourcePack pack) {
if (path == null) {
Identifier pathIdentifier = new Identifier(propertyIdentifier.getNamespace(), propertyIdentifier.getPath().replace(".properties", extension));
- return pack.contains(ResourceType.CLIENT_RESOURCES, pathIdentifier) ? pathIdentifier : null;
+ return pack == null || pack.contains(ResourceType.CLIENT_RESOURCES, pathIdentifier) ? pathIdentifier : null;
}
Identifier pathIdentifier = new Identifier(path);
@@ -331,7 +331,7 @@ public abstract class CIT {
path = path.substring(2);
else if (!path.contains("..")) {
pathIdentifier = new Identifier(pathIdentifier.getNamespace(), path);
- if (pack.contains(ResourceType.CLIENT_RESOURCES, pathIdentifier))
+ if (pack == null || pack.contains(ResourceType.CLIENT_RESOURCES, pathIdentifier))
return pathIdentifier;
}
@@ -353,7 +353,7 @@ public abstract class CIT {
pathIdentifier = new Identifier(propertyIdentifier.getNamespace(), path);
- return pack.contains(ResourceType.CLIENT_RESOURCES, pathIdentifier) ? pathIdentifier : null;
+ return pack == null || pack.contains(ResourceType.CLIENT_RESOURCES, pathIdentifier) ? pathIdentifier : null;
}
/**
diff --git a/src/main/java/shcm/shsupercm/fabric/citresewn/pack/cits/CITItem.java b/src/main/java/shcm/shsupercm/fabric/citresewn/pack/cits/CITItem.java
index a2ef7ea..6464863 100644
--- a/src/main/java/shcm/shsupercm/fabric/citresewn/pack/cits/CITItem.java
+++ b/src/main/java/shcm/shsupercm/fabric/citresewn/pack/cits/CITItem.java
@@ -21,6 +21,7 @@ import shcm.shsupercm.fabric.citresewn.ex.CITLoadException;
import shcm.shsupercm.fabric.citresewn.ex.CITParseException;
import shcm.shsupercm.fabric.citresewn.mixin.JsonUnbakedModelAccessor;
import shcm.shsupercm.fabric.citresewn.pack.CITPack;
+import shcm.shsupercm.fabric.citresewn.pack.ResewnItemModelIdentifier;
import shcm.shsupercm.fabric.citresewn.pack.ResewnTextureIdentifier;
import java.io.InputStream;
@@ -152,6 +153,24 @@ public class CITItem extends CIT {
json = JsonUnbakedModel.deserialize(IOUtils.toString(is = (resource = resourceManager.getResource(identifier)).getInputStream(), StandardCharsets.UTF_8));
json.id = identifier.toString();
json.id = json.id.substring(0, json.id.length() - 5);
+
+ ((JsonUnbakedModelAccessor) json).getTextureMap().replaceAll((layer, original) -> {
+ Optional<SpriteIdentifier> left = original.left();
+ if (left.isPresent() && left.get().getTextureId().getPath().startsWith("./")) {
+ Identifier resolvedIdentifier = resolvePath(identifier, left.get().getTextureId().getPath(), ".png", pack.resourcePack);
+ if (resolvedIdentifier != null)
+ return Either.left(new SpriteIdentifier(left.get().getAtlasId(), new ResewnTextureIdentifier(resolvedIdentifier)));
+ }
+ return original;
+ });
+
+ Identifier parentId = ((JsonUnbakedModelAccessor) json).getParentId();
+ if (parentId.getPath().startsWith("./")) {
+ parentId = resolvePath(identifier, parentId.getPath(), ".json", pack.resourcePack);
+ if (parentId != null)
+ ((JsonUnbakedModelAccessor) json).setParentId(new ResewnItemModelIdentifier(parentId));
+ }
+
return json;
} finally {
IOUtils.closeQuietly(is, resource);