Actual map conversion, loading of saves
This commit is contained in:
@@ -44,36 +44,8 @@ public class SerializeAnnotationProcessor extends AbstractProcessor{
|
|||||||
classBuilder.addJavadoc(RemoteMethodAnnotationProcessor.autogenWarning);
|
classBuilder.addJavadoc(RemoteMethodAnnotationProcessor.autogenWarning);
|
||||||
|
|
||||||
|
|
||||||
classBuilder.addField(FieldSpec.builder(ParameterizedTypeName.get(ClassName.bestGuess("io.anuke.arc.collection.ObjectMap"),
|
|
||||||
ClassName.get(Class.class),
|
|
||||||
ClassName.bestGuess("io.anuke.mindustry.io.JsonTypeWriter")),
|
|
||||||
"writers", Modifier.PRIVATE, Modifier.STATIC).initializer("new io.anuke.arc.collection.ObjectMap<>()").build());
|
|
||||||
|
|
||||||
classBuilder.addField(FieldSpec.builder(ParameterizedTypeName.get(ClassName.bestGuess("io.anuke.arc.collection.ObjectMap"),
|
|
||||||
ClassName.get(Class.class),
|
|
||||||
ClassName.bestGuess("io.anuke.mindustry.io.JsonTypeReader")),
|
|
||||||
"readers", Modifier.PRIVATE, Modifier.STATIC).initializer("new io.anuke.arc.collection.ObjectMap<>()").build());
|
|
||||||
|
|
||||||
classBuilder.addMethod(MethodSpec.methodBuilder("setSerializer")
|
|
||||||
.addModifiers(Modifier.STATIC, Modifier.PUBLIC)
|
|
||||||
.addTypeVariable(TypeVariableName.get("T"))
|
|
||||||
.addParameter(ParameterizedTypeName.get(ClassName.get(Class.class), TypeVariableName.get("T")), "type")
|
|
||||||
.addParameter(ParameterizedTypeName.get(ClassName.bestGuess("io.anuke.mindustry.io.JsonTypeWriter"), TypeVariableName.get("T")), "writer")
|
|
||||||
.addParameter(ParameterizedTypeName.get(ClassName.bestGuess("io.anuke.mindustry.io.JsonTypeReader"), TypeVariableName.get("T")), "reader")
|
|
||||||
.addStatement("writers.put(type, writer)")
|
|
||||||
.addStatement("readers.put(type, reader)").build());
|
|
||||||
|
|
||||||
|
|
||||||
MethodSpec.Builder method = MethodSpec.methodBuilder("init").addModifiers(Modifier.PUBLIC, Modifier.STATIC);
|
MethodSpec.Builder method = MethodSpec.methodBuilder("init").addModifiers(Modifier.PUBLIC, Modifier.STATIC);
|
||||||
|
|
||||||
TypeName jsonType = ClassName.bestGuess("io.anuke.arc.util.serialization.Json");
|
|
||||||
TypeName jsonValueType = ClassName.bestGuess("io.anuke.arc.util.serialization.JsonValue");
|
|
||||||
|
|
||||||
classBuilder.addField(jsonType, "bjson", Modifier.STATIC, Modifier.PRIVATE);
|
|
||||||
classBuilder.addStaticBlock(CodeBlock.builder()
|
|
||||||
.addStatement("bjson = new " + jsonType + "()")
|
|
||||||
.build());
|
|
||||||
|
|
||||||
for(TypeElement elem : elements){
|
for(TypeElement elem : elements){
|
||||||
TypeName type = TypeName.get(elem.asType());
|
TypeName type = TypeName.get(elem.asType());
|
||||||
String simpleTypeName = type.toString().substring(type.toString().lastIndexOf('.') + 1);
|
String simpleTypeName = type.toString().substring(type.toString().lastIndexOf('.') + 1);
|
||||||
@@ -95,19 +67,7 @@ public class SerializeAnnotationProcessor extends AbstractProcessor{
|
|||||||
.addException(IOException.class)
|
.addException(IOException.class)
|
||||||
.addModifiers(Modifier.PUBLIC);
|
.addModifiers(Modifier.PUBLIC);
|
||||||
|
|
||||||
MethodSpec.Builder jsonWriteMethod = MethodSpec.methodBuilder("write" + simpleTypeName + "Json")
|
|
||||||
.returns(void.class)
|
|
||||||
.addParameter(jsonType, "json")
|
|
||||||
.addParameter(type, "object")
|
|
||||||
.addModifiers(Modifier.PUBLIC, Modifier.STATIC);
|
|
||||||
|
|
||||||
MethodSpec.Builder jsonReadMethod = MethodSpec.methodBuilder("read" + simpleTypeName + "Json")
|
|
||||||
.returns(type)
|
|
||||||
.addParameter(jsonValueType, "value")
|
|
||||||
.addModifiers(Modifier.PUBLIC, Modifier.STATIC);
|
|
||||||
|
|
||||||
readMethod.addStatement("$L object = new $L()", type, type);
|
readMethod.addStatement("$L object = new $L()", type, type);
|
||||||
jsonReadMethod.addStatement("$L object = new $L()", type, type);
|
|
||||||
|
|
||||||
List<VariableElement> fields = ElementFilter.fieldsIn(Utils.elementUtils.getAllMembers(elem));
|
List<VariableElement> fields = ElementFilter.fieldsIn(Utils.elementUtils.getAllMembers(elem));
|
||||||
for(VariableElement field : fields){
|
for(VariableElement field : fields){
|
||||||
@@ -121,43 +81,13 @@ public class SerializeAnnotationProcessor extends AbstractProcessor{
|
|||||||
if(field.asType().getKind().isPrimitive()){
|
if(field.asType().getKind().isPrimitive()){
|
||||||
writeMethod.addStatement("stream.write" + capName + "(object." + name + ")");
|
writeMethod.addStatement("stream.write" + capName + "(object." + name + ")");
|
||||||
readMethod.addStatement("object." + name + "= stream.read" + capName + "()");
|
readMethod.addStatement("object." + name + "= stream.read" + capName + "()");
|
||||||
|
|
||||||
jsonWriteMethod.addStatement("json.writeValue(\"" + name + "\", object." + name + ")");
|
|
||||||
jsonReadMethod.addStatement("if(value.has(\"" + name + "\")) object." + name + "= value.get" + capName + "(\"" + name + "\")");
|
|
||||||
}else{
|
}else{
|
||||||
writeMethod.addStatement("io.anuke.arc.Core.settings.getSerializer(" + typeName + ".class).write(stream, object." + name + ")");
|
writeMethod.addStatement("io.anuke.arc.Core.settings.getSerializer(" + typeName + ".class).write(stream, object." + name + ")");
|
||||||
readMethod.addStatement("object." + name + " = (" + typeName + ")io.anuke.arc.Core.settings.getSerializer(" + typeName + ".class).read(stream)");
|
readMethod.addStatement("object." + name + " = (" + typeName + ")io.anuke.arc.Core.settings.getSerializer(" + typeName + ".class).read(stream)");
|
||||||
|
|
||||||
if(field.asType().toString().equalsIgnoreCase("java.lang.String")){
|
|
||||||
jsonWriteMethod.addStatement("json.writeValue(\"" + name + "\", object." + name + ")");
|
|
||||||
jsonReadMethod.addStatement("if(value.has(\"" + name + "\")) object." + name + "= value.getString(\"" + name + "\")");
|
|
||||||
}else if(field.asType().toString().startsWith("io.anuke.arc.collection.Array")){ //oh boy here it begins
|
|
||||||
String genericType = field.asType().toString().substring(field.asType().toString().indexOf('<') + 1, field.asType().toString().indexOf('>'));
|
|
||||||
{
|
|
||||||
jsonWriteMethod.addStatement("json.writeArrayStart($S)", name)
|
|
||||||
.beginControlFlow("for(" + genericType + " item : object." + name + ")")
|
|
||||||
.addStatement("json.writeValue(item)")
|
|
||||||
.endControlFlow()
|
|
||||||
.addStatement("json.writeArrayEnd()");
|
|
||||||
}
|
|
||||||
|
|
||||||
{
|
|
||||||
//jsonWriteMethod.beginControlFlow("if(value.has($S))", name);
|
|
||||||
//jsonWriteMethod.addStatement("io.anuke.arc.util.serialization.JsonValue list = value.get($S)", name);
|
|
||||||
//jsonWriteMethod.endControlFlow();
|
|
||||||
}
|
|
||||||
//jsonWriteMethod.addStatement("for( ")
|
|
||||||
}else{
|
|
||||||
jsonWriteMethod.addStatement("if(object."+name+" != null) writers.getThrow("+typeName+".class, () -> new IllegalArgumentException(\"Class '"
|
|
||||||
+ typeName + "' does not have a serializer!\")).write(bjson, object."+name+", \"" + name+"\")");
|
|
||||||
jsonReadMethod.addStatement("if(value.has(\"" + name + "\")) object." + name + " = ("+typeName+")readers.getThrow("+typeName+".class, () -> new IllegalArgumentException(\"Class '"
|
|
||||||
+ typeName + "' does not have a serializer!\")).read(value, \""+name+"\")");
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
readMethod.addStatement("return object");
|
readMethod.addStatement("return object");
|
||||||
jsonReadMethod.addStatement("return object");
|
|
||||||
|
|
||||||
serializer.addMethod(writeMethod.build());
|
serializer.addMethod(writeMethod.build());
|
||||||
serializer.addMethod(readMethod.build());
|
serializer.addMethod(readMethod.build());
|
||||||
@@ -172,53 +102,6 @@ public class SerializeAnnotationProcessor extends AbstractProcessor{
|
|||||||
|
|
||||||
classBuilder.addMethod(writeMethod.build());
|
classBuilder.addMethod(writeMethod.build());
|
||||||
classBuilder.addMethod(readMethod.build());
|
classBuilder.addMethod(readMethod.build());
|
||||||
|
|
||||||
classBuilder.addMethod(jsonWriteMethod.build());
|
|
||||||
classBuilder.addMethod(jsonReadMethod.build());
|
|
||||||
|
|
||||||
MethodSpec.Builder binaryJsonWriteMethod = MethodSpec.methodBuilder("write" + simpleTypeName + "StreamJson")
|
|
||||||
.returns(void.class)
|
|
||||||
.addParameter(DataOutput.class, "stream")
|
|
||||||
.addParameter(type, "object")
|
|
||||||
.addException(IOException.class)
|
|
||||||
.addModifiers(Modifier.PUBLIC, Modifier.STATIC)
|
|
||||||
.addStatement("java.io.StringWriter output = new java.io.StringWriter()")
|
|
||||||
.addStatement("bjson.setWriter(output)")
|
|
||||||
.addStatement("bjson.writeObjectStart(" + type + ".class, " + type + ".class)")
|
|
||||||
.addStatement("write" + simpleTypeName + "Json(bjson, object)")
|
|
||||||
.addStatement("bjson.writeObjectEnd()")
|
|
||||||
.addStatement("stream.writeUTF(output.toString())");
|
|
||||||
|
|
||||||
MethodSpec.Builder binaryJsonWriteStringMethod = MethodSpec.methodBuilder("write" + simpleTypeName + "Json")
|
|
||||||
.returns(String.class)
|
|
||||||
.addParameter(type, "object")
|
|
||||||
.addException(IOException.class)
|
|
||||||
.addModifiers(Modifier.PUBLIC, Modifier.STATIC)
|
|
||||||
.addStatement("java.io.StringWriter output = new java.io.StringWriter()")
|
|
||||||
.addStatement("bjson.setWriter(output)")
|
|
||||||
.addStatement("bjson.writeObjectStart(" + type + ".class, " + type + ".class)")
|
|
||||||
.addStatement("write" + simpleTypeName + "Json(bjson, object)")
|
|
||||||
.addStatement("bjson.writeObjectEnd()")
|
|
||||||
.addStatement("return output.toString()");
|
|
||||||
|
|
||||||
MethodSpec.Builder binaryJsonReadMethod = MethodSpec.methodBuilder("read" + simpleTypeName + "StreamJson")
|
|
||||||
.returns(type)
|
|
||||||
.addParameter(DataInput.class, "stream")
|
|
||||||
.addException(IOException.class)
|
|
||||||
.addModifiers(Modifier.PUBLIC, Modifier.STATIC)
|
|
||||||
.addStatement("return read" + simpleTypeName + "Json(bjson.fromJson(null, stream.readUTF()))");
|
|
||||||
|
|
||||||
MethodSpec.Builder binaryJsonReadStringMethod = MethodSpec.methodBuilder("read" + simpleTypeName + "StringJson")
|
|
||||||
.returns(type)
|
|
||||||
.addParameter(String.class, "str")
|
|
||||||
.addException(IOException.class)
|
|
||||||
.addModifiers(Modifier.PUBLIC, Modifier.STATIC)
|
|
||||||
.addStatement("return read" + simpleTypeName + "Json(bjson.fromJson(null, str))");
|
|
||||||
|
|
||||||
classBuilder.addMethod(binaryJsonWriteMethod.build());
|
|
||||||
classBuilder.addMethod(binaryJsonWriteStringMethod.build());
|
|
||||||
classBuilder.addMethod(binaryJsonReadMethod.build());
|
|
||||||
classBuilder.addMethod(binaryJsonReadStringMethod.build());
|
|
||||||
}
|
}
|
||||||
|
|
||||||
classBuilder.addMethod(method.build());
|
classBuilder.addMethod(method.build());
|
||||||
|
|||||||
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
@@ -210,9 +210,9 @@ public class Control implements ApplicationListener{
|
|||||||
public void playZone(Zone zone){
|
public void playZone(Zone zone){
|
||||||
ui.loadAnd(() -> {
|
ui.loadAnd(() -> {
|
||||||
logic.reset();
|
logic.reset();
|
||||||
|
world.loadGenerator(zone.generator);
|
||||||
state.rules = zone.rules.get();
|
state.rules = zone.rules.get();
|
||||||
state.rules.zone = zone;
|
state.rules.zone = zone;
|
||||||
world.loadGenerator(zone.generator);
|
|
||||||
for(Tile core : state.teams.get(defaultTeam).cores){
|
for(Tile core : state.teams.get(defaultTeam).cores){
|
||||||
for(ItemStack stack : zone.getStartingItems()){
|
for(ItemStack stack : zone.getStartingItems()){
|
||||||
core.entity.items.add(stack.item, stack.amount);
|
core.entity.items.add(stack.item, stack.amount);
|
||||||
|
|||||||
@@ -35,9 +35,12 @@ public class World implements ApplicationListener{
|
|||||||
private boolean generating, invalidMap;
|
private boolean generating, invalidMap;
|
||||||
|
|
||||||
public World(){
|
public World(){
|
||||||
//TODO swap
|
maps.load();
|
||||||
Core.app.post(maps::load);
|
}
|
||||||
//maps.load();
|
|
||||||
|
@Override
|
||||||
|
public void init(){
|
||||||
|
maps.loadLegacyMaps();
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
|
|||||||
@@ -1,13 +1,29 @@
|
|||||||
package io.anuke.mindustry.io;
|
package io.anuke.mindustry.io;
|
||||||
|
|
||||||
import io.anuke.arc.util.serialization.Json;
|
import io.anuke.arc.util.serialization.Json;
|
||||||
|
import io.anuke.arc.util.serialization.JsonValue;
|
||||||
|
import io.anuke.mindustry.Vars;
|
||||||
import io.anuke.mindustry.game.Rules;
|
import io.anuke.mindustry.game.Rules;
|
||||||
import io.anuke.mindustry.game.SpawnGroup;
|
import io.anuke.mindustry.game.SpawnGroup;
|
||||||
|
import io.anuke.mindustry.type.ContentType;
|
||||||
|
import io.anuke.mindustry.type.Zone;
|
||||||
|
|
||||||
public class JsonIO{
|
public class JsonIO{
|
||||||
private static Json json = new Json(){{
|
private static Json json = new Json(){{
|
||||||
setIgnoreUnknownFields(true);
|
setIgnoreUnknownFields(true);
|
||||||
setElementType(Rules.class, "spawns", SpawnGroup.class);
|
setElementType(Rules.class, "spawns", SpawnGroup.class);
|
||||||
|
|
||||||
|
setSerializer(Zone.class, new Serializer<Zone>(){
|
||||||
|
@Override
|
||||||
|
public void write(Json json, Zone object, Class knownType){
|
||||||
|
json.writeValue(object.name);
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public Zone read(Json json, JsonValue jsonData, Class type){
|
||||||
|
return Vars.content.getByName(ContentType.zone, jsonData.asString());
|
||||||
|
}
|
||||||
|
});
|
||||||
}};
|
}};
|
||||||
|
|
||||||
public static String write(Object object){
|
public static String write(Object object){
|
||||||
|
|||||||
@@ -1,7 +0,0 @@
|
|||||||
package io.anuke.mindustry.io;
|
|
||||||
|
|
||||||
import io.anuke.arc.util.serialization.JsonValue;
|
|
||||||
|
|
||||||
public interface JsonTypeReader<T>{
|
|
||||||
T read(JsonValue json, String name);
|
|
||||||
}
|
|
||||||
@@ -1,7 +0,0 @@
|
|||||||
package io.anuke.mindustry.io;
|
|
||||||
|
|
||||||
import io.anuke.arc.util.serialization.Json;
|
|
||||||
|
|
||||||
public interface JsonTypeWriter<T>{
|
|
||||||
void write(Json json, T object, String name);
|
|
||||||
}
|
|
||||||
@@ -59,16 +59,6 @@ public class Maps implements Disposable{
|
|||||||
/** Load all maps. Should be called at application start. */
|
/** Load all maps. Should be called at application start. */
|
||||||
public void load(){
|
public void load(){
|
||||||
try{
|
try{
|
||||||
//TODO remove, this is only for testing
|
|
||||||
|
|
||||||
for(FileHandle in : Core.files.absolute("/home/anuke/Projects/Mindustry/core/assets/maps").list()){
|
|
||||||
if(in.extension().equalsIgnoreCase(oldMapExtension)){
|
|
||||||
Log.info("Converting {0}...", in);
|
|
||||||
LegacyMapIO.convertMap(in, in.sibling(in.nameWithoutExtension() + "." + mapExtension));
|
|
||||||
Log.info("Converted {0}", in);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
for(String name : defaultMapNames){
|
for(String name : defaultMapNames){
|
||||||
FileHandle file = Core.files.internal("maps/" + name + "." + mapExtension);
|
FileHandle file = Core.files.internal("maps/" + name + "." + mapExtension);
|
||||||
loadMap(file, false);
|
loadMap(file, false);
|
||||||
@@ -167,6 +157,34 @@ public class Maps implements Disposable{
|
|||||||
return str == null ? null : str.equals("[]") ? new Array<>() : Array.with(json.fromJson(SpawnGroup[].class, str));
|
return str == null ? null : str.equals("[]") ? new Array<>() : Array.with(json.fromJson(SpawnGroup[].class, str));
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public void loadLegacyMaps(){
|
||||||
|
boolean convertedAny = false;
|
||||||
|
for(FileHandle file : customMapDirectory.list()){
|
||||||
|
if(file.extension().equalsIgnoreCase(oldMapExtension)){
|
||||||
|
try{
|
||||||
|
LegacyMapIO.convertMap(file, file.sibling(file.nameWithoutExtension() + "." + mapExtension));
|
||||||
|
//delete old, converted file; it is no longer useful
|
||||||
|
file.delete();
|
||||||
|
convertedAny = true;
|
||||||
|
Log.info("Converted file {0}", file);
|
||||||
|
}catch(IOException e){
|
||||||
|
//rename the file to a 'mmap_conversion_failed' extension to keep it there just in case
|
||||||
|
//but don't delete it
|
||||||
|
file.copyTo(file.sibling(file.name() + "_conversion_failed"));
|
||||||
|
file.delete();
|
||||||
|
Log.err(e);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
//free up any potential memory that was used up during conversion
|
||||||
|
if(convertedAny){
|
||||||
|
world.createTiles(0, 0);
|
||||||
|
//reload maps to load the converted ones
|
||||||
|
reload();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
/** Find a new filename to put a map to. */
|
/** Find a new filename to put a map to. */
|
||||||
private FileHandle findFile(){
|
private FileHandle findFile(){
|
||||||
//find a map name that isn't used.
|
//find a map name that isn't used.
|
||||||
@@ -193,26 +211,6 @@ public class Maps implements Disposable{
|
|||||||
}
|
}
|
||||||
|
|
||||||
private void loadCustomMaps(){
|
private void loadCustomMaps(){
|
||||||
boolean convertedAny = false;
|
|
||||||
for(FileHandle file : customMapDirectory.list()){
|
|
||||||
if(file.extension().equalsIgnoreCase(oldMapExtension)){
|
|
||||||
convertedAny = true;
|
|
||||||
try{
|
|
||||||
LegacyMapIO.convertMap(file, file.sibling(file.nameWithoutExtension() + "." + mapExtension));
|
|
||||||
//TODO delete so conversion doesn't happen again
|
|
||||||
//file.delete();
|
|
||||||
}catch(IOException e){
|
|
||||||
//don't convert
|
|
||||||
Log.err(e);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
//free up any potential memory that was used up during conversion
|
|
||||||
if(convertedAny){
|
|
||||||
world.createTiles(0, 0);
|
|
||||||
}
|
|
||||||
|
|
||||||
for(FileHandle file : customMapDirectory.list()){
|
for(FileHandle file : customMapDirectory.list()){
|
||||||
try{
|
try{
|
||||||
if(file.extension().equalsIgnoreCase(mapExtension)){
|
if(file.extension().equalsIgnoreCase(mapExtension)){
|
||||||
|
|||||||
@@ -13,7 +13,6 @@ import io.anuke.mindustry.game.EventType.ZoneConfigureCompleteEvent;
|
|||||||
import io.anuke.mindustry.game.EventType.ZoneRequireCompleteEvent;
|
import io.anuke.mindustry.game.EventType.ZoneRequireCompleteEvent;
|
||||||
import io.anuke.mindustry.game.Rules;
|
import io.anuke.mindustry.game.Rules;
|
||||||
import io.anuke.mindustry.game.UnlockableContent;
|
import io.anuke.mindustry.game.UnlockableContent;
|
||||||
import io.anuke.mindustry.gen.Serialization;
|
|
||||||
import io.anuke.mindustry.maps.generators.Generator;
|
import io.anuke.mindustry.maps.generators.Generator;
|
||||||
import io.anuke.mindustry.world.Block;
|
import io.anuke.mindustry.world.Block;
|
||||||
|
|
||||||
@@ -39,12 +38,6 @@ public class Zone extends UnlockableContent{
|
|||||||
|
|
||||||
private Array<ItemStack> defaultStartingItems = new Array<>();
|
private Array<ItemStack> defaultStartingItems = new Array<>();
|
||||||
|
|
||||||
static{
|
|
||||||
Serialization.setSerializer(Zone.class,
|
|
||||||
(json, zone, name) -> json.writeValue(name, zone.name),
|
|
||||||
(json, name) -> content.getByName(ContentType.zone, json.getString(name)));
|
|
||||||
}
|
|
||||||
|
|
||||||
public Zone(String name, Generator generator){
|
public Zone(String name, Generator generator){
|
||||||
super(name);
|
super(name);
|
||||||
this.generator = generator;
|
this.generator = generator;
|
||||||
|
|||||||
Reference in New Issue
Block a user