2023-04-03 08:58:28 +02:00
|
|
|
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;
|
2023-04-03 08:58:28 +02:00
|
|
|
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;
|
2023-04-03 08:58:28 +02:00
|
|
|
import com.jozufozu.flywheel.config.FlwConfig;
|
2023-11-18 20:46:04 +01:00
|
|
|
import com.jozufozu.flywheel.impl.visualization.VisualizationManagerImpl;
|
2023-04-08 01:01:03 +02:00
|
|
|
import com.jozufozu.flywheel.lib.backend.SimpleBackend;
|
2023-04-03 08:58:28 +02:00
|
|
|
import com.mojang.logging.LogUtils;
|
|
|
|
|
2023-04-08 01:01:03 +02:00
|
|
|
import net.minecraft.ChatFormatting;
|
|
|
|
import net.minecraft.client.multiplayer.ClientLevel;
|
2023-11-23 06:30:58 +01:00
|
|
|
import net.minecraft.network.chat.Component;
|
2023-11-18 20:46:04 +01:00
|
|
|
import net.minecraft.resources.ResourceLocation;
|
2023-04-08 01:01:03 +02:00
|
|
|
import net.minecraftforge.fml.CrashReportCallables;
|
|
|
|
|
2023-04-03 08:58:28 +02:00
|
|
|
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()
|
2023-11-23 06:30:58 +01:00
|
|
|
.engineMessage(Component.literal("Disabled Flywheel")
|
|
|
|
.withStyle(ChatFormatting.RED))
|
2023-04-08 01:01:03 +02:00
|
|
|
.engineFactory(level -> {
|
2023-11-18 20:46:04 +01:00
|
|
|
throw new UnsupportedOperationException("Cannot create engine when backend is off.");
|
2023-04-08 01:01:03 +02:00
|
|
|
})
|
|
|
|
.supported(() -> true)
|
|
|
|
.register(Flywheel.rl("off"));
|
|
|
|
|
2023-04-03 08:58:28 +02:00
|
|
|
private static final Backend DEFAULT_BACKEND = findDefaultBackend();
|
2023-04-08 01:01:03 +02:00
|
|
|
|
2023-04-20 21:22:51 +02:00
|
|
|
private static Backend backend = OFF_BACKEND;
|
2023-04-03 08:58:28 +02:00
|
|
|
|
2023-11-18 20:46:04 +01:00
|
|
|
private BackendManagerImpl() {
|
|
|
|
}
|
|
|
|
|
2023-04-03 08:58:28 +02:00
|
|
|
public static Backend getBackend() {
|
|
|
|
return backend;
|
|
|
|
}
|
|
|
|
|
|
|
|
public static boolean isOn() {
|
2023-04-20 21:22:51 +02:00
|
|
|
return backend != OFF_BACKEND;
|
2023-04-03 08:58:28 +02:00
|
|
|
}
|
|
|
|
|
2023-04-08 01:01:03 +02:00
|
|
|
public static Backend getOffBackend() {
|
|
|
|
return OFF_BACKEND;
|
2023-04-03 08:58:28 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
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) {
|
2023-11-18 20:46:04 +01:00
|
|
|
VisualizationManagerImpl.reset(level);
|
2023-04-08 01:01:03 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-04-03 08:58:28 +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));
|
2023-04-03 08:58:28 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
return actual;
|
|
|
|
}
|
|
|
|
|
2023-11-18 20:46:04 +01:00
|
|
|
public static String getBackendString() {
|
|
|
|
ResourceLocation backendId = Backend.REGISTRY.getId(backend);
|
|
|
|
if (backendId == null) {
|
|
|
|
return "[unregistered]";
|
|
|
|
}
|
|
|
|
return backendId.toString();
|
2023-04-06 03:03:25 +02:00
|
|
|
}
|
|
|
|
|
2023-11-18 20:46:04 +01:00
|
|
|
public static void init() {
|
|
|
|
CrashReportCallables.registerCrashCallable("Flywheel Backend", BackendManagerImpl::getBackendString);
|
2023-04-03 08:58:28 +02:00
|
|
|
}
|
|
|
|
}
|