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-07-18 18:38:07 +01:00
using System.Threading.Tasks ;
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 ;
#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-07-15 03:21:28 +01:00
IEveUnityInputCapabilitySource
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" ;
[SerializeField] private string replicaPath = "" ;
[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-10 14:13:53 +01:00
public event Action < EveUnitySceneProviderSurfaceDocument > ? DocumentAvailable
{
add { Bridge . DocumentAvailable + = value ; }
remove { Bridge . DocumentAvailable - = value ; }
}
public event Action < EveUnitySceneCommandReceipt > ? ReceiptAvailable
{
add { Bridge . ReceiptAvailable + = value ; }
remove { Bridge . ReceiptAvailable - = value ; }
}
public void Configure (
string endpoint ,
string replicaStorePath = "" ,
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 ? ? "" ;
replicaPath = replicaStorePath ? ? "" ;
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 ( ) ;
}
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 ( )
{
_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
{
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 ) ;
var resolvedReplicaPath = string . IsNullOrWhiteSpace ( replicaPath )
? Path . Combine ( Application . temporaryCachePath , $"eve-unity-{GetInstanceID()}.cc" )
: replicaPath ;
_transport = new EveUnityCultMeshLiveProviderTransport (
resolvedReplicaPath ,
Selection . Endpoint ,
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-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
}
}