SDKs

C# e Unity

Valkyr.cs, o wrapper P/Invoke em um único arquivo para .NET e Unity, e um MonoBehaviour de exemplo para um servidor dedicado Unity.

bindings/csharp/Valkyr.cs é um wrapper P/Invoke em um único arquivo sobre o valkyr.h:

  • Engine, ClientSession, ProtectedLong e ProtectedDouble têm cada um o seu SafeHandle e liberam o objeto nativo no Dispose, com o finalizador como reserva.
  • Erros lançam ValkyrException com a mensagem da biblioteca.
  • As strings são convertidas para UTF-8 à mão, então .NET Framework, .NET e Mono se comportam igual.
  • Native expõe os pontos de entrada brutos, se você preferir.

Ele se limita ao C# 7.3 para que o compilador da Unity aceite.

Status

O Valkyr.cs foi compilado com o compilador Roslyn para o .NET Framework 4.8.1 e passa no smoke test. O exemplo de Unity abaixo precisa do UnityEngine e não foi compilado aqui.

Servidor dedicado Unity

Coloque o Valkyr.cs e o exemplo em Assets/, e a biblioteca nativa em Assets/Plugins/x86_64/ (valkyr_ffi.dll no Windows, libvalkyr_ffi.so em servidores Linux). Envie só estado que o servidor controla ou validou.

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>

A Unity já usa metros e Y para cima, e o yaw em ângulos de Euler bate com o do valkyr (0 ao longo de +Z, 90 ao longo de +X). Só o sinal do pitch é diferente: o x de Euler da Unity é positivo quando se olha para baixo; Coords.PitchFromUnityEulerX faz a conversão.

Smoke test

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

Sem código nativo

Se você prefere não carregar uma biblioteca nativa no servidor de jogo, rode o valkyr-server ao lado dele e envie eventos via HTTP, como faz a bridge do Godot. O SDK .NET do projeto de integrações encapsula essa API.

Editar esta página no GitHub