Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Added NavMesh.ToWriter() in case you don't need a navmesh file #39

Open
wants to merge 2 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
31 changes: 28 additions & 3 deletions detour/mesh.go
Original file line number Diff line number Diff line change
Expand Up @@ -130,6 +130,11 @@ func (m *NavMesh) SaveToFile(fn string) error {
return err
}

return m.ToWriter(f)
}

// ToWriter writes binary navigation mesh to io.Writer.
func (m *NavMesh) ToWriter(w io.Writer) error {
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Good idea. It would be even better if the function was called WriteTo and had the following signature:

WriteTo(w Writer) (n int64, err error)

since that would make NavMesh implement the io.WriterTo interface.

// Store header.
var header navMeshSetHeader
header.Magic = navMeshSetMagic
Expand All @@ -143,7 +148,7 @@ func (m *NavMesh) SaveToFile(fn string) error {
}
header.Params = m.Params

if _, err = header.WriteTo(f); err != nil {
if _, err := header.WriteTo(w); err != nil {
return fmt.Errorf("Error writing header: %v", err)
}

Expand All @@ -157,21 +162,41 @@ func (m *NavMesh) SaveToFile(fn string) error {
var tileHeader navMeshTileHeader
tileHeader.TileRef = m.TileRef(tile)
tileHeader.DataSize = tile.DataSize
if _, err = tileHeader.WriteTo(f); err != nil {
if _, err := tileHeader.WriteTo(w); err != nil {
return err
}
var data []byte = make([]byte, tile.DataSize)
// first Serialize the tile header
tile.Header.serialize(data)
// then the tile itself
tile.serialize(data[tile.Header.size():])
if _, err = f.Write(data); err != nil {
if _, err := w.Write(data); err != nil {
return err
}
}
return nil
}

// Returns binary navigation mesh file size.
func (m *NavMesh) Size() int32 {
var header navMeshSetHeader
header.Params = m.Params
hSize := header.Size()

var tilesSize int32 = 0
var tileHeader navMeshTileHeader
for i := int32(0); i < m.MaxTiles; i++ {
tile := &m.Tiles[i]
if tile.DataSize == 0 {
continue
}

tilesSize += int32(tileHeader.Size()) + tile.DataSize
}

return int32(hSize) + tilesSize
}

Comment on lines +180 to +199
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

What's the use case for that method?
By using the WriteTo method (with the new signature), one also obtains the final file size.
If you're not interested by the data, but just by its size, you could something like

var navMesh *NavMesh
n, err := navMesh.WriteTo(io.discard)
if err {...}
fmt.Println("nav mesh file size:", n)

Let me know if that works for you.

// InitForSingleTile set up the navigation mesh for single tile use.
//
// Arguments:
Expand Down
6 changes: 3 additions & 3 deletions detour/structs.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,12 +13,12 @@ type navMeshSetHeader struct {
Params NavMeshParams
}

func (s *navMeshSetHeader) size() int {
func (s *navMeshSetHeader) Size() int {
return 12 + s.Params.size()
}

func (s *navMeshSetHeader) serialize(dst []byte) {
if len(dst) < s.size() {
if len(dst) < s.Size() {
panic("undersized buffer for navMeshSetHeader")
}
var (
Expand All @@ -34,7 +34,7 @@ func (s *navMeshSetHeader) serialize(dst []byte) {
}

func (s *navMeshSetHeader) WriteTo(w io.Writer) (int64, error) {
buf := make([]byte, s.size())
buf := make([]byte, s.Size())
s.serialize(buf)

n, err := w.Write(buf)
Expand Down