SDK

C# y Unity

Valkyr.cs, el wrapper P/Invoke de un solo archivo para .NET y Unity, y un MonoBehaviour de ejemplo para un servidor dedicado de Unity.

bindings/csharp/Valkyr.cs es un wrapper P/Invoke de un solo archivo sobre valkyr.h:

  • Engine, ClientSession, ProtectedLong y ProtectedDouble tienen cada uno un SafeHandle y liberan el objeto nativo en Dispose, con el finalizador como respaldo.
  • Los errores lanzan ValkyrException con el mensaje de la biblioteca.
  • Las cadenas se convierten a UTF-8 a mano, así que .NET Framework, .NET y Mono se comportan igual.
  • Native expone los puntos de entrada sin envoltorio si los prefieres.

Se limita a C# 7.3 para que el compilador de Unity lo acepte.

Estado

Valkyr.cs se compiló con el compilador Roslyn contra .NET Framework 4.8.1 y pasa su prueba básica. El ejemplo de Unity de abajo necesita UnityEngine y no se compiló aquí.

Servidor dedicado de Unity

Pon Valkyr.cs y el ejemplo en Assets/, y la biblioteca nativa en Assets/Plugins/x86_64/ (valkyr_ffi.dll en Windows, libvalkyr_ffi.so en servidores Linux). Envía solo el estado que el servidor posee o validó.

ValkyrServerExample.csC#
    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 ya usa metros e Y hacia arriba, y su yaw en ángulos de Euler coincide con el de valkyr (0 a lo largo de +Z, 90 a lo largo de +X). Solo cambia el signo del pitch: el eje x de Euler en Unity es positivo al mirar hacia abajo; Coords.PitchFromUnityEulerX lo convierte.

Prueba básica

ShellShell
cargo build -p valkyr-fficsc -nologo -platform:x64 -out:SmokeTest.exe Valkyr.cs tests/SmokeTest.cscopy ..\..\target\debug\valkyr_ffi.dll .SmokeTest.exe

Sin código nativo

Si prefieres no cargar una biblioteca nativa en el servidor de juego, ejecuta valkyr-server junto a él y envía los eventos por HTTP, como hace el puente de Godot. El SDK de .NET del proyecto de integraciones envuelve esa API.

Editar esta página en GitHub