SDKs
C
The valkyr C ABI: valkyr.h, building the library, the engine and client session calls, error handling and memory ownership.
The C interface is crates/, implemented by the valkyr-ffi crate. It is the base of the C++, C# and Godot bindings, and of any language with a C FFI.
Build the library
cargo build -p valkyr-ffi --release
| Platform | Dynamic | Static |
|---|---|---|
| Windows (MSVC) | valkyr_ and valkyr_ | valkyr_ plus the system libraries listed in valkyr.h |
| Linux | libvalkyr_ | libvalkyr_ plus -lpthread -ldl -lm |
| macOS | libvalkyr_ | libvalkyr_ |
The release archives and installers already contain the library and the header.
Example
A dedicated server feeds a player moving at 20 m/s, prints the first verdict and the player's trust:
int main(void) { ValkyrEngine *engine; uint64_t i; int32_t total_flags = 0; double probability = 0.0, trust = 100.0; int32_t action = VALKYR_ACTION_NONE; char *json; const uint64_t player = 7; printf("valkyr %s (abi %d)\n", valkyr_version(), (int)valkyr_abi_version()); if (valkyr_abi_version() != VALKYR_ABI_VERSION) { fprintf(stderr, "header and library ABI versions differ\n"); return 1; } engine = valkyr_engine_new(NULL); /* default configuration */ if (!check_ptr(engine, "valkyr_engine_new")) { return 1; } /* Spawn at the origin (JSON path), then 100 ticks of 50 ms (fast path). */ json = valkyr_engine_ingest_json(engine, "{\"type\":\"spawn\",\"player\":7,\"server_time_ms\":0," "\"position\":{\"x\":0,\"y\":0,\"z\":0},\"team\":1}"); if (!check_ptr(json, "valkyr_engine_ingest_json")) { valkyr_engine_free(engine); return 1; } valkyr_string_free(json); for (i = 0; i < 100; i++) { /* 1 m per 50 ms tick along +X: 20 m/s. */ int32_t n = valkyr_engine_frame(engine, player, i, i * 50, i * 50 + 30, (double)i, 0.0, 0.0, 90.0, 0.0, 1, 0); if (n < 0) { fprintf(stderr, "valkyr_engine_frame failed: %s\n", valkyr_last_error()); valkyr_engine_free(engine); return 1; } if (n > 0 && total_flags == 0) { /* Print the first verdict in full. */ json = valkyr_engine_last_verdicts_json(engine); if (check_ptr(json, "valkyr_engine_last_verdicts_json")) { printf("first verdict at tick %u: %s\n", (unsigned)i, json); valkyr_string_free(json); } } total_flags += n; } printf("flags raised: %d\n", (int)total_flags); if (valkyr_engine_trust(engine, player, 5000, &probability, &trust, &action) != VALKYR_OK) { fprintf(stderr, "valkyr_engine_trust failed: %s\n", valkyr_last_error()); valkyr_engine_free(engine); return 1; } printf("trust: probability=%.3f trust=%.1f action=%d\n", probability, trust, (int)action); json = valkyr_engine_trust_json(engine, player, 5000); if (check_ptr(json, "valkyr_engine_trust_json")) { printf("trust report: %s\n", json); valkyr_string_free(json); } /* Errors are reported, never crash: a NULL engine gives VALKYR_ERR_NULL. */ if (valkyr_engine_frame(NULL, 1, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0) == VALKYR_ERR_NULL) { printf("null handle rejected: %s\n", valkyr_last_error()); } valkyr_engine_free(engine); return total_flags > 0 ? 0 : 2;}
The test suite compiles and runs this file (cargo test -p valkyr-ffi --test c_examples).
Rules of the ABI
- Errors never crash. Functions return
NULLor a negativeVALKYR_code;ERR_* valkyr_gives the message for the calling thread.last_ error() - Strings you get, you free with
valkyr_. Strings you pass are borrowed for the call only.string_ free - Handles are not thread safe. Use one engine per thread, or guard it with your own lock.
- ABI version. Compare
valkyr_withabi_ version() VALKYR_at start, as the example does.ABI_ VERSION
Main entry points
| Function | Purpose |
|---|---|
valkyr_, valkyr_ | Create an engine from JSON config (or NULL for defaults) and free it. |
valkyr_ | Colliders for line of sight. |
valkyr_, valkyr_ | Fast paths for the two most frequent events. |
valkyr_ | Any event as JSON. |
valkyr_ | Verdicts of the last call. |
valkyr_, valkyr_, valkyr_ | Trust and evidence of a player. |
valkyr_, valkyr_ | Which entities a viewer may be sent. |
valkyr_, valkyr_ | Record a match for replay. |
valkyr_ | The client session of valkyr-client. |
valkyr_ | Build and verify the file manifest. |