mirror of
https://github.com/xiaozhou233/OrangeX-Client.git
synced 2026-12-05 21:15:53 +08:00
Feat: Persist module settings to named config files
- Add ConfigManager, Config and ConfigSerializer with JSON storage, validated names, sorted listings and atomic temp-file writes - Serialize module enabled state and typed values; ignore unknown or invalid entries so a malformed file cannot break a module - Track dirty state from value and module changes, then save on GUI close and JVM shutdown instead of only when the GUI is closed - Remember the last loaded config and refuse to delete the active one - Snapshot in-memory state before loading and roll back on failure; back up unreadable files so the client still has a writable config - Key modules by class name while still reading legacy display-name configs, so renaming a module keeps its stored settings - Initialise the config manager during OrangeX startup
This commit is contained in:
1 parent
30fde895b6
commit
8f314e1f5c
8 files changed
+632
-2
No files matched your search
@@ -1,5 +1,6 @@
|
|||||||
package cn.xiaozhou233.orangex;
|
package cn.xiaozhou233.orangex;
|
||||||
|
|
||||||
|
import cn.xiaozhou233.orangex.config.ConfigManager;
|
||||||
import cn.xiaozhou233.orangex.event.impl.EventKey;
|
import cn.xiaozhou233.orangex.event.impl.EventKey;
|
||||||
import cn.xiaozhou233.orangex.event.impl.EventRender2D;
|
import cn.xiaozhou233.orangex.event.impl.EventRender2D;
|
||||||
import cn.xiaozhou233.orangex.event.impl.EventTickPre;
|
import cn.xiaozhou233.orangex.event.impl.EventTickPre;
|
||||||
@@ -18,7 +19,11 @@ import lombok.Getter;
|
|||||||
import org.greenrobot.eventbus.EventBus;
|
import org.greenrobot.eventbus.EventBus;
|
||||||
import org.greenrobot.eventbus.Subscribe;
|
import org.greenrobot.eventbus.Subscribe;
|
||||||
|
|
||||||
|
import java.io.File;
|
||||||
|
|
||||||
public class OrangeX {
|
public class OrangeX {
|
||||||
|
public static String OrangeXPath = new File(System.getProperty("user.home")+"/.orangex").getAbsolutePath();
|
||||||
|
|
||||||
@Getter
|
@Getter
|
||||||
private static final OrangeX instance = new OrangeX();
|
private static final OrangeX instance = new OrangeX();
|
||||||
@Getter
|
@Getter
|
||||||
@@ -34,6 +39,9 @@ public class OrangeX {
|
|||||||
private Render2DManager render2DManager;
|
private Render2DManager render2DManager;
|
||||||
@Getter
|
@Getter
|
||||||
private ModuleManager moduleManager = new ModuleManager();
|
private ModuleManager moduleManager = new ModuleManager();
|
||||||
|
@Getter
|
||||||
|
private ConfigManager configManager;
|
||||||
|
|
||||||
@Getter
|
@Getter
|
||||||
private Minecraft minecraft;
|
private Minecraft minecraft;
|
||||||
@Getter
|
@Getter
|
||||||
@@ -76,6 +84,9 @@ public class OrangeX {
|
|||||||
|
|
||||||
moduleManager.init();
|
moduleManager.init();
|
||||||
|
|
||||||
|
configManager = new ConfigManager(new File(OrangeXPath, "configs/"), moduleManager);
|
||||||
|
configManager.init();
|
||||||
|
|
||||||
HookManager.getINSTANCE().applyAll();
|
HookManager.getINSTANCE().applyAll();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -0,0 +1,37 @@
|
|||||||
|
package cn.xiaozhou233.orangex.config;
|
||||||
|
|
||||||
|
import lombok.Getter;
|
||||||
|
|
||||||
|
import java.io.File;
|
||||||
|
|
||||||
|
public class Config {
|
||||||
|
|
||||||
|
private final ConfigManager manager;
|
||||||
|
@Getter
|
||||||
|
private final String name;
|
||||||
|
@Getter
|
||||||
|
private final File file;
|
||||||
|
|
||||||
|
public Config(ConfigManager manager, String name, File file) {
|
||||||
|
this.manager = manager;
|
||||||
|
this.name = name;
|
||||||
|
this.file = file;
|
||||||
|
}
|
||||||
|
|
||||||
|
public boolean load() {
|
||||||
|
return manager.load(name);
|
||||||
|
}
|
||||||
|
|
||||||
|
public boolean save() {
|
||||||
|
return manager.save(name);
|
||||||
|
}
|
||||||
|
|
||||||
|
public boolean exists() {
|
||||||
|
return file.isFile();
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public String toString() {
|
||||||
|
return name;
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -0,0 +1,462 @@
|
|||||||
|
package cn.xiaozhou233.orangex.config;
|
||||||
|
|
||||||
|
import cn.xiaozhou233.orangex.config.impl.JsonConfigSerializer;
|
||||||
|
import cn.xiaozhou233.orangex.modules.ModuleManager;
|
||||||
|
import cn.xiaozhou233.orangex.value.Value;
|
||||||
|
import com.google.gson.Gson;
|
||||||
|
import com.google.gson.GsonBuilder;
|
||||||
|
import com.google.gson.JsonObject;
|
||||||
|
import com.google.gson.JsonParser;
|
||||||
|
|
||||||
|
import java.io.File;
|
||||||
|
import java.io.FileReader;
|
||||||
|
import java.io.FileWriter;
|
||||||
|
import java.io.IOException;
|
||||||
|
import java.nio.charset.StandardCharsets;
|
||||||
|
import java.nio.file.Files;
|
||||||
|
import java.nio.file.StandardCopyOption;
|
||||||
|
import java.util.ArrayList;
|
||||||
|
import java.util.Collections;
|
||||||
|
import java.util.Comparator;
|
||||||
|
import java.util.List;
|
||||||
|
import java.util.Locale;
|
||||||
|
import java.util.regex.Pattern;
|
||||||
|
|
||||||
|
public class ConfigManager {
|
||||||
|
|
||||||
|
private static final Pattern VALID_NAME =
|
||||||
|
Pattern.compile("[a-zA-Z0-9_-]+");
|
||||||
|
|
||||||
|
private static final String EXTENSION = ".json";
|
||||||
|
private static final String TEMP_SUFFIX = ".json.tmp";
|
||||||
|
private static final String BACKUP_SUFFIX = ".json.bak";
|
||||||
|
private static final String CURRENT_FILE = "current.txt";
|
||||||
|
private static final String DEFAULT_CONFIG = "default";
|
||||||
|
|
||||||
|
private final File directory;
|
||||||
|
private final ModuleManager moduleManager;
|
||||||
|
private final ConfigSerializer serializer;
|
||||||
|
|
||||||
|
private final Gson gson = new GsonBuilder()
|
||||||
|
.setPrettyPrinting()
|
||||||
|
.create();
|
||||||
|
|
||||||
|
private Config currentConfig;
|
||||||
|
private volatile boolean dirty;
|
||||||
|
private boolean initialised;
|
||||||
|
|
||||||
|
public ConfigManager(File directory, ModuleManager moduleManager) {
|
||||||
|
this(directory, moduleManager, new JsonConfigSerializer());
|
||||||
|
}
|
||||||
|
|
||||||
|
public ConfigManager(
|
||||||
|
File directory,
|
||||||
|
ModuleManager moduleManager,
|
||||||
|
ConfigSerializer serializer
|
||||||
|
) {
|
||||||
|
this.directory = directory;
|
||||||
|
this.moduleManager = moduleManager;
|
||||||
|
this.serializer = serializer;
|
||||||
|
}
|
||||||
|
|
||||||
|
public synchronized void init() {
|
||||||
|
ensureDirectory();
|
||||||
|
|
||||||
|
if (!getFile(DEFAULT_CONFIG).isFile()) {
|
||||||
|
create(DEFAULT_CONFIG);
|
||||||
|
}
|
||||||
|
|
||||||
|
// Track any later value change so the client can persist settings even
|
||||||
|
// when the user never opens the config screen.
|
||||||
|
Value.setGlobalChangeHook(this::markDirty);
|
||||||
|
|
||||||
|
String name = readCurrentName();
|
||||||
|
if (name == null || getConfig(name) == null) {
|
||||||
|
name = DEFAULT_CONFIG;
|
||||||
|
}
|
||||||
|
|
||||||
|
if (!load(name)) {
|
||||||
|
// Never leave an unreadable file in place: keep a backup and start
|
||||||
|
// over from the current (default) state.
|
||||||
|
backupUnreadable(name);
|
||||||
|
create(name);
|
||||||
|
if (!load(name)) {
|
||||||
|
System.err.println(
|
||||||
|
"[OrangeX] Could not restore config '" + name + "'");
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
if (!initialised) {
|
||||||
|
initialised = true;
|
||||||
|
Runtime.getRuntime().addShutdownHook(new Thread(() -> {
|
||||||
|
try {
|
||||||
|
saveIfDirty();
|
||||||
|
} catch (Throwable throwable) {
|
||||||
|
System.err.println(
|
||||||
|
"[OrangeX] Failed to save config on shutdown: "
|
||||||
|
+ throwable.getMessage());
|
||||||
|
}
|
||||||
|
}, "OrangeX Config Save"));
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Creates a config file from the current in-memory module state, so a new
|
||||||
|
* preset starts out identical to what the user currently sees.
|
||||||
|
*/
|
||||||
|
public synchronized Config create(String name) {
|
||||||
|
validateName(name);
|
||||||
|
|
||||||
|
File file = getFile(name);
|
||||||
|
|
||||||
|
if (file.exists()) {
|
||||||
|
throw new IllegalArgumentException(
|
||||||
|
"Config already exists: " + name);
|
||||||
|
}
|
||||||
|
|
||||||
|
Config config = new Config(this, name, file);
|
||||||
|
|
||||||
|
if (!save(name)) {
|
||||||
|
throw new IllegalStateException(
|
||||||
|
"Failed to create config: " + name);
|
||||||
|
}
|
||||||
|
|
||||||
|
return config;
|
||||||
|
}
|
||||||
|
|
||||||
|
public synchronized boolean load(String name) {
|
||||||
|
Config config = getConfig(name);
|
||||||
|
|
||||||
|
if (config == null) {
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
|
// Do not silently drop unsaved changes when switching presets.
|
||||||
|
if (!isCurrent(name) && dirty && currentConfig != null && !save()) {
|
||||||
|
System.err.println(
|
||||||
|
"[OrangeX] Refusing to switch config: changes to '"
|
||||||
|
+ currentConfig.getName() + "' could not be saved");
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
|
File file = config.getFile();
|
||||||
|
boolean wasDirty = dirty;
|
||||||
|
|
||||||
|
// Snapshot the live state so a malformed file cannot leave modules
|
||||||
|
// half-restored.
|
||||||
|
JsonObject snapshot = serializer.serialize(moduleManager);
|
||||||
|
|
||||||
|
try (FileReader reader = new FileReader(file)) {
|
||||||
|
JsonObject json = JsonParser.parseReader(reader).getAsJsonObject();
|
||||||
|
|
||||||
|
int version = json.has("version")
|
||||||
|
? json.get("version").getAsInt()
|
||||||
|
: 1;
|
||||||
|
|
||||||
|
if (version != ConfigSerializer.CURRENT_VERSION) {
|
||||||
|
throw new IllegalStateException(
|
||||||
|
"Unsupported config version: " + version);
|
||||||
|
}
|
||||||
|
|
||||||
|
serializer.deserialize(moduleManager, json);
|
||||||
|
|
||||||
|
currentConfig = config;
|
||||||
|
dirty = false;
|
||||||
|
writeCurrentName(name);
|
||||||
|
return true;
|
||||||
|
|
||||||
|
} catch (Exception e) {
|
||||||
|
System.err.println(
|
||||||
|
"[OrangeX] Failed to load config '" + name + "': " + e.getMessage());
|
||||||
|
|
||||||
|
try {
|
||||||
|
serializer.deserialize(moduleManager, snapshot);
|
||||||
|
} catch (Exception rollback) {
|
||||||
|
System.err.println(
|
||||||
|
"[OrangeX] Failed to restore previous state: "
|
||||||
|
+ rollback.getMessage());
|
||||||
|
}
|
||||||
|
|
||||||
|
dirty = wasDirty;
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
public synchronized boolean save() {
|
||||||
|
if (currentConfig == null) {
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
|
return save(currentConfig.getName());
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Persists the current module state under {@code name}.
|
||||||
|
*
|
||||||
|
* @return {@code true} when the file was written.
|
||||||
|
*/
|
||||||
|
public synchronized boolean save(String name) {
|
||||||
|
validateName(name);
|
||||||
|
|
||||||
|
File file = getFile(name);
|
||||||
|
File tempFile = new File(directory, name + TEMP_SUFFIX);
|
||||||
|
|
||||||
|
JsonObject json = serializer.serialize(moduleManager);
|
||||||
|
|
||||||
|
try {
|
||||||
|
ensureDirectory();
|
||||||
|
|
||||||
|
try (FileWriter writer = new FileWriter(tempFile)) {
|
||||||
|
gson.toJson(json, writer);
|
||||||
|
}
|
||||||
|
|
||||||
|
try {
|
||||||
|
Files.move(
|
||||||
|
tempFile.toPath(),
|
||||||
|
file.toPath(),
|
||||||
|
StandardCopyOption.REPLACE_EXISTING,
|
||||||
|
StandardCopyOption.ATOMIC_MOVE
|
||||||
|
);
|
||||||
|
} catch (IOException e) {
|
||||||
|
Files.move(
|
||||||
|
tempFile.toPath(),
|
||||||
|
file.toPath(),
|
||||||
|
StandardCopyOption.REPLACE_EXISTING
|
||||||
|
);
|
||||||
|
}
|
||||||
|
|
||||||
|
if (currentConfig != null
|
||||||
|
&& currentConfig.getName().equalsIgnoreCase(name)) {
|
||||||
|
dirty = false;
|
||||||
|
}
|
||||||
|
|
||||||
|
return true;
|
||||||
|
|
||||||
|
} catch (IOException e) {
|
||||||
|
tempFile.delete();
|
||||||
|
|
||||||
|
System.err.println(
|
||||||
|
"[OrangeX] Failed to save config '" + name + "': "
|
||||||
|
+ e.getMessage());
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Saves only when values or module states changed since the last load or
|
||||||
|
* save. Used by periodic/GUI/shutdown save hooks.
|
||||||
|
*/
|
||||||
|
public synchronized boolean saveIfDirty() {
|
||||||
|
if (!dirty || currentConfig == null) {
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
return save();
|
||||||
|
}
|
||||||
|
|
||||||
|
public synchronized boolean delete(String name) {
|
||||||
|
Config config = getConfig(name);
|
||||||
|
|
||||||
|
if (config == null) {
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
|
// Deleting the active config would leave the client with nowhere to
|
||||||
|
// save to; require the caller to load another config first.
|
||||||
|
if (isCurrent(name)) {
|
||||||
|
System.err.println(
|
||||||
|
"[OrangeX] Refusing to delete the active config: " + name);
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
|
boolean deleted = config.getFile().delete();
|
||||||
|
|
||||||
|
if (deleted && name.equalsIgnoreCase(readCurrentName())) {
|
||||||
|
clearCurrentName();
|
||||||
|
}
|
||||||
|
|
||||||
|
return deleted;
|
||||||
|
}
|
||||||
|
|
||||||
|
public synchronized boolean rename(String oldName, String newName) {
|
||||||
|
validateName(oldName);
|
||||||
|
validateName(newName);
|
||||||
|
|
||||||
|
if (oldName.equalsIgnoreCase(newName)) {
|
||||||
|
// A pure case change cannot be represented reliably on
|
||||||
|
// case-insensitive file systems.
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
|
Config oldConfig = getConfig(oldName);
|
||||||
|
|
||||||
|
if (oldConfig == null || getFile(newName).exists()) {
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
|
File newFile = getFile(newName);
|
||||||
|
|
||||||
|
try {
|
||||||
|
Files.move(
|
||||||
|
oldConfig.getFile().toPath(),
|
||||||
|
newFile.toPath()
|
||||||
|
);
|
||||||
|
|
||||||
|
if (isCurrent(oldName)) {
|
||||||
|
currentConfig = new Config(this, newName, newFile);
|
||||||
|
writeCurrentName(newName);
|
||||||
|
}
|
||||||
|
|
||||||
|
return true;
|
||||||
|
|
||||||
|
} catch (IOException e) {
|
||||||
|
System.err.println(
|
||||||
|
"[OrangeX] Failed to rename config: " + e.getMessage());
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
public Config getConfig(String name) {
|
||||||
|
if (name == null) {
|
||||||
|
return null;
|
||||||
|
}
|
||||||
|
|
||||||
|
File file = getFile(name);
|
||||||
|
|
||||||
|
if (!file.isFile()) {
|
||||||
|
return null;
|
||||||
|
}
|
||||||
|
|
||||||
|
return new Config(this, name, file);
|
||||||
|
}
|
||||||
|
|
||||||
|
public Config getCurrentConfig() {
|
||||||
|
return currentConfig;
|
||||||
|
}
|
||||||
|
|
||||||
|
public boolean isDirty() {
|
||||||
|
return dirty;
|
||||||
|
}
|
||||||
|
|
||||||
|
public void markDirty() {
|
||||||
|
dirty = true;
|
||||||
|
}
|
||||||
|
|
||||||
|
public synchronized List<Config> getConfigs() {
|
||||||
|
File[] files = directory.listFiles(
|
||||||
|
(dir, name) -> name.toLowerCase(Locale.ROOT).endsWith(EXTENSION)
|
||||||
|
);
|
||||||
|
|
||||||
|
if (files == null) {
|
||||||
|
return Collections.emptyList();
|
||||||
|
}
|
||||||
|
|
||||||
|
List<Config> configs = new ArrayList<>();
|
||||||
|
|
||||||
|
for (File file : files) {
|
||||||
|
String name = file.getName();
|
||||||
|
name = name.substring(0, name.length() - EXTENSION.length());
|
||||||
|
|
||||||
|
if (VALID_NAME.matcher(name).matches()) {
|
||||||
|
configs.add(new Config(this, name, file));
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
configs.sort(Comparator.comparing(
|
||||||
|
Config::getName, String.CASE_INSENSITIVE_ORDER));
|
||||||
|
|
||||||
|
return configs;
|
||||||
|
}
|
||||||
|
|
||||||
|
public File getDirectory() {
|
||||||
|
return directory;
|
||||||
|
}
|
||||||
|
|
||||||
|
private boolean isCurrent(String name) {
|
||||||
|
return currentConfig != null
|
||||||
|
&& currentConfig.getName().equalsIgnoreCase(name);
|
||||||
|
}
|
||||||
|
|
||||||
|
private File getFile(String name) {
|
||||||
|
validateName(name);
|
||||||
|
return new File(directory, name + EXTENSION);
|
||||||
|
}
|
||||||
|
|
||||||
|
private void validateName(String name) {
|
||||||
|
if (name == null || !VALID_NAME.matcher(name).matches()) {
|
||||||
|
throw new IllegalArgumentException(
|
||||||
|
"Invalid config name: " + name);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
private void ensureDirectory() {
|
||||||
|
if (!directory.exists() && !directory.mkdirs()) {
|
||||||
|
throw new IllegalStateException(
|
||||||
|
"Failed to create config directory: " + directory);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @return the last config that was successfully loaded, or {@code null}.
|
||||||
|
*/
|
||||||
|
private String readCurrentName() {
|
||||||
|
File file = new File(directory, CURRENT_FILE);
|
||||||
|
|
||||||
|
if (!file.isFile()) {
|
||||||
|
return null;
|
||||||
|
}
|
||||||
|
|
||||||
|
try {
|
||||||
|
String name = new String(
|
||||||
|
Files.readAllBytes(file.toPath()), StandardCharsets.UTF_8)
|
||||||
|
.trim();
|
||||||
|
return VALID_NAME.matcher(name).matches() ? name : null;
|
||||||
|
} catch (IOException e) {
|
||||||
|
return null;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
private void writeCurrentName(String name) {
|
||||||
|
try {
|
||||||
|
Files.write(
|
||||||
|
new File(directory, CURRENT_FILE).toPath(),
|
||||||
|
name.getBytes(StandardCharsets.UTF_8)
|
||||||
|
);
|
||||||
|
} catch (IOException e) {
|
||||||
|
System.err.println(
|
||||||
|
"[OrangeX] Failed to remember current config: "
|
||||||
|
+ e.getMessage());
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
private void clearCurrentName() {
|
||||||
|
File file = new File(directory, CURRENT_FILE);
|
||||||
|
if (file.isFile() && !file.delete()) {
|
||||||
|
System.err.println(
|
||||||
|
"[OrangeX] Failed to clear current config marker");
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
private void backupUnreadable(String name) {
|
||||||
|
File file = getFile(name);
|
||||||
|
|
||||||
|
if (!file.isFile()) {
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
File backup = new File(directory, name + BACKUP_SUFFIX);
|
||||||
|
|
||||||
|
try {
|
||||||
|
Files.move(
|
||||||
|
file.toPath(),
|
||||||
|
backup.toPath(),
|
||||||
|
StandardCopyOption.REPLACE_EXISTING
|
||||||
|
);
|
||||||
|
System.err.println(
|
||||||
|
"[OrangeX] Backed up unreadable config to " + backup);
|
||||||
|
} catch (IOException e) {
|
||||||
|
System.err.println(
|
||||||
|
"[OrangeX] Failed to back up config '" + name + "': "
|
||||||
|
+ e.getMessage());
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -0,0 +1,14 @@
|
|||||||
|
package cn.xiaozhou233.orangex.config;
|
||||||
|
|
||||||
|
import cn.xiaozhou233.orangex.modules.ModuleManager;
|
||||||
|
import com.google.gson.JsonObject;
|
||||||
|
|
||||||
|
public interface ConfigSerializer {
|
||||||
|
|
||||||
|
/** On-disk format version shared by the manager and the serializer. */
|
||||||
|
int CURRENT_VERSION = 1;
|
||||||
|
|
||||||
|
JsonObject serialize(ModuleManager moduleManager);
|
||||||
|
|
||||||
|
void deserialize(ModuleManager moduleManager, JsonObject json);
|
||||||
|
}
|
||||||
@@ -0,0 +1,68 @@
|
|||||||
|
package cn.xiaozhou233.orangex.config.impl;
|
||||||
|
|
||||||
|
import cn.xiaozhou233.orangex.config.ConfigSerializer;
|
||||||
|
import cn.xiaozhou233.orangex.modules.Module;
|
||||||
|
import cn.xiaozhou233.orangex.modules.ModuleManager;
|
||||||
|
import com.google.gson.JsonElement;
|
||||||
|
import com.google.gson.JsonObject;
|
||||||
|
|
||||||
|
public class JsonConfigSerializer implements ConfigSerializer {
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public JsonObject serialize(ModuleManager moduleManager) {
|
||||||
|
JsonObject root = new JsonObject();
|
||||||
|
JsonObject modules = new JsonObject();
|
||||||
|
|
||||||
|
root.addProperty("version", CURRENT_VERSION);
|
||||||
|
|
||||||
|
for (Module module : moduleManager.getAllModules()) {
|
||||||
|
JsonObject moduleJson = new JsonObject();
|
||||||
|
|
||||||
|
moduleJson.addProperty("enabled", module.isEnabled());
|
||||||
|
moduleJson.add("values", module.valuesToJson());
|
||||||
|
|
||||||
|
modules.add(module.getConfigKey(), moduleJson);
|
||||||
|
}
|
||||||
|
|
||||||
|
root.add("modules", modules);
|
||||||
|
|
||||||
|
return root;
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public void deserialize(ModuleManager moduleManager, JsonObject json) {
|
||||||
|
if (json == null || !json.has("modules")
|
||||||
|
|| !json.get("modules").isJsonObject()) {
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
JsonObject modules = json.getAsJsonObject("modules");
|
||||||
|
|
||||||
|
for (Module module : moduleManager.getAllModules()) {
|
||||||
|
JsonElement element = modules.get(module.getConfigKey());
|
||||||
|
|
||||||
|
// Older configs keyed modules by display name.
|
||||||
|
if (element == null) {
|
||||||
|
element = modules.get(module.getName());
|
||||||
|
}
|
||||||
|
|
||||||
|
if (element == null || !element.isJsonObject()) {
|
||||||
|
continue;
|
||||||
|
}
|
||||||
|
|
||||||
|
JsonObject moduleJson = element.getAsJsonObject();
|
||||||
|
|
||||||
|
// Restore values before enabling the module.
|
||||||
|
if (moduleJson.has("values")
|
||||||
|
&& moduleJson.get("values").isJsonObject()) {
|
||||||
|
module.valuesFromJson(moduleJson.getAsJsonObject("values"));
|
||||||
|
}
|
||||||
|
|
||||||
|
if (moduleJson.has("enabled")
|
||||||
|
&& moduleJson.get("enabled").isJsonPrimitive()
|
||||||
|
&& moduleJson.get("enabled").getAsJsonPrimitive().isBoolean()) {
|
||||||
|
module.setEnabled(moduleJson.get("enabled").getAsBoolean());
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -1,6 +1,7 @@
|
|||||||
package cn.xiaozhou233.orangex.modules;
|
package cn.xiaozhou233.orangex.modules;
|
||||||
|
|
||||||
import cn.xiaozhou233.orangex.OrangeX;
|
import cn.xiaozhou233.orangex.OrangeX;
|
||||||
|
import cn.xiaozhou233.orangex.config.ConfigManager;
|
||||||
import cn.xiaozhou233.orangex.input.KeyCodes;
|
import cn.xiaozhou233.orangex.input.KeyCodes;
|
||||||
import cn.xiaozhou233.orangex.value.BooleanValue;
|
import cn.xiaozhou233.orangex.value.BooleanValue;
|
||||||
import cn.xiaozhou233.orangex.value.KeybindValue;
|
import cn.xiaozhou233.orangex.value.KeybindValue;
|
||||||
@@ -64,6 +65,14 @@ public abstract class Module {
|
|||||||
return addValue(new ModeValue(name, defaultMode, modes));
|
return addValue(new ModeValue(name, defaultMode, modes));
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Stable key used for persistence. Keeping the class name instead of the
|
||||||
|
* display name means renamed modules keep their stored settings.
|
||||||
|
*/
|
||||||
|
public String getConfigKey() {
|
||||||
|
return getClass().getName();
|
||||||
|
}
|
||||||
|
|
||||||
public Value<?> getValue(String name) {
|
public Value<?> getValue(String name) {
|
||||||
return valueMap.get(name);
|
return valueMap.get(name);
|
||||||
}
|
}
|
||||||
@@ -126,6 +135,7 @@ public abstract class Module {
|
|||||||
if (this.enabled == setEnabled) return;
|
if (this.enabled == setEnabled) return;
|
||||||
|
|
||||||
this.enabled = setEnabled;
|
this.enabled = setEnabled;
|
||||||
|
markConfigDirty();
|
||||||
|
|
||||||
if (this.enabled) {
|
if (this.enabled) {
|
||||||
if (OrangeX.getInstance().getEventBus().isRegistered(this))
|
if (OrangeX.getInstance().getEventBus().isRegistered(this))
|
||||||
@@ -144,4 +154,11 @@ public abstract class Module {
|
|||||||
setEnabled(!this.enabled);
|
setEnabled(!this.enabled);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
private void markConfigDirty() {
|
||||||
|
ConfigManager configManager = OrangeX.getInstance().getConfigManager();
|
||||||
|
if (configManager != null) {
|
||||||
|
configManager.markDirty();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
@@ -1,5 +1,6 @@
|
|||||||
package cn.xiaozhou233.orangex.modules.impl.render;
|
package cn.xiaozhou233.orangex.modules.impl.render;
|
||||||
|
|
||||||
|
import cn.xiaozhou233.orangex.OrangeX;
|
||||||
import cn.xiaozhou233.orangex.event.impl.EventRender2D;
|
import cn.xiaozhou233.orangex.event.impl.EventRender2D;
|
||||||
import cn.xiaozhou233.orangex.event.impl.EventTickPre;
|
import cn.xiaozhou233.orangex.event.impl.EventTickPre;
|
||||||
import cn.xiaozhou233.orangex.input.ClientInput;
|
import cn.xiaozhou233.orangex.input.ClientInput;
|
||||||
@@ -45,6 +46,9 @@ public class ClickGUI extends Module {
|
|||||||
justEnabled = false;
|
justEnabled = false;
|
||||||
ClickGuiRender.reset();
|
ClickGuiRender.reset();
|
||||||
ClientInput.requestClose();
|
ClientInput.requestClose();
|
||||||
|
if (OrangeX.getInstance().getConfigManager() != null) {
|
||||||
|
OrangeX.getInstance().getConfigManager().saveIfDirty();
|
||||||
|
}
|
||||||
super.onDisable();
|
super.onDisable();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -17,6 +17,8 @@ import java.util.function.Supplier;
|
|||||||
*/
|
*/
|
||||||
public abstract class Value<T> {
|
public abstract class Value<T> {
|
||||||
|
|
||||||
|
private static volatile Runnable globalChangeHook;
|
||||||
|
|
||||||
private final String name;
|
private final String name;
|
||||||
private final T defaultValue;
|
private final T defaultValue;
|
||||||
|
|
||||||
@@ -47,11 +49,26 @@ public abstract class Value<T> {
|
|||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
this.value = newValue;
|
this.value = newValue;
|
||||||
if (changeListener != null) {
|
try {
|
||||||
changeListener.accept(newValue);
|
if (changeListener != null) {
|
||||||
|
changeListener.accept(newValue);
|
||||||
|
}
|
||||||
|
} finally {
|
||||||
|
Runnable hook = globalChangeHook;
|
||||||
|
if (hook != null) {
|
||||||
|
hook.run();
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Registers a process-wide callback invoked whenever any value changes.
|
||||||
|
* Used by the config manager to track unsaved changes.
|
||||||
|
*/
|
||||||
|
public static void setGlobalChangeHook(Runnable hook) {
|
||||||
|
globalChangeHook = hook;
|
||||||
|
}
|
||||||
|
|
||||||
public T getDefaultValue() {
|
public T getDefaultValue() {
|
||||||
return defaultValue;
|
return defaultValue;
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in new issue
Block a user