2023-11-13 20:06:07 -08:00
|
|
|
package loading
|
|
|
|
|
|
|
|
import (
|
2023-11-18 19:47:09 -08:00
|
|
|
"path/filepath"
|
2023-11-16 00:24:09 -08:00
|
|
|
"strconv"
|
|
|
|
"strings"
|
|
|
|
|
2023-12-13 23:48:53 -08:00
|
|
|
"github.com/NickyBoy89/spatial-db/world"
|
2023-11-13 20:06:07 -08:00
|
|
|
"github.com/Tnze/go-mc/save"
|
|
|
|
"github.com/Tnze/go-mc/save/region"
|
|
|
|
)
|
|
|
|
|
2023-11-13 22:56:20 -08:00
|
|
|
// LoadRegionFile loads a single region file into an array of chunks
|
|
|
|
//
|
|
|
|
// A region is a 32x32 grid of chunks, although the final output can store less
|
|
|
|
func LoadRegionFile(fileName string) ([]world.ChunkData, error) {
|
|
|
|
regionFile, err := region.Open(fileName)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
defer regionFile.Close()
|
|
|
|
|
2023-11-16 00:24:09 -08:00
|
|
|
// Parse the name of the region to find its position within the world
|
2023-11-18 19:47:09 -08:00
|
|
|
nameParts := strings.Split(filepath.Base(fileName), ".")
|
2023-11-16 00:24:09 -08:00
|
|
|
regionX, err := strconv.Atoi(nameParts[1])
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
regionY, err := strconv.Atoi(nameParts[2])
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
2023-11-13 22:56:20 -08:00
|
|
|
// A region file is a 32x32 grid of chunks
|
|
|
|
chunks := []world.ChunkData{}
|
|
|
|
|
|
|
|
for i := 0; i < 32; i++ {
|
|
|
|
for j := 0; j < 32; j++ {
|
|
|
|
if regionFile.ExistSector(i, j) {
|
|
|
|
sectorFile, err := regionFile.ReadSector(i, j)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
2023-11-13 20:06:07 -08:00
|
|
|
}
|
|
|
|
|
2023-11-13 22:56:20 -08:00
|
|
|
// Read each chunk from disk
|
|
|
|
var chunk save.Chunk
|
|
|
|
if err := chunk.Load(sectorFile); err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
2023-11-13 20:06:07 -08:00
|
|
|
|
2023-11-13 22:56:20 -08:00
|
|
|
// Convert each chunk into the database's format
|
|
|
|
var chunkData world.ChunkData
|
|
|
|
chunkData.FromMCAChunk(chunk)
|
2023-11-13 20:06:07 -08:00
|
|
|
|
2023-11-16 00:24:09 -08:00
|
|
|
chunkData.Pos.X = regionX*32 + i
|
|
|
|
chunkData.Pos.Z = regionY*32 + j
|
|
|
|
|
2023-11-13 22:56:20 -08:00
|
|
|
chunks = append(chunks, chunkData)
|
2023-11-13 20:06:07 -08:00
|
|
|
}
|
|
|
|
}
|
2023-11-13 22:56:20 -08:00
|
|
|
}
|
2023-11-13 20:06:07 -08:00
|
|
|
|
2023-11-13 22:56:20 -08:00
|
|
|
return chunks, nil
|
2023-11-13 20:06:07 -08:00
|
|
|
}
|