diff --git a/Common/src/main/java/dev/lukebemish/defaultresources/impl/AutoMetadataPathPackResources.java b/Common/src/main/java/dev/lukebemish/defaultresources/impl/AutoMetadataPathPackResources.java index 37ce89d..9a6942b 100644 --- a/Common/src/main/java/dev/lukebemish/defaultresources/impl/AutoMetadataPathPackResources.java +++ b/Common/src/main/java/dev/lukebemish/defaultresources/impl/AutoMetadataPathPackResources.java @@ -6,23 +6,83 @@ package dev.lukebemish.defaultresources.impl; import com.google.gson.JsonObject; +import com.mojang.logging.LogUtils; +import net.minecraft.FileUtil; import net.minecraft.SharedConstants; +import net.minecraft.resources.ResourceLocation; +import net.minecraft.server.packs.AbstractPackResources; import net.minecraft.server.packs.PackType; import net.minecraft.server.packs.PathPackResources; import net.minecraft.server.packs.metadata.MetadataSectionSerializer; +import net.minecraft.server.packs.resources.IoSupplier; +import org.jetbrains.annotations.NotNull; import org.jetbrains.annotations.Nullable; +import org.slf4j.Logger; +import java.io.IOException; +import java.io.InputStream; +import java.nio.file.DirectoryStream; +import java.nio.file.Files; import java.nio.file.Path; +import java.util.HashSet; +import java.util.Locale; +import java.util.Set; -public class AutoMetadataPathPackResources extends PathPackResources { +public class AutoMetadataPathPackResources extends AbstractPackResources { + private static final Logger LOGGER = LogUtils.getLogger(); + private final String name; + private final Path path; private final PackType packType; - public AutoMetadataPathPackResources(String s, PackType packType, Path path) { - super(s, path, false); + public AutoMetadataPathPackResources(String s, String prefix, Path path, PackType packType) { + super(s, false); + this.name = prefix+packType.getDirectory(); + this.path = path; this.packType = packType; } + @Nullable + @Override + public IoSupplier getRootResource(String... elements) { + return null; + } + + @Nullable + @Override + public IoSupplier getResource(PackType packType, ResourceLocation location) { + Path path = this.path.resolve(name).resolve(location.getNamespace()); + return PathPackResources.getResource(location, path); + } + + @Override + public void listResources(PackType packType, String namespace, String path, ResourceOutput resourceOutput) { + FileUtil.decomposePath(path).get().ifLeft((list) -> { + Path namespacePath = this.path.resolve(name).resolve(namespace); + PathPackResources.listPath(namespace, namespacePath, list, resourceOutput); + }).ifRight((partialResult) -> LOGGER.error("Invalid path {}: {}", path, partialResult.message())); + } + + @Override + public @NotNull Set getNamespaces(PackType type) { + Set set = new HashSet<>(); + Path path = this.path.resolve(name); + + try (DirectoryStream paths = Files.newDirectoryStream(path)) { + for (Path namespacePath : paths) { + if (Files.isDirectory(namespacePath)) { + String namespace = namespacePath.getFileName().toString(); + if (namespace.equals(namespace.toLowerCase(Locale.ROOT))) { + set.add(namespace); + } + } + } + } catch (IOException e) { + LOGGER.error("Failed to list path {}", path, e); + } + return set; + } + @Nullable @Override public T getMetadataSection(MetadataSectionSerializer serializer) { @@ -34,4 +94,9 @@ public T getMetadataSection(MetadataSectionSerializer serializer) { } return null; } + + @Override + public void close() { + + } } diff --git a/Common/src/main/java/dev/lukebemish/defaultresources/impl/DefaultResources.java b/Common/src/main/java/dev/lukebemish/defaultresources/impl/DefaultResources.java index dd5432d..c1adb75 100644 --- a/Common/src/main/java/dev/lukebemish/defaultresources/impl/DefaultResources.java +++ b/Common/src/main/java/dev/lukebemish/defaultresources/impl/DefaultResources.java @@ -42,6 +42,7 @@ public class DefaultResources { private static final Map>> QUEUED_RESOURCES = new HashMap<>(); private static final Map>> QUEUED_STATIC_RESOURCES = new HashMap<>(); + public static final String GLOBAL_PREFIX = "global"; public static void forMod(Path configDir, Function inJarPathGetter, String modId) { Path defaultResourcesMeta = inJarPathGetter.apply(META_FILE_PATH); @@ -57,11 +58,11 @@ public static void forMod(Path configDir, Function inJarPathGetter if (extractionState == Config.ExtractionState.UNEXTRACTED) { QUEUED_RESOURCES.put("__extracted_" + modId, (s, type) -> { if (!Files.exists(defaultResources.resolve(type.getDirectory()))) return null; - return () -> new AutoMetadataPathPackResources(s, type, defaultResources); + return () -> new AutoMetadataPathPackResources(s, "", defaultResources, type); }); QUEUED_STATIC_RESOURCES.put("__extracted_" + modId, (s, type) -> { - if (!Files.exists(defaultResources.resolve("static").resolve(type.getDirectory()))) return null; - return () -> new AutoMetadataPathPackResources(s, type, defaultResources.resolve("static")); + if (!Files.exists(defaultResources.resolve(GLOBAL_PREFIX+type.getDirectory()))) return null; + return () -> new AutoMetadataPathPackResources(s, GLOBAL_PREFIX, defaultResources, type); }); } else if ((meta.markerPath().isPresent() && !Files.exists(configDir.resolve(meta.markerPath().get())) && extractionState.extractIfMissing) || extractionState.extractRegardless) { Config.INSTANCE.get().extract().put(modId, Config.ExtractionState.EXTRACTED); @@ -129,10 +130,10 @@ public static List> getPackResources(PackTy try (var files = Files.list(Services.PLATFORM.getGlobalFolder())) { for (var file : files.toList()) { if (Files.isDirectory(file)) { - Pack.ResourcesSupplier packResources = s -> new AutoMetadataPathPackResources(s, type, file); + Pack.ResourcesSupplier packResources = s -> new AutoMetadataPathPackResources(s, "", file, type); packs.add(new Pair<>(file.getFileName().toString(), packResources)); } else if (file.getFileName().toString().endsWith(".zip")) { - Pack.ResourcesSupplier packResources = s -> new AutoMetadataPathPackResources(s, type, file); + Pack.ResourcesSupplier packResources = s -> new AutoMetadataPathPackResources(s, "", file, type); packs.add(new Pair<>(file.getFileName().toString(), packResources)); } } @@ -153,10 +154,10 @@ private static List> getStaticPackResources try (var files = Files.list(Services.PLATFORM.getGlobalFolder())) { for (var file : files.toList()) { if (Files.isDirectory(file)) { - Pack.ResourcesSupplier packResources = s -> new AutoMetadataPathPackResources(s, type, file); + Pack.ResourcesSupplier packResources = s -> new AutoMetadataPathPackResources(s, GLOBAL_PREFIX, file, type); packs.add(new Pair<>(file.getFileName().toString(), packResources)); } else if (file.getFileName().toString().endsWith(".zip")) { - Pack.ResourcesSupplier packResources = s -> new AutoMetadataPathPackResources(s, type, file); + Pack.ResourcesSupplier packResources = s -> new AutoMetadataPathPackResources(s, GLOBAL_PREFIX, file, type); packs.add(new Pair<>(file.getFileName().toString(), packResources)); } } @@ -184,10 +185,6 @@ public synchronized static void initialize() { public synchronized static GlobalResourceManager createStaticResourceManager(PackType type) { initialize(); List> sources = new ArrayList<>(getStaticPackResources(type)); - try (var paths = Files.list(Services.PLATFORM.getGlobalFolder())) { - paths.forEach(path -> sources.add(new Pair<>(path.getFileName().toString(), s -> new AutoMetadataPathPackResources(s, type, path)))); - } catch (IOException ignored) { - } sources.addAll(Services.PLATFORM.getJarProviders(type)); return new CombinedResourceManager(type, sources); } diff --git a/Forge/src/main/java/dev/lukebemish/defaultresources/impl/forge/PlatformImpl.java b/Forge/src/main/java/dev/lukebemish/defaultresources/impl/forge/PlatformImpl.java index 5d62738..707d564 100644 --- a/Forge/src/main/java/dev/lukebemish/defaultresources/impl/forge/PlatformImpl.java +++ b/Forge/src/main/java/dev/lukebemish/defaultresources/impl/forge/PlatformImpl.java @@ -52,8 +52,8 @@ public Collection> getJarProviders(PackType FMLLoader.getLoadingModList().getModFiles().stream().flatMap(f -> f.getMods().stream()) .filter(mod -> !(mod.getModId().equals("forge") || mod.getModId().equals("minecraft"))) .forEach(mod -> { - Path packPath = mod.getOwningFile().getFile().getSecureJar().getPath(String.join("/static/")); - providers.add(new Pair<>(mod.getModId(), s -> new AutoMetadataPathPackResources(s, type, packPath))); + Path packPath = mod.getOwningFile().getFile().getSecureJar().getPath(String.join("/")); + providers.add(new Pair<>(mod.getModId(), s -> new AutoMetadataPathPackResources(s, "global", packPath, type))); }); return providers; } diff --git a/Quilt/src/main/java/dev/lukebemish/defaultresources/impl/quilt/PlatformImpl.java b/Quilt/src/main/java/dev/lukebemish/defaultresources/impl/quilt/PlatformImpl.java index 7986d95..206df23 100644 --- a/Quilt/src/main/java/dev/lukebemish/defaultresources/impl/quilt/PlatformImpl.java +++ b/Quilt/src/main/java/dev/lukebemish/defaultresources/impl/quilt/PlatformImpl.java @@ -52,8 +52,8 @@ public Collection> getJarProviders(PackType QuiltLoader.getAllMods().forEach(mod -> { String modid = mod.metadata().id(); if (!modid.equals("minecraft")) { - Path packPath = mod.rootPath().resolve("static"); - providers.add(new Pair<>(modid, s -> new AutoMetadataPathPackResources(s, type, packPath))); + Path packPath = mod.rootPath(); + providers.add(new Pair<>(modid, s -> new AutoMetadataPathPackResources(s, "global", packPath, type))); } }); return providers;