namespace load fix

This commit is contained in:
2025-04-03 22:19:31 +02:00
parent dc70abf450
commit 6b67b7bf8e
8 changed files with 214 additions and 207 deletions
@@ -1,5 +1,7 @@
package org.saturnclient.saturnclient.config;
import com.google.gson.Gson;
import com.google.gson.GsonBuilder;
import com.google.gson.JsonElement;
import com.google.gson.JsonObject;
import com.google.gson.JsonParser;
@@ -87,7 +89,7 @@ public class ConfigManager {
if (p.getType() == Property.PropertyType.NAMESPACE) {
// Handle nested namespace
JsonObject nestedConfig = c.getAsJsonObject();
Map<String, Property<?>> nestedProperties = (Map<String, Property<?>>) p.value;
Map<String, Property<?>> nestedProperties = p.getNamespaceValue();
loadProperties(nestedConfig, nestedProperties);
} else {
// Handle regular properties
@@ -136,8 +138,12 @@ public class ConfigManager {
jsonObject.add(namespace, namespaceConfig);
}
// Format the json so it can easily be edited
Gson gson = new GsonBuilder().setPrettyPrinting().create();
String formattedJson = gson.toJson(jsonObject);
// Write the JSON object to the config file
Files.write(configFile.toPath(), jsonObject.toString().getBytes());
Files.write(configFile.toPath(), formattedJson.getBytes());
SaturnClient.LOGGER.info("Config saved successfully.");
} catch (IOException e) {
SaturnClient.LOGGER.error("Error saving the config file", e);
@@ -29,7 +29,7 @@ public class Property<T> {
type = PropertyType.FLOAT;
} else if (value instanceof String) {
type = PropertyType.STRING;
} else if (isMapOfStringToProperty(value)) {
} else if (valueIsNamespace(value)) {
type = PropertyType.NAMESPACE;
}
}
@@ -87,6 +87,14 @@ public class Property<T> {
return type;
}
@SuppressWarnings("unchecked")
public Map<String, Property<?>> getNamespaceValue() {
if (type == PropertyType.NAMESPACE && value instanceof Map) {
return (Map<String, Property<?>>) value;
}
throw new IllegalStateException("Property is not a namespace");
}
public boolean matchesJson(JsonElement element) {
if (element.isJsonPrimitive()) {
JsonPrimitive primitive = element.getAsJsonPrimitive();
@@ -98,14 +106,14 @@ public class Property<T> {
return true;
if (value instanceof String && primitive.isString())
return true;
if (isMapOfStringToProperty(value) && primitive.isJsonObject())
return true;
// Check if JSON string represents a valid hex integer
if (value instanceof String && primitive.isString()) {
String str = primitive.getAsString();
return str.matches("0x[0-9A-Fa-f]+");
}
} else if (element.isJsonObject()) {
return isNamespace(value);
}
return false;
}
@@ -115,19 +123,11 @@ public class Property<T> {
return String.valueOf(value);
}
public static boolean isMapOfStringToProperty(Object obj) {
if (!(obj instanceof Map<?, ?> map)) {
return false;
public boolean isNamespace(Object obj) {
return type == PropertyType.NAMESPACE && value instanceof Map;
}
for (Map.Entry<?, ?> entry : map.entrySet()) {
if (!(entry.getKey() instanceof String)) {
return false;
}
if (!(entry.getValue() instanceof Property<?>)) {
return false;
}
}
return true;
public static boolean valueIsNamespace(Object obj) {
return obj instanceof Map;
}
}
@@ -176,7 +176,8 @@ public class ConfigEditor extends SaturnUi {
// Recursively draw nested properties and update total rows
Map<String, Property<?>> nestedProperties = (Map<String, Property<?>>) prop.value;
totalRows = drawProperties(configScroll, nestedProperties, rectWidth, totalRows + 1, 0);
totalRows = drawProperties(configScroll, nestedProperties, rectWidth,
totalRows + 1, 0);
}
if (full) {
@@ -200,4 +201,10 @@ public class ConfigEditor extends SaturnUi {
return true;
}
}
@Override
public void close() {
ConfigManager.save();
super.close();
}
}
@@ -11,7 +11,6 @@ import org.saturnclient.ui.components.SaturnHex;
import org.saturnclient.ui.components.SaturnInteger;
import org.saturnclient.ui.components.SaturnString;
import org.saturnclient.ui.components.SaturnToggle;
import org.saturnclient.ui.widgets.SaturnClickableImage;
import org.saturnclient.ui.widgets.SaturnScroll;
import org.saturnclient.ui.widgets.SaturnSprite;
import org.saturnclient.ui.widgets.SaturnText;
@@ -37,16 +36,6 @@ public class SaturnConfigEditor extends SaturnUi {
.setHeight(rectHeight)
.setAnimations(SaturnClient.getAnimations()));
draw(
new SaturnClickableImage(Textures.CLOSE, () -> {
this.close();
})
.setX(rectX)
.setY(rectY - 15)
.setWidth(13)
.setHeight(13)
.setAnimations(SaturnClient.getAnimations()));
SaturnScroll configScroll = new SaturnScroll();
int row = 0;
@@ -172,7 +161,8 @@ public class SaturnConfigEditor extends SaturnUi {
// Recursively draw nested properties and update total rows
Map<String, Property<?>> nestedProperties = (Map<String, Property<?>>) prop.value;
totalRows = drawProperties(configScroll, nestedProperties, rectWidth, totalRows + 1, 0);
totalRows = drawProperties(configScroll, nestedProperties, rectWidth,
totalRows + 1, 0);
}
if (full) {
@@ -59,6 +59,7 @@ public class SaturnFloat extends SaturnWidget {
text.substring(cursorPosition);
prop.value = text.isEmpty() ? 0 : Float.parseFloat(text);
org.saturnclient.saturnclient.config.ConfigManager.save();
cursorPosition++;
}
@@ -77,6 +78,7 @@ public class SaturnFloat extends SaturnWidget {
cursorPosition = 1;
}
prop.value = Float.parseFloat(text);
org.saturnclient.saturnclient.config.ConfigManager.save();
}
} else if (keyCode == GLFW.GLFW_KEY_LEFT) {
if (cursorPosition > 0) {
@@ -42,6 +42,7 @@ public class SaturnHex extends SaturnWidget {
if (text.length() == 7) { // Only update value when full hex is entered
prop.value = hexToInt(text);
org.saturnclient.saturnclient.config.ConfigManager.save();
}
}
}
@@ -65,6 +66,7 @@ public class SaturnHex extends SaturnWidget {
if (text.length() == 7) { // Update only when valid hex
prop.value = hexToInt(text);
org.saturnclient.saturnclient.config.ConfigManager.save();
}
}
@@ -59,6 +59,7 @@ public class SaturnInteger extends SaturnWidget {
text.substring(cursorPosition);
prop.value = text.isEmpty() ? 0 : Integer.parseInt(text);
org.saturnclient.saturnclient.config.ConfigManager.save();
cursorPosition++;
}
@@ -77,6 +78,7 @@ public class SaturnInteger extends SaturnWidget {
cursorPosition = 1;
}
prop.value = Integer.parseInt(text);
org.saturnclient.saturnclient.config.ConfigManager.save();
}
} else if (keyCode == GLFW.GLFW_KEY_LEFT) {
if (cursorPosition > 0) {
@@ -22,8 +22,7 @@ public class SaturnToggle extends SaturnWidget {
DrawContext context,
boolean hovering,
int mouseX,
int mouseY
) {
int mouseY) {
int color = prop.value
? SaturnClient.COLOR.value
: SaturnClient.getWhite(alpha);
@@ -42,8 +41,7 @@ public class SaturnToggle extends SaturnWidget {
h,
w,
h,
color
);
color);
context.drawTexture(
RenderLayer::getGuiTextured,
@@ -56,12 +54,12 @@ public class SaturnToggle extends SaturnWidget {
s,
s,
s,
color
);
color);
}
@Override
public void click(int mouseX, int mouseY) {
prop.value = !prop.value;
org.saturnclient.saturnclient.config.ConfigManager.save();
}
}