mirror of
https://github.com/MinecraftForge/MinecraftForge.git
synced 2026-11-18 03:25:18 +00:00
Support pack overlay system. Closes #9818
This commit is contained in:
11 files changed
+265
-365
No files matched your search
@@ -22,18 +22,21 @@ import net.minecraft.server.packs.PackResources;
|
||||
import net.minecraft.server.packs.PackType;
|
||||
import net.minecraft.server.packs.metadata.MetadataSectionSerializer;
|
||||
import net.minecraft.server.packs.metadata.pack.PackMetadataSection;
|
||||
import net.minecraft.server.packs.repository.Pack;
|
||||
import net.minecraft.server.packs.repository.Pack.Info;
|
||||
import net.minecraft.server.packs.resources.IoSupplier;
|
||||
|
||||
import org.jetbrains.annotations.ApiStatus;
|
||||
import org.jetbrains.annotations.Nullable;
|
||||
|
||||
public class DelegatingPackResources extends AbstractPackResources
|
||||
{
|
||||
@ApiStatus.Internal
|
||||
public class DelegatingPackResources extends AbstractPackResources {
|
||||
private final PackMetadataSection packMeta;
|
||||
private final List<PackResources> delegates;
|
||||
private final Map<String, List<PackResources>> namespacesAssets;
|
||||
private final Map<String, List<PackResources>> namespacesData;
|
||||
|
||||
public DelegatingPackResources(String packId, boolean isBuiltin, PackMetadataSection packMeta, List<? extends PackResources> packs)
|
||||
{
|
||||
public DelegatingPackResources(String packId, boolean isBuiltin, PackMetadataSection packMeta, List<? extends PackResources> packs) {
|
||||
super(packId, isBuiltin);
|
||||
this.packMeta = packMeta;
|
||||
this.delegates = ImmutableList.copyOf(packs);
|
||||
@@ -41,15 +44,11 @@ public class DelegatingPackResources extends AbstractPackResources
|
||||
this.namespacesData = this.buildNamespaceMap(PackType.SERVER_DATA, delegates);
|
||||
}
|
||||
|
||||
private Map<String, List<PackResources>> buildNamespaceMap(PackType type, List<PackResources> packList)
|
||||
{
|
||||
private Map<String, List<PackResources>> buildNamespaceMap(PackType type, List<PackResources> packList) {
|
||||
Map<String, List<PackResources>> map = new HashMap<>();
|
||||
for (PackResources pack : packList)
|
||||
{
|
||||
for (PackResources pack : packList) {
|
||||
for (String namespace : pack.getNamespaces(type))
|
||||
{
|
||||
map.computeIfAbsent(namespace, k -> new ArrayList<>()).add(pack);
|
||||
}
|
||||
}
|
||||
map.replaceAll((k, list) -> ImmutableList.copyOf(list));
|
||||
return ImmutableMap.copyOf(map);
|
||||
@@ -58,49 +57,38 @@ public class DelegatingPackResources extends AbstractPackResources
|
||||
@SuppressWarnings("unchecked")
|
||||
@Nullable
|
||||
@Override
|
||||
public <T> T getMetadataSection(MetadataSectionSerializer<T> deserializer) throws IOException
|
||||
{
|
||||
public <T> T getMetadataSection(MetadataSectionSerializer<T> deserializer) throws IOException {
|
||||
return deserializer.getMetadataSectionName().equals("pack") ? (T) this.packMeta : null;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void listResources(PackType type, String resourceNamespace, String paths, ResourceOutput resourceOutput)
|
||||
{
|
||||
public void listResources(PackType type, String resourceNamespace, String paths, ResourceOutput resourceOutput) {
|
||||
for (PackResources delegate : this.delegates)
|
||||
{
|
||||
delegate.listResources(type, resourceNamespace, paths, resourceOutput);
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public Set<String> getNamespaces(PackType type)
|
||||
{
|
||||
public Set<String> getNamespaces(PackType type) {
|
||||
return type == PackType.CLIENT_RESOURCES ? namespacesAssets.keySet() : namespacesData.keySet();
|
||||
}
|
||||
|
||||
@Override
|
||||
public void close()
|
||||
{
|
||||
public void close() {
|
||||
for (PackResources pack : delegates)
|
||||
{
|
||||
pack.close();
|
||||
}
|
||||
}
|
||||
|
||||
@Nullable
|
||||
@Override
|
||||
public IoSupplier<InputStream> getRootResource(String... paths)
|
||||
{
|
||||
public IoSupplier<InputStream> getRootResource(String... paths) {
|
||||
// Root resources do not make sense here
|
||||
return null;
|
||||
}
|
||||
|
||||
@Nullable
|
||||
@Override
|
||||
public IoSupplier<InputStream> getResource(PackType type, ResourceLocation location)
|
||||
{
|
||||
for (PackResources pack : getCandidatePacks(type, location))
|
||||
{
|
||||
public IoSupplier<InputStream> getResource(PackType type, ResourceLocation location) {
|
||||
for (PackResources pack : getCandidatePacks(type, location)) {
|
||||
IoSupplier<InputStream> ioSupplier = pack.getResource(type, location);
|
||||
if (ioSupplier != null)
|
||||
return ioSupplier;
|
||||
@@ -110,15 +98,29 @@ public class DelegatingPackResources extends AbstractPackResources
|
||||
}
|
||||
|
||||
@Nullable
|
||||
public Collection<PackResources> getChildren()
|
||||
{
|
||||
public Collection<PackResources> getChildren() {
|
||||
return delegates;
|
||||
}
|
||||
|
||||
private List<PackResources> getCandidatePacks(PackType type, ResourceLocation location)
|
||||
{
|
||||
private List<PackResources> getCandidatePacks(PackType type, ResourceLocation location) {
|
||||
Map<String, List<PackResources>> map = type == PackType.CLIENT_RESOURCES ? namespacesAssets : namespacesData;
|
||||
List<PackResources> packsWithNamespace = map.get(location.getNamespace());
|
||||
return packsWithNamespace == null ? Collections.emptyList() : packsWithNamespace;
|
||||
}
|
||||
|
||||
public Pack.ResourcesSupplier supplier() {
|
||||
return new Supplier();
|
||||
}
|
||||
|
||||
private class Supplier implements Pack.ResourcesSupplier {
|
||||
@Override
|
||||
public PackResources openPrimary(String path) {
|
||||
return DelegatingPackResources.this;
|
||||
}
|
||||
|
||||
@Override
|
||||
public PackResources openFull(String path, Info info) {
|
||||
return DelegatingPackResources.this;
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -5,14 +5,12 @@
|
||||
|
||||
package net.minecraftforge.resource;
|
||||
|
||||
import com.google.common.base.Joiner;
|
||||
import com.mojang.logging.LogUtils;
|
||||
import java.io.IOException;
|
||||
import java.io.InputStream;
|
||||
import java.nio.file.Files;
|
||||
import java.nio.file.Path;
|
||||
import java.util.Collections;
|
||||
import java.util.List;
|
||||
import java.util.Locale;
|
||||
import java.util.Set;
|
||||
import java.util.stream.Collectors;
|
||||
@@ -31,10 +29,9 @@ import org.slf4j.Logger;
|
||||
* <p>
|
||||
* This is primarily intended to support including optional resource packs inside a mod,
|
||||
* such as to have alternative textures to use along with Programmer Art, or optional
|
||||
* alternative recipes for compatibility ot to replace vanilla recipes.
|
||||
* alternative recipes for compatibility to to replace vanilla recipes.
|
||||
*/
|
||||
public class PathPackResources extends AbstractPackResources
|
||||
{
|
||||
public class PathPackResources extends AbstractPackResources {
|
||||
private static final Logger LOGGER = LogUtils.getLogger();
|
||||
private final Path source;
|
||||
|
||||
@@ -46,8 +43,7 @@ public class PathPackResources extends AbstractPackResources
|
||||
* @param isBuiltin whether this pack resources should be considered builtin
|
||||
* @param source the root path of the pack. This needs to point to the folder that contains "assets" and/or "data", not the asset folder itself!
|
||||
*/
|
||||
public PathPackResources(String packId, boolean isBuiltin, final Path source)
|
||||
{
|
||||
public PathPackResources(String packId, boolean isBuiltin, final Path source) {
|
||||
super(packId, isBuiltin);
|
||||
this.source = source;
|
||||
}
|
||||
@@ -58,8 +54,7 @@ public class PathPackResources extends AbstractPackResources
|
||||
*
|
||||
* @return the root path of the resources.
|
||||
*/
|
||||
public Path getSource()
|
||||
{
|
||||
public Path getSource() {
|
||||
return this.source;
|
||||
}
|
||||
|
||||
@@ -69,8 +64,7 @@ public class PathPackResources extends AbstractPackResources
|
||||
* @param paths One or more path strings to resolve. Can include slash-separated paths.
|
||||
* @return the resulting path, which may not exist.
|
||||
*/
|
||||
protected Path resolve(String... paths)
|
||||
{
|
||||
protected Path resolve(String... paths) {
|
||||
Path path = getSource();
|
||||
for (String name : paths)
|
||||
path = path.resolve(name);
|
||||
@@ -79,8 +73,7 @@ public class PathPackResources extends AbstractPackResources
|
||||
|
||||
@Nullable
|
||||
@Override
|
||||
public IoSupplier<InputStream> getRootResource(String... paths)
|
||||
{
|
||||
public IoSupplier<InputStream> getRootResource(String... paths) {
|
||||
final Path path = resolve(paths);
|
||||
if (!Files.exists(path))
|
||||
return null;
|
||||
@@ -89,27 +82,22 @@ public class PathPackResources extends AbstractPackResources
|
||||
}
|
||||
|
||||
@Override
|
||||
public void listResources(PackType type, String namespace, String path, ResourceOutput resourceOutput)
|
||||
{
|
||||
public void listResources(PackType type, String namespace, String path, ResourceOutput resourceOutput) {
|
||||
FileUtil.decomposePath(path).get()
|
||||
.ifLeft(parts -> net.minecraft.server.packs.PathPackResources.listPath(namespace, resolve(type.getDirectory(), namespace).toAbsolutePath(), parts, resourceOutput))
|
||||
.ifRight(dataResult -> LOGGER.error("Invalid path {}: {}", path, dataResult.message()));
|
||||
}
|
||||
|
||||
@Override
|
||||
public Set<String> getNamespaces(PackType type)
|
||||
{
|
||||
public Set<String> getNamespaces(PackType type) {
|
||||
return getNamespacesFromDisk(type);
|
||||
}
|
||||
|
||||
@NotNull
|
||||
private Set<String> getNamespacesFromDisk(final PackType type)
|
||||
{
|
||||
try
|
||||
{
|
||||
private Set<String> getNamespacesFromDisk(final PackType type) {
|
||||
try {
|
||||
Path root = resolve(type.getDirectory());
|
||||
try (Stream<Path> walker = Files.walk(root, 1))
|
||||
{
|
||||
try (Stream<Path> walker = Files.walk(root, 1)) {
|
||||
return walker
|
||||
.filter(Files::isDirectory)
|
||||
.map(root::relativize)
|
||||
@@ -118,28 +106,20 @@ public class PathPackResources extends AbstractPackResources
|
||||
.filter(s -> !s.isEmpty()) // Filter empty strings, otherwise empty strings default to minecraft namespace in ResourceLocations
|
||||
.collect(Collectors.toSet());
|
||||
}
|
||||
}
|
||||
catch (IOException e)
|
||||
{
|
||||
} catch (IOException e) {
|
||||
if (type == PackType.SERVER_DATA) // We still have to add the resource namespace if client resources exist, as we load langs (which are in assets) on server
|
||||
{
|
||||
return this.getNamespaces(PackType.CLIENT_RESOURCES);
|
||||
}
|
||||
else
|
||||
{
|
||||
return Collections.emptySet();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public IoSupplier<InputStream> getResource(PackType type, ResourceLocation location)
|
||||
{
|
||||
public IoSupplier<InputStream> getResource(PackType type, ResourceLocation location) {
|
||||
return this.getRootResource(getPathFromLocation(location.getPath().startsWith("lang/") ? PackType.CLIENT_RESOURCES : type, location));
|
||||
}
|
||||
|
||||
private static String[] getPathFromLocation(PackType type, ResourceLocation location)
|
||||
{
|
||||
private static String[] getPathFromLocation(PackType type, ResourceLocation location) {
|
||||
String[] parts = location.getPath().split("/");
|
||||
String[] result = new String[parts.length + 2];
|
||||
result[0] = type.getDirectory();
|
||||
@@ -149,13 +129,10 @@ public class PathPackResources extends AbstractPackResources
|
||||
}
|
||||
|
||||
@Override
|
||||
public void close()
|
||||
{
|
||||
}
|
||||
public void close() { }
|
||||
|
||||
@Override
|
||||
public String toString()
|
||||
{
|
||||
public String toString() {
|
||||
return String.format(Locale.ROOT, "%s: %s (%s)", getClass().getName(), this.packId(), getSource());
|
||||
}
|
||||
}
|
||||
@@ -8,62 +8,97 @@ package net.minecraftforge.resource;
|
||||
import java.nio.file.Path;
|
||||
import java.util.ArrayList;
|
||||
import java.util.Comparator;
|
||||
import java.util.LinkedHashMap;
|
||||
import java.util.List;
|
||||
import java.util.Locale;
|
||||
import java.util.Map;
|
||||
import java.util.Optional;
|
||||
import java.util.function.BiConsumer;
|
||||
import java.util.function.BiFunction;
|
||||
import java.util.function.Function;
|
||||
import java.util.function.Consumer;
|
||||
import java.util.stream.Collectors;
|
||||
|
||||
import com.mojang.datafixers.util.Pair;
|
||||
import it.unimi.dsi.fastutil.objects.Object2IntMap;
|
||||
import it.unimi.dsi.fastutil.objects.Object2IntOpenHashMap;
|
||||
import net.minecraft.server.packs.repository.RepositorySource;
|
||||
import net.minecraft.server.packs.repository.Pack.Info;
|
||||
import net.minecraft.SharedConstants;
|
||||
import net.minecraft.network.chat.Component;
|
||||
import net.minecraft.server.packs.CompositePackResources;
|
||||
import net.minecraft.server.packs.PackResources;
|
||||
import net.minecraft.server.packs.PackType;
|
||||
import net.minecraft.server.packs.metadata.pack.PackMetadataSection;
|
||||
import net.minecraft.server.packs.repository.Pack;
|
||||
import net.minecraft.server.packs.repository.PackRepository;
|
||||
import net.minecraft.server.packs.repository.PackSource;
|
||||
import net.minecraftforge.fml.Logging;
|
||||
import net.minecraftforge.fml.ModList;
|
||||
import net.minecraftforge.fml.ModLoader;
|
||||
import net.minecraftforge.fml.ModLoadingStage;
|
||||
import net.minecraftforge.fml.ModLoadingWarning;
|
||||
import net.minecraftforge.forgespi.language.IModFileInfo;
|
||||
import net.minecraftforge.forgespi.locating.IModFile;
|
||||
|
||||
import org.apache.logging.log4j.LogManager;
|
||||
import org.apache.logging.log4j.Logger;
|
||||
import org.jetbrains.annotations.ApiStatus;
|
||||
import org.jetbrains.annotations.NotNull;
|
||||
|
||||
@ApiStatus.Internal
|
||||
public class ResourcePackLoader {
|
||||
private static Map<IModFile, PathPackResources> modResourcePacks;
|
||||
private static final Logger LOGGER = LogManager.getLogger();
|
||||
|
||||
public static Optional<PathPackResources> getPackFor(String modId) {
|
||||
return Optional.ofNullable(ModList.get().getModFileById(modId)).
|
||||
map(IModFileInfo::getFile).map(mf->modResourcePacks.get(mf));
|
||||
}
|
||||
|
||||
@Deprecated
|
||||
public static void loadResourcePacks(PackRepository resourcePacks, BiFunction<Map<IModFile, ? extends PathPackResources>, BiConsumer<? super PathPackResources, Pack>, ? extends RepositorySource> packFinder) {
|
||||
loadResourcePacks(resourcePacks, (map) -> packFinder.apply(map, (rp,p) -> {}));
|
||||
}
|
||||
|
||||
public static void loadResourcePacks(PackRepository resourcePacks, Function<Map<IModFile, ? extends PathPackResources>, ? extends RepositorySource> packFinder) {
|
||||
modResourcePacks = ModList.get().getModFiles().stream()
|
||||
.filter(mf->mf.requiredLanguageLoaders().stream().noneMatch(ls->ls.languageName().equals("minecraft")))
|
||||
.map(mf -> Pair.of(mf, createPackForMod(mf)))
|
||||
.collect(Collectors.toMap(p -> p.getFirst().getFile(), Pair::getSecond, (u,v) -> { throw new IllegalStateException(String.format(Locale.ENGLISH, "Duplicate key %s", u)); }, LinkedHashMap::new));
|
||||
resourcePacks.addPackFinder(packFinder.apply(modResourcePacks));
|
||||
public static void loadResourcePacks(PackRepository resourcePacks, boolean client) {
|
||||
resourcePacks.addPackFinder(repo -> findPacks(repo, client));
|
||||
}
|
||||
|
||||
@NotNull
|
||||
public static PathPackResources createPackForMod(IModFileInfo mf)
|
||||
{
|
||||
return new PathPackResources(mf.getFile().getFileName(), true, mf.getFile().getFilePath())
|
||||
{
|
||||
private final IModFile modFile = mf.getFile();
|
||||
public static PathPackResources createPackForMod(IModFileInfo mf) {
|
||||
return new ModPathPackResources(mf.getFile());
|
||||
}
|
||||
|
||||
@NotNull
|
||||
@Override
|
||||
protected Path resolve(@NotNull String... paths)
|
||||
{
|
||||
return this.modFile.findResource(paths);
|
||||
private static class ModPathPackResources extends PathPackResources {
|
||||
private final IModFile mod;
|
||||
private final String[] prefix;
|
||||
|
||||
private ModPathPackResources(IModFile mod, String... prefix) {
|
||||
super(mod.getFileName(), true, mod.getFilePath());
|
||||
this.mod = mod;
|
||||
this.prefix = prefix;
|
||||
}
|
||||
|
||||
@NotNull
|
||||
@Override
|
||||
protected Path resolve(@NotNull String... paths) {
|
||||
if (prefix != null && prefix.length > 0) {
|
||||
var tmp = new String[prefix.length + paths.length];
|
||||
System.arraycopy(prefix, 0, tmp, 0, prefix.length);
|
||||
System.arraycopy(paths, 0, tmp, prefix.length, paths.length);
|
||||
return this.mod.findResource(tmp);
|
||||
}
|
||||
};
|
||||
return this.mod.findResource(paths);
|
||||
}
|
||||
|
||||
private static class Supplier implements Pack.ResourcesSupplier {
|
||||
private final IModFile mod;
|
||||
|
||||
private Supplier(IModFile mod) {
|
||||
this.mod = mod;
|
||||
}
|
||||
|
||||
@Override
|
||||
public PackResources openPrimary(String packId) {
|
||||
return new ModPathPackResources(mod);
|
||||
}
|
||||
|
||||
@Override
|
||||
public PackResources openFull(String packId, Info info) {
|
||||
var primary = openPrimary(packId);
|
||||
if (info.overlays().isEmpty())
|
||||
return primary;
|
||||
|
||||
var lst = new ArrayList<PackResources>(info.overlays().size());
|
||||
for (var overlay : info.overlays())
|
||||
lst.add(new ModPathPackResources(mod, overlay));
|
||||
|
||||
return new CompositePackResources(primary, lst);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
public static List<String> getPackNames() {
|
||||
@@ -99,4 +134,55 @@ public class ResourcePackLoader {
|
||||
};
|
||||
}
|
||||
|
||||
private static void findPacks(Consumer<Pack> packAcceptor, boolean client) {
|
||||
var type = client ? PackType.CLIENT_RESOURCES : PackType.SERVER_DATA;
|
||||
var version = SharedConstants.getCurrentVersion().getPackVersion(type);
|
||||
var hiddenPacks = new ArrayList<PackResources>();
|
||||
|
||||
for (var mod : ModList.get().getModFiles()) {
|
||||
if (mod.requiredLanguageLoaders().stream().anyMatch(ls -> ls.languageName().equals("minecraft")))
|
||||
continue;
|
||||
|
||||
var file = mod.getFile();
|
||||
|
||||
var supplier = new ModPathPackResources.Supplier(file);
|
||||
|
||||
var modinfo = file.getModInfos().get(0);
|
||||
var name = "mod:" + modinfo.getModId();
|
||||
var meta = Pack.readPackInfo(name, supplier, version);
|
||||
Pack pack = null;
|
||||
if (meta != null)
|
||||
pack = Pack.create(name, Component.literal(file.getFileName()), false, supplier, meta, Pack.Position.BOTTOM, false, PackSource.DEFAULT);
|
||||
|
||||
if (pack == null) {
|
||||
// Vanilla only logs an error, instead of propagating, so handle null and warn that something went wrong
|
||||
ModLoader.get().addWarning(new ModLoadingWarning(modinfo, ModLoadingStage.ERROR, "fml.modloading.brokenresources", file));
|
||||
continue;
|
||||
}
|
||||
|
||||
LOGGER.debug(Logging.CORE, "Generating PackInfo named {} for mod file {}", name, file.getFilePath());
|
||||
if (!client || mod.showAsResourcePack())
|
||||
packAcceptor.accept(pack);
|
||||
else
|
||||
hiddenPacks.add(pack.open());
|
||||
}
|
||||
|
||||
if (!hiddenPacks.isEmpty()) {
|
||||
@SuppressWarnings("resource")
|
||||
var delegating = new DelegatingPackResources("mod_resources", false,
|
||||
new PackMetadataSection(
|
||||
Component.translatable("fml.resources.modresources", hiddenPacks.size()),
|
||||
version,
|
||||
Optional.empty()
|
||||
),
|
||||
hiddenPacks
|
||||
);
|
||||
|
||||
var supplier = delegating.supplier();
|
||||
|
||||
// Create a resource pack merging all mod resources that should be hidden
|
||||
var modResourcesPack = Pack.readMetaAndCreate("mod_resources", Component.literal("Mod Resources"), true, supplier, type, Pack.Position.BOTTOM, PackSource.DEFAULT);
|
||||
packAcceptor.accept(modResourcesPack);
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user