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.Network` 在当前工作区中的包内变更。
## [0.1.0] - 2026-04-07
### Added
- 增加 `command/execute` RPC 桥接。
- 远程会话可自动映射为 `IShrinkCommandSource`,统一走命令运行时。
- 命令执行结果可直接通过 `ShrinkNetwork` 返回给远端调用方。
### Changed
- 桥接层公共类型补齐 `#nullable enable`,对齐当前运行时与宿主侧的可空语义。
- 保持桥接层职责边界,命令核心仍不直接依赖网络实现。
@@ -0,0 +1,7 @@
fileFormatVersion: 2
guid: cc0d137c46b092f49a54bc3afcd7c161
TextScriptImporter:
externalObjects: {}
userData:
assetBundleName:
assetBundleVariant:
@@ -0,0 +1,24 @@
# ShrinkCommand.Integration.Network
`ShrinkCommand``ShrinkNetwork` 的桥接层。
## 能力
- 通过网络 RPC 执行命令
- 远程命令来源自动映射为 `IShrinkCommandSource`
- 继续复用命令系统自己的权限与来源限制
## 接入
```csharp
commandService = new ShrinkCommandService();
commandService.AutoRegisterAll();
networkService.UseCommandBridge(commandService);
```
客户端调用:
```csharp
var response = await session.ExecuteCommandAsync("help");
```
@@ -0,0 +1,7 @@
fileFormatVersion: 2
guid: c85d4974b8207cf45848e1379ad7baa3
TextScriptImporter:
externalObjects: {}
userData:
assetBundleName:
assetBundleVariant:
@@ -0,0 +1,19 @@
{
"name": "ShrinkCommand.Integration.Network",
"rootNamespace": "ShrinkCommand.Integration",
"references": [
"ShrinkCommand.Runtime",
"ShrinkNetwork.Runtime",
"ShrinkContext.Core.Runtime",
"UniTask"
],
"optionalUnityReferences": [],
"includePlatforms": [],
"excludePlatforms": [],
"allowUnsafeCode": false,
"overrideReferences": false,
"autoReferenced": true,
"defineConstraints": [],
"versionDefines": [],
"noEngineReferences": false
}
@@ -0,0 +1,7 @@
fileFormatVersion: 2
guid: d9c305f415a0515409b2795a809e7240
AssemblyDefinitionImporter:
externalObjects: {}
userData:
assetBundleName:
assetBundleVariant:
@@ -0,0 +1,49 @@
#nullable enable
using System;
using System.Collections.Generic;
using Cysharp.Threading.Tasks;
using ShrinkCommand;
using ShrinkContext;
using ShrinkNetwork;
namespace ShrinkCommand.Integration
{
/// <summary>
/// 命令-网络集成组件(论文 6.5 集成组件模式):
/// 核心(command / network)互不依赖,本组件注入双方服务键,全部就绪才激活,
/// 任一提供者退役即自动停用——依赖接线由响应式余效应结构性完成,替代手工桥接编排。
///
/// 已知边界:ShrinkNetworkService 当前无 handler 注销 API,停用不撤销已注册的
/// command/execute 处理器(传输解绑后不可达;网络服务实例重建时自然消失)。
/// </summary>
public sealed class ShrinkCommandNetworkIntegrationComponent : IShrinkComponent
{
/// <summary>与 ShrinkNetworkAppComponent.ServiceKey 保持一致(避免跨包硬引用)。</summary>
public const string NetworkServiceKey = "shrink.service.network";
public const string CommandServiceKey = "shrink.service.command";
public const string ProvideKey = "shrink.integration.command-network";
private static readonly string[] InjectKeys = { CommandServiceKey, NetworkServiceKey };
private static readonly string[] ProvideKeys = { ProvideKey };
public string Name => "shrink.integration.command-network";
public IReadOnlyList<string> Inject => InjectKeys;
public IReadOnlyList<string> Provide => ProvideKeys;
public UniTask ApplyAsync(ShrinkCtx ctx, object? config)
{
var commandService = ctx.Get<ShrinkCommandService>(CommandServiceKey);
var networkService = ctx.Get<ShrinkNetworkService>(NetworkServiceKey);
var options = config as ShrinkNetworkCommandBridgeOptions;
ShrinkNetworkCommandBridge.RegisterService(networkService, commandService, options);
ctx.Set(ProvideKey, ShrinkNetworkCommandBridge.ExecuteRoute);
// 桥注册按网络服务实例幂等去重;可逆面为集成键的发布/撤回
return UniTask.CompletedTask;
}
}
}
@@ -0,0 +1,11 @@
fileFormatVersion: 2
guid: c5710d88a762cde489282366255f755c
MonoImporter:
externalObjects: {}
serializedVersion: 2
defaultReferences: []
executionOrder: 0
icon: {instanceID: 0}
userData:
assetBundleName:
assetBundleVariant:
@@ -0,0 +1,86 @@
#nullable enable
using System;
using System.Collections.Generic;
using Cysharp.Threading.Tasks;
using ShrinkNetwork;
namespace ShrinkCommand.Integration
{
public static class ShrinkNetworkCommandBridge
{
public const string ExecuteRoute = "command/execute";
private static readonly object SyncRoot = new();
private static readonly HashSet<ShrinkNetworkService> RegisteredServices = new();
public static void RegisterService(
ShrinkNetworkService networkService,
ShrinkCommandService commandService,
ShrinkNetworkCommandBridgeOptions? options = null)
{
if (networkService == null)
throw new ArgumentNullException(nameof(networkService));
if (commandService == null)
throw new ArgumentNullException(nameof(commandService));
lock (SyncRoot)
{
if (RegisteredServices.Contains(networkService))
return;
RegisteredServices.Add(networkService);
}
options ??= new ShrinkNetworkCommandBridgeOptions();
EnsureMessagesRegistered(networkService);
var requirement = new ShrinkNetworkPermissionRequirement(options.Authority, options.Permission);
networkService.RegisterRequestHandler<ShrinkNetworkCommandRequest, ShrinkNetworkCommandResponse>(
(context, request) => HandleExecuteAsync(commandService, options, context, request),
requirement);
}
public static void EnsureMessagesRegistered(ShrinkNetworkService service)
{
if (!service.MessageRegistry.TryGetMeta(typeof(ShrinkNetworkCommandRequest), out _))
{
var requestAttribute = (ShrinkNetworkMessageAttribute)Attribute.GetCustomAttribute(
typeof(ShrinkNetworkCommandRequest),
typeof(ShrinkNetworkMessageAttribute),
false)!;
service.RegisterMessage(typeof(ShrinkNetworkCommandRequest), requestAttribute.Opcode, requestAttribute.Route);
}
if (!service.MessageRegistry.TryGetMeta(typeof(ShrinkNetworkCommandResponse), out _))
{
var responseAttribute = (ShrinkNetworkMessageAttribute)Attribute.GetCustomAttribute(
typeof(ShrinkNetworkCommandResponse),
typeof(ShrinkNetworkMessageAttribute),
false)!;
service.RegisterMessage(typeof(ShrinkNetworkCommandResponse), responseAttribute.Opcode, responseAttribute.Route);
}
}
private static async UniTask<ShrinkNetworkCommandResponse> HandleExecuteAsync(
ShrinkCommandService commandService,
ShrinkNetworkCommandBridgeOptions options,
ShrinkNetworkContext context,
ShrinkNetworkCommandRequest request)
{
var source = options.SourceFactory?.Invoke(context, request) ?? new ShrinkNetworkCommandSource(context, request);
var result = await commandService.ExecuteAsync(source, request.CommandLine);
var output = source is ShrinkNetworkCommandSource networkSource
? networkSource.BuildOutputMessage(result.Message)
: result.Message;
return new ShrinkNetworkCommandResponse
{
ServiceName = string.IsNullOrWhiteSpace(request.ServiceName) ? options.ServiceName : request.ServiceName.Trim(),
CommandLine = request.CommandLine ?? string.Empty,
OutputMessage = output,
ErrorCode = result.IsSuccess ? 0 : ShrinkRpcErrorCode.HandlerException,
ErrorMessage = result.IsSuccess ? string.Empty : output
};
}
}
}
@@ -0,0 +1,11 @@
fileFormatVersion: 2
guid: f22899cbeb3779d4eb1338217fa4d38f
MonoImporter:
externalObjects: {}
serializedVersion: 2
defaultReferences: []
executionOrder: 0
icon: {instanceID: 0}
userData:
assetBundleName:
assetBundleVariant:
@@ -0,0 +1,52 @@
#nullable enable
using System;
using Cysharp.Threading.Tasks;
using ShrinkNetwork;
namespace ShrinkCommand.Integration
{
public static class ShrinkNetworkCommandBridgeExtensions
{
public static ShrinkNetworkService UseCommandBridge(
this ShrinkNetworkService networkService,
ShrinkCommandService commandService,
ShrinkNetworkCommandBridgeOptions? options = null)
{
if (networkService == null)
throw new ArgumentNullException(nameof(networkService));
if (commandService == null)
throw new ArgumentNullException(nameof(commandService));
ShrinkNetworkCommandBridge.RegisterService(networkService, commandService, options);
return networkService;
}
public static async UniTask<ShrinkNetworkCommandResponse> ExecuteCommandAsync(
this ShrinkNetworkSession session,
string commandLine,
string? serviceName = null,
string? clientLabel = null,
ShrinkRpcCallOptions? options = null)
{
if (session == null)
throw new ArgumentNullException(nameof(session));
ShrinkNetworkCommandBridge.EnsureMessagesRegistered(session.Service);
return await session.RpcAsync<ShrinkNetworkCommandRequest, ShrinkNetworkCommandResponse>(
new ShrinkNetworkCommandRequest
{
ServiceName = string.IsNullOrWhiteSpace(serviceName)
? ShrinkCommandConstants.DefaultServiceName
: serviceName.Trim(),
CommandLine = commandLine ?? string.Empty,
ClientLabel = clientLabel ?? string.Empty
},
options ?? new ShrinkRpcCallOptions
{
TimeoutMs = 5000,
DebugLabel = "RemoteCommand"
});
}
}
}
@@ -0,0 +1,11 @@
fileFormatVersion: 2
guid: 11bf32dbf7be0e14cbddf1f03ded5cbb
MonoImporter:
externalObjects: {}
serializedVersion: 2
defaultReferences: []
executionOrder: 0
icon: {instanceID: 0}
userData:
assetBundleName:
assetBundleVariant:
@@ -0,0 +1,15 @@
#nullable enable
using System;
using ShrinkNetwork;
namespace ShrinkCommand.Integration
{
public sealed class ShrinkNetworkCommandBridgeOptions
{
public string ServiceName { get; set; } = ShrinkCommandConstants.DefaultServiceName;
public ShrinkNetworkAuthority Authority { get; set; } = ShrinkNetworkAuthority.ClientOnly;
public string Permission { get; set; } = string.Empty;
public Func<ShrinkNetworkContext, ShrinkNetworkCommandRequest, IShrinkCommandSource>? SourceFactory { get; set; }
}
}
@@ -0,0 +1,11 @@
fileFormatVersion: 2
guid: 174745a3cab12534c8d4b09788063fe9
MonoImporter:
externalObjects: {}
serializedVersion: 2
defaultReferences: []
executionOrder: 0
icon: {instanceID: 0}
userData:
assetBundleName:
assetBundleVariant:
@@ -0,0 +1,20 @@
using ShrinkNetwork;
namespace ShrinkCommand.Integration
{
[ShrinkNetworkMessage(1501, ShrinkNetworkCommandBridge.ExecuteRoute)]
public sealed class ShrinkNetworkCommandRequest : IShrinkNetworkRequest
{
public string ServiceName { get; set; } = ShrinkCommandConstants.DefaultServiceName;
public string CommandLine { get; set; } = string.Empty;
public string ClientLabel { get; set; } = string.Empty;
}
[ShrinkNetworkMessage(1502, "command/execute_response")]
public sealed class ShrinkNetworkCommandResponse : ShrinkRpcResponseBase
{
public string ServiceName { get; set; } = ShrinkCommandConstants.DefaultServiceName;
public string CommandLine { get; set; } = string.Empty;
public string OutputMessage { get; set; } = string.Empty;
}
}
@@ -0,0 +1,11 @@
fileFormatVersion: 2
guid: 8b44977ad838cce438d99ea565848485
MonoImporter:
externalObjects: {}
serializedVersion: 2
defaultReferences: []
executionOrder: 0
icon: {instanceID: 0}
userData:
assetBundleName:
assetBundleVariant:
@@ -0,0 +1,79 @@
using System;
using System.Collections.Generic;
using System.Text;
using System.Threading;
using Cysharp.Threading.Tasks;
using ShrinkNetwork;
namespace ShrinkCommand.Integration
{
public sealed class ShrinkNetworkCommandSource : IShrinkCommandSource
{
private readonly List<string> _lines = new();
private readonly ShrinkNetworkContext _context;
private readonly ShrinkNetworkCommandRequest _request;
public ShrinkNetworkCommandSource(ShrinkNetworkContext context, ShrinkNetworkCommandRequest request)
{
_context = context;
_request = request;
SourceId = $"network:{context.Session.SessionId}";
DisplayName = ResolveDisplayName(context, request);
}
public string SourceId { get; }
public string DisplayName { get; }
public bool IsConsole => false;
public ShrinkNetworkSession Session => _context.Session;
public bool HasPermission(string permission)
{
return string.IsNullOrWhiteSpace(permission) || Session.HasPermission(permission);
}
public UniTask WriteLineAsync(string message, CancellationToken cancellationToken = default)
{
if (!string.IsNullOrWhiteSpace(message))
_lines.Add(message);
return UniTask.CompletedTask;
}
public string BuildOutputMessage(string resultMessage)
{
if (_lines.Count == 0)
return resultMessage ?? string.Empty;
var builder = new StringBuilder();
for (var index = 0; index < _lines.Count; index++)
{
if (index > 0)
builder.AppendLine();
builder.Append(_lines[index]);
}
if (!string.IsNullOrWhiteSpace(resultMessage))
{
if (builder.Length > 0 && !string.Equals(_lines[_lines.Count - 1], resultMessage, StringComparison.Ordinal))
builder.AppendLine();
if (!string.Equals(_lines[_lines.Count - 1], resultMessage, StringComparison.Ordinal))
builder.Append(resultMessage);
}
return builder.ToString();
}
private static string ResolveDisplayName(ShrinkNetworkContext context, ShrinkNetworkCommandRequest request)
{
if (context.Session.Items.TryGetValue("auth.name", out var authName) && authName is string authNameText &&
!string.IsNullOrWhiteSpace(authNameText))
{
return authNameText.Trim();
}
if (!string.IsNullOrWhiteSpace(request.ClientLabel))
return request.ClientLabel.Trim();
return context.Session.RemoteAddress;
}
}
}
@@ -0,0 +1,11 @@
fileFormatVersion: 2
guid: bf31a6f53f1bb0c4ca0af60b92dce81f
MonoImporter:
externalObjects: {}
serializedVersion: 2
defaultReferences: []
executionOrder: 0
icon: {instanceID: 0}
userData:
assetBundleName:
assetBundleVariant:
@@ -0,0 +1,8 @@
fileFormatVersion: 2
guid: a9777f2e6be37f64e8ec2df9b9afef87
folderAsset: yes
DefaultImporter:
externalObjects: {}
userData:
assetBundleName:
assetBundleVariant:
@@ -0,0 +1,99 @@
#nullable enable
using System.Collections.Generic;
using Cysharp.Threading.Tasks;
using NUnit.Framework;
using ShrinkCommand.Integration;
using ShrinkCommand.Integration.App;
using ShrinkContext;
using ShrinkDataSaver.Integration.App;
using ShrinkNetwork;
using ShrinkNetwork.Integration.App;
namespace ShrinkCommand.Integration.Network.Tests
{
/// <summary>
/// 阶段 3 集成组件模式(论文 6.5):
/// 核心(command/network/datasaver)互不依赖,集成组件注入双方服务键;
/// 全部提供者就绪才激活,任一退役即停用——依赖接线由响应式余效应完成。
/// </summary>
public class IntegrationComponentsTests
{
private ShrinkContextRuntime _runtime = null!;
[SetUp]
public void SetUp()
{
_runtime = new ShrinkContextRuntime();
}
[TearDown]
public void TearDown()
{
_runtime.ShutdownAsync().GetAwaiter().GetResult();
}
private static void Run(UniTask task) => task.GetAwaiter().GetResult();
[Test]
public void Integration_ActivatesOnlyWhenBothProvidersActive()
{
var integration = _runtime.Use(new ShrinkCommandNetworkIntegrationComponent());
Assert.AreEqual(ShrinkFiberState.Inactive, integration.State, "两个服务键都缺失时等待");
var command = _runtime.Use(new ShrinkCommandAppComponent());
Assert.AreEqual(ShrinkFiberState.Active, command.State);
Assert.AreEqual(ShrinkFiberState.Inactive, integration.State, "仅命令服务就绪仍等待网络服务");
var network = _runtime.Use(new ShrinkNetworkAppComponent());
Assert.AreEqual(ShrinkFiberState.Active, network.State);
Assert.AreEqual(ShrinkFiberState.Active, integration.State,
"双键齐备后集成组件激活并完成桥注册");
}
[Test]
public void EitherProviderRetire_DeactivatesIntegration_AndReactivatesOnReturn()
{
var command = _runtime.Use(new ShrinkCommandAppComponent());
var network = _runtime.Use(new ShrinkNetworkAppComponent());
var integration = _runtime.Use(new ShrinkCommandNetworkIntegrationComponent());
Assert.AreEqual(ShrinkFiberState.Active, integration.State);
Run(_runtime.RetireAsync(network));
Assert.AreEqual(ShrinkFiberState.Inactive, integration.State,
"网络提供者退役,集成组件自动停用(服务键撤回触发)");
Assert.AreEqual(ShrinkFiberState.Active, command.State, "命令核心不受影响");
var networkAgain = _runtime.Use(new ShrinkNetworkAppComponent());
Assert.AreEqual(ShrinkFiberState.Active, integration.State,
"提供者回归后集成组件自动重新激活");
}
[Test]
public void CommandComponent_ProvidesDefaultServiceKey()
{
var fiber = _runtime.Use(new ShrinkCommandAppComponent());
Assert.AreEqual(ShrinkFiberState.Active, fiber.State);
Assert.IsTrue(_runtime.TryGetRaw<ShrinkCommandService>(_runtime.RootContext,
ShrinkCommandAppComponent.ServiceKey, out var service));
Assert.AreSame(ShrinkCommandRuntime.Default, service);
Run(_runtime.RetireAsync(fiber));
Assert.IsFalse(_runtime.TryGetRaw<ShrinkCommandService>(_runtime.RootContext,
ShrinkCommandAppComponent.ServiceKey, out _), "退役撤回服务键");
}
[Test]
public void DataSaverComponent_Activates_FlushesOnDeactivate_WithdrawsKey()
{
var fiber = _runtime.Use(new ShrinkDataSaverAppComponent());
Assert.AreEqual(ShrinkFiberState.Active, fiber.State);
Assert.IsTrue(_runtime.TryGetRaw<object>(_runtime.RootContext,
ShrinkDataSaverAppComponent.ServiceKey, out _));
Assert.DoesNotThrow(() => Run(_runtime.RetireAsync(fiber)),
"停用执行补偿式收尾(设置最终落盘;未初始化环境安全跳过)");
Assert.IsFalse(_runtime.TryGetRaw<object>(_runtime.RootContext,
ShrinkDataSaverAppComponent.ServiceKey, out _));
}
}
}
@@ -0,0 +1,11 @@
fileFormatVersion: 2
guid: 5c9445def3a931642955715272969a72
MonoImporter:
externalObjects: {}
serializedVersion: 2
defaultReferences: []
executionOrder: 0
icon: {instanceID: 0}
userData:
assetBundleName:
assetBundleVariant:
@@ -0,0 +1,28 @@
{
"name": "ShrinkCommand.Integration.Network.Tests",
"rootNamespace": "ShrinkCommand.Integration.Network.Tests",
"references": [
"ShrinkCommand.Integration.Network",
"ShrinkCommand.Integration.App",
"ShrinkDataSaver.Integration.App",
"ShrinkNetwork.Integration.App",
"ShrinkCommand.Runtime",
"ShrinkNetwork.Runtime",
"ShrinkContext.Core.Runtime",
"UniTask",
"UnityEngine.TestRunner",
"UnityEditor.TestRunner"
],
"includePlatforms": [
"Editor"
],
"excludePlatforms": [],
"allowUnsafeCode": false,
"overrideReferences": false,
"autoReferenced": false,
"defineConstraints": [
"UNITY_INCLUDE_TESTS"
],
"versionDefines": [],
"noEngineReferences": false
}
@@ -0,0 +1,7 @@
fileFormatVersion: 2
guid: dd3e105f80569534b9ba2d6fa913a2cc
AssemblyDefinitionImporter:
externalObjects: {}
userData:
assetBundleName:
assetBundleVariant:
@@ -0,0 +1,22 @@
{
"name": "com.cneicy.shrink-command-integration-network",
"version": "0.1.0",
"displayName": "ShrinkCommand - Network Integration",
"description": "ShrinkCommand 与 ShrinkNetwork 的桥接层,允许远程会话通过 RPC 执行命令。",
"unity": "2022.3",
"dependencies": {
"com.cneicy.shrink-command": "0.1.0",
"com.cneicy.shrink-network": "0.1.0",
"com.cneicy.shrink-context-core": "0.1.0"
},
"keywords": [
"command",
"network",
"integration",
"rpc"
],
"author": {
"name": "cneicy",
"url": "https://github.com/cneicy"
}
}
@@ -0,0 +1,7 @@
fileFormatVersion: 2
guid: 73eb22917e75fcf4b8f1c7df5490014e
TextScriptImporter:
externalObjects: {}
userData:
assetBundleName:
assetBundleVariant: