2026-07-10 14:13:53 +01:00
using System ;
using System.IO ;
2026-07-12 12:33:36 +01:00
using System.Collections.Concurrent ;
2026-07-15 19:59:37 +01:00
using System.Collections.Generic ;
2026-08-17 22:43:10 +02:00
using System.Linq ;
2026-07-18 18:38:07 +01:00
using System.Threading.Tasks ;
2026-07-19 20:34:03 +01:00
using Stopwatch = System . Diagnostics . Stopwatch ;
2026-07-10 14:13:53 +01:00
using GameCult.Eve.Surface ;
2026-07-15 17:12:11 +01:00
using GameCult.Eve.PluginFields ;
using GameCult.Eve.UnityScene.Fields ;
2026-07-13 18:01:12 +01:00
using GameCult.Mesh ;
2026-07-10 14:13:53 +01:00
using UnityEngine ;
2026-08-17 21:02:01 +02:00
using UnityEngine.Serialization ;
2026-07-10 14:13:53 +01:00
#nullable enable
namespace GameCult.Eve.UnityScene
{
public sealed class EveUnityCultMeshPlayableWorldProvider :
MonoBehaviour ,
IEveUnitySceneProviderSurfaceDocumentSource ,
IEveUnitySceneProviderSurfaceDocumentConnection ,
IEveUnitySceneCommandSink ,
IEveUnitySceneCommandReceiptSource ,
IEveUnityProviderRefreshSource ,
2026-07-12 12:33:36 +01:00
IEveUnityEntitySoaViewDocumentSource ,
2026-07-15 17:12:11 +01:00
IEveUnityFieldsSplatsDocumentSource ,
2026-07-12 06:25:10 +01:00
IEveUnityCameraRenderPolicySource ,
2026-07-15 03:21:28 +01:00
IEveUnityNativeAssetProvider ,
2026-07-15 19:59:37 +01:00
IEveUnityNativeAssetMetadataProvider ,
2026-08-17 22:43:10 +02:00
IEveUnityInputCapabilitySource ,
IEveUnityNavigableProvider
2026-07-10 14:13:53 +01:00
{
[SerializeField] private string rendezvousEndpoint = "" ;
[SerializeField] private string providerFilter = "" ;
[SerializeField] private string surfaceFilter = "" ;
[SerializeField] private string verseFilter = "" ;
[SerializeField] private string surfaceKind = "interactive-world" ;
2026-08-17 21:02:01 +02:00
[FormerlySerializedAs("replicaPath")]
[SerializeField] private string cacheDirectory = "" ;
2026-07-10 14:13:53 +01:00
[SerializeField] private string runtimeId = "eve-unity" ;
private EveUnityCultMeshLiveProviderTransport ? _transport ;
private EveUnitySceneLiveProviderBridge ? _bridge ;
2026-07-18 18:38:07 +01:00
private Task ? _preparation ;
2026-07-13 18:01:12 +01:00
private readonly ConcurrentQueue < EntityViewLease > _pendingEntityViews = new ConcurrentQueue < EntityViewLease > ( ) ;
2026-07-15 17:12:11 +01:00
private readonly ConcurrentQueue < EveFieldsSplatsDocument > _pendingFields = new ConcurrentQueue < EveFieldsSplatsDocument > ( ) ;
2026-07-12 12:33:36 +01:00
2026-07-13 18:01:12 +01:00
public event Action < EveEntitySoaViewDocument , ICultMeshBodyReadLease > ? EntityViewAvailable ;
2026-07-10 14:13:53 +01:00
2026-07-15 17:12:11 +01:00
public event Action < EveFieldsSplatsDocument > ? FieldsSplatsAvailable ;
2026-07-10 14:13:53 +01:00
public EveUnityCultMeshProviderSelection ? Selection { get ; private set ; }
public string SinkKind = > Bridge . SinkKind ;
public EveUnitySceneProviderSurfaceDocument CurrentDocument = > Bridge . CurrentSurfaceDocument ;
2026-07-15 03:21:28 +01:00
public EveInputCapabilityDocument CurrentInputCapability
{
get
{
2026-07-18 18:38:07 +01:00
RequirePrepared ( ) ;
2026-07-15 03:21:28 +01:00
return _transport ! . CurrentInputCapability ;
}
}
2026-07-16 14:17:37 +01:00
public CultMeshBodyTransportKind ? CurrentAssetBodyTransportKind
{
get
{
2026-07-18 18:38:07 +01:00
RequirePrepared ( ) ;
2026-07-16 14:17:37 +01:00
return _transport ! . CurrentAssetBodyTransportKind ;
}
}
2026-07-20 04:58:49 +01:00
public event Action < EveUnitySceneProviderSurfaceDocument > ? DocumentAvailable ;
2026-07-10 14:13:53 +01:00
2026-07-20 04:58:49 +01:00
public event Action < EveUnitySceneCommandReceipt > ? ReceiptAvailable ;
2026-07-10 14:13:53 +01:00
public void Configure (
string endpoint ,
2026-08-17 21:02:01 +02:00
string localCacheDirectory = "" ,
2026-07-10 14:13:53 +01:00
string providerId = "" ,
string surfaceId = "" ,
string verseId = "" ,
string requiredSurfaceKind = "interactive-world" ,
string clientRuntimeId = "eve-unity" )
{
2026-07-18 18:38:07 +01:00
if ( _bridge ! = null | | _preparation ! = null )
2026-07-10 14:13:53 +01:00
throw new InvalidOperationException ( "Disconnect the active provider before changing discovery configuration." ) ;
rendezvousEndpoint = endpoint ? ? "" ;
2026-08-17 21:02:01 +02:00
cacheDirectory = localCacheDirectory ? ? "" ;
2026-07-10 14:13:53 +01:00
providerFilter = providerId ? ? "" ;
surfaceFilter = surfaceId ? ? "" ;
verseFilter = verseId ? ? "" ;
surfaceKind = string . IsNullOrWhiteSpace ( requiredSurfaceKind ) ? "interactive-world" : requiredSurfaceKind ;
runtimeId = string . IsNullOrWhiteSpace ( clientRuntimeId ) ? "eve-unity" : clientRuntimeId ;
}
public void Connect ( )
{
Bridge . Connect ( ) ;
}
2026-07-18 18:38:07 +01:00
public Task PrepareAsync ( )
{
if ( _bridge ! = null )
return Task . CompletedTask ;
if ( _preparation = = null | | _preparation . IsCanceled | | _preparation . IsFaulted )
_preparation = PrepareTransportAsync ( ) ;
return _preparation ;
}
2026-07-10 14:13:53 +01:00
public void Disconnect ( )
{
_bridge ? . Disconnect ( ) ;
}
2026-08-17 22:43:10 +02:00
public async Task NavigateAsync ( EveUnitySceneNavigationTarget target )
{
if ( target = = null ) throw new ArgumentNullException ( nameof ( target ) ) ;
if ( string . IsNullOrWhiteSpace ( target . VerseId ) )
throw new ArgumentException ( "Eve provider navigation requires a stable Verse identity." , nameof ( target ) ) ;
if ( string . IsNullOrWhiteSpace ( target . SurfaceId ) )
throw new ArgumentException ( "Eve provider navigation requires a surface identity." , nameof ( target ) ) ;
ReleaseTransport ( ) ;
var navigationEndpoint = target . RendezvousEndpoints
. FirstOrDefault ( endpoint = > ! string . IsNullOrWhiteSpace ( endpoint ) ) ;
if ( ! string . IsNullOrWhiteSpace ( navigationEndpoint ) )
rendezvousEndpoint = navigationEndpoint ;
verseFilter = target . VerseId ;
providerFilter = target . ProviderId ;
surfaceFilter = target . SurfaceId ;
surfaceKind = string . IsNullOrWhiteSpace ( target . SurfaceKind ) ? "interactive-world" : target . SurfaceKind ;
await PrepareAsync ( ) ;
}
2026-07-10 14:13:53 +01:00
public void Refresh ( )
{
2026-07-12 07:20:43 +01:00
if ( Bridge . IsConnected )
Bridge . Refresh ( ) ;
else
Bridge . Connect ( ) ;
2026-07-10 14:13:53 +01:00
}
public void Submit ( EveSurfaceCommandRequest request )
{
Bridge . Submit ( request ) ;
}
public GameObject ? ResolvePrefab ( EveUnityPlayableWorldAssetBinding asset )
{
2026-07-18 18:38:07 +01:00
RequirePrepared ( ) ;
2026-07-10 14:13:53 +01:00
return _transport ! . ResolvePrefab ( asset ) ;
}
2026-07-12 06:25:10 +01:00
public UnityEngine . Object ? ResolveAsset ( EveUnityPlayableWorldAssetBinding asset , Type assetType )
{
2026-07-18 18:38:07 +01:00
RequirePrepared ( ) ;
2026-07-12 06:25:10 +01:00
return _transport ! . ResolveAsset ( asset , assetType ) ;
}
2026-07-15 19:59:37 +01:00
public bool TryResolveAssetMetadata (
EveUnityPlayableWorldAssetBinding asset ,
out IReadOnlyDictionary < string , string > metadata )
{
2026-07-18 18:38:07 +01:00
RequirePrepared ( ) ;
2026-07-15 19:59:37 +01:00
return _transport ! . TryResolveAssetMetadata ( asset , out metadata ) ;
}
2026-07-13 21:35:11 +01:00
public bool TryGetRenderChannelLayer ( string channel , out int layer )
2026-07-12 06:25:10 +01:00
{
2026-07-18 18:38:07 +01:00
RequirePrepared ( ) ;
2026-07-13 21:35:11 +01:00
return _transport ! . TryGetRenderChannelLayer ( channel , out layer ) ;
2026-07-12 06:25:10 +01:00
}
2026-07-10 14:13:53 +01:00
private void OnDestroy ( )
2026-08-17 22:43:10 +02:00
{
ReleaseTransport ( ) ;
}
private void ReleaseTransport ( )
2026-07-10 14:13:53 +01:00
{
2026-07-20 04:58:49 +01:00
if ( _bridge ! = null )
{
_bridge . DocumentAvailable - = ForwardDocument ;
_bridge . ReceiptAvailable - = ForwardReceipt ;
}
2026-07-10 14:13:53 +01:00
_bridge ? . Dispose ( ) ;
_transport ? . Dispose ( ) ;
2026-07-13 18:01:12 +01:00
while ( _pendingEntityViews . TryDequeue ( out var pending ) ) pending . Lease . Dispose ( ) ;
2026-07-15 17:12:11 +01:00
while ( _pendingFields . TryDequeue ( out _ ) ) { }
2026-07-10 14:13:53 +01:00
_bridge = null ;
_transport = null ;
2026-07-18 18:38:07 +01:00
_preparation = null ;
2026-07-10 14:13:53 +01:00
Selection = null ;
}
2026-07-12 12:33:36 +01:00
private void Update ( )
{
2026-07-12 13:10:43 +01:00
_transport ? . PumpLiveEvents ( ) ;
2026-07-15 17:12:11 +01:00
EveFieldsSplatsDocument ? latestFields = null ;
while ( _pendingFields . TryDequeue ( out var fields ) )
latestFields = fields ;
if ( latestFields ! = null )
FieldsSplatsAvailable ? . Invoke ( latestFields ) ;
2026-07-13 18:01:12 +01:00
EntityViewLease ? latest = null ;
while ( _pendingEntityViews . TryDequeue ( out var next ) )
{
latest ? . Lease . Dispose ( ) ;
latest = next ;
}
2026-07-12 12:33:36 +01:00
if ( latest = = null ) return ;
2026-07-13 18:01:12 +01:00
var handler = EntityViewAvailable ;
if ( handler = = null ) latest . Lease . Dispose ( ) ;
else handler ( latest . Document , latest . Lease ) ;
2026-07-12 12:33:36 +01:00
}
2026-07-10 14:13:53 +01:00
private EveUnitySceneLiveProviderBridge Bridge
{
get
{
2026-07-18 18:38:07 +01:00
RequirePrepared ( ) ;
2026-07-10 14:13:53 +01:00
return _bridge ! ;
}
}
2026-07-18 18:38:07 +01:00
private async Task PrepareTransportAsync ( )
2026-07-10 14:13:53 +01:00
{
2026-07-19 20:34:03 +01:00
var elapsed = Stopwatch . StartNew ( ) ;
2026-07-10 14:13:53 +01:00
if ( _transport ! = null )
return ;
if ( string . IsNullOrWhiteSpace ( rendezvousEndpoint ) )
throw new InvalidOperationException ( "EveUnity requires a CultMesh rendezvous endpoint." ) ;
2026-07-18 18:38:07 +01:00
Selection = await new EveUnityCultMeshProviderDiscovery ( ) . DiscoverAsync (
2026-07-10 14:13:53 +01:00
rendezvousEndpoint ,
providerFilter ,
surfaceFilter ,
surfaceKind ,
verseFilter ) ;
2026-07-19 20:34:03 +01:00
TraceStartup ( $"discovery {elapsed.Elapsed.TotalMilliseconds:0.###}ms" ) ;
elapsed . Restart ( ) ;
2026-08-17 21:02:01 +02:00
var resolvedCachePath = string . IsNullOrWhiteSpace ( cacheDirectory )
? Path . Combine ( Application . temporaryCachePath , $"eve-unity-{GetInstanceID()}" )
: cacheDirectory ;
2026-07-10 14:13:53 +01:00
_transport = new EveUnityCultMeshLiveProviderTransport (
2026-08-17 21:02:01 +02:00
resolvedCachePath ,
Selection . RendezvousEndpoint ,
Selection . EndpointId ,
2026-07-10 14:13:53 +01:00
Selection . ProviderId ,
Selection . SurfaceId ,
runtimeId ) ;
2026-07-13 18:01:12 +01:00
_transport . EntityViewAvailable + = ( view , lease ) = > _pendingEntityViews . Enqueue ( new EntityViewLease ( view , lease ) ) ;
2026-07-15 17:12:11 +01:00
_transport . FieldsSplatsAvailable + = fields = > _pendingFields . Enqueue ( fields ) ;
2026-07-10 14:13:53 +01:00
_bridge = new EveUnitySceneLiveProviderBridge ( _transport ) ;
2026-07-20 04:58:49 +01:00
_bridge . DocumentAvailable + = ForwardDocument ;
_bridge . ReceiptAvailable + = ForwardReceipt ;
2026-07-19 20:34:03 +01:00
TraceStartup ( $"transport-construction {elapsed.Elapsed.TotalMilliseconds:0.###}ms" ) ;
}
2026-07-20 04:58:49 +01:00
private void ForwardDocument ( EveUnitySceneProviderSurfaceDocument document ) = >
DocumentAvailable ? . Invoke ( document ) ;
private void ForwardReceipt ( EveUnitySceneCommandReceipt receipt ) = >
ReceiptAvailable ? . Invoke ( receipt ) ;
2026-07-19 20:34:03 +01:00
private static void TraceStartup ( string message )
{
if ( string . Equals ( Environment . GetEnvironmentVariable ( "EVEUNITY_TRACE_STARTUP_PHASES" ) , "1" , StringComparison . Ordinal ) )
Debug . Log ( $"EveUnity startup phase {message}." ) ;
2026-07-10 14:13:53 +01:00
}
2026-07-13 18:01:12 +01:00
2026-07-18 18:38:07 +01:00
private void RequirePrepared ( )
{
if ( _transport = = null | | _bridge = = null )
throw new InvalidOperationException (
"The EveUnity CultMesh provider is not prepared. Await PrepareAsync before mounting the playable world." ) ;
}
2026-07-13 18:01:12 +01:00
private sealed class EntityViewLease
{
public EntityViewLease ( EveEntitySoaViewDocument document , ICultMeshBodyReadLease lease )
{
Document = document ;
Lease = lease ;
}
public EveEntitySoaViewDocument Document { get ; }
public ICultMeshBodyReadLease Lease { get ; }
}
2026-07-10 14:13:53 +01:00
}
}