Skip to content

Commit

Permalink
Recover from corrupted config by reinstating the default config
Browse files Browse the repository at this point in the history
I've seen a few users run into this (mostly before we saved the
config with a different name and then moved it) but I'd like to
have this fallback either way so their game at can start again.
  • Loading branch information
LostLuma committed Apr 13, 2024
1 parent e68432b commit d406032
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 11 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ public final class DynamicFPSConfig {
@SerializedName("states")
private final Map<PowerState, Config> configs;

DynamicFPSConfig(boolean enabled, int abandonTime, boolean uncapMenuFrameRate, Map<PowerState, Config> configs) {
private DynamicFPSConfig(boolean enabled, int abandonTime, boolean uncapMenuFrameRate, Map<PowerState, Config> configs) {
this.enabled = enabled;
this.idleTime = abandonTime;
this.uncapMenuFrameRate = uncapMenuFrameRate;
Expand All @@ -28,6 +28,18 @@ public final class DynamicFPSConfig {
}
}

public static DynamicFPSConfig createDefault() {
DynamicFPSConfig instance = new DynamicFPSConfig(
true,
0,
false,
new EnumMap<>(PowerState.class)
);

instance.save();
return instance;
}

public Config get(PowerState state) {
if (state == PowerState.FOCUSED) {
return Config.ACTIVE;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@
import dynamic_fps.impl.GraphicsState;
import dynamic_fps.impl.PowerState;
import dynamic_fps.impl.service.Platform;
import net.minecraft.sounds.SoundSource;
import dynamic_fps.impl.util.Logging;
import org.jetbrains.annotations.Nullable;

import java.io.IOException;
Expand All @@ -26,7 +26,6 @@
import java.nio.file.NoSuchFileException;
import java.nio.file.Path;
import java.nio.file.StandardCopyOption;
import java.util.EnumMap;
import java.util.Locale;
import java.util.Map;

Expand Down Expand Up @@ -76,18 +75,18 @@ public static DynamicFPSConfig load() {
try {
data = Files.readAllBytes(config);
} catch (NoSuchFileException e) {
DynamicFPSConfig instance = new DynamicFPSConfig(
true,
0,
false,
new EnumMap<>(PowerState.class)
);
instance.save();
return instance;
return DynamicFPSConfig.createDefault();
} catch (IOException e) {
throw new RuntimeException("Failed to load Dynamic FPS config.", e);
}

// Sometimes when the config failed to save properly it'll end up being only null bytes.
// Since most users don't seem to know how to deal with this we'll just replace the config.
if (data[0] == 0) {
Logging.getLogger().warn("Dynamic FPS config corrupted! Recreating from defaults ...");
return DynamicFPSConfig.createDefault();
}

JsonElement root = new JsonParser().parse(new String(data, StandardCharsets.UTF_8));

upgradeConfig((JsonObject) root);
Expand Down

0 comments on commit d406032

Please sign in to comment.