diff options
3 files changed, 82 insertions, 14 deletions
diff --git a/defaults/src/main/java/shcm/shsupercm/fabric/citresewn/defaults/cit/types/TypeItem.java b/defaults/src/main/java/shcm/shsupercm/fabric/citresewn/defaults/cit/types/TypeItem.java index aa3562b..b08f0c9 100644 --- a/defaults/src/main/java/shcm/shsupercm/fabric/citresewn/defaults/cit/types/TypeItem.java +++ b/defaults/src/main/java/shcm/shsupercm/fabric/citresewn/defaults/cit/types/TypeItem.java @@ -70,18 +70,18 @@ public class TypeItem extends CITType { boolean containsTexture = modelProp == null && !properties.get("citresewn", "texture", "tile").isEmpty(); if (!containsTexture) { - assetIdentifier = resolvePath(identifier, modelProp, ".json", id -> pack.resourcePack.contains(ResourceType.CLIENT_RESOURCES, id)); + assetIdentifier = resolveAsset(properties.identifier, modelProp, "models", ".json", resourceManager); if (assetIdentifier != null) assetIdentifiers.put(null, assetIdentifier); else if (modelProp != null) { - assetIdentifier = resolvePath(identifier, modelProp, ".json", id -> pack.resourcePack.contains(ResourceType.CLIENT_RESOURCES, id)); + assetIdentifier = resolveAsset(properties.identifier, modelProp, "models", ".json", resourceManager); if (assetIdentifier != null) assetIdentifiers.put(null, assetIdentifier); } } for (PropertyValue property : properties.get("citresewn", "model")) { - Identifier subIdentifier = resolvePath(identifier, properties.getProperty(property), ".json", id -> pack.resourcePack.contains(ResourceType.CLIENT_RESOURCES, id)); + Identifier subIdentifier = resolveAsset(properties.identifier, property, "models", ".json", resourceManager); if (subIdentifier == null) throw new CITParsingException("Cannot resolve path", properties, property.position()); @@ -93,12 +93,12 @@ public class TypeItem extends CITType { if (assetIdentifiers.size() == 0) { // attempt to load texture isTexture = true; PropertyValue textureProp = properties.getLastWithoutMetadata("citresewn", "texture", "tile"); - assetIdentifier = resolvePath(identifier, textureProp, ".png", id -> pack.resourcePack.contains(ResourceType.CLIENT_RESOURCES, id)); + assetIdentifier = resolveAsset(properties.identifier, textureProp, "textures", ".png", resourceManager); if (assetIdentifier != null) assetIdentifiers.put(null, assetIdentifier); for (PropertyValue property : properties.get("citresewn", "texture", "tile")) { - Identifier subIdentifier = resolvePath(identifier, properties.getProperty(property), ".png", id -> pack.resourcePack.contains(ResourceType.CLIENT_RESOURCES, id)); + Identifier subIdentifier = resolveAsset(properties.identifier, property, "textures", ".png", resourceManager); if (subIdentifier == null) throw new CITParsingException("Cannot resolve path", properties, property.position()); @@ -109,7 +109,7 @@ public class TypeItem extends CITType { } else { // attempt to load textureOverrideMap from textures PropertyValue textureProp = properties.getLastWithoutMetadata("citresewn", "texture", "tile"); if (textureProp != null) { - assetIdentifier = resolvePath(identifier, textureProp, ".png", id -> pack.resourcePack.contains(ResourceType.CLIENT_RESOURCES, id)); + assetIdentifier = resolveAsset(properties.identifier, textureProp, "textures", ".png", resourceManager); if (assetIdentifier != null) textureOverrideMap.put(null, Either.left(new SpriteIdentifier(SpriteAtlasTexture.BLOCK_ATLAS_TEXTURE, new ResewnTextureIdentifier(assetIdentifier)))); else @@ -118,7 +118,7 @@ public class TypeItem extends CITType { for (PropertyValue property : properties.get("citresewn", "texture", "tile")) { textureProp = property; - Identifier subIdentifier = resolvePath(identifier, textureProp, ".png", id -> pack.resourcePack.contains(ResourceType.CLIENT_RESOURCES, id)); + Identifier subIdentifier = resolveAsset(properties.identifier, textureProp, "textures", ".png", resourceManager); if (subIdentifier == null) throw new CITParsingException("Cannot resolve path", properties, property.position()); @@ -206,7 +206,7 @@ public class TypeItem extends CITType { Collections.reverse(overrideModels); for (Identifier overrideModel : overrideModels) { - Identifier replacement = resolvePath(baseIdentifier, overrideModel.toString(), ".json", resourceManager::containsResource); + Identifier replacement = resolveAsset(baseIdentifier, overrideModel.toString(), "models", ".json", resourceManager); if (replacement != null) { String subTexturePath = replacement.toString().substring(0, replacement.toString().lastIndexOf('.')); final String subTextureName = subTexturePath.substring(subTexturePath.lastIndexOf('/') + 1); @@ -294,7 +294,7 @@ public class TypeItem extends CITType { ((JsonUnbakedModelAccessor) json).getTextureMap().replaceAll((layer, original) -> { Optional<SpriteIdentifier> left = original.left(); if (left.isPresent()) { - Identifier resolvedIdentifier = resolvePath(identifier, left.get().getTextureId().getPath(), ".png", resourceManager::containsResource); + Identifier resolvedIdentifier = resolveAsset(identifier, left.get().getTextureId().getPath(), "textures", ".png", resourceManager); if (resolvedIdentifier != null) return Either.left(new SpriteIdentifier(left.get().getAtlasId(), new ResewnTextureIdentifier(resolvedIdentifier))); } @@ -329,7 +329,7 @@ public class TypeItem extends CITType { if (parentId != null) { String[] parentIdPathSplit = parentId.getPath().split("/"); if (parentId.getPath().startsWith("./") || (parentIdPathSplit.length > 2 && parentIdPathSplit[1].equals("cit"))) { - parentId = resolvePath(identifier, parentId.getPath(), ".json", id -> pack.resourcePack.contains(ResourceType.CLIENT_RESOURCES, id)); + parentId = resolveAsset(identifier, parentId.getPath(), "models", ".json", resourceManager); if (parentId != null) ((JsonUnbakedModelAccessor) json).setParentId(new ResewnItemModelIdentifier(parentId)); } @@ -338,7 +338,7 @@ public class TypeItem extends CITType { json.getOverrides().replaceAll(override -> { String[] modelIdPathSplit = override.getModelId().getPath().split("/"); if (override.getModelId().getPath().startsWith("./") || (modelIdPathSplit.length > 2 && modelIdPathSplit[1].equals("cit"))) { - Identifier resolvedOverridePath = resolvePath(identifier, override.getModelId().getPath(), ".json", id -> pack.resourcePack.contains(ResourceType.CLIENT_RESOURCES, id)); + Identifier resolvedOverridePath = resolveAsset(identifier, override.getModelId().getPath(), "models", ".json", resourceManager); if (resolvedOverridePath != null) return new ModelOverride(new ResewnItemModelIdentifier(resolvedOverridePath), override.streamConditions().collect(Collectors.toList())); } diff --git a/defaults/src/main/java/shcm/shsupercm/fabric/citresewn/defaults/mixin/types/item/ModelLoaderMixin.java b/defaults/src/main/java/shcm/shsupercm/fabric/citresewn/defaults/mixin/types/item/ModelLoaderMixin.java index 22e9373..c95e08a 100644 --- a/defaults/src/main/java/shcm/shsupercm/fabric/citresewn/defaults/mixin/types/item/ModelLoaderMixin.java +++ b/defaults/src/main/java/shcm/shsupercm/fabric/citresewn/defaults/mixin/types/item/ModelLoaderMixin.java @@ -117,7 +117,7 @@ public class ModelLoaderMixin { String originalPath = left.get().getTextureId().getPath(); String[] split = originalPath.split("/"); if (originalPath.startsWith("./") || (split.length > 2 && split[1].equals("cit"))) { - Identifier resolvedIdentifier = CITType.resolvePath(id, originalPath, ".png", identifier -> resourceManager.containsResource(identifier)); + Identifier resolvedIdentifier = CITType.resolveAsset(id, originalPath, "textures", ".png", resourceManager); if (resolvedIdentifier != null) return Either.left(new SpriteIdentifier(left.get().getAtlasId(), new ResewnTextureIdentifier(resolvedIdentifier))); } @@ -129,7 +129,7 @@ public class ModelLoaderMixin { if (parentId != null) { String[] parentIdPathSplit = parentId.getPath().split("/"); if (parentId.getPath().startsWith("./") || (parentIdPathSplit.length > 2 && parentIdPathSplit[1].equals("cit"))) { - parentId = CITType.resolvePath(id, parentId.getPath(), ".json", identifier -> resourceManager.containsResource(identifier)); + parentId = CITType.resolveAsset(id, parentId.getPath(), "models", ".json", resourceManager); if (parentId != null) ((JsonUnbakedModelAccessor) json).setParentId(new ResewnItemModelIdentifier(parentId)); } @@ -138,7 +138,7 @@ public class ModelLoaderMixin { json.getOverrides().replaceAll(override -> { String[] modelIdPathSplit = override.getModelId().getPath().split("/"); if (override.getModelId().getPath().startsWith("./") || (modelIdPathSplit.length > 2 && modelIdPathSplit[1].equals("cit"))) { - Identifier resolvedOverridePath = CITType.resolvePath(id, override.getModelId().getPath(), ".json", identifier -> resourceManager.containsResource(identifier)); + Identifier resolvedOverridePath = CITType.resolveAsset(id, override.getModelId().getPath(), "models", ".json", resourceManager); if (resolvedOverridePath != null) return new ModelOverride(new ResewnItemModelIdentifier(resolvedOverridePath), override.streamConditions().collect(Collectors.toList())); } diff --git a/src/main/java/shcm/shsupercm/fabric/citresewn/cit/CITType.java b/src/main/java/shcm/shsupercm/fabric/citresewn/cit/CITType.java index 7ee1e03..3618dfd 100644 --- a/src/main/java/shcm/shsupercm/fabric/citresewn/cit/CITType.java +++ b/src/main/java/shcm/shsupercm/fabric/citresewn/cit/CITType.java @@ -1,11 +1,14 @@ package shcm.shsupercm.fabric.citresewn.cit; import net.minecraft.resource.ResourceManager; +import net.minecraft.util.Identifier; import shcm.shsupercm.fabric.citresewn.CITResewn; import shcm.shsupercm.fabric.citresewn.ex.CITParsingException; import shcm.shsupercm.fabric.citresewn.pack.format.PropertyGroup; import shcm.shsupercm.fabric.citresewn.pack.format.PropertyValue; +import java.util.Arrays; +import java.util.LinkedList; import java.util.List; public abstract class CITType { @@ -14,4 +17,69 @@ public abstract class CITType { protected void warn(String message, PropertyValue value, PropertyGroup properties) { CITResewn.logWarnLoading("Warning: " + CITParsingException.descriptionOf(message, properties, value.position())); } + + /** + * Takes a defined path and resolves it to an identifier pointing to the resourcepack's path of the specified extension(returns null if no path can be resolved).<br> + * If definedPath is null, will try to resolve a relative file with the same name as the rootIdentifier with the extension, otherwise: <br> + * definedPath will be formatted to replace "\\" with "/" the extension will be appended if not there already. <br> + * It will first try using definedPath as an absolute path, if it cant resolve(or definedPath starts with ./), definedPath will be considered relative. <br> + * Relative paths support going to parent directories using "..". + */ + public static Identifier resolveAsset(Identifier rootIdentifier, String path, String defaultedTypeDirectory, String extension, ResourceManager resourceManager) { + if (path == null) { + path = rootIdentifier.getPath().substring(0, rootIdentifier.getPath().length() - 11); + if (!path.endsWith(extension)) + path = path + extension; + Identifier pathIdentifier = new Identifier(rootIdentifier.getNamespace(), path); + return resourceManager.containsResource(pathIdentifier) ? pathIdentifier : null; + } + + Identifier pathIdentifier = new Identifier(path); + + path = pathIdentifier.getPath().replace('\\', '/'); + if (!path.endsWith(extension)) + path = path + extension; + + if (path.startsWith("./")) + path = path.substring(2); + else if (!path.contains("..")) { + pathIdentifier = new Identifier(pathIdentifier.getNamespace(), path); + if (resourceManager.containsResource(pathIdentifier)) + return pathIdentifier; + else if (path.startsWith("assets/")) { + path = path.substring(7); + int sep = path.indexOf('/'); + pathIdentifier = new Identifier(path.substring(0, sep), path.substring(sep + 1)); + if (resourceManager.containsResource(pathIdentifier)) + return pathIdentifier; + } + pathIdentifier = new Identifier(pathIdentifier.getNamespace(), defaultedTypeDirectory + "/" + path); + if (resourceManager.containsResource(pathIdentifier)) + return pathIdentifier; + } + + LinkedList<String> pathParts = new LinkedList<>(Arrays.asList(rootIdentifier.getPath().split("/"))); + pathParts.removeLast(); + + if (path.contains("/")) { + for (String part : path.split("/")) { + if (part.equals("..")) { + if (pathParts.size() == 0) + return null; + pathParts.removeLast(); + } else + pathParts.addLast(part); + } + } else + pathParts.addLast(path); + path = String.join("/", pathParts); + + pathIdentifier = new Identifier(rootIdentifier.getNamespace(), path); + + return resourceManager.containsResource(pathIdentifier) ? pathIdentifier : null; + } + + public static Identifier resolveAsset(Identifier rootIdentifier, PropertyValue path, String defaultedTypeDirectory, String extension, ResourceManager resourceManager) { + return resolveAsset(rootIdentifier, path == null ? null : path.value(), defaultedTypeDirectory, extension, resourceManager); + } } |