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; package org.saturnclient.saturnclient.config;
import com.google.gson.Gson;
import com.google.gson.GsonBuilder;
import com.google.gson.JsonElement; import com.google.gson.JsonElement;
import com.google.gson.JsonObject; import com.google.gson.JsonObject;
import com.google.gson.JsonParser; import com.google.gson.JsonParser;
@@ -87,7 +89,7 @@ public class ConfigManager {
if (p.getType() == Property.PropertyType.NAMESPACE) { if (p.getType() == Property.PropertyType.NAMESPACE) {
// Handle nested namespace // Handle nested namespace
JsonObject nestedConfig = c.getAsJsonObject(); JsonObject nestedConfig = c.getAsJsonObject();
Map<String, Property<?>> nestedProperties = (Map<String, Property<?>>) p.value; Map<String, Property<?>> nestedProperties = p.getNamespaceValue();
loadProperties(nestedConfig, nestedProperties); loadProperties(nestedConfig, nestedProperties);
} else { } else {
// Handle regular properties // Handle regular properties
@@ -136,8 +138,12 @@ public class ConfigManager {
jsonObject.add(namespace, namespaceConfig); 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 // 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."); SaturnClient.LOGGER.info("Config saved successfully.");
} catch (IOException e) { } catch (IOException e) {
SaturnClient.LOGGER.error("Error saving the config file", e); SaturnClient.LOGGER.error("Error saving the config file", e);
@@ -29,7 +29,7 @@ public class Property<T> {
type = PropertyType.FLOAT; type = PropertyType.FLOAT;
} else if (value instanceof String) { } else if (value instanceof String) {
type = PropertyType.STRING; type = PropertyType.STRING;
} else if (isMapOfStringToProperty(value)) { } else if (valueIsNamespace(value)) {
type = PropertyType.NAMESPACE; type = PropertyType.NAMESPACE;
} }
} }
@@ -87,6 +87,14 @@ public class Property<T> {
return type; 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) { public boolean matchesJson(JsonElement element) {
if (element.isJsonPrimitive()) { if (element.isJsonPrimitive()) {
JsonPrimitive primitive = element.getAsJsonPrimitive(); JsonPrimitive primitive = element.getAsJsonPrimitive();
@@ -98,14 +106,14 @@ public class Property<T> {
return true; return true;
if (value instanceof String && primitive.isString()) if (value instanceof String && primitive.isString())
return true; return true;
if (isMapOfStringToProperty(value) && primitive.isJsonObject())
return true;
// Check if JSON string represents a valid hex integer // Check if JSON string represents a valid hex integer
if (value instanceof String && primitive.isString()) { if (value instanceof String && primitive.isString()) {
String str = primitive.getAsString(); String str = primitive.getAsString();
return str.matches("0x[0-9A-Fa-f]+"); return str.matches("0x[0-9A-Fa-f]+");
} }
} else if (element.isJsonObject()) {
return isNamespace(value);
} }
return false; return false;
} }
@@ -115,19 +123,11 @@ public class Property<T> {
return String.valueOf(value); return String.valueOf(value);
} }
public static boolean isMapOfStringToProperty(Object obj) { public boolean isNamespace(Object obj) {
if (!(obj instanceof Map<?, ?> map)) { return type == PropertyType.NAMESPACE && value instanceof Map;
return false;
} }
for (Map.Entry<?, ?> entry : map.entrySet()) { public static boolean valueIsNamespace(Object obj) {
if (!(entry.getKey() instanceof String)) { return obj instanceof Map;
return false;
}
if (!(entry.getValue() instanceof Property<?>)) {
return false;
}
}
return true;
} }
} }
@@ -176,7 +176,8 @@ public class ConfigEditor extends SaturnUi {
// Recursively draw nested properties and update total rows // Recursively draw nested properties and update total rows
Map<String, Property<?>> nestedProperties = (Map<String, Property<?>>) prop.value; 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) { if (full) {
@@ -200,4 +201,10 @@ public class ConfigEditor extends SaturnUi {
return true; 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.SaturnInteger;
import org.saturnclient.ui.components.SaturnString; import org.saturnclient.ui.components.SaturnString;
import org.saturnclient.ui.components.SaturnToggle; import org.saturnclient.ui.components.SaturnToggle;
import org.saturnclient.ui.widgets.SaturnClickableImage;
import org.saturnclient.ui.widgets.SaturnScroll; import org.saturnclient.ui.widgets.SaturnScroll;
import org.saturnclient.ui.widgets.SaturnSprite; import org.saturnclient.ui.widgets.SaturnSprite;
import org.saturnclient.ui.widgets.SaturnText; import org.saturnclient.ui.widgets.SaturnText;
@@ -37,16 +36,6 @@ public class SaturnConfigEditor extends SaturnUi {
.setHeight(rectHeight) .setHeight(rectHeight)
.setAnimations(SaturnClient.getAnimations())); .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(); SaturnScroll configScroll = new SaturnScroll();
int row = 0; int row = 0;
@@ -172,7 +161,8 @@ public class SaturnConfigEditor extends SaturnUi {
// Recursively draw nested properties and update total rows // Recursively draw nested properties and update total rows
Map<String, Property<?>> nestedProperties = (Map<String, Property<?>>) prop.value; 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) { if (full) {
@@ -59,6 +59,7 @@ public class SaturnFloat extends SaturnWidget {
text.substring(cursorPosition); text.substring(cursorPosition);
prop.value = text.isEmpty() ? 0 : Float.parseFloat(text); prop.value = text.isEmpty() ? 0 : Float.parseFloat(text);
org.saturnclient.saturnclient.config.ConfigManager.save();
cursorPosition++; cursorPosition++;
} }
@@ -77,6 +78,7 @@ public class SaturnFloat extends SaturnWidget {
cursorPosition = 1; cursorPosition = 1;
} }
prop.value = Float.parseFloat(text); prop.value = Float.parseFloat(text);
org.saturnclient.saturnclient.config.ConfigManager.save();
} }
} else if (keyCode == GLFW.GLFW_KEY_LEFT) { } else if (keyCode == GLFW.GLFW_KEY_LEFT) {
if (cursorPosition > 0) { if (cursorPosition > 0) {
@@ -42,6 +42,7 @@ public class SaturnHex extends SaturnWidget {
if (text.length() == 7) { // Only update value when full hex is entered if (text.length() == 7) { // Only update value when full hex is entered
prop.value = hexToInt(text); 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 if (text.length() == 7) { // Update only when valid hex
prop.value = hexToInt(text); prop.value = hexToInt(text);
org.saturnclient.saturnclient.config.ConfigManager.save();
} }
} }
@@ -59,6 +59,7 @@ public class SaturnInteger extends SaturnWidget {
text.substring(cursorPosition); text.substring(cursorPosition);
prop.value = text.isEmpty() ? 0 : Integer.parseInt(text); prop.value = text.isEmpty() ? 0 : Integer.parseInt(text);
org.saturnclient.saturnclient.config.ConfigManager.save();
cursorPosition++; cursorPosition++;
} }
@@ -77,6 +78,7 @@ public class SaturnInteger extends SaturnWidget {
cursorPosition = 1; cursorPosition = 1;
} }
prop.value = Integer.parseInt(text); prop.value = Integer.parseInt(text);
org.saturnclient.saturnclient.config.ConfigManager.save();
} }
} else if (keyCode == GLFW.GLFW_KEY_LEFT) { } else if (keyCode == GLFW.GLFW_KEY_LEFT) {
if (cursorPosition > 0) { if (cursorPosition > 0) {
@@ -22,8 +22,7 @@ public class SaturnToggle extends SaturnWidget {
DrawContext context, DrawContext context,
boolean hovering, boolean hovering,
int mouseX, int mouseX,
int mouseY int mouseY) {
) {
int color = prop.value int color = prop.value
? SaturnClient.COLOR.value ? SaturnClient.COLOR.value
: SaturnClient.getWhite(alpha); : SaturnClient.getWhite(alpha);
@@ -42,8 +41,7 @@ public class SaturnToggle extends SaturnWidget {
h, h,
w, w,
h, h,
color color);
);
context.drawTexture( context.drawTexture(
RenderLayer::getGuiTextured, RenderLayer::getGuiTextured,
@@ -56,12 +54,12 @@ public class SaturnToggle extends SaturnWidget {
s, s,
s, s,
s, s,
color color);
);
} }
@Override @Override
public void click(int mouseX, int mouseY) { public void click(int mouseX, int mouseY) {
prop.value = !prop.value; prop.value = !prop.value;
org.saturnclient.saturnclient.config.ConfigManager.save();
} }
} }