Files
lingua-engine/plugins/engine.physics/Engine.Physics/Native.cs
T
EmilandClaude Sonnet 5 cd8f221ddd Fix real bugs from independent review (docs/review-handoff.md)
A second AI session read the whole codebase in parallel (read-only, no
code changes) and left a handoff doc. Addressed the correctness findings:

- PhysicsWorld.Sync's early return compared _bodies.Count to live.Count,
  not their contents — same size, different membership (destroy one
  tracked GameObject, gain one untracked-because-no-collider one; or any
  Restore where the scene has both a Rigidbody+collider object and a
  Rigidbody-without-one) skipped cleanup entirely, leaking the native
  Box3D body forever. Fixed by checking the actual stale set. Two new
  regression tests reproduce the review's own two scenarios via a new
  Lingua_GetBodyCount native export, asserting on the native table's own
  count rather than PhysicsWorld's C#-side bookkeeping.
- TryCreateBody stored handle -1 (native shim refused: invalid world, or
  its 8192-slot body table full) as if it were real — every later
  GetBodyTransform on it silently teleported the GameObject to the
  origin with a degenerate rotation, no error anywhere. Now checked and
  warned once, same as the missing-collider case.
- PluginHost.Load didn't roll back anything when Configure threw partway
  through: Schedule.Add/Events.Subscribe registrations it already made
  stayed forever, and its ALC was never unloaded — neither loaded (no
  _loaded entry) nor cleanly unloadable. Fixed with try/catch: best-effort
  Shutdown (the only thing that knows which services this plugin
  provided), RemoveAllFrom on both Schedule and EventBus, best-effort ALC
  unload, rethrow. New fixture plugin (sandbox.failing-configure, mirrors
  sandbox.echo's own real-load pattern) registers a system against a
  shared Ping component then throws, so FailedConfigureRollbackTests can
  assert the dangling system actually stops firing — an earlier version
  tried to prove this via AssemblyLoadContext.All instead, which passed
  even against the deliberately-reverted buggy code (the ALC turned out
  to get collected either way once its only references went out of
  scope); watching the dangling system is what actually distinguishes
  rolled-back from not, confirmed by deliberately reverting the fix and
  watching this specific test fail before restoring it.
- Engine.Host's "r <id>" left a plugin unloaded on a failed reload with
  no honest indication of that, and retrying threw "not loaded" instead
  of ever reaching Load again. Added PluginHost.IsLoaded so the handler
  only calls Unload when there's something to unload, and the failure
  message now says the plugin is unloaded, not just "failed."
- AssetService.ReloadWithRetry's `when (attempt < 4)` guard meant the
  5th and final IOException fell out of the loop and propagated from a
  discarded fire-and-forget Task — no log, no event, nothing. Now logged.
- EditorState.Selected kept pointing at a GameObject Restore had already
  destroyed after ExitPlay, so Inspector/gizmo would silently keep
  editing something no longer in the world. EditorPlugin.DrawUi now
  compares IsPlaying against its own previous frame (not just reacting to
  the Stop button) so this is caught whether Play was exited via the
  button or the "stop" stdin command — the same stdin-vs-real-control gap
  already hit once earlier this session — and re-resolves the selection
  by name.
- README.md and kernel-contract.md's Event Bus/Time rows had fallen a
  milestone behind (still said "no asset system yet" and "no fixed-step
  accumulator yet" after both shipped).

Full suite: 98 tests.

Co-Authored-By: Claude Sonnet 5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01N1qPfzq8TDCUMFMV3UwV5N
2026-09-02 20:57:08 +03:00

79 lines
3.0 KiB
C#

using System.Runtime.InteropServices;
namespace Engine.Physics;
/// <summary>
/// The entire P/Invoke surface, matching native/physics-native/
/// lingua_physics.c's exports one-to-one. Every parameter is a plain
/// int/float/bool — no struct crosses this boundary, on purpose, see that
/// file's own doc comment for why.
///
/// Classic DllImport, not the newer source-generated LibraryImport:
/// LibraryImport's generated marshalling code needs AllowUnsafeBlocks even
/// for an all-blittable signature like every one of these, and there's no
/// reason to open that door — DllImport marshals plain int/float/bool/out
/// float scalars with no unsafe code at all, which is what actually keeps
/// this plugin inside the kernel's "no unsafe in the v1 hot path" rule
/// (Directory.Build.props) instead of needing a narrow exception to it.
/// </summary>
internal static class Native
{
private const string Lib = "lingua_physics";
[DllImport(Lib)]
public static extern int Lingua_CreateWorld(float gravityX, float gravityY, float gravityZ);
[DllImport(Lib)]
public static extern void Lingua_DestroyWorld(int worldHandle);
[DllImport(Lib)]
public static extern void Lingua_WorldStep(int worldHandle, float timeStep, int subStepCount);
/// <summary>Total live bodies across every world — a test-only escape
/// hatch (see PhysicsWorldTests) to assert on the native side's own
/// bookkeeping, not just PhysicsWorld's C#-side dictionary.</summary>
[DllImport(Lib)]
public static extern int Lingua_GetBodyCount();
[DllImport(Lib)]
public static extern int Lingua_CreateBoxBody(
int worldHandle,
float px, float py, float pz,
float qx, float qy, float qz, float qw,
float halfWidth, float halfHeight, float halfDepth,
int bodyType, float density, float friction, float restitution);
[DllImport(Lib)]
public static extern int Lingua_CreateSphereBody(
int worldHandle,
float px, float py, float pz,
float qx, float qy, float qz, float qw,
float radius,
int bodyType, float density, float friction, float restitution);
[DllImport(Lib)]
public static extern void Lingua_DestroyBody(int bodyHandle);
[DllImport(Lib)]
public static extern void Lingua_GetBodyTransform(
int bodyHandle,
out float px, out float py, out float pz,
out float qx, out float qy, out float qz, out float qw);
[DllImport(Lib)]
public static extern void Lingua_SetBodyTransform(
int bodyHandle,
float px, float py, float pz,
float qx, float qy, float qz, float qw);
[DllImport(Lib)]
public static extern void Lingua_ApplyLinearImpulse(
int bodyHandle, float ix, float iy, float iz, [MarshalAs(UnmanagedType.U1)] bool wake);
[DllImport(Lib)]
public static extern void Lingua_GetLinearVelocity(int bodyHandle, out float vx, out float vy, out float vz);
[DllImport(Lib)]
public static extern void Lingua_SetLinearVelocity(int bodyHandle, float vx, float vy, float vz);
}