| | | 1 | | using System; |
| | | 2 | | using System.Collections.Generic; |
| | | 3 | | using Lattice.ECS.Core; |
| | | 4 | | |
| | | 5 | | namespace Lattice.ECS.Session |
| | | 6 | | { |
| | | 7 | | /// <summary> |
| | | 8 | | /// 很薄的 Session 运行时上下文。 |
| | | 9 | | /// 只承载显式声明的 runtime shared service 与稳定公开的运行元信息, |
| | | 10 | | /// 避免继续侵入 `Frame` 或退化成万能对象包。 |
| | | 11 | | /// </summary> |
| | | 12 | | public sealed class SessionRuntimeContext : IDisposable |
| | | 13 | | { |
| | 1 | 14 | | private static readonly Type? LegacySnapshotType = |
| | 1 | 15 | | typeof(Frame).Assembly.GetType("Lattice.ECS.Core." + "Frame" + "Snapshot", throwOnError: false); |
| | | 16 | | |
| | 1 | 17 | | private static readonly SessionRuntimeContextBoundary DefaultBoundary = new( |
| | 1 | 18 | | SessionRuntimeContextKind.TypedRuntimeSharedServices, |
| | 1 | 19 | | SessionRuntimeContextCapability.TypedSharedLookup | |
| | 1 | 20 | | SessionRuntimeContextCapability.ExplicitRuntimeSharedServiceRegistration | |
| | 1 | 21 | | SessionRuntimeContextCapability.OwnedSharedObjectLifetime | |
| | 1 | 22 | | SessionRuntimeContextCapability.RuntimeMetadataExposure, |
| | 1 | 23 | | UnsupportedSessionRuntimeContextCapability.ArbitraryObjectBag | |
| | 1 | 24 | | UnsupportedSessionRuntimeContextCapability.RollbackStateStorage | |
| | 1 | 25 | | UnsupportedSessionRuntimeContextCapability.GameplayStateExchange | |
| | 1 | 26 | | UnsupportedSessionRuntimeContextCapability.StringKeyedServiceLocator); |
| | | 27 | | |
| | | 28 | | private readonly SessionRuntime _runtime; |
| | 113 | 29 | | private readonly Dictionary<Type, object> _sharedObjects = new(); |
| | 113 | 30 | | private readonly HashSet<object> _ownedObjects = new(ReferenceEqualityComparer.Instance); |
| | | 31 | | private bool _disposed; |
| | | 32 | | |
| | 113 | 33 | | internal SessionRuntimeContext(SessionRuntime runtime, string runnerName) |
| | | 34 | | { |
| | 113 | 35 | | _runtime = runtime ?? throw new ArgumentNullException(nameof(runtime)); |
| | | 36 | | |
| | 113 | 37 | | if (string.IsNullOrWhiteSpace(runnerName)) |
| | | 38 | | { |
| | 0 | 39 | | throw new ArgumentException("Runner name cannot be null or whitespace.", nameof(runnerName)); |
| | | 40 | | } |
| | | 41 | | |
| | 113 | 42 | | RunnerName = runnerName; |
| | 113 | 43 | | } |
| | | 44 | | |
| | | 45 | | /// <summary> |
| | | 46 | | /// 当前运行时上下文边界。 |
| | | 47 | | /// </summary> |
| | 9 | 48 | | public SessionRuntimeContextBoundary Boundary => DefaultBoundary; |
| | | 49 | | |
| | | 50 | | /// <summary> |
| | | 51 | | /// 当前上下文所属的运行时实例。 |
| | | 52 | | /// </summary> |
| | 3 | 53 | | public SessionRuntime Runtime => _runtime; |
| | | 54 | | |
| | | 55 | | /// <summary> |
| | | 56 | | /// 当前运行器名称。 |
| | | 57 | | /// 对于未接入 runner 的独立 runtime,默认使用 runtime 类型名。 |
| | | 58 | | /// </summary> |
| | 158 | 59 | | public string RunnerName { get; private set; } |
| | | 60 | | |
| | | 61 | | /// <summary> |
| | | 62 | | /// 稳定公开的运行时参数。 |
| | | 63 | | /// </summary> |
| | 5 | 64 | | public SessionRuntimeOptions RuntimeOptions => _runtime.RuntimeOptions; |
| | | 65 | | |
| | | 66 | | /// <summary> |
| | | 67 | | /// 当前运行时类型。 |
| | | 68 | | /// </summary> |
| | 2 | 69 | | public SessionRuntimeKind RuntimeKind => _runtime.RuntimeKind; |
| | | 70 | | |
| | | 71 | | /// <summary> |
| | | 72 | | /// 当前运行时边界。 |
| | | 73 | | /// </summary> |
| | 2 | 74 | | public SessionRuntimeBoundary RuntimeBoundary => _runtime.RuntimeBoundary; |
| | | 75 | | |
| | | 76 | | /// <summary> |
| | | 77 | | /// 当前已注册的共享对象数量。 |
| | | 78 | | /// </summary> |
| | 2 | 79 | | 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 | | { |
| | 14 | 87 | | ThrowIfDisposed(); |
| | 14 | 88 | | ArgumentNullException.ThrowIfNull(instance); |
| | 14 | 89 | | ValidateSharedRegistration(instance); |
| | | 90 | | |
| | 10 | 91 | | Type type = typeof(T); |
| | 10 | 92 | | if (_sharedObjects.TryGetValue(type, out object? existing)) |
| | | 93 | | { |
| | 0 | 94 | | if (ReferenceEquals(existing, instance)) |
| | | 95 | | { |
| | 0 | 96 | | if (disposeWithContext) |
| | | 97 | | { |
| | 0 | 98 | | _ownedObjects.Add(instance); |
| | | 99 | | } |
| | | 100 | | |
| | 0 | 101 | | return; |
| | | 102 | | } |
| | | 103 | | |
| | 0 | 104 | | if (_ownedObjects.Remove(existing) && existing is IDisposable disposableExisting) |
| | | 105 | | { |
| | 0 | 106 | | disposableExisting.Dispose(); |
| | | 107 | | } |
| | | 108 | | } |
| | | 109 | | |
| | 10 | 110 | | _sharedObjects[type] = instance; |
| | 10 | 111 | | if (disposeWithContext) |
| | | 112 | | { |
| | 2 | 113 | | _ownedObjects.Add(instance); |
| | | 114 | | } |
| | 10 | 115 | | } |
| | | 116 | | |
| | | 117 | | /// <summary> |
| | | 118 | | /// 尝试获取一个按类型索引的共享对象。 |
| | | 119 | | /// </summary> |
| | | 120 | | public bool TryGetShared<T>(out T? instance) where T : class |
| | | 121 | | { |
| | 15 | 122 | | ThrowIfDisposed(); |
| | | 123 | | |
| | 15 | 124 | | if (_sharedObjects.TryGetValue(typeof(T), out object? value)) |
| | | 125 | | { |
| | 14 | 126 | | instance = (T)value; |
| | 14 | 127 | | return true; |
| | | 128 | | } |
| | | 129 | | |
| | 1 | 130 | | instance = null; |
| | 1 | 131 | | return false; |
| | | 132 | | } |
| | | 133 | | |
| | | 134 | | /// <summary> |
| | | 135 | | /// 获取一个必须存在的共享对象。 |
| | | 136 | | /// </summary> |
| | | 137 | | public T GetRequiredShared<T>() where T : class |
| | | 138 | | { |
| | 13 | 139 | | ThrowIfDisposed(); |
| | | 140 | | |
| | 13 | 141 | | if (TryGetShared<T>(out T? instance)) |
| | | 142 | | { |
| | 13 | 143 | | return instance!; |
| | | 144 | | } |
| | | 145 | | |
| | 0 | 146 | | 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 | | { |
| | 1 | 154 | | ThrowIfDisposed(); |
| | | 155 | | |
| | 1 | 156 | | Type type = typeof(T); |
| | 1 | 157 | | if (!_sharedObjects.Remove(type, out object? existing)) |
| | | 158 | | { |
| | 0 | 159 | | return false; |
| | | 160 | | } |
| | | 161 | | |
| | 1 | 162 | | if (_ownedObjects.Remove(existing) && existing is IDisposable disposable) |
| | | 163 | | { |
| | 0 | 164 | | disposable.Dispose(); |
| | | 165 | | } |
| | | 166 | | |
| | 1 | 167 | | return true; |
| | | 168 | | } |
| | | 169 | | |
| | | 170 | | internal void BindRunnerName(string runnerName) |
| | | 171 | | { |
| | 38 | 172 | | ThrowIfDisposed(); |
| | | 173 | | |
| | 38 | 174 | | if (string.IsNullOrWhiteSpace(runnerName)) |
| | | 175 | | { |
| | 0 | 176 | | throw new ArgumentException("Runner name cannot be null or whitespace.", nameof(runnerName)); |
| | | 177 | | } |
| | | 178 | | |
| | 38 | 179 | | RunnerName = runnerName; |
| | 38 | 180 | | } |
| | | 181 | | |
| | | 182 | | public void Dispose() |
| | | 183 | | { |
| | 111 | 184 | | if (_disposed) |
| | | 185 | | { |
| | 0 | 186 | | return; |
| | | 187 | | } |
| | | 188 | | |
| | 226 | 189 | | foreach (object owned in _ownedObjects) |
| | | 190 | | { |
| | 2 | 191 | | if (owned is IDisposable disposable) |
| | | 192 | | { |
| | 2 | 193 | | disposable.Dispose(); |
| | | 194 | | } |
| | | 195 | | } |
| | | 196 | | |
| | 111 | 197 | | _ownedObjects.Clear(); |
| | 111 | 198 | | _sharedObjects.Clear(); |
| | 111 | 199 | | _disposed = true; |
| | 111 | 200 | | } |
| | | 201 | | |
| | | 202 | | private static void ValidateSharedRegistration(object instance) |
| | | 203 | | { |
| | 14 | 204 | | Type instanceType = instance.GetType(); |
| | | 205 | | |
| | 14 | 206 | | if (!typeof(ISessionRuntimeSharedService).IsAssignableFrom(instanceType)) |
| | | 207 | | { |
| | 1 | 208 | | throw new ArgumentException( |
| | 1 | 209 | | $"Shared object type {instanceType.Name} must implement {nameof(ISessionRuntimeSharedService)} befor |
| | 1 | 210 | | nameof(instance)); |
| | | 211 | | } |
| | | 212 | | |
| | 13 | 213 | | if (IsDisallowedSharedType(instanceType)) |
| | | 214 | | { |
| | 3 | 215 | | throw new ArgumentException( |
| | 3 | 216 | | $"Shared object type {instanceType.Name} cannot be registered in {nameof(SessionRuntimeContext)} bec |
| | 3 | 217 | | nameof(instance)); |
| | | 218 | | } |
| | 10 | 219 | | } |
| | | 220 | | |
| | | 221 | | private static bool IsDisallowedSharedType(Type instanceType) |
| | | 222 | | { |
| | 13 | 223 | | return typeof(Frame).IsAssignableFrom(instanceType) || |
| | 13 | 224 | | (LegacySnapshotType != null && LegacySnapshotType.IsAssignableFrom(instanceType)) || |
| | 13 | 225 | | typeof(PackedFrameSnapshot).IsAssignableFrom(instanceType) || |
| | 13 | 226 | | typeof(SessionCheckpoint).IsAssignableFrom(instanceType) || |
| | 13 | 227 | | typeof(SessionRuntime).IsAssignableFrom(instanceType) || |
| | 13 | 228 | | typeof(SessionRuntimeContext).IsAssignableFrom(instanceType); |
| | | 229 | | } |
| | | 230 | | |
| | | 231 | | private void ThrowIfDisposed() |
| | | 232 | | { |
| | 81 | 233 | | if (_disposed) |
| | | 234 | | { |
| | 0 | 235 | | throw new ObjectDisposedException(nameof(SessionRuntimeContext)); |
| | | 236 | | } |
| | 81 | 237 | | } |
| | | 238 | | |
| | | 239 | | private sealed class ReferenceEqualityComparer : IEqualityComparer<object> |
| | | 240 | | { |
| | 114 | 241 | | public static ReferenceEqualityComparer Instance { get; } = new(); |
| | | 242 | | |
| | | 243 | | public new bool Equals(object? x, object? y) |
| | | 244 | | { |
| | 0 | 245 | | return ReferenceEquals(x, y); |
| | | 246 | | } |
| | | 247 | | |
| | | 248 | | public int GetHashCode(object obj) |
| | | 249 | | { |
| | 2 | 250 | | return System.Runtime.CompilerServices.RuntimeHelpers.GetHashCode(obj); |
| | | 251 | | } |
| | | 252 | | } |
| | | 253 | | } |
| | | 254 | | } |