split up TilePrefab saving. clean up loading a bit to be more consistent with the others
This commit is contained in:
parent
7795b5046c
commit
f19e769891
|
@ -3,7 +3,7 @@ package ca.blarg.gdx.tilemap3d.json.prefabs;
|
||||||
import ca.blarg.gdx.Strings;
|
import ca.blarg.gdx.Strings;
|
||||||
import ca.blarg.gdx.tilemap3d.json.tilemap.TileDataSerializer;
|
import ca.blarg.gdx.tilemap3d.json.tilemap.TileDataSerializer;
|
||||||
import ca.blarg.gdx.tilemap3d.prefabs.TilePrefab;
|
import ca.blarg.gdx.tilemap3d.prefabs.TilePrefab;
|
||||||
import com.badlogic.gdx.Gdx;
|
import com.badlogic.gdx.assets.AssetManager;
|
||||||
import com.badlogic.gdx.files.FileHandle;
|
import com.badlogic.gdx.files.FileHandle;
|
||||||
import com.badlogic.gdx.utils.Base64Coder;
|
import com.badlogic.gdx.utils.Base64Coder;
|
||||||
import com.badlogic.gdx.utils.Json;
|
import com.badlogic.gdx.utils.Json;
|
||||||
|
@ -11,48 +11,22 @@ import com.badlogic.gdx.utils.Json;
|
||||||
import java.nio.ByteBuffer;
|
import java.nio.ByteBuffer;
|
||||||
|
|
||||||
public class TilePrefabJsonLoader {
|
public class TilePrefabJsonLoader {
|
||||||
public static TilePrefab load(String prefabFile) {
|
public static JsonTilePrefab load(FileHandle file) {
|
||||||
return load(Gdx.files.internal(prefabFile));
|
Json json = new Json();
|
||||||
|
return json.fromJson(JsonTilePrefab.class, file);
|
||||||
}
|
}
|
||||||
|
|
||||||
public static TilePrefab load(FileHandle prefabFile) {
|
public static TilePrefab create(JsonTilePrefab definition, AssetManager assetManager) {
|
||||||
if (prefabFile == null)
|
if (Strings.isNullOrEmpty(definition.data))
|
||||||
throw new IllegalArgumentException();
|
|
||||||
|
|
||||||
Json json = new Json();
|
|
||||||
JsonTilePrefab jsonPrefab = json.fromJson(JsonTilePrefab.class, prefabFile);
|
|
||||||
|
|
||||||
if (Strings.isNullOrEmpty(jsonPrefab.data))
|
|
||||||
throw new RuntimeException("Invalid prefab: no tile data.");
|
throw new RuntimeException("Invalid prefab: no tile data.");
|
||||||
|
|
||||||
TilePrefab prefab = new TilePrefab(jsonPrefab.width, jsonPrefab.height, jsonPrefab.depth);
|
TilePrefab prefab = new TilePrefab(definition.width, definition.height, definition.depth);
|
||||||
|
|
||||||
byte[] dataBytes = Base64Coder.decode(jsonPrefab.data);
|
byte[] dataBytes = Base64Coder.decode(definition.data);
|
||||||
ByteBuffer buffer = ByteBuffer.wrap(dataBytes);
|
ByteBuffer buffer = ByteBuffer.wrap(dataBytes);
|
||||||
|
|
||||||
TileDataSerializer.deserialize(buffer, prefab);
|
TileDataSerializer.deserialize(buffer, prefab);
|
||||||
|
|
||||||
return prefab;
|
return prefab;
|
||||||
}
|
}
|
||||||
|
|
||||||
public static void save(TilePrefab prefab, String outputFilename) {
|
|
||||||
if (prefab == null)
|
|
||||||
throw new IllegalArgumentException();
|
|
||||||
|
|
||||||
JsonTilePrefab jsonPrefab = new JsonTilePrefab();
|
|
||||||
jsonPrefab.width = prefab.getWidth();
|
|
||||||
jsonPrefab.height = prefab.getHeight();
|
|
||||||
jsonPrefab.depth = prefab.getDepth();
|
|
||||||
|
|
||||||
byte[] dataBytes = new byte[prefab.getData().length * TileDataSerializer.TILE_SIZE_BYTES];
|
|
||||||
ByteBuffer buffer = ByteBuffer.wrap(dataBytes);
|
|
||||||
|
|
||||||
TileDataSerializer.serialize(prefab, buffer);
|
|
||||||
jsonPrefab.data = new String(Base64Coder.encode(dataBytes));
|
|
||||||
|
|
||||||
Json json = new Json();
|
|
||||||
String output = json.prettyPrint(jsonPrefab);
|
|
||||||
FileHandle outputFile = Gdx.files.local(outputFilename);
|
|
||||||
outputFile.writeString(output, false);
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
|
@ -13,6 +13,7 @@ public class TilePrefabLoader extends AsynchronousAssetLoader<TilePrefab, TilePr
|
||||||
public static class TilePrefabParameter extends AssetLoaderParameters<TilePrefab> {
|
public static class TilePrefabParameter extends AssetLoaderParameters<TilePrefab> {
|
||||||
}
|
}
|
||||||
|
|
||||||
|
JsonTilePrefab definition;
|
||||||
TilePrefab prefab;
|
TilePrefab prefab;
|
||||||
|
|
||||||
public TilePrefabLoader(FileHandleResolver resolver) {
|
public TilePrefabLoader(FileHandleResolver resolver) {
|
||||||
|
@ -21,12 +22,13 @@ public class TilePrefabLoader extends AsynchronousAssetLoader<TilePrefab, TilePr
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public Array<AssetDescriptor> getDependencies(String fileName, FileHandle file, TilePrefabParameter parameter) {
|
public Array<AssetDescriptor> getDependencies(String fileName, FileHandle file, TilePrefabParameter parameter) {
|
||||||
|
definition = TilePrefabJsonLoader.load(file);
|
||||||
return null;
|
return null;
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public void loadAsync(AssetManager manager, String fileName, FileHandle file, TilePrefabParameter parameter) {
|
public void loadAsync(AssetManager manager, String fileName, FileHandle file, TilePrefabParameter parameter) {
|
||||||
prefab = TilePrefabJsonLoader.load(file);
|
prefab = TilePrefabJsonLoader.create(definition, manager);
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
|
|
|
@ -0,0 +1,33 @@
|
||||||
|
package ca.blarg.gdx.tilemap3d.json.prefabs;
|
||||||
|
|
||||||
|
import ca.blarg.gdx.tilemap3d.json.tilemap.TileDataSerializer;
|
||||||
|
import ca.blarg.gdx.tilemap3d.prefabs.TilePrefab;
|
||||||
|
import com.badlogic.gdx.Gdx;
|
||||||
|
import com.badlogic.gdx.files.FileHandle;
|
||||||
|
import com.badlogic.gdx.utils.Base64Coder;
|
||||||
|
import com.badlogic.gdx.utils.Json;
|
||||||
|
|
||||||
|
import java.nio.ByteBuffer;
|
||||||
|
|
||||||
|
public class TilePrefabSaver {
|
||||||
|
public static void save(TilePrefab prefab, String outputFilename) {
|
||||||
|
if (prefab == null)
|
||||||
|
throw new IllegalArgumentException();
|
||||||
|
|
||||||
|
JsonTilePrefab jsonPrefab = new JsonTilePrefab();
|
||||||
|
jsonPrefab.width = prefab.getWidth();
|
||||||
|
jsonPrefab.height = prefab.getHeight();
|
||||||
|
jsonPrefab.depth = prefab.getDepth();
|
||||||
|
|
||||||
|
byte[] dataBytes = new byte[prefab.getData().length * TileDataSerializer.TILE_SIZE_BYTES];
|
||||||
|
ByteBuffer buffer = ByteBuffer.wrap(dataBytes);
|
||||||
|
|
||||||
|
TileDataSerializer.serialize(prefab, buffer);
|
||||||
|
jsonPrefab.data = new String(Base64Coder.encode(dataBytes));
|
||||||
|
|
||||||
|
Json json = new Json();
|
||||||
|
String output = json.prettyPrint(jsonPrefab);
|
||||||
|
FileHandle outputFile = Gdx.files.local(outputFilename);
|
||||||
|
outputFile.writeString(output, false);
|
||||||
|
}
|
||||||
|
}
|
Loading…
Reference in a new issue