ModNioResourcePack: don't extend AbstractFileResourcePack ()

This commit is contained in:
J. Fronny 2022-09-25 15:44:52 +02:00 committed by GitHub
parent 0b73465810
commit 03bf91853d
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23

View file

@ -33,16 +33,20 @@ import java.util.Collections;
import java.util.EnumMap;
import java.util.HashSet;
import java.util.List;
import java.util.Locale;
import java.util.Map;
import java.util.Set;
import java.util.function.Predicate;
import java.util.regex.Pattern;
import org.jetbrains.annotations.ApiStatus;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import net.minecraft.resource.AbstractFileResourcePack;
import net.minecraft.resource.ResourcePack;
import net.minecraft.resource.ResourceType;
import net.minecraft.resource.metadata.ResourceMetadataReader;
import net.minecraft.util.Identifier;
import net.minecraft.util.InvalidIdentifierException;
@ -51,7 +55,8 @@ import net.fabricmc.fabric.api.resource.ResourcePackActivationType;
import net.fabricmc.loader.api.ModContainer;
import net.fabricmc.loader.api.metadata.ModMetadata;
public class ModNioResourcePack extends AbstractFileResourcePack implements ModResourcePack {
@ApiStatus.Internal
public class ModNioResourcePack implements ResourcePack, ModResourcePack {
private static final Logger LOGGER = LoggerFactory.getLogger(ModNioResourcePack.class);
private static final Pattern RESOURCE_PACK_PATH = Pattern.compile("[a-z0-9-_.]+");
private static final FileSystem DEFAULT_FS = FileSystems.getDefault();
@ -94,8 +99,6 @@ public class ModNioResourcePack extends AbstractFileResourcePack implements ModR
}
private ModNioResourcePack(Identifier id, String name, ModMetadata modInfo, List<Path> paths, ResourceType type, AutoCloseable closer, ResourcePackActivationType activationType) {
super(null);
this.id = id;
this.name = name;
this.modInfo = modInfo;
@ -183,8 +186,7 @@ public class ModNioResourcePack extends AbstractFileResourcePack implements ModR
return !namespaces.get(type).contains(filename.substring(prefixLen, nsEnd));
}
@Override
protected InputStream openFile(String filename) throws IOException {
private InputStream openFile(String filename) throws IOException {
InputStream stream;
Path path = getPath(filename);
@ -204,13 +206,17 @@ public class ModNioResourcePack extends AbstractFileResourcePack implements ModR
}
@Override
protected boolean containsFile(String filename) {
if (ModResourcePackUtil.containsDefault(modInfo, filename)) {
return true;
public InputStream openRoot(String fileName) throws IOException {
if (fileName.contains("/") || fileName.contains("\\")) {
throw new IllegalArgumentException("Root resources can only be filenames, not paths (no / allowed!)");
}
Path path = getPath(filename);
return path != null && Files.isRegularFile(path);
return this.openFile(fileName);
}
@Override
public InputStream open(ResourceType type, Identifier id) throws IOException {
return openFile(getFilename(type, id));
}
@Override
@ -252,11 +258,30 @@ public class ModNioResourcePack extends AbstractFileResourcePack implements ModR
return ids;
}
@Override
public boolean contains(ResourceType type, Identifier id) {
String filename = getFilename(type, id);
if (ModResourcePackUtil.containsDefault(modInfo, filename)) {
return true;
}
Path path = getPath(filename);
return path != null && Files.isRegularFile(path);
}
@Override
public Set<String> getNamespaces(ResourceType type) {
return namespaces.getOrDefault(type, Collections.emptySet());
}
@Override
public <T> T parseMetadata(ResourceMetadataReader<T> metaReader) throws IOException {
try (InputStream is = openFile("pack.mcmeta")) {
return AbstractFileResourcePack.parseMetadata(metaReader, is);
}
}
@Override
public void close() {
if (closer != null) {
@ -290,4 +315,8 @@ public class ModNioResourcePack extends AbstractFileResourcePack implements ModR
// NIO Files.exists is notoriously slow when checking the file system
return path.getFileSystem() == DEFAULT_FS ? path.toFile().exists() : Files.exists(path);
}
private static String getFilename(ResourceType type, Identifier id) {
return String.format(Locale.ROOT, "%s/%s/%s", type.getDirectory(), id.getNamespace(), id.getPath());
}
}