All Downloads are FREE. Search and download functionalities are using the official Maven repository.

ch.fritteli.maze.generator.serialization.v2.SerializerDeserializerV2 Maven / Gradle / Ivy

package ch.fritteli.maze.generator.serialization.v2;

import ch.fritteli.maze.generator.model.Maze;
import ch.fritteli.maze.generator.model.Position;
import ch.fritteli.maze.generator.model.Tile;
import lombok.experimental.UtilityClass;
import org.jetbrains.annotations.NotNull;

import java.lang.reflect.Constructor;
import java.lang.reflect.InvocationTargetException;

/**
 * Header bytes are:
 * 
 *   byte  hex meaning
 *     00 0x1a magic
 *     01 0xb1 magic
 *     02 0x02 version (0x00 -> dev, 0x01 -> deprecated, 0x02 -> stable)
 *     03..06  width (int)
 *     07..10  height (int)
 *     11..14  start-x (int)
 *     15..18  start-y (int)
 *     19..22  end-x (int)
 *     23..26  end-y (int)
 *     27..34  random seed number (long)
 *     35..    tiles
 * 
* Extraneous space (poss. last nibble) is ignored. */ @UtilityClass public class SerializerDeserializerV2 { public final byte VERSION_BYTE = 0x02; /** * Serializes the {@code maze} into a byte array. * * @param maze The {@link Maze} to be serialized. * @return The resulting byte array. */ @NotNull public byte[] serialize(@NotNull final Maze maze) { final MazeOutputStreamV2 stream = new MazeOutputStreamV2(); stream.writeHeader(); stream.writeMazeData(maze); return stream.toByteArray(); } /** * Deserializes the byte array into an instance of {@link Maze}. * * @param bytes The byte array to be deserialized. * @return An instance of {@link Maze}. */ @NotNull public Maze deserialize(@NotNull final byte[] bytes) { final MazeInputStreamV2 stream = new MazeInputStreamV2(bytes); stream.checkHeader(); return stream.readMazeData(); } @NotNull Maze createMaze(@NotNull final Tile[][] field, final int width, final int height, @NotNull final Position start, @NotNull final Position end, final long randomSeed) { try { final Constructor constructor = Maze.class.getDeclaredConstructor(Tile[][].class, Integer.TYPE, Integer.TYPE, Position.class, Position.class, Long.TYPE); constructor.setAccessible(true); return constructor.newInstance(field, width, height, start, end, randomSeed); } catch (@NotNull final NoSuchMethodException | IllegalAccessException | InstantiationException | InvocationTargetException e) { throw new RuntimeException("Can not deserialize Maze from maze data.", e); } } }




© 2015 - 2025 Weber Informatics LLC | Privacy Policy