16
16
import dynamic_fps .impl .GraphicsState ;
17
17
import dynamic_fps .impl .PowerState ;
18
18
import dynamic_fps .impl .service .Platform ;
19
- import net . minecraft . sounds . SoundSource ;
19
+ import dynamic_fps . impl . util . Logging ;
20
20
import org .jetbrains .annotations .Nullable ;
21
21
22
22
import java .io .IOException ;
26
26
import java .nio .file .NoSuchFileException ;
27
27
import java .nio .file .Path ;
28
28
import java .nio .file .StandardCopyOption ;
29
- import java .util .EnumMap ;
30
29
import java .util .Locale ;
31
30
import java .util .Map ;
32
31
@@ -53,13 +52,21 @@ public static void save(DynamicFPSConfig instance) {
53
52
Path temp = Files .createTempFile (cache , "config" , ".json" );
54
53
55
54
Files .write (temp , data .getBytes (StandardCharsets .UTF_8 ));
56
- Files .move (temp , config , StandardCopyOption . ATOMIC_MOVE );
55
+ Serialization .move (temp , config ); // Attempt atomic move, fall back otherwise.
57
56
} catch (IOException e ) {
58
57
// Cloth Config's built-in saving does not support catching exceptions :(
59
58
throw new RuntimeException ("Failed to save or modify Dynamic FPS config!" , e );
60
59
}
61
60
}
62
61
62
+ private static void move (Path from , Path to ) throws IOException {
63
+ try {
64
+ Files .move (from , to , StandardCopyOption .ATOMIC_MOVE );
65
+ } catch (IOException | UnsupportedOperationException e ) {
66
+ Files .move (from , to , StandardCopyOption .REPLACE_EXISTING );
67
+ }
68
+ }
69
+
63
70
@ SuppressWarnings ("deprecation" )
64
71
public static DynamicFPSConfig load () {
65
72
byte [] data ;
@@ -68,18 +75,18 @@ public static DynamicFPSConfig load() {
68
75
try {
69
76
data = Files .readAllBytes (config );
70
77
} catch (NoSuchFileException e ) {
71
- DynamicFPSConfig instance = new DynamicFPSConfig (
72
- true ,
73
- 0 ,
74
- false ,
75
- new EnumMap <>(PowerState .class )
76
- );
77
- instance .save ();
78
- return instance ;
78
+ return DynamicFPSConfig .createDefault ();
79
79
} catch (IOException e ) {
80
80
throw new RuntimeException ("Failed to load Dynamic FPS config." , e );
81
81
}
82
82
83
+ // Sometimes when the config failed to save properly it'll end up being only null bytes.
84
+ // Since most users don't seem to know how to deal with this we'll just replace the config.
85
+ if (data [0 ] == 0 ) {
86
+ Logging .getLogger ().warn ("Dynamic FPS config corrupted! Recreating from defaults ..." );
87
+ return DynamicFPSConfig .createDefault ();
88
+ }
89
+
83
90
JsonElement root = new JsonParser ().parse (new String (data , StandardCharsets .UTF_8 ));
84
91
85
92
upgradeConfig ((JsonObject ) root );
0 commit comments