Skip to content

Commit

Permalink
Create bindings for DetourTileCache.
Browse files Browse the repository at this point in the history
The inline lib requires more definitions (since there are more interfaces).
  • Loading branch information
andriyDev committed Apr 8, 2023
1 parent 09dee33 commit 9e4e7a7
Show file tree
Hide file tree
Showing 4 changed files with 207 additions and 4 deletions.
32 changes: 30 additions & 2 deletions build.rs
Original file line number Diff line number Diff line change
Expand Up @@ -161,6 +161,20 @@ fn generate_recast_bindings() {
},
out_path.join("detour_crowd.rs"),
);

create_bindings(
|builder| {
builder
.header("recastnavigation/DetourTileCache/Include/DetourTileCache.h")
.header(
"recastnavigation/DetourTileCache/Include/DetourTileCacheBuilder.h",
)
.blocklist_file(".*DetourAlloc\\.h")
.blocklist_file(".*DetourStatus\\.h")
.clang_args(["-Irecastnavigation/Detour/Include"].iter())
},
out_path.join("detour_tile_cache.rs"),
)
}

fn build_and_link_inline_lib() {
Expand All @@ -169,6 +183,9 @@ fn build_and_link_inline_lib() {
cc::Build::new()
.file("inline_lib_src/inline.cc")
.include("recastnavigation/Recast/Include")
.include("recastnavigation/Detour/Include")
.include("recastnavigation/DetourCrowd/Include")
.include("recastnavigation/DetourTileCache/Include")
.compile("recast_inline");

println!("cargo:rustc-link-search=native={}", env::var("OUT_DIR").unwrap());
Expand All @@ -178,9 +195,20 @@ fn build_and_link_inline_lib() {
fn generate_inline_bindings() {
let bindings = bindgen::Builder::default()
.header("inline_lib_src/inline.h")
.blocklist_type("rcContext")
.parse_callbacks(Box::new(bindgen::CargoCallbacks))
.clang_args(["-x", "c++"].iter())
.clang_args(
[
"-x",
"c++",
"-Irecastnavigation/Recast/Include",
"-Irecastnavigation/Detour/Include",
"-Irecastnavigation/DetourCrowd/Include",
"-Irecastnavigation/DetourTileCache/Include",
]
.iter(),
)
.allowlist_recursively(false)
.allowlist_file("inline_lib_src/inline.h")
.generate()
.expect("Unable to generate bindings.");

Expand Down
114 changes: 114 additions & 0 deletions inline_lib_src/inline.cc
Original file line number Diff line number Diff line change
Expand Up @@ -6,3 +6,117 @@
rcContext* CreateContext(bool state) { return new rcContext(state); }

void DeleteContext(rcContext* context) { delete context; }

class ForwardVtableTileCacheMeshProcess : public dtTileCacheMeshProcess {
public:
ForwardVtableTileCacheMeshProcess(
void* object_ptr, ForwardVtableTileCacheMeshProcessProcessFn process_fn)
: object_ptr(object_ptr), process_fn(process_fn) {}

void process(dtNavMeshCreateParams* params, unsigned char* polyAreas,
unsigned short* polyFlags) override {
process_fn(object_ptr, params, polyAreas, polyFlags);
}

private:
void* object_ptr;
ForwardVtableTileCacheMeshProcessProcessFn process_fn;
};

dtTileCacheMeshProcess* CreateForwardedTileCacheMeshProcess(
void* object_ptr, ForwardVtableTileCacheMeshProcessProcessFn process_fn) {
return new ForwardVtableTileCacheMeshProcess(object_ptr, process_fn);
}

void DeleteTileCacheMeshProcess(dtTileCacheMeshProcess* mesh_process) {
delete mesh_process;
}

dtTileCacheAlloc* CreateDefaultTileCacheAlloc() {
return new dtTileCacheAlloc();
}

void DeleteTileCacheAlloc(dtTileCacheAlloc* alloc) { delete alloc; }

class ForwardVtableTileCacheAlloc : public dtTileCacheAlloc {
public:
ForwardVtableTileCacheAlloc(void* object_ptr,
ForwardVtableTileCacheAllocResetFn reset_fn,
ForwardVtableTileCacheAllocAllocFn alloc_fn,
ForwardVtableTileCacheAllocFreeFn free_fn)
: object_ptr_(object_ptr),
reset_fn_(reset_fn),
alloc_fn_(alloc_fn),
free_fn_(free_fn) {}

private:
void reset() override { reset_fn_(object_ptr_); }

void* alloc(const size_t size) override {
return alloc_fn_(object_ptr_, size);
}

void free(void* ptr) override { free_fn_(object_ptr_, ptr); }

void* object_ptr_;
ForwardVtableTileCacheAllocResetFn reset_fn_;
ForwardVtableTileCacheAllocAllocFn alloc_fn_;
ForwardVtableTileCacheAllocFreeFn free_fn_;
};

dtTileCacheAlloc* CreateForwardedTileCacheAlloc(
void* object_ptr, ForwardVtableTileCacheAllocResetFn reset_fn,
ForwardVtableTileCacheAllocAllocFn alloc_fn,
ForwardVtableTileCacheAllocFreeFn free_fn) {
return new ForwardVtableTileCacheAlloc(object_ptr, reset_fn, alloc_fn,
free_fn);
}

class ForwardVtableTileCacheCompressor : public dtTileCacheCompressor {
public:
ForwardVtableTileCacheCompressor(
void* object_ptr,
ForwardVtableTileCacheCompressorMaxCompressedSizeFn
max_compressed_size_fn,
ForwardVtableTileCacheCompressorCompressFn compress_fn,
ForwardVtableTileCacheCompressorDecompressFn decompress_fn)
: object_ptr_(object_ptr),
max_compressed_size_fn_(max_compressed_size_fn),
compress_fn_(compress_fn),
decompress_fn_(decompress_fn) {}

private:
int maxCompressedSize(const int bufferSize) override {
return max_compressed_size_fn_(object_ptr_, bufferSize);
}
dtStatus compress(const unsigned char* buffer, const int bufferSize,
unsigned char* compressed, const int maxCompressedSize,
int* compressedSize) override {
return compress_fn_(object_ptr_, buffer, bufferSize, compressed,
maxCompressedSize, compressedSize);
}
dtStatus decompress(const unsigned char* compressed, const int compressedSize,
unsigned char* buffer, const int maxBufferSize,
int* bufferSize) override {
return decompress_fn_(object_ptr_, compressed, compressedSize, buffer,
maxBufferSize, bufferSize);
}

void* object_ptr_;
ForwardVtableTileCacheCompressorMaxCompressedSizeFn max_compressed_size_fn_;
ForwardVtableTileCacheCompressorCompressFn compress_fn_;
ForwardVtableTileCacheCompressorDecompressFn decompress_fn_;
};

dtTileCacheCompressor* CreateForwardedTileCacheCompressor(
void* object_ptr,
ForwardVtableTileCacheCompressorMaxCompressedSizeFn max_compressed_size_fn,
ForwardVtableTileCacheCompressorCompressFn compress_fn,
ForwardVtableTileCacheCompressorDecompressFn decompress_fn) {
return new ForwardVtableTileCacheCompressor(
object_ptr, max_compressed_size_fn, compress_fn, decompress_fn);
}

void DeleteTileCacheCompressor(dtTileCacheCompressor* compressor) {
delete compressor;
}
52 changes: 51 additions & 1 deletion inline_lib_src/inline.h
Original file line number Diff line number Diff line change
@@ -1,7 +1,57 @@
#pragma once

class rcContext;
// Recast definitions.

#include "Recast.h"

rcContext* CreateContext(bool state = true);

void DeleteContext(rcContext* context);

// DetourTileCache definitions.

#include "DetourNavMeshBuilder.h"
#include "DetourTileCache.h"
#include "DetourTileCacheBuilder.h"

using ForwardVtableTileCacheMeshProcessProcessFn =
void (*)(void* object_ptr, dtNavMeshCreateParams* params,
unsigned char* polyAreas, unsigned short* polyFlags);

dtTileCacheMeshProcess* CreateForwardedTileCacheMeshProcess(
void* object_ptr, ForwardVtableTileCacheMeshProcessProcessFn process_fn);

void DeleteTileCacheMeshProcess(dtTileCacheMeshProcess* mesh_process);

dtTileCacheAlloc* CreateDefaultTileCacheAlloc();

void DeleteTileCacheAlloc(dtTileCacheAlloc* alloc);

using ForwardVtableTileCacheAllocResetFn = void (*)(void* object_ptr);
using ForwardVtableTileCacheAllocAllocFn = void* (*)(void* object_ptr,
const size_t size);
using ForwardVtableTileCacheAllocFreeFn = void (*)(void* object_ptr,
void* size);

dtTileCacheAlloc* CreateForwardedTileCacheAlloc(
void* object_ptr, ForwardVtableTileCacheAllocResetFn reset_fn,
ForwardVtableTileCacheAllocAllocFn alloc_fn,
ForwardVtableTileCacheAllocFreeFn free_fn);

using ForwardVtableTileCacheCompressorMaxCompressedSizeFn =
int (*)(void* object_ptr, const int bufferSize);
using ForwardVtableTileCacheCompressorCompressFn =
dtStatus (*)(void* object_ptr, const unsigned char* buffer,
const int bufferSize, unsigned char* compressed,
const int maxCompressedSize, int* compressedSize);
using ForwardVtableTileCacheCompressorDecompressFn = dtStatus (*)(
void* object_ptr, const unsigned char* compressed, const int compressedSize,
unsigned char* buffer, const int maxBufferSize, int* bufferSize);

dtTileCacheCompressor* CreateForwardedTileCacheCompressor(
void* object_ptr,
ForwardVtableTileCacheCompressorMaxCompressedSizeFn max_compressed_size_fn,
ForwardVtableTileCacheCompressorCompressFn compress_fn,
ForwardVtableTileCacheCompressorDecompressFn decompress_fn);

void DeleteTileCacheCompressor(dtTileCacheCompressor* compressor);
13 changes: 12 additions & 1 deletion src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -15,14 +15,25 @@ mod ffi_detour_crowd {
include!(concat!(env!("OUT_DIR"), "/detour_crowd.rs"));
}

#[allow(non_camel_case_types, non_snake_case, non_upper_case_globals)]
mod ffi_detour_tile_cache {
use crate::ffi_detour::*;

include!(concat!(env!("OUT_DIR"), "/detour_tile_cache.rs"));
}

#[allow(non_camel_case_types, non_snake_case, non_upper_case_globals)]
mod ffi_inline {
use crate::ffi_recast::rcContext;
use crate::ffi_detour::*;
use crate::ffi_detour_tile_cache::*;
use crate::ffi_recast::*;

include!(concat!(env!("OUT_DIR"), "/inline.rs"));
}

pub use ffi_detour::*;
pub use ffi_detour_crowd::*;
pub use ffi_detour_tile_cache::*;
pub use ffi_inline::*;
pub use ffi_recast::*;

Expand Down

0 comments on commit 9e4e7a7

Please sign in to comment.