Skip to content

Commit

Permalink
Merge pull request #139 from LostLuma/atomic-config-saving
Browse files Browse the repository at this point in the history
Atomic config saving
  • Loading branch information
LostLuma authored Dec 16, 2023
2 parents dc8d0d5 + 18b2c08 commit ec676e0
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 8 deletions.
2 changes: 0 additions & 2 deletions src/main/java/dynamic_fps/impl/DynamicFPSMod.java
Original file line number Diff line number Diff line change
Expand Up @@ -62,8 +62,6 @@ public class DynamicFPSMod implements ClientModInitializer {

@Override
public void onInitializeClient() {
modConfig.save(); // Force create file on disk

toggleForcedKeyBinding.register();
toggleDisabledKeyBinding.register();

Expand Down
21 changes: 15 additions & 6 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,9 +57,11 @@ public static DynamicFPSConfig load() {
String data;

try {
data = Files.readString(PATH);
data = Files.readString(CONFIG_FILE);
} catch (NoSuchFileException e) {
return new DynamicFPSConfig(new EnumMap<>(PowerState.class));
var config = new DynamicFPSConfig(new EnumMap<>(PowerState.class));
config.save();
return config;
} catch (IOException e) {
throw new RuntimeException("Failed to load Dynamic FPS config.", e);
}
Expand All @@ -72,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 ec676e0

Please sign in to comment.