Skip to content

Commit

Permalink
Fix accessing existing reload listeners in server data reload
Browse files Browse the repository at this point in the history
  • Loading branch information
Su5eD committed Sep 5, 2023
1 parent 1c0ea72 commit b48e1c2
Show file tree
Hide file tree
Showing 8 changed files with 147 additions and 69 deletions.

This file was deleted.

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.resource.loader;

import net.minecraft.resource.ResourceType;

public interface FabricLifecycledResourceManager {
ResourceType fabric_getResourceType();
}
Original file line number Diff line number Diff line change
Expand Up @@ -16,40 +16,19 @@

package net.fabricmc.fabric.impl.resource.loader;

import java.util.List;

import net.minecraftforge.api.distmarker.Dist;
import net.minecraftforge.common.MinecraftForge;
import net.minecraftforge.event.AddPackFindersEvent;
import net.minecraftforge.event.AddReloadListenerEvent;
import net.minecraftforge.eventbus.api.IEventBus;
import net.minecraftforge.fml.common.Mod;
import net.minecraftforge.fml.javafmlmod.FMLJavaModLoadingContext;
import net.minecraftforge.fml.loading.FMLLoader;

import net.minecraft.resource.ResourceReloader;
import net.minecraft.resource.ResourceType;

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

@Mod("fabric_resource_loader_v0")
public class ResourceLoaderImpl {
public ResourceLoaderImpl() {
IEventBus bus = FMLJavaModLoadingContext.get().getModEventBus();
if (FMLLoader.getDist() == Dist.CLIENT) {
// Run first
bus.addListener(ResourceLoaderClient::onClientResourcesReload);
}
bus.addListener(ResourceLoaderImpl::addPackFinders);
MinecraftForge.EVENT_BUS.addListener(ResourceLoaderImpl::onServerDataReload);
}

private static void addPackFinders(AddPackFindersEvent event) {
event.addRepositorySource(new ModResourcePackCreator(event.getPackType()));
}

private static void onServerDataReload(AddReloadListenerEvent event) {
List<ResourceReloader> listeners = ResourceManagerHelperImpl.sort(ResourceType.SERVER_DATA, event.getListeners());
listeners.forEach(event::addListener);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -134,15 +134,14 @@ public static List<ResourceReloader> sort(ResourceType type, List<ResourceReload

if (instance != null) {
List<ResourceReloader> mutable = new ArrayList<>(listeners);
return Collections.unmodifiableList(instance.sort(mutable));
instance.sort(mutable);
return Collections.unmodifiableList(mutable);
}

return listeners;
}

protected List<ResourceReloader> sort(List<ResourceReloader> listeners) {
List<ResourceReloader> fabricListeners = new ArrayList<>();

protected void sort(List<ResourceReloader> listeners) {
listeners.removeAll(addedListeners);

// General rules:
Expand All @@ -162,8 +161,8 @@ protected List<ResourceReloader> sort(List<ResourceReloader> listeners) {

int lastSize = -1;

while (fabricListeners.size() != lastSize) {
lastSize = fabricListeners.size();
while (listeners.size() != lastSize) {
lastSize = listeners.size();

Iterator<IdentifiableResourceReloadListener> it = listenersToAdd.iterator();

Expand All @@ -172,7 +171,7 @@ protected List<ResourceReloader> sort(List<ResourceReloader> listeners) {

if (resolvedIds.containsAll(listener.getFabricDependencies())) {
resolvedIds.add(listener.getFabricId());
fabricListeners.add(listener);
listeners.add(listener);
it.remove();
}
}
Expand All @@ -181,8 +180,6 @@ protected List<ResourceReloader> sort(List<ResourceReloader> listeners) {
for (IdentifiableResourceReloadListener listener : listenersToAdd) {
LOGGER.warn("Could not resolve dependencies for listener: " + listener.getFabricId() + "!");
}

return fabricListeners;
}

@Override
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
/*
* 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 = "<init>", at = @At("TAIL"))
private void init(ResourceType resourceType, List<ResourcePack> list, CallbackInfo ci) {
this.fabric_ResourceType = resourceType;
}

@Override
public ResourceType fabric_getResourceType() {
return fabric_ResourceType;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,67 @@
/*
* 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<ResourceType> fabric_resourceType = new ThreadLocal<>();

@Inject(method = "start", at = @At("HEAD"))
private static void method_40087(ResourceManager resourceManager, List<ResourceReloader> list, Executor executor, Executor executor2, CompletableFuture<Unit> completableFuture, boolean bl, CallbackInfoReturnable<ResourceReload> 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<ResourceReloader> sortSimple(List<ResourceReloader> reloaders) {
List<ResourceReloader> sorted = ResourceManagerHelperImpl.sort(fabric_resourceType.get(), reloaders);
fabric_resourceType.set(null);
return sorted;
}

@Redirect(method = "start", at = @At(value = "NEW", target = "Lnet/minecraft/resource/ProfiledResourceReload;<init>"))
private static ProfiledResourceReload sortProfiled(ResourceManager manager, List<ResourceReloader> reloaders, Executor prepareExecutor, Executor applyExecutor, CompletableFuture<Unit> initialStage) {
List<ResourceReloader> sorted = ResourceManagerHelperImpl.sort(fabric_resourceType.get(), reloaders);
fabric_resourceType.set(null);
return new ProfiledResourceReload(manager, sorted, prepareExecutor, applyExecutor, initialStage);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,9 @@
"mixins": [
"KeyedResourceReloadListenerMixin",
"NamespaceResourceManagerMixin",
"ReloadableResourceManagerImplMixin"
"ReloadableResourceManagerImplMixin",
"LifecycledResourceManagerImplMixin",
"SimpleResourceReloadMixin"
],
"injectors": {
"defaultRequire": 1
Expand Down
2 changes: 1 addition & 1 deletion gradle.properties
Original file line number Diff line number Diff line change
Expand Up @@ -67,5 +67,5 @@ fabric-client-tags-api-v1-version=1.1.1
loom.platform=forge
forge_version=1.20.1-47.1.3
pack_format=15
forgified_version=1.9.8
forgified_version=1.9.9
forge_fabric_loader_version=2.2.1+0.14.21+1.20.1

0 comments on commit b48e1c2

Please sign in to comment.