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,189 +11,179 @@ 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;
public class SaturnConfigEditor extends SaturnUi { public class SaturnConfigEditor extends SaturnUi {
public SaturnConfigEditor() { public SaturnConfigEditor() {
super(Text.of("Config editor")); super(Text.of("Config editor"));
} }
@Override @Override
protected void init() { protected void init() {
int rectWidth = 330; int rectWidth = 330;
int rectHeight = 228; int rectHeight = 228;
int rectX = (width - rectWidth) / 2; int rectX = (width - rectWidth) / 2;
int rectY = (height - rectHeight + 15) / 2; int rectY = (height - rectHeight + 15) / 2;
draw( draw(
new SaturnSprite(Textures.SETTINGS_BG) new SaturnSprite(Textures.SETTINGS_BG)
.setX(rectX) .setX(rectX)
.setY(rectY) .setY(rectY)
.setWidth(rectWidth) .setWidth(rectWidth)
.setHeight(rectHeight) .setHeight(rectHeight)
.setAnimations(SaturnClient.getAnimations())); .setAnimations(SaturnClient.getAnimations()));
draw( SaturnScroll configScroll = new SaturnScroll();
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;
int col = 0;
int row = 0; drawProperties(configScroll, SaturnClient.config.getProperties(), rectWidth, row, col);
int col = 0;
drawProperties(configScroll, SaturnClient.config.getProperties(), rectWidth, row, col); draw(
configScroll
.setX(rectX + 17)
.setY(rectY + 10)
.setWidth(rectWidth - 17)
.setHeight(rectHeight - 10)
.setAnimations(SaturnClient.getAnimations()));
draw( super.init();
configScroll }
.setX(rectX + 17)
.setY(rectY + 10)
.setWidth(rectWidth - 17)
.setHeight(rectHeight - 10)
.setAnimations(SaturnClient.getAnimations()));
super.init(); @SuppressWarnings("unchecked")
} private int drawProperties(SaturnScroll configScroll, Map<String, Property<?>> properties, int rectWidth,
int row, int col) {
int totalRows = row;
for (Map.Entry<String, Property<?>> propEntry : properties.entrySet()) {
Property<?> prop = propEntry.getValue();
String propName = propEntry.getKey();
@SuppressWarnings("unchecked") boolean full = isFull(prop);
private int drawProperties(SaturnScroll configScroll, Map<String, Property<?>> properties, int rectWidth,
int row, int col) {
int totalRows = row;
for (Map.Entry<String, Property<?>> propEntry : properties.entrySet()) {
Property<?> prop = propEntry.getValue();
String propName = propEntry.getKey();
boolean full = isFull(prop); if (full && col > 0) {
col = 0;
totalRows++;
}
if (full && col > 0) { int modX = ((rectWidth / 2) - 17) * col;
col = 0; int modY = 14 * totalRows;
totalRows++;
}
int modX = ((rectWidth / 2) - 17) * col; switch (prop.getType()) {
int modY = 14 * totalRows; case BOOLEAN:
configScroll.draw(
new SaturnToggle((Property<Boolean>) prop)
.setX(modX)
.setY(modY));
configScroll.draw(
new SaturnText(
propName.substring(0, 1).toUpperCase() +
propName.substring(1))
.setX(modX + 18)
.setY(modY)
.setScale(0.8f));
break;
case INTEGER:
configScroll.draw(
new SaturnText(
propName.substring(0, 1).toUpperCase() +
propName.substring(1))
.setX(modX)
.setY(modY)
.setScale(0.8f));
switch (prop.getType()) { configScroll.draw(
case BOOLEAN: new SaturnInteger(
configScroll.draw( (Property<Integer>) prop,
new SaturnToggle((Property<Boolean>) prop) (rectWidth / 2) - 17,
.setX(modX) modY,
.setY(modY)); 70));
configScroll.draw( break;
new SaturnText( case FLOAT:
propName.substring(0, 1).toUpperCase() + configScroll.draw(
propName.substring(1)) new SaturnText(
.setX(modX + 18) propName.substring(0, 1).toUpperCase() +
.setY(modY) propName.substring(1))
.setScale(0.8f)); .setX(modX)
break; .setY(modY)
case INTEGER: .setScale(0.8f));
configScroll.draw(
new SaturnText(
propName.substring(0, 1).toUpperCase() +
propName.substring(1))
.setX(modX)
.setY(modY)
.setScale(0.8f));
configScroll.draw( configScroll.draw(
new SaturnInteger( new SaturnFloat(
(Property<Integer>) prop, (Property<Float>) prop,
(rectWidth / 2) - 17, (rectWidth / 2) - 17,
modY, modY,
70)); 70));
break; break;
case FLOAT: case STRING:
configScroll.draw( configScroll.draw(
new SaturnText( new SaturnText(
propName.substring(0, 1).toUpperCase() + propName.substring(0, 1).toUpperCase() +
propName.substring(1)) propName.substring(1))
.setX(modX) .setX(modX)
.setY(modY) .setY(modY)
.setScale(0.8f)); .setScale(0.8f));
configScroll.draw( configScroll.draw(
new SaturnFloat( new SaturnString(
(Property<Float>) prop, (Property<String>) prop,
(rectWidth / 2) - 17, (rectWidth / 2) - 17,
modY, modY,
70)); 70));
break; break;
case STRING: case HEX:
configScroll.draw( configScroll.draw(
new SaturnText( new SaturnText(
propName.substring(0, 1).toUpperCase() + propName.substring(0, 1).toUpperCase() +
propName.substring(1)) propName.substring(1))
.setX(modX) .setX(modX)
.setY(modY) .setY(modY)
.setScale(0.8f)); .setScale(0.8f));
configScroll.draw( configScroll.draw(
new SaturnString( new SaturnHex(
(Property<String>) prop, (Property<Integer>) prop,
(rectWidth / 2) - 17, (rectWidth / 2) - 17,
modY, modY,
70)); 70));
break; break;
case HEX: case NAMESPACE:
configScroll.draw( configScroll.draw(
new SaturnText( new SaturnText(
propName.substring(0, 1).toUpperCase() + propName.substring(0, 1).toUpperCase() +
propName.substring(1)) propName.substring(1))
.setX(modX) .setX(modX)
.setY(modY) .setY(modY)
.setScale(0.8f)); .setScale(0.8f));
configScroll.draw( // Recursively draw nested properties and update total rows
new SaturnHex( Map<String, Property<?>> nestedProperties = (Map<String, Property<?>>) prop.value;
(Property<Integer>) prop, totalRows = drawProperties(configScroll, nestedProperties, rectWidth,
(rectWidth / 2) - 17, totalRows + 1, 0);
modY, }
70));
break;
case NAMESPACE:
configScroll.draw(
new SaturnText(
propName.substring(0, 1).toUpperCase() +
propName.substring(1))
.setX(modX)
.setY(modY)
.setScale(0.8f));
// Recursively draw nested properties and update total rows if (full) {
Map<String, Property<?>> nestedProperties = (Map<String, Property<?>>) prop.value; totalRows++;
totalRows = drawProperties(configScroll, nestedProperties, rectWidth, totalRows + 1, 0); } else {
} col++;
if (col > 1) {
if (full) { col = 0;
totalRows++; totalRows++;
} else { }
col++; }
if (col > 1) {
col = 0;
totalRows++;
} }
} return totalRows;
} }
return totalRows;
}
public static boolean isFull(Property<?> prop) { public static boolean isFull(Property<?> prop) {
switch (prop.getType()) { switch (prop.getType()) {
case BOOLEAN: case BOOLEAN:
return false; return false;
default: default:
return true; return true;
}
} }
}
} }
@@ -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) {
@@ -19,49 +19,47 @@ public class SaturnToggle extends SaturnWidget {
@Override @Override
public void render( public void render(
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);
int w = 13; int w = 13;
int h = 6; int h = 6;
int s = 8; int s = 8;
context.drawTexture( context.drawTexture(
RenderLayer::getGuiTextured, RenderLayer::getGuiTextured,
Textures.TOGGLE_BG, Textures.TOGGLE_BG,
0, 0,
1, 1,
0, 0,
0, 0,
w, w,
h, h,
w, w,
h, h,
color color);
);
context.drawTexture( context.drawTexture(
RenderLayer::getGuiTextured, RenderLayer::getGuiTextured,
Textures.TOGGLE_INDICATOR, Textures.TOGGLE_INDICATOR,
prop.value ? 5 : 0, prop.value ? 5 : 0,
0, 0,
0, 0,
0, 0,
s, s,
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();
} }
} }