1818
1919import cn .enaium .joe .config .extend .ApplicationConfig ;
2020import cn .enaium .joe .config .extend .CFRConfig ;
21- import cn .enaium .joe .config .value .Value ;
21+ import cn .enaium .joe .config .extend .FernFlowerConfig ;
22+ import cn .enaium .joe .config .extend .ProcyonConfig ;
23+ import cn .enaium .joe .config .value .*;
2224import cn .enaium .joe .util .MessageUtil ;
2325import cn .enaium .joe .util .ReflectUtil ;
2426import com .google .gson .*;
2931import java .lang .reflect .Field ;
3032import java .nio .charset .StandardCharsets ;
3133import java .nio .file .Files ;
32- import java .util .HashMap ;
33- import java .util .Map ;
34+ import java .util .*;
3435
3536/**
3637 * @author Enaium
3738 * @since 0.7.0
3839 */
3940public class ConfigManager {
40- private final Map <Class <? extends Config >, Config > configMap = new HashMap <>();
41+ private final Map <Class <? extends Config >, Config > configMap = new LinkedHashMap <>();
4142
4243 public ConfigManager () {
4344 setByClass (new ApplicationConfig ());
4445 setByClass (new CFRConfig ());
46+ setByClass (new FernFlowerConfig ());
47+ setByClass (new ProcyonConfig ());
4548 }
4649
4750 @ SuppressWarnings ("unchecked" )
@@ -70,7 +73,7 @@ public Map<String, String> getConfigMap(Class<? extends Config> config) {
7073 if (o instanceof Value <?>) {
7174 Object value = ((Value <?>) o ).getValue ();
7275 if (value != null ) {
73- map .put ((( Value <?>) o ) .getName (), value .toString ());
76+ map .put (declaredField .getName (), value .toString ());
7477 }
7578 }
7679 } catch (IllegalAccessException e ) {
@@ -92,34 +95,48 @@ public void load() {
9295 try {
9396 File file = new File (System .getProperty ("." ), config .getName () + ".json" );
9497 if (file .exists ()) {
95- //Step.1 get json object
9698 JsonObject jsonObject = gson ().fromJson (new String (Files .readAllBytes (file .toPath ()), StandardCharsets .UTF_8 ), JsonObject .class );
9799
98- //Step.2 get all field of the config
99100 for (Field configField : klass .getDeclaredFields ()) {
100101 configField .setAccessible (true );
101102 if (!jsonObject .has (configField .getName ())) {
102103 continue ;
103104 }
104105
105- //Step.3 deserialize
106- Object setting = gson ().fromJson (jsonObject .get (configField .getName ()).toString (), configField .getType ());
106+ if (!jsonObject .has (configField .getName ())) {
107+ continue ;
108+ }
107109
108- //Step.3.1 get all field of the setting
109- for ( Field settingField : setting . getClass (). getDeclaredFields ()) {
110- settingField . setAccessible ( true );
110+ if (! jsonObject . get ( configField . getName ()). getAsJsonObject (). has ( "value" )) {
111+ continue ;
112+ }
111113
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 ()));
114+ JsonElement valueJsonElement = jsonObject .get (configField .getName ()).getAsJsonObject ().get ("value" );
115+
116+ Object valueObject = configField .get (config );
117+ if (valueObject instanceof Value <?>) {
118+ Value <?> value = (Value <?>) valueObject ;
119+ if (value instanceof EnableValue ) {
120+ ((EnableValue ) value ).setValue (valueJsonElement .getAsBoolean ());
121+ } else if (value instanceof IntegerValue ) {
122+ ((IntegerValue ) value ).setValue (valueJsonElement .getAsInt ());
123+ } else if (value instanceof ModeValue ) {
124+ ModeValue modeValue = (ModeValue ) value ;
125+ if (modeValue .getMode ().contains (valueJsonElement .getAsString ())) {
126+ modeValue .setValue (valueJsonElement .getAsString ());
127+ } else {
128+ modeValue .setValue (modeValue .getMode ().get (0 ));
129+ }
130+ } else if (value instanceof StringSetValue ) {
131+ Set <String > strings = new HashSet <>();
132+ for (JsonElement jsonElement : valueJsonElement .getAsJsonArray ()) {
133+ strings .add (jsonElement .getAsString ());
117134 }
135+ ((StringSetValue ) value ).setValue (strings );
136+ } else if (value instanceof StringValue ) {
137+ ((StringValue ) value ).setValue (valueJsonElement .getAsString ());
118138 }
119139 }
120-
121- //Step.4 use the value from step 3 to set the value of config
122- configField .set (config , setting );
123140 }
124141 }
125142 } catch (Throwable e ) {
0 commit comments