init
This commit is contained in:
commit
f25ba8adcd
64 changed files with 5959 additions and 0 deletions
32
src/GsDChat.Modding/CommandRegistry.cs
Normal file
32
src/GsDChat.Modding/CommandRegistry.cs
Normal file
|
|
@ -0,0 +1,32 @@
|
|||
using System;
|
||||
using System.Collections.Concurrent;
|
||||
using System.Threading.Tasks;
|
||||
|
||||
namespace GsDChat.Modding;
|
||||
|
||||
public class CommandRegistry : ICommandRegistry
|
||||
{
|
||||
private readonly ConcurrentDictionary<string, (string Description, Func<CommandContext, Task<CommandResult>> Handler)> _commands = new(StringComparer.OrdinalIgnoreCase);
|
||||
|
||||
public void RegisterCommand(string name, string description, Func<CommandContext, Task<CommandResult>> handler)
|
||||
{
|
||||
string clean = name.TrimStart('/').ToLowerInvariant();
|
||||
_commands[clean] = (description, handler);
|
||||
}
|
||||
|
||||
public async Task<CommandResult?> ExecuteCommandAsync(CommandContext context)
|
||||
{
|
||||
string clean = context.Command.TrimStart('/').ToLowerInvariant();
|
||||
if (_commands.TryGetValue(clean, out var entry))
|
||||
{
|
||||
return await entry.Handler(context);
|
||||
}
|
||||
return null;
|
||||
}
|
||||
|
||||
public bool HasCommand(string name)
|
||||
{
|
||||
string clean = name.TrimStart('/').ToLowerInvariant();
|
||||
return _commands.ContainsKey(clean);
|
||||
}
|
||||
}
|
||||
13
src/GsDChat.Modding/GsDChat.Modding.csproj
Normal file
13
src/GsDChat.Modding/GsDChat.Modding.csproj
Normal file
|
|
@ -0,0 +1,13 @@
|
|||
<Project Sdk="Microsoft.NET.Sdk">
|
||||
|
||||
<ItemGroup>
|
||||
<ProjectReference Include="..\GsDChat.Core\GsDChat.Core.csproj" />
|
||||
</ItemGroup>
|
||||
|
||||
<PropertyGroup>
|
||||
<TargetFramework>net10.0</TargetFramework>
|
||||
<ImplicitUsings>enable</ImplicitUsings>
|
||||
<Nullable>enable</Nullable>
|
||||
</PropertyGroup>
|
||||
|
||||
</Project>
|
||||
30
src/GsDChat.Modding/ICommandRegistry.cs
Normal file
30
src/GsDChat.Modding/ICommandRegistry.cs
Normal file
|
|
@ -0,0 +1,30 @@
|
|||
using System;
|
||||
using System.Threading.Tasks;
|
||||
|
||||
namespace GsDChat.Modding;
|
||||
|
||||
public class CommandContext
|
||||
{
|
||||
public string Command { get; set; } = string.Empty;
|
||||
public string Args { get; set; } = string.Empty;
|
||||
public string UserId { get; set; } = string.Empty;
|
||||
public string Username { get; set; } = string.Empty;
|
||||
public string UserRole { get; set; } = string.Empty;
|
||||
public string ChannelId { get; set; } = string.Empty;
|
||||
}
|
||||
|
||||
public class CommandResult
|
||||
{
|
||||
public bool Success { get; set; }
|
||||
public string Output { get; set; } = string.Empty;
|
||||
|
||||
public static CommandResult Ok(string output) => new() { Success = true, Output = output };
|
||||
public static CommandResult Fail(string error) => new() { Success = false, Output = error };
|
||||
}
|
||||
|
||||
public interface ICommandRegistry
|
||||
{
|
||||
void RegisterCommand(string name, string description, Func<CommandContext, Task<CommandResult>> handler);
|
||||
Task<CommandResult?> ExecuteCommandAsync(CommandContext context);
|
||||
bool HasCommand(string name);
|
||||
}
|
||||
23
src/GsDChat.Modding/IMessagePipeline.cs
Normal file
23
src/GsDChat.Modding/IMessagePipeline.cs
Normal file
|
|
@ -0,0 +1,23 @@
|
|||
using System;
|
||||
using System.Threading.Tasks;
|
||||
using GsDChat.Core.Models;
|
||||
|
||||
namespace GsDChat.Modding;
|
||||
|
||||
public class MessagePipelineContext
|
||||
{
|
||||
public ChatMessage Message { get; set; }
|
||||
public bool IsCancelled { get; set; } = false;
|
||||
public string? CancellationReason { get; set; }
|
||||
|
||||
public MessagePipelineContext(ChatMessage message)
|
||||
{
|
||||
Message = message;
|
||||
}
|
||||
}
|
||||
|
||||
public interface IMessagePipeline
|
||||
{
|
||||
void Use(Func<MessagePipelineContext, Func<Task>, Task> middleware);
|
||||
Task<MessagePipelineContext> ExecuteAsync(ChatMessage message);
|
||||
}
|
||||
14
src/GsDChat.Modding/IMod.cs
Normal file
14
src/GsDChat.Modding/IMod.cs
Normal file
|
|
@ -0,0 +1,14 @@
|
|||
using System.Threading.Tasks;
|
||||
|
||||
namespace GsDChat.Modding;
|
||||
|
||||
public interface IMod
|
||||
{
|
||||
string Id { get; }
|
||||
string Name { get; }
|
||||
string Version { get; }
|
||||
string Description { get; }
|
||||
|
||||
Task InitializeAsync(IModContext context);
|
||||
Task ShutdownAsync();
|
||||
}
|
||||
16
src/GsDChat.Modding/IModContext.cs
Normal file
16
src/GsDChat.Modding/IModContext.cs
Normal file
|
|
@ -0,0 +1,16 @@
|
|||
using System;
|
||||
|
||||
namespace GsDChat.Modding;
|
||||
|
||||
public interface IModContext
|
||||
{
|
||||
IModEventBus Events { get; }
|
||||
ICommandRegistry Commands { get; }
|
||||
IMessagePipeline Pipeline { get; }
|
||||
IUiRegistry Ui { get; }
|
||||
string ModDataDirectory { get; }
|
||||
|
||||
void LogInfo(string message);
|
||||
void LogWarning(string message);
|
||||
void LogError(string message, Exception? ex = null);
|
||||
}
|
||||
18
src/GsDChat.Modding/IModEventBus.cs
Normal file
18
src/GsDChat.Modding/IModEventBus.cs
Normal file
|
|
@ -0,0 +1,18 @@
|
|||
using System;
|
||||
using System.Threading.Tasks;
|
||||
using GsDChat.Core.Models;
|
||||
|
||||
namespace GsDChat.Modding;
|
||||
|
||||
public interface IModEventBus
|
||||
{
|
||||
void SubscribeUserConnected(Func<User, Task> handler);
|
||||
void SubscribeUserDisconnected(Func<User, Task> handler);
|
||||
void SubscribeChannelCreated(Func<Channel, Task> handler);
|
||||
void SubscribeFileUploaded(Func<FileRecord, Task> handler);
|
||||
|
||||
Task PublishUserConnectedAsync(User user);
|
||||
Task PublishUserDisconnectedAsync(User user);
|
||||
Task PublishChannelCreatedAsync(Channel channel);
|
||||
Task PublishFileUploadedAsync(FileRecord file);
|
||||
}
|
||||
11
src/GsDChat.Modding/IUiRegistry.cs
Normal file
11
src/GsDChat.Modding/IUiRegistry.cs
Normal file
|
|
@ -0,0 +1,11 @@
|
|||
using GsDChat.Core.Protocol;
|
||||
|
||||
namespace GsDChat.Modding;
|
||||
|
||||
public interface IUiRegistry
|
||||
{
|
||||
void RegisterScreen(ScreenDefinition screen);
|
||||
void SetMotd(string motd);
|
||||
void SetTheme(ThemeDefinition theme);
|
||||
ServerUiSync GetUiSync();
|
||||
}
|
||||
34
src/GsDChat.Modding/MessagePipeline.cs
Normal file
34
src/GsDChat.Modding/MessagePipeline.cs
Normal file
|
|
@ -0,0 +1,34 @@
|
|||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Threading.Tasks;
|
||||
using GsDChat.Core.Models;
|
||||
|
||||
namespace GsDChat.Modding;
|
||||
|
||||
public class MessagePipeline : IMessagePipeline
|
||||
{
|
||||
private readonly List<Func<MessagePipelineContext, Func<Task>, Task>> _middlewares = new();
|
||||
|
||||
public void Use(Func<MessagePipelineContext, Func<Task>, Task> middleware)
|
||||
{
|
||||
_middlewares.Add(middleware);
|
||||
}
|
||||
|
||||
public async Task<MessagePipelineContext> ExecuteAsync(ChatMessage message)
|
||||
{
|
||||
var context = new MessagePipelineContext(message);
|
||||
|
||||
int index = 0;
|
||||
async Task Next()
|
||||
{
|
||||
if (index < _middlewares.Count && !context.IsCancelled)
|
||||
{
|
||||
var current = _middlewares[index++];
|
||||
await current(context, Next);
|
||||
}
|
||||
}
|
||||
|
||||
await Next();
|
||||
return context;
|
||||
}
|
||||
}
|
||||
32
src/GsDChat.Modding/ModContext.cs
Normal file
32
src/GsDChat.Modding/ModContext.cs
Normal file
|
|
@ -0,0 +1,32 @@
|
|||
using System;
|
||||
using System.IO;
|
||||
|
||||
namespace GsDChat.Modding;
|
||||
|
||||
public class ModContext : IModContext
|
||||
{
|
||||
public IModEventBus Events { get; }
|
||||
public ICommandRegistry Commands { get; }
|
||||
public IMessagePipeline Pipeline { get; }
|
||||
public IUiRegistry Ui { get; }
|
||||
public string ModDataDirectory { get; }
|
||||
|
||||
public ModContext(
|
||||
IModEventBus events,
|
||||
ICommandRegistry commands,
|
||||
IMessagePipeline pipeline,
|
||||
IUiRegistry ui,
|
||||
string modDataDirectory)
|
||||
{
|
||||
Events = events;
|
||||
Commands = commands;
|
||||
Pipeline = pipeline;
|
||||
Ui = ui;
|
||||
ModDataDirectory = modDataDirectory;
|
||||
Directory.CreateDirectory(modDataDirectory);
|
||||
}
|
||||
|
||||
public void LogInfo(string message) => Console.WriteLine($"[Mod INFO] {message}");
|
||||
public void LogWarning(string message) => Console.WriteLine($"[Mod WARN] {message}");
|
||||
public void LogError(string message, Exception? ex = null) => Console.WriteLine($"[Mod ERROR] {message} {ex?.Message}");
|
||||
}
|
||||
51
src/GsDChat.Modding/ModEventBus.cs
Normal file
51
src/GsDChat.Modding/ModEventBus.cs
Normal file
|
|
@ -0,0 +1,51 @@
|
|||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Threading.Tasks;
|
||||
using GsDChat.Core.Models;
|
||||
|
||||
namespace GsDChat.Modding;
|
||||
|
||||
public class ModEventBus : IModEventBus
|
||||
{
|
||||
private readonly List<Func<User, Task>> _userConnectedHandlers = new();
|
||||
private readonly List<Func<User, Task>> _userDisconnectedHandlers = new();
|
||||
private readonly List<Func<Channel, Task>> _channelCreatedHandlers = new();
|
||||
private readonly List<Func<FileRecord, Task>> _fileUploadedHandlers = new();
|
||||
|
||||
public void SubscribeUserConnected(Func<User, Task> handler) => _userConnectedHandlers.Add(handler);
|
||||
public void SubscribeUserDisconnected(Func<User, Task> handler) => _userDisconnectedHandlers.Add(handler);
|
||||
public void SubscribeChannelCreated(Func<Channel, Task> handler) => _channelCreatedHandlers.Add(handler);
|
||||
public void SubscribeFileUploaded(Func<FileRecord, Task> handler) => _fileUploadedHandlers.Add(handler);
|
||||
|
||||
public async Task PublishUserConnectedAsync(User user)
|
||||
{
|
||||
foreach (var h in _userConnectedHandlers)
|
||||
{
|
||||
try { await h(user); } catch { }
|
||||
}
|
||||
}
|
||||
|
||||
public async Task PublishUserDisconnectedAsync(User user)
|
||||
{
|
||||
foreach (var h in _userDisconnectedHandlers)
|
||||
{
|
||||
try { await h(user); } catch { }
|
||||
}
|
||||
}
|
||||
|
||||
public async Task PublishChannelCreatedAsync(Channel channel)
|
||||
{
|
||||
foreach (var h in _channelCreatedHandlers)
|
||||
{
|
||||
try { await h(channel); } catch { }
|
||||
}
|
||||
}
|
||||
|
||||
public async Task PublishFileUploadedAsync(FileRecord file)
|
||||
{
|
||||
foreach (var h in _fileUploadedHandlers)
|
||||
{
|
||||
try { await h(file); } catch { }
|
||||
}
|
||||
}
|
||||
}
|
||||
40
src/GsDChat.Modding/UiRegistry.cs
Normal file
40
src/GsDChat.Modding/UiRegistry.cs
Normal file
|
|
@ -0,0 +1,40 @@
|
|||
using System.Collections.Generic;
|
||||
using GsDChat.Core.Protocol;
|
||||
|
||||
namespace GsDChat.Modding;
|
||||
|
||||
public class UiRegistry : IUiRegistry
|
||||
{
|
||||
private readonly ServerUiSync _sync;
|
||||
|
||||
public UiRegistry(string serverName, string defaultMotd)
|
||||
{
|
||||
_sync = ServerUiSync.CreateDefault(serverName, defaultMotd);
|
||||
}
|
||||
|
||||
public void RegisterScreen(ScreenDefinition screen)
|
||||
{
|
||||
// Replace if exists, or append
|
||||
int existingIndex = _sync.Screens.FindIndex(s => s.ScreenId == screen.ScreenId);
|
||||
if (existingIndex >= 0)
|
||||
{
|
||||
_sync.Screens[existingIndex] = screen;
|
||||
}
|
||||
else
|
||||
{
|
||||
_sync.Screens.Add(screen);
|
||||
}
|
||||
}
|
||||
|
||||
public void SetMotd(string motd)
|
||||
{
|
||||
_sync.Motd = motd;
|
||||
}
|
||||
|
||||
public void SetTheme(ThemeDefinition theme)
|
||||
{
|
||||
_sync.Theme = theme;
|
||||
}
|
||||
|
||||
public ServerUiSync GetUiSync() => _sync;
|
||||
}
|
||||
Loading…
Add table
Add a link
Reference in a new issue