Skip to content

Commit

Permalink
Another way to load listeners early
Browse files Browse the repository at this point in the history
It's horrible, but it works, and it's wonderful

Fixes #106
Maybe fix #90 (PepperCode1/Continuity#446)
  • Loading branch information
dima-dencep committed Aug 10, 2024
1 parent 988e01d commit 24a0360
Show file tree
Hide file tree
Showing 5 changed files with 140 additions and 80 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,138 @@
/*
* Copyright 2023 - 2024 dima_dencep.
*
* Licensed under the Open Software License, Version 3.0 (the "License");
* you may not use this file except in compliance with the License.
*
* You may obtain a copy of the License at
* https://spdx.org/licenses/OSL-3.0.txt
*/

package org.redlance.dima_dencep.mods.rrls.mixins.workaround;

import com.google.common.collect.Lists;
import net.minecraft.Util;
import net.minecraft.client.Minecraft;
import net.minecraft.client.gui.GuiSpriteManager;
import net.minecraft.client.gui.font.FontManager;
import net.minecraft.client.resources.SplashManager;
import net.minecraft.client.resources.language.LanguageManager;
import net.minecraft.server.packs.PackResources;
import net.minecraft.server.packs.PackType;
import net.minecraft.server.packs.resources.CloseableResourceManager;
import net.minecraft.server.packs.resources.MultiPackResourceManager;
import net.minecraft.server.packs.resources.PreparableReloadListener;
import net.minecraft.server.packs.resources.ReloadInstance;
import net.minecraft.server.packs.resources.ReloadableResourceManager;
import net.minecraft.util.Unit;
import net.minecraft.util.profiling.InactiveProfiler;
import org.redlance.dima_dencep.mods.rrls.ConfigExpectPlatform;
import org.redlance.dima_dencep.mods.rrls.Rrls;
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 org.spongepowered.asm.mixin.injection.callback.CallbackInfoReturnable;

import java.util.List;
import java.util.concurrent.CompletableFuture;
import java.util.concurrent.Executor;
import java.util.function.BiConsumer;

@Mixin(ReloadableResourceManager.class)
public class ReloadableResourceManagerMixin {
@Shadow
private CloseableResourceManager resources;

@Unique
private static final Minecraft RRLS$MINECRAFT = Minecraft.getInstance();

@Unique
private final List<PreparableReloadListener> rrls$listeners = Lists.newArrayList();

@Inject(
method = "registerReloadListener",
at = @At(
value = "TAIL"
)
)
public void rrls$initReloader(PreparableReloadListener listener, CallbackInfo ci) {
if (!ConfigExpectPlatform.hideType().forceClose()) {
return;
}

if (listener instanceof FontManager fontManager &&
!fontManager.fontSets.containsKey(Minecraft.DEFAULT_FONT)
) {
rrls$reloadListener(fontManager, RRLS$MINECRAFT,
(unused, throwable) -> rrls$refreshScreen()
);
}

if (listener instanceof LanguageManager languageManager &&
languageManager.getLanguages().size() <= 1
) {
rrls$reloadListener(languageManager, Util.backgroundExecutor(), (unused, throwable) -> {});
}

if (listener instanceof SplashManager splashManager &&
splashManager.splashes.isEmpty()
) {
rrls$reloadListener(splashManager, Util.backgroundExecutor(),
(unused, throwable) -> rrls$refreshScreen()
);
}

if (listener instanceof GuiSpriteManager spriteManager &&
spriteManager.textureAtlas.texturesByName.isEmpty()
) {
rrls$reloadListener(spriteManager, RRLS$MINECRAFT, (unused, throwable) -> {});
}
}

@Inject(
method = "createReload",
at = @At(
value = "INVOKE",
target = "Lnet/minecraft/server/packs/resources/SimpleReloadInstance;create(Lnet/minecraft/server/packs/resources/ResourceManager;Ljava/util/List;Ljava/util/concurrent/Executor;Ljava/util/concurrent/Executor;Ljava/util/concurrent/CompletableFuture;Z)Lnet/minecraft/server/packs/resources/ReloadInstance;"
)
)
private void rrls$initReloaders(Executor backgroundExecutor, Executor gameExecutor, CompletableFuture<Unit> waitingFor, List<PackResources> resourcePacks, CallbackInfoReturnable<ReloadInstance> cir) {
for (PreparableReloadListener listener : this.rrls$listeners) {
rrls$reloadListener(listener, RRLS$MINECRAFT, (unused, throwable) -> {});
}
this.rrls$listeners.clear();
}

@Unique
private void rrls$reloadListener(PreparableReloadListener listener, Executor gameExecutor, BiConsumer<Void, Throwable> action) {
try {
if (this.resources.getNamespaces().isEmpty()) {
this.resources = new MultiPackResourceManager(PackType.CLIENT_RESOURCES,
RRLS$MINECRAFT.getResourcePackRepository().openAllSelected()
);
}

Rrls.LOGGER.info("Quick reload listener '{}'", listener.getName());

listener.reload(
CompletableFuture::completedFuture, (ReloadableResourceManager) (Object) this, InactiveProfiler.INSTANCE,
InactiveProfiler.INSTANCE, Util.backgroundExecutor(), gameExecutor
).whenCompleteAsync(action, Util.backgroundExecutor());

} catch (Throwable th) {
this.rrls$listeners.add(listener);
}
}

@Unique
private void rrls$refreshScreen() {
if (ConfigExpectPlatform.reInitScreen() && RRLS$MINECRAFT.screen != null) {
RRLS$MINECRAFT.screen.init(RRLS$MINECRAFT,
RRLS$MINECRAFT.getWindow().getGuiScaledWidth(), RRLS$MINECRAFT.getWindow().getGuiScaledHeight()
);
}
}
}

This file was deleted.

1 change: 0 additions & 1 deletion common/src/main/resources/rrls.accesswidener
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
accessWidener v2 named
extendable method net/minecraft/server/packs/resources/SimplePreparableReloadListener reload (Lnet/minecraft/server/packs/resources/PreparableReloadListener$PreparationBarrier;Lnet/minecraft/server/packs/resources/ResourceManager;Lnet/minecraft/util/profiling/ProfilerFiller;Lnet/minecraft/util/profiling/ProfilerFiller;Ljava/util/concurrent/Executor;Ljava/util/concurrent/Executor;)Ljava/util/concurrent/CompletableFuture;
accessible method net/minecraft/client/gui/GuiGraphics flushIfUnmanaged ()V
extendable method net/minecraft/client/gui/GuiGraphics flushIfUnmanaged ()V
accessible method net/minecraft/client/gui/GuiGraphics flushIfManaged ()V
Expand Down
2 changes: 1 addition & 1 deletion common/src/main/resources/rrls.mixins.json
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@
"compat.OverlayMixin",
"compat.ServerPackManagerMixin",
"workaround.GuiGraphicsMixin",
"workaround.SimpleReloadInstanceMixin",
"workaround.ReloadableResourceManagerMixin",
"workaround.VertexBufferMixin",
"workaround.EntityRenderDispatcherMixin"
],
Expand Down
2 changes: 1 addition & 1 deletion gradle.properties
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ org.gradle.parallel=true
forge_version = 21.0.167

# Mod Properties
mod_version = 5.0.6
mod_version = 5.0.7
maven_group = org.redlance.dima_dencep.mods
archives_base_name = rrls

Expand Down

0 comments on commit 24a0360

Please sign in to comment.