1
0
mirror of https://github.com/Anuken/Mindustry.git synced 2024-11-13 07:15:28 +03:00

Actual map conversion, loading of saves

This commit is contained in:
Anuken 2019-05-09 10:53:52 -04:00
parent 8127e5a66f
commit 486e3ffc0a
30 changed files with 51 additions and 172 deletions

View File

@ -44,36 +44,8 @@ public class SerializeAnnotationProcessor extends AbstractProcessor{
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);
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){
TypeName type = TypeName.get(elem.asType());
String simpleTypeName = type.toString().substring(type.toString().lastIndexOf('.') + 1);
@ -95,19 +67,7 @@ public class SerializeAnnotationProcessor extends AbstractProcessor{
.addException(IOException.class)
.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);
jsonReadMethod.addStatement("$L object = new $L()", type, type);
List<VariableElement> fields = ElementFilter.fieldsIn(Utils.elementUtils.getAllMembers(elem));
for(VariableElement field : fields){
@ -121,43 +81,13 @@ public class SerializeAnnotationProcessor extends AbstractProcessor{
if(field.asType().getKind().isPrimitive()){
writeMethod.addStatement("stream.write" + capName + "(object." + name + ")");
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{
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)");
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");
jsonReadMethod.addStatement("return object");
serializer.addMethod(writeMethod.build());
serializer.addMethod(readMethod.build());
@ -172,53 +102,6 @@ public class SerializeAnnotationProcessor extends AbstractProcessor{
classBuilder.addMethod(writeMethod.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());

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.

BIN
core/assets/maps/map_9.msav Normal file

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.

View File

@ -210,9 +210,9 @@ public class Control implements ApplicationListener{
public void playZone(Zone zone){
ui.loadAnd(() -> {
logic.reset();
world.loadGenerator(zone.generator);
state.rules = zone.rules.get();
state.rules.zone = zone;
world.loadGenerator(zone.generator);
for(Tile core : state.teams.get(defaultTeam).cores){
for(ItemStack stack : zone.getStartingItems()){
core.entity.items.add(stack.item, stack.amount);

View File

@ -35,9 +35,12 @@ public class World implements ApplicationListener{
private boolean generating, invalidMap;
public World(){
//TODO swap
Core.app.post(maps::load);
//maps.load();
maps.load();
}
@Override
public void init(){
maps.loadLegacyMaps();
}
@Override

View File

@ -1,13 +1,29 @@
package io.anuke.mindustry.io;
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.SpawnGroup;
import io.anuke.mindustry.type.ContentType;
import io.anuke.mindustry.type.Zone;
public class JsonIO{
private static Json json = new Json(){{
setIgnoreUnknownFields(true);
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){

View File

@ -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);
}

View File

@ -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);
}

View File

@ -59,16 +59,6 @@ public class Maps implements Disposable{
/** Load all maps. Should be called at application start. */
public void load(){
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){
FileHandle file = Core.files.internal("maps/" + name + "." + mapExtension);
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));
}
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. */
private FileHandle findFile(){
//find a map name that isn't used.
@ -193,26 +211,6 @@ public class Maps implements Disposable{
}
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()){
try{
if(file.extension().equalsIgnoreCase(mapExtension)){

View File

@ -13,7 +13,6 @@ import io.anuke.mindustry.game.EventType.ZoneConfigureCompleteEvent;
import io.anuke.mindustry.game.EventType.ZoneRequireCompleteEvent;
import io.anuke.mindustry.game.Rules;
import io.anuke.mindustry.game.UnlockableContent;
import io.anuke.mindustry.gen.Serialization;
import io.anuke.mindustry.maps.generators.Generator;
import io.anuke.mindustry.world.Block;
@ -39,12 +38,6 @@ public class Zone extends UnlockableContent{
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){
super(name);
this.generator = generator;