Beispiel #1
0
// LoadLevel loads level data from the given level.dat file.
func LoadLevel(file string) (*Level, error) {
	fd, err := os.Open(file)
	if err != nil {
		return nil, err
	}

	defer fd.Close()

	gz, err := gzip.NewReader(fd)
	if err != nil {
		return nil, err
	}

	defer gz.Close()

	var v struct {
		Data Level
	}

	err = nbt.Unmarshal(gz, &v)
	if err != nil {
		return nil, err
	}

	return &v.Data, nil
}
Beispiel #2
0
// Read decompresses chunk data into the given structure.
// Returns false if ther eis no data or the decompression failed.
func (cd *ChunkDescriptor) Read(c *Chunk) bool {
	var r io.ReadCloser
	var err error

	buf := bytes.NewBuffer(cd.data)

	switch cd.scheme {
	case GZip:
		r, err = gzip.NewReader(buf)
	case ZLib:
		r, err = zlib.NewReader(buf)
	default:
		return false
	}

	if err != nil {
		return false
	}

	// Clear out existing data; the nbt decoder will append to the existing slices.
	c.Sections = nil
	c.Biomes = nil
	c.HeightMap = nil
	c.Entities = nil
	c.TileEntities = nil
	c.TileTicks = nil

	var v struct {
		Level *Chunk
	}
	v.Level = c

	err = nbt.Unmarshal(r, &v)
	r.Close()

	if err != nil {
		return false
	}

	return true
}