This commit is contained in:
sharlottes
2021-11-07 01:39:07 +09:00
parent aa03e8e2f1
commit 0d8b7a34ba
5 changed files with 129 additions and 25 deletions

View File

@@ -1,49 +1,87 @@
package UnitInfo.core;
import arc.Core;
import arc.files.Fi;
import arc.struct.Seq;
import arc.util.Log;
import arc.util.*;
import mindustry.*;
import mindustry.ctype.Content;
import mindustry.ctype.MappableContent;
import mindustry.ctype.*;
import org.hjson.*;
import java.lang.reflect.Field;
import java.lang.reflect.*;
import static mindustry.Vars.modDirectory;
public class ContentJSON {
public static void collect(String name, Object object, JsonObject obj) {
Object preval = obj.get(name);
if(preval != null) obj.set(name, preval + " or " + object);
else {
if(object == null) obj.add(name, "null");
else if(!Modifier.isStatic(object.getClass().getModifiers())) { //no static
if(object instanceof Integer val) obj.add(name, val);
else if(object instanceof Double val) obj.add(name, val);
else if(object instanceof Float val) obj.add(name, val);
else if(object instanceof Long val) obj.add(name, val);
else if(object instanceof String val) obj.add(name, val);
else if(object instanceof Boolean val) obj.add(name, val);
else if(object instanceof Content cont){ //create new json object
//obj.add(name, getContent(cont, new JsonObject()));
}
else if(object instanceof Seq seq && seq.any()) {
StringBuilder str = new StringBuilder("[");
for(int i = 0; i < seq.size; i++) {
if(seq.get(i) != null) str.append(seq.get(i).toString()).append(i+1==seq.size ? "" : ", ");
}
str.append("]");
obj.add(name, str.toString());
}
else {
if(object.getClass().isArray()) {
StringBuilder str = new StringBuilder("[");
for(int i = 0; i < Array.getLength(object); i++) {
if(Array.get(object, i) != null) str.append(Array.get(object, i).toString()).append(i+1==Array.getLength(object) ? "" : ", ");
}
str.append("]");
obj.add(name, str.toString());
} else {
obj.add(name, object.toString());
}
}
}
}
}
public static JsonObject getContent(Content cont, JsonObject obj) {
for(Field field : cont.getClass().getFields()){
try {
collect(field.getName(), field.get(cont), obj);
} catch(Throwable e) {
Log.warn(e.getMessage() + "on " + cont);
obj.add(field.getName(), "### ERROR ###");
}
}
return obj;
}
public static void save() {
for(Seq<Content> content : Vars.content.getContentMap()) {
if(content.isEmpty()) continue;
JsonObject data = new JsonObject();
content.each(cont -> {
JsonObject obj = new JsonObject();
Seq<Field> seq = new Seq<Field>(cont.getClass().getFields());
seq.reverse();
obj.add("type", cont.getClass().getName());
for(Field field : seq){
try {
String str = field.getName();
Object object = field.get(cont);
if(object instanceof Integer val) obj.add(str, val);
if(object instanceof Long val) obj.add(str, val);
if(object instanceof Float val) obj.add(str, val);
if(object instanceof String val) obj.add(str, val);
if(object instanceof Double val) obj.add(str, val);
if(object instanceof Boolean val) obj.add(str, val);
} catch (IllegalAccessException e) {
e.printStackTrace();
}
}
getContent(cont, obj);
String name = cont.toString();
if(cont instanceof MappableContent mapCont) name = mapCont.name;
data.add(name, obj);
});
try {
modDirectory.child("UnitInfo").child(content.peek().getContentType().toString() + ".json").writeString(data.toString(Stringify.FORMATTED));
} catch (Throwable t){
} catch (Throwable e){
Log.warn(e.getMessage());
}
}
Log.info("JSON file is completely updated!");