feat(cordis): 接入上下文组合与模组事务热替换

This commit is contained in:
2026-08-16 23:20:40 +08:00
commit ad256f109b
676 changed files with 52168 additions and 0 deletions
@@ -0,0 +1,16 @@
# Changelog
本文件记录 `ShrinkCommand.Integration.EventBus` 在当前工作区中的包内变更。
## [0.1.1] - 2026-04-07
### Added
- 提供 `UseEventBusBridge(...)` 扩展,可把命令服务接到 EventBus。
- 提供 `RequestCommandAsync(...)` 扩展,支持通过事件请求式执行命令。
- 建立命令桥接事件:`ShrinkCommandExecuteRequestEvent``ShrinkCommandExecutingEvent``ShrinkCommandExecutedEvent``ShrinkCommandFailedEvent`
### Changed
- 桥接公共类型补齐 `#nullable enable`,统一可空语义。
- 请求结果字段统一使用 `ExecutionResult`,避免与 `EventBase.Result` 命名冲突。
@@ -0,0 +1,7 @@
fileFormatVersion: 2
guid: ca7f23ed84bf7aa409d36b858591a823
TextScriptImporter:
externalObjects: {}
userData:
assetBundleName:
assetBundleVariant:
@@ -0,0 +1,37 @@
# ShrinkCommand.Integration.EventBus
`ShrinkCommand``ShrinkEventBus` 的桥接层。
## 能力
- 通过事件请求执行命令
- 把命令执行过程发布为 EventBus 生命周期事件
- 保持 `ShrinkCommand` 核心不依赖 `ShrinkEventBus`
## 事件
- `ShrinkCommandExecuteRequestEvent`
- `ShrinkCommandExecutingEvent`
- `ShrinkCommandExecutedEvent`
- `ShrinkCommandFailedEvent`
## 接入
`ShrinkApp.Starter.Basic` 的 ContextLoader 组合根会注册 `ShrinkCommandEventBusComponent`:它注入
`shrink.service.command`,激活时接桥,依赖撤回时自动拆桥。独立使用本包时再显式注册:
```csharp
var service = new ShrinkCommandService();
service.AutoRegisterAll();
service.UseEventBusBridge(new ShrinkCommandEventBusBridgeOptions
{
ServiceName = "default",
DefaultSource = source
});
```
请求执行:
```csharp
var result = await ShrinkCommandEventBusBridge.RequestCommandAsync("help", source);
```
@@ -0,0 +1,7 @@
fileFormatVersion: 2
guid: f5b03f228e6ccef4e8225c62b96bd4a8
TextScriptImporter:
externalObjects: {}
userData:
assetBundleName:
assetBundleVariant:
@@ -0,0 +1,19 @@
{
"name": "ShrinkCommand.Integration.EventBus",
"rootNamespace": "ShrinkCommand.Integration",
"references": [
"ShrinkCommand.Runtime",
"ShrinkContext.Core.Runtime",
"ShrinkEventBus.Runtime",
"UniTask"
],
"optionalUnityReferences": [],
"includePlatforms": [],
"excludePlatforms": [],
"allowUnsafeCode": false,
"overrideReferences": false,
"autoReferenced": true,
"defineConstraints": [],
"versionDefines": [],
"noEngineReferences": false
}
@@ -0,0 +1,7 @@
fileFormatVersion: 2
guid: 63b0366d1e731824bb0900fbb820d543
AssemblyDefinitionImporter:
externalObjects: {}
userData:
assetBundleName:
assetBundleVariant:
@@ -0,0 +1,209 @@
#nullable enable
using System;
using System.Collections.Generic;
using Cysharp.Threading.Tasks;
using ShrinkEventBus;
namespace ShrinkCommand.Integration
{
public static class ShrinkCommandEventBusBridge
{
private sealed class ServiceRegistration
{
public string ServiceName = ShrinkCommandConstants.DefaultServiceName;
public ShrinkCommandService Service = null!;
public IShrinkCommandSource? DefaultSource;
public Action<ShrinkCommandExecutingInfo>? ExecutingHandler;
public Action<ShrinkCommandExecutedInfo>? ExecutedHandler;
}
private static readonly object SyncRoot = new();
private static readonly Dictionary<string, ServiceRegistration> RegisteredByName = new(StringComparer.OrdinalIgnoreCase);
private static readonly Dictionary<ShrinkCommandService, ServiceRegistration> RegisteredByService = new();
private static bool _initialized;
[UnityEngine.RuntimeInitializeOnLoadMethod(UnityEngine.RuntimeInitializeLoadType.SubsystemRegistration)]
private static void ResetStaticState()
{
lock (SyncRoot)
{
foreach (var registration in RegisteredByService.Values)
{
if (registration.ExecutingHandler != null)
registration.Service.OnCommandExecuting -= registration.ExecutingHandler;
if (registration.ExecutedHandler != null)
registration.Service.OnCommandExecuted -= registration.ExecutedHandler;
}
RegisteredByService.Clear();
RegisteredByName.Clear();
_initialized = false;
}
}
public static void RegisterService(
ShrinkCommandService service,
ShrinkCommandEventBusBridgeOptions? options = null)
{
if (service == null)
throw new ArgumentNullException(nameof(service));
EnsureInitialized();
options ??= new ShrinkCommandEventBusBridgeOptions();
lock (SyncRoot)
{
if (RegisteredByService.TryGetValue(service, out var existing))
{
existing.DefaultSource = options.DefaultSource;
if (!string.Equals(existing.ServiceName, options.ServiceName, StringComparison.OrdinalIgnoreCase))
{
RegisteredByName.Remove(existing.ServiceName);
existing.ServiceName = NormalizeServiceName(options.ServiceName);
RegisteredByName[existing.ServiceName] = existing;
}
return;
}
var registration = new ServiceRegistration
{
ServiceName = NormalizeServiceName(options.ServiceName),
Service = service,
DefaultSource = options.DefaultSource
};
registration.ExecutingHandler = info => PublishExecuting(registration, info);
registration.ExecutedHandler = info => PublishExecuted(registration, info);
service.OnCommandExecuting += registration.ExecutingHandler;
service.OnCommandExecuted += registration.ExecutedHandler;
RegisteredByService[service] = registration;
RegisteredByName[registration.ServiceName] = registration;
}
}
public static void UnregisterService(ShrinkCommandService service)
{
if (service == null)
return;
lock (SyncRoot)
{
if (!RegisteredByService.TryGetValue(service, out var registration))
return;
if (registration.ExecutingHandler != null)
service.OnCommandExecuting -= registration.ExecutingHandler;
if (registration.ExecutedHandler != null)
service.OnCommandExecuted -= registration.ExecutedHandler;
RegisteredByService.Remove(service);
RegisteredByName.Remove(registration.ServiceName);
}
}
public static async UniTask<ShrinkCommandExecutionResult> RequestCommandAsync(
string rawInput,
IShrinkCommandSource? source = null,
string? serviceName = null)
{
var requestEvent = new ShrinkCommandExecuteRequestEvent
{
ServiceName = NormalizeServiceName(serviceName),
RawInput = rawInput ?? string.Empty,
Source = source
};
await EventBus.TriggerEventAsync(requestEvent);
return requestEvent.ExecutionResult ?? ShrinkCommandExecutionResult.Failure("没有命令桥处理该请求。");
}
private static void EnsureInitialized()
{
if (_initialized)
return;
_initialized = true;
EventBus.RegisterEvent<ShrinkCommandExecuteRequestEvent>(HandleExecuteRequestAsync, EventPriority.LOWEST);
}
private static async UniTask HandleExecuteRequestAsync(ShrinkCommandExecuteRequestEvent eventArgs)
{
if (eventArgs == null || eventArgs.IsHandled || eventArgs.IsCanceled)
return;
ServiceRegistration? registration;
lock (SyncRoot)
{
RegisteredByName.TryGetValue(NormalizeServiceName(eventArgs.ServiceName), out registration);
}
if (registration == null)
{
eventArgs.IsHandled = true;
eventArgs.ExecutionResult = ShrinkCommandExecutionResult.Failure(
$"未找到命令服务: {NormalizeServiceName(eventArgs.ServiceName)}");
return;
}
var source = eventArgs.Source ?? registration.DefaultSource;
if (source == null)
{
eventArgs.IsHandled = true;
eventArgs.ExecutionResult = ShrinkCommandExecutionResult.Failure("命令请求缺少来源对象。");
return;
}
eventArgs.ExecutionResult = await registration.Service.ExecuteAsync(source, eventArgs.RawInput);
eventArgs.IsHandled = true;
}
private static void PublishExecuting(ServiceRegistration registration, ShrinkCommandExecutingInfo info)
{
EventBus.TriggerEvent(new ShrinkCommandExecutingEvent
{
ServiceName = registration.ServiceName,
RawInput = info.RawInput,
CommandPath = info.Command.Path,
SourceId = info.Source.SourceId,
SourceDisplayName = info.Source.DisplayName,
Arguments = new Dictionary<string, string>(info.Arguments, StringComparer.OrdinalIgnoreCase)
});
}
private static void PublishExecuted(ServiceRegistration registration, ShrinkCommandExecutedInfo info)
{
var commandPath = info.Command?.Path ?? string.Empty;
var arguments = new Dictionary<string, string>(info.Arguments, StringComparer.OrdinalIgnoreCase);
EventBus.TriggerEvent(new ShrinkCommandExecutedEvent
{
ServiceName = registration.ServiceName,
RawInput = info.RawInput,
CommandPath = commandPath,
SourceId = info.Source.SourceId,
SourceDisplayName = info.Source.DisplayName,
Arguments = arguments,
IsSuccess = info.Result.IsSuccess,
Message = info.Result.Message
});
if (info.Result.IsSuccess)
return;
EventBus.TriggerEvent(new ShrinkCommandFailedEvent
{
ServiceName = registration.ServiceName,
RawInput = info.RawInput,
CommandPath = commandPath,
SourceId = info.Source.SourceId,
SourceDisplayName = info.Source.DisplayName,
Arguments = arguments,
ErrorMessage = info.Result.Message
});
}
private static string NormalizeServiceName(string? serviceName)
{
return string.IsNullOrWhiteSpace(serviceName) ? ShrinkCommandConstants.DefaultServiceName : serviceName.Trim();
}
}
}
@@ -0,0 +1,11 @@
fileFormatVersion: 2
guid: de2d897c6d6fa1f419101a2c04ad57e1
MonoImporter:
externalObjects: {}
serializedVersion: 2
defaultReferences: []
executionOrder: 0
icon: {instanceID: 0}
userData:
assetBundleName:
assetBundleVariant:
@@ -0,0 +1,49 @@
#nullable enable
using System.Collections.Generic;
using ShrinkEventBus;
namespace ShrinkCommand.Integration
{
[Cancelable]
public sealed class ShrinkCommandExecuteRequestEvent : EventBase
{
public string ServiceName { get; set; } = ShrinkCommandConstants.DefaultServiceName;
public string RawInput { get; set; } = string.Empty;
public IShrinkCommandSource? Source { get; set; }
public bool IsHandled { get; set; }
public ShrinkCommandExecutionResult? ExecutionResult { get; set; }
}
public sealed class ShrinkCommandExecutingEvent : EventBase
{
public string ServiceName { get; set; } = ShrinkCommandConstants.DefaultServiceName;
public string RawInput { get; set; } = string.Empty;
public string CommandPath { get; set; } = string.Empty;
public string SourceId { get; set; } = string.Empty;
public string SourceDisplayName { get; set; } = string.Empty;
public Dictionary<string, string> Arguments { get; set; } = new();
}
public sealed class ShrinkCommandExecutedEvent : EventBase
{
public string ServiceName { get; set; } = ShrinkCommandConstants.DefaultServiceName;
public string RawInput { get; set; } = string.Empty;
public string CommandPath { get; set; } = string.Empty;
public string SourceId { get; set; } = string.Empty;
public string SourceDisplayName { get; set; } = string.Empty;
public Dictionary<string, string> Arguments { get; set; } = new();
public bool IsSuccess { get; set; }
public string Message { get; set; } = string.Empty;
}
public sealed class ShrinkCommandFailedEvent : EventBase
{
public string ServiceName { get; set; } = ShrinkCommandConstants.DefaultServiceName;
public string RawInput { get; set; } = string.Empty;
public string CommandPath { get; set; } = string.Empty;
public string SourceId { get; set; } = string.Empty;
public string SourceDisplayName { get; set; } = string.Empty;
public Dictionary<string, string> Arguments { get; set; } = new();
public string ErrorMessage { get; set; } = string.Empty;
}
}
@@ -0,0 +1,11 @@
fileFormatVersion: 2
guid: 7f8b986a535559d41a2ab05a20708b28
MonoImporter:
externalObjects: {}
serializedVersion: 2
defaultReferences: []
executionOrder: 0
icon: {instanceID: 0}
userData:
assetBundleName:
assetBundleVariant:
@@ -0,0 +1,29 @@
#nullable enable
using System;
using Cysharp.Threading.Tasks;
namespace ShrinkCommand.Integration
{
public static class ShrinkCommandEventBusBridgeExtensions
{
public static ShrinkCommandService UseEventBusBridge(
this ShrinkCommandService service,
ShrinkCommandEventBusBridgeOptions? options = null)
{
if (service == null)
throw new ArgumentNullException(nameof(service));
ShrinkCommandEventBusBridge.RegisterService(service, options);
return service;
}
public static UniTask<ShrinkCommandExecutionResult> RequestCommandAsync(
this ShrinkCommandService service,
string rawInput,
IShrinkCommandSource? source = null,
string? serviceName = null)
{
return ShrinkCommandEventBusBridge.RequestCommandAsync(rawInput, source, serviceName);
}
}
}
@@ -0,0 +1,11 @@
fileFormatVersion: 2
guid: b0ed31dba396b6d4ab075e75a16b00d5
MonoImporter:
externalObjects: {}
serializedVersion: 2
defaultReferences: []
executionOrder: 0
icon: {instanceID: 0}
userData:
assetBundleName:
assetBundleVariant:
@@ -0,0 +1,10 @@
#nullable enable
namespace ShrinkCommand.Integration
{
public sealed class ShrinkCommandEventBusBridgeOptions
{
public string ServiceName { get; set; } = ShrinkCommandConstants.DefaultServiceName;
public IShrinkCommandSource? DefaultSource { get; set; }
}
}
@@ -0,0 +1,11 @@
fileFormatVersion: 2
guid: c6de8286e0b74a54398432828ae503fb
MonoImporter:
externalObjects: {}
serializedVersion: 2
defaultReferences: []
executionOrder: 0
icon: {instanceID: 0}
userData:
assetBundleName:
assetBundleVariant:
@@ -0,0 +1,36 @@
#nullable enable
using System.Collections.Generic;
using Cysharp.Threading.Tasks;
using ShrinkContext;
namespace ShrinkCommand.Integration
{
/// <summary>通过命令服务键管理 Command ↔ EventBus 桥的可逆生命周期。</summary>
public sealed class ShrinkCommandEventBusComponent : IShrinkComponent
{
public const string CommandServiceKey = "shrink.service.command";
public const string ProvideKey = "shrink.integration.command-eventbus";
private static readonly string[] InjectKeys = { CommandServiceKey };
private static readonly string[] ProvideKeys = { ProvideKey };
public string Name => "shrink.integration.command-eventbus";
public IReadOnlyList<string> Inject => InjectKeys;
public IReadOnlyList<string> Provide => ProvideKeys;
public UniTask ApplyAsync(ShrinkCtx ctx, object? config)
{
var service = ctx.Get<ShrinkCommandService>(CommandServiceKey);
var options = config as ShrinkCommandEventBusBridgeOptions;
ShrinkCommandEventBusBridge.RegisterService(service, options);
ctx.EffectInverse(() =>
{
ShrinkCommandEventBusBridge.UnregisterService(service);
return UniTask.CompletedTask;
});
ctx.Set(ProvideKey, Name);
return UniTask.CompletedTask;
}
}
}
@@ -0,0 +1,11 @@
fileFormatVersion: 2
guid: 078a765d611231c478ac77312b44c444
MonoImporter:
externalObjects: {}
serializedVersion: 2
defaultReferences: []
executionOrder: 0
icon: {instanceID: 0}
userData:
assetBundleName:
assetBundleVariant:
@@ -0,0 +1,17 @@
{
"name": "com.cneicy.shrink-command-integration-eventbus",
"version": "0.1.1",
"displayName": "ShrinkCommand - EventBus Integration",
"description": "ShrinkCommand 与 ShrinkEventBus 的桥接层,支持事件请求执行命令,以及命令执行生命周期事件发布。",
"unity": "2022.3",
"dependencies": {
"com.cneicy.shrink-command": "0.1.0",
"com.cneicy.shrink-context-core": "0.1.0",
"com.cneicy.shrink-eventbus": "1.1.5"
},
"keywords": ["command", "eventbus", "integration", "bridge"],
"author": {
"name": "cneicy",
"url": "https://github.com/cneicy"
}
}
@@ -0,0 +1,7 @@
fileFormatVersion: 2
guid: 63114b97f96b400469a77b9701d8ffbc
TextScriptImporter:
externalObjects: {}
userData:
assetBundleName:
assetBundleVariant: