public static byte[] SerializeArrayThree <T>(T[,,] map, ValueSerializationType type) { IValueSerialization serialization = ValueSerializationFactory.GetSerialization(type); MemoryStream mStream = new MemoryStream(); int width = map.GetLength(0); int height = map.GetLength(1); int depth = map.GetLength(2); byte[] widthBytes = BitConverter.GetBytes(width); byte[] heightBytes = BitConverter.GetBytes(height); byte[] depthBytes = BitConverter.GetBytes(depth); mStream.Write(widthBytes, 0, widthBytes.Length); mStream.Write(heightBytes, 0, heightBytes.Length); mStream.Write(depthBytes, 0, depthBytes.Length); for (int x = 0; x < width; x++) { for (int y = 0; y < height; y++) { for (int z = 0; z < depth; z++) { byte[] buffer = serialization.GetBytes(map[x, y, z]); mStream.Write(buffer, 0, buffer.Length); } } } byte[] result = mStream.ToArray(); mStream.Close(); mStream.Dispose(); return(result); }
public static T[,] DeserializeArrayTwo <T>(byte[] data, ValueSerializationType type) { IValueSerialization serialization = ValueSerializationFactory.GetSerialization(type); MemoryStream mStream = new MemoryStream(data); byte[] widthBytes = new byte[4]; byte[] heightBytes = new byte[4]; mStream.Read(widthBytes, 0, widthBytes.Length); mStream.Read(heightBytes, 0, heightBytes.Length); int width = BitConverter.ToInt32(widthBytes, 0); int height = BitConverter.ToInt32(heightBytes, 0); T[,] result = new T[width, height]; for (int x = 0; x < width; x++) { for (int z = 0; z < height; z++) { byte[] buffer = new byte[serialization.Length]; mStream.Read(buffer, 0, buffer.Length); T value = (T)serialization.GetValue(buffer); result[x, z] = value; } } return(result); }