Skip to content

Commit 72a653e

Browse files
committed
Merge branch 'experiment/loading-screen' into 1.20
2 parents 59235e6 + 73d2a44 commit 72a653e

File tree

2 files changed

+110
-0
lines changed

2 files changed

+110
-0
lines changed
Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
1+
package org.embeddedt.modernfix.forge.mixin.feature.registry_event_progress;
2+
3+
import net.minecraftforge.eventbus.api.Event;
4+
import net.minecraftforge.fml.ModList;
5+
import net.minecraftforge.fml.ModLoader;
6+
import net.minecraftforge.fml.ModLoadingContext;
7+
import net.minecraftforge.fml.StartupMessageManager;
8+
import net.minecraftforge.fml.event.IModBusEvent;
9+
import net.minecraftforge.registries.GameData;
10+
import net.minecraftforge.registries.RegisterEvent;
11+
import org.embeddedt.modernfix.annotation.ClientOnlyMixin;
12+
import org.embeddedt.modernfix.forge.util.AsyncLoadingScreen;
13+
import org.spongepowered.asm.mixin.Mixin;
14+
import org.spongepowered.asm.mixin.injection.At;
15+
import org.spongepowered.asm.mixin.injection.Inject;
16+
import org.spongepowered.asm.mixin.injection.Redirect;
17+
import org.spongepowered.asm.mixin.injection.callback.CallbackInfo;
18+
19+
@Mixin(value = GameData.class, remap = false)
20+
@ClientOnlyMixin
21+
public class GameDataMixin {
22+
23+
private static AsyncLoadingScreen mfix$asyncScreen;
24+
25+
@Inject(method = "postRegisterEvents", at = @At(value = "INVOKE", target = "Ljava/util/Set;iterator()Ljava/util/Iterator;", ordinal = 0))
26+
private static void createAsyncScreen(CallbackInfo ci) {
27+
mfix$asyncScreen = new AsyncLoadingScreen();
28+
}
29+
30+
@Inject(method = "postRegisterEvents", at = @At(value = "INVOKE", target = "Ljava/lang/RuntimeException;getSuppressed()[Ljava/lang/Throwable;", ordinal = 0))
31+
private static void closeAsyncScreen(CallbackInfo ci) {
32+
mfix$asyncScreen.close();
33+
mfix$asyncScreen = null;
34+
}
35+
36+
@Redirect(method = "postRegisterEvents", at = @At(value = "INVOKE", target = "Lnet/minecraftforge/fml/ModLoader;postEventWrapContainerInModOrder(Lnet/minecraftforge/eventbus/api/Event;)V"))
37+
private static <T extends Event & IModBusEvent> void swapThreadAndPost(ModLoader loader, T event) {
38+
RegisterEvent registryEvent = (RegisterEvent)event;
39+
var pb = StartupMessageManager.addProgressBar(registryEvent.getRegistryKey().location().toString(), ModList.get().size());
40+
try {
41+
loader.postEventWithWrapInModOrder(event, (mc, e) -> {
42+
ModLoadingContext.get().setActiveContainer(mc);
43+
pb.label(pb.name() + " - " + mc.getModInfo().getDisplayName());
44+
pb.increment();
45+
}, (mc, e) -> {
46+
ModLoadingContext.get().setActiveContainer(null);
47+
});
48+
} finally {
49+
pb.complete();
50+
}
51+
}
52+
}
Lines changed: 58 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,58 @@
1+
package org.embeddedt.modernfix.forge.util;
2+
3+
import net.minecraftforge.fml.loading.ImmediateWindowHandler;
4+
import org.lwjgl.glfw.GLFW;
5+
import org.lwjgl.opengl.GL;
6+
import org.lwjgl.opengl.GLCapabilities;
7+
8+
import java.util.concurrent.TimeUnit;
9+
import java.util.concurrent.atomic.AtomicBoolean;
10+
import java.util.concurrent.locks.LockSupport;
11+
12+
public class AsyncLoadingScreen extends Thread implements AutoCloseable {
13+
private final long theWindow;
14+
private final AtomicBoolean keepRunning;
15+
16+
private static int splashThreadNum = 1;
17+
18+
private static GLCapabilities caps;
19+
20+
public AsyncLoadingScreen() {
21+
this.setName("ModernFix splash thread " + splashThreadNum++);
22+
this.theWindow = GLFW.glfwGetCurrentContext();
23+
if(caps == null)
24+
caps = GL.createCapabilities();
25+
if(this.theWindow == 0)
26+
throw new IllegalStateException("No context found but async loading screen was requested");
27+
this.keepRunning = new AtomicBoolean(true);
28+
this.start();
29+
}
30+
31+
@Override
32+
public synchronized void start() {
33+
GLFW.glfwMakeContextCurrent(0);
34+
super.start();
35+
}
36+
37+
@Override
38+
public void run() {
39+
GLFW.glfwMakeContextCurrent(theWindow);
40+
GL.setCapabilities(caps);
41+
while(keepRunning.get()) {
42+
LockSupport.parkNanos(TimeUnit.MILLISECONDS.toNanos(50));
43+
ImmediateWindowHandler.renderTick();
44+
}
45+
GLFW.glfwMakeContextCurrent(0);
46+
}
47+
48+
@Override
49+
public void close() {
50+
keepRunning.set(false);
51+
try {
52+
this.join();
53+
} catch(InterruptedException e) {
54+
Thread.currentThread().interrupt();
55+
}
56+
GLFW.glfwMakeContextCurrent(theWindow);
57+
}
58+
}

0 commit comments

Comments
 (0)