This commit is contained in:
@@ -0,0 +1,25 @@
|
||||
using System;
|
||||
|
||||
namespace ShrinkCommand
|
||||
{
|
||||
[AttributeUsage(AttributeTargets.Class, Inherited = false, AllowMultiple = false)]
|
||||
public sealed class ShrinkCommandSubscriberAttribute : Attribute
|
||||
{
|
||||
}
|
||||
|
||||
[AttributeUsage(AttributeTargets.Method, Inherited = false, AllowMultiple = true)]
|
||||
public sealed class ShrinkCommandAttribute : Attribute
|
||||
{
|
||||
public ShrinkCommandAttribute(string path)
|
||||
{
|
||||
Path = string.IsNullOrWhiteSpace(path) ? string.Empty : path.Trim();
|
||||
}
|
||||
|
||||
public string Path { get; }
|
||||
public string Description { get; set; } = string.Empty;
|
||||
public string Permission { get; set; } = string.Empty;
|
||||
public ShrinkCommandSourceKind SourceKind { get; set; } = ShrinkCommandSourceKind.Any;
|
||||
public bool Hidden { get; set; }
|
||||
public string[] Aliases { get; set; } = Array.Empty<string>();
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,11 @@
|
||||
fileFormatVersion: 2
|
||||
guid: 7bdd28a64d1899849b6cfce21ad2e91f
|
||||
MonoImporter:
|
||||
externalObjects: {}
|
||||
serializedVersion: 2
|
||||
defaultReferences: []
|
||||
executionOrder: 0
|
||||
icon: {instanceID: 0}
|
||||
userData:
|
||||
assetBundleName:
|
||||
assetBundleVariant:
|
||||
@@ -0,0 +1,7 @@
|
||||
namespace ShrinkCommand
|
||||
{
|
||||
public static class ShrinkCommandConstants
|
||||
{
|
||||
public const string DefaultServiceName = "default";
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,11 @@
|
||||
fileFormatVersion: 2
|
||||
guid: 438cadef623271d4d82e99c08a4e3b68
|
||||
MonoImporter:
|
||||
externalObjects: {}
|
||||
serializedVersion: 2
|
||||
defaultReferences: []
|
||||
executionOrder: 0
|
||||
icon: {instanceID: 0}
|
||||
userData:
|
||||
assetBundleName:
|
||||
assetBundleVariant:
|
||||
@@ -0,0 +1,144 @@
|
||||
#nullable enable
|
||||
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using System.Threading;
|
||||
using Cysharp.Threading.Tasks;
|
||||
|
||||
namespace ShrinkCommand
|
||||
{
|
||||
public enum ShrinkCommandSourceKind
|
||||
{
|
||||
Any = 0,
|
||||
ConsoleOnly = 1,
|
||||
NonConsoleOnly = 2
|
||||
}
|
||||
|
||||
public interface IShrinkCommandSource
|
||||
{
|
||||
string SourceId { get; }
|
||||
string DisplayName { get; }
|
||||
bool IsConsole { get; }
|
||||
|
||||
bool HasPermission(string permission);
|
||||
|
||||
UniTask WriteLineAsync(string message, CancellationToken cancellationToken = default);
|
||||
}
|
||||
|
||||
public sealed class ShrinkCommandExecutionResult
|
||||
{
|
||||
public bool IsSuccess { get; private set; }
|
||||
public string Message { get; private set; } = string.Empty;
|
||||
|
||||
public static ShrinkCommandExecutionResult Success(string message = "")
|
||||
{
|
||||
return new ShrinkCommandExecutionResult
|
||||
{
|
||||
IsSuccess = true,
|
||||
Message = message ?? string.Empty
|
||||
};
|
||||
}
|
||||
|
||||
public static ShrinkCommandExecutionResult Failure(string message)
|
||||
{
|
||||
return new ShrinkCommandExecutionResult
|
||||
{
|
||||
IsSuccess = false,
|
||||
Message = message ?? string.Empty
|
||||
};
|
||||
}
|
||||
}
|
||||
|
||||
public sealed class ShrinkCommandDescriptor
|
||||
{
|
||||
public string Path { get; set; } = string.Empty;
|
||||
public IReadOnlyList<string> Aliases { get; set; } = Array.Empty<string>();
|
||||
public string Description { get; set; } = string.Empty;
|
||||
public string Permission { get; set; } = string.Empty;
|
||||
public ShrinkCommandSourceKind SourceKind { get; set; }
|
||||
public bool Hidden { get; set; }
|
||||
|
||||
public IReadOnlyList<string> GetAllPaths()
|
||||
{
|
||||
if (Aliases == null || Aliases.Count == 0)
|
||||
return new[] { Path };
|
||||
|
||||
return new[] { Path }.Concat(Aliases).ToArray();
|
||||
}
|
||||
}
|
||||
|
||||
public sealed class ShrinkCommandRegistration
|
||||
{
|
||||
public string Path { get; set; } = string.Empty;
|
||||
public string[] Aliases { get; set; } = Array.Empty<string>();
|
||||
public string Description { get; set; } = string.Empty;
|
||||
public string Permission { get; set; } = string.Empty;
|
||||
public ShrinkCommandSourceKind SourceKind { get; set; } = ShrinkCommandSourceKind.Any;
|
||||
public bool Hidden { get; set; }
|
||||
public Func<ShrinkCommandContext, UniTask<ShrinkCommandExecutionResult>> Handler { get; set; } =
|
||||
_ => UniTask.FromResult(ShrinkCommandExecutionResult.Success());
|
||||
}
|
||||
|
||||
public sealed class ShrinkCommandContext
|
||||
{
|
||||
private readonly IReadOnlyDictionary<string, string> _arguments;
|
||||
|
||||
internal ShrinkCommandContext(
|
||||
ShrinkCommandService service,
|
||||
IShrinkCommandSource source,
|
||||
ShrinkCommandDescriptor command,
|
||||
string rawInput,
|
||||
IReadOnlyDictionary<string, string> arguments,
|
||||
CancellationToken cancellationToken)
|
||||
{
|
||||
Service = service;
|
||||
Source = source;
|
||||
Command = command;
|
||||
RawInput = rawInput ?? string.Empty;
|
||||
_arguments = arguments ?? new Dictionary<string, string>(StringComparer.OrdinalIgnoreCase);
|
||||
CancellationToken = cancellationToken;
|
||||
}
|
||||
|
||||
public ShrinkCommandService Service { get; }
|
||||
public IShrinkCommandSource Source { get; }
|
||||
public ShrinkCommandDescriptor Command { get; }
|
||||
public string RawInput { get; }
|
||||
public CancellationToken CancellationToken { get; }
|
||||
public IReadOnlyDictionary<string, string> Arguments => _arguments;
|
||||
|
||||
public string GetArgument(string name, string fallback = "")
|
||||
{
|
||||
if (string.IsNullOrWhiteSpace(name))
|
||||
return fallback;
|
||||
|
||||
return _arguments.TryGetValue(name.Trim(), out var value) ? value : fallback;
|
||||
}
|
||||
|
||||
public UniTask ReplyAsync(string message)
|
||||
{
|
||||
return Source.WriteLineAsync(message ?? string.Empty, CancellationToken);
|
||||
}
|
||||
}
|
||||
|
||||
public sealed class ShrinkCommandExecutingInfo
|
||||
{
|
||||
public ShrinkCommandService Service { get; set; } = null!;
|
||||
public IShrinkCommandSource Source { get; set; } = null!;
|
||||
public ShrinkCommandDescriptor Command { get; set; } = null!;
|
||||
public string RawInput { get; set; } = string.Empty;
|
||||
public IReadOnlyDictionary<string, string> Arguments { get; set; } =
|
||||
new Dictionary<string, string>(StringComparer.OrdinalIgnoreCase);
|
||||
}
|
||||
|
||||
public sealed class ShrinkCommandExecutedInfo
|
||||
{
|
||||
public ShrinkCommandService Service { get; set; } = null!;
|
||||
public IShrinkCommandSource Source { get; set; } = null!;
|
||||
public ShrinkCommandDescriptor? Command { get; set; }
|
||||
public string RawInput { get; set; } = string.Empty;
|
||||
public IReadOnlyDictionary<string, string> Arguments { get; set; } =
|
||||
new Dictionary<string, string>(StringComparer.OrdinalIgnoreCase);
|
||||
public ShrinkCommandExecutionResult Result { get; set; } = ShrinkCommandExecutionResult.Success();
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,11 @@
|
||||
fileFormatVersion: 2
|
||||
guid: 895c916a18310be4fb635652a6c84e9e
|
||||
MonoImporter:
|
||||
externalObjects: {}
|
||||
serializedVersion: 2
|
||||
defaultReferences: []
|
||||
executionOrder: 0
|
||||
icon: {instanceID: 0}
|
||||
userData:
|
||||
assetBundleName:
|
||||
assetBundleVariant:
|
||||
Reference in New Issue
Block a user