Skip to content

Commit

Permalink
Some levels crash the editor (#212)
Browse files Browse the repository at this point in the history
* Calling postLoad when needed

* 💄

* 💄
  • Loading branch information
Felix Siebeneicker authored Mar 24, 2021
1 parent 0f71e64 commit a89996b
Show file tree
Hide file tree
Showing 4 changed files with 62 additions and 61 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -349,6 +349,7 @@ private Level loadBinFile(FileHandle file) {
/** Loads a `.dat` level file. */
private Level loadDatFile(FileHandle file) {
Level level = JsonUtil.fromJson(Level.class, file);
level.postLoad();
level.init(Level.Source.EDITOR);

return level;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,37 +1,8 @@
package com.interrupt.dungeoneer.serializers;

import java.io.*;
import java.util.HashMap;

import com.interrupt.dungeoneer.serializers.v1.LevelSerializer;
import org.objenesis.strategy.SerializingInstantiatorStrategy;
import org.objenesis.strategy.StdInstantiatorStrategy;

import com.badlogic.gdx.Gdx;
import com.badlogic.gdx.files.FileHandle;
import com.badlogic.gdx.graphics.Color;
import com.badlogic.gdx.math.Vector2;
import com.badlogic.gdx.math.Vector3;
import com.badlogic.gdx.utils.Array;
import com.badlogic.gdx.utils.Array.ArrayIterator;
import com.badlogic.gdx.utils.IntArray;
import com.esotericsoftware.kryo.Kryo;
import com.esotericsoftware.kryo.Serializer;
import com.esotericsoftware.kryo.io.FastInput;
import com.esotericsoftware.kryo.io.FastOutput;
import com.esotericsoftware.kryo.io.Input;
import com.esotericsoftware.kryo.io.Output;
import com.esotericsoftware.kryo.serializers.CompatibleFieldSerializer;
import com.interrupt.dungeoneer.entities.Entity;
import com.interrupt.dungeoneer.entities.Model;
import com.interrupt.dungeoneer.entities.Monster;
import com.interrupt.dungeoneer.entities.Particle;
import com.interrupt.dungeoneer.entities.Prefab;
import com.interrupt.dungeoneer.entities.Sprite;
import com.interrupt.dungeoneer.game.Level;
import com.interrupt.dungeoneer.gfx.drawables.DrawableMesh;
import com.interrupt.dungeoneer.gfx.drawables.DrawableSprite;
import com.interrupt.dungeoneer.tiles.Tile;
import com.interrupt.dungeoneer.serializers.v1.LevelSerializer;

public class KryoSerializer {
public static synchronized Level loadLevel(FileHandle file) {
Expand All @@ -42,7 +13,7 @@ public static synchronized Level loadLevel(FileHandle file) {
return LevelSerializer.loadLevel(file);
}
}

public static synchronized Level loadLevel(byte[] bytes) {
try {
return com.interrupt.dungeoneer.serializers.v2.LevelSerializer.loadLevel(bytes);
Expand All @@ -60,11 +31,11 @@ public static synchronized Level loadOverworldLevel(FileHandle file) {
return null;
}
}

public static synchronized void saveLevel(FileHandle file, Level level) {
com.interrupt.dungeoneer.serializers.v2.LevelSerializer.saveLevel(file, level);
}

public static synchronized Object copyObject(Object object) {
return com.interrupt.dungeoneer.serializers.v2.LevelSerializer.copyObject(object);
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,11 +1,17 @@
package com.interrupt.dungeoneer.serializers.v1;

import java.io.ByteArrayOutputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.util.HashMap;

import com.badlogic.gdx.Gdx;
import com.badlogic.gdx.files.FileHandle;
import com.badlogic.gdx.graphics.Color;
import com.badlogic.gdx.math.Vector2;
import com.badlogic.gdx.math.Vector3;
import com.badlogic.gdx.math.collision.BoundingBox;
import com.badlogic.gdx.utils.Array;
import com.badlogic.gdx.utils.Array.ArrayIterator;
import com.badlogic.gdx.utils.IntArray;
Expand All @@ -15,22 +21,30 @@
import com.esotericsoftware.kryo.io.Input;
import com.esotericsoftware.kryo.io.Output;
import com.esotericsoftware.kryo.serializers.CompatibleFieldSerializer;
import com.interrupt.dungeoneer.entities.*;
import com.interrupt.dungeoneer.entities.Entity;
import com.interrupt.dungeoneer.entities.Model;
import com.interrupt.dungeoneer.entities.Monster;
import com.interrupt.dungeoneer.entities.Particle;
import com.interrupt.dungeoneer.entities.Prefab;
import com.interrupt.dungeoneer.entities.Sprite;
import com.interrupt.dungeoneer.game.Level;
import com.interrupt.dungeoneer.gfx.drawables.DrawableMesh;
import com.interrupt.dungeoneer.gfx.drawables.DrawableSprite;
import com.interrupt.dungeoneer.serializers.*;
import com.interrupt.dungeoneer.serializers.ArraySerializer;
import com.interrupt.dungeoneer.serializers.ColorSerializer;
import com.interrupt.dungeoneer.serializers.HashMapSerializer;
import com.interrupt.dungeoneer.serializers.IntArraySerializer;
import com.interrupt.dungeoneer.serializers.LibGdxArrayIteratorSerializer;
import com.interrupt.dungeoneer.serializers.PrefabSerializer;
import com.interrupt.dungeoneer.serializers.TileSerializer;
import com.interrupt.dungeoneer.tiles.Tile;

import java.io.*;
import java.util.HashMap;

public class LevelSerializer {
private static Kryo kryo = new Kryo();

static {
kryo.setDefaultSerializer(CompatibleFieldSerializer.class);

// register some classes
kryo.register(Color.class);
kryo.register(Vector3.class);
Expand All @@ -44,7 +58,7 @@ public class LevelSerializer {
kryo.register(DrawableSprite.class);
kryo.register(DrawableMesh.class);
kryo.register(Array.class);

// register some serializers
kryo.register(Prefab.class, new PrefabSerializer());
kryo.register(ArrayIterator.class, new LibGdxArrayIteratorSerializer());
Expand All @@ -54,39 +68,42 @@ public class LevelSerializer {
kryo.register(IntArray.class, new IntArraySerializer());
kryo.register(HashMap.class, new HashMapSerializer());
}

public static Level loadLevel(FileHandle file) {
Input input = new Input(file.read());
Level level = kryo.readObject(input, Level.class);
input.close();
level.postLoad();
return level;
}

public static Level loadLevel(File file) {
try {
Input input = new Input(new FileInputStream(file));
Level level = kryo.readObject(input, Level.class);
input.close();
level.postLoad();
return level;
}
catch (Exception ex) {
return null;
}
}

public static Level loadLevel(byte[] bytes) {
Input input = new Input(bytes);
Level level = kryo.readObject(input, Level.class);
input.close();
level.postLoad();
return level;
}

public static void saveLevel(FileHandle file, Level level) {
Output output = new Output(file.write(false));
kryo.writeObject(output, level);
output.close();
}

public static void saveLevel(File file, Level level) {
try {
Output output;
Expand All @@ -97,7 +114,7 @@ public static void saveLevel(File file, Level level) {
// oops!
}
}

public static Object copyObject(Object object) {
if(object == null) return null;
try {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,11 +1,17 @@
package com.interrupt.dungeoneer.serializers.v2;

import java.io.ByteArrayOutputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.util.HashMap;

import com.badlogic.gdx.Gdx;
import com.badlogic.gdx.files.FileHandle;
import com.badlogic.gdx.graphics.Color;
import com.badlogic.gdx.math.Vector2;
import com.badlogic.gdx.math.Vector3;
import com.badlogic.gdx.math.collision.BoundingBox;
import com.badlogic.gdx.utils.Array;
import com.badlogic.gdx.utils.Array.ArrayIterator;
import com.badlogic.gdx.utils.IntArray;
Expand All @@ -15,17 +21,23 @@
import com.esotericsoftware.kryo.io.Input;
import com.esotericsoftware.kryo.io.Output;
import com.esotericsoftware.kryo.serializers.CompatibleFieldSerializer;
import com.interrupt.dungeoneer.entities.*;
import com.interrupt.dungeoneer.entities.Entity;
import com.interrupt.dungeoneer.entities.Model;
import com.interrupt.dungeoneer.entities.Monster;
import com.interrupt.dungeoneer.entities.Particle;
import com.interrupt.dungeoneer.entities.Prefab;
import com.interrupt.dungeoneer.entities.Sprite;
import com.interrupt.dungeoneer.game.Level;
import com.interrupt.dungeoneer.game.OverworldLevel;
import com.interrupt.dungeoneer.gfx.drawables.DrawableMesh;
import com.interrupt.dungeoneer.gfx.drawables.DrawableSprite;
import com.interrupt.dungeoneer.serializers.*;
import com.interrupt.dungeoneer.serializers.ArraySerializer;
import com.interrupt.dungeoneer.serializers.ColorSerializer;
import com.interrupt.dungeoneer.serializers.HashMapSerializer;
import com.interrupt.dungeoneer.serializers.IntArraySerializer;
import com.interrupt.dungeoneer.serializers.LibGdxArrayIteratorSerializer;
import com.interrupt.dungeoneer.serializers.PrefabSerializer;
import com.interrupt.dungeoneer.tiles.Tile;
import com.interrupt.dungeoneer.tiles.TileMaterials;

import java.io.*;
import java.util.HashMap;

public class LevelSerializer {
private static Kryo kryo = new Kryo();
Expand Down Expand Up @@ -56,15 +68,15 @@ public class LevelSerializer {
kryo.register(IntArray.class, new IntArraySerializer());
kryo.register(HashMap.class, new HashMapSerializer());
}

public static Level loadLevel(FileHandle file) {
Input input = new Input(file.read());
Level level = kryo.readObject(input, Level.class);
input.close();
level.postLoad();
return level;
}

public static Level loadLevel(File file) {
try {
Input input = new Input(new FileInputStream(file));
Expand All @@ -77,7 +89,7 @@ public static Level loadLevel(File file) {
return null;
}
}

public static Level loadLevel(byte[] bytes) {
Input input = new Input(bytes);
Level level = kryo.readObject(input, Level.class);
Expand All @@ -93,13 +105,13 @@ public static OverworldLevel loadOverworldLevel(FileHandle file) {
level.postLoad();
return level;
}

public static void saveLevel(FileHandle file, Level level) {
Output output = new Output(file.write(false));
kryo.writeObject(output, level);
output.close();
}

public static void saveLevel(File file, Level level) {
try {
Output output;
Expand All @@ -110,7 +122,7 @@ public static void saveLevel(File file, Level level) {
// oops!
}
}

public static Object copyObject(Object object) {
if(object == null) return null;
try {
Expand Down

0 comments on commit a89996b

Please sign in to comment.