/* 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);
    }
    /* Deregister world encoder by reference. */
    public static bool DeregisterEncoder(WorldEncoder encoder)
    {
        if (encoder == null)
        {
            return(false);
        }

        return(DeregisterEncoder(encoder.VersionCode));
    }
    /* 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);
    }
    /* Register world encoder. */
    public static bool RegisterEncoder(WorldEncoder encoder)
    {
        if (encoder == null)
        {
            return(false);
        }

        if (!encoders.ContainsKey(encoder.VersionCode))
        {
            try {
                encoders.Add(encoder.VersionCode, encoder);

                return(true);
            } catch (Exception e) {
                Error(e);
            }
        }

        return(false);
    }