2023-11-13 20:06:07 -08:00
|
|
|
package loading
|
|
|
|
|
|
|
|
import (
|
|
|
|
"git.nicholasnovak.io/nnovak/spatial-db/world"
|
|
|
|
"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()
|
|
|
|
|
|
|
|
// 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-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
|
|
|
}
|