Skip to content

Commit

Permalink
Merge pull request #182 from LostLuma/fix-config-file-saving-issues
Browse files Browse the repository at this point in the history
Fix config file de/serialization issues
  • Loading branch information
LostLuma authored Apr 13, 2024
2 parents 854cdd9 + d406032 commit 1482363
Show file tree
Hide file tree
Showing 2 changed files with 31 additions and 12 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 All @@ -53,13 +52,21 @@ public static void save(DynamicFPSConfig instance) {
Path temp = Files.createTempFile(cache, "config", ".json");

Files.write(temp, data.getBytes(StandardCharsets.UTF_8));
Files.move(temp, config, StandardCopyOption.ATOMIC_MOVE);
Serialization.move(temp, config); // Attempt atomic move, fall back otherwise.
} catch (IOException e) {
// Cloth Config's built-in saving does not support catching exceptions :(
throw new RuntimeException("Failed to save or modify Dynamic FPS config!", e);
}
}

private static void move(Path from, Path to) throws IOException {
try {
Files.move(from, to, StandardCopyOption.ATOMIC_MOVE);
} catch (IOException | UnsupportedOperationException e) {
Files.move(from, to, StandardCopyOption.REPLACE_EXISTING);
}
}

@SuppressWarnings("deprecation")
public static DynamicFPSConfig load() {
byte[] data;
Expand All @@ -68,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 1482363

Please sign in to comment.