2026-07-10 11:04:26 +01:00
using System ;
using System.Collections.Generic ;
using System.Linq ;
2026-08-18 10:08:03 +02:00
using System.Threading ;
2026-07-12 07:14:55 +01:00
using System.Threading.Tasks ;
2026-07-10 11:04:26 +01:00
using GameCult.Eve.Surface ;
using GameCult.Mesh ;
using GameCult.Networking ;
#nullable enable
namespace GameCult.Eve.UnityScene
{
public sealed class EveUnityCultMeshProviderSelection
{
public EveUnityCultMeshProviderSelection (
2026-08-17 21:02:01 +02:00
string rendezvousEndpoint ,
2026-07-10 11:04:26 +01:00
string verseId ,
2026-08-18 07:27:13 +02:00
string authorityRuntimeId ,
2026-07-10 11:04:26 +01:00
string providerId ,
string surfaceId ,
string surfaceKind )
{
2026-08-17 21:02:01 +02:00
RendezvousEndpoint = rendezvousEndpoint ? ? "" ;
2026-07-10 11:04:26 +01:00
VerseId = verseId ? ? "" ;
2026-08-18 07:27:13 +02:00
AuthorityRuntimeId = authorityRuntimeId ? ? "" ;
2026-07-10 11:04:26 +01:00
ProviderId = providerId ? ? "" ;
SurfaceId = surfaceId ? ? "" ;
SurfaceKind = surfaceKind ? ? "" ;
}
2026-08-17 21:02:01 +02:00
public string RendezvousEndpoint { get ; }
2026-07-10 11:04:26 +01:00
public string VerseId { get ; }
2026-08-18 07:27:13 +02:00
public string AuthorityRuntimeId { get ; }
2026-07-10 11:04:26 +01:00
public string ProviderId { get ; }
public string SurfaceId { get ; }
public string SurfaceKind { get ; }
}
public sealed class EveUnityCultMeshProviderDiscovery
{
2026-08-18 19:41:25 +02:00
private readonly CultMeshAuthorityTrustPolicy _authorityTrust ;
public EveUnityCultMeshProviderDiscovery ( CultMeshAuthorityTrustPolicy ? authorityTrust = null )
{
_authorityTrust = authorityTrust ? ? new CultMeshAuthorityTrustPolicy (
CultMeshAuthorityTrustMode . AuthenticatedRemote ) ;
}
2026-07-18 18:38:07 +01:00
public async Task < EveUnityCultMeshProviderSelection > DiscoverAsync (
2026-07-10 11:04:26 +01:00
string rendezvousEndpoint ,
string providerId = "" ,
string surfaceId = "" ,
string surfaceKind = "interactive-world" ,
2026-08-18 10:08:03 +02:00
string verseId = "" ,
2026-08-19 03:31:16 +02:00
CancellationToken cancellationToken = default ,
string requiredAuthorityRuntimeId = "" )
2026-07-10 11:04:26 +01:00
{
if ( string . IsNullOrWhiteSpace ( rendezvousEndpoint ) )
throw new ArgumentException ( "Rendezvous endpoint must be non-empty." , nameof ( rendezvousEndpoint ) ) ;
2026-07-19 19:35:49 +01:00
CultMeshVerseCatalogResponseMessage response ;
try
{
2026-08-18 20:14:02 +02:00
response = await CultMesh . CreateVerseDiscoveryClient ( EveUnityCultMeshConnectivity . Discovery ( ) ) . FetchAsync (
2026-07-10 11:04:26 +01:00
rendezvousEndpoint ,
new CultMeshVerseCatalogRequestMessage
{
VerseIds = string . IsNullOrWhiteSpace ( verseId ) ? null : new [ ] { verseId } ,
TransportVersion = "cultmesh.v0"
2026-07-18 18:38:07 +01:00
} ) ;
2026-08-18 10:08:03 +02:00
cancellationToken . ThrowIfCancellationRequested ( ) ;
2026-07-19 19:35:49 +01:00
}
catch ( Exception error )
{
throw new InvalidOperationException (
$"Could not query CultMesh rendezvous endpoint '{rendezvousEndpoint}'." ,
error ) ;
}
2026-07-10 11:04:26 +01:00
var candidates = response . Verses
. Where ( verse = > string . IsNullOrWhiteSpace ( verseId ) | |
string . Equals ( verse . VerseId , verseId , StringComparison . Ordinal ) )
2026-08-17 21:02:01 +02:00
. Where ( verse = > ! string . IsNullOrWhiteSpace ( verse . VerseId ) & &
( verse . DiscoveryEndpoints ? ? Array . Empty < string > ( ) ) . Any ( endpoint = >
! string . IsNullOrWhiteSpace ( endpoint ) ) )
. GroupBy ( verse = > verse . VerseId , StringComparer . Ordinal )
. Select ( group = > group . First ( ) )
2026-07-10 11:04:26 +01:00
. ToArray ( ) ;
if ( candidates . Length = = 0 )
throw new InvalidOperationException ( "The rendezvous endpoint advertised no compatible Verse endpoints." ) ;
var failures = new List < string > ( ) ;
2026-07-18 19:22:35 +01:00
var observed = new List < string > ( ) ;
2026-08-17 21:02:01 +02:00
using var mesh = new CultMeshClient ( new CultMeshClientOptions
{
2026-08-18 19:41:25 +02:00
RendezvousEndpoints = new [ ] { rendezvousEndpoint } ,
2026-08-18 20:14:02 +02:00
Discovery = EveUnityCultMeshConnectivity . Discovery ( ) ,
Sessions = new CultMeshSessionManagerOptions { Trust = _authorityTrust } ,
Connectors = EveUnityCultMeshConnectivity . SchemaConnectors ( ) ,
ContentConnectors = EveUnityCultMeshConnectivity . ContentConnectors ( )
2026-08-17 21:02:01 +02:00
} ) ;
2026-07-10 11:04:26 +01:00
foreach ( var candidate in candidates )
{
2026-08-19 03:31:16 +02:00
foreach ( var authorityRuntimeId in EligibleAuthorityRuntimeIds (
candidate . AuthorityRuntimeIds ,
requiredAuthorityRuntimeId ) )
2026-07-10 11:04:26 +01:00
{
2026-08-18 07:27:13 +02:00
try
{
var target = new CultMeshSessionTarget ( candidate . VerseId , authorityRuntimeId ) ;
using var advertisementsLease = await mesh
2026-08-18 10:08:03 +02:00
. LeaseCollectionAsync < EveProviderAdvertisementDocument > ( target , cancellationToken )
2026-08-18 07:27:13 +02:00
. ConfigureAwait ( false ) ;
var advertisements = await advertisementsLease . Handle . LatestAsync ( ) . ConfigureAwait ( false ) ;
observed . AddRange ( advertisements . Select ( document = >
$"{target}: {document.ProviderId}[{string.Join(" , ", document.Surfaces.Select(surface => $" { surface . SurfaceId } : { surface . SurfaceKind } "))}]" ) ) ;
var advertisement = advertisements
. Where ( document = > string . IsNullOrWhiteSpace ( providerId ) | |
string . Equals ( document . ProviderId , providerId , StringComparison . Ordinal ) )
. Select ( document = > new
{
Document = document ,
Surface = document . Surfaces . FirstOrDefault ( surface = >
( string . IsNullOrWhiteSpace ( surfaceId ) | |
string . Equals ( surface . SurfaceId , surfaceId , StringComparison . Ordinal ) ) & &
( string . IsNullOrWhiteSpace ( surfaceKind ) | |
string . Equals ( surface . SurfaceKind , surfaceKind , StringComparison . Ordinal ) ) )
} )
. FirstOrDefault ( match = > match . Surface ! = null ) ;
if ( advertisement ? . Surface = = null )
continue ;
2026-07-10 11:04:26 +01:00
2026-08-18 07:27:13 +02:00
return new EveUnityCultMeshProviderSelection (
rendezvousEndpoint ,
candidate . VerseId ,
authorityRuntimeId ,
advertisement . Document . ProviderId ,
advertisement . Surface . SurfaceId ,
advertisement . Surface . SurfaceKind ) ;
}
catch ( Exception error )
{
failures . Add ( $"{candidate.VerseId}/{authorityRuntimeId}: {error.Message}" ) ;
}
2026-07-10 11:04:26 +01:00
}
}
2026-08-19 03:31:16 +02:00
var filter = $"provider='{providerId}', surface='{surfaceId}', kind='{surfaceKind}', authority='{requiredAuthorityRuntimeId}'" ;
2026-07-10 11:04:26 +01:00
var detail = failures . Count = = 0 ? "" : $" Endpoint failures: {string.Join(" | ", failures)}" ;
2026-07-18 19:22:35 +01:00
var observedDetail = observed . Count = = 0 ? "" : $" Observed: {string.Join(" | ", observed)}" ;
throw new InvalidOperationException ( $"No advertised Eve surface matched {filter}.{detail}{observedDetail}" ) ;
2026-07-10 11:04:26 +01:00
}
2026-08-19 03:31:16 +02:00
internal static IReadOnlyList < string > EligibleAuthorityRuntimeIds (
IEnumerable < string > ? authorityRuntimeIds ,
string requiredAuthorityRuntimeId ) = >
( authorityRuntimeIds ? ? Array . Empty < string > ( ) )
. Where ( id = > ! string . IsNullOrWhiteSpace ( id ) )
. Where ( id = > string . IsNullOrWhiteSpace ( requiredAuthorityRuntimeId ) | |
string . Equals ( id , requiredAuthorityRuntimeId , StringComparison . Ordinal ) )
. Distinct ( StringComparer . Ordinal )
. OrderBy ( id = > id , StringComparer . Ordinal )
. ToArray ( ) ;
2026-07-10 11:04:26 +01:00
}
}