/* Loads a world from the provided stream returning the world, optionally closing the stream. */
    public static World LoadWorld(Stream stream, bool closeStream)
    {
        try {
            var lengthBytes = new byte[4];
            stream.Read(lengthBytes, 0, 4);
            var length = BitConverter.ToInt32(lengthBytes, 0);
            Debug("Reading world (" + WorldEncoder.DataSizeString(length) + ").");

            var data = new byte[length];
            stream.Read(data, 0, length);

            if (closeStream)
            {
                Debug("Closing stream based on preferences.");
                stream.Close();
                Debug("Closed stream based on preferences.");
            }

            return(ConvertFromBytes(data));
        } catch (Exception e) {
            Debug(e);
        }

        return(null);
    }
    /* Convert the provided world to bytes. */
    public static byte[] ConvertToBytes(World world)
    {
        var encoder = GetEncoder();

        Debug("Encoding world (" + WorldEncoder.DataSizeString(encoder.CalculateSize(world)) + ").");

        var data          = new byte[encoder.CalculateSize(world)];
        var dataInterface = new DataInterface(ref data);

        dataInterface.Write(data.Length);
        dataInterface.Write(encoder.VersionCode);
        encoder.Encode(world, dataInterface);

        Debug("Finished encoding the world.");

        return(data);
    }