Compress config command code

- Remove BooleanConfig, BooleanConfigCommand, and BooleanDirective
- Make Flywheel.VERSION private so it cannot be changed
- Move createUpdateLimiter from FlwConfig to InstanceManager
This commit is contained in:
PepperCode1 2022-02-01 20:44:53 -08:00
parent 06d2325a7a
commit d3cffaf495
10 changed files with 166 additions and 238 deletions

View File

@ -36,30 +36,26 @@ public class Flywheel {
public static final String ID = "flywheel";
public static final Logger LOGGER = LogManager.getLogger(Flywheel.class);
public static ArtifactVersion VERSION;
private static ArtifactVersion version;
public Flywheel() {
IModFileInfo modFileById = ModList.get()
.getModFileById(ID);
VERSION = modFileById.getMods()
version = modFileById.getMods()
.get(0)
.getVersion();
FMLJavaModLoadingContext.get()
.getModEventBus()
.addListener(this::setup);
.addListener(Flywheel::setup);
FlwConfig.init();
DistExecutor.unsafeRunWhenOn(Dist.CLIENT, () -> Flywheel::clientInit);
}
public static ResourceLocation rl(String path) {
return new ResourceLocation(ID, path);
}
public static void clientInit() {
private static void clientInit() {
CrashReportCallables.registerCrashCallable("Flywheel Backend", Backend::getBackendDescriptor);
OptifineHandler.init();
@ -74,7 +70,6 @@ public class Flywheel {
modEventBus.addListener(StitchedSprite::onTextureStitchPost);
MinecraftForge.EVENT_BUS.addListener(FlwCommands::registerClientCommands);
MinecraftForge.EVENT_BUS.<ReloadRenderersEvent>addListener(ProgramCompiler::invalidateAll);
VanillaInstances.init();
@ -84,10 +79,18 @@ public class Flywheel {
// Only thing I've seen that's close to a fix is to force the class to load before trying to use it.
// From the SpongePowered discord:
// https://discord.com/channels/142425412096491520/626802111455297538/675007581168599041
LOGGER.info("Successfully loaded {}", PausedPartialTickAccessor.class.getName());
LOGGER.debug("Successfully loaded {}", PausedPartialTickAccessor.class.getName());
}
private void setup(final FMLCommonSetupEvent event) {
private static void setup(final FMLCommonSetupEvent event) {
ArgumentTypes.register(rl("engine").toString(), EngineArgument.class, new EmptyArgumentSerializer<>(EngineArgument::getInstance));
}
public static ArtifactVersion getVersion() {
return version;
}
public static ResourceLocation rl(String path) {
return new ResourceLocation(ID, path);
}
}

View File

@ -14,7 +14,9 @@ import com.jozufozu.flywheel.api.instance.DynamicInstance;
import com.jozufozu.flywheel.api.instance.TickableInstance;
import com.jozufozu.flywheel.backend.Backend;
import com.jozufozu.flywheel.backend.instancing.instancing.InstancingEngine;
import com.jozufozu.flywheel.backend.instancing.ratelimit.BandedPrimeLimiter;
import com.jozufozu.flywheel.backend.instancing.ratelimit.DistanceUpdateLimiter;
import com.jozufozu.flywheel.backend.instancing.ratelimit.NonLimiter;
import com.jozufozu.flywheel.config.FlwConfig;
import com.jozufozu.flywheel.light.LightUpdater;
import com.mojang.math.Vector3f;
@ -46,9 +48,16 @@ public abstract class InstanceManager<T> implements InstancingEngine.OriginShift
this.dynamicInstances = new Object2ObjectOpenHashMap<>();
this.tickableInstances = new Object2ObjectOpenHashMap<>();
FlwConfig config = FlwConfig.get();
frame = config.createUpdateLimiter();
tick = config.createUpdateLimiter();
frame = createUpdateLimiter();
tick = createUpdateLimiter();
}
protected DistanceUpdateLimiter createUpdateLimiter() {
if (FlwConfig.get().limitUpdates()) {
return new BandedPrimeLimiter();
} else {
return new NonLimiter();
}
}
/**

View File

@ -4,7 +4,7 @@ import java.util.List;
import com.jozufozu.flywheel.Flywheel;
import com.jozufozu.flywheel.backend.Backend;
import com.jozufozu.flywheel.config.BooleanConfig;
import com.jozufozu.flywheel.config.FlwCommands;
import com.jozufozu.flywheel.config.FlwConfig;
import com.jozufozu.flywheel.event.BeginFrameEvent;
import com.jozufozu.flywheel.event.ReloadRenderersEvent;
@ -73,7 +73,6 @@ public class InstancedRenderDispatcher {
@SubscribeEvent
public static void tick(TickEvent.ClientTickEvent event) {
if (!Backend.isGameActive() || event.phase == TickEvent.Phase.START) {
return;
}
@ -120,12 +119,12 @@ public class InstancedRenderDispatcher {
public static void getDebugString(List<String> debug) {
debug.add("");
debug.add("Flywheel: " + Flywheel.VERSION);
debug.add("Flywheel: " + Flywheel.getVersion());
if (Backend.isOn()) {
InstanceWorld instanceWorld = instanceWorlds.get(Minecraft.getInstance().level);
debug.add("Update limiting: " + BooleanConfig.boolToText(FlwConfig.get().limitUpdates()).getString());
debug.add("Update limiting: " + FlwCommands.boolToText(FlwConfig.get().limitUpdates()).getString());
debug.add("B: " + instanceWorld.blockEntityInstanceManager.getObjectCount() + ", E: " + instanceWorld.entityInstanceManager.getObjectCount());
instanceWorld.engine.addDebugInfo(debug);
} else {

View File

@ -1,63 +0,0 @@
package com.jozufozu.flywheel.config;
import java.util.function.Consumer;
import com.jozufozu.flywheel.backend.Backend;
import net.minecraft.ChatFormatting;
import net.minecraft.client.Minecraft;
import net.minecraft.client.player.LocalPlayer;
import net.minecraft.network.chat.Component;
import net.minecraft.network.chat.MutableComponent;
import net.minecraft.network.chat.TextComponent;
public enum BooleanConfig {
NORMAL_OVERLAY(BooleanConfig::normalOverlay),
LIMIT_UPDATES(BooleanConfig::limitUpdates);
final Consumer<BooleanDirective> receiver;
BooleanConfig(Consumer<BooleanDirective> receiver) {
this.receiver = receiver;
}
private static void limitUpdates(BooleanDirective booleanDirective) {
LocalPlayer player = Minecraft.getInstance().player;
if (player == null || booleanDirective == null) return;
if (booleanDirective == BooleanDirective.DISPLAY) {
Component text = new TextComponent("Update limiting is currently: ").append(boolToText(FlwConfig.get().limitUpdates()));
player.displayClientMessage(text, false);
return;
}
FlwConfig.get().client.limitUpdates.set(booleanDirective.get());
Component text = boolToText(FlwConfig.get().limitUpdates()).append(new TextComponent(" update limiting.").withStyle(ChatFormatting.WHITE));
player.displayClientMessage(text, false);
Backend.reloadWorldRenderers();
}
private static void normalOverlay(BooleanDirective state) {
LocalPlayer player = Minecraft.getInstance().player;
if (player == null || state == null) return;
if (state == BooleanDirective.DISPLAY) {
Component text = new TextComponent("Normal debug mode is currently: ").append(boolToText(FlwConfig.get().debugNormals()));
player.displayClientMessage(text, false);
return;
}
FlwConfig.get().client.debugNormals.set(state.get());
Component text = boolToText(FlwConfig.get().debugNormals()).append(new TextComponent(" normal debug mode").withStyle(ChatFormatting.WHITE));
player.displayClientMessage(text, false);
}
public static MutableComponent boolToText(boolean b) {
return b ? new TextComponent("enabled").withStyle(ChatFormatting.DARK_GREEN) : new TextComponent("disabled").withStyle(ChatFormatting.RED);
}
}

View File

@ -1,37 +0,0 @@
package com.jozufozu.flywheel.config;
import com.mojang.brigadier.Command;
import com.mojang.brigadier.builder.ArgumentBuilder;
import net.minecraft.commands.CommandSourceStack;
import net.minecraft.commands.Commands;
public class BooleanConfigCommand {
private final String name;
private final BooleanConfig value;
public BooleanConfigCommand(String name, BooleanConfig value) {
this.name = name;
this.value = value;
}
public ArgumentBuilder<CommandSourceStack, ?> register() {
return Commands.literal(name)
.executes(context -> {
value.receiver.accept(BooleanDirective.DISPLAY);
return Command.SINGLE_SUCCESS;
})
.then(Commands.literal("on")
.executes(context -> {
value.receiver.accept(BooleanDirective.TRUE);
return Command.SINGLE_SUCCESS;
}))
.then(Commands.literal("off")
.executes(context -> {
value.receiver.accept(BooleanDirective.FALSE);
return Command.SINGLE_SUCCESS;
}));
}
}

View File

@ -1,22 +0,0 @@
package com.jozufozu.flywheel.config;
public enum BooleanDirective {
TRUE(true),
FALSE(false),
/**
* Don't change anything, just display what the value currently is.
*/
DISPLAY(true),
;
private final boolean b;
BooleanDirective(boolean b) {
this.b = b;
}
public boolean get() {
if (this == DISPLAY) throw new IllegalStateException("DISPLAY directive has no value");
return b;
}
}

View File

@ -1,46 +1,152 @@
package com.jozufozu.flywheel.config;
import java.util.function.BiConsumer;
import org.jetbrains.annotations.NotNull;
import com.jozufozu.flywheel.backend.Backend;
import com.mojang.brigadier.Command;
import com.mojang.brigadier.CommandDispatcher;
import com.mojang.brigadier.builder.ArgumentBuilder;
import com.mojang.brigadier.builder.LiteralArgumentBuilder;
import net.minecraft.ChatFormatting;
import net.minecraft.client.Minecraft;
import net.minecraft.client.player.LocalPlayer;
import net.minecraft.commands.CommandSourceStack;
import net.minecraft.commands.Commands;
import net.minecraft.network.chat.Component;
import net.minecraft.network.chat.MutableComponent;
import net.minecraft.network.chat.TextComponent;
import net.minecraftforge.client.event.RegisterClientCommandsEvent;
import net.minecraftforge.common.ForgeConfigSpec.ConfigValue;
import net.minecraftforge.fml.ModList;
public class FlwCommands {
public static void registerClientCommands(RegisterClientCommandsEvent event) {
CommandDispatcher<CommandSourceStack> dispatcher = event.getDispatcher();
FlwConfig config = FlwConfig.get();
dispatcher.register(Commands.literal("flywheel")
.then(debugNormalsCommand())
.then(backendCommand())
.then(limitUpdatesCommand())
);
}
ConfigCommandBuilder commandBuilder = new ConfigCommandBuilder("flywheel");
private static ArgumentBuilder<CommandSourceStack, ?> debugNormalsCommand() {
return new BooleanConfigCommand("debugNormals", BooleanConfig.NORMAL_OVERLAY).register();
}
private static ArgumentBuilder<CommandSourceStack, ?> limitUpdatesCommand() {
return new BooleanConfigCommand("limitUpdates", BooleanConfig.LIMIT_UPDATES).register();
}
private static ArgumentBuilder<CommandSourceStack, ?> backendCommand() {
return Commands.literal("backend")
commandBuilder.addValue(config.client.engine, "backend", (builder, value) ->
builder
.executes(context -> {
FlwEngine.handle(null);
LocalPlayer player = Minecraft.getInstance().player;
if (player != null) {
player.displayClientMessage(getEngineMessage(value.get()), false);
}
return Command.SINGLE_SUCCESS;
})
.then(Commands.argument("type", EngineArgument.INSTANCE)
.executes(context -> {
.executes(context -> {
LocalPlayer player = Minecraft.getInstance().player;
if (player != null) {
FlwEngine type = context.getArgument("type", FlwEngine.class);
value.set(type);
FlwEngine.handle(type);
Component message = getEngineMessage(type);
player.displayClientMessage(message, false);
return Command.SINGLE_SUCCESS;
}));
Backend.reloadWorldRenderers();
}
return Command.SINGLE_SUCCESS;
})));
commandBuilder.addValue(config.client.debugNormals, "debugNormals", (builder, value) -> booleanValueCommand(builder, config, value,
(source, bool) -> {
LocalPlayer player = Minecraft.getInstance().player;
if (player == null) return;
Component text = new TextComponent("Normal debug mode is currently: ").append(boolToText(bool));
player.displayClientMessage(text, false);
},
(source, bool) -> {
LocalPlayer player = Minecraft.getInstance().player;
if (player == null) return;
Component text = boolToText(bool).append(new TextComponent(" normal debug mode").withStyle(ChatFormatting.WHITE));
player.displayClientMessage(text, false);
}
));
commandBuilder.addValue(config.client.limitUpdates, "limitUpdates", (builder, value) -> booleanValueCommand(builder, config, value,
(source, bool) -> {
LocalPlayer player = Minecraft.getInstance().player;
if (player == null) return;
Component text = new TextComponent("Update limiting is currently: ").append(boolToText(bool));
player.displayClientMessage(text, false);
},
(source, bool) -> {
LocalPlayer player = Minecraft.getInstance().player;
if (player == null) return;
Component text = boolToText(bool).append(new TextComponent(" update limiting.").withStyle(ChatFormatting.WHITE));
player.displayClientMessage(text, false);
Backend.reloadWorldRenderers();
}
));
commandBuilder.build(event.getDispatcher());
}
public static void booleanValueCommand(LiteralArgumentBuilder<CommandSourceStack> builder, FlwConfig config, ConfigValue<Boolean> value, BiConsumer<CommandSourceStack, Boolean> displayAction, BiConsumer<CommandSourceStack, Boolean> setAction) {
builder
.executes(context -> {
displayAction.accept(context.getSource(), value.get());
return Command.SINGLE_SUCCESS;
})
.then(Commands.literal("on")
.executes(context -> {
value.set(true);
setAction.accept(context.getSource(), value.get());
return Command.SINGLE_SUCCESS;
}))
.then(Commands.literal("off")
.executes(context -> {
value.set(false);
setAction.accept(context.getSource(), value.get());
return Command.SINGLE_SUCCESS;
}));
}
public static MutableComponent boolToText(boolean b) {
return b ? new TextComponent("enabled").withStyle(ChatFormatting.DARK_GREEN) : new TextComponent("disabled").withStyle(ChatFormatting.RED);
}
public static Component getEngineMessage(@NotNull FlwEngine type) {
return switch (type) {
case OFF -> new TextComponent("Disabled Flywheel").withStyle(ChatFormatting.RED);
case INSTANCING -> new TextComponent("Using Instancing Engine").withStyle(ChatFormatting.GREEN);
case BATCHING -> {
MutableComponent msg = new TextComponent("Using Batching Engine").withStyle(ChatFormatting.GREEN);
if (ModList.get()
.isLoaded("create")) {
// FIXME: batching engine contraption lighting issues
msg.append(new TextComponent("\nWARNING: May cause issues with Create Contraptions").withStyle(ChatFormatting.RED));
}
yield msg;
}
};
}
public static class ConfigCommandBuilder {
protected LiteralArgumentBuilder<CommandSourceStack> command;
public ConfigCommandBuilder(String baseLiteral) {
command = Commands.literal(baseLiteral);
}
public <T extends ConfigValue<?>> void addValue(T value, String subcommand, BiConsumer<LiteralArgumentBuilder<CommandSourceStack>, T> consumer) {
LiteralArgumentBuilder<CommandSourceStack> builder = Commands.literal(subcommand);
consumer.accept(builder, value);
command.then(builder);
}
public void build(CommandDispatcher<CommandSourceStack> dispatcher) {
dispatcher.register(command);
}
}
}

View File

@ -2,12 +2,9 @@ package com.jozufozu.flywheel.config;
import org.apache.commons.lang3.tuple.Pair;
import com.jozufozu.flywheel.backend.instancing.ratelimit.BandedPrimeLimiter;
import com.jozufozu.flywheel.backend.instancing.ratelimit.DistanceUpdateLimiter;
import com.jozufozu.flywheel.backend.instancing.ratelimit.NonLimiter;
import net.minecraftforge.common.ForgeConfigSpec;
import net.minecraftforge.common.ForgeConfigSpec.BooleanValue;
import net.minecraftforge.common.ForgeConfigSpec.EnumValue;
import net.minecraftforge.fml.ModLoadingContext;
import net.minecraftforge.fml.config.ModConfig;
@ -42,24 +39,15 @@ public class FlwConfig {
return client.limitUpdates.get();
}
public DistanceUpdateLimiter createUpdateLimiter() {
if (limitUpdates()) {
return new BandedPrimeLimiter();
} else {
return new NonLimiter();
}
}
public static void init() {
}
public static class ClientConfig {
public final ForgeConfigSpec.EnumValue<FlwEngine> engine;
public final EnumValue<FlwEngine> engine;
public final BooleanValue debugNormals;
public final BooleanValue limitUpdates;
public ClientConfig(ForgeConfigSpec.Builder builder) {
engine = builder.comment("Enable or disable the entire engine")
.defineEnum("backend", FlwEngine.INSTANCING);

View File

@ -6,19 +6,6 @@ import java.util.Map;
import javax.annotation.Nullable;
import org.jetbrains.annotations.NotNull;
import com.jozufozu.flywheel.backend.Backend;
import net.minecraft.ChatFormatting;
import net.minecraft.client.Minecraft;
import net.minecraft.client.player.LocalPlayer;
import net.minecraft.network.FriendlyByteBuf;
import net.minecraft.network.chat.Component;
import net.minecraft.network.chat.MutableComponent;
import net.minecraft.network.chat.TextComponent;
import net.minecraftforge.fml.ModList;
public enum FlwEngine {
OFF("off", "Off"),
BATCHING("batching", "Parallel Batching"),
@ -42,57 +29,14 @@ public enum FlwEngine {
this.properName = properName;
}
public String getShortName() {
return shortName;
}
public String getProperName() {
return properName;
}
public void encode(FriendlyByteBuf buffer) {
buffer.writeByte(this.ordinal());
}
public static void handle(@Nullable FlwEngine type) {
LocalPlayer player = Minecraft.getInstance().player;
if (player == null) return;
if (type != null) {
FlwConfig.get().client.engine.set(type);
Component message = getMessage(type);
player.displayClientMessage(message, false);
Backend.reloadWorldRenderers();
} else {
player.displayClientMessage(getMessage(FlwConfig.get().getEngine()), false);
}
}
private static Component getMessage(@NotNull FlwEngine type) {
return switch (type) {
case OFF -> new TextComponent("Disabled Flywheel").withStyle(ChatFormatting.RED);
case INSTANCING -> new TextComponent("Using Instancing Engine").withStyle(ChatFormatting.GREEN);
case BATCHING -> {
MutableComponent msg = new TextComponent("Using Batching Engine").withStyle(ChatFormatting.GREEN);
if (ModList.get()
.isLoaded("create")) {
// FIXME: batching engine contraption lighting issues
msg.append(new TextComponent("\nWARNING: May cause issues with Create Contraptions").withStyle(ChatFormatting.RED));
}
yield msg;
}
};
}
@Nullable
public static FlwEngine decode(FriendlyByteBuf buffer) {
byte b = buffer.readByte();
if (b == -1) return null;
return values()[b];
}
@Nullable
public static FlwEngine byName(String name) {
return lookup.get(name);

View File

@ -54,6 +54,7 @@ public class ProgramCompiler<P extends GlProgram> extends Memoizer<ProgramContex
return super.get(ctx);
}
@Override
public void invalidate() {
super.invalidate();
vertexCompiler.invalidate();