Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Atomic config saving #139

Merged
merged 2 commits into from
Dec 16, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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