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.tilemap3d.json.tilemap.TileDataSerializer;
|
||||
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.utils.Base64Coder;
|
||||
import com.badlogic.gdx.utils.Json;
|
||||
|
@ -11,48 +11,22 @@ import com.badlogic.gdx.utils.Json;
|
|||
import java.nio.ByteBuffer;
|
||||
|
||||
public class TilePrefabJsonLoader {
|
||||
public static TilePrefab load(String prefabFile) {
|
||||
return load(Gdx.files.internal(prefabFile));
|
||||
public static JsonTilePrefab load(FileHandle file) {
|
||||
Json json = new Json();
|
||||
return json.fromJson(JsonTilePrefab.class, file);
|
||||
}
|
||||
|
||||
public static TilePrefab load(FileHandle prefabFile) {
|
||||
if (prefabFile == null)
|
||||
throw new IllegalArgumentException();
|
||||
|
||||
Json json = new Json();
|
||||
JsonTilePrefab jsonPrefab = json.fromJson(JsonTilePrefab.class, prefabFile);
|
||||
|
||||
if (Strings.isNullOrEmpty(jsonPrefab.data))
|
||||
public static TilePrefab create(JsonTilePrefab definition, AssetManager assetManager) {
|
||||
if (Strings.isNullOrEmpty(definition.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);
|
||||
|
||||
TileDataSerializer.deserialize(buffer, 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> {
|
||||
}
|
||||
|
||||
JsonTilePrefab definition;
|
||||
TilePrefab prefab;
|
||||
|
||||
public TilePrefabLoader(FileHandleResolver resolver) {
|
||||
|
@ -21,12 +22,13 @@ public class TilePrefabLoader extends AsynchronousAssetLoader<TilePrefab, TilePr
|
|||
|
||||
@Override
|
||||
public Array<AssetDescriptor> getDependencies(String fileName, FileHandle file, TilePrefabParameter parameter) {
|
||||
definition = TilePrefabJsonLoader.load(file);
|
||||
return null;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void loadAsync(AssetManager manager, String fileName, FileHandle file, TilePrefabParameter parameter) {
|
||||
prefab = TilePrefabJsonLoader.load(file);
|
||||
prefab = TilePrefabJsonLoader.create(definition, manager);
|
||||
}
|
||||
|
||||
@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