This may have been a bad idea

This commit is contained in:
Anuken
2026-07-30 22:30:45 -04:00
parent 02785f49e8
commit cb6c5b1c5d
15 changed files with 76 additions and 80 deletions

View File

@@ -44,7 +44,7 @@ public class NetClient implements ApplicationListener{
planSyncTime = Timekeeper.ofSeconds(0.5f),
pingTime = Timekeeper.ofSeconds(1f);
private static final Reads dataReads = new Reads(null);
private static final JsonValue tmpJsonMap = new JsonValue(ValueType.object);
private static final Jval tmpJsonMap = Jval.newObject();
private long ping;
//private Interval timer = new Interval(5);

View File

@@ -147,7 +147,7 @@ public class MapPatchesView implements AssetView{
}
}
int countFields(JsonValue value){
int countFields(Jval value){
if(value.isObject() || value.isArray()){
int sum = 0;
for(var child : value){

View File

@@ -806,7 +806,7 @@ public class MapObjectives implements Iterable<MapObjective>, Eachable<MapObject
}
@Override
public void read(Json json, JsonValue jsonData){
public void read(Json json, Jval jsonData){
json.readFields(this, jsonData);
if(jsonData.has("textureName")) setTexture(jsonData.getString("textureName"));
}
@@ -1442,7 +1442,7 @@ public class MapObjectives implements Iterable<MapObjective>, Eachable<MapObject
}
@Override
public void read(Json json, JsonValue jsonData){
public void read(Json json, Jval jsonData){
if(jsonData.has("string")){
value = jsonData.get("string").asString();
}else if(jsonData.has("content")){

View File

@@ -426,8 +426,8 @@ public class Rules{
}
@Override
public void read(Json json, JsonValue jsonData){
for(JsonValue value : jsonData){
public void read(Json json, Jval jsonData){
for(Jval value : jsonData){
values[Integer.parseInt(value.name)] = json.readValue(TeamRule.class, value);
}
}

View File

@@ -128,7 +128,7 @@ public class SpawnGroup implements JsonSerializable, Cloneable{
}
@Override
public void read(Json json, JsonValue data){
public void read(Json json, Jval data){
String tname = data.getString("type", "dagger");
type = content.unit(LegacyIO.unitMap.get(tname, tname));

View File

@@ -100,7 +100,7 @@ public class JsonIO{
}
@Override
public MusicContainer read(Json json, JsonValue jsonData, Class type){
public MusicContainer read(Json json, Jval jsonData, Class type){
return new MusicContainer(jsonData.isString() ? jsonData.asString() : "");
}
});
@@ -112,7 +112,7 @@ public class JsonIO{
}
@Override
public Color read(Json json, JsonValue jsonData, Class type){
public Color read(Json json, Jval jsonData, Class type){
if(jsonData.isString()){
return Color.valueOf(jsonData.asString());
}
@@ -129,7 +129,7 @@ public class JsonIO{
}
@Override
public Sector read(Json json, JsonValue jsonData, Class type){
public Sector read(Json json, Jval jsonData, Class type){
String name = jsonData.asString();
int idx = name.lastIndexOf('-');
return Vars.content.<Planet>getByName(ContentType.planet, name.substring(0, idx)).sectors.get(Integer.parseInt(name.substring(idx + 1)));
@@ -143,7 +143,7 @@ public class JsonIO{
}
@Override
public SectorPreset read(Json json, JsonValue jsonData, Class type){
public SectorPreset read(Json json, Jval jsonData, Class type){
return Vars.content.getByName(ContentType.sector, jsonData.asString());
}
});
@@ -155,7 +155,7 @@ public class JsonIO{
}
@Override
public Liquid read(Json json, JsonValue jsonData, Class type){
public Liquid read(Json json, Jval jsonData, Class type){
if(jsonData.asString() == null) return Liquids.water;
Liquid i = Vars.content.getByName(ContentType.liquid, jsonData.asString());
return i == null ? Liquids.water : i;
@@ -169,7 +169,7 @@ public class JsonIO{
}
@Override
public Attribute read(Json json, JsonValue jsonData, Class type){
public Attribute read(Json json, Jval jsonData, Class type){
return Attribute.get(jsonData.asString());
}
});
@@ -181,7 +181,7 @@ public class JsonIO{
}
@Override
public Item read(Json json, JsonValue jsonData, Class type){
public Item read(Json json, Jval jsonData, Class type){
if(jsonData.asString() == null) return Items.copper;
Item i = Vars.content.getByName(ContentType.item, jsonData.asString());
return i == null ? Items.copper : i;
@@ -195,7 +195,7 @@ public class JsonIO{
}
@Override
public Team read(Json json, JsonValue jsonData, Class type){
public Team read(Json json, Jval jsonData, Class type){
return Team.get(jsonData.asInt());
}
});
@@ -207,7 +207,7 @@ public class JsonIO{
}
@Override
public Block read(Json json, JsonValue jsonData, Class type){
public Block read(Json json, Jval jsonData, Class type){
Block block = Vars.content.getByName(ContentType.block, jsonData.asString());
if(block == null) block = Vars.content.getByName(ContentType.block, SaveVersion.fallback.get(jsonData.asString(), ""));
return block == null ? Blocks.air : block;
@@ -221,7 +221,7 @@ public class JsonIO{
}
@Override
public Planet read(Json json, JsonValue jsonData, Class type){
public Planet read(Json json, Jval jsonData, Class type){
if(jsonData.asString() == null){
return null;
}
@@ -237,7 +237,7 @@ public class JsonIO{
}
@Override
public Weather read(Json json, JsonValue jsonData, Class type){
public Weather read(Json json, Jval jsonData, Class type){
return Vars.content.getByName(ContentType.weather, jsonData.asString());
}
});
@@ -249,7 +249,7 @@ public class JsonIO{
}
@Override
public UnitType read(Json json, JsonValue jsonData, Class type){
public UnitType read(Json json, Jval jsonData, Class type){
if(jsonData.asString() == null) return UnitTypes.dagger;
UnitType u = Vars.content.getByName(ContentType.unit, jsonData.asString());
return u == null ? UnitTypes.dagger : u;
@@ -266,7 +266,7 @@ public class JsonIO{
}
@Override
public ItemStack read(Json json, JsonValue jsonData, Class type){
public ItemStack read(Json json, Jval jsonData, Class type){
return new ItemStack(json.getSerializer(Item.class).read(json, jsonData.get("item"), Item.class), jsonData.getInt("amount"));
}
});
@@ -278,7 +278,7 @@ public class JsonIO{
}
@Override
public UnlockableContent read(Json json, JsonValue jsonData, Class type){
public UnlockableContent read(Json json, Jval jsonData, Class type){
if(jsonData.isNull()) return null;
String str = jsonData.asString();
var map = Vars.content.byName(str);
@@ -309,7 +309,7 @@ public class JsonIO{
}
@Override
public MapObjectives read(Json json, JsonValue data, Class type){
public MapObjectives read(Json json, Jval data, Class type){
var exec = new MapObjectives();
// First iteration to instantiate the objectives.
for(var value = data.child; value != null; value = value.next){

View File

@@ -64,7 +64,7 @@ public class LogicScript implements JsonSerializable{
}
@Override
public void read(Json json, JsonValue jsonData){
public void read(Json json, Jval jsonData){
if(jsonData.isObject()){
timeout = Math.min(maxTimeoutMs, jsonData.getInt("timeout", 0));
resetVars = jsonData.getBoolean("resetVars", false);

View File

@@ -236,13 +236,13 @@ public class ContentParser{
PartProgress base = (PartProgress)field(PartProgress.class, data.getString("type"));
JsonValue opval =
Jval opval =
data.has("operation") ? data.get("operation") :
data.has("op") ? data.get("op") : null;
//no singular operation, check for multi-operation
if(opval == null){
JsonValue opsVal =
Jval opsVal =
data.has("operations") ? data.get("operations") :
data.has("ops") ? data.get("ops") : null;
@@ -250,9 +250,9 @@ public class ContentParser{
if(!opsVal.isArray()) throw new RuntimeException("Chained PartProgress operations must be an array.");
int i = 0;
while(true){
JsonValue val = opsVal.get(i);
Jval val = opsVal.get(i);
if(val == null) break;
JsonValue op = val.has("operation") ? val.get("operation") :
Jval op = val.has("operation") ? val.get("operation") :
val.has("op") ? val.get("op") : null;
base = parseProgressOp(base, op.asString(), val);
@@ -427,7 +427,7 @@ public class ContentParser{
}
@Override
public <T> T readValue(Class<T> type, Class elementType, JsonValue jsonData, Class keyType){
public <T> T readValue(Class<T> type, Class elementType, Jval jsonData, Class keyType){
T t = internalRead(type, elementType, jsonData, keyType);
if(t != null && !Reflect.isWrapper(t.getClass()) && (type == null || !type.isPrimitive())){
checkNullFields(t);
@@ -438,7 +438,7 @@ public class ContentParser{
return t;
}
private <T> T internalRead(Class<T> type, Class elementType, JsonValue jsonData, Class keyType){
private <T> T internalRead(Class<T> type, Class elementType, Jval jsonData, Class keyType){
if(type != null){
if(classParsers.containsKey(type)){
try{
@@ -521,8 +521,8 @@ public class ContentParser{
}
};
public void readBlockConsumers(Block block, JsonValue value){
for(JsonValue child : value){
public void readBlockConsumers(Block block, Jval value){
for(Jval child : value){
switch(child.name){
case "remove" -> {
String[] values = child.isString() ? new String[]{child.asString()} : child.asStringArray();
@@ -643,7 +643,7 @@ public class ContentParser{
unit.beforeParse();
//add reconstructor type
if(value.has("requirements")){
JsonValue rec = value.remove("requirements");
Jval rec = value.remove("requirements");
UnitReq req = parser.readValue(UnitReq.class, rec);
@@ -671,7 +671,7 @@ public class ContentParser{
//read extra default waves
if(value.has("waves")){
JsonValue waves = value.remove("waves");
Jval waves = value.remove("waves");
SpawnGroup[] groups = parser.readValue(SpawnGroup[].class, waves);
for(SpawnGroup group : groups){
group.type = unit;
@@ -785,7 +785,7 @@ public class ContentParser{
value.remove("planet");
if(value.has("rules")){
JsonValue r = value.remove("rules");
Jval r = value.remove("rules");
if(!r.isObject()) throw new RuntimeException("Rules must be an object!");
preset.rules = rules -> {
try{
@@ -842,7 +842,7 @@ public class ContentParser{
}
if(value.has("rules")){
JsonValue r = value.remove("rules");
Jval r = value.remove("rules");
if(!r.isObject()) throw new RuntimeException("Rules must be an object!");
planet.ruleSetter = rules -> {
try{
@@ -884,7 +884,7 @@ public class ContentParser{
}
);
Prov<Unit> unitType(JsonValue value){
Prov<Unit> unitType(Jval value){
if(value == null) return UnitEntity::create;
return switch(value.asString()){
case "flying" -> UnitEntity::create;
@@ -901,7 +901,7 @@ public class ContentParser{
};
}
private String getString(JsonValue value, String key){
private String getString(Jval value, String key){
if(value.has(key)){
return value.getString(key);
}else{
@@ -909,7 +909,7 @@ public class ContentParser{
}
}
private String getType(JsonValue value){
private String getType(Jval value){
return getString(value, "type");
}
@@ -936,7 +936,7 @@ public class ContentParser{
};
}
private void readBundle(ContentType type, String name, JsonValue value){
private void readBundle(ContentType type, String name, Jval value){
UnlockableContent cont = allowPatching && locate(type, name) instanceof UnlockableContent ? locate(type, name) : null;
String entryName = cont == null ? type + "." + currentMod.name + "-" + name + "." : type + "." + cont.name + ".";
@@ -1033,7 +1033,7 @@ public class ContentParser{
currentMod = mod;
var rawValue = parser.fromJson(null, Jval.read(json).toString(Jformat.plain));
if(!(rawValue instanceof JsonValue value)) throw new SerializationException("Content JSON must be an object, not a single value.");
if(!(rawValue instanceof Jval value)) throw new SerializationException("Content JSON must be an object, not a single value.");
if(!parsers.containsKey(type)){
throw new SerializationException("No parsers for content type '" + type + "'");
@@ -1112,7 +1112,7 @@ public class ContentParser{
return null;
}
private GenericMesh[] parseMeshes(Planet planet, JsonValue array){
private GenericMesh[] parseMeshes(Planet planet, Jval array){
var res = new GenericMesh[array.size];
for(int i = 0; i < array.size; i++){
//yes get is O(n) but it's practically irrelevant here
@@ -1121,7 +1121,7 @@ public class ContentParser{
return res;
}
private GenericMesh parseMesh(Planet planet, JsonValue data){
private GenericMesh parseMesh(Planet planet, Jval data){
if(data.isArray()){
return new MultiMesh(parseMeshes(planet, data));
}
@@ -1158,7 +1158,7 @@ public class ContentParser{
};
}
private PartProgress parseProgressOp(PartProgress base, String op, JsonValue data){
private PartProgress parseProgressOp(PartProgress base, String op, Jval data){
//I have to hard-code this, no easy way of getting parameter names, unfortunately
return switch(op){
case "inv" -> base.inv();
@@ -1222,7 +1222,7 @@ public class ContentParser{
return controller::get;
}
Object field(Class<?> type, JsonValue value){
Object field(Class<?> type, Jval value){
return field(type, value.asString());
}
@@ -1236,7 +1236,7 @@ public class ContentParser{
throw new RuntimeException(e);
}
}
Object fieldOpt(Class<?> type, JsonValue value){
Object fieldOpt(Class<?> type, Jval value){
try{
return type.getField(value.asString()).get(null);
}catch(Exception e){
@@ -1262,19 +1262,19 @@ public class ContentParser{
});
}
private void readFields(Object object, JsonValue jsonMap, boolean stripType){
private void readFields(Object object, Jval jsonMap, boolean stripType){
if(stripType) jsonMap.remove("type");
readFields(object, jsonMap);
}
void readFields(Object object, JsonValue jsonMap){
void readFields(Object object, Jval jsonMap){
if(!jsonMap.isObject()) throw new SerializationException("Expecting an object, but found: '" + jsonMap + "'");
JsonValue research = jsonMap.remove("research");
Jval research = jsonMap.remove("research");
toBeParsed.remove(object);
var type = object.getClass();
var fields = parser.getFields(type);
for(JsonValue child = jsonMap.child; child != null; child = child.next){
for(Jval child = jsonMap.child; child != null; child = child.next){
FieldMetadata metadata = fields.get(child.name().replace(" ", "_"));
if(metadata == null){
if(ignoreUnknownFields){
@@ -1475,11 +1475,11 @@ public class ContentParser{
}
private interface FieldParser{
Object parse(Class<?> type, JsonValue value) throws Exception;
Object parse(Class<?> type, Jval value) throws Exception;
}
private interface TypeParser<T extends Content>{
T parse(String mod, String name, JsonValue value) throws Exception;
T parse(String mod, String name, Jval value) throws Exception;
}
//intermediate class for parsing
@@ -1492,7 +1492,7 @@ public class ContentParser{
}
public interface ParseListener{
void parsed(Class<?> type, JsonValue jsonData, Object result);
void parsed(Class<?> type, Jval jsonData, Object result);
}
}

View File

@@ -227,10 +227,10 @@ public class DataPatcher{
try{
Object someValue = parser.getJson().fromJson(null, Jval.read(set.patch).toString(Jformat.plain));
if(!(someValue instanceof JsonValue value)) throw new SerializationException("Patch must be a JSON object.");
if(!(someValue instanceof Jval value)) throw new SerializationException("Patch must be a JSON object.");
if(Vars.state.rules.planet != null && value.has("requiredPlanets")){
JsonValue req = value.get("requiredPlanets");
Jval req = value.get("requiredPlanets");
value.remove("requiredPlanets");
//this should be ignored unless this instance is a dedicated server
@@ -412,7 +412,7 @@ public class DataPatcher{
visit(object);
if(object == root){
if(value instanceof JsonValue jval && jval.isObject()){
if(value instanceof Jval jval && jval.isObject()){
for(var child : jval){
assign(root, field + "." + child.name, child, null, null, null);
}
@@ -425,7 +425,7 @@ public class DataPatcher{
var meta = new FieldData(metadata.type.isArray() ? metadata.type.getComponentType() : metadata.elementType, null, null);
boolean multiAdd;
if(value instanceof JsonValue jval && jval.isArray()){
if(value instanceof Jval jval && jval.isArray()){
meta = metadata;
multiAdd = true;
}else{
@@ -508,7 +508,7 @@ public class DataPatcher{
var meta = new FieldData(metadata.elementType, null, null);
boolean multiAdd;
if(value instanceof JsonValue jval && jval.isArray()){
if(value instanceof Jval jval && jval.isArray()){
meta = metadata;
multiAdd = true;
}else{
@@ -536,7 +536,7 @@ public class DataPatcher{
var copy = map.copy();
reset(() -> map.set(copy));
if(value instanceof JsonValue jval && jval.isString() && (jval.asString().equals("-"))){
if(value instanceof Jval jval && jval.isString() && (jval.asString().equals("-"))){
//removal syntax:
//"value": "-"
map.remove(key);
@@ -557,14 +557,14 @@ public class DataPatcher{
var copy = map.copy();
reset(() -> map.set(copy));
if(value instanceof JsonValue jval && jval.isString() && (jval.asString().equals("-"))){
if(value instanceof Jval jval && jval.isString() && (jval.asString().equals("-"))){
//removal syntax:
//"value": "-"
map.remove(key, 0f);
}else{
assignValue(object, field, new FieldData(float.class, null, null), () -> map.get(key, 0f), val -> map.put(key, (Float)val), value, false);
}
}else if(object instanceof Attributes map && value instanceof JsonValue jval){
}else if(object instanceof Attributes map && value instanceof Jval jval){
Attribute key = Attribute.getOrNull(field);
if(key == null){
warn("Unknown attribute: '@'", field);
@@ -585,10 +585,10 @@ public class DataPatcher{
var fdata = fields.get(field);
var fobj = object;
if(value instanceof JsonValue jsv && object instanceof UnitType && field.equals("controller")){
if(value instanceof Jval jsv && object instanceof UnitType && field.equals("controller")){
var fmeta = fields.get("controller");
assignValue(object, "controller", new FieldData(fmeta), () -> Reflect.get(fobj, fmeta.field), val -> Reflect.set(fobj, fmeta.field, val), (Func<Unit, UnitController>)(u -> parser.resolveController(jsv.asString()).get()), true);
}else if(value instanceof JsonValue jsv && object instanceof UnitType && field.equals("aiController")){
}else if(value instanceof Jval jsv && object instanceof UnitType && field.equals("aiController")){
var fmeta = fields.get("aiController");
assignValue(object, "aiController", new FieldData(fmeta), () -> Reflect.get(fobj, fmeta.field), val -> Reflect.set(fobj, fmeta.field, val), parser.resolveController(jsv.asString()), true);
}else if(fdata != null){
@@ -601,7 +601,7 @@ public class DataPatcher{
}
Reflect.set(fobj, fdata.field, fv);
}, value, true);
}else if(value instanceof JsonValue jsv && object instanceof Block bl && jsv.isObject() && field.equals("consumes")){
}else if(value instanceof Jval jsv && object instanceof Block bl && jsv.isObject() && field.equals("consumes")){
Seq<Consume> prevBuilder = Reflect.<Seq<Consume>>get(Block.class, bl, "consumeBuilder").copy();
boolean hadItems = bl.hasItems, hadLiquids = bl.hasLiquids, hadPower = bl.hasPower, acceptedItems = bl.acceptsItems;
Runnable resetCons = () -> {
@@ -624,7 +624,7 @@ public class DataPatcher{
Log.err(e);
warn("Failed to read consumers for '@': @", bl, Strings.getSimpleMessage(e));
}
}else if(value instanceof JsonValue jsv && object instanceof UnitType && field.equals("type")){
}else if(value instanceof Jval jsv && object instanceof UnitType && field.equals("type")){
var fmeta = fields.get("constructor");
assignValue(object, "constructor", new FieldData(fmeta), () -> Reflect.get(fobj, fmeta.field), val -> Reflect.set(fobj, fmeta.field, val), parser.unitType(jsv), true);
}else{
@@ -640,7 +640,7 @@ public class DataPatcher{
Object prevValue = getter.get();
try{
if(value instanceof JsonValue jsv){ //setting values from object
if(value instanceof Jval jsv){ //setting values from object
if(prevValue == null || !jsv.isObject() || (jsv.has("type") && metadata.type != MappableContent.class) || (metadata != null && metadata.type == Attributes.class)){
if(UnlockableContent.class.isAssignableFrom(metadata.type) && jsv.isObject()){
warn("New content must not be instantiated: @", jsv);

View File

@@ -4,19 +4,18 @@ import arc.files.*;
import arc.struct.*;
import arc.util.*;
import arc.util.serialization.*;
import arc.util.serialization.JsonWriter.*;
import arc.util.serialization.Jval.*;
import java.io.*;
import java.util.*;
public class PatchAsset extends DataAsset{
private static final JsonValue emptyValue = new JsonValue("error");
private static final Jval emptyValue = Jval.valueOf("error");
/** Raw string value, containing original formatting. */
public String patch = "";
/** Parsed JSON value. Can be an empty error value if parsing failed. */
public JsonValue json = emptyValue;
public Jval json = emptyValue;
/** True if an error was encountered. */
public boolean error;
/** Warnings encountered during patching. */

View File

@@ -150,7 +150,7 @@ public class ItemSeq implements Iterable<ItemStack>, JsonSerializable{
}
@Override
public void read(Json json, JsonValue jsonData){
public void read(Json json, Jval jsonData){
total = 0;
for(Item item : Vars.content.items()){
values[item.id] = jsonData.getInt(item.name, 0);

View File

@@ -19,11 +19,11 @@ public class MapLocales extends ObjectMap<String, StringMap> implements JsonSeri
}
@Override
public void read(Json json, JsonValue jsonData){
for(JsonValue value : jsonData){
public void read(Json json, Jval jsonData){
for(Jval value : jsonData){
StringMap map = new StringMap();
for(JsonValue child = value.child; child != null; child = child.next){
for(Jval child = value.child; child != null; child = child.next){
map.put(child.name, json.readValue(String.class, null, child));
}

View File

@@ -50,7 +50,7 @@ public class Attributes implements JsonSerializable{
}
@Override
public void read(Json json, JsonValue data){
public void read(Json json, Jval data){
check();
for(Attribute at : Attribute.all){
arr[at.id] = data.getFloat(at.name, 0);

View File

@@ -9,8 +9,6 @@ import arc.util.Timer;
import arc.util.CommandHandler.*;
import arc.util.Timer.*;
import arc.util.serialization.*;
import arc.util.serialization.JsonValue.*;
import arc.util.serialization.JsonWriter.*;
import arc.util.serialization.Jval.*;
import mindustry.*;
import mindustry.core.GameState.*;
@@ -396,7 +394,7 @@ public class ServerControl implements ApplicationListener{
});
}
JsonValue readRulesFile(){
Jval readRulesFile(){
return JsonIO.json.fromJson(null, Jval.read(rulesFile.readString()).toString(Jformat.plain));
}
@@ -690,7 +688,7 @@ public class ServerControl implements ApplicationListener{
});
handler.register("rules", "[remove/add] [name] [value...]", "List, remove or add global rules. These will apply regardless of map.", arg -> {
JsonValue base = readRulesFile();
Jval base = readRulesFile();
if(arg.length == 0){
info("Rules:\n@", Jval.read(base.toJson(OutputType.minimal)).toString(Jformat.hjson));
@@ -718,10 +716,10 @@ public class ServerControl implements ApplicationListener{
}
try{
JsonValue value = new JsonReader().parse(arg[2]);
Jval value = new JsonReader().parse(arg[2]);
value.name = arg[1];
JsonValue parent = new JsonValue(ValueType.object);
Jval parent = new Jval(ValueType.object);
parent.addChild(value);
JsonIO.json.readField(state.rules, value.name, parent);

View File

@@ -6,7 +6,6 @@ import arc.struct.*;
import arc.util.*;
import arc.util.io.*;
import arc.util.serialization.*;
import arc.util.serialization.JsonValue.*;
import mindustry.*;
import mindustry.content.*;
import mindustry.core.*;