The choice is ours

- Allow "DEFAULT" to be used in place of a backend ID in commands and configs to dynamically and non-persistently select the best backend
This commit is contained in:
PepperCode1 2025-02-23 17:56:48 -08:00
parent 8aa610648a
commit 71a5eba3b3
5 changed files with 60 additions and 16 deletions

View file

@ -4,6 +4,8 @@ import dev.engine_room.flywheel.api.backend.Backend;
import dev.engine_room.flywheel.backend.BackendConfig;
public interface FlwConfig {
String DEFAULT_BACKEND_STR = "DEFAULT";
FlwConfig INSTANCE = FlwImplXplat.INSTANCE.getConfig();
Backend backend();

View file

@ -38,8 +38,8 @@ public class FabricFlwConfig implements FlwConfig {
private final File file;
// Don't actually default to off, we'll find the true default in #load
public Backend backend = BackendManager.offBackend();
public boolean useDefaultBackend = true;
public boolean limitUpdates = LIMIT_UPDATES_DEFAULT;
public int workerThreads = WORKER_THREADS_DEFAULT;
@ -51,6 +51,10 @@ public class FabricFlwConfig implements FlwConfig {
@Override
public Backend backend() {
if (useDefaultBackend) {
return BackendManager.defaultBackend();
}
return backend;
}
@ -70,10 +74,6 @@ public class FabricFlwConfig implements FlwConfig {
}
public void load() {
// Grab the default backend here because this object is constructed
// very early in flywheel loading and not all backends may be registered
backend = BackendManager.defaultBackend();
if (file.exists()) {
try (FileReader reader = new FileReader(file)) {
fromJson(JsonParser.parseReader(reader));
@ -96,7 +96,8 @@ public class FabricFlwConfig implements FlwConfig {
public void fromJson(JsonElement json) {
if (!(json instanceof JsonObject object)) {
FlwImpl.CONFIG_LOGGER.warn("Config JSON must be an object");
backend = BackendManager.defaultBackend();
backend = BackendManager.offBackend();
useDefaultBackend = true;
limitUpdates = LIMIT_UPDATES_DEFAULT;
workerThreads = WORKER_THREADS_DEFAULT;
return;
@ -114,8 +115,15 @@ public class FabricFlwConfig implements FlwConfig {
if (backendJson instanceof JsonPrimitive primitive && primitive.isString()) {
var value = primitive.getAsString();
if (value.equals(DEFAULT_BACKEND_STR)) {
backend = BackendManager.offBackend();
useDefaultBackend = true;
return;
}
try {
this.backend = Backend.REGISTRY.getOrThrow(new ResourceLocation(value));
backend = Backend.REGISTRY.getOrThrow(new ResourceLocation(value));
useDefaultBackend = false;
return;
} catch (ResourceLocationException e) {
msg = "'backend' value '" + value + "' is not a valid resource location";
@ -133,7 +141,8 @@ public class FabricFlwConfig implements FlwConfig {
if (msg != null) {
FlwImpl.CONFIG_LOGGER.warn(msg);
}
backend = BackendManager.defaultBackend();
backend = BackendManager.offBackend();
useDefaultBackend = true;
}
private void readLimitUpdates(JsonObject object) {
@ -181,7 +190,7 @@ public class FabricFlwConfig implements FlwConfig {
public JsonObject toJson() {
JsonObject object = new JsonObject();
object.addProperty("backend", Backend.REGISTRY.getIdOrThrow(backend).toString());
object.addProperty("backend", useDefaultBackend ? DEFAULT_BACKEND_STR : Backend.REGISTRY.getIdOrThrow(backend).toString());
object.addProperty("limitUpdates", limitUpdates);
object.addProperty("workerThreads", workerThreads);
object.add("flw_backends", backendConfig.toJson());

View file

@ -38,10 +38,26 @@ public final class FlwCommands {
context.getSource().sendFeedback(Component.translatable("command.flywheel.backend.get", idStr));
return Command.SINGLE_SUCCESS;
})
.then(ClientCommandManager.literal("DEFAULT")
.executes(context -> {
FabricFlwConfig.INSTANCE.backend = BackendManager.offBackend();
FabricFlwConfig.INSTANCE.useDefaultBackend = true;
FabricFlwConfig.INSTANCE.save();
// Reload renderers so we can report the actual backend.
Minecraft.getInstance().levelRenderer.allChanged();
Backend actualBackend = BackendManager.currentBackend();
String actualIdStr = Backend.REGISTRY.getIdOrThrow(actualBackend)
.toString();
context.getSource().sendFeedback(Component.translatable("command.flywheel.backend.set", actualIdStr));
return Command.SINGLE_SUCCESS;
}))
.then(ClientCommandManager.argument("id", BackendArgument.INSTANCE)
.executes(context -> {
Backend requestedBackend = context.getArgument("id", Backend.class);
FabricFlwConfig.INSTANCE.backend = requestedBackend;
FabricFlwConfig.INSTANCE.useDefaultBackend = false;
FabricFlwConfig.INSTANCE.save();
// Reload renderers so we can report the actual backend.

View file

@ -38,6 +38,19 @@ public final class FlwCommands {
sendMessage(context.getSource(), Component.translatable("command.flywheel.backend.get", idStr));
return Command.SINGLE_SUCCESS;
})
.then(Commands.literal("DEFAULT")
.executes(context -> {
backendValue.set(FlwConfig.DEFAULT_BACKEND_STR);
// Reload renderers so we can report the actual backend.
Minecraft.getInstance().levelRenderer.allChanged();
Backend actualBackend = BackendManager.currentBackend();
String actualIdStr = Backend.REGISTRY.getIdOrThrow(actualBackend)
.toString();
sendMessage(context.getSource(), Component.translatable("command.flywheel.backend.set", actualIdStr));
return Command.SINGLE_SUCCESS;
}))
.then(Commands.argument("id", BackendArgument.INSTANCE)
.executes(context -> {
Backend requestedBackend = context.getArgument("id", Backend.class);

View file

@ -29,20 +29,24 @@ public class ForgeFlwConfig implements FlwConfig {
public Backend backend() {
Backend backend = parseBackend(client.backend.get());
if (backend == null) {
client.backend.set(DEFAULT_BACKEND_STR);
backend = BackendManager.defaultBackend();
client.backend.set(Backend.REGISTRY.getIdOrThrow(backend).toString());
}
return backend;
}
@Nullable
private static Backend parseBackend(String idStr) {
private static Backend parseBackend(String value) {
if (value.equals(DEFAULT_BACKEND_STR)) {
return BackendManager.defaultBackend();
}
ResourceLocation backendId;
try {
backendId = new ResourceLocation(idStr);
backendId = new ResourceLocation(value);
} catch (ResourceLocationException e) {
FlwImpl.CONFIG_LOGGER.warn("'backend' value '{}' is not a valid resource location", idStr);
FlwImpl.CONFIG_LOGGER.warn("'backend' value '{}' is not a valid resource location", value);
return null;
}
@ -82,8 +86,8 @@ public class ForgeFlwConfig implements FlwConfig {
public final ForgeBackendConfig backendConfig;
private ClientConfig(ForgeConfigSpec.Builder builder) {
backend = builder.comment("Select the backend to use.")
.define("backend", () -> Backend.REGISTRY.getIdOrThrow(BackendManager.defaultBackend()).toString(), o -> o != null && String.class.isAssignableFrom(o.getClass()));
backend = builder.comment("Select the backend to use. Set to \"DEFAULT\" to let Flywheel decide.")
.define("backend", DEFAULT_BACKEND_STR);
limitUpdates = builder.comment("Enable or disable instance update limiting with distance.")
.define("limitUpdates", true);
@ -103,7 +107,7 @@ public class ForgeFlwConfig implements FlwConfig {
public final ForgeConfigSpec.EnumValue<LightSmoothness> lightSmoothness;
public ForgeBackendConfig(ForgeConfigSpec.Builder builder) {
lightSmoothness = builder.comment("How smooth flywheel's shader-based lighting should be. May have a large performance impact.")
lightSmoothness = builder.comment("How smooth Flywheel's shader-based lighting should be. May have a large performance impact.")
.defineEnum("lightSmoothness", LightSmoothness.SMOOTH);
}