Skip to content

Commit

Permalink
Save config atomically to prevent corruption
Browse files Browse the repository at this point in the history
  • Loading branch information
LostLuma committed Dec 15, 2023
1 parent 2dccc28 commit 18b2c08
Showing 1 changed file with 12 additions and 5 deletions.
17 changes: 12 additions & 5 deletions src/main/java/dynamic_fps/impl/config/DynamicFPSConfig.java
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
import java.nio.file.Files;
import java.nio.file.NoSuchFileException;
import java.nio.file.Path;
import java.nio.file.StandardCopyOption;
import java.util.EnumMap;
import java.util.Map;

Expand All @@ -21,7 +22,9 @@
public final class DynamicFPSConfig {
private Map<PowerState, Config> configs;

private static final Path PATH = FabricLoader.getInstance().getConfigDir().resolve(DynamicFPSMod.MOD_ID + ".json");
private static final Path CONFIGS = FabricLoader.getInstance().getConfigDir();
private static final Path CONFIG_FILE = CONFIGS.resolve(DynamicFPSMod.MOD_ID + ".json");

private static final Codec<Map<PowerState, Config>> STATES_CODEC = Codec.unboundedMap(PowerState.CODEC, Config.CODEC);

private static final Codec<DynamicFPSConfig> CODEC = RecordCodecBuilder.create(instance -> instance.group(
Expand Down Expand Up @@ -54,7 +57,7 @@ public static DynamicFPSConfig load() {
String data;

try {
data = Files.readString(PATH);
data = Files.readString(CONFIG_FILE);
} catch (NoSuchFileException e) {
var config = new DynamicFPSConfig(new EnumMap<>(PowerState.class));
config.save();
Expand All @@ -74,10 +77,14 @@ public void save() {
var root = data.getOrThrow(false, RuntimeException::new);

try {
Files.writeString(PATH, root.toString(), StandardCharsets.UTF_8);
var temp = Files.createTempFile(CONFIGS, "dynamic_fps", ".json");
Files.writeString(temp, root.toString(), StandardCharsets.UTF_8);

Files.deleteIfExists(CONFIG_FILE);
Files.move(temp, CONFIG_FILE, StandardCopyOption.ATOMIC_MOVE);
} catch (IOException e) {
// Cloth Config's automatic saving does not support catching exceptions
throw new RuntimeException("Failed to save Dynamic FPS config.", e);
// Cloth Config's built-in saving does not support catching exceptions :(
throw new RuntimeException("Failed to save or modify Dynamic FPS config!", e);
}
}

Expand Down

0 comments on commit 18b2c08

Please sign in to comment.