mirror of
https://github.com/xiaozhou233/OrangeX-Client.git
synced 2026-12-05 13:05:52 +08:00
Merge pull request #5 from xiaozhou233/feat/ConfigManager
[Add] Module config system & fix apply order
This commit is contained in:
commit
3c754e8f41
10 files changed
+184
-22
No files matched your search
@@ -36,8 +36,8 @@ public class OrangeX {
|
||||
// See Loader -> cn.xiaozhou233.orangex.loader.Loader
|
||||
// See Minecraft -> cn.xiaozhou233.MCPEntry
|
||||
public void start() {
|
||||
configManager.loadConfig();
|
||||
moduleManager.init();
|
||||
configManager.loadConfig();
|
||||
altManager.init();
|
||||
mixinManager.start();
|
||||
}
|
||||
|
||||
@@ -1,37 +1,65 @@
|
||||
package cn.xiaozhou233.orangex.config;
|
||||
|
||||
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.JsonElement;
|
||||
import com.google.gson.reflect.TypeToken;
|
||||
|
||||
import java.io.File;
|
||||
import java.io.FileReader;
|
||||
import java.io.FileWriter;
|
||||
import java.io.IOException;
|
||||
import java.lang.reflect.Type;
|
||||
import java.util.HashMap;
|
||||
|
||||
public class ConfigManager {
|
||||
|
||||
private final Gson gson = new Gson();
|
||||
|
||||
private HashMap<String, PanelPosition> panelConfigMap = new HashMap<>();
|
||||
|
||||
private HashMap<String, ModuleConfig> moduleConfigMap = new HashMap<>();
|
||||
|
||||
public void loadConfig() {
|
||||
try {
|
||||
// Load Panel Position
|
||||
File panelConfig = OrangeX.getInstance().getResourceManager().getPanelConfig();
|
||||
if (!panelConfig.exists()) {
|
||||
panelConfig.createNewFile();
|
||||
}
|
||||
if (!panelConfig.exists()) panelConfig.createNewFile();
|
||||
if (panelConfig.length() != 0) {
|
||||
Type type = new TypeToken<HashMap<String, PanelPosition>>() {}.getType();
|
||||
FileReader reader = new FileReader(panelConfig);
|
||||
HashMap<String, PanelPosition> map = gson.fromJson(reader, type);
|
||||
reader.close();
|
||||
if (map != null) {
|
||||
this.panelConfigMap = map;
|
||||
Type type = new TypeToken<HashMap<String, PanelPosition>>() {
|
||||
}.getType();
|
||||
try (FileReader reader = new FileReader(panelConfig)) {
|
||||
HashMap<String, PanelPosition> map = gson.fromJson(reader, type);
|
||||
if (map != null) this.panelConfigMap = map;
|
||||
}
|
||||
}
|
||||
} catch (Exception e) {
|
||||
System.out.println("Failed to load panel config!");
|
||||
throw new RuntimeException(e);
|
||||
}
|
||||
|
||||
try {
|
||||
// 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();
|
||||
try (FileReader reader = new FileReader(moduleConfig)) {
|
||||
HashMap<String, ModuleConfig> map = gson.fromJson(reader, type);
|
||||
if (map != null) this.moduleConfigMap = map;
|
||||
}
|
||||
}
|
||||
|
||||
applyModuleConfig();
|
||||
|
||||
} catch (Exception e) {
|
||||
System.out.println("Failed to load module config!");
|
||||
throw new RuntimeException(e);
|
||||
}
|
||||
}
|
||||
@@ -45,15 +73,94 @@ public class ConfigManager {
|
||||
}
|
||||
|
||||
public void saveConfig() {
|
||||
try {
|
||||
// Panel Position
|
||||
FileWriter writer = new FileWriter(OrangeX.getInstance().getResourceManager().getPanelConfig());
|
||||
// Save panel config
|
||||
try(FileWriter writer = new FileWriter(
|
||||
OrangeX.getInstance().getResourceManager().getPanelConfig())) {
|
||||
writer.write(gson.toJson(this.panelConfigMap));
|
||||
writer.close();
|
||||
} catch (IOException e) {
|
||||
System.out.println("Failed to save panel config");
|
||||
throw new RuntimeException(e);
|
||||
}
|
||||
|
||||
|
||||
} catch (Exception e) {
|
||||
// Save module config
|
||||
generateModuleConfig();
|
||||
try (FileWriter writer = new FileWriter(
|
||||
OrangeX.getInstance().getResourceManager().getModulesConfig())) {
|
||||
writer.write(gson.toJson(this.moduleConfigMap));
|
||||
} catch (IOException e) {
|
||||
System.out.println("Failed to save module config");
|
||||
throw new RuntimeException(e);
|
||||
}
|
||||
}
|
||||
|
||||
private void generateModuleConfig() {
|
||||
moduleConfigMap.clear();
|
||||
|
||||
for (Module module : OrangeX.getInstance().getModuleManager().getAllModules()) {
|
||||
ModuleConfig moduleConfig = new ModuleConfig();
|
||||
moduleConfig.setEnabled(module.isEnabled());
|
||||
moduleConfig.setKeybind(module.getKeyBind().getValue());
|
||||
|
||||
for (Value<?> v : module.getValues()) {
|
||||
moduleConfig.getValues().put(
|
||||
v.getName(),
|
||||
gson.toJsonTree(v.getValue())
|
||||
);
|
||||
}
|
||||
|
||||
moduleConfigMap.put(module.getName(), moduleConfig);
|
||||
}
|
||||
}
|
||||
|
||||
private void applyModuleConfig() {
|
||||
for (Module module : OrangeX.getInstance().getModuleManager().getAllModules()) {
|
||||
ModuleConfig mc = moduleConfigMap.get(module.getName());
|
||||
if (mc == null) continue;
|
||||
|
||||
module.setEnabled(mc.isEnabled());
|
||||
module.getKeyBind().setValue(mc.getKeybind());
|
||||
|
||||
for (Value<?> value : module.getValues()) {
|
||||
JsonElement element = mc.getValues().get(value.getName());
|
||||
if (element == null) continue;
|
||||
|
||||
value.fromJson(element);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
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,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 {
|
||||
private boolean enabled;
|
||||
private int keybind;
|
||||
private Map<String, JsonElement> values = new HashMap<>();
|
||||
}
|
||||
@@ -13,4 +13,6 @@ public class ResourceManager {
|
||||
|
||||
// Panel Config
|
||||
private File panelConfig = new File(orangeXDir, "panel.json");
|
||||
// Modules Config
|
||||
private File modulesConfig = new File(orangeXDir, "modules.json");
|
||||
}
|
||||
@@ -11,13 +11,15 @@ import net.minecraft.client.gui.FontRenderer;
|
||||
import java.awt.*;
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
import java.util.Optional;
|
||||
|
||||
@Getter
|
||||
public abstract class Module {
|
||||
protected static final Minecraft mc = Minecraft.getMinecraft();
|
||||
protected static final FontRenderer fr = mc.fontRendererObj;
|
||||
protected final Minecraft mc = Minecraft.getMinecraft();
|
||||
protected final FontRenderer fr = mc.fontRendererObj;
|
||||
|
||||
protected final String name;
|
||||
@Getter
|
||||
@Setter
|
||||
protected KeybindValue keyBind = new KeybindValue("Bind", "KeyBinding", 0);
|
||||
protected final ModuleCategory category;
|
||||
@@ -72,12 +74,13 @@ public abstract class Module {
|
||||
}
|
||||
|
||||
@SuppressWarnings("unchecked")
|
||||
public <T> Value<T> getValue(String name) {
|
||||
public <T> Optional<Value<T>> getValue(String name, Class<T> type) {
|
||||
for (Value<?> v : values) {
|
||||
if (v.getName().equalsIgnoreCase(name)) {
|
||||
return (Value<T>) v;
|
||||
if (v.getName().equalsIgnoreCase(name) && type.isInstance(v.getValue())) {
|
||||
return Optional.of((Value<T>) v);
|
||||
}
|
||||
}
|
||||
return null;
|
||||
return Optional.empty();
|
||||
}
|
||||
|
||||
}
|
||||
@@ -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,14 @@
|
||||
package cn.xiaozhou233.orangex.module.value;
|
||||
|
||||
import com.google.gson.Gson;
|
||||
import com.google.gson.JsonElement;
|
||||
import com.google.gson.JsonSyntaxException;
|
||||
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 +21,15 @@ public abstract class Value<T> {
|
||||
this.value = defaultValue;
|
||||
}
|
||||
|
||||
public void fromJson(JsonElement element) {
|
||||
try {
|
||||
setValue(gson.fromJson(element, getValueClass()));
|
||||
} catch (JsonSyntaxException e) {
|
||||
System.out.println("Failed to parse json for value " + name);
|
||||
e.printStackTrace();
|
||||
}
|
||||
}
|
||||
|
||||
public abstract Class<T> getValueClass();
|
||||
|
||||
}
|
||||
Reference in new issue
Block a user