From 25622760611bbf98bc3744d927345697538c11f7 Mon Sep 17 00:00:00 2001 From: OroArmor Date: Mon, 2 Sep 2024 16:41:02 -0700 Subject: [PATCH] Add core:resource_loader --- fabric-loot-api-v3/build.gradle | 4 + .../fabricmc/fabric/impl/loot/LootUtil.java | 6 +- fabric-resource-loader-v0/build.gradle | 4 + .../client/ClientDataPackManagerMixin.java | 55 --- .../loader/client/CreateWorldScreenMixin.java | 67 ---- .../loader/client/GameOptionsMixin.java | 136 -------- .../client/GameOptionsWriteVisitorMixin.java | 63 ---- .../client/ResourcePackOrganizerMixin.java | 61 ---- .../VanillaResourcePackProviderMixin.java | 42 --- ...bric-resource-loader-v0.client.mixins.json | 8 +- .../IdentifiableResourceReloadListener.java | 10 +- .../fabric/api/resource/ModResourcePack.java | 3 + .../api/resource/ResourceManagerHelper.java | 22 +- .../resource/ResourcePackActivationType.java | 12 + .../resource/ResourceReloadListenerKeys.java | 3 + .../SimpleResourceReloadListener.java | 3 + ...mpleSynchronousResourceReloadListener.java | 3 + .../loader/BuiltinModResourcePackSource.java | 33 -- .../FabricLifecycledResourceManager.java | 23 -- .../FabricOriginalKnownPacksGetter.java | 28 -- .../loader/FabricResourcePackProfile.java | 47 --- .../resource/loader/ModNioResourcePack.java | 319 ------------------ .../loader/ModResourcePackCreator.java | 157 --------- .../loader/ModResourcePackFactory.java | 49 --- .../resource/loader/ModResourcePackUtil.java | 247 -------------- .../loader/PlaceholderResourcePack.java | 116 ------- .../loader/ResourceManagerHelperImpl.java | 267 +-------------- .../resource/loader/ServerLanguageUtil.java | 56 --- .../resource/loader/DatapackCommandMixin.java | 68 ---- .../LifecycledResourceManagerImplMixin.java | 47 --- .../resource/loader/MinecraftServerMixin.java | 79 ----- .../loader/ResourcePackManagerMixin.java | 106 ------ .../loader/ResourcePackProfileMixin.java | 27 +- .../SelectKnownPacksC2SPacketMixin.java | 33 -- ...erverConfigurationNetworkHandlerMixin.java | 49 --- .../loader/ServerPropertiesHandlerMixin.java | 34 -- .../loader/SimpleResourceReloadMixin.java | 67 ---- .../SynchronizeRegistriesTaskMixin.java | 73 ---- .../resource/loader/TestServerMixin.java | 39 --- .../resource/loader/server/LanguageMixin.java | 69 ---- .../fabric-resource-loader-v0.mixins.json | 15 +- .../loader/unit/ModResourcePackUtilTests.java | 255 -------------- 42 files changed, 69 insertions(+), 2736 deletions(-) delete mode 100644 fabric-resource-loader-v0/src/client/java/net/fabricmc/fabric/mixin/resource/loader/client/ClientDataPackManagerMixin.java delete mode 100644 fabric-resource-loader-v0/src/client/java/net/fabricmc/fabric/mixin/resource/loader/client/CreateWorldScreenMixin.java delete mode 100644 fabric-resource-loader-v0/src/client/java/net/fabricmc/fabric/mixin/resource/loader/client/GameOptionsMixin.java delete mode 100644 fabric-resource-loader-v0/src/client/java/net/fabricmc/fabric/mixin/resource/loader/client/GameOptionsWriteVisitorMixin.java delete mode 100644 fabric-resource-loader-v0/src/client/java/net/fabricmc/fabric/mixin/resource/loader/client/ResourcePackOrganizerMixin.java delete mode 100644 fabric-resource-loader-v0/src/client/java/net/fabricmc/fabric/mixin/resource/loader/client/VanillaResourcePackProviderMixin.java delete mode 100644 fabric-resource-loader-v0/src/main/java/net/fabricmc/fabric/impl/resource/loader/BuiltinModResourcePackSource.java delete mode 100644 fabric-resource-loader-v0/src/main/java/net/fabricmc/fabric/impl/resource/loader/FabricLifecycledResourceManager.java delete mode 100644 fabric-resource-loader-v0/src/main/java/net/fabricmc/fabric/impl/resource/loader/FabricOriginalKnownPacksGetter.java delete mode 100644 fabric-resource-loader-v0/src/main/java/net/fabricmc/fabric/impl/resource/loader/FabricResourcePackProfile.java delete mode 100644 fabric-resource-loader-v0/src/main/java/net/fabricmc/fabric/impl/resource/loader/ModNioResourcePack.java delete mode 100644 fabric-resource-loader-v0/src/main/java/net/fabricmc/fabric/impl/resource/loader/ModResourcePackCreator.java delete mode 100644 fabric-resource-loader-v0/src/main/java/net/fabricmc/fabric/impl/resource/loader/ModResourcePackFactory.java delete mode 100644 fabric-resource-loader-v0/src/main/java/net/fabricmc/fabric/impl/resource/loader/ModResourcePackUtil.java delete mode 100644 fabric-resource-loader-v0/src/main/java/net/fabricmc/fabric/impl/resource/loader/PlaceholderResourcePack.java delete mode 100644 fabric-resource-loader-v0/src/main/java/net/fabricmc/fabric/impl/resource/loader/ServerLanguageUtil.java delete mode 100644 fabric-resource-loader-v0/src/main/java/net/fabricmc/fabric/mixin/resource/loader/DatapackCommandMixin.java delete mode 100644 fabric-resource-loader-v0/src/main/java/net/fabricmc/fabric/mixin/resource/loader/LifecycledResourceManagerImplMixin.java delete mode 100644 fabric-resource-loader-v0/src/main/java/net/fabricmc/fabric/mixin/resource/loader/MinecraftServerMixin.java delete mode 100644 fabric-resource-loader-v0/src/main/java/net/fabricmc/fabric/mixin/resource/loader/ResourcePackManagerMixin.java delete mode 100644 fabric-resource-loader-v0/src/main/java/net/fabricmc/fabric/mixin/resource/loader/SelectKnownPacksC2SPacketMixin.java delete mode 100644 fabric-resource-loader-v0/src/main/java/net/fabricmc/fabric/mixin/resource/loader/ServerConfigurationNetworkHandlerMixin.java delete mode 100644 fabric-resource-loader-v0/src/main/java/net/fabricmc/fabric/mixin/resource/loader/ServerPropertiesHandlerMixin.java delete mode 100644 fabric-resource-loader-v0/src/main/java/net/fabricmc/fabric/mixin/resource/loader/SimpleResourceReloadMixin.java delete mode 100644 fabric-resource-loader-v0/src/main/java/net/fabricmc/fabric/mixin/resource/loader/SynchronizeRegistriesTaskMixin.java delete mode 100644 fabric-resource-loader-v0/src/main/java/net/fabricmc/fabric/mixin/resource/loader/TestServerMixin.java delete mode 100644 fabric-resource-loader-v0/src/main/java/net/fabricmc/fabric/mixin/resource/loader/server/LanguageMixin.java delete mode 100644 fabric-resource-loader-v0/src/test/java/net/fabricmc/fabric/test/resource/loader/unit/ModResourcePackUtilTests.java diff --git a/fabric-loot-api-v3/build.gradle b/fabric-loot-api-v3/build.gradle index f2db1b51aab..43bc66b6e97 100644 --- a/fabric-loot-api-v3/build.gradle +++ b/fabric-loot-api-v3/build.gradle @@ -4,3 +4,7 @@ moduleDependencies(project, [ 'fabric-api-base', 'fabric-resource-loader-v0' ]) + +dependencies { + modImplementation getQslModule("core", "resource_loader") +} diff --git a/fabric-loot-api-v3/src/main/java/net/fabricmc/fabric/impl/loot/LootUtil.java b/fabric-loot-api-v3/src/main/java/net/fabricmc/fabric/impl/loot/LootUtil.java index 0f4543d4ef4..c670402061d 100644 --- a/fabric-loot-api-v3/src/main/java/net/fabricmc/fabric/impl/loot/LootUtil.java +++ b/fabric-loot-api-v3/src/main/java/net/fabricmc/fabric/impl/loot/LootUtil.java @@ -19,14 +19,14 @@ import java.util.HashMap; import java.util.Map; +import org.quiltmc.qsl.resource.loader.impl.QuiltBuiltinPackProfile; + import net.minecraft.resource.Resource; import net.minecraft.resource.ResourcePackSource; import net.minecraft.util.Identifier; import net.fabricmc.fabric.api.loot.v3.LootTableSource; -import net.fabricmc.fabric.impl.resource.loader.BuiltinModResourcePackSource; import net.fabricmc.fabric.impl.resource.loader.FabricResource; -import net.fabricmc.fabric.impl.resource.loader.ModResourcePackCreator; public final class LootUtil { public static final ThreadLocal> SOURCES = ThreadLocal.withInitial(HashMap::new); @@ -37,7 +37,7 @@ public static LootTableSource determineSource(Resource resource) { if (packSource == ResourcePackSource.BUILTIN) { return LootTableSource.VANILLA; - } else if (packSource == ModResourcePackCreator.RESOURCE_PACK_SOURCE || packSource instanceof BuiltinModResourcePackSource) { + } else if (packSource instanceof QuiltBuiltinPackProfile.BuiltinPackSource) { return LootTableSource.MOD; } } diff --git a/fabric-resource-loader-v0/build.gradle b/fabric-resource-loader-v0/build.gradle index 3c7ab2966e2..6f7f4c3963d 100644 --- a/fabric-resource-loader-v0/build.gradle +++ b/fabric-resource-loader-v0/build.gradle @@ -9,3 +9,7 @@ testDependencies(project, [ ':fabric-api-base', ':fabric-resource-loader-v0' ]) + +dependencies { + modApi getQslModule("core", "resource_loader") +} diff --git a/fabric-resource-loader-v0/src/client/java/net/fabricmc/fabric/mixin/resource/loader/client/ClientDataPackManagerMixin.java b/fabric-resource-loader-v0/src/client/java/net/fabricmc/fabric/mixin/resource/loader/client/ClientDataPackManagerMixin.java deleted file mode 100644 index 28166019c4e..00000000000 --- a/fabric-resource-loader-v0/src/client/java/net/fabricmc/fabric/mixin/resource/loader/client/ClientDataPackManagerMixin.java +++ /dev/null @@ -1,55 +0,0 @@ -/* - * Copyright (c) 2016, 2017, 2018, 2019 FabricMC - * - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ - -package net.fabricmc.fabric.mixin.resource.loader.client; - -import java.util.List; - -import com.llamalad7.mixinextras.injector.ModifyReturnValue; -import org.slf4j.Logger; -import org.slf4j.LoggerFactory; -import org.spongepowered.asm.mixin.Mixin; -import org.spongepowered.asm.mixin.Unique; -import org.spongepowered.asm.mixin.injection.At; -import org.spongepowered.asm.mixin.injection.Redirect; - -import net.minecraft.client.resource.ClientDataPackManager; -import net.minecraft.registry.VersionedIdentifier; -import net.minecraft.resource.ResourcePackManager; - -import net.fabricmc.fabric.impl.resource.loader.ModResourcePackCreator; -import net.fabricmc.fabric.impl.resource.loader.ModResourcePackUtil; - -@Mixin(ClientDataPackManager.class) -public class ClientDataPackManagerMixin { - @Unique - private static final Logger LOGGER = LoggerFactory.getLogger("ClientDataPackManagerMixin"); - - @Redirect(method = "", at = @At(value = "INVOKE", target = "Lnet/minecraft/resource/VanillaDataPackProvider;createClientManager()Lnet/minecraft/resource/ResourcePackManager;")) - public ResourcePackManager createClientManager() { - return ModResourcePackUtil.createClientManager(); - } - - @ModifyReturnValue(method = "getCommonKnownPacks", at = @At("RETURN")) - List getCommonKnownPacksReturn(List original) { - if (original.size() > ModResourcePackCreator.MAX_KNOWN_PACKS) { - LOGGER.warn("Too many knownPacks: Found {}; max {}", original.size(), ModResourcePackCreator.MAX_KNOWN_PACKS); - return original.subList(0, ModResourcePackCreator.MAX_KNOWN_PACKS); - } - - return original; - } -} diff --git a/fabric-resource-loader-v0/src/client/java/net/fabricmc/fabric/mixin/resource/loader/client/CreateWorldScreenMixin.java b/fabric-resource-loader-v0/src/client/java/net/fabricmc/fabric/mixin/resource/loader/client/CreateWorldScreenMixin.java deleted file mode 100644 index 6e528187a16..00000000000 --- a/fabric-resource-loader-v0/src/client/java/net/fabricmc/fabric/mixin/resource/loader/client/CreateWorldScreenMixin.java +++ /dev/null @@ -1,67 +0,0 @@ -/* - * Copyright (c) 2016, 2017, 2018, 2019 FabricMC - * - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ - -package net.fabricmc.fabric.mixin.resource.loader.client; - -import java.io.File; - -import com.mojang.datafixers.util.Pair; -import org.spongepowered.asm.mixin.Mixin; -import org.spongepowered.asm.mixin.Shadow; -import org.spongepowered.asm.mixin.injection.At; -import org.spongepowered.asm.mixin.injection.Inject; -import org.spongepowered.asm.mixin.injection.ModifyVariable; -import org.spongepowered.asm.mixin.injection.Redirect; -import org.spongepowered.asm.mixin.injection.callback.CallbackInfoReturnable; - -import net.minecraft.client.gui.screen.Screen; -import net.minecraft.client.gui.screen.world.CreateWorldScreen; -import net.minecraft.resource.DataConfiguration; -import net.minecraft.resource.ResourcePackManager; -import net.minecraft.resource.ResourceType; - -import net.fabricmc.fabric.impl.resource.loader.ModResourcePackCreator; -import net.fabricmc.fabric.impl.resource.loader.ModResourcePackUtil; - -@Mixin(CreateWorldScreen.class) -public abstract class CreateWorldScreenMixin extends Screen { - @Shadow - private ResourcePackManager packManager; - - private CreateWorldScreenMixin() { - super(null); - } - - @ModifyVariable(method = "create(Lnet/minecraft/client/MinecraftClient;Lnet/minecraft/client/gui/screen/Screen;)V", - at = @At(value = "INVOKE", target = "Lnet/minecraft/client/gui/screen/world/CreateWorldScreen;createServerConfig(Lnet/minecraft/resource/ResourcePackManager;Lnet/minecraft/resource/DataConfiguration;)Lnet/minecraft/server/SaveLoading$ServerConfig;")) - private static ResourcePackManager onCreateResManagerInit(ResourcePackManager manager) { - // Add mod data packs to the initial res pack manager so they are active even if the user doesn't use custom data packs - manager.providers.add(new ModResourcePackCreator(ResourceType.SERVER_DATA)); - return manager; - } - - @Redirect(method = "create(Lnet/minecraft/client/MinecraftClient;Lnet/minecraft/client/gui/screen/Screen;)V", at = @At(value = "FIELD", target = "Lnet/minecraft/resource/DataConfiguration;SAFE_MODE:Lnet/minecraft/resource/DataConfiguration;", ordinal = 0)) - private static DataConfiguration replaceDefaultSettings() { - return ModResourcePackUtil.createDefaultDataConfiguration(); - } - - @Inject(method = "getScannedPack", - at = @At(value = "INVOKE", target = "Lnet/minecraft/resource/ResourcePackManager;scanPacks()V", shift = At.Shift.BEFORE)) - private void onScanPacks(CallbackInfoReturnable> cir) { - // Allow to display built-in data packs in the data pack selection screen at world creation. - this.packManager.providers.add(new ModResourcePackCreator(ResourceType.SERVER_DATA)); - } -} diff --git a/fabric-resource-loader-v0/src/client/java/net/fabricmc/fabric/mixin/resource/loader/client/GameOptionsMixin.java b/fabric-resource-loader-v0/src/client/java/net/fabricmc/fabric/mixin/resource/loader/client/GameOptionsMixin.java deleted file mode 100644 index 9799ee7dd10..00000000000 --- a/fabric-resource-loader-v0/src/client/java/net/fabricmc/fabric/mixin/resource/loader/client/GameOptionsMixin.java +++ /dev/null @@ -1,136 +0,0 @@ -/* - * Copyright (c) 2016, 2017, 2018, 2019 FabricMC - * - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ - -package net.fabricmc.fabric.mixin.resource.loader.client; - -import java.io.IOException; -import java.nio.file.Files; -import java.nio.file.Path; -import java.util.ArrayList; -import java.util.HashSet; -import java.util.LinkedHashSet; -import java.util.List; -import java.util.Set; - -import org.slf4j.Logger; -import org.spongepowered.asm.mixin.Final; -import org.spongepowered.asm.mixin.Mixin; -import org.spongepowered.asm.mixin.Shadow; -import org.spongepowered.asm.mixin.injection.At; -import org.spongepowered.asm.mixin.injection.Inject; -import org.spongepowered.asm.mixin.injection.callback.CallbackInfo; - -import net.minecraft.client.option.GameOptions; -import net.minecraft.nbt.NbtCompound; -import net.minecraft.nbt.NbtElement; -import net.minecraft.nbt.NbtIo; -import net.minecraft.nbt.NbtList; -import net.minecraft.nbt.NbtSizeTracker; -import net.minecraft.nbt.NbtString; -import net.minecraft.resource.ResourcePack; -import net.minecraft.resource.ResourcePackProfile; - -import net.fabricmc.fabric.impl.resource.loader.ModNioResourcePack; -import net.fabricmc.fabric.impl.resource.loader.ModResourcePackCreator; -import net.fabricmc.loader.api.FabricLoader; - -@Mixin(GameOptions.class) -public class GameOptionsMixin { - @Shadow - public List resourcePacks; - - @Shadow - @Final - static Logger LOGGER; - - @Inject(method = "load", at = @At("RETURN")) - private void onLoad(CallbackInfo ci) { - // Track built-in resource packs if they are enabled by default. - // - If there is NO value with matching resource pack id, add it to the enabled packs and the tracker file. - // - If there is a matching value and pack id, do not add it to the enabled packs and let - // the options value decides if it is enabled or not. - // - If there is a value without matching pack id (e.g. because the mod is removed), - // remove it from the tracker file so that it would be enabled again if added back later. - - Path dataDir = FabricLoader.getInstance().getGameDir().resolve("data"); - - if (Files.notExists(dataDir)) { - try { - Files.createDirectories(dataDir); - } catch (IOException e) { - LOGGER.warn("[Fabric Resource Loader] Could not create data directory: " + dataDir.toAbsolutePath()); - } - } - - Path trackerFile = dataDir.resolve("fabricDefaultResourcePacks.dat"); - Set trackedPacks = new HashSet<>(); - - if (Files.exists(trackerFile)) { - try { - NbtCompound data = NbtIo.readCompressed(trackerFile, NbtSizeTracker.ofUnlimitedBytes()); - NbtList values = data.getList("values", NbtElement.STRING_TYPE); - - for (int i = 0; i < values.size(); i++) { - trackedPacks.add(values.getString(i)); - } - } catch (IOException e) { - LOGGER.warn("[Fabric Resource Loader] Could not read " + trackerFile.toAbsolutePath(), e); - } - } - - Set removedPacks = new HashSet<>(trackedPacks); - Set resourcePacks = new LinkedHashSet<>(this.resourcePacks); - - List profiles = new ArrayList<>(); - ModResourcePackCreator.CLIENT_RESOURCE_PACK_PROVIDER.register(profiles::add); - - for (ResourcePackProfile profile : profiles) { - // Always add "Fabric Mods" pack to enabled resource packs. - if (profile.getId().equals(ModResourcePackCreator.FABRIC)) { - resourcePacks.add(profile.getId()); - continue; - } - - try (ResourcePack pack = profile.createResourcePack()) { - if (pack instanceof ModNioResourcePack builtinPack && builtinPack.getActivationType().isEnabledByDefault()) { - if (trackedPacks.add(builtinPack.getId())) { - resourcePacks.add(profile.getId()); - } else { - removedPacks.remove(builtinPack.getId()); - } - } - } - } - - try { - NbtList values = new NbtList(); - - for (String id : trackedPacks) { - if (!removedPacks.contains(id)) { - values.add(NbtString.of(id)); - } - } - - NbtCompound nbt = new NbtCompound(); - nbt.put("values", values); - NbtIo.writeCompressed(nbt, trackerFile); - } catch (IOException e) { - LOGGER.warn("[Fabric Resource Loader] Could not write to " + trackerFile.toAbsolutePath(), e); - } - - this.resourcePacks = new ArrayList<>(resourcePacks); - } -} diff --git a/fabric-resource-loader-v0/src/client/java/net/fabricmc/fabric/mixin/resource/loader/client/GameOptionsWriteVisitorMixin.java b/fabric-resource-loader-v0/src/client/java/net/fabricmc/fabric/mixin/resource/loader/client/GameOptionsWriteVisitorMixin.java deleted file mode 100644 index 0563ad2a45e..00000000000 --- a/fabric-resource-loader-v0/src/client/java/net/fabricmc/fabric/mixin/resource/loader/client/GameOptionsWriteVisitorMixin.java +++ /dev/null @@ -1,63 +0,0 @@ -/* - * Copyright (c) 2016, 2017, 2018, 2019 FabricMC - * - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ - -package net.fabricmc.fabric.mixin.resource.loader.client; - -import java.util.ArrayList; -import java.util.List; - -import com.llamalad7.mixinextras.sugar.Local; -import org.spongepowered.asm.mixin.Mixin; -import org.spongepowered.asm.mixin.Unique; -import org.spongepowered.asm.mixin.injection.At; -import org.spongepowered.asm.mixin.injection.ModifyArg; - -import net.minecraft.client.MinecraftClient; -import net.minecraft.resource.ResourcePackManager; -import net.minecraft.resource.ResourcePackProfile; - -import net.fabricmc.fabric.impl.resource.loader.FabricResourcePackProfile; - -/** - * Mixins to the anonymous class in #write method. - */ -@Mixin(targets = "net/minecraft/client/option/GameOptions$3") -public class GameOptionsWriteVisitorMixin { - @Unique - private static List toPackListString(List packs) { - List copy = new ArrayList<>(packs.size()); - ResourcePackManager manager = MinecraftClient.getInstance().getResourcePackManager(); - - for (String pack : packs) { - ResourcePackProfile profile = manager.getProfile(pack); - - // Nonexistent pack profiles should be handled in the same way as vanilla - if (profile == null || !((FabricResourcePackProfile) profile).fabric_isHidden()) copy.add(pack); - } - - return copy; - } - - @SuppressWarnings("unchecked") - @ModifyArg(method = "visitObject", at = @At(value = "INVOKE", target = "Ljava/util/function/Function;apply(Ljava/lang/Object;)Ljava/lang/Object;")) - private T skipHiddenPacks(T value, @Local String key) { - if ("resourcePacks".equals(key) && value instanceof List) { - return (T) toPackListString((List) value); - } - - return value; - } -} diff --git a/fabric-resource-loader-v0/src/client/java/net/fabricmc/fabric/mixin/resource/loader/client/ResourcePackOrganizerMixin.java b/fabric-resource-loader-v0/src/client/java/net/fabricmc/fabric/mixin/resource/loader/client/ResourcePackOrganizerMixin.java deleted file mode 100644 index d6444d83c6a..00000000000 --- a/fabric-resource-loader-v0/src/client/java/net/fabricmc/fabric/mixin/resource/loader/client/ResourcePackOrganizerMixin.java +++ /dev/null @@ -1,61 +0,0 @@ -/* - * Copyright (c) 2016, 2017, 2018, 2019 FabricMC - * - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ - -package net.fabricmc.fabric.mixin.resource.loader.client; - -import java.util.List; -import java.util.function.Consumer; -import java.util.function.Function; - -import org.spongepowered.asm.mixin.Final; -import org.spongepowered.asm.mixin.Mixin; -import org.spongepowered.asm.mixin.Shadow; -import org.spongepowered.asm.mixin.injection.At; -import org.spongepowered.asm.mixin.injection.Inject; -import org.spongepowered.asm.mixin.injection.callback.CallbackInfo; - -import net.minecraft.client.gui.screen.pack.ResourcePackOrganizer; -import net.minecraft.resource.ResourcePackManager; -import net.minecraft.resource.ResourcePackProfile; - -import net.fabricmc.fabric.impl.resource.loader.FabricResourcePackProfile; - -@Mixin(ResourcePackOrganizer.class) -public class ResourcePackOrganizerMixin { - @Shadow - @Final - List enabledPacks; - - @Shadow - @Final - List disabledPacks; - - /** - * Do not list hidden packs in either enabledPacks or disabledPacks. - * They are managed entirely by ResourcePackManager on save, and are invisible to client. - */ - @Inject(method = "", at = @At("TAIL")) - private void removeHiddenPacksInit(Runnable updateCallback, Function iconIdSupplier, ResourcePackManager resourcePackManager, Consumer applier, CallbackInfo ci) { - this.enabledPacks.removeIf(profile -> ((FabricResourcePackProfile) profile).fabric_isHidden()); - this.disabledPacks.removeIf(profile -> ((FabricResourcePackProfile) profile).fabric_isHidden()); - } - - @Inject(method = "refresh", at = @At("TAIL")) - private void removeHiddenPacksRefresh(CallbackInfo ci) { - this.enabledPacks.removeIf(profile -> ((FabricResourcePackProfile) profile).fabric_isHidden()); - this.disabledPacks.removeIf(profile -> ((FabricResourcePackProfile) profile).fabric_isHidden()); - } -} diff --git a/fabric-resource-loader-v0/src/client/java/net/fabricmc/fabric/mixin/resource/loader/client/VanillaResourcePackProviderMixin.java b/fabric-resource-loader-v0/src/client/java/net/fabricmc/fabric/mixin/resource/loader/client/VanillaResourcePackProviderMixin.java deleted file mode 100644 index f6bc06ddfda..00000000000 --- a/fabric-resource-loader-v0/src/client/java/net/fabricmc/fabric/mixin/resource/loader/client/VanillaResourcePackProviderMixin.java +++ /dev/null @@ -1,42 +0,0 @@ -/* - * Copyright (c) 2016, 2017, 2018, 2019 FabricMC - * - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ - -package net.fabricmc.fabric.mixin.resource.loader.client; - -import java.util.function.Consumer; - -import org.spongepowered.asm.mixin.Mixin; -import org.spongepowered.asm.mixin.injection.At; -import org.spongepowered.asm.mixin.injection.Inject; -import org.spongepowered.asm.mixin.injection.callback.CallbackInfo; - -import net.minecraft.client.resource.DefaultClientResourcePackProvider; -import net.minecraft.resource.ResourcePackProfile; -import net.minecraft.resource.VanillaResourcePackProvider; - -import net.fabricmc.fabric.impl.resource.loader.ModResourcePackCreator; - -@Mixin(VanillaResourcePackProvider.class) -public class VanillaResourcePackProviderMixin { - @Inject(method = "register", at = @At("RETURN")) - private void addBuiltinResourcePacks(Consumer consumer, CallbackInfo ci) { - // Register mod and built-in resource packs after the vanilla built-in resource packs are registered. - // noinspection ConstantConditions - if ((Object) this instanceof DefaultClientResourcePackProvider) { - ModResourcePackCreator.CLIENT_RESOURCE_PACK_PROVIDER.register(consumer); - } - } -} diff --git a/fabric-resource-loader-v0/src/client/resources/fabric-resource-loader-v0.client.mixins.json b/fabric-resource-loader-v0/src/client/resources/fabric-resource-loader-v0.client.mixins.json index 7a91d9fb121..fafdbd209fb 100644 --- a/fabric-resource-loader-v0/src/client/resources/fabric-resource-loader-v0.client.mixins.json +++ b/fabric-resource-loader-v0/src/client/resources/fabric-resource-loader-v0.client.mixins.json @@ -3,14 +3,8 @@ "package": "net.fabricmc.fabric.mixin.resource.loader.client", "compatibilityLevel": "JAVA_17", "client": [ - "CreateWorldScreenMixin", "FontManagerMixin", - "GameOptionsMixin", - "KeyedResourceReloadListenerClientMixin", - "ResourcePackOrganizerMixin", - "VanillaResourcePackProviderMixin", - "GameOptionsWriteVisitorMixin", - "ClientDataPackManagerMixin" + "KeyedResourceReloadListenerClientMixin" ], "injectors": { "defaultRequire": 1 diff --git a/fabric-resource-loader-v0/src/main/java/net/fabricmc/fabric/api/resource/IdentifiableResourceReloadListener.java b/fabric-resource-loader-v0/src/main/java/net/fabricmc/fabric/api/resource/IdentifiableResourceReloadListener.java index 51ebf6c90e0..9516569f60a 100644 --- a/fabric-resource-loader-v0/src/main/java/net/fabricmc/fabric/api/resource/IdentifiableResourceReloadListener.java +++ b/fabric-resource-loader-v0/src/main/java/net/fabricmc/fabric/api/resource/IdentifiableResourceReloadListener.java @@ -30,8 +30,11 @@ * themselves. * * @see ResourceReloadListenerKeys + * + * @deprecated see {@link org.quiltmc.qsl.resource.loader.api.reloader.IdentifiableResourceReloader IdentifiableResourceReloader} */ -public interface IdentifiableResourceReloadListener extends ResourceReloader { +@Deprecated +public interface IdentifiableResourceReloadListener extends ResourceReloader, org.quiltmc.qsl.resource.loader.api.reloader.IdentifiableResourceReloader { /** * @return The unique identifier of this listener. */ @@ -45,4 +48,9 @@ public interface IdentifiableResourceReloadListener extends ResourceReloader { default Collection getFabricDependencies() { return Collections.emptyList(); } + + @Override + default Identifier getQuiltId() { + return this.getFabricId(); + } } diff --git a/fabric-resource-loader-v0/src/main/java/net/fabricmc/fabric/api/resource/ModResourcePack.java b/fabric-resource-loader-v0/src/main/java/net/fabricmc/fabric/api/resource/ModResourcePack.java index b707e742fef..bda2ecf161e 100644 --- a/fabric-resource-loader-v0/src/main/java/net/fabricmc/fabric/api/resource/ModResourcePack.java +++ b/fabric-resource-loader-v0/src/main/java/net/fabricmc/fabric/api/resource/ModResourcePack.java @@ -22,7 +22,10 @@ /** * Interface implemented by mod-provided resource packs. + * + * @deprecated see {@link org.quiltmc.qsl.resource.loader.api.QuiltPack QuiltPack} */ +@Deprecated public interface ModResourcePack extends ResourcePack { /** * @return The ModMetadata object associated with the mod providing this diff --git a/fabric-resource-loader-v0/src/main/java/net/fabricmc/fabric/api/resource/ResourceManagerHelper.java b/fabric-resource-loader-v0/src/main/java/net/fabricmc/fabric/api/resource/ResourceManagerHelper.java index 39d2d32461d..8bd7d6b58d0 100644 --- a/fabric-resource-loader-v0/src/main/java/net/fabricmc/fabric/api/resource/ResourceManagerHelper.java +++ b/fabric-resource-loader-v0/src/main/java/net/fabricmc/fabric/api/resource/ResourceManagerHelper.java @@ -31,7 +31,10 @@ /** * Helper for working with {@link ResourceManager} instances, and other resource loader generalities. + * + * @deprecated see {@link org.quiltmc.qsl.resource.loader.api.ResourceLoader ResourceLoader} */ +@Deprecated @ApiStatus.NonExtendable public interface ResourceManagerHelper { /** @@ -89,7 +92,8 @@ static ResourceManagerHelper get(ResourceType type) { * @return {@code true} if successfully registered the resource pack, else {@code false} */ static boolean registerBuiltinResourcePack(Identifier id, ModContainer container, ResourcePackActivationType activationType) { - return ResourceManagerHelperImpl.registerBuiltinResourcePack(id, "resourcepacks/" + id.getPath(), container, activationType); + org.quiltmc.loader.api.ModContainer quiltContainer = org.quiltmc.loader.api.QuiltLoader.getModContainer(container.getMetadata().getId()).get(); + return org.quiltmc.qsl.resource.loader.api.ResourceLoader.registerBuiltinPack(id, quiltContainer, activationType.toQuilt()); } /** @@ -110,7 +114,8 @@ static boolean registerBuiltinResourcePack(Identifier id, ModContainer container * @return {@code true} if successfully registered the resource pack, else {@code false} */ static boolean registerBuiltinResourcePack(Identifier id, ModContainer container, Text displayName, ResourcePackActivationType activationType) { - return ResourceManagerHelperImpl.registerBuiltinResourcePack(id, "resourcepacks/" + id.getPath(), container, displayName, activationType); + org.quiltmc.loader.api.ModContainer quiltContainer = org.quiltmc.loader.api.QuiltLoader.getModContainer(container.getMetadata().getId()).get(); + return org.quiltmc.qsl.resource.loader.api.ResourceLoader.registerBuiltinPack(id, quiltContainer, activationType.toQuilt(), displayName); } /** @@ -133,7 +138,8 @@ static boolean registerBuiltinResourcePack(Identifier id, ModContainer container */ @Deprecated static boolean registerBuiltinResourcePack(Identifier id, ModContainer container, String displayName, ResourcePackActivationType activationType) { - return ResourceManagerHelperImpl.registerBuiltinResourcePack(id, "resourcepacks/" + id.getPath(), container, Text.literal(displayName), activationType); + org.quiltmc.loader.api.ModContainer quiltContainer = org.quiltmc.loader.api.QuiltLoader.getModContainer(container.getMetadata().getId()).get(); + return org.quiltmc.qsl.resource.loader.api.ResourceLoader.registerBuiltinPack(id, quiltContainer, activationType.toQuilt(), Text.literal(displayName)); } /** @@ -159,7 +165,13 @@ static boolean registerBuiltinResourcePack(Identifier id, ModContainer container */ @Deprecated static boolean registerBuiltinResourcePack(Identifier id, String subPath, ModContainer container, boolean enabledByDefault) { - return ResourceManagerHelperImpl.registerBuiltinResourcePack(id, subPath, container, - enabledByDefault ? ResourcePackActivationType.DEFAULT_ENABLED : ResourcePackActivationType.NORMAL); + org.quiltmc.loader.api.ModContainer quiltContainer = org.quiltmc.loader.api.QuiltLoader.getModContainer(container.getMetadata().getId()).get(); + return org.quiltmc.qsl.resource.loader.impl.ResourceLoaderImpl.registerBuiltinPack( + id, + subPath, + quiltContainer, + enabledByDefault ? org.quiltmc.qsl.resource.loader.api.PackActivationType.DEFAULT_ENABLED : org.quiltmc.qsl.resource.loader.api.PackActivationType.NORMAL, + org.quiltmc.qsl.resource.loader.impl.ResourceLoaderImpl.getBuiltinPackDisplayNameFromId(id) + ); } } diff --git a/fabric-resource-loader-v0/src/main/java/net/fabricmc/fabric/api/resource/ResourcePackActivationType.java b/fabric-resource-loader-v0/src/main/java/net/fabricmc/fabric/api/resource/ResourcePackActivationType.java index 7a0118b3609..eb0b9b5005b 100644 --- a/fabric-resource-loader-v0/src/main/java/net/fabricmc/fabric/api/resource/ResourcePackActivationType.java +++ b/fabric-resource-loader-v0/src/main/java/net/fabricmc/fabric/api/resource/ResourcePackActivationType.java @@ -1,5 +1,6 @@ /* * Copyright (c) 2016, 2017, 2018, 2019 FabricMC + * Copyright 2022 The Quilt Project * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. @@ -18,7 +19,10 @@ /** * Represents the resource pack activation type. + * + * @deprecated see {@link org.quiltmc.qsl.resource.loader.api.PackActivationType PackActivationType} */ +@Deprecated public enum ResourcePackActivationType { /** * Normal activation. The user has full control over the activation of the resource pack. @@ -41,4 +45,12 @@ public enum ResourcePackActivationType { public boolean isEnabledByDefault() { return this == DEFAULT_ENABLED || this == ALWAYS_ENABLED; } + + public org.quiltmc.qsl.resource.loader.api.PackActivationType toQuilt() { + return switch (this) { + case NORMAL -> org.quiltmc.qsl.resource.loader.api.PackActivationType.NORMAL; + case DEFAULT_ENABLED -> org.quiltmc.qsl.resource.loader.api.PackActivationType.DEFAULT_ENABLED; + case ALWAYS_ENABLED -> org.quiltmc.qsl.resource.loader.api.PackActivationType.ALWAYS_ENABLED; + }; + } } diff --git a/fabric-resource-loader-v0/src/main/java/net/fabricmc/fabric/api/resource/ResourceReloadListenerKeys.java b/fabric-resource-loader-v0/src/main/java/net/fabricmc/fabric/api/resource/ResourceReloadListenerKeys.java index d46933d7aa1..d38b7d6c089 100644 --- a/fabric-resource-loader-v0/src/main/java/net/fabricmc/fabric/api/resource/ResourceReloadListenerKeys.java +++ b/fabric-resource-loader-v0/src/main/java/net/fabricmc/fabric/api/resource/ResourceReloadListenerKeys.java @@ -22,7 +22,10 @@ * This class contains default keys for various Minecraft resource reload listeners. * * @see IdentifiableResourceReloadListener + * + * @deprecated see {@link org.quiltmc.qsl.resource.loader.api.reloader.ResourceReloaderKeys ResourceReloaderKeys} */ +@Deprecated public final class ResourceReloadListenerKeys { // client public static final Identifier SOUNDS = Identifier.ofVanilla("sounds"); diff --git a/fabric-resource-loader-v0/src/main/java/net/fabricmc/fabric/api/resource/SimpleResourceReloadListener.java b/fabric-resource-loader-v0/src/main/java/net/fabricmc/fabric/api/resource/SimpleResourceReloadListener.java index 32ed64244e5..6a3988f87a8 100644 --- a/fabric-resource-loader-v0/src/main/java/net/fabricmc/fabric/api/resource/SimpleResourceReloadListener.java +++ b/fabric-resource-loader-v0/src/main/java/net/fabricmc/fabric/api/resource/SimpleResourceReloadListener.java @@ -42,7 +42,10 @@ * {@link IdentifiableResourceReloadListener}. * * @param The data object. + * + * @deprecated see {@link org.quiltmc.qsl.resource.loader.api.reloader.SimpleResourceReloader SimpleResourceReloader} */ +@Deprecated public interface SimpleResourceReloadListener extends IdentifiableResourceReloadListener { @Override default CompletableFuture reload(ResourceReloader.Synchronizer helper, ResourceManager manager, Profiler loadProfiler, Profiler applyProfiler, Executor loadExecutor, Executor applyExecutor) { diff --git a/fabric-resource-loader-v0/src/main/java/net/fabricmc/fabric/api/resource/SimpleSynchronousResourceReloadListener.java b/fabric-resource-loader-v0/src/main/java/net/fabricmc/fabric/api/resource/SimpleSynchronousResourceReloadListener.java index 5a31104816c..ed49f0cdef1 100644 --- a/fabric-resource-loader-v0/src/main/java/net/fabricmc/fabric/api/resource/SimpleSynchronousResourceReloadListener.java +++ b/fabric-resource-loader-v0/src/main/java/net/fabricmc/fabric/api/resource/SimpleSynchronousResourceReloadListener.java @@ -21,6 +21,9 @@ /** * A simplified version of the "resource reload listener" interface, hiding the * peculiarities of the API and ensuring all data is loaded on the main thread. + * + * @deprecated see {@link org.quiltmc.qsl.resource.loader.api.reloader.SimpleSynchronousResourceReloader SimpleSynchronousResourceReloader} */ +@Deprecated public interface SimpleSynchronousResourceReloadListener extends IdentifiableResourceReloadListener, SynchronousResourceReloader { } diff --git a/fabric-resource-loader-v0/src/main/java/net/fabricmc/fabric/impl/resource/loader/BuiltinModResourcePackSource.java b/fabric-resource-loader-v0/src/main/java/net/fabricmc/fabric/impl/resource/loader/BuiltinModResourcePackSource.java deleted file mode 100644 index c8b312bd2ed..00000000000 --- a/fabric-resource-loader-v0/src/main/java/net/fabricmc/fabric/impl/resource/loader/BuiltinModResourcePackSource.java +++ /dev/null @@ -1,33 +0,0 @@ -/* - * Copyright (c) 2016, 2017, 2018, 2019 FabricMC - * - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ - -package net.fabricmc.fabric.impl.resource.loader; - -import net.minecraft.resource.ResourcePackSource; -import net.minecraft.text.Text; -import net.minecraft.util.Formatting; - -public record BuiltinModResourcePackSource(String modId) implements ResourcePackSource { - @Override - public boolean canBeEnabledLater() { - return true; - } - - @Override - public Text decorate(Text packName) { - return Text.translatable("pack.nameAndSource", packName, Text.translatable("pack.source.builtinMod", modId)).formatted(Formatting.GRAY); - } -} diff --git a/fabric-resource-loader-v0/src/main/java/net/fabricmc/fabric/impl/resource/loader/FabricLifecycledResourceManager.java b/fabric-resource-loader-v0/src/main/java/net/fabricmc/fabric/impl/resource/loader/FabricLifecycledResourceManager.java deleted file mode 100644 index 3585fa2b4bf..00000000000 --- a/fabric-resource-loader-v0/src/main/java/net/fabricmc/fabric/impl/resource/loader/FabricLifecycledResourceManager.java +++ /dev/null @@ -1,23 +0,0 @@ -/* - * Copyright (c) 2016, 2017, 2018, 2019 FabricMC - * - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ - -package net.fabricmc.fabric.impl.resource.loader; - -import net.minecraft.resource.ResourceType; - -public interface FabricLifecycledResourceManager { - ResourceType fabric_getResourceType(); -} diff --git a/fabric-resource-loader-v0/src/main/java/net/fabricmc/fabric/impl/resource/loader/FabricOriginalKnownPacksGetter.java b/fabric-resource-loader-v0/src/main/java/net/fabricmc/fabric/impl/resource/loader/FabricOriginalKnownPacksGetter.java deleted file mode 100644 index cfef14cc19e..00000000000 --- a/fabric-resource-loader-v0/src/main/java/net/fabricmc/fabric/impl/resource/loader/FabricOriginalKnownPacksGetter.java +++ /dev/null @@ -1,28 +0,0 @@ -/* - * Copyright (c) 2016, 2017, 2018, 2019 FabricMC - * - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ - -package net.fabricmc.fabric.impl.resource.loader; - -import java.util.List; - -import net.minecraft.registry.VersionedIdentifier; - -public interface FabricOriginalKnownPacksGetter { - /** - * @return the data packs known at server start - */ - List fabric_getOriginalKnownPacks(); -} diff --git a/fabric-resource-loader-v0/src/main/java/net/fabricmc/fabric/impl/resource/loader/FabricResourcePackProfile.java b/fabric-resource-loader-v0/src/main/java/net/fabricmc/fabric/impl/resource/loader/FabricResourcePackProfile.java deleted file mode 100644 index 5f9d9873c76..00000000000 --- a/fabric-resource-loader-v0/src/main/java/net/fabricmc/fabric/impl/resource/loader/FabricResourcePackProfile.java +++ /dev/null @@ -1,47 +0,0 @@ -/* - * Copyright (c) 2016, 2017, 2018, 2019 FabricMC - * - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ - -package net.fabricmc.fabric.impl.resource.loader; - -import java.util.Set; -import java.util.function.Predicate; - -/** - * Fabric addition to ResourcePackProfile. - * @see ModResourcePackCreator - */ -public interface FabricResourcePackProfile { - /** - * Returns whether the pack is internal and hidden from end users. - */ - default boolean fabric_isHidden() { - return false; - } - - /** - * Returns whether every parent is enabled. If this is not empty, the pack's status - * is synced to that of the parent pack(s), where the pack gets enabled if and only - * if each of the parent is enabled. Note that non-Fabric packs always return {@code true}. - * - * @return whether every parent is enabled. - */ - default boolean fabric_parentsEnabled(Set enabled) { - return true; - } - - default void fabric_setParentsPredicate(Predicate> predicate) { - } -} diff --git a/fabric-resource-loader-v0/src/main/java/net/fabricmc/fabric/impl/resource/loader/ModNioResourcePack.java b/fabric-resource-loader-v0/src/main/java/net/fabricmc/fabric/impl/resource/loader/ModNioResourcePack.java deleted file mode 100644 index 7846bf359c4..00000000000 --- a/fabric-resource-loader-v0/src/main/java/net/fabricmc/fabric/impl/resource/loader/ModNioResourcePack.java +++ /dev/null @@ -1,319 +0,0 @@ -/* - * Copyright (c) 2016, 2017, 2018, 2019 FabricMC - * - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ - -package net.fabricmc.fabric.impl.resource.loader; - -import java.io.IOException; -import java.io.InputStream; -import java.nio.file.DirectoryStream; -import java.nio.file.FileSystem; -import java.nio.file.FileSystems; -import java.nio.file.FileVisitResult; -import java.nio.file.Files; -import java.nio.file.Path; -import java.nio.file.SimpleFileVisitor; -import java.nio.file.attribute.BasicFileAttributes; -import java.util.ArrayList; -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.Objects; -import java.util.Optional; -import java.util.Set; -import java.util.regex.Pattern; - -import org.jetbrains.annotations.Nullable; -import org.slf4j.Logger; -import org.slf4j.LoggerFactory; - -import net.minecraft.registry.VersionedIdentifier; -import net.minecraft.resource.AbstractFileResourcePack; -import net.minecraft.resource.InputSupplier; -import net.minecraft.resource.ResourcePack; -import net.minecraft.resource.ResourcePackInfo; -import net.minecraft.resource.ResourceType; -import net.minecraft.resource.metadata.ResourceMetadataReader; -import net.minecraft.text.Text; -import net.minecraft.util.Identifier; -import net.minecraft.util.PathUtil; - -import net.fabricmc.fabric.api.resource.ModResourcePack; -import net.fabricmc.fabric.api.resource.ResourcePackActivationType; -import net.fabricmc.loader.api.ModContainer; -import net.fabricmc.loader.api.metadata.ModMetadata; - -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(); - - private final String id; - private final ModContainer mod; - private final List basePaths; - private final ResourceType type; - private final ResourcePackActivationType activationType; - private final Map> namespaces; - private final ResourcePackInfo metadata; - /** - * Whether the pack is bundled and loaded by default, as opposed to registered built-in packs. - * @see ModResourcePackUtil#appendModResourcePacks(List, ResourceType, String) - */ - private final boolean modBundled; - - public static ModNioResourcePack create(String id, ModContainer mod, String subPath, ResourceType type, ResourcePackActivationType activationType, boolean modBundled) { - List rootPaths = mod.getRootPaths(); - List paths; - - if (subPath == null) { - paths = rootPaths; - } else { - paths = new ArrayList<>(rootPaths.size()); - - for (Path path : rootPaths) { - path = path.toAbsolutePath().normalize(); - Path childPath = path.resolve(subPath.replace("/", path.getFileSystem().getSeparator())).normalize(); - - if (!childPath.startsWith(path) || !exists(childPath)) { - continue; - } - - paths.add(childPath); - } - } - - if (paths.isEmpty()) return null; - - String packId = subPath != null && modBundled ? id + "_" + subPath : id; - Text displayName = subPath == null - ? Text.translatable("pack.name.fabricMod", mod.getMetadata().getName()) - : Text.translatable("pack.name.fabricMod.subPack", mod.getMetadata().getName(), Text.translatable("resourcePack." + subPath + ".name")); - ResourcePackInfo metadata = new ResourcePackInfo( - packId, - displayName, - ModResourcePackCreator.RESOURCE_PACK_SOURCE, - Optional.of(new VersionedIdentifier(ModResourcePackCreator.FABRIC, packId, mod.getMetadata().getVersion().getFriendlyString())) - ); - ModNioResourcePack ret = new ModNioResourcePack(packId, mod, paths, type, activationType, modBundled, metadata); - - return ret.getNamespaces(type).isEmpty() ? null : ret; - } - - private ModNioResourcePack(String id, ModContainer mod, List paths, ResourceType type, ResourcePackActivationType activationType, boolean modBundled, ResourcePackInfo metadata) { - this.id = id; - this.mod = mod; - this.basePaths = paths; - this.type = type; - this.activationType = activationType; - this.modBundled = modBundled; - this.namespaces = readNamespaces(paths, mod.getMetadata().getId()); - this.metadata = metadata; - } - - @Override - public ModNioResourcePack createOverlay(String overlay) { - // See DirectoryResourcePack. - return new ModNioResourcePack(id, mod, basePaths.stream().map( - path -> path.resolve(overlay) - ).toList(), type, activationType, modBundled, metadata); - } - - static Map> readNamespaces(List paths, String modId) { - Map> ret = new EnumMap<>(ResourceType.class); - - for (ResourceType type : ResourceType.values()) { - Set namespaces = null; - - for (Path path : paths) { - Path dir = path.resolve(type.getDirectory()); - if (!Files.isDirectory(dir)) continue; - - String separator = path.getFileSystem().getSeparator(); - - try (DirectoryStream ds = Files.newDirectoryStream(dir)) { - for (Path p : ds) { - if (!Files.isDirectory(p)) continue; - - String s = p.getFileName().toString(); - // s may contain trailing slashes, remove them - s = s.replace(separator, ""); - - if (!RESOURCE_PACK_PATH.matcher(s).matches()) { - LOGGER.warn("Fabric NioResourcePack: ignored invalid namespace: {} in mod ID {}", s, modId); - continue; - } - - if (namespaces == null) namespaces = new HashSet<>(); - - namespaces.add(s); - } - } catch (IOException e) { - LOGGER.warn("getNamespaces in mod " + modId + " failed!", e); - } - } - - ret.put(type, namespaces != null ? namespaces : Collections.emptySet()); - } - - return ret; - } - - private Path getPath(String filename) { - if (hasAbsentNs(filename)) return null; - - for (Path basePath : basePaths) { - Path childPath = basePath.resolve(filename.replace("/", basePath.getFileSystem().getSeparator())).toAbsolutePath().normalize(); - - if (childPath.startsWith(basePath) && exists(childPath)) { - return childPath; - } - } - - return null; - } - - private static final String resPrefix = ResourceType.CLIENT_RESOURCES.getDirectory() + "/"; - private static final String dataPrefix = ResourceType.SERVER_DATA.getDirectory() + "/"; - - private boolean hasAbsentNs(String filename) { - int prefixLen; - ResourceType type; - - if (filename.startsWith(resPrefix)) { - prefixLen = resPrefix.length(); - type = ResourceType.CLIENT_RESOURCES; - } else if (filename.startsWith(dataPrefix)) { - prefixLen = dataPrefix.length(); - type = ResourceType.SERVER_DATA; - } else { - return false; - } - - int nsEnd = filename.indexOf('/', prefixLen); - if (nsEnd < 0) return false; - - return !namespaces.get(type).contains(filename.substring(prefixLen, nsEnd)); - } - - private InputSupplier openFile(String filename) { - Path path = getPath(filename); - - if (path != null && Files.isRegularFile(path)) { - return () -> Files.newInputStream(path); - } - - if (ModResourcePackUtil.containsDefault(filename, this.modBundled)) { - return () -> ModResourcePackUtil.openDefault(this.mod, this.type, filename); - } - - return null; - } - - @Nullable - @Override - public InputSupplier openRoot(String... pathSegments) { - PathUtil.validatePath(pathSegments); - - return this.openFile(String.join("/", pathSegments)); - } - - @Override - @Nullable - public InputSupplier open(ResourceType type, Identifier id) { - final Path path = getPath(getFilename(type, id)); - return path == null ? null : InputSupplier.create(path); - } - - @Override - public void findResources(ResourceType type, String namespace, String path, ResultConsumer visitor) { - if (!namespaces.getOrDefault(type, Collections.emptySet()).contains(namespace)) { - return; - } - - for (Path basePath : basePaths) { - String separator = basePath.getFileSystem().getSeparator(); - Path nsPath = basePath.resolve(type.getDirectory()).resolve(namespace); - Path searchPath = nsPath.resolve(path.replace("/", separator)).normalize(); - if (!exists(searchPath)) continue; - - try { - Files.walkFileTree(searchPath, new SimpleFileVisitor<>() { - @Override - public FileVisitResult visitFile(Path file, BasicFileAttributes attrs) { - String filename = nsPath.relativize(file).toString().replace(separator, "/"); - Identifier identifier = Identifier.tryParse(namespace, filename); - - if (identifier == null) { - LOGGER.error("Invalid path in mod resource-pack {}: {}:{}, ignoring", id, namespace, filename); - } else { - visitor.accept(identifier, InputSupplier.create(file)); - } - - return FileVisitResult.CONTINUE; - } - }); - } catch (IOException e) { - LOGGER.warn("findResources at " + path + " in namespace " + namespace + ", mod " + mod.getMetadata().getId() + " failed!", e); - } - } - } - - @Override - public Set getNamespaces(ResourceType type) { - return namespaces.getOrDefault(type, Collections.emptySet()); - } - - @Override - public T parseMetadata(ResourceMetadataReader metaReader) throws IOException { - try (InputStream is = Objects.requireNonNull(openFile("pack.mcmeta")).get()) { - return AbstractFileResourcePack.parseMetadata(metaReader, is); - } - } - - @Override - public ResourcePackInfo getInfo() { - return metadata; - } - - @Override - public void close() { - } - - @Override - public ModMetadata getFabricModMetadata() { - return mod.getMetadata(); - } - - public ResourcePackActivationType getActivationType() { - return this.activationType; - } - - @Override - public String getId() { - return id; - } - - private static boolean exists(Path path) { - // 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()); - } -} diff --git a/fabric-resource-loader-v0/src/main/java/net/fabricmc/fabric/impl/resource/loader/ModResourcePackCreator.java b/fabric-resource-loader-v0/src/main/java/net/fabricmc/fabric/impl/resource/loader/ModResourcePackCreator.java deleted file mode 100644 index 50242fa9761..00000000000 --- a/fabric-resource-loader-v0/src/main/java/net/fabricmc/fabric/impl/resource/loader/ModResourcePackCreator.java +++ /dev/null @@ -1,157 +0,0 @@ -/* - * Copyright (c) 2016, 2017, 2018, 2019 FabricMC - * - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ - -package net.fabricmc.fabric.impl.resource.loader; - -import java.util.ArrayList; -import java.util.List; -import java.util.Optional; -import java.util.Set; -import java.util.function.Consumer; -import java.util.function.Predicate; - -import org.jetbrains.annotations.Nullable; -import org.jetbrains.annotations.VisibleForTesting; - -import net.minecraft.resource.ResourcePackInfo; -import net.minecraft.resource.ResourcePackPosition; -import net.minecraft.resource.ResourcePackProfile; -import net.minecraft.resource.ResourcePackProvider; -import net.minecraft.resource.ResourcePackSource; -import net.minecraft.resource.ResourceType; -import net.minecraft.text.Text; - -import net.fabricmc.fabric.api.resource.ModResourcePack; - -/** - * Represents a resource pack provider for mods and built-in mods resource packs. - */ -public class ModResourcePackCreator implements ResourcePackProvider { - /** - * The ID of the root resource pack profile for bundled packs. - */ - public static final String FABRIC = "fabric"; - private static final String PROGRAMMER_ART = "programmer_art"; - private static final String HIGH_CONTRAST = "high_contrast"; - public static final Set POST_CHANGE_HANDLE_REQUIRED = Set.of(FABRIC, PROGRAMMER_ART, HIGH_CONTRAST); - @VisibleForTesting - public static final Predicate> BASE_PARENT = enabled -> enabled.contains(FABRIC); - @VisibleForTesting - public static final Predicate> PROGRAMMER_ART_PARENT = enabled -> enabled.contains(FABRIC) && enabled.contains(PROGRAMMER_ART); - @VisibleForTesting - public static final Predicate> HIGH_CONTRAST_PARENT = enabled -> enabled.contains(FABRIC) && enabled.contains(HIGH_CONTRAST); - /** - * This can be used to check if a pack profile is for mod-provided packs. - */ - public static final ResourcePackSource RESOURCE_PACK_SOURCE = new ResourcePackSource() { - @Override - public Text decorate(Text packName) { - return Text.translatable("pack.nameAndSource", packName, Text.translatable("pack.source.fabricmod")); - } - - @Override - public boolean canBeEnabledLater() { - return true; - } - }; - public static final ModResourcePackCreator CLIENT_RESOURCE_PACK_PROVIDER = new ModResourcePackCreator(ResourceType.CLIENT_RESOURCES); - /** - * The maximum number of known data packs requested from the client, including vanilla data packs. - */ - public static final int MAX_KNOWN_PACKS = Integer.getInteger("fabric-resource-loader-v0:maxKnownPacks", 1024); - - private final ResourceType type; - private final ResourcePackPosition activationInfo; - private final boolean forClientDataPackManager; - - public ModResourcePackCreator(ResourceType type) { - this(type, false); - } - - protected ModResourcePackCreator(ResourceType type, boolean forClientDataPackManager) { - this.type = type; - this.activationInfo = new ResourcePackPosition(!forClientDataPackManager, ResourcePackProfile.InsertionPosition.TOP, false); - this.forClientDataPackManager = forClientDataPackManager; - } - - /** - * Registers the resource packs. - * - * @param consumer The resource pack profile consumer. - */ - @Override - public void register(Consumer consumer) { - /* - Register order rule in this provider: - 1. Mod resource packs - 2. Mod built-in resource packs - - Register order rule globally: - 1. Default and Vanilla built-in resource packs - 2. Mod resource packs - 3. Mod built-in resource packs - 4. User resource packs - */ - - ResourcePackInfo metadata = new ResourcePackInfo( - FABRIC, - Text.translatable("pack.name.fabricMods"), - RESOURCE_PACK_SOURCE, - Optional.empty() - ); - - consumer.accept(ResourcePackProfile.create( - metadata, - new PlaceholderResourcePack.Factory(this.type, metadata), - this.type, - this.activationInfo - )); - - // Build a list of mod resource packs. - registerModPack(consumer, null, BASE_PARENT); - - if (this.type == ResourceType.CLIENT_RESOURCES) { - // Programmer Art/High Contrast data packs can never be enabled. - registerModPack(consumer, PROGRAMMER_ART, PROGRAMMER_ART_PARENT); - registerModPack(consumer, HIGH_CONTRAST, HIGH_CONTRAST_PARENT); - } - - // Register all built-in resource packs provided by mods. - ResourceManagerHelperImpl.registerBuiltinResourcePacks(this.type, consumer); - } - - private void registerModPack(Consumer consumer, @Nullable String subPath, Predicate> parents) { - List packs = new ArrayList<>(); - ModResourcePackUtil.appendModResourcePacks(packs, this.type, subPath); - - for (ModResourcePack pack : packs) { - ResourcePackProfile profile = ResourcePackProfile.create( - pack.getInfo(), - new ModResourcePackFactory(pack), - this.type, - this.activationInfo - ); - - if (profile != null) { - if (!forClientDataPackManager) { - ((FabricResourcePackProfile) profile).fabric_setParentsPredicate(parents); - } - - consumer.accept(profile); - } - } - } -} diff --git a/fabric-resource-loader-v0/src/main/java/net/fabricmc/fabric/impl/resource/loader/ModResourcePackFactory.java b/fabric-resource-loader-v0/src/main/java/net/fabricmc/fabric/impl/resource/loader/ModResourcePackFactory.java deleted file mode 100644 index a4e4e068792..00000000000 --- a/fabric-resource-loader-v0/src/main/java/net/fabricmc/fabric/impl/resource/loader/ModResourcePackFactory.java +++ /dev/null @@ -1,49 +0,0 @@ -/* - * Copyright (c) 2016, 2017, 2018, 2019 FabricMC - * - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ - -package net.fabricmc.fabric.impl.resource.loader; - -import java.util.ArrayList; -import java.util.List; - -import net.minecraft.resource.OverlayResourcePack; -import net.minecraft.resource.ResourcePack; -import net.minecraft.resource.ResourcePackInfo; -import net.minecraft.resource.ResourcePackProfile; - -import net.fabricmc.fabric.api.resource.ModResourcePack; - -public record ModResourcePackFactory(ModResourcePack pack) implements ResourcePackProfile.PackFactory { - @Override - public ResourcePack open(ResourcePackInfo var1) { - return pack; - } - - @Override - public ResourcePack openWithOverlays(ResourcePackInfo var1, ResourcePackProfile.Metadata metadata) { - if (metadata.overlays().isEmpty()) { - return pack; - } else { - List overlays = new ArrayList<>(metadata.overlays().size()); - - for (String overlay : metadata.overlays()) { - overlays.add(pack.createOverlay(overlay)); - } - - return new OverlayResourcePack(pack, overlays); - } - } -} diff --git a/fabric-resource-loader-v0/src/main/java/net/fabricmc/fabric/impl/resource/loader/ModResourcePackUtil.java b/fabric-resource-loader-v0/src/main/java/net/fabricmc/fabric/impl/resource/loader/ModResourcePackUtil.java deleted file mode 100644 index 9163e00a4e4..00000000000 --- a/fabric-resource-loader-v0/src/main/java/net/fabricmc/fabric/impl/resource/loader/ModResourcePackUtil.java +++ /dev/null @@ -1,247 +0,0 @@ -/* - * Copyright (c) 2016, 2017, 2018, 2019 FabricMC - * - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ - -package net.fabricmc.fabric.impl.resource.loader; - -import java.io.IOException; -import java.io.InputStream; -import java.nio.file.Files; -import java.nio.file.Path; -import java.util.ArrayList; -import java.util.HashSet; -import java.util.Iterator; -import java.util.LinkedHashSet; -import java.util.List; -import java.util.ListIterator; -import java.util.Map; -import java.util.Objects; -import java.util.Optional; -import java.util.Set; - -import com.google.common.base.Charsets; -import com.google.gson.Gson; -import com.google.gson.JsonObject; -import org.apache.commons.io.IOUtils; -import org.jetbrains.annotations.Nullable; -import org.slf4j.Logger; -import org.slf4j.LoggerFactory; - -import net.minecraft.SharedConstants; -import net.minecraft.resource.DataConfiguration; -import net.minecraft.resource.DataPackSettings; -import net.minecraft.resource.ResourcePack; -import net.minecraft.resource.ResourcePackManager; -import net.minecraft.resource.ResourcePackProfile; -import net.minecraft.resource.ResourceType; -import net.minecraft.resource.VanillaDataPackProvider; -import net.minecraft.resource.featuretoggle.FeatureFlags; -import net.minecraft.resource.metadata.PackResourceMetadata; -import net.minecraft.text.Text; -import net.minecraft.util.path.SymlinkFinder; - -import net.fabricmc.fabric.api.resource.ModResourcePack; -import net.fabricmc.fabric.api.resource.ResourcePackActivationType; -import net.fabricmc.loader.api.FabricLoader; -import net.fabricmc.loader.api.ModContainer; -import net.fabricmc.loader.api.metadata.ModMetadata; - -/** - * Internal utilities for managing resource packs. - */ -public final class ModResourcePackUtil { - public static final Gson GSON = new Gson(); - private static final Logger LOGGER = LoggerFactory.getLogger(ModResourcePackUtil.class); - - private ModResourcePackUtil() { - } - - /** - * Appends mod resource packs to the given list. - * - * @param packs the resource pack list to append - * @param type the type of resource - * @param subPath the resource pack sub path directory in mods, may be {@code null} - */ - public static void appendModResourcePacks(List packs, ResourceType type, @Nullable String subPath) { - for (ModContainer container : FabricLoader.getInstance().getAllMods()) { - if (container.getMetadata().getType().equals("builtin")) { - continue; - } - - ModResourcePack pack = ModNioResourcePack.create(container.getMetadata().getId(), container, subPath, type, ResourcePackActivationType.ALWAYS_ENABLED, true); - - if (pack != null) { - packs.add(pack); - } - } - } - - public static void refreshAutoEnabledPacks(List enabledProfiles, Map allProfiles) { - LOGGER.debug("[Fabric] Starting internal pack sorting with: {}", enabledProfiles.stream().map(ResourcePackProfile::getId).toList()); - enabledProfiles.removeIf(profile -> ((FabricResourcePackProfile) profile).fabric_isHidden()); - LOGGER.debug("[Fabric] Removed all internal packs, result: {}", enabledProfiles.stream().map(ResourcePackProfile::getId).toList()); - ListIterator it = enabledProfiles.listIterator(); - Set seen = new LinkedHashSet<>(); - - while (it.hasNext()) { - ResourcePackProfile profile = it.next(); - seen.add(profile.getId()); - - for (ResourcePackProfile p : allProfiles.values()) { - FabricResourcePackProfile fp = (FabricResourcePackProfile) p; - - if (fp.fabric_isHidden() && fp.fabric_parentsEnabled(seen) && seen.add(p.getId())) { - it.add(p); - LOGGER.debug("[Fabric] cur @ {}, auto-enabled {}, currently enabled: {}", profile.getId(), p.getId(), seen); - } - } - } - - LOGGER.debug("[Fabric] Final sorting result: {}", enabledProfiles.stream().map(ResourcePackProfile::getId).toList()); - } - - public static boolean containsDefault(String filename, boolean modBundled) { - return "pack.mcmeta".equals(filename) || (modBundled && "pack.png".equals(filename)); - } - - public static InputStream getDefaultIcon() throws IOException { - Optional loaderIconPath = FabricLoader.getInstance().getModContainer("fabric-resource-loader-v0") - .flatMap(resourceLoaderContainer -> resourceLoaderContainer.getMetadata().getIconPath(512).flatMap(resourceLoaderContainer::findPath)); - - if (loaderIconPath.isPresent()) { - return Files.newInputStream(loaderIconPath.get()); - } - - // Should never happen in practice - return null; - } - - public static InputStream openDefault(ModContainer container, ResourceType type, String filename) throws IOException { - switch (filename) { - case "pack.mcmeta": - String description = Objects.requireNonNullElse(container.getMetadata().getId(), ""); - String metadata = serializeMetadata(SharedConstants.getGameVersion().getResourceVersion(type), description); - return IOUtils.toInputStream(metadata, Charsets.UTF_8); - case "pack.png": - Optional path = container.getMetadata().getIconPath(512).flatMap(container::findPath); - - if (path.isPresent()) { - return Files.newInputStream(path.get()); - } else { - return getDefaultIcon(); - } - default: - return null; - } - } - - public static PackResourceMetadata getMetadataPack(int packVersion, Text description) { - return new PackResourceMetadata(description, packVersion, Optional.empty()); - } - - public static JsonObject getMetadataPackJson(int packVersion, Text description) { - return PackResourceMetadata.SERIALIZER.toJson(getMetadataPack(packVersion, description)); - } - - public static String serializeMetadata(int packVersion, String description) { - // This seems to be still manually deserialized - JsonObject pack = getMetadataPackJson(packVersion, Text.literal(description)); - JsonObject metadata = new JsonObject(); - metadata.add("pack", pack); - return GSON.toJson(metadata); - } - - public static Text getName(ModMetadata info) { - if (info.getId() != null) { - return Text.literal(info.getId()); - } else { - return Text.translatable("pack.name.fabricMod", info.getId()); - } - } - - /** - * Creates the default data pack settings that replaces - * {@code DataPackSettings.SAFE_MODE} used in vanilla. - * @return the default data pack settings - */ - public static DataConfiguration createDefaultDataConfiguration() { - ModResourcePackCreator modResourcePackCreator = new ModResourcePackCreator(ResourceType.SERVER_DATA); - List moddedResourcePacks = new ArrayList<>(); - modResourcePackCreator.register(moddedResourcePacks::add); - - List enabled = new ArrayList<>(DataPackSettings.SAFE_MODE.getEnabled()); - List disabled = new ArrayList<>(DataPackSettings.SAFE_MODE.getDisabled()); - - // This ensures that any built-in registered data packs by mods which needs to be enabled by default are - // as the data pack screen automatically put any data pack as disabled except the Default data pack. - for (ResourcePackProfile profile : moddedResourcePacks) { - if (profile.getSource() == ModResourcePackCreator.RESOURCE_PACK_SOURCE) { - enabled.add(profile.getId()); - continue; - } - - try (ResourcePack pack = profile.createResourcePack()) { - if (pack instanceof ModNioResourcePack && ((ModNioResourcePack) pack).getActivationType().isEnabledByDefault()) { - enabled.add(profile.getId()); - } else { - disabled.add(profile.getId()); - } - } - } - - return new DataConfiguration( - new DataPackSettings(enabled, disabled), - FeatureFlags.DEFAULT_ENABLED_FEATURES - ); - } - - /** - * Vanilla enables all available datapacks automatically in TestServer#create, but it does so in alphabetical order, - * which means the Vanilla pack has higher precedence than modded, breaking our tests. - * To fix this, we move all modded pack profiles to the end of the list. - */ - public static DataPackSettings createTestServerSettings(List enabled, List disabled) { - // Collect modded profiles - Set moddedProfiles = new HashSet<>(); - ModResourcePackCreator modResourcePackCreator = new ModResourcePackCreator(ResourceType.SERVER_DATA); - modResourcePackCreator.register(profile -> moddedProfiles.add(profile.getId())); - - // Remove them from the enabled list - List moveToTheEnd = new ArrayList<>(); - - for (Iterator it = enabled.iterator(); it.hasNext();) { - String profile = it.next(); - - if (moddedProfiles.contains(profile)) { - moveToTheEnd.add(profile); - it.remove(); - } - } - - // Add back at the end - enabled.addAll(moveToTheEnd); - - return new DataPackSettings(enabled, disabled); - } - - /** - * Creates the ResourcePackManager used by the ClientDataPackManager and replaces - * {@code VanillaDataPackProvider.createClientManager} used by vanilla. - */ - public static ResourcePackManager createClientManager() { - return new ResourcePackManager(new VanillaDataPackProvider(new SymlinkFinder((path) -> true)), new ModResourcePackCreator(ResourceType.SERVER_DATA, true)); - } -} diff --git a/fabric-resource-loader-v0/src/main/java/net/fabricmc/fabric/impl/resource/loader/PlaceholderResourcePack.java b/fabric-resource-loader-v0/src/main/java/net/fabricmc/fabric/impl/resource/loader/PlaceholderResourcePack.java deleted file mode 100644 index 129c70eecf9..00000000000 --- a/fabric-resource-loader-v0/src/main/java/net/fabricmc/fabric/impl/resource/loader/PlaceholderResourcePack.java +++ /dev/null @@ -1,116 +0,0 @@ -/* - * Copyright (c) 2016, 2017, 2018, 2019 FabricMC - * - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ - -package net.fabricmc.fabric.impl.resource.loader; - -import java.io.InputStream; -import java.nio.charset.StandardCharsets; -import java.util.Collections; -import java.util.Set; - -import org.apache.commons.io.IOUtils; -import org.jetbrains.annotations.Nullable; - -import net.minecraft.SharedConstants; -import net.minecraft.resource.InputSupplier; -import net.minecraft.resource.ResourcePack; -import net.minecraft.resource.ResourcePackInfo; -import net.minecraft.resource.ResourcePackProfile; -import net.minecraft.resource.ResourceType; -import net.minecraft.resource.metadata.PackResourceMetadata; -import net.minecraft.resource.metadata.ResourceMetadataMap; -import net.minecraft.resource.metadata.ResourceMetadataReader; -import net.minecraft.text.Text; -import net.minecraft.util.Identifier; - -public record PlaceholderResourcePack(ResourceType type, ResourcePackInfo metadata) implements ResourcePack { - private static final Text DESCRIPTION_TEXT = Text.translatable("pack.description.modResources"); - - public PackResourceMetadata getMetadata() { - return ModResourcePackUtil.getMetadataPack( - SharedConstants.getGameVersion().getResourceVersion(type), - DESCRIPTION_TEXT - ); - } - - @Nullable - @Override - public InputSupplier openRoot(String... segments) { - if (segments.length > 0) { - switch (segments[0]) { - case "pack.mcmeta": - return () -> { - String metadata = ModResourcePackUtil.GSON.toJson(PackResourceMetadata.SERIALIZER.toJson(getMetadata())); - return IOUtils.toInputStream(metadata, StandardCharsets.UTF_8); - }; - case "pack.png": - return ModResourcePackUtil::getDefaultIcon; - } - } - - return null; - } - - /** - * This pack has no actual contents. - */ - @Nullable - @Override - public InputSupplier open(ResourceType type, Identifier id) { - return null; - } - - @Override - public void findResources(ResourceType type, String namespace, String prefix, ResultConsumer consumer) { - } - - @Override - public Set getNamespaces(ResourceType type) { - return Collections.emptySet(); - } - - @Nullable - @Override - public T parseMetadata(ResourceMetadataReader metaReader) { - return ResourceMetadataMap.of(PackResourceMetadata.SERIALIZER, getMetadata()).get(metaReader); - } - - @Override - public ResourcePackInfo getInfo() { - return metadata; - } - - @Override - public String getId() { - return ModResourcePackCreator.FABRIC; - } - - @Override - public void close() { - } - - public record Factory(ResourceType type, ResourcePackInfo metadata) implements ResourcePackProfile.PackFactory { - @Override - public ResourcePack open(ResourcePackInfo var1) { - return new PlaceholderResourcePack(this.type, metadata); - } - - @Override - public ResourcePack openWithOverlays(ResourcePackInfo var1, ResourcePackProfile.Metadata metadata) { - return open(var1); - } - } -} diff --git a/fabric-resource-loader-v0/src/main/java/net/fabricmc/fabric/impl/resource/loader/ResourceManagerHelperImpl.java b/fabric-resource-loader-v0/src/main/java/net/fabricmc/fabric/impl/resource/loader/ResourceManagerHelperImpl.java index afd382e1d9e..f89bbd3786f 100644 --- a/fabric-resource-loader-v0/src/main/java/net/fabricmc/fabric/impl/resource/loader/ResourceManagerHelperImpl.java +++ b/fabric-resource-loader-v0/src/main/java/net/fabricmc/fabric/impl/resource/loader/ResourceManagerHelperImpl.java @@ -16,296 +16,39 @@ package net.fabricmc.fabric.impl.resource.loader; -import java.nio.file.Path; -import java.util.ArrayList; -import java.util.Collections; import java.util.HashMap; -import java.util.HashSet; -import java.util.Iterator; -import java.util.LinkedHashSet; -import java.util.List; import java.util.Map; -import java.util.Objects; -import java.util.Set; -import java.util.function.Consumer; import java.util.function.Function; -import com.google.common.collect.Lists; -import org.jetbrains.annotations.Nullable; -import org.slf4j.Logger; -import org.slf4j.LoggerFactory; +import org.quiltmc.qsl.resource.loader.api.ResourceLoader; -import net.minecraft.recipe.RecipeManager; import net.minecraft.registry.RegistryWrapper; -import net.minecraft.resource.OverlayResourcePack; -import net.minecraft.resource.ResourcePack; -import net.minecraft.resource.ResourcePackInfo; -import net.minecraft.resource.ResourcePackPosition; -import net.minecraft.resource.ResourcePackProfile; -import net.minecraft.resource.ResourceReloader; import net.minecraft.resource.ResourceType; -import net.minecraft.text.Text; import net.minecraft.util.Identifier; -import net.minecraft.util.Pair; import net.fabricmc.fabric.api.resource.IdentifiableResourceReloadListener; import net.fabricmc.fabric.api.resource.ResourceManagerHelper; -import net.fabricmc.fabric.api.resource.ResourcePackActivationType; -import net.fabricmc.loader.api.ModContainer; public class ResourceManagerHelperImpl implements ResourceManagerHelper { private static final Map registryMap = new HashMap<>(); - private static final Set> builtinResourcePacks = new HashSet<>(); - private static final Logger LOGGER = LoggerFactory.getLogger(ResourceManagerHelperImpl.class); - private final Set addedListenerIds = new HashSet<>(); - private final Set listenerFactories = new LinkedHashSet<>(); - private final Set addedListeners = new LinkedHashSet<>(); - private final ResourceType type; + private final ResourceLoader quiltLoader; private ResourceManagerHelperImpl(ResourceType type) { - this.type = type; + this.quiltLoader = ResourceLoader.get(type); } public static ResourceManagerHelperImpl get(ResourceType type) { return registryMap.computeIfAbsent(type, ResourceManagerHelperImpl::new); } - /** - * Registers a built-in resource pack. Internal implementation. - * - * @param id the identifier of the resource pack - * @param subPath the sub path in the mod resources - * @param container the mod container - * @param displayName the display name of the resource pack - * @param activationType the activation type of the resource pack - * @return {@code true} if successfully registered the resource pack, else {@code false} - * @see ResourceManagerHelper#registerBuiltinResourcePack(Identifier, ModContainer, Text, ResourcePackActivationType) - * @see ResourceManagerHelper#registerBuiltinResourcePack(Identifier, ModContainer, ResourcePackActivationType) - */ - public static boolean registerBuiltinResourcePack(Identifier id, String subPath, ModContainer container, Text displayName, ResourcePackActivationType activationType) { - // Assuming the mod has multiple paths, we simply "hope" that the file separator is *not* different across them - List paths = container.getRootPaths(); - String separator = paths.getFirst().getFileSystem().getSeparator(); - subPath = subPath.replace("/", separator); - ModNioResourcePack resourcePack = ModNioResourcePack.create(id.toString(), container, subPath, ResourceType.CLIENT_RESOURCES, activationType, false); - ModNioResourcePack dataPack = ModNioResourcePack.create(id.toString(), container, subPath, ResourceType.SERVER_DATA, activationType, false); - if (resourcePack == null && dataPack == null) return false; - - if (resourcePack != null) { - builtinResourcePacks.add(new Pair<>(displayName, resourcePack)); - } - - if (dataPack != null) { - builtinResourcePacks.add(new Pair<>(displayName, dataPack)); - } - - return true; - } - - /** - * Registers a built-in resource pack. Internal implementation. - * - * @param id the identifier of the resource pack - * @param subPath the sub path in the mod resources - * @param container the mod container - * @param activationType the activation type of the resource pack - * @return {@code true} if successfully registered the resource pack, else {@code false} - * @see ResourceManagerHelper#registerBuiltinResourcePack(Identifier, ModContainer, ResourcePackActivationType) - * @see ResourceManagerHelper#registerBuiltinResourcePack(Identifier, ModContainer, Text, ResourcePackActivationType) - */ - public static boolean registerBuiltinResourcePack(Identifier id, String subPath, ModContainer container, ResourcePackActivationType activationType) { - return registerBuiltinResourcePack(id, subPath, container, Text.literal(id.getNamespace() + "/" + id.getPath()), activationType); - } - - public static void registerBuiltinResourcePacks(ResourceType resourceType, Consumer consumer) { - // Loop through each registered built-in resource packs and add them if valid. - for (Pair entry : builtinResourcePacks) { - ModNioResourcePack pack = entry.getRight(); - - // Add the built-in pack only if namespaces for the specified resource type are present. - if (!pack.getNamespaces(resourceType).isEmpty()) { - // Make the resource pack profile for built-in pack, should never be always enabled. - ResourcePackInfo info = new ResourcePackInfo( - entry.getRight().getId(), - entry.getLeft(), - new BuiltinModResourcePackSource(pack.getFabricModMetadata().getName()), - entry.getRight().getKnownPackInfo() - ); - ResourcePackPosition info2 = new ResourcePackPosition( - pack.getActivationType() == ResourcePackActivationType.ALWAYS_ENABLED, - ResourcePackProfile.InsertionPosition.TOP, - false - ); - - ResourcePackProfile profile = ResourcePackProfile.create(info, new ResourcePackProfile.PackFactory() { - @Override - public ResourcePack open(ResourcePackInfo var1) { - return entry.getRight(); - } - - @Override - public ResourcePack openWithOverlays(ResourcePackInfo var1, ResourcePackProfile.Metadata metadata) { - ModNioResourcePack pack = entry.getRight(); - - if (metadata.overlays().isEmpty()) { - return pack; - } - - List overlays = new ArrayList<>(metadata.overlays().size()); - - for (String overlay : metadata.overlays()) { - overlays.add(pack.createOverlay(overlay)); - } - - return new OverlayResourcePack(pack, overlays); - } - }, resourceType, info2); - consumer.accept(profile); - } - } - } - - public static List sort(ResourceType type, List listeners) { - if (type == null) { - return listeners; - } - - ResourceManagerHelperImpl instance = get(type); - - if (instance != null) { - List mutable = new ArrayList<>(listeners); - instance.sort(mutable); - return Collections.unmodifiableList(mutable); - } - - return listeners; - } - - protected void sort(List listeners) { - listeners.removeAll(addedListeners); - - // General rules: - // - We *do not* touch the ordering of vanilla listeners. Ever. - // While dependency values are provided where possible, we cannot - // trust them 100%. Only code doesn't lie. - // - We addReloadListener all custom listeners after vanilla listeners. Same reasons. - - final RegistryWrapper.WrapperLookup wrapperLookup = getWrapperLookup(listeners); - List listenersToAdd = Lists.newArrayList(); - - for (ListenerFactory addedListener : listenerFactories) { - listenersToAdd.add(addedListener.get(wrapperLookup)); - } - - addedListeners.clear(); - addedListeners.addAll(listenersToAdd); - - Set resolvedIds = new HashSet<>(); - - for (ResourceReloader listener : listeners) { - if (listener instanceof IdentifiableResourceReloadListener) { - resolvedIds.add(((IdentifiableResourceReloadListener) listener).getFabricId()); - } - } - - int lastSize = -1; - - while (listeners.size() != lastSize) { - lastSize = listeners.size(); - - Iterator it = listenersToAdd.iterator(); - - while (it.hasNext()) { - IdentifiableResourceReloadListener listener = it.next(); - - if (resolvedIds.containsAll(listener.getFabricDependencies())) { - resolvedIds.add(listener.getFabricId()); - listeners.add(listener); - it.remove(); - } - } - } - - for (IdentifiableResourceReloadListener listener : listenersToAdd) { - LOGGER.warn("Could not resolve dependencies for listener: " + listener.getFabricId() + "!"); - } - } - - // A bit of a hack to get the registry, but it works. - @Nullable - private RegistryWrapper.WrapperLookup getWrapperLookup(List listeners) { - if (type == ResourceType.CLIENT_RESOURCES) { - // We don't need the registry for client resources. - return null; - } - - for (ResourceReloader resourceReloader : listeners) { - if (resourceReloader instanceof RecipeManager recipeManager) { - return recipeManager.registryLookup; - } - } - - throw new IllegalStateException("No RecipeManager found in listeners!"); - } - @Override public void registerReloadListener(IdentifiableResourceReloadListener listener) { - registerReloadListener(new SimpleResourceReloaderFactory(listener)); + this.quiltLoader.registerReloader(listener); } @Override public void registerReloadListener(Identifier identifier, Function listenerFactory) { - if (type == ResourceType.CLIENT_RESOURCES) { - throw new IllegalArgumentException("Cannot register a registry listener for the client resource type!"); - } - - registerReloadListener(new RegistryResourceReloaderFactory(identifier, listenerFactory)); - } - - private void registerReloadListener(ListenerFactory factory) { - if (!addedListenerIds.add(factory.id())) { - LOGGER.warn("Tried to register resource reload listener " + factory.id() + " twice!"); - return; - } - - if (!listenerFactories.add(factory)) { - throw new RuntimeException("Listener with previously unknown ID " + factory.id() + " already in listener set!"); - } - } - - private sealed interface ListenerFactory permits SimpleResourceReloaderFactory, RegistryResourceReloaderFactory { - Identifier id(); - - IdentifiableResourceReloadListener get(RegistryWrapper.WrapperLookup registry); - } - - private record SimpleResourceReloaderFactory(IdentifiableResourceReloadListener listener) implements ListenerFactory { - @Override - public Identifier id() { - return listener.getFabricId(); - } - - @Override - public IdentifiableResourceReloadListener get(RegistryWrapper.WrapperLookup registry) { - return listener; - } - } - - private record RegistryResourceReloaderFactory(Identifier id, Function listenerFactory) implements ListenerFactory { - private RegistryResourceReloaderFactory { - Objects.requireNonNull(listenerFactory); - } - - @Override - public IdentifiableResourceReloadListener get(RegistryWrapper.WrapperLookup registry) { - final IdentifiableResourceReloadListener listener = listenerFactory.apply(registry); - - if (!id.equals(listener.getFabricId())) { - throw new IllegalStateException("Listener factory for " + id + " created a listener with ID " + listener.getFabricId()); - } - - return listener; - } + this.quiltLoader.registerReloader(identifier, listenerFactory::apply); } } diff --git a/fabric-resource-loader-v0/src/main/java/net/fabricmc/fabric/impl/resource/loader/ServerLanguageUtil.java b/fabric-resource-loader-v0/src/main/java/net/fabricmc/fabric/impl/resource/loader/ServerLanguageUtil.java deleted file mode 100644 index a38a8efbe30..00000000000 --- a/fabric-resource-loader-v0/src/main/java/net/fabricmc/fabric/impl/resource/loader/ServerLanguageUtil.java +++ /dev/null @@ -1,56 +0,0 @@ -/* - * Copyright (c) 2016, 2017, 2018, 2019 FabricMC - * - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ - -package net.fabricmc.fabric.impl.resource.loader; - -import java.nio.file.Files; -import java.nio.file.Path; -import java.util.Collection; -import java.util.Collections; -import java.util.LinkedHashSet; -import java.util.Map; -import java.util.Set; - -import net.minecraft.resource.ResourceType; -import net.minecraft.util.Language; - -import net.fabricmc.loader.api.FabricLoader; -import net.fabricmc.loader.api.ModContainer; - -public final class ServerLanguageUtil { - private static final String ASSETS_PREFIX = ResourceType.CLIENT_RESOURCES.getDirectory() + '/'; - - private ServerLanguageUtil() { - } - - public static Collection getModLanguageFiles() { - Set paths = new LinkedHashSet<>(); - - for (ModContainer mod : FabricLoader.getInstance().getAllMods()) { - if (mod.getMetadata().getType().equals("builtin")) continue; - - final Map> map = ModNioResourcePack.readNamespaces(mod.getRootPaths(), mod.getMetadata().getId()); - - for (String ns : map.get(ResourceType.CLIENT_RESOURCES)) { - mod.findPath(ASSETS_PREFIX + ns + "/lang/" + Language.DEFAULT_LANGUAGE + ".json") - .filter(Files::isRegularFile) - .ifPresent(paths::add); - } - } - - return Collections.unmodifiableCollection(paths); - } -} diff --git a/fabric-resource-loader-v0/src/main/java/net/fabricmc/fabric/mixin/resource/loader/DatapackCommandMixin.java b/fabric-resource-loader-v0/src/main/java/net/fabricmc/fabric/mixin/resource/loader/DatapackCommandMixin.java deleted file mode 100644 index 4b71afbc041..00000000000 --- a/fabric-resource-loader-v0/src/main/java/net/fabricmc/fabric/mixin/resource/loader/DatapackCommandMixin.java +++ /dev/null @@ -1,68 +0,0 @@ -/* - * Copyright (c) 2016, 2017, 2018, 2019 FabricMC - * - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ - -package net.fabricmc.fabric.mixin.resource.loader; - -import java.util.Collection; -import java.util.function.Predicate; -import java.util.stream.Stream; - -import com.llamalad7.mixinextras.injector.wrapoperation.Operation; -import com.llamalad7.mixinextras.injector.wrapoperation.WrapOperation; -import com.llamalad7.mixinextras.sugar.Local; -import com.mojang.brigadier.context.CommandContext; -import com.mojang.brigadier.exceptions.CommandSyntaxException; -import com.mojang.brigadier.exceptions.DynamicCommandExceptionType; -import org.spongepowered.asm.mixin.Mixin; -import org.spongepowered.asm.mixin.Unique; -import org.spongepowered.asm.mixin.injection.At; -import org.spongepowered.asm.mixin.injection.Inject; -import org.spongepowered.asm.mixin.injection.Redirect; -import org.spongepowered.asm.mixin.injection.callback.CallbackInfoReturnable; - -import net.minecraft.resource.ResourcePackManager; -import net.minecraft.resource.ResourcePackProfile; -import net.minecraft.server.command.DatapackCommand; -import net.minecraft.server.command.ServerCommandSource; -import net.minecraft.text.Text; - -import net.fabricmc.fabric.impl.resource.loader.FabricResourcePackProfile; - -/** - * Disables enabling/disabling internal data packs. - * Listing them is still allowed, but they do not appear in suggestions. - */ -@Mixin(DatapackCommand.class) -public class DatapackCommandMixin { - @Unique - private static final DynamicCommandExceptionType INTERNAL_PACK_EXCEPTION = new DynamicCommandExceptionType( - packName -> Text.stringifiedTranslatable("commands.datapack.fabric.internal", packName)); - - @Redirect(method = "method_13136", at = @At(value = "INVOKE", target = "Lnet/minecraft/resource/ResourcePackManager;getEnabledIds()Ljava/util/Collection;")) - private static Collection filterEnabledPackSuggestions(ResourcePackManager dataPackManager) { - return dataPackManager.getEnabledProfiles().stream().filter(profile -> !((FabricResourcePackProfile) profile).fabric_isHidden()).map(ResourcePackProfile::getId).toList(); - } - - @WrapOperation(method = "method_13120", at = @At(value = "INVOKE", target = "Ljava/util/stream/Stream;filter(Ljava/util/function/Predicate;)Ljava/util/stream/Stream;", ordinal = 0)) - private static Stream filterDisabledPackSuggestions(Stream instance, Predicate predicate, Operation> original) { - return original.call(instance, predicate).filter(profile -> !((FabricResourcePackProfile) profile).fabric_isHidden()); - } - - @Inject(method = "getPackContainer", at = @At(value = "INVOKE", target = "Ljava/util/Collection;contains(Ljava/lang/Object;)Z", shift = At.Shift.BEFORE)) - private static void errorOnInternalPack(CommandContext context, String name, boolean enable, CallbackInfoReturnable cir, @Local ResourcePackProfile profile) throws CommandSyntaxException { - if (((FabricResourcePackProfile) profile).fabric_isHidden()) throw INTERNAL_PACK_EXCEPTION.create(profile.getId()); - } -} diff --git a/fabric-resource-loader-v0/src/main/java/net/fabricmc/fabric/mixin/resource/loader/LifecycledResourceManagerImplMixin.java b/fabric-resource-loader-v0/src/main/java/net/fabricmc/fabric/mixin/resource/loader/LifecycledResourceManagerImplMixin.java deleted file mode 100644 index 25d9d4c778b..00000000000 --- a/fabric-resource-loader-v0/src/main/java/net/fabricmc/fabric/mixin/resource/loader/LifecycledResourceManagerImplMixin.java +++ /dev/null @@ -1,47 +0,0 @@ -/* - * Copyright (c) 2016, 2017, 2018, 2019 FabricMC - * - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ - -package net.fabricmc.fabric.mixin.resource.loader; - -import java.util.List; - -import org.spongepowered.asm.mixin.Mixin; -import org.spongepowered.asm.mixin.Unique; -import org.spongepowered.asm.mixin.injection.At; -import org.spongepowered.asm.mixin.injection.Inject; -import org.spongepowered.asm.mixin.injection.callback.CallbackInfo; - -import net.minecraft.resource.LifecycledResourceManagerImpl; -import net.minecraft.resource.ResourcePack; -import net.minecraft.resource.ResourceType; - -import net.fabricmc.fabric.impl.resource.loader.FabricLifecycledResourceManager; - -@Mixin(LifecycledResourceManagerImpl.class) -public class LifecycledResourceManagerImplMixin implements FabricLifecycledResourceManager { - @Unique - private ResourceType fabric_ResourceType; - - @Inject(method = "", at = @At("TAIL")) - private void init(ResourceType resourceType, List list, CallbackInfo ci) { - this.fabric_ResourceType = resourceType; - } - - @Override - public ResourceType fabric_getResourceType() { - return fabric_ResourceType; - } -} diff --git a/fabric-resource-loader-v0/src/main/java/net/fabricmc/fabric/mixin/resource/loader/MinecraftServerMixin.java b/fabric-resource-loader-v0/src/main/java/net/fabricmc/fabric/mixin/resource/loader/MinecraftServerMixin.java deleted file mode 100644 index d5c060764e4..00000000000 --- a/fabric-resource-loader-v0/src/main/java/net/fabricmc/fabric/mixin/resource/loader/MinecraftServerMixin.java +++ /dev/null @@ -1,79 +0,0 @@ -/* - * Copyright (c) 2016, 2017, 2018, 2019 FabricMC - * - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ - -package net.fabricmc.fabric.mixin.resource.loader; - -import java.net.Proxy; -import java.util.List; - -import com.mojang.datafixers.DataFixer; -import org.spongepowered.asm.mixin.Mixin; -import org.spongepowered.asm.mixin.Unique; -import org.spongepowered.asm.mixin.injection.At; -import org.spongepowered.asm.mixin.injection.Inject; -import org.spongepowered.asm.mixin.injection.Redirect; -import org.spongepowered.asm.mixin.injection.callback.CallbackInfo; - -import net.minecraft.registry.VersionedIdentifier; -import net.minecraft.resource.ResourcePack; -import net.minecraft.resource.ResourcePackManager; -import net.minecraft.resource.ResourcePackProfile; -import net.minecraft.server.MinecraftServer; -import net.minecraft.server.SaveLoader; -import net.minecraft.server.WorldGenerationProgressListenerFactory; -import net.minecraft.util.ApiServices; -import net.minecraft.world.level.storage.LevelStorage; - -import net.fabricmc.fabric.impl.resource.loader.BuiltinModResourcePackSource; -import net.fabricmc.fabric.impl.resource.loader.FabricOriginalKnownPacksGetter; -import net.fabricmc.fabric.impl.resource.loader.ModNioResourcePack; - -@Mixin(MinecraftServer.class) -public class MinecraftServerMixin implements FabricOriginalKnownPacksGetter { - @Unique - private List fabric_originalKnownPacks; - - @Inject(method = "", at = @At("TAIL")) - private void init(Thread serverThread, LevelStorage.Session session, ResourcePackManager dataPackManager, SaveLoader saveLoader, Proxy proxy, DataFixer dataFixer, ApiServices apiServices, WorldGenerationProgressListenerFactory worldGenerationProgressListenerFactory, CallbackInfo ci) { - this.fabric_originalKnownPacks = saveLoader.resourceManager().streamResourcePacks().flatMap(pack -> pack.getInfo().knownPackInfo().stream()).toList(); - } - - @Redirect(method = "loadDataPacks(Lnet/minecraft/resource/ResourcePackManager;Lnet/minecraft/resource/DataConfiguration;ZZ)Lnet/minecraft/resource/DataConfiguration;", at = @At(value = "INVOKE", target = "Ljava/util/List;contains(Ljava/lang/Object;)Z")) - private static boolean onCheckDisabled(List list, Object o, ResourcePackManager resourcePackManager) { - String profileId = (String) o; - boolean contains = list.contains(profileId); - - if (contains) { - return true; - } - - ResourcePackProfile profile = resourcePackManager.getProfile(profileId); - - if (profile.getSource() instanceof BuiltinModResourcePackSource) { - try (ResourcePack pack = profile.createResourcePack()) { - // Prevents automatic load for built-in data packs provided by mods. - return pack instanceof ModNioResourcePack modPack && !modPack.getActivationType().isEnabledByDefault(); - } - } - - return false; - } - - @Override - public List fabric_getOriginalKnownPacks() { - return this.fabric_originalKnownPacks; - } -} diff --git a/fabric-resource-loader-v0/src/main/java/net/fabricmc/fabric/mixin/resource/loader/ResourcePackManagerMixin.java b/fabric-resource-loader-v0/src/main/java/net/fabricmc/fabric/mixin/resource/loader/ResourcePackManagerMixin.java deleted file mode 100644 index 0c32b8f8f8d..00000000000 --- a/fabric-resource-loader-v0/src/main/java/net/fabricmc/fabric/mixin/resource/loader/ResourcePackManagerMixin.java +++ /dev/null @@ -1,106 +0,0 @@ -/* - * Copyright (c) 2016, 2017, 2018, 2019 FabricMC - * - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ - -package net.fabricmc.fabric.mixin.resource.loader; - -import java.util.Collection; -import java.util.LinkedHashSet; -import java.util.List; -import java.util.Map; -import java.util.Set; -import java.util.stream.Collectors; - -import com.llamalad7.mixinextras.sugar.Local; -import org.slf4j.Logger; -import org.slf4j.LoggerFactory; -import org.spongepowered.asm.mixin.Final; -import org.spongepowered.asm.mixin.Mixin; -import org.spongepowered.asm.mixin.Mutable; -import org.spongepowered.asm.mixin.Shadow; -import org.spongepowered.asm.mixin.Unique; -import org.spongepowered.asm.mixin.injection.At; -import org.spongepowered.asm.mixin.injection.Inject; -import org.spongepowered.asm.mixin.injection.callback.CallbackInfo; -import org.spongepowered.asm.mixin.injection.callback.CallbackInfoReturnable; - -import net.minecraft.resource.FileResourcePackProvider; -import net.minecraft.resource.ResourcePackManager; -import net.minecraft.resource.ResourcePackProfile; -import net.minecraft.resource.ResourcePackProvider; -import net.minecraft.resource.ResourcePackSource; -import net.minecraft.resource.ResourceType; - -import net.fabricmc.fabric.impl.resource.loader.FabricResourcePackProfile; -import net.fabricmc.fabric.impl.resource.loader.ModResourcePackCreator; -import net.fabricmc.fabric.impl.resource.loader.ModResourcePackUtil; - -@Mixin(ResourcePackManager.class) -public abstract class ResourcePackManagerMixin { - @Unique - private static final Logger LOGGER = LoggerFactory.getLogger("ResourcePackManagerMixin"); - - @Shadow - @Final - @Mutable - public Set providers; - - @Shadow - private Map profiles; - - @Inject(method = "", at = @At("RETURN")) - public void construct(ResourcePackProvider[] resourcePackProviders, CallbackInfo info) { - // Use a LinkedHashSet to preserve ordering - providers = new LinkedHashSet<>(providers); - - // Search resource pack providers to find any server-related pack provider. - boolean shouldAddServerProvider = false; - - for (ResourcePackProvider provider : this.providers) { - if (provider instanceof FileResourcePackProvider - && (((FileResourcePackProvider) provider).source == ResourcePackSource.WORLD - || ((FileResourcePackProvider) provider).source == ResourcePackSource.SERVER)) { - shouldAddServerProvider = true; - break; - } - } - - // On server, add the mod resource pack provider. - if (shouldAddServerProvider) { - providers.add(new ModResourcePackCreator(ResourceType.SERVER_DATA)); - } - } - - @Inject(method = "buildEnabledProfiles", at = @At(value = "INVOKE", target = "Lcom/google/common/collect/ImmutableList;copyOf(Ljava/util/Collection;)Lcom/google/common/collect/ImmutableList;", shift = At.Shift.BEFORE)) - private void handleAutoEnableDisable(Collection enabledNames, CallbackInfoReturnable> cir, @Local List enabledAfterFirstRun) { - ModResourcePackUtil.refreshAutoEnabledPacks(enabledAfterFirstRun, this.profiles); - } - - @Inject(method = "enable", at = @At(value = "INVOKE", target = "Ljava/util/List;add(Ljava/lang/Object;)Z", shift = At.Shift.AFTER)) - private void handleAutoEnable(String profile, CallbackInfoReturnable cir, @Local List newlyEnabled) { - if (ModResourcePackCreator.POST_CHANGE_HANDLE_REQUIRED.contains(profile)) { - ModResourcePackUtil.refreshAutoEnabledPacks(newlyEnabled, this.profiles); - } - } - - @Inject(method = "disable", at = @At(value = "INVOKE", target = "Ljava/util/List;remove(Ljava/lang/Object;)Z")) - private void handleAutoDisable(String profile, CallbackInfoReturnable cir, @Local List enabled) { - if (ModResourcePackCreator.POST_CHANGE_HANDLE_REQUIRED.contains(profile)) { - Set currentlyEnabled = enabled.stream().map(ResourcePackProfile::getId).collect(Collectors.toSet()); - enabled.removeIf(p -> !((FabricResourcePackProfile) p).fabric_parentsEnabled(currentlyEnabled)); - LOGGER.debug("[Fabric] Internal pack auto-removed upon disabling {}, result: {}", profile, enabled.stream().map(ResourcePackProfile::getId).toList()); - } - } -} diff --git a/fabric-resource-loader-v0/src/main/java/net/fabricmc/fabric/mixin/resource/loader/ResourcePackProfileMixin.java b/fabric-resource-loader-v0/src/main/java/net/fabricmc/fabric/mixin/resource/loader/ResourcePackProfileMixin.java index c917141d7d9..3590a4fa3bc 100644 --- a/fabric-resource-loader-v0/src/main/java/net/fabricmc/fabric/mixin/resource/loader/ResourcePackProfileMixin.java +++ b/fabric-resource-loader-v0/src/main/java/net/fabricmc/fabric/mixin/resource/loader/ResourcePackProfileMixin.java @@ -16,12 +16,8 @@ package net.fabricmc.fabric.mixin.resource.loader; -import java.util.Set; -import java.util.function.Predicate; - import org.spongepowered.asm.mixin.Mixin; import org.spongepowered.asm.mixin.Shadow; -import org.spongepowered.asm.mixin.Unique; import org.spongepowered.asm.mixin.injection.At; import org.spongepowered.asm.mixin.injection.Inject; import org.spongepowered.asm.mixin.injection.callback.CallbackInfoReturnable; @@ -31,7 +27,6 @@ import net.minecraft.resource.ResourcePackProfile; import net.minecraft.resource.ResourcePackSource; -import net.fabricmc.fabric.impl.resource.loader.FabricResourcePackProfile; import net.fabricmc.fabric.impl.resource.loader.ResourcePackSourceTracker; /** @@ -42,12 +37,7 @@ * @see ResourcePackSourceTracker */ @Mixin(ResourcePackProfile.class) -abstract class ResourcePackProfileMixin implements FabricResourcePackProfile { - @Unique - private static final Predicate> DEFAULT_PARENT_PREDICATE = parents -> true; - @Unique - private Predicate> parentsPredicate = DEFAULT_PARENT_PREDICATE; - +abstract class ResourcePackProfileMixin { @Shadow public abstract ResourcePackInfo getInfo(); @@ -55,19 +45,4 @@ abstract class ResourcePackProfileMixin implements FabricResourcePackProfile { private void onCreateResourcePack(CallbackInfoReturnable info) { ResourcePackSourceTracker.setSource(info.getReturnValue(), getInfo().source()); } - - @Override - public boolean fabric_isHidden() { - return parentsPredicate != DEFAULT_PARENT_PREDICATE; - } - - @Override - public boolean fabric_parentsEnabled(Set enabled) { - return parentsPredicate.test(enabled); - } - - @Override - public void fabric_setParentsPredicate(Predicate> predicate) { - this.parentsPredicate = predicate; - } } diff --git a/fabric-resource-loader-v0/src/main/java/net/fabricmc/fabric/mixin/resource/loader/SelectKnownPacksC2SPacketMixin.java b/fabric-resource-loader-v0/src/main/java/net/fabricmc/fabric/mixin/resource/loader/SelectKnownPacksC2SPacketMixin.java deleted file mode 100644 index e7af5319127..00000000000 --- a/fabric-resource-loader-v0/src/main/java/net/fabricmc/fabric/mixin/resource/loader/SelectKnownPacksC2SPacketMixin.java +++ /dev/null @@ -1,33 +0,0 @@ -/* - * Copyright (c) 2016, 2017, 2018, 2019 FabricMC - * - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ - -package net.fabricmc.fabric.mixin.resource.loader; - -import org.spongepowered.asm.mixin.Mixin; -import org.spongepowered.asm.mixin.injection.At; -import org.spongepowered.asm.mixin.injection.ModifyArg; - -import net.minecraft.network.packet.c2s.config.SelectKnownPacksC2SPacket; - -import net.fabricmc.fabric.impl.resource.loader.ModResourcePackCreator; - -@Mixin(SelectKnownPacksC2SPacket.class) -public class SelectKnownPacksC2SPacketMixin { - @ModifyArg(method = "", at = @At(value = "INVOKE", target = "Lnet/minecraft/network/codec/PacketCodecs;toList(I)Lnet/minecraft/network/codec/PacketCodec$ResultFunction;")) - private static int setMaxKnownPacks(int constant) { - return ModResourcePackCreator.MAX_KNOWN_PACKS; - } -} diff --git a/fabric-resource-loader-v0/src/main/java/net/fabricmc/fabric/mixin/resource/loader/ServerConfigurationNetworkHandlerMixin.java b/fabric-resource-loader-v0/src/main/java/net/fabricmc/fabric/mixin/resource/loader/ServerConfigurationNetworkHandlerMixin.java deleted file mode 100644 index 842f6340548..00000000000 --- a/fabric-resource-loader-v0/src/main/java/net/fabricmc/fabric/mixin/resource/loader/ServerConfigurationNetworkHandlerMixin.java +++ /dev/null @@ -1,49 +0,0 @@ -/* - * Copyright (c) 2016, 2017, 2018, 2019 FabricMC - * - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ - -package net.fabricmc.fabric.mixin.resource.loader; - -import java.util.List; - -import org.spongepowered.asm.mixin.Mixin; -import org.spongepowered.asm.mixin.injection.At; -import org.spongepowered.asm.mixin.injection.ModifyArg; - -import net.minecraft.network.ClientConnection; -import net.minecraft.registry.VersionedIdentifier; -import net.minecraft.server.MinecraftServer; -import net.minecraft.server.network.ConnectedClientData; -import net.minecraft.server.network.ServerCommonNetworkHandler; -import net.minecraft.server.network.ServerConfigurationNetworkHandler; - -import net.fabricmc.fabric.impl.resource.loader.FabricOriginalKnownPacksGetter; - -@Mixin(ServerConfigurationNetworkHandler.class) -public abstract class ServerConfigurationNetworkHandlerMixin extends ServerCommonNetworkHandler { - public ServerConfigurationNetworkHandlerMixin(MinecraftServer server, ClientConnection connection, ConnectedClientData clientData) { - super(server, connection, clientData); - } - - /** - * Only use packs that were enabled at server start and are enabled now. This avoids a descync when packs have been - * enabled or disabled before the client joins. Since the server registry contents aren't reloaded, we don't want - * the client to use the new data pack data. - */ - @ModifyArg(method = "sendConfigurations", at = @At(value = "INVOKE", target = "Lnet/minecraft/server/network/SynchronizeRegistriesTask;(Ljava/util/List;Lnet/minecraft/registry/CombinedDynamicRegistries;)V", ordinal = 0)) - public List filterKnownPacks(List currentKnownPacks) { - return ((FabricOriginalKnownPacksGetter) this.server).fabric_getOriginalKnownPacks().stream().filter(currentKnownPacks::contains).toList(); - } -} diff --git a/fabric-resource-loader-v0/src/main/java/net/fabricmc/fabric/mixin/resource/loader/ServerPropertiesHandlerMixin.java b/fabric-resource-loader-v0/src/main/java/net/fabricmc/fabric/mixin/resource/loader/ServerPropertiesHandlerMixin.java deleted file mode 100644 index 8d0c01a20fd..00000000000 --- a/fabric-resource-loader-v0/src/main/java/net/fabricmc/fabric/mixin/resource/loader/ServerPropertiesHandlerMixin.java +++ /dev/null @@ -1,34 +0,0 @@ -/* - * Copyright (c) 2016, 2017, 2018, 2019 FabricMC - * - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ - -package net.fabricmc.fabric.mixin.resource.loader; - -import org.spongepowered.asm.mixin.Mixin; -import org.spongepowered.asm.mixin.injection.At; -import org.spongepowered.asm.mixin.injection.Redirect; - -import net.minecraft.resource.DataConfiguration; -import net.minecraft.server.dedicated.ServerPropertiesHandler; - -import net.fabricmc.fabric.impl.resource.loader.ModResourcePackUtil; - -@Mixin(ServerPropertiesHandler.class) -public class ServerPropertiesHandlerMixin { - @Redirect(method = "", at = @At(value = "FIELD", target = "Lnet/minecraft/resource/DataConfiguration;SAFE_MODE:Lnet/minecraft/resource/DataConfiguration;")) - private DataConfiguration replaceDefaultDataConfiguration() { - return ModResourcePackUtil.createDefaultDataConfiguration(); - } -} diff --git a/fabric-resource-loader-v0/src/main/java/net/fabricmc/fabric/mixin/resource/loader/SimpleResourceReloadMixin.java b/fabric-resource-loader-v0/src/main/java/net/fabricmc/fabric/mixin/resource/loader/SimpleResourceReloadMixin.java deleted file mode 100644 index 2c67f46c9bf..00000000000 --- a/fabric-resource-loader-v0/src/main/java/net/fabricmc/fabric/mixin/resource/loader/SimpleResourceReloadMixin.java +++ /dev/null @@ -1,67 +0,0 @@ -/* - * Copyright (c) 2016, 2017, 2018, 2019 FabricMC - * - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ - -package net.fabricmc.fabric.mixin.resource.loader; - -import java.util.List; -import java.util.concurrent.CompletableFuture; -import java.util.concurrent.Executor; - -import org.spongepowered.asm.mixin.Mixin; -import org.spongepowered.asm.mixin.Unique; -import org.spongepowered.asm.mixin.injection.At; -import org.spongepowered.asm.mixin.injection.Inject; -import org.spongepowered.asm.mixin.injection.ModifyArg; -import org.spongepowered.asm.mixin.injection.Redirect; -import org.spongepowered.asm.mixin.injection.callback.CallbackInfoReturnable; - -import net.minecraft.resource.ProfiledResourceReload; -import net.minecraft.resource.ResourceManager; -import net.minecraft.resource.ResourceReload; -import net.minecraft.resource.ResourceReloader; -import net.minecraft.resource.ResourceType; -import net.minecraft.resource.SimpleResourceReload; -import net.minecraft.util.Unit; - -import net.fabricmc.fabric.impl.resource.loader.FabricLifecycledResourceManager; -import net.fabricmc.fabric.impl.resource.loader.ResourceManagerHelperImpl; - -@Mixin(SimpleResourceReload.class) -public class SimpleResourceReloadMixin { - @Unique - private static final ThreadLocal fabric_resourceType = new ThreadLocal<>(); - - @Inject(method = "start", at = @At("HEAD")) - private static void method_40087(ResourceManager resourceManager, List list, Executor executor, Executor executor2, CompletableFuture completableFuture, boolean bl, CallbackInfoReturnable cir) { - if (resourceManager instanceof FabricLifecycledResourceManager flrm) { - fabric_resourceType.set(flrm.fabric_getResourceType()); - } - } - - @ModifyArg(method = "start", index = 1, at = @At(value = "INVOKE", target = "Lnet/minecraft/resource/SimpleResourceReload;create(Lnet/minecraft/resource/ResourceManager;Ljava/util/List;Ljava/util/concurrent/Executor;Ljava/util/concurrent/Executor;Ljava/util/concurrent/CompletableFuture;)Lnet/minecraft/resource/SimpleResourceReload;")) - private static List sortSimple(List reloaders) { - List sorted = ResourceManagerHelperImpl.sort(fabric_resourceType.get(), reloaders); - fabric_resourceType.set(null); - return sorted; - } - - @Redirect(method = "start", at = @At(value = "NEW", target = "(Lnet/minecraft/resource/ResourceManager;Ljava/util/List;Ljava/util/concurrent/Executor;Ljava/util/concurrent/Executor;Ljava/util/concurrent/CompletableFuture;)Lnet/minecraft/resource/ProfiledResourceReload;")) - private static ProfiledResourceReload sortProfiled(ResourceManager manager, List reloaders, Executor prepareExecutor, Executor applyExecutor, CompletableFuture initialStage) { - List sorted = ResourceManagerHelperImpl.sort(fabric_resourceType.get(), reloaders); - fabric_resourceType.set(null); - return new ProfiledResourceReload(manager, sorted, prepareExecutor, applyExecutor, initialStage); - } -} diff --git a/fabric-resource-loader-v0/src/main/java/net/fabricmc/fabric/mixin/resource/loader/SynchronizeRegistriesTaskMixin.java b/fabric-resource-loader-v0/src/main/java/net/fabricmc/fabric/mixin/resource/loader/SynchronizeRegistriesTaskMixin.java deleted file mode 100644 index 8aef37525b3..00000000000 --- a/fabric-resource-loader-v0/src/main/java/net/fabricmc/fabric/mixin/resource/loader/SynchronizeRegistriesTaskMixin.java +++ /dev/null @@ -1,73 +0,0 @@ -/* - * Copyright (c) 2016, 2017, 2018, 2019 FabricMC - * - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ - -package net.fabricmc.fabric.mixin.resource.loader; - -import java.util.HashSet; -import java.util.List; -import java.util.Set; -import java.util.function.Consumer; - -import org.slf4j.Logger; -import org.slf4j.LoggerFactory; -import org.spongepowered.asm.mixin.Final; -import org.spongepowered.asm.mixin.Mixin; -import org.spongepowered.asm.mixin.Shadow; -import org.spongepowered.asm.mixin.Unique; -import org.spongepowered.asm.mixin.injection.At; -import org.spongepowered.asm.mixin.injection.Inject; -import org.spongepowered.asm.mixin.injection.callback.CallbackInfo; - -import net.minecraft.network.packet.Packet; -import net.minecraft.network.packet.s2c.config.SelectKnownPacksS2CPacket; -import net.minecraft.registry.VersionedIdentifier; -import net.minecraft.server.network.SynchronizeRegistriesTask; - -import net.fabricmc.fabric.impl.resource.loader.ModResourcePackCreator; - -@Mixin(SynchronizeRegistriesTask.class) -public abstract class SynchronizeRegistriesTaskMixin { - @Unique - private static final Logger LOGGER = LoggerFactory.getLogger("SynchronizeRegistriesTaskMixin"); - @Shadow - @Final - private List knownPacks; - - @Shadow - protected abstract void syncRegistryAndTags(Consumer> sender, Set commonKnownPacks); - - @Inject(method = "onSelectKnownPacks", at = @At("HEAD"), cancellable = true) - public void onSelectKnownPacks(List clientKnownPacks, Consumer> sender, CallbackInfo ci) { - if (new HashSet<>(this.knownPacks).containsAll(clientKnownPacks)) { - this.syncRegistryAndTags(sender, Set.copyOf(clientKnownPacks)); - ci.cancel(); - } - } - - @Inject(method = "syncRegistryAndTags", at = @At("HEAD")) - public void syncRegistryAndTags(Consumer> sender, Set commonKnownPacks, CallbackInfo ci) { - LOGGER.debug("Synchronizing registries with common known packs: {}", commonKnownPacks); - } - - @Inject(method = "sendPacket", at = @At("HEAD"), cancellable = true) - private void sendPacket(Consumer> sender, CallbackInfo ci) { - if (this.knownPacks.size() > ModResourcePackCreator.MAX_KNOWN_PACKS) { - LOGGER.warn("Too many knownPacks: Found {}; max {}", this.knownPacks.size(), ModResourcePackCreator.MAX_KNOWN_PACKS); - sender.accept(new SelectKnownPacksS2CPacket(this.knownPacks.subList(0, ModResourcePackCreator.MAX_KNOWN_PACKS))); - ci.cancel(); - } - } -} diff --git a/fabric-resource-loader-v0/src/main/java/net/fabricmc/fabric/mixin/resource/loader/TestServerMixin.java b/fabric-resource-loader-v0/src/main/java/net/fabricmc/fabric/mixin/resource/loader/TestServerMixin.java deleted file mode 100644 index e7eab35f978..00000000000 --- a/fabric-resource-loader-v0/src/main/java/net/fabricmc/fabric/mixin/resource/loader/TestServerMixin.java +++ /dev/null @@ -1,39 +0,0 @@ -/* - * Copyright (c) 2016, 2017, 2018, 2019 FabricMC - * - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ - -package net.fabricmc.fabric.mixin.resource.loader; - -import java.util.List; - -import org.spongepowered.asm.mixin.Mixin; -import org.spongepowered.asm.mixin.injection.At; -import org.spongepowered.asm.mixin.injection.Redirect; - -import net.minecraft.resource.DataPackSettings; -import net.minecraft.test.TestServer; - -import net.fabricmc.fabric.impl.resource.loader.ModResourcePackUtil; - -/** - * @see ModResourcePackUtil#createTestServerSettings - */ -@Mixin(TestServer.class) -public class TestServerMixin { - @Redirect(method = "create", at = @At(value = "NEW", target = "(Ljava/util/List;Ljava/util/List;)Lnet/minecraft/resource/DataPackSettings;")) - private static DataPackSettings replaceDefaultDataPackSettings(List enabled, List disabled) { - return ModResourcePackUtil.createTestServerSettings(enabled, disabled); - } -} diff --git a/fabric-resource-loader-v0/src/main/java/net/fabricmc/fabric/mixin/resource/loader/server/LanguageMixin.java b/fabric-resource-loader-v0/src/main/java/net/fabricmc/fabric/mixin/resource/loader/server/LanguageMixin.java deleted file mode 100644 index c7c6d2cd2b7..00000000000 --- a/fabric-resource-loader-v0/src/main/java/net/fabricmc/fabric/mixin/resource/loader/server/LanguageMixin.java +++ /dev/null @@ -1,69 +0,0 @@ -/* - * Copyright (c) 2016, 2017, 2018, 2019 FabricMC - * - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ - -package net.fabricmc.fabric.mixin.resource.loader.server; - -import java.io.IOException; -import java.io.InputStream; -import java.nio.file.Files; -import java.nio.file.Path; -import java.util.HashMap; -import java.util.Map; -import java.util.function.BiConsumer; - -import com.google.common.collect.ImmutableMap; -import com.google.gson.JsonParseException; -import org.slf4j.Logger; -import org.spongepowered.asm.mixin.Final; -import org.spongepowered.asm.mixin.Mixin; -import org.spongepowered.asm.mixin.Shadow; -import org.spongepowered.asm.mixin.injection.At; -import org.spongepowered.asm.mixin.injection.Redirect; - -import net.minecraft.util.Language; - -import net.fabricmc.fabric.impl.resource.loader.ServerLanguageUtil; - -@Mixin(Language.class) -class LanguageMixin { - @Shadow - @Final - private static Logger LOGGER; - - @Redirect(method = "create", at = @At(value = "INVOKE", target = "Lcom/google/common/collect/ImmutableMap$Builder;build()Lcom/google/common/collect/ImmutableMap;", remap = false)) - private static ImmutableMap create(ImmutableMap.Builder cir) { - Map map = new HashMap<>(cir.buildOrThrow()); - - for (Path path : ServerLanguageUtil.getModLanguageFiles()) { - loadFromPath(path, map::put); - } - - return ImmutableMap.copyOf(map); - } - - private static void loadFromPath(Path path, BiConsumer entryConsumer) { - try (InputStream stream = Files.newInputStream(path)) { - LOGGER.debug("Loading translations from {}", path); - load(stream, entryConsumer); - } catch (JsonParseException | IOException e) { - LOGGER.error("Couldn't read strings from {}", path, e); - } - } - - @Shadow - public static void load(InputStream inputStream, BiConsumer entryConsumer) { - } -} diff --git a/fabric-resource-loader-v0/src/main/resources/fabric-resource-loader-v0.mixins.json b/fabric-resource-loader-v0/src/main/resources/fabric-resource-loader-v0.mixins.json index 04c6747b739..50f8d82a3dc 100644 --- a/fabric-resource-loader-v0/src/main/resources/fabric-resource-loader-v0.mixins.json +++ b/fabric-resource-loader-v0/src/main/resources/fabric-resource-loader-v0.mixins.json @@ -3,22 +3,9 @@ "package": "net.fabricmc.fabric.mixin.resource.loader", "compatibilityLevel": "JAVA_17", "mixins": [ - "DatapackCommandMixin", "KeyedResourceReloadListenerMixin", - "LifecycledResourceManagerImplMixin", - "MinecraftServerMixin", "ResourceMixin", - "ResourcePackManagerMixin", - "ResourcePackProfileMixin", - "SelectKnownPacksC2SPacketMixin", - "ServerConfigurationNetworkHandlerMixin", - "ServerPropertiesHandlerMixin", - "SimpleResourceReloadMixin", - "SynchronizeRegistriesTaskMixin", - "TestServerMixin" - ], - "server": [ - "server.LanguageMixin" + "ResourcePackProfileMixin" ], "injectors": { "defaultRequire": 1 diff --git a/fabric-resource-loader-v0/src/test/java/net/fabricmc/fabric/test/resource/loader/unit/ModResourcePackUtilTests.java b/fabric-resource-loader-v0/src/test/java/net/fabricmc/fabric/test/resource/loader/unit/ModResourcePackUtilTests.java deleted file mode 100644 index 94925b1a5ae..00000000000 --- a/fabric-resource-loader-v0/src/test/java/net/fabricmc/fabric/test/resource/loader/unit/ModResourcePackUtilTests.java +++ /dev/null @@ -1,255 +0,0 @@ -/* - * Copyright (c) 2016, 2017, 2018, 2019 FabricMC - * - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ - -package net.fabricmc.fabric.test.resource.loader.unit; - -import static org.junit.jupiter.api.Assertions.assertDoesNotThrow; -import static org.junit.jupiter.api.Assertions.assertEquals; - -import java.util.ArrayList; -import java.util.List; -import java.util.Map; -import java.util.Optional; -import java.util.Set; -import java.util.TreeMap; -import java.util.function.Predicate; - -import com.google.gson.Gson; -import com.google.gson.JsonObject; -import com.google.gson.JsonParseException; -import org.jetbrains.annotations.Nullable; -import org.junit.jupiter.api.BeforeAll; -import org.junit.jupiter.api.Test; - -import net.minecraft.Bootstrap; -import net.minecraft.SharedConstants; -import net.minecraft.resource.ResourcePackInfo; -import net.minecraft.resource.ResourcePackPosition; -import net.minecraft.resource.ResourcePackProfile; - -import net.fabricmc.fabric.impl.resource.loader.FabricResourcePackProfile; -import net.fabricmc.fabric.impl.resource.loader.ModResourcePackCreator; -import net.fabricmc.fabric.impl.resource.loader.ModResourcePackUtil; - -public class ModResourcePackUtilTests { - private static final Gson GSON = new Gson(); - - @BeforeAll - static void beforeAll() { - SharedConstants.createGameVersion(); - Bootstrap.initialize(); - } - - @Test - void testRefreshAutoEnabledPacks() { - // Vanilla uses tree map, and we test the behavior - Map profiles = new TreeMap<>(); - Map modAProfiles = new TreeMap<>(); - Map modBProfiles = new TreeMap<>(); - Map allProfiles = new TreeMap<>(); - ResourcePackProfile vanilla = mockProfile(profiles, "vanilla", null); - ResourcePackProfile fabric = mockProfile(profiles, ModResourcePackCreator.FABRIC, null); - ResourcePackProfile modA = mockProfile(modAProfiles, "mod_a", ModResourcePackCreator.BASE_PARENT); - ResourcePackProfile modAProg = mockProfile(modAProfiles, "mod_a_programmer_art", ModResourcePackCreator.PROGRAMMER_ART_PARENT); - ResourcePackProfile modAHigh = mockProfile(modAProfiles, "mod_a_high_contrast", ModResourcePackCreator.HIGH_CONTRAST_PARENT); - ResourcePackProfile modB = mockProfile(modBProfiles, "mod_b", ModResourcePackCreator.BASE_PARENT); - ResourcePackProfile modBProg = mockProfile(modBProfiles, "mod_b_programmer_art", ModResourcePackCreator.PROGRAMMER_ART_PARENT); - ResourcePackProfile modBHigh = mockProfile(modBProfiles, "mod_b_high_contrast", ModResourcePackCreator.HIGH_CONTRAST_PARENT); - ResourcePackProfile programmerArt = mockProfile(profiles, "programmer_art", null); - ResourcePackProfile highContrast = mockProfile(profiles, "high_contrast", null); - ResourcePackProfile userPackA = mockProfile(profiles, "user_pack_a", null); - ResourcePackProfile userPackB = mockProfile(profiles, "user_pack_b", null); - modAProfiles.putAll(profiles); - modBProfiles.putAll(profiles); - allProfiles.putAll(modAProfiles); - allProfiles.putAll(modBProfiles); - - testRefreshAutoEnabledPacks( - profiles, - List.of(vanilla, fabric), - List.of(vanilla, fabric), - "keep (no mods)" - ); - testRefreshAutoEnabledPacks( - profiles, - List.of(vanilla, fabric, userPackA), - List.of(vanilla, fabric, userPackA), - "keep (no mods, keep user pack)" - ); - testRefreshAutoEnabledPacks( - modAProfiles, - List.of(vanilla, fabric, modA), - List.of(vanilla, fabric, modA), - "keep (mod A only)" - ); - testRefreshAutoEnabledPacks( - modAProfiles, - List.of(vanilla, fabric, modA, programmerArt, modAProg), - List.of(vanilla, fabric, modA, programmerArt, modAProg), - "keep (programmer_art)" - ); - testRefreshAutoEnabledPacks( - allProfiles, - List.of(vanilla, fabric, modA, modB, programmerArt, modAProg, modBProg), - List.of(vanilla, fabric, modA, modB, programmerArt, modAProg, modBProg), - "keep (mod A and mod B, programmer_art)" - ); - testRefreshAutoEnabledPacks( - allProfiles, - List.of(vanilla, fabric, modA, modB, programmerArt, modAProg, modBProg, highContrast, modAHigh, modBHigh), - List.of(vanilla, fabric, modA, modB, programmerArt, modAProg, modBProg, highContrast, modAHigh, modBHigh), - "keep (mod A and mod B, both)" - ); - testRefreshAutoEnabledPacks( - allProfiles, - List.of(vanilla, fabric, modA, modB, highContrast, modAHigh, modBHigh, programmerArt, modAProg, modBProg), - List.of(vanilla, fabric, modA, modB, highContrast, modAHigh, modBHigh, programmerArt, modAProg, modBProg), - "keep (remembers programmer_art-high_contrast order)" - ); - testRefreshAutoEnabledPacks( - modAProfiles, - List.of(vanilla, fabric), - List.of(vanilla, fabric, modA), - "fix (adding missing mods)" - ); - testRefreshAutoEnabledPacks( - allProfiles, - List.of(vanilla, fabric, userPackA), - List.of(vanilla, fabric, modA, modB, userPackA), - "fix (adding missing mods at the right place)" - ); - testRefreshAutoEnabledPacks( - allProfiles, - List.of(vanilla, fabric, modB, modA), - List.of(vanilla, fabric, modA, modB), - "fix (mod A and B, sorting)" - ); - testRefreshAutoEnabledPacks( - modAProfiles, - List.of(vanilla, fabric, userPackB, modA, userPackA), - List.of(vanilla, fabric, modA, userPackB, userPackA), - "fix (user pack goes last)" - ); - testRefreshAutoEnabledPacks( - modAProfiles, - List.of(vanilla, fabric, modA, programmerArt), - List.of(vanilla, fabric, modA, programmerArt, modAProg), - "fix (adding 1 met dep)" - ); - testRefreshAutoEnabledPacks( - modBProfiles, - List.of(vanilla, fabric, modB, highContrast), - List.of(vanilla, fabric, modB, highContrast, modBHigh), - "fix (adding 1 met dep, part 2)" - ); - testRefreshAutoEnabledPacks( - modAProfiles, - List.of(vanilla, fabric, modA, programmerArt, highContrast), - List.of(vanilla, fabric, modA, programmerArt, modAProg, highContrast, modAHigh), - "fix (adding 2 met deps)" - ); - testRefreshAutoEnabledPacks( - modAProfiles, - List.of(vanilla, fabric, modA, programmerArt, modAProg, highContrast), - List.of(vanilla, fabric, modA, programmerArt, modAProg, highContrast, modAHigh), - "fix (adding 2 met deps + preexisting)" - ); - testRefreshAutoEnabledPacks( - modAProfiles, - List.of(vanilla, fabric, modA, modAProg, modAHigh), - List.of(vanilla, fabric, modA), - "fix (removing 2 unmet deps)" - ); - testRefreshAutoEnabledPacks( - modAProfiles, - List.of(vanilla, fabric, modA, programmerArt, modAProg, modAHigh), - List.of(vanilla, fabric, modA, programmerArt, modAProg), - "fix (removing 1 unmet dep)" - ); - testRefreshAutoEnabledPacks( - modBProfiles, - List.of(vanilla, fabric, modB, highContrast, modBProg, modBHigh), - List.of(vanilla, fabric, modB, highContrast, modBHigh), - "fix (removing 1 unmet dep, part 2)" - ); - testRefreshAutoEnabledPacks( - modAProfiles, - List.of(vanilla, fabric, modAProg, programmerArt, modA), - List.of(vanilla, fabric, modA, programmerArt, modAProg), - "reorder (bundled comes just after parents)" - ); - testRefreshAutoEnabledPacks( - modAProfiles, - List.of(vanilla, fabric, modAProg, userPackA, programmerArt, modA, userPackB), - List.of(vanilla, fabric, modA, userPackA, programmerArt, modAProg, userPackB), - "reorder (keep user pack order)" - ); - testRefreshAutoEnabledPacks( - modAProfiles, - List.of(vanilla, fabric, userPackB, modA, programmerArt, userPackA, modAProg), - List.of(vanilla, fabric, modA, userPackB, programmerArt, modAProg, userPackA), - "reorder (no user pack between parent-bundled)" - ); - } - - private ResourcePackProfile mockProfile(Map profiles, String id, @Nullable Predicate> parents) { - ResourcePackProfile profile = new ResourcePackProfile( - new ResourcePackInfo( - id, - null, - null, - Optional.empty() - ), - null, - null, - new ResourcePackPosition( - false, - null, - false) - ); - - if (parents != null) ((FabricResourcePackProfile) profile).fabric_setParentsPredicate(parents); - - profiles.put(id, profile); - return profile; - } - - private void testRefreshAutoEnabledPacks(Map profiles, List before, List after, String reason) { - List processed = new ArrayList<>(before); - ModResourcePackUtil.refreshAutoEnabledPacks(processed, profiles); - assertEquals( - after.stream().map(ResourcePackProfile::getId).toList(), - processed.stream().map(ResourcePackProfile::getId).toList(), - () -> "Testing %s; input %s".formatted(reason, before.stream().map(ResourcePackProfile::getId).toList()) - ); - } - - @Test - void testSerializeMetadata() { - // Test various metadata serialization issues (#2407) - testMetadataSerialization(""); - testMetadataSerialization("Quotes: \"\" \""); - testMetadataSerialization("Backslash: \\ \\\\"); - } - - private void testMetadataSerialization(String description) throws JsonParseException { - String metadata = ModResourcePackUtil.serializeMetadata(1, description); - JsonObject json = assertDoesNotThrow(() -> GSON.fromJson(metadata, JsonObject.class), () -> "Failed to serialize " + description); - - String parsedDescription = json.get("pack").getAsJsonObject().get("description").getAsString(); - assertEquals(description, parsedDescription, "Parsed description differs from original one"); - } -}