< Summary

Information
Class: Lattice.ECS.Session.SessionRuntimeContext
Assembly: Lattice
File(s): /home/runner/work/Sideline/Sideline/godot/scripts/lattice/ECS/Session/SessionRuntimeContext.cs
Line coverage
85%
Covered lines: 81
Uncovered lines: 14
Coverable lines: 95
Total lines: 254
Line coverage: 85.2%
Branch coverage
69%
Covered branches: 36
Total branches: 52
Branch coverage: 69.2%
Method coverage

Feature is only available for sponsors

Upgrade to PRO version

Metrics

MethodBranch coverage Crap Score Cyclomatic complexity Line coverage
.cctor()100%11100%
.ctor(...)50%4487.5%
get_Boundary()100%11100%
get_Runtime()100%11100%
get_RunnerName()100%11100%
get_RuntimeOptions()100%11100%
get_RuntimeKind()100%11100%
get_RuntimeBoundary()100%11100%
get_SharedObjectCount()100%11100%
SetShared(...)41.66%211260%
TryGetShared(...)100%22100%
GetRequiredShared()50%2275%
RemoveShared()50%7671.42%
BindRunnerName(...)50%2280%
Dispose()83.33%6688.88%
ValidateSharedRegistration(...)100%44100%
IsDisallowedSharedType(...)100%1212100%
ThrowIfDisposed()50%2266.66%
get_Instance()100%11100%
Equals(...)100%210%
GetHashCode(...)100%11100%

File(s)

/home/runner/work/Sideline/Sideline/godot/scripts/lattice/ECS/Session/SessionRuntimeContext.cs

#LineLine coverage
 1using System;
 2using System.Collections.Generic;
 3using Lattice.ECS.Core;
 4
 5namespace Lattice.ECS.Session
 6{
 7    /// <summary>
 8    /// 很薄的 Session 运行时上下文。
 9    /// 只承载显式声明的 runtime shared service 与稳定公开的运行元信息,
 10    /// 避免继续侵入 `Frame` 或退化成万能对象包。
 11    /// </summary>
 12    public sealed class SessionRuntimeContext : IDisposable
 13    {
 114        private static readonly Type? LegacySnapshotType =
 115            typeof(Frame).Assembly.GetType("Lattice.ECS.Core." + "Frame" + "Snapshot", throwOnError: false);
 16
 117        private static readonly SessionRuntimeContextBoundary DefaultBoundary = new(
 118            SessionRuntimeContextKind.TypedRuntimeSharedServices,
 119            SessionRuntimeContextCapability.TypedSharedLookup |
 120            SessionRuntimeContextCapability.ExplicitRuntimeSharedServiceRegistration |
 121            SessionRuntimeContextCapability.OwnedSharedObjectLifetime |
 122            SessionRuntimeContextCapability.RuntimeMetadataExposure,
 123            UnsupportedSessionRuntimeContextCapability.ArbitraryObjectBag |
 124            UnsupportedSessionRuntimeContextCapability.RollbackStateStorage |
 125            UnsupportedSessionRuntimeContextCapability.GameplayStateExchange |
 126            UnsupportedSessionRuntimeContextCapability.StringKeyedServiceLocator);
 27
 28        private readonly SessionRuntime _runtime;
 11329        private readonly Dictionary<Type, object> _sharedObjects = new();
 11330        private readonly HashSet<object> _ownedObjects = new(ReferenceEqualityComparer.Instance);
 31        private bool _disposed;
 32
 11333        internal SessionRuntimeContext(SessionRuntime runtime, string runnerName)
 34        {
 11335            _runtime = runtime ?? throw new ArgumentNullException(nameof(runtime));
 36
 11337            if (string.IsNullOrWhiteSpace(runnerName))
 38            {
 039                throw new ArgumentException("Runner name cannot be null or whitespace.", nameof(runnerName));
 40            }
 41
 11342            RunnerName = runnerName;
 11343        }
 44
 45        /// <summary>
 46        /// 当前运行时上下文边界。
 47        /// </summary>
 948        public SessionRuntimeContextBoundary Boundary => DefaultBoundary;
 49
 50        /// <summary>
 51        /// 当前上下文所属的运行时实例。
 52        /// </summary>
 353        public SessionRuntime Runtime => _runtime;
 54
 55        /// <summary>
 56        /// 当前运行器名称。
 57        /// 对于未接入 runner 的独立 runtime,默认使用 runtime 类型名。
 58        /// </summary>
 15859        public string RunnerName { get; private set; }
 60
 61        /// <summary>
 62        /// 稳定公开的运行时参数。
 63        /// </summary>
 564        public SessionRuntimeOptions RuntimeOptions => _runtime.RuntimeOptions;
 65
 66        /// <summary>
 67        /// 当前运行时类型。
 68        /// </summary>
 269        public SessionRuntimeKind RuntimeKind => _runtime.RuntimeKind;
 70
 71        /// <summary>
 72        /// 当前运行时边界。
 73        /// </summary>
 274        public SessionRuntimeBoundary RuntimeBoundary => _runtime.RuntimeBoundary;
 75
 76        /// <summary>
 77        /// 当前已注册的共享对象数量。
 78        /// </summary>
 279        public int SharedObjectCount => _sharedObjects.Count;
 80
 81        /// <summary>
 82        /// 写入或替换一个按类型索引的共享对象。
 83        /// 只有显式声明为 runtime shared service 的类型才允许注册。
 84        /// </summary>
 85        public void SetShared<T>(T instance, bool disposeWithContext = false) where T : class
 86        {
 1487            ThrowIfDisposed();
 1488            ArgumentNullException.ThrowIfNull(instance);
 1489            ValidateSharedRegistration(instance);
 90
 1091            Type type = typeof(T);
 1092            if (_sharedObjects.TryGetValue(type, out object? existing))
 93            {
 094                if (ReferenceEquals(existing, instance))
 95                {
 096                    if (disposeWithContext)
 97                    {
 098                        _ownedObjects.Add(instance);
 99                    }
 100
 0101                    return;
 102                }
 103
 0104                if (_ownedObjects.Remove(existing) && existing is IDisposable disposableExisting)
 105                {
 0106                    disposableExisting.Dispose();
 107                }
 108            }
 109
 10110            _sharedObjects[type] = instance;
 10111            if (disposeWithContext)
 112            {
 2113                _ownedObjects.Add(instance);
 114            }
 10115        }
 116
 117        /// <summary>
 118        /// 尝试获取一个按类型索引的共享对象。
 119        /// </summary>
 120        public bool TryGetShared<T>(out T? instance) where T : class
 121        {
 15122            ThrowIfDisposed();
 123
 15124            if (_sharedObjects.TryGetValue(typeof(T), out object? value))
 125            {
 14126                instance = (T)value;
 14127                return true;
 128            }
 129
 1130            instance = null;
 1131            return false;
 132        }
 133
 134        /// <summary>
 135        /// 获取一个必须存在的共享对象。
 136        /// </summary>
 137        public T GetRequiredShared<T>() where T : class
 138        {
 13139            ThrowIfDisposed();
 140
 13141            if (TryGetShared<T>(out T? instance))
 142            {
 13143                return instance!;
 144            }
 145
 0146            throw new InvalidOperationException($"Shared object of type {typeof(T).Name} is not registered.");
 147        }
 148
 149        /// <summary>
 150        /// 移除一个按类型索引的共享对象。
 151        /// </summary>
 152        public bool RemoveShared<T>() where T : class
 153        {
 1154            ThrowIfDisposed();
 155
 1156            Type type = typeof(T);
 1157            if (!_sharedObjects.Remove(type, out object? existing))
 158            {
 0159                return false;
 160            }
 161
 1162            if (_ownedObjects.Remove(existing) && existing is IDisposable disposable)
 163            {
 0164                disposable.Dispose();
 165            }
 166
 1167            return true;
 168        }
 169
 170        internal void BindRunnerName(string runnerName)
 171        {
 38172            ThrowIfDisposed();
 173
 38174            if (string.IsNullOrWhiteSpace(runnerName))
 175            {
 0176                throw new ArgumentException("Runner name cannot be null or whitespace.", nameof(runnerName));
 177            }
 178
 38179            RunnerName = runnerName;
 38180        }
 181
 182        public void Dispose()
 183        {
 111184            if (_disposed)
 185            {
 0186                return;
 187            }
 188
 226189            foreach (object owned in _ownedObjects)
 190            {
 2191                if (owned is IDisposable disposable)
 192                {
 2193                    disposable.Dispose();
 194                }
 195            }
 196
 111197            _ownedObjects.Clear();
 111198            _sharedObjects.Clear();
 111199            _disposed = true;
 111200        }
 201
 202        private static void ValidateSharedRegistration(object instance)
 203        {
 14204            Type instanceType = instance.GetType();
 205
 14206            if (!typeof(ISessionRuntimeSharedService).IsAssignableFrom(instanceType))
 207            {
 1208                throw new ArgumentException(
 1209                    $"Shared object type {instanceType.Name} must implement {nameof(ISessionRuntimeSharedService)} befor
 1210                    nameof(instance));
 211            }
 212
 13213            if (IsDisallowedSharedType(instanceType))
 214            {
 3215                throw new ArgumentException(
 3216                    $"Shared object type {instanceType.Name} cannot be registered in {nameof(SessionRuntimeContext)} bec
 3217                    nameof(instance));
 218            }
 10219        }
 220
 221        private static bool IsDisallowedSharedType(Type instanceType)
 222        {
 13223            return typeof(Frame).IsAssignableFrom(instanceType) ||
 13224                   (LegacySnapshotType != null && LegacySnapshotType.IsAssignableFrom(instanceType)) ||
 13225                   typeof(PackedFrameSnapshot).IsAssignableFrom(instanceType) ||
 13226                   typeof(SessionCheckpoint).IsAssignableFrom(instanceType) ||
 13227                   typeof(SessionRuntime).IsAssignableFrom(instanceType) ||
 13228                   typeof(SessionRuntimeContext).IsAssignableFrom(instanceType);
 229        }
 230
 231        private void ThrowIfDisposed()
 232        {
 81233            if (_disposed)
 234            {
 0235                throw new ObjectDisposedException(nameof(SessionRuntimeContext));
 236            }
 81237        }
 238
 239        private sealed class ReferenceEqualityComparer : IEqualityComparer<object>
 240        {
 114241            public static ReferenceEqualityComparer Instance { get; } = new();
 242
 243            public new bool Equals(object? x, object? y)
 244            {
 0245                return ReferenceEquals(x, y);
 246            }
 247
 248            public int GetHashCode(object obj)
 249            {
 2250                return System.Runtime.CompilerServices.RuntimeHelpers.GetHashCode(obj);
 251            }
 252        }
 253    }
 254}