11package net .vulkanmod .config ;
22
3- import com .google .gson .Gson ;
4- import com .google .gson .GsonBuilder ;
3+ import com .google .gson .*;
4+ import com .google .gson .annotations .JsonAdapter ;
5+ import net .vulkanmod .Initializer ;
6+ import net .vulkanmod .config .video .VideoMode ;
57import net .vulkanmod .config .video .VideoModeManager ;
6- import net .vulkanmod .config .video .VideoModeSet ;
78
8- import java .io .FileReader ;
99import java .io .IOException ;
10- import java .lang .reflect .Modifier ;
1110import java .nio .file .Files ;
1211import java .nio .file .Path ;
13- import java .util .Collections ;
1412
13+ @ JsonAdapter (Config .GsonAdapter .class )
1514public class Config {
16- public VideoModeSet .VideoMode videoMode = VideoModeManager .getFirstAvailable ().getVideoMode ();
15+
16+ public VideoMode videoMode ;
1717 public int windowMode = 0 ;
1818
1919 public int advCulling = 2 ;
2020 public boolean indirectDraw = true ;
21-
2221 public boolean uniqueOpaqueLayer = true ;
2322 public boolean entityCulling = true ;
24- public int device = -1 ;
2523
2624 public int ambientOcclusion = 1 ;
2725 public int frameQueueSize = 2 ;
2826 public int builderThreads = 0 ;
29-
3027 public boolean backFaceCulling = true ;
3128 public boolean textureAnimations = true ;
3229
33- public void write () {
30+ public int device = - 1 ;
3431
35- if (!Files .exists (CONFIG_PATH .getParent ())) {
36- try {
37- Files .createDirectories (CONFIG_PATH );
38- } catch (IOException e ) {
39- e .printStackTrace ();
40- }
41- }
32+ private static Path CONFIG_PATH ;
33+ private static final Gson GSON = new GsonBuilder ()
34+ .setPrettyPrinting ()
35+ .registerTypeAdapter (Config .class , new GsonAdapter ())
36+ .create ();
4237
38+ public void save () {
4339 try {
44- Files .write (CONFIG_PATH , Collections .singleton (GSON .toJson (this )));
40+ Files .createDirectories (CONFIG_PATH .getParent ());
41+ Files .writeString (CONFIG_PATH , GSON .toJson (this ));
4542 } catch (IOException e ) {
46- e . printStackTrace ( );
43+ Initializer . LOGGER . error ( "Error saving config file!" , e );
4744 }
4845 }
4946
50- private static Path CONFIG_PATH ;
51-
52- private static final Gson GSON = new GsonBuilder ()
53- .setPrettyPrinting ()
54- .excludeFieldsWithModifiers (Modifier .PRIVATE )
55- .create ();
56-
5747 public static Config load (Path path ) {
58- Config config ;
59- Config .CONFIG_PATH = path ;
48+ CONFIG_PATH = path ;
6049
6150 if (Files .exists (path )) {
62- try (FileReader fileReader = new FileReader (path .toFile ())) {
63- config = GSON .fromJson (fileReader , Config .class );
51+ try {
52+ String content = Files .readString (path );
53+ Config config = GSON .fromJson (content , Config .class );
54+
55+ if (config .videoMode == null ||
56+ VideoModeManager .findSetFor (config .videoMode ) == null ) {
57+ config .videoMode = VideoModeManager .currentOsMode ();
58+ }
59+
60+ return config ;
61+ } catch (IOException | JsonSyntaxException e ) {
62+ System .err .println ("Failed to load config, using defaults: " + e .getMessage ());
6463 }
65- catch (IOException exception ) {
66- throw new RuntimeException (exception .getMessage ());
64+ }
65+
66+ Config config = new Config ();
67+ config .videoMode = VideoModeManager .currentOsMode ();
68+ return config ;
69+ }
70+
71+ public static class GsonAdapter implements JsonSerializer <Config >, JsonDeserializer <Config > {
72+
73+ @ Override
74+ public JsonElement serialize (Config src , java .lang .reflect .Type typeOfSrc , JsonSerializationContext context ) {
75+ JsonObject obj = new JsonObject ();
76+
77+ if (src .videoMode != null ) {
78+ JsonObject vm = new JsonObject ();
79+ vm .addProperty ("width" , src .videoMode .width ());
80+ vm .addProperty ("height" , src .videoMode .height ());
81+ vm .addProperty ("bitDepth" , src .videoMode .bitDepth ());
82+ vm .addProperty ("refreshRate" , src .videoMode .refreshRate ());
83+ obj .add ("videoMode" , vm );
6784 }
85+
86+ obj .addProperty ("windowMode" , src .windowMode );
87+ obj .addProperty ("advCulling" , src .advCulling );
88+ obj .addProperty ("indirectDraw" , src .indirectDraw );
89+ obj .addProperty ("uniqueOpaqueLayer" , src .uniqueOpaqueLayer );
90+ obj .addProperty ("entityCulling" , src .entityCulling );
91+ obj .addProperty ("ambientOcclusion" , src .ambientOcclusion );
92+ obj .addProperty ("frameQueueSize" , src .frameQueueSize );
93+ obj .addProperty ("builderThreads" , src .builderThreads );
94+ obj .addProperty ("backFaceCulling" , src .backFaceCulling );
95+ obj .addProperty ("textureAnimations" , src .textureAnimations );
96+ obj .addProperty ("device" , src .device );
97+
98+ return obj ;
6899 }
69- else {
70- config = null ;
100+
101+ @ Override
102+ public Config deserialize (JsonElement json , java .lang .reflect .Type typeOfT , JsonDeserializationContext context ) throws JsonParseException {
103+ Config config = new Config ();
104+ JsonObject obj = json .getAsJsonObject ();
105+
106+ if (obj .has ("videoMode" )) {
107+ JsonObject vm = obj .getAsJsonObject ("videoMode" );
108+ int w = getInt (vm , "width" , 1920 );
109+ int h = getInt (vm , "height" , 1080 );
110+ int bd = getInt (vm , "bitDepth" , 8 );
111+ int rr = getInt (vm , "refreshRate" , 60 );
112+ config .videoMode = new VideoMode (w , h , bd , rr );
113+ } else {
114+ config .videoMode = VideoModeManager .currentOsMode ();
115+ }
116+
117+ config .windowMode = getInt (obj , "windowMode" , 0 );
118+ config .advCulling = getInt (obj , "advCulling" , 2 );
119+ config .indirectDraw = getBoolean (obj , "indirectDraw" );
120+ config .uniqueOpaqueLayer = getBoolean (obj , "uniqueOpaqueLayer" );
121+ config .entityCulling = getBoolean (obj , "entityCulling" );
122+ config .ambientOcclusion = getInt (obj , "ambientOcclusion" , 1 );
123+ config .frameQueueSize = getInt (obj , "frameQueueSize" , 2 );
124+ config .builderThreads = getInt (obj , "builderThreads" , 0 );
125+ config .backFaceCulling = getBoolean (obj , "backFaceCulling" );
126+ config .textureAnimations = getBoolean (obj , "textureAnimations" );
127+ config .device = getInt (obj , "device" , -1 );
128+
129+ return config ;
71130 }
72131
73- return config ;
132+ private int getInt (JsonObject obj , String key , int def ) {
133+ JsonElement el = obj .get (key );
134+ return el != null && el .isJsonPrimitive () ? el .getAsInt () : def ;
135+ }
136+
137+ private boolean getBoolean (JsonObject obj , String key ) {
138+ JsonElement el = obj .get (key );
139+ return el == null || !el .isJsonPrimitive () || el .getAsBoolean ();
140+ }
74141 }
75- }
142+ }
0 commit comments