Flywheel/src/main/java/com/jozufozu/flywheel/impl/BackendManagerImpl.java

93 lines
2.7 KiB
Java
Raw Normal View History

package com.jozufozu.flywheel.impl;
import org.jetbrains.annotations.Nullable;
import org.slf4j.Logger;
2023-04-08 01:01:03 +02:00
import com.jozufozu.flywheel.Flywheel;
import com.jozufozu.flywheel.api.backend.Backend;
2023-04-08 01:01:03 +02:00
import com.jozufozu.flywheel.api.event.ReloadRenderersEvent;
import com.jozufozu.flywheel.backend.Backends;
import com.jozufozu.flywheel.config.FlwConfig;
import com.jozufozu.flywheel.impl.visualization.VisualizedRenderDispatcher;
2023-04-08 01:01:03 +02:00
import com.jozufozu.flywheel.lib.backend.SimpleBackend;
import com.mojang.logging.LogUtils;
2023-04-08 01:01:03 +02:00
import net.minecraft.ChatFormatting;
import net.minecraft.client.multiplayer.ClientLevel;
import net.minecraft.network.chat.TextComponent;
import net.minecraftforge.fml.CrashReportCallables;
public final class BackendManagerImpl {
private static final Logger LOGGER = LogUtils.getLogger();
2023-04-08 01:01:03 +02:00
private static final Backend OFF_BACKEND = SimpleBackend.builder()
.engineMessage(new TextComponent("Disabled Flywheel").withStyle(ChatFormatting.RED))
.engineFactory(level -> {
throw new IllegalStateException("Cannot create engine when backend is off.");
})
.supported(() -> true)
.register(Flywheel.rl("off"));
private static final Backend DEFAULT_BACKEND = findDefaultBackend();
2023-04-08 01:01:03 +02:00
private static Backend backend = OFF_BACKEND;
public static Backend getBackend() {
return backend;
}
public static boolean isOn() {
return backend != OFF_BACKEND;
}
2023-04-08 01:01:03 +02:00
public static Backend getOffBackend() {
return OFF_BACKEND;
}
public static Backend getDefaultBackend() {
return DEFAULT_BACKEND;
}
2023-04-08 01:01:03 +02:00
private static Backend findDefaultBackend() {
// TODO: Automatically select the best default config based on the user's driver
// TODO: Figure out how this will work if custom backends are registered and without hardcoding the default backends
return Backends.INDIRECT;
}
public static void onReloadRenderers(ReloadRenderersEvent event) {
refresh(event.getLevel());
}
public static void refresh(@Nullable ClientLevel level) {
backend = chooseBackend();
if (level != null) {
VisualizedRenderDispatcher.resetVisualWorld(level);
2023-04-08 01:01:03 +02:00
}
}
private static Backend chooseBackend() {
var preferred = FlwConfig.get().getBackend();
var actual = preferred.findFallback();
if (preferred != actual) {
2023-04-04 21:36:54 +02:00
LOGGER.warn("Flywheel backend fell back from '{}' to '{}'", Backend.REGISTRY.getIdOrThrow(preferred), Backend.REGISTRY.getIdOrThrow(actual));
}
return actual;
}
2023-04-08 01:01:03 +02:00
public static void init() {
CrashReportCallables.registerCrashCallable("Flywheel Backend", () -> {
var backendId = Backend.REGISTRY.getId(backend);
if (backendId == null) {
return "Unregistered";
}
return backendId.toString();
});
2023-04-06 03:03:25 +02:00
}
private BackendManagerImpl() {
}
}