2020-07-22 06:23:34 +01:00
|
|
|
package net.minecraft.server.packs;
|
|
|
|
|
|
|
|
import net.minecraft.Util;
|
|
|
|
import com.google.common.collect.Maps;
|
|
|
|
import org.apache.logging.log4j.LogManager;
|
|
|
|
import java.nio.file.FileSystemNotFoundException;
|
|
|
|
import java.util.Collections;
|
|
|
|
import java.nio.file.FileSystems;
|
|
|
|
import java.util.HashMap;
|
|
|
|
import net.minecraft.server.packs.metadata.MetadataSectionSerializer;
|
|
|
|
import java.io.File;
|
|
|
|
import javax.annotation.Nullable;
|
2020-07-22 06:25:47 +01:00
|
|
|
import java.util.stream.Stream;
|
|
|
|
import java.util.function.Consumer;
|
2020-07-22 06:23:34 +01:00
|
|
|
import java.nio.file.FileVisitOption;
|
|
|
|
import java.net.URI;
|
|
|
|
import java.util.Enumeration;
|
|
|
|
import java.nio.file.NoSuchFileException;
|
|
|
|
import java.net.URISyntaxException;
|
|
|
|
import java.nio.file.Paths;
|
|
|
|
import java.net.URL;
|
|
|
|
import com.google.common.collect.Sets;
|
|
|
|
import java.util.Collection;
|
|
|
|
import java.util.function.Predicate;
|
|
|
|
import java.io.FileNotFoundException;
|
|
|
|
import net.minecraft.resources.ResourceLocation;
|
|
|
|
import java.io.IOException;
|
|
|
|
import java.nio.file.OpenOption;
|
|
|
|
import java.nio.file.Files;
|
|
|
|
import java.nio.file.LinkOption;
|
|
|
|
import java.io.InputStream;
|
|
|
|
import com.google.common.collect.ImmutableSet;
|
|
|
|
import java.util.Set;
|
|
|
|
import java.nio.file.FileSystem;
|
|
|
|
import java.util.Map;
|
|
|
|
import org.apache.logging.log4j.Logger;
|
|
|
|
import java.nio.file.Path;
|
|
|
|
|
|
|
|
public class VanillaPack implements Pack {
|
|
|
|
public static Path generatedDir;
|
|
|
|
private static final Logger LOGGER;
|
|
|
|
public static Class<?> clientObject;
|
|
|
|
private static final Map<PackType, FileSystem> JAR_FILESYSTEM_BY_TYPE;
|
|
|
|
public final Set<String> namespaces;
|
|
|
|
|
|
|
|
public VanillaPack(final String... arr) {
|
|
|
|
this.namespaces = ImmutableSet.<String>copyOf(arr);
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public InputStream getRootResource(final String string) throws IOException {
|
|
|
|
if (string.contains("/") || string.contains("\\")) {
|
|
|
|
throw new IllegalArgumentException("Root resources can only be filenames, not paths (no / allowed!)");
|
|
|
|
}
|
|
|
|
if (VanillaPack.generatedDir != null) {
|
|
|
|
final Path path3 = VanillaPack.generatedDir.resolve(string);
|
|
|
|
if (Files.exists(path3)) {
|
|
|
|
return Files.newInputStream(path3);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return this.getResourceAsStream(string);
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
2020-07-22 06:25:47 +01:00
|
|
|
public InputStream getResource(final PackType yf, final ResourceLocation sm) throws IOException {
|
|
|
|
final InputStream inputStream4 = this.getResourceAsStream(yf, sm);
|
2020-07-22 06:23:34 +01:00
|
|
|
if (inputStream4 != null) {
|
|
|
|
return inputStream4;
|
|
|
|
}
|
2020-07-22 06:25:47 +01:00
|
|
|
throw new FileNotFoundException(sm.getPath());
|
2020-07-22 06:23:34 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
2020-07-22 06:25:47 +01:00
|
|
|
public Collection<ResourceLocation> getResources(final PackType yf, final String string2, final String string3, final int integer, final Predicate<String> predicate) {
|
|
|
|
final Set<ResourceLocation> set7 = Sets.newHashSet();
|
2020-07-22 06:23:34 +01:00
|
|
|
if (VanillaPack.generatedDir != null) {
|
|
|
|
try {
|
2020-07-22 06:25:47 +01:00
|
|
|
getResources(set7, integer, string2, VanillaPack.generatedDir.resolve(yf.getDirectory()), string3, predicate);
|
2020-07-22 06:23:34 +01:00
|
|
|
}
|
|
|
|
catch (IOException ex2) {}
|
2020-07-22 06:25:47 +01:00
|
|
|
if (yf == PackType.CLIENT_RESOURCES) {
|
|
|
|
Enumeration<URL> enumeration8 = null;
|
2020-07-22 06:23:34 +01:00
|
|
|
try {
|
2020-07-22 06:25:47 +01:00
|
|
|
enumeration8 = VanillaPack.clientObject.getClassLoader().getResources(yf.getDirectory() + "/");
|
2020-07-22 06:23:34 +01:00
|
|
|
}
|
|
|
|
catch (IOException ex3) {}
|
2020-07-22 06:25:47 +01:00
|
|
|
while (enumeration8 != null && enumeration8.hasMoreElements()) {
|
2020-07-22 06:23:34 +01:00
|
|
|
try {
|
2020-07-22 06:25:47 +01:00
|
|
|
final URI uRI9 = enumeration8.nextElement().toURI();
|
|
|
|
if (!"file".equals(uRI9.getScheme())) {
|
2020-07-22 06:23:34 +01:00
|
|
|
continue;
|
|
|
|
}
|
2020-07-22 06:25:47 +01:00
|
|
|
getResources(set7, integer, string2, Paths.get(uRI9), string3, predicate);
|
2020-07-22 06:23:34 +01:00
|
|
|
}
|
|
|
|
catch (URISyntaxException | IOException ex4) {}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
try {
|
2020-07-22 06:25:47 +01:00
|
|
|
final URL uRL8 = VanillaPack.class.getResource("/" + yf.getDirectory() + "/.mcassetsroot");
|
|
|
|
if (uRL8 == null) {
|
2020-07-22 06:23:34 +01:00
|
|
|
VanillaPack.LOGGER.error("Couldn't find .mcassetsroot, cannot load vanilla resources");
|
2020-07-22 06:25:47 +01:00
|
|
|
return set7;
|
2020-07-22 06:23:34 +01:00
|
|
|
}
|
2020-07-22 06:25:47 +01:00
|
|
|
final URI uRI9 = uRL8.toURI();
|
|
|
|
if ("file".equals(uRI9.getScheme())) {
|
|
|
|
final URL uRL9 = new URL(uRL8.toString().substring(0, uRL8.toString().length() - ".mcassetsroot".length()));
|
|
|
|
final Path path11 = Paths.get(uRL9.toURI());
|
|
|
|
getResources(set7, integer, string2, path11, string3, predicate);
|
2020-07-22 06:23:34 +01:00
|
|
|
}
|
2020-07-22 06:25:47 +01:00
|
|
|
else if ("jar".equals(uRI9.getScheme())) {
|
|
|
|
final Path path12 = VanillaPack.JAR_FILESYSTEM_BY_TYPE.get(yf).getPath("/" + yf.getDirectory());
|
|
|
|
getResources(set7, integer, "minecraft", path12, string3, predicate);
|
2020-07-22 06:23:34 +01:00
|
|
|
}
|
|
|
|
else {
|
2020-07-22 06:25:47 +01:00
|
|
|
VanillaPack.LOGGER.error("Unsupported scheme {} trying to list vanilla resources (NYI?)", uRI9);
|
2020-07-22 06:23:34 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
catch (FileNotFoundException | NoSuchFileException ex5) {}
|
|
|
|
catch (URISyntaxException | IOException ex6) {
|
|
|
|
final Exception ex;
|
2020-07-22 06:25:47 +01:00
|
|
|
final Exception exception8 = ex;
|
|
|
|
VanillaPack.LOGGER.error("Couldn't get a list of all vanilla resources", (Throwable)exception8);
|
2020-07-22 06:23:34 +01:00
|
|
|
}
|
2020-07-22 06:25:47 +01:00
|
|
|
return set7;
|
2020-07-22 06:23:34 +01:00
|
|
|
}
|
|
|
|
|
2020-07-22 06:25:47 +01:00
|
|
|
private static void getResources(final Collection<ResourceLocation> collection, final int integer, final String string3, final Path path, final String string5, final Predicate<String> predicate) throws IOException {
|
|
|
|
final Path path4 = path.resolve(string3);
|
|
|
|
try (final Stream<Path> stream8 = Files.walk(path4.resolve(string5), integer)) {
|
|
|
|
stream8.filter(path -> !path.endsWith(".mcmeta") && Files.isRegularFile(path) && predicate.test(path.getFileName().toString())).map(path3 -> new ResourceLocation(string3, path4.relativize(path3).toString().replaceAll("\\\\", "/"))).forEach(collection::add);
|
2020-07-22 06:23:34 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
@Nullable
|
2020-07-22 06:25:47 +01:00
|
|
|
protected InputStream getResourceAsStream(final PackType yf, final ResourceLocation sm) {
|
|
|
|
final String string4 = createPath(yf, sm);
|
2020-07-22 06:23:34 +01:00
|
|
|
if (VanillaPack.generatedDir != null) {
|
2020-07-22 06:25:47 +01:00
|
|
|
final Path path5 = VanillaPack.generatedDir.resolve(yf.getDirectory() + "/" + sm.getNamespace() + "/" + sm.getPath());
|
2020-07-22 06:23:34 +01:00
|
|
|
if (Files.exists(path5)) {
|
|
|
|
try {
|
|
|
|
return Files.newInputStream(path5);
|
|
|
|
}
|
|
|
|
catch (IOException ex) {}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
try {
|
|
|
|
final URL uRL5 = VanillaPack.class.getResource(string4);
|
|
|
|
if (isResourceUrlValid(string4, uRL5)) {
|
|
|
|
return uRL5.openStream();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
catch (IOException iOException5) {
|
|
|
|
return VanillaPack.class.getResourceAsStream(string4);
|
|
|
|
}
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
|
2020-07-22 06:25:47 +01:00
|
|
|
private static String createPath(final PackType yf, final ResourceLocation sm) {
|
|
|
|
return "/" + yf.getDirectory() + "/" + sm.getNamespace() + "/" + sm.getPath();
|
2020-07-22 06:23:34 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
private static boolean isResourceUrlValid(final String string, @Nullable final URL uRL) throws IOException {
|
|
|
|
return uRL != null && (uRL.getProtocol().equals("jar") || FolderResourcePack.validatePath(new File(uRL.getFile()), string));
|
|
|
|
}
|
|
|
|
|
|
|
|
@Nullable
|
|
|
|
protected InputStream getResourceAsStream(final String string) {
|
|
|
|
return VanillaPack.class.getResourceAsStream("/" + string);
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
2020-07-22 06:25:47 +01:00
|
|
|
public boolean hasResource(final PackType yf, final ResourceLocation sm) {
|
|
|
|
final String string4 = createPath(yf, sm);
|
2020-07-22 06:23:34 +01:00
|
|
|
if (VanillaPack.generatedDir != null) {
|
2020-07-22 06:25:47 +01:00
|
|
|
final Path path5 = VanillaPack.generatedDir.resolve(yf.getDirectory() + "/" + sm.getNamespace() + "/" + sm.getPath());
|
2020-07-22 06:23:34 +01:00
|
|
|
if (Files.exists(path5)) {
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
try {
|
|
|
|
final URL uRL5 = VanillaPack.class.getResource(string4);
|
|
|
|
return isResourceUrlValid(string4, uRL5);
|
|
|
|
}
|
|
|
|
catch (IOException ex) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
2020-07-22 06:25:47 +01:00
|
|
|
public Set<String> getNamespaces(final PackType yf) {
|
2020-07-22 06:23:34 +01:00
|
|
|
return this.namespaces;
|
|
|
|
}
|
|
|
|
|
|
|
|
@Nullable
|
|
|
|
@Override
|
2020-07-22 06:25:47 +01:00
|
|
|
public <T> T getMetadataSection(final MetadataSectionSerializer<T> yi) throws IOException {
|
2020-07-22 06:23:34 +01:00
|
|
|
try (final InputStream inputStream3 = this.getRootResource("pack.mcmeta")) {
|
2020-07-22 06:25:47 +01:00
|
|
|
return AbstractResourcePack.<T>getMetadataFromStream(yi, inputStream3);
|
2020-07-22 06:23:34 +01:00
|
|
|
}
|
|
|
|
catch (RuntimeException | FileNotFoundException ex2) {
|
|
|
|
final Exception ex;
|
|
|
|
final Exception exception3 = ex;
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public String getName() {
|
|
|
|
return "Default";
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public void close() {
|
|
|
|
}
|
|
|
|
|
|
|
|
static {
|
|
|
|
LOGGER = LogManager.getLogger();
|
|
|
|
final PackType[] array;
|
|
|
|
int length;
|
|
|
|
int i = 0;
|
2020-07-22 06:25:47 +01:00
|
|
|
PackType yf6;
|
2020-07-22 06:23:34 +01:00
|
|
|
URL uRL7;
|
|
|
|
URI uRI8;
|
|
|
|
FileSystem fileSystem9;
|
|
|
|
final Exception ex;
|
|
|
|
Exception exception8;
|
|
|
|
JAR_FILESYSTEM_BY_TYPE = Util.<Map<PackType, FileSystem>>make(Maps.newHashMap(), hashMap -> {
|
|
|
|
synchronized (VanillaPack.class) {
|
|
|
|
PackType.values();
|
|
|
|
for (length = array.length; i < length; ++i) {
|
2020-07-22 06:25:47 +01:00
|
|
|
yf6 = array[i];
|
|
|
|
uRL7 = VanillaPack.class.getResource("/" + yf6.getDirectory() + "/.mcassetsroot");
|
2020-07-22 06:23:34 +01:00
|
|
|
try {
|
|
|
|
uRI8 = uRL7.toURI();
|
|
|
|
if ("jar".equals(uRI8.getScheme())) {
|
|
|
|
try {
|
|
|
|
fileSystem9 = FileSystems.getFileSystem(uRI8);
|
|
|
|
}
|
|
|
|
catch (FileSystemNotFoundException fileSystemNotFoundException10) {
|
|
|
|
fileSystem9 = FileSystems.newFileSystem(uRI8, Collections.emptyMap());
|
|
|
|
}
|
2020-07-22 06:25:47 +01:00
|
|
|
hashMap.put(yf6, fileSystem9);
|
2020-07-22 06:23:34 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
catch (URISyntaxException | IOException ex2) {
|
|
|
|
exception8 = ex;
|
|
|
|
VanillaPack.LOGGER.error("Couldn't get a list of all vanilla resources", (Throwable)exception8);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
});
|
|
|
|
}
|
|
|
|
}
|