Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Refactor resource loader internals #3473

Merged
merged 24 commits into from
Jan 28, 2024
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
Show all changes
24 commits
Select commit Hold shift + click to select a range
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
/*
* 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.client.resource.loader;

public interface FabricAbstractPack {
default boolean isHidden() {
return false;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
/*
* 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 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.gui.screen.pack.PackListWidget;
import net.minecraft.client.gui.screen.pack.PackScreen;
import net.minecraft.client.gui.screen.pack.ResourcePackOrganizer;

import net.fabricmc.fabric.impl.client.resource.loader.FabricAbstractPack;

@Mixin(PackScreen.class)
public class PackScreenMixin {
@Inject(method = "method_29672", at = @At("HEAD"), cancellable = true)
private void hideHiddenPacks(PackListWidget packListWidget, String string, ResourcePackOrganizer.Pack pack, CallbackInfo ci) {
// (Fabric)AbstractPack is the only vanilla implementation of Pack, but we cast anyway.
if (pack instanceof FabricAbstractPack fabricAbstractPack && fabricAbstractPack.isHidden()) {
apple502j marked this conversation as resolved.
Show resolved Hide resolved
ci.cancel();
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
/*
* 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 org.spongepowered.asm.mixin.Final;
import org.spongepowered.asm.mixin.Mixin;
import org.spongepowered.asm.mixin.Shadow;

import net.minecraft.resource.ResourcePackProfile;

import net.fabricmc.fabric.impl.client.resource.loader.FabricAbstractPack;

@Mixin(targets = "net/minecraft/client/gui/screen/pack/ResourcePackOrganizer$AbstractPack")
public class ResourcePackOrganizerAbstractPackMixin implements FabricAbstractPack {
@Shadow
@Final
private ResourcePackProfile profile;

@Override
public boolean isHidden() {
return this.profile.isHidden();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,13 @@
"package": "net.fabricmc.fabric.mixin.resource.loader.client",
"compatibilityLevel": "JAVA_17",
"client": [
"VanillaResourcePackProviderMixin",
"CreateWorldScreenMixin",
"FontManagerMixin",
"GameOptionsMixin",
"KeyedResourceReloadListenerClientMixin"
"KeyedResourceReloadListenerClientMixin",
"PackScreenMixin",
"ResourcePackOrganizerAbstractPackMixin",
"VanillaResourcePackProviderMixin"
],
"injectors": {
"defaultRequire": 1
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,66 @@
/*
* 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;

/**
* 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;getEnabledNames()Ljava/util/Collection;"))
private static Collection<String> filterEnabledPackSuggestions(ResourcePackManager dataPackManager) {
return dataPackManager.getEnabledProfiles().stream().filter(Predicate.not(ResourcePackProfile::isHidden)).map(ResourcePackProfile::getName).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<ResourcePackProfile> filterDisabledPackSuggestions(Stream<ResourcePackProfile> instance, Predicate<? super ResourcePackProfile> predicate, Operation<Stream<ResourcePackProfile>> original) {
return original.call(instance, predicate).filter(Predicate.not(ResourcePackProfile::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<ServerCommandSource> context, String name, boolean enable, CallbackInfoReturnable<ResourcePackProfile> cir, @Local ResourcePackProfile profile) throws CommandSyntaxException {
if (profile.isHidden()) throw INTERNAL_PACK_EXCEPTION.create(name);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -4,5 +4,6 @@
"pack.source.builtinMod": "built-in: %s",
"pack.name.fabricMod": "Fabric Mod \"%s\"",
"pack.name.fabricMods": "Fabric Mods",
"pack.name.fabricMod.subPack": "Fabric Mod \"%s\" (%s)"
"pack.name.fabricMod.subPack": "Fabric Mod \"%s\" (%s)",
"commands.datapack.fabric.internal": "Cannot enable or disable Fabric internal pack \"%s\"."
}
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
"package": "net.fabricmc.fabric.mixin.resource.loader",
"compatibilityLevel": "JAVA_17",
"mixins": [
"DatapackCommandMixin",
"KeyedResourceReloadListenerMixin",
"LifecycledResourceManagerImplMixin",
"MinecraftServerMixin",
Expand Down
3 changes: 2 additions & 1 deletion fabric-resource-loader-v0/src/main/resources/fabric.mod.json
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,8 @@
"custom": {
"fabric-api:module-lifecycle": "stable",
"loom:injected_interfaces": {
"net/minecraft/class_3288": ["net/fabricmc/fabric/impl/resource/loader/FabricResourcePackProfile"]
"net/minecraft/class_3288": ["net/fabricmc/fabric/impl/resource/loader/FabricResourcePackProfile"],
"net/minecraft/class_5369$class_5372": ["net/fabricmc/fabric/impl/client/resource/loader/FabricAbstractPack"]
}
}
}
Loading