mirror of
https://github.com/xiaozhou233/OrangeX-Client.git
synced 2026-12-05 21:15:53 +08:00
[Refactor] Make module config type-safe with JsonElement
[Change] Replace Object-based value storage with JsonElement [Change] Let Value handle its own deserialization logic [Remove] Unsafe casts, convertValue helper and suppression warnings [Improve] Config apply logic stability and maintainability
This commit is contained in:
1 parent
cdb50a36f0
commit
0825004689
7 files changed
+53
-18
No files matched your search
@@ -8,6 +8,7 @@ import cn.xiaozhou233.orangex.module.value.ModeValue;
|
||||
import cn.xiaozhou233.orangex.module.value.NumberValue;
|
||||
import cn.xiaozhou233.orangex.module.value.Value;
|
||||
import com.google.gson.Gson;
|
||||
import com.google.gson.JsonElement;
|
||||
import com.google.gson.reflect.TypeToken;
|
||||
|
||||
import java.io.File;
|
||||
@@ -96,39 +97,39 @@ public class ConfigManager {
|
||||
moduleConfigMap.clear();
|
||||
|
||||
for (Module module : OrangeX.getInstance().getModuleManager().getAllModules()) {
|
||||
ModuleConfig mc = new ModuleConfig();
|
||||
mc.enabled = module.isEnabled();
|
||||
mc.keybind = module.getKeyBind().getValue();
|
||||
ModuleConfig moduleConfig = new ModuleConfig();
|
||||
moduleConfig.setEnabled(module.isEnabled());
|
||||
moduleConfig.setKeybind(module.getKeyBind().getValue());
|
||||
|
||||
for (Value<?> v : module.getValues()) {
|
||||
mc.values.put(v.getName(), v.getValue());
|
||||
moduleConfig.getValues().put(
|
||||
v.getName(),
|
||||
gson.toJsonTree(v.getValue())
|
||||
);
|
||||
}
|
||||
|
||||
moduleConfigMap.put(module.getName(), mc);
|
||||
moduleConfigMap.put(module.getName(), moduleConfig);
|
||||
}
|
||||
}
|
||||
|
||||
@SuppressWarnings("unchecked")
|
||||
private void applyModuleConfig() {
|
||||
for (Module module : OrangeX.getInstance().getModuleManager().getAllModules()) {
|
||||
ModuleConfig mc = moduleConfigMap.get(module.getName());
|
||||
if (mc == null) continue;
|
||||
|
||||
module.setEnabled(mc.enabled);
|
||||
module.getKeyBind().setValue(mc.keybind);
|
||||
module.setEnabled(mc.isEnabled());
|
||||
module.getKeyBind().setValue(mc.getKeybind());
|
||||
|
||||
for (Value<?> v : module.getValues()) {
|
||||
Object raw = mc.values.get(v.getName());
|
||||
if (raw == null) continue;
|
||||
for (Value<?> value : module.getValues()) {
|
||||
JsonElement element = mc.getValues().get(value.getName());
|
||||
if (element == null) continue;
|
||||
|
||||
Object converted = convertValue(raw, v);
|
||||
if (converted != null) {
|
||||
((Value<Object>) v).setValue(converted);
|
||||
}
|
||||
value.fromJson(element);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
private Object convertValue(Object raw, Value<?> targetValue) {
|
||||
if (raw == null) return null;
|
||||
|
||||
|
||||
@@ -1,10 +1,15 @@
|
||||
package cn.xiaozhou233.orangex.config;
|
||||
|
||||
import com.google.gson.JsonElement;
|
||||
import lombok.Getter;
|
||||
import lombok.Setter;
|
||||
|
||||
import java.util.HashMap;
|
||||
import java.util.Map;
|
||||
|
||||
@Getter @Setter
|
||||
public class ModuleConfig {
|
||||
public boolean enabled;
|
||||
public int keybind;
|
||||
public Map<String, Object> values = new HashMap<>();
|
||||
private boolean enabled;
|
||||
private int keybind;
|
||||
private Map<String, JsonElement> values = new HashMap<>();
|
||||
}
|
||||
@@ -4,4 +4,9 @@ public class BooleanValue extends Value<Boolean> {
|
||||
public BooleanValue(String name, String description, Boolean defaultValue) {
|
||||
super(name, description, defaultValue);
|
||||
}
|
||||
|
||||
@Override
|
||||
public Class<Boolean> getValueClass() {
|
||||
return Boolean.class;
|
||||
}
|
||||
}
|
||||
@@ -9,4 +9,9 @@ public class KeybindValue extends Value<Integer> {
|
||||
public void setKey(int key) {
|
||||
setValue(key);
|
||||
}
|
||||
|
||||
@Override
|
||||
public Class<Integer> getValueClass() {
|
||||
return Integer.class;
|
||||
}
|
||||
}
|
||||
@@ -46,4 +46,10 @@ public class ModeValue extends Value<String> {
|
||||
public boolean isValidMode(String mode) {
|
||||
return modes.contains(mode);
|
||||
}
|
||||
|
||||
|
||||
@Override
|
||||
public Class<String> getValueClass() {
|
||||
return String.class;
|
||||
}
|
||||
}
|
||||
@@ -16,4 +16,8 @@ public class NumberValue extends Value<Double> {
|
||||
this.increment = increment;
|
||||
}
|
||||
|
||||
@Override
|
||||
public Class<Double> getValueClass() {
|
||||
return Double.class;
|
||||
}
|
||||
}
|
||||
@@ -1,10 +1,13 @@
|
||||
package cn.xiaozhou233.orangex.module.value;
|
||||
|
||||
import com.google.gson.Gson;
|
||||
import com.google.gson.JsonElement;
|
||||
import lombok.Getter;
|
||||
import lombok.Setter;
|
||||
|
||||
@Getter
|
||||
public abstract class Value<T> {
|
||||
private static final Gson gson = new Gson();
|
||||
|
||||
private final String name;
|
||||
private final String description;
|
||||
@@ -17,4 +20,10 @@ public abstract class Value<T> {
|
||||
this.value = defaultValue;
|
||||
}
|
||||
|
||||
public void fromJson(JsonElement element) {
|
||||
setValue(gson.fromJson(element, getValueClass()));
|
||||
}
|
||||
|
||||
public abstract Class<T> getValueClass();
|
||||
|
||||
}
|
||||
Reference in new issue
Block a user