2023-12-08 19:30:19 -08:00
|
|
|
package server
|
2023-11-13 16:22:46 -08:00
|
|
|
|
2023-11-18 19:47:09 -08:00
|
|
|
import (
|
2023-12-13 23:48:53 -08:00
|
|
|
"github.com/NickyBoy89/spatial-db/storage"
|
|
|
|
"github.com/NickyBoy89/spatial-db/world"
|
2023-12-10 21:46:15 -08:00
|
|
|
log "github.com/sirupsen/logrus"
|
2023-11-18 19:47:09 -08:00
|
|
|
)
|
2023-11-13 16:22:46 -08:00
|
|
|
|
|
|
|
type HashServer struct {
|
|
|
|
blocks map[world.BlockPos]world.BlockID
|
|
|
|
}
|
|
|
|
|
|
|
|
func (hs *HashServer) SetStorageRoot(path string) {
|
|
|
|
hs.blocks = make(map[world.BlockPos]world.BlockID)
|
2023-11-18 19:47:09 -08:00
|
|
|
|
2023-12-11 16:43:10 -08:00
|
|
|
u, err := storage.OpenUnityFile(path, path+".metadata")
|
|
|
|
if err != nil {
|
|
|
|
panic(err)
|
|
|
|
}
|
|
|
|
defer u.Close()
|
|
|
|
|
|
|
|
chunks, err := u.ReadAllChunks()
|
2023-11-18 19:47:09 -08:00
|
|
|
if err != nil {
|
|
|
|
panic(err)
|
|
|
|
}
|
|
|
|
|
2023-12-10 21:46:15 -08:00
|
|
|
for chunkIndex, data := range chunks {
|
2023-11-18 19:47:09 -08:00
|
|
|
// Load in each data point from disk
|
2023-12-10 21:46:15 -08:00
|
|
|
log.Infof("Reading in chunk %d of %d", chunkIndex, len(chunks))
|
|
|
|
|
2023-11-18 19:47:09 -08:00
|
|
|
for _, section := range data.Sections {
|
|
|
|
for blockIndex, blockState := range section.BlockStates {
|
|
|
|
pos := data.IndexToBlockPos(blockIndex)
|
2023-12-10 22:37:37 -08:00
|
|
|
hs.blocks[pos] = section.Palette.State(blockState)
|
2023-11-18 19:47:09 -08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2023-11-13 16:22:46 -08:00
|
|
|
}
|
|
|
|
|
|
|
|
func (hs *HashServer) FetchChunk(pos world.ChunkPos) (world.ChunkData, error) {
|
|
|
|
panic("Unimplemented")
|
|
|
|
}
|
|
|
|
|
|
|
|
func (hs *HashServer) ChangeBlock(
|
|
|
|
worldPosition world.BlockPos,
|
|
|
|
targetState world.BlockID,
|
|
|
|
) error {
|
|
|
|
hs.blocks[worldPosition] = targetState
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (hs *HashServer) ChangeBlockRange(
|
|
|
|
targetState world.BlockID,
|
|
|
|
start, end world.BlockPos,
|
|
|
|
) error {
|
|
|
|
panic("Unimplemented")
|
|
|
|
}
|
|
|
|
|
|
|
|
func (hs *HashServer) ReadBlockAt(pos world.BlockPos) (world.BlockID, error) {
|
2023-12-05 15:27:40 -08:00
|
|
|
return hs.blocks[pos], nil
|
2023-11-13 16:22:46 -08:00
|
|
|
}
|
|
|
|
|
|
|
|
func (hs *HashServer) ReadChunkAt(pos world.ChunkPos) (world.ChunkData, error) {
|
|
|
|
var data world.ChunkData
|
|
|
|
data.Pos = pos
|
|
|
|
for blockPos, state := range hs.blocks {
|
|
|
|
if blockPos.ToChunkPos() == pos {
|
|
|
|
sec := data.SectionFor(blockPos)
|
|
|
|
sec.UpdateBlock(blockPos, state)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return data, nil
|
|
|
|
}
|