Skip to content

Commit

Permalink
Change path naming system
Browse files Browse the repository at this point in the history
  • Loading branch information
lukebemish committed Aug 18, 2023
1 parent 5716d51 commit 78c0b2f
Show file tree
Hide file tree
Showing 4 changed files with 80 additions and 18 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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<InputStream> getRootResource(String... elements) {
return null;
}

@Nullable
@Override
public IoSupplier<InputStream> 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<String> getNamespaces(PackType type) {
Set<String> set = new HashSet<>();
Path path = this.path.resolve(name);

try (DirectoryStream<Path> 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> T getMetadataSection(MetadataSectionSerializer<T> serializer) {
Expand All @@ -34,4 +94,9 @@ public <T> T getMetadataSection(MetadataSectionSerializer<T> serializer) {
}
return null;
}

@Override
public void close() {

}
}
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,7 @@ public class DefaultResources {

private static final Map<String, BiFunction<String, PackType, Supplier<PackResources>>> QUEUED_RESOURCES = new HashMap<>();
private static final Map<String, BiFunction<String, PackType, Supplier<PackResources>>> QUEUED_STATIC_RESOURCES = new HashMap<>();
public static final String GLOBAL_PREFIX = "global";

public static void forMod(Path configDir, Function<String, Path> inJarPathGetter, String modId) {
Path defaultResourcesMeta = inJarPathGetter.apply(META_FILE_PATH);
Expand All @@ -57,11 +58,11 @@ public static void forMod(Path configDir, Function<String, Path> 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);
Expand Down Expand Up @@ -129,10 +130,10 @@ public static List<Pair<String, Pack.ResourcesSupplier>> 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));
}
}
Expand All @@ -153,10 +154,10 @@ private static List<Pair<String, Pack.ResourcesSupplier>> 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));
}
}
Expand Down Expand Up @@ -184,10 +185,6 @@ public synchronized static void initialize() {
public synchronized static GlobalResourceManager createStaticResourceManager(PackType type) {
initialize();
List<Pair<String, Pack.ResourcesSupplier>> 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);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -52,8 +52,8 @@ public Collection<Pair<String, Pack.ResourcesSupplier>> 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;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -52,8 +52,8 @@ public Collection<Pair<String, Pack.ResourcesSupplier>> 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;
Expand Down

0 comments on commit 78c0b2f

Please sign in to comment.