feat(cordis): 接入上下文组合与模组事务热替换
This commit is contained in:
@@ -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;
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user