SDKs
C# and Unity
Valkyr.cs, the single file P/Invoke wrapper for .NET and Unity, and an example MonoBehaviour for a Unity dedicated server.
bindings/ is a single file P/Invoke wrapper over valkyr.h:
Engine,ClientSession,ProtectedLongandProtectedDoubleeach own aSafeHandleand free the native object onDispose, with the finalizer as a fallback.- Errors throw
ValkyrExceptionwith the library's message. - Strings are marshaled as UTF-8 by hand, so .NET Framework, .NET and Mono behave the same.
Nativeexposes the raw entry points if you prefer them.
It sticks to C# 7.3 so Unity's compiler accepts it.
Status
Valkyr.cs was compiled with the Roslyn compiler against .NET Framework 4.8.1 and passes its smoke test. The Unity example below needs UnityEngine and was not compiled here.
Unity dedicated server
Put Valkyr.cs and the example in Assets/, and the native library in Assets/ (valkyr_ on Windows, libvalkyr_ on Linux servers). Feed only state the server owns or validated.
private void Awake() {#if !UNITY_SERVER && !UNITY_EDITOR // Detection belongs on the server. A client build has nothing to judge. enabled = false; return;#else engine = new Engine(string.IsNullOrEmpty(configJson) ? null : configJson); Debug.Log("valkyr " + ValkyrLib.Version + " ready");#endif } private void FixedUpdate() { if (engine == null) return; ulong now = (ulong)clock.ElapsedMilliseconds; foreach (var p in players) { if (p.body == null) continue; Vector3 pos = p.body.position; double yaw = Coords.YawFromUnityEulerY(p.body.eulerAngles.y); double pitch = p.head != null ? Coords.PitchFromUnityEulerX(p.head.localEulerAngles.x) : 0.0; bool grounded = p.controller != null && p.controller.isGrounded; // client_ms: use the timestamp the client sent with its input // when you have one; the server time is a stand in here. int flags = engine.Frame(p.playerId, p.seq++, now, now, pos.x, pos.y, pos.z, yaw, pitch, grounded, false); if (flags > 0) { OnVerdicts(p, engine.LastVerdicts()); } } } /// <summary>Call this from your weapon code when the server accepts a shot.</summary>
Unity is already meters and Y up, and its euler yaw matches valkyr (0 along +Z, 90 along +X). Only the pitch sign differs: Unity euler x is positive when looking down; Coords. converts it.
Smoke test
cargo build -p valkyr-fficsc -nologo -platform:x64 -out:SmokeTest.exe Valkyr.cs tests/SmokeTest.cscopy ..\..\target\debug\valkyr_ffi.dll .SmokeTest.exe
Without native code
If you would rather not load a native library into the game server, run valkyr-server next to it and post events over HTTP, as the Godot bridge does. The .NET SDK in the integrations project wraps that API.