2020import cn .enaium .joe .config .extend .CFRConfig ;
2121import cn .enaium .joe .config .value .Value ;
2222import cn .enaium .joe .util .MessageUtil ;
23- import com . google . gson . Gson ;
24- import com .google .gson .GsonBuilder ;
25- import org . tinylog . Logger ;
23+ import cn . enaium . joe . util . ReflectUtil ;
24+ import com .google .gson .* ;
25+ import com . google . gson . annotations . Expose ;
2626
2727import java .io .File ;
2828import java .io .IOException ;
@@ -44,6 +44,7 @@ public ConfigManager() {
4444 setByClass (new CFRConfig ());
4545 }
4646
47+ @ SuppressWarnings ("unchecked" )
4748 public <T > T getByClass (Class <T > klass ) {
4849 if (configMap .containsKey (klass )) {
4950 return (T ) configMap .get (klass );
@@ -79,15 +80,49 @@ public Map<String, String> getConfigMap(Class<? extends Config> config) {
7980 return map ;
8081 }
8182
83+ private Gson gson () {
84+ return new GsonBuilder ().setPrettyPrinting ().create ();
85+ }
86+
8287 public void load () {
83- for (Config value : configMap .values ()) {
88+ for (Map .Entry <Class <? extends Config >, Config > classConfigEntry : configMap .entrySet ()) {
89+
90+ Class <? extends Config > klass = classConfigEntry .getKey ();
91+ Config config = classConfigEntry .getValue ();
8492 try {
85- Gson gson = new GsonBuilder ().setPrettyPrinting ().create ();
86- File file = new File (System .getProperty ("." ), value .getName () + ".json" );
93+ File file = new File (System .getProperty ("." ), config .getName () + ".json" );
8794 if (file .exists ()) {
88- configMap .put (value .getClass (), gson .fromJson (new String (Files .readAllBytes (file .toPath ()), StandardCharsets .UTF_8 ), value .getClass ()));
95+ //Step.1 get json object
96+ JsonObject jsonObject = gson ().fromJson (new String (Files .readAllBytes (file .toPath ()), StandardCharsets .UTF_8 ), JsonObject .class );
97+
98+ //Step.2 get all field of the config
99+ for (Field configField : klass .getDeclaredFields ()) {
100+ configField .setAccessible (true );
101+ if (!jsonObject .has (configField .getName ())) {
102+ continue ;
103+ }
104+
105+ //Step.3 deserialize
106+ Object setting = gson ().fromJson (jsonObject .get (configField .getName ()).toString (), configField .getType ());
107+
108+ //Step.3.1 get all field of the setting
109+ for (Field settingField : setting .getClass ().getDeclaredFields ()) {
110+ settingField .setAccessible (true );
111+
112+ if (settingField .isAnnotationPresent (Expose .class )) {
113+ if (!settingField .getAnnotation (Expose .class ).deserialize ()) {
114+ Field declaredField = ReflectUtil .getField (setting .getClass (), settingField .getName ());
115+ //Step.3.2 use the value from config to set the value of setting
116+ declaredField .set (setting , ReflectUtil .getFieldValue (ReflectUtil .getFieldValue (config , configField .getName ()), settingField .getName ()));
117+ }
118+ }
119+ }
120+
121+ //Step.4 use the value from step 3 to set the value of config
122+ configField .set (config , setting );
123+ }
89124 }
90- } catch (IOException e ) {
125+ } catch (Throwable e ) {
91126 MessageUtil .error (e );
92127 }
93128 }
@@ -96,8 +131,7 @@ public void load() {
96131 public void save () {
97132 for (Config value : configMap .values ()) {
98133 try {
99- Gson gson = new GsonBuilder ().setPrettyPrinting ().create ();
100- Files .write (new File (System .getProperty ("." ), value .getName () + ".json" ).toPath (), gson .toJson (value ).getBytes (StandardCharsets .UTF_8 ));
134+ Files .write (new File (System .getProperty ("." ), value .getName () + ".json" ).toPath (), gson ().toJson (value ).getBytes (StandardCharsets .UTF_8 ));
101135 } catch (IOException e ) {
102136 MessageUtil .error (e );
103137 }
0 commit comments