mirror of
https://github.com/FabricMC/fabric.git
synced 2025-04-21 03:10:54 -04:00
ModNioResourcePack: don't extend AbstractFileResourcePack (#2509)
This commit is contained in:
parent
0b73465810
commit
03bf91853d
1 changed files with 39 additions and 10 deletions
|
@ -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());
|
||||
}
|
||||
}
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue