mirror of
https://github.com/Anuken/Mindustry.git
synced 2026-10-01 18:50:27 +03:00
Fixed some horrible JSON hacks
This commit is contained in:
@@ -20,11 +20,7 @@ import java.io.*;
|
||||
|
||||
@SuppressWarnings("unchecked")
|
||||
public class JsonIO{
|
||||
private static final CustomJson jsonBase = new CustomJson();
|
||||
|
||||
public static final Json json = new Json(){
|
||||
{ apply(this); }
|
||||
|
||||
@Override
|
||||
public void writeValue(Object value, Class knownType, Class elementType){
|
||||
if(value instanceof MappableContent c){
|
||||
@@ -80,8 +76,9 @@ public class JsonIO{
|
||||
return json.fromJson(type, string.replace("io.anuke.", ""));
|
||||
}
|
||||
|
||||
public static <T> T read(Class<T> type, T base, String string){
|
||||
return jsonBase.fromBaseJson(type, base, string.replace("io.anuke.", ""));
|
||||
public static <T> T read(T base, String string){
|
||||
json.readFields(base, new JsonReader().parse(string.replace("io.anuke.", "")));
|
||||
return base;
|
||||
}
|
||||
|
||||
public static String print(String in){
|
||||
@@ -90,10 +87,9 @@ public class JsonIO{
|
||||
|
||||
public static void classTag(String tag, Class<?> type){
|
||||
json.addClassTag(tag, type);
|
||||
jsonBase.addClassTag(tag, type);
|
||||
}
|
||||
|
||||
static void apply(Json json){
|
||||
static{
|
||||
json.setElementType(Rules.class, "spawns", SpawnGroup.class);
|
||||
json.setElementType(Rules.class, "loadout", ItemStack.class);
|
||||
|
||||
@@ -356,28 +352,4 @@ public class JsonIO{
|
||||
json.addClassTag(Strings.camelize(i.getClass().getSimpleName().replace("Filter", "")), i.getClass());
|
||||
}
|
||||
}
|
||||
|
||||
static class CustomJson extends Json{
|
||||
private Object baseObject;
|
||||
|
||||
{ apply(this); }
|
||||
|
||||
@Override
|
||||
public <T> T fromJson(Class<T> type, String json){
|
||||
return fromBaseJson(type, null, json);
|
||||
}
|
||||
|
||||
public <T> T fromBaseJson(Class<T> type, T base, String json){
|
||||
this.baseObject = base;
|
||||
return readValue(type, null, new JsonReader().parse(json));
|
||||
}
|
||||
|
||||
@Override
|
||||
protected Object newInstance(Class type){
|
||||
if(baseObject == null || baseObject.getClass() != type){
|
||||
return super.newInstance(type);
|
||||
}
|
||||
return baseObject;
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -100,7 +100,8 @@ public class Map implements Comparable<Map>, Publishable{
|
||||
|
||||
public Rules rules(Rules base){
|
||||
try{
|
||||
Rules result = JsonIO.read(Rules.class, base, tags.get("rules", "{}"));
|
||||
Rules result = JsonIO.read(base, tags.get("rules", "{}"));
|
||||
|
||||
//replace the default serpulo env with erekir
|
||||
if(result.planet == Planets.serpulo && result.hasEnv(Env.scorching)){
|
||||
result.planet = Planets.erekir;
|
||||
|
||||
Reference in New Issue
Block a user