24
24
package com .falsepattern .lib .internal .config ;
25
25
26
26
import com .falsepattern .lib .StableAPI ;
27
+ import com .falsepattern .lib .internal .FalsePatternLib ;
28
+ import com .falsepattern .lib .internal .Tags ;
27
29
import com .falsepattern .lib .internal .core .LowLevelCallMultiplexer ;
28
30
import com .google .gson .Gson ;
31
+ import com .google .gson .GsonBuilder ;
29
32
import com .google .gson .annotations .Expose ;
30
33
import lombok .Data ;
31
34
import lombok .SneakyThrows ;
32
35
import lombok .experimental .Accessors ;
33
36
import lombok .val ;
37
+ import org .apache .logging .log4j .LogManager ;
38
+ import org .apache .logging .log4j .Logger ;
39
+ import org .jetbrains .annotations .NotNull ;
34
40
41
+ import java .io .IOException ;
35
42
import java .nio .charset .StandardCharsets ;
36
43
import java .nio .file .Files ;
37
44
@@ -43,22 +50,45 @@ public class EarlyConfig {
43
50
@ StableAPI .Expose (since = "__INTERNAL__" )
44
51
private boolean enableLibraryDownloads ;
45
52
46
- private static EarlyConfig instance = null ;
53
+ private static volatile EarlyConfig instance = null ;
47
54
48
- @ SneakyThrows
49
- public static EarlyConfig load () {
50
- if (instance != null )
51
- return instance ;
52
- val configFile = LowLevelCallMultiplexer .gameDir ().resolve ("config" ).resolve ("falsepatternlib-early.json" );
53
- val gson = new Gson ();
54
- EarlyConfig config ;
55
- if (!Files .exists (configFile )) {
55
+ private static final Logger LOG = LogManager .getLogger (Tags .MODNAME + " Early Config" );
56
+
57
+ public static @ NotNull EarlyConfig getInstance () {
58
+ val config = instance ;
59
+ if (config != null )
60
+ return config ;
61
+ return loadFromDisk ();
62
+ }
63
+
64
+ private static synchronized @ NotNull EarlyConfig loadFromDisk () {
65
+ var config = instance ;
66
+ if (config != null ) {
67
+ return config ;
68
+ }
69
+ val configDir = LowLevelCallMultiplexer .gameDir ().resolve ("config" );
70
+ val configFile = configDir .resolve ("falsepatternlib-early.json" );
71
+ val gson = new GsonBuilder ().setPrettyPrinting ().create ();
72
+ try {
73
+ if (!Files .exists (configDir )) {
74
+ Files .createDirectories (configDir );
75
+ }
76
+ if (Files .exists (configFile )) {
77
+ config = gson .fromJson (new String (Files .readAllBytes (configFile ), StandardCharsets .UTF_8 ),
78
+ EarlyConfig .class );
79
+ }
80
+ } catch (IOException e ) {
81
+ LOG .error ("Failed to load from disk" , e );
82
+ }
83
+ if (config == null ) {
56
84
config = new EarlyConfig ();
57
85
config .enableLibraryDownloads (true );
58
- } else {
59
- config = gson .fromJson (new String (Files .readAllBytes (configFile ), StandardCharsets .UTF_8 ), EarlyConfig .class );
86
+ try {
87
+ Files .write (configFile , gson .toJson (config ).getBytes (StandardCharsets .UTF_8 ));
88
+ } catch (IOException e ) {
89
+ LOG .error ("Failed to write to disk" , e );
90
+ }
60
91
}
61
- Files .write (configFile , gson .toJson (config ).getBytes (StandardCharsets .UTF_8 ));
62
92
instance = config ;
63
93
return config ;
64
94
}
0 commit comments