2026-07-10 02:27:47 +01:00
using System ;
using System.Collections.Generic ;
using UnityEngine ;
#nullable enable
namespace GameCult.Eve.UnityScene
{
public interface IEveUnityGameObjectAssetProvider
{
GameObject ? ResolvePrefab ( EveUnityPlayableWorldAssetBinding asset ) ;
}
2026-07-12 06:25:10 +01:00
public interface IEveUnityNativeAssetProvider : IEveUnityGameObjectAssetProvider
{
UnityEngine . Object ? ResolveAsset ( EveUnityPlayableWorldAssetBinding asset , Type assetType ) ;
}
2026-07-10 02:27:47 +01:00
public sealed class EveUnityGameObjectPlayableWorldSceneSink : IEveUnityPlayableWorldSceneSink
{
private readonly Transform _root ;
private readonly IEveUnityGameObjectAssetProvider _assetProvider ;
private readonly Dictionary < string , GameObject > _instances = new Dictionary < string , GameObject > ( StringComparer . Ordinal ) ;
2026-07-13 05:33:45 +01:00
private string _subjectEntityId = "" ;
private bool _subjectVisible = true ;
2026-07-10 02:27:47 +01:00
public EveUnityGameObjectPlayableWorldSceneSink (
Transform root ,
IEveUnityGameObjectAssetProvider assetProvider )
{
_root = root ! = null ? root : throw new ArgumentNullException ( nameof ( root ) ) ;
_assetProvider = assetProvider ? ? throw new ArgumentNullException ( nameof ( assetProvider ) ) ;
}
public int ActiveEntityCount = > _instances . Count ;
public void ConfigureWorld ( EveUnityPlayableWorldProjection world )
{
if ( world = = null ) throw new ArgumentNullException ( nameof ( world ) ) ;
if ( ! string . IsNullOrWhiteSpace ( world . WorldRootId ) )
_root . name = world . WorldRootId ;
2026-07-13 05:33:45 +01:00
_subjectEntityId = world . PlayerEntityId ;
_subjectVisible = world . SubjectVisible ;
ApplySubjectVisibility ( ) ;
2026-07-10 02:27:47 +01:00
}
public void UpsertEntity ( EveUnityPlayableWorldEntity entity , EveUnityPlayableWorldAssetBinding asset )
{
if ( entity = = null ) throw new ArgumentNullException ( nameof ( entity ) ) ;
if ( asset = = null ) throw new ArgumentNullException ( nameof ( asset ) ) ;
if ( string . IsNullOrWhiteSpace ( entity . EntityId ) )
return ;
GameObject instance ;
if ( ! _instances . TryGetValue ( entity . EntityId , out instance ) | | instance = = null )
{
instance = InstantiateEntity ( entity , asset ) ;
_instances [ entity . EntityId ] = instance ;
}
else
{
var marker = instance . GetComponent < EveUnityPlayableWorldEntityMarker > ( ) ;
if ( marker ! = null & & ! string . Equals ( marker . AssetRef , asset . AssetRef , StringComparison . Ordinal ) )
{
DestroyInstance ( instance ) ;
instance = InstantiateEntity ( entity , asset ) ;
_instances [ entity . EntityId ] = instance ;
}
}
ApplyEntity ( instance , entity , asset ) ;
2026-07-13 05:33:45 +01:00
ApplySubjectVisibility ( instance , entity . EntityId ) ;
2026-07-10 02:27:47 +01:00
}
public void RemoveEntity ( string entityId )
{
if ( string . IsNullOrWhiteSpace ( entityId ) )
return ;
GameObject instance ;
if ( ! _instances . TryGetValue ( entityId , out instance ) )
return ;
_instances . Remove ( entityId ) ;
if ( instance ! = null )
DestroyInstance ( instance ) ;
}
private GameObject InstantiateEntity ( EveUnityPlayableWorldEntity entity , EveUnityPlayableWorldAssetBinding asset )
{
var prefab = _assetProvider . ResolvePrefab ( asset ) ;
2026-07-10 06:00:36 +01:00
GameObject instance ;
if ( prefab ! = null )
{
instance = new GameObject ( entity . EntityId ) ;
instance . transform . SetParent ( _root , false ) ;
var visual = UnityEngine . Object . Instantiate ( prefab , instance . transform ) ;
visual . transform . localPosition = Vector3 . zero ;
visual . transform . localRotation = Quaternion . identity ;
}
else
{
instance = GameObject . CreatePrimitive ( PrimitiveType . Capsule ) ;
}
2026-07-10 02:27:47 +01:00
if ( instance . transform . parent ! = _root )
instance . transform . SetParent ( _root , false ) ;
return instance ;
}
private static void ApplyEntity (
GameObject instance ,
EveUnityPlayableWorldEntity entity ,
EveUnityPlayableWorldAssetBinding asset )
{
instance . name = string . IsNullOrWhiteSpace ( entity . Label )
? entity . EntityId
: $"{entity.EntityId} ({entity.Label})" ;
instance . transform . localPosition = new Vector3 ( entity . PositionX , entity . PositionY , entity . PositionZ ) ;
instance . transform . localRotation = Quaternion . Euler ( 0f , entity . RotationY , 0f ) ;
2026-07-11 17:01:57 +01:00
if ( entity . Radius > 0f & &
string . Equals ( asset . PresentationKind , "unity-generated-placeholder" , StringComparison . Ordinal ) )
2026-07-10 02:27:47 +01:00
instance . transform . localScale = Vector3 . one * entity . Radius ;
var marker = instance . GetComponent < EveUnityPlayableWorldEntityMarker > ( ) ;
if ( marker = = null )
marker = instance . AddComponent < EveUnityPlayableWorldEntityMarker > ( ) ;
marker . Apply ( entity , asset ) ;
2026-07-12 02:21:59 +01:00
if ( entity . Props . TryGetValue ( "presentationState" , out var presentationState ) & &
! string . IsNullOrWhiteSpace ( presentationState ) )
{
var semanticPresentation = instance . GetComponent < EveUnitySemanticEntityPresentation > ( ) ;
if ( semanticPresentation = = null )
semanticPresentation = instance . AddComponent < EveUnitySemanticEntityPresentation > ( ) ;
semanticPresentation . Apply ( entity . Props ) ;
}
2026-07-10 02:27:47 +01:00
}
private static void DestroyInstance ( GameObject instance )
{
if ( Application . isPlaying )
UnityEngine . Object . Destroy ( instance ) ;
else
UnityEngine . Object . DestroyImmediate ( instance ) ;
}
2026-07-13 05:33:45 +01:00
private void ApplySubjectVisibility ( )
{
foreach ( var pair in _instances )
ApplySubjectVisibility ( pair . Value , pair . Key ) ;
}
private void ApplySubjectVisibility ( GameObject instance , string entityId )
{
if ( instance = = null | | ! string . Equals ( entityId , _subjectEntityId , StringComparison . Ordinal ) )
return ;
instance . SetActive ( _subjectVisible ) ;
}
2026-07-10 02:27:47 +01:00
}
2026-07-12 02:21:59 +01:00
public sealed class EveUnitySemanticEntityPresentation : MonoBehaviour
{
private static readonly int Emission = Shader . PropertyToID ( "_Emission" ) ;
private static readonly int EmissionColor = Shader . PropertyToID ( "_EmissionColor" ) ;
private MaterialPropertyBlock ? _properties ;
private Renderer [ ] _renderers = Array . Empty < Renderer > ( ) ;
private string _state = "" ;
private float _activePulseSeconds = 1f ;
private float _triggeredPulseSeconds = 0.25f ;
private float _activeEmission = 1f ;
private float _triggeredEmission = 4f ;
public string PresentationState = > _state ;
public void Apply ( IReadOnlyDictionary < string , string > props )
{
props ? ? = new Dictionary < string , string > ( StringComparer . Ordinal ) ;
_state = Read ( props , "presentationState" ) ;
_activePulseSeconds = Positive ( ReadFloat ( props , "activePulseSeconds" , _activePulseSeconds ) , 1f ) ;
_triggeredPulseSeconds = Positive ( ReadFloat ( props , "triggeredPulseSeconds" , _triggeredPulseSeconds ) , 0.25f ) ;
_activeEmission = Math . Max ( 0f , ReadFloat ( props , "activeEmission" , _activeEmission ) ) ;
_triggeredEmission = Math . Max ( 0f , ReadFloat ( props , "triggeredEmission" , _triggeredEmission ) ) ;
_renderers = GetComponentsInChildren < Renderer > ( true ) ;
ApplyAt ( Time . time ) ;
}
public void ApplyAt ( float timeSeconds )
{
var triggered = string . Equals ( _state , "triggered" , StringComparison . Ordinal ) ;
var active = triggered | | string . Equals ( _state , "active" , StringComparison . Ordinal ) ;
var period = triggered ? _triggeredPulseSeconds : _activePulseSeconds ;
var peak = triggered ? _triggeredEmission : _activeEmission ;
var envelope = active
? Mathf . Lerp ( 0.1f , 1f , Mathf . Abs ( Mathf . Cos ( Mathf . PI * timeSeconds / period ) ) )
: 0f ;
var value = peak * envelope ;
_properties ? ? = new MaterialPropertyBlock ( ) ;
foreach ( var renderer in _renderers )
{
if ( renderer = = null ) continue ;
renderer . GetPropertyBlock ( _properties ) ;
_properties . SetFloat ( Emission , value ) ;
_properties . SetColor ( EmissionColor , Color . white * value ) ;
renderer . SetPropertyBlock ( _properties ) ;
}
}
private void Update ( ) = > ApplyAt ( Time . time ) ;
private static string Read ( IReadOnlyDictionary < string , string > props , string key ) = >
props . TryGetValue ( key , out var value ) ? value ? ? "" : "" ;
private static float ReadFloat ( IReadOnlyDictionary < string , string > props , string key , float fallback ) = >
props . TryGetValue ( key , out var value ) & & float . TryParse ( value ,
System . Globalization . NumberStyles . Float ,
System . Globalization . CultureInfo . InvariantCulture ,
out var parsed )
? parsed
: fallback ;
private static float Positive ( float value , float fallback ) = > value > 0f ? value : fallback ;
}
2026-07-12 06:25:10 +01:00
public sealed class EveUnityResourcesAssetProvider : IEveUnityNativeAssetProvider
2026-07-10 02:27:47 +01:00
{
public GameObject ? ResolvePrefab ( EveUnityPlayableWorldAssetBinding asset )
{
if ( asset = = null ) throw new ArgumentNullException ( nameof ( asset ) ) ;
var resourcesPath = EveUnityPlayableWorldAssetManifestEntry . NormalizeResourcesPath ( asset . AssetRef ) ;
return string . IsNullOrWhiteSpace ( resourcesPath )
? null
: Resources . Load < GameObject > ( resourcesPath ) ;
}
2026-07-12 06:25:10 +01:00
public UnityEngine . Object ? ResolveAsset ( EveUnityPlayableWorldAssetBinding asset , Type assetType )
{
if ( asset = = null ) throw new ArgumentNullException ( nameof ( asset ) ) ;
if ( assetType = = null ) throw new ArgumentNullException ( nameof ( assetType ) ) ;
var resourcesPath = EveUnityPlayableWorldAssetManifestEntry . NormalizeResourcesPath ( asset . AssetRef ) ;
return string . IsNullOrWhiteSpace ( resourcesPath ) ? null : Resources . Load ( resourcesPath , assetType ) ;
}
2026-07-10 02:27:47 +01:00
}
public sealed class EveUnityPlayableWorldEntityMarker : MonoBehaviour
{
public string EntityId = "" ;
public string NodeId = "" ;
public string EntityKind = "" ;
public string Label = "" ;
public string Faction = "" ;
public string AssetRef = "" ;
public string PresentationKind = "" ;
public bool Selectable ;
public bool Controllable ;
public void Apply ( EveUnityPlayableWorldEntity entity , EveUnityPlayableWorldAssetBinding asset )
{
if ( entity = = null ) throw new ArgumentNullException ( nameof ( entity ) ) ;
if ( asset = = null ) throw new ArgumentNullException ( nameof ( asset ) ) ;
EntityId = entity . EntityId ;
NodeId = entity . NodeId ;
EntityKind = entity . EntityKind ;
Label = entity . Label ;
Faction = entity . Faction ;
AssetRef = asset . AssetRef ;
PresentationKind = asset . PresentationKind ;
Selectable = entity . Selectable ;
Controllable = entity . Controllable ;
}
}
}