Add revisioned retained Box3D queries
This commit is contained in:
parent
014c95c454
commit
d854d9376f
6 changed files with 199 additions and 12 deletions
|
|
@ -112,6 +112,50 @@ public sealed record YmirSessionStepResult(
|
|||
long StepIndex,
|
||||
IReadOnlyList<YmirContactFact> ContactFacts);
|
||||
|
||||
public sealed record YmirSessionCircleCastQuery(
|
||||
string QueryId,
|
||||
long ExpectedRevision,
|
||||
Vec2 Origin,
|
||||
Vec2 Direction,
|
||||
float Distance,
|
||||
float Radius,
|
||||
IReadOnlyList<string> CandidateBodyIds);
|
||||
|
||||
public sealed record YmirSessionCircleOverlapQuery(
|
||||
string QueryId,
|
||||
long ExpectedRevision,
|
||||
Vec2 Center,
|
||||
float Radius,
|
||||
IReadOnlyList<string> CandidateBodyIds);
|
||||
|
||||
public enum YmirQueryError
|
||||
{
|
||||
None,
|
||||
StaleRevision,
|
||||
UnknownBody,
|
||||
InvalidQuery,
|
||||
SessionDisposed,
|
||||
SessionFaulted
|
||||
}
|
||||
|
||||
public sealed record YmirSessionCircleCastResult(
|
||||
string QueryId,
|
||||
string SessionId,
|
||||
string SessionGeneration,
|
||||
long ObservedRevision,
|
||||
long ObservedStepIndex,
|
||||
YmirQueryError Error,
|
||||
IReadOnlyList<CircleCastHit> Hits);
|
||||
|
||||
public sealed record YmirSessionCircleOverlapResult(
|
||||
string QueryId,
|
||||
string SessionId,
|
||||
string SessionGeneration,
|
||||
long ObservedRevision,
|
||||
long ObservedStepIndex,
|
||||
YmirQueryError Error,
|
||||
IReadOnlyList<CircleOverlapHit> Hits);
|
||||
|
||||
public sealed record YmirSessionInfo(
|
||||
string SessionId,
|
||||
string SessionGeneration,
|
||||
|
|
|
|||
|
|
@ -409,6 +409,59 @@ public sealed class YmirSession : IDisposable
|
|||
}
|
||||
}
|
||||
|
||||
public YmirSessionCircleCastResult CastCircle(YmirSessionCircleCastQuery query)
|
||||
{
|
||||
ArgumentNullException.ThrowIfNull(query);
|
||||
lock (_gate)
|
||||
{
|
||||
var error = QueryPreflight(query.QueryId, query.ExpectedRevision, query.CandidateBodyIds, out var bodies);
|
||||
if (error != YmirQueryError.None)
|
||||
{
|
||||
return new YmirSessionCircleCastResult(
|
||||
query.QueryId ?? "", _sessionId, _sessionGeneration, _revision, _stepIndex, error, []);
|
||||
}
|
||||
|
||||
try
|
||||
{
|
||||
var result = YmirQueries.CastCircle(new CircleCastQueryRequest(
|
||||
query.Origin, query.Direction, query.Distance, query.Radius, bodies));
|
||||
return new YmirSessionCircleCastResult(
|
||||
query.QueryId, _sessionId, _sessionGeneration, _revision, _stepIndex, YmirQueryError.None, result.Hits);
|
||||
}
|
||||
catch (ArgumentException)
|
||||
{
|
||||
return new YmirSessionCircleCastResult(
|
||||
query.QueryId ?? "", _sessionId, _sessionGeneration, _revision, _stepIndex, YmirQueryError.InvalidQuery, []);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
public YmirSessionCircleOverlapResult OverlapCircle(YmirSessionCircleOverlapQuery query)
|
||||
{
|
||||
ArgumentNullException.ThrowIfNull(query);
|
||||
lock (_gate)
|
||||
{
|
||||
var error = QueryPreflight(query.QueryId, query.ExpectedRevision, query.CandidateBodyIds, out var bodies);
|
||||
if (error != YmirQueryError.None)
|
||||
{
|
||||
return new YmirSessionCircleOverlapResult(
|
||||
query.QueryId ?? "", _sessionId, _sessionGeneration, _revision, _stepIndex, error, []);
|
||||
}
|
||||
|
||||
try
|
||||
{
|
||||
var result = YmirQueries.OverlapCircle(new CircleOverlapQueryRequest(query.Center, query.Radius, bodies));
|
||||
return new YmirSessionCircleOverlapResult(
|
||||
query.QueryId, _sessionId, _sessionGeneration, _revision, _stepIndex, YmirQueryError.None, result.Hits);
|
||||
}
|
||||
catch (ArgumentException)
|
||||
{
|
||||
return new YmirSessionCircleOverlapResult(
|
||||
query.QueryId ?? "", _sessionId, _sessionGeneration, _revision, _stepIndex, YmirQueryError.InvalidQuery, []);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
public YmirCommandReceipt Dispose(YmirDisposeSessionCommand command)
|
||||
{
|
||||
ArgumentNullException.ThrowIfNull(command);
|
||||
|
|
@ -458,6 +511,48 @@ public sealed class YmirSession : IDisposable
|
|||
});
|
||||
}
|
||||
|
||||
private YmirQueryError QueryPreflight(
|
||||
string? queryId,
|
||||
long expectedRevision,
|
||||
IReadOnlyList<string>? candidateBodyIds,
|
||||
out PhysicsBody[] bodies)
|
||||
{
|
||||
bodies = [];
|
||||
if (_faulted)
|
||||
{
|
||||
return YmirQueryError.SessionFaulted;
|
||||
}
|
||||
if (_disposed)
|
||||
{
|
||||
return YmirQueryError.SessionDisposed;
|
||||
}
|
||||
if (string.IsNullOrWhiteSpace(queryId) || expectedRevision < 0 || candidateBodyIds == null)
|
||||
{
|
||||
return YmirQueryError.InvalidQuery;
|
||||
}
|
||||
if (expectedRevision != _revision)
|
||||
{
|
||||
return YmirQueryError.StaleRevision;
|
||||
}
|
||||
|
||||
var seen = new HashSet<string>(StringComparer.Ordinal);
|
||||
var selected = new List<PhysicsBody>(candidateBodyIds.Count);
|
||||
foreach (var bodyId in candidateBodyIds)
|
||||
{
|
||||
if (string.IsNullOrWhiteSpace(bodyId) || !seen.Add(bodyId))
|
||||
{
|
||||
return YmirQueryError.InvalidQuery;
|
||||
}
|
||||
if (!_bodies.TryGetValue(bodyId, out var body))
|
||||
{
|
||||
return YmirQueryError.UnknownBody;
|
||||
}
|
||||
selected.Add(body);
|
||||
}
|
||||
bodies = selected.ToArray();
|
||||
return YmirQueryError.None;
|
||||
}
|
||||
|
||||
private IReadOnlyList<YmirContactFact> ProjectFacts(IReadOnlyList<Box3DContactFact> nativeFacts)
|
||||
{
|
||||
var projected = new List<ProjectedFact>(nativeFacts.Count);
|
||||
|
|
|
|||
Loading…
Reference in a new issue