mirror of
https://github.com/xiaozhou233/OrangeX-Client.git
synced 2026-12-05 21:15:53 +08:00
[Add] Module config system & fix apply order
This commit is contained in:
1 parent
270d259294
commit
7ddc45bd4e
5 files changed
+114
-7
No files matched your search
@@ -36,8 +36,8 @@ public class OrangeX {
|
|||||||
// See Loader -> cn.xiaozhou233.orangex.loader.Loader
|
// See Loader -> cn.xiaozhou233.orangex.loader.Loader
|
||||||
// See Minecraft -> cn.xiaozhou233.MCPEntry
|
// See Minecraft -> cn.xiaozhou233.MCPEntry
|
||||||
public void start() {
|
public void start() {
|
||||||
configManager.loadConfig();
|
|
||||||
moduleManager.init();
|
moduleManager.init();
|
||||||
|
configManager.loadConfig();
|
||||||
altManager.init();
|
altManager.init();
|
||||||
mixinManager.start();
|
mixinManager.start();
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -1,6 +1,12 @@
|
|||||||
package cn.xiaozhou233.orangex.config;
|
package cn.xiaozhou233.orangex.config;
|
||||||
|
|
||||||
import cn.xiaozhou233.orangex.OrangeX;
|
import cn.xiaozhou233.orangex.OrangeX;
|
||||||
|
import cn.xiaozhou233.orangex.module.Module;
|
||||||
|
import cn.xiaozhou233.orangex.module.value.BooleanValue;
|
||||||
|
import cn.xiaozhou233.orangex.module.value.KeybindValue;
|
||||||
|
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.Gson;
|
||||||
import com.google.gson.reflect.TypeToken;
|
import com.google.gson.reflect.TypeToken;
|
||||||
|
|
||||||
@@ -13,24 +19,37 @@ import java.util.HashMap;
|
|||||||
public class ConfigManager {
|
public class ConfigManager {
|
||||||
|
|
||||||
private final Gson gson = new Gson();
|
private final Gson gson = new Gson();
|
||||||
|
|
||||||
private HashMap<String, PanelPosition> panelConfigMap = new HashMap<>();
|
private HashMap<String, PanelPosition> panelConfigMap = new HashMap<>();
|
||||||
|
|
||||||
|
private HashMap<String, ModuleConfig> moduleConfigMap = new HashMap<>();
|
||||||
|
|
||||||
public void loadConfig() {
|
public void loadConfig() {
|
||||||
try {
|
try {
|
||||||
// Load Panel Position
|
// Load Panel Position
|
||||||
File panelConfig = OrangeX.getInstance().getResourceManager().getPanelConfig();
|
File panelConfig = OrangeX.getInstance().getResourceManager().getPanelConfig();
|
||||||
if (!panelConfig.exists()) {
|
if (!panelConfig.exists()) panelConfig.createNewFile();
|
||||||
panelConfig.createNewFile();
|
|
||||||
}
|
|
||||||
if (panelConfig.length() != 0) {
|
if (panelConfig.length() != 0) {
|
||||||
Type type = new TypeToken<HashMap<String, PanelPosition>>() {}.getType();
|
Type type = new TypeToken<HashMap<String, PanelPosition>>() {}.getType();
|
||||||
FileReader reader = new FileReader(panelConfig);
|
FileReader reader = new FileReader(panelConfig);
|
||||||
HashMap<String, PanelPosition> map = gson.fromJson(reader, type);
|
HashMap<String, PanelPosition> map = gson.fromJson(reader, type);
|
||||||
reader.close();
|
reader.close();
|
||||||
if (map != null) {
|
if (map != null) this.panelConfigMap = map;
|
||||||
this.panelConfigMap = map;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// Load Module Config
|
||||||
|
File moduleConfig = OrangeX.getInstance().getResourceManager().getModulesConfig();
|
||||||
|
if (!moduleConfig.exists()) moduleConfig.createNewFile();
|
||||||
|
if (moduleConfig.length() != 0) {
|
||||||
|
Type type = new TypeToken<HashMap<String, ModuleConfig>>() {}.getType();
|
||||||
|
FileReader reader = new FileReader(moduleConfig);
|
||||||
|
HashMap<String, ModuleConfig> map = gson.fromJson(reader, type);
|
||||||
|
reader.close();
|
||||||
|
if (map != null) this.moduleConfigMap = map;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
applyModuleConfig();
|
||||||
|
|
||||||
} catch (Exception e) {
|
} catch (Exception e) {
|
||||||
throw new RuntimeException(e);
|
throw new RuntimeException(e);
|
||||||
}
|
}
|
||||||
@@ -46,14 +65,88 @@ public class ConfigManager {
|
|||||||
|
|
||||||
public void saveConfig() {
|
public void saveConfig() {
|
||||||
try {
|
try {
|
||||||
// Panel Position
|
|
||||||
FileWriter writer = new FileWriter(OrangeX.getInstance().getResourceManager().getPanelConfig());
|
FileWriter writer = new FileWriter(OrangeX.getInstance().getResourceManager().getPanelConfig());
|
||||||
writer.write(gson.toJson(this.panelConfigMap));
|
writer.write(gson.toJson(this.panelConfigMap));
|
||||||
writer.close();
|
writer.close();
|
||||||
|
|
||||||
|
generateModuleConfig();
|
||||||
|
FileWriter moduleWriter = new FileWriter(OrangeX.getInstance().getResourceManager().getModulesConfig());
|
||||||
|
moduleWriter.write(gson.toJson(this.moduleConfigMap));
|
||||||
|
moduleWriter.close();
|
||||||
|
|
||||||
} catch (Exception e) {
|
} catch (Exception e) {
|
||||||
throw new RuntimeException(e);
|
throw new RuntimeException(e);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
private void generateModuleConfig() {
|
||||||
|
moduleConfigMap.clear();
|
||||||
|
|
||||||
|
for (Module module : OrangeX.getInstance().getModuleManager().getAllModules()) {
|
||||||
|
ModuleConfig mc = new ModuleConfig();
|
||||||
|
mc.enabled = module.isEnabled();
|
||||||
|
mc.keybind = module.getKeyBind().getValue();
|
||||||
|
|
||||||
|
for (Value<?> v : module.getValues()) {
|
||||||
|
mc.values.put(v.getName(), v.getValue());
|
||||||
|
}
|
||||||
|
|
||||||
|
moduleConfigMap.put(module.getName(), mc);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
@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);
|
||||||
|
|
||||||
|
for (Value<?> v : module.getValues()) {
|
||||||
|
Object raw = mc.values.get(v.getName());
|
||||||
|
if (raw == null) continue;
|
||||||
|
|
||||||
|
Object converted = convertValue(raw, v);
|
||||||
|
if (converted != null) {
|
||||||
|
((Value<Object>) v).setValue(converted);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
private Object convertValue(Object raw, Value<?> targetValue) {
|
||||||
|
if (raw == null) return null;
|
||||||
|
|
||||||
|
if (targetValue instanceof KeybindValue) {
|
||||||
|
if (raw instanceof Number) return ((Number) raw).intValue();
|
||||||
|
if (raw instanceof String) return Integer.parseInt((String) raw);
|
||||||
|
}
|
||||||
|
|
||||||
|
if (targetValue instanceof BooleanValue) {
|
||||||
|
if (raw instanceof Boolean) return raw;
|
||||||
|
if (raw instanceof String) return Boolean.parseBoolean((String) raw);
|
||||||
|
}
|
||||||
|
|
||||||
|
if (targetValue instanceof NumberValue) {
|
||||||
|
if (raw instanceof Number) return ((Number) raw).doubleValue();
|
||||||
|
if (raw instanceof String) return Double.parseDouble((String) raw);
|
||||||
|
}
|
||||||
|
|
||||||
|
if (targetValue instanceof ModeValue) {
|
||||||
|
if (raw instanceof String) {
|
||||||
|
String mode = (String) raw;
|
||||||
|
ModeValue mv = (ModeValue) targetValue;
|
||||||
|
if (mv.isValidMode(mode)) return mode;
|
||||||
|
return mv.getValue();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
if (targetValue.getValue().getClass().isInstance(raw)) {
|
||||||
|
return raw;
|
||||||
|
}
|
||||||
|
|
||||||
|
return null;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
@@ -0,0 +1,10 @@
|
|||||||
|
package cn.xiaozhou233.orangex.config;
|
||||||
|
|
||||||
|
import java.util.HashMap;
|
||||||
|
import java.util.Map;
|
||||||
|
|
||||||
|
public class ModuleConfig {
|
||||||
|
public boolean enabled;
|
||||||
|
public int keybind;
|
||||||
|
public Map<String, Object> values = new HashMap<>();
|
||||||
|
}
|
||||||
@@ -13,4 +13,6 @@ public class ResourceManager {
|
|||||||
|
|
||||||
// Panel Config
|
// Panel Config
|
||||||
private File panelConfig = new File(orangeXDir, "panel.json");
|
private File panelConfig = new File(orangeXDir, "panel.json");
|
||||||
|
// Modules Config
|
||||||
|
private File modulesConfig = new File(orangeXDir, "modules.json");
|
||||||
}
|
}
|
||||||
@@ -18,6 +18,7 @@ public abstract class Module {
|
|||||||
protected static final FontRenderer fr = mc.fontRendererObj;
|
protected static final FontRenderer fr = mc.fontRendererObj;
|
||||||
|
|
||||||
protected final String name;
|
protected final String name;
|
||||||
|
@Getter
|
||||||
@Setter
|
@Setter
|
||||||
protected KeybindValue keyBind = new KeybindValue("Bind", "KeyBinding", 0);
|
protected KeybindValue keyBind = new KeybindValue("Bind", "KeyBinding", 0);
|
||||||
protected final ModuleCategory category;
|
protected final ModuleCategory category;
|
||||||
@@ -80,4 +81,5 @@ public abstract class Module {
|
|||||||
}
|
}
|
||||||
return null;
|
return null;
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
Reference in new issue
Block a user