-
Notifications
You must be signed in to change notification settings - Fork 52
Feature: Add a Console Gui Interface to handle command input #509
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
stevesensei
wants to merge
8
commits into
ObsidianMC:1.21.x
Choose a base branch
from
stevesensei:console-gui
base: 1.21.x
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from 4 commits
Commits
Show all changes
8 commits
Select commit
Hold shift + click to select a range
7064182
Init
stevesensei 2bacd07
initial console
stevesensei a15ac66
basic command input implement.
stevesensei d437dba
feat: Add ConsoleCommandSender to handle the command sent from the co…
stevesensei c19b0e2
Correct the folder name and fix some potential issues
stevesensei 667c3c2
Add document
stevesensei 628cc23
Merge branch '1.21.x' into console-gui
stevesensei cd1b1b5
Merge branch '1.21.x' into console-gui
stevesensei File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,4 @@ | ||
| global using System; | ||
| global using System.Collections.Generic; | ||
| global using System.IO; | ||
| global using System.Threading.Tasks; |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,30 @@ | ||
| using Microsoft.Extensions.Logging; | ||
| using Obsidian.GuiConsole.Window; | ||
| using Terminal.Gui.Views; | ||
|
|
||
| namespace Obsidian.GuiConsole.Logger; | ||
|
|
||
| public class TerminalGuiLogger : ILogger | ||
| { | ||
| private readonly ObsdianConsole _console; | ||
| private readonly LogLevel _minLevel; | ||
|
|
||
| public TerminalGuiLogger(ObsdianConsole console, LogLevel minLevel) | ||
| { | ||
| _console = console; | ||
| _minLevel = minLevel; | ||
| } | ||
|
|
||
| public IDisposable? BeginScope<TState>(TState state) where TState : notnull => null; | ||
|
|
||
| public bool IsEnabled(LogLevel logLevel) => logLevel >= _minLevel; | ||
|
|
||
| public void Log<TState>(LogLevel logLevel, EventId eventId, TState state, Exception? exception, Func<TState, Exception?, string> formatter) | ||
| { | ||
| if (!IsEnabled(logLevel)) return; | ||
|
|
||
| var message = formatter(state, exception); | ||
|
|
||
| _console.AppendLog(logLevel,message); | ||
| } | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,24 @@ | ||
| using Microsoft.Extensions.Logging; | ||
| using Obsidian.GuiConsole.Window; | ||
| using Terminal.Gui.Views; | ||
|
|
||
| namespace Obsidian.GuiConsole.Logger; | ||
|
|
||
| public class TerminalGuiLoggerProvider: ILoggerProvider | ||
| { | ||
| private readonly ObsdianConsole _console; | ||
| private readonly LogLevel _minLevel; | ||
|
|
||
| public TerminalGuiLoggerProvider(ObsdianConsole console, LogLevel minLevel) | ||
| { | ||
| _console = console; | ||
| _minLevel = minLevel; | ||
| } | ||
|
|
||
| public ILogger CreateLogger(string categoryName) | ||
| { | ||
| return new TerminalGuiLogger(_console, _minLevel); | ||
| } | ||
|
|
||
| public void Dispose() { } | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,24 @@ | ||
| <Project Sdk="Microsoft.NET.Sdk"> | ||
|
|
||
| <PropertyGroup> | ||
| <OutputType>Exe</OutputType> | ||
| <TargetFramework>net10.0</TargetFramework> | ||
| <ImplicitUsings>enable</ImplicitUsings> | ||
| <Nullable>enable</Nullable> | ||
| <EnablePreviewFeatures>True</EnablePreviewFeatures> | ||
| </PropertyGroup> | ||
|
|
||
| <ItemGroup> | ||
| <PackageReference Include="Terminal.Gui" Version="2.0.0-develop.4957" /> | ||
| </ItemGroup> | ||
|
|
||
| <ItemGroup> | ||
| <ProjectReference Include="..\Obsidian\Obsidian.csproj" /> | ||
| </ItemGroup> | ||
|
|
||
| <ItemGroup> | ||
| <EmbeddedResource Include="config\server.json" /> | ||
| <EmbeddedResource Include="config\whitelist.json" /> | ||
| </ItemGroup> | ||
|
|
||
| </Project> |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,52 @@ | ||
| using System.Reflection; | ||
|
|
||
| namespace Obsidian.GuiConsole; | ||
|
|
||
| public partial class Program | ||
| { | ||
| private static async ValueTask GenerateConfigFiles() | ||
| { | ||
| const string path = "config"; | ||
|
|
||
| Directory.CreateDirectory(path); | ||
|
|
||
| var serverJsonFile = Path.Combine(path, "server.json"); | ||
| var whitelistJsonFile = Path.Combine(path, "whitelist.json"); | ||
|
|
||
| if (!File.Exists(serverJsonFile)) | ||
| { | ||
| await using var file = File.Create(serverJsonFile); | ||
|
|
||
| await using var embeddedFile = Assembly.GetExecutingAssembly().GetManifestResourceStream("Obsidian.GuiConsole.config.server.json"); | ||
|
|
||
| await embeddedFile!.CopyToAsync(file); | ||
| } | ||
|
|
||
| if (!File.Exists(whitelistJsonFile)) | ||
| { | ||
| await using var file = File.Create(whitelistJsonFile); | ||
|
|
||
| await using var embeddedFile = Assembly.GetExecutingAssembly().GetManifestResourceStream("Obsidian.GuiConsole.config.whitelist.json"); | ||
|
|
||
| await embeddedFile!.CopyToAsync(file); | ||
| } | ||
| } | ||
|
|
||
| public static void DrawLogo() | ||
| { | ||
| const string asciilogo = | ||
| "\n" + | ||
| " ▄▄▄▄ ▄▄ ▄▄▄▄ ▀ ▄▄▄ ▐ ▄ \n" + | ||
| " ▐█ ▀█ ▐█ ▀ ██ ██ ██ ██ ▐█ ▀█ █▌▐█\n" + | ||
| " ▄█▀▄ ▐█▀▀█▄▄▀▀▀█▄▐█ ▐█ ▐█▌▐█ ▄█▀▀█ ▐█▐▐▌\n" + | ||
| "▐█▌ ▐▌██▄ ▐█▐█▄ ▐█▐█▌██ ██ ▐█▌▐█ ▐▌██▐█▌\n" + | ||
| " ▀█▄▀ ▀▀▀▀ ▀▀▀▀ ▀▀▀▀▀▀▀▀ ▀▀▀ ▀ ▀ ▀▀ █ \n\n"; | ||
|
|
||
| Console.Title = $"Obsidian for {ServerConstants.DefaultProtocol} ({ServerConstants.VERSION})"; | ||
| Console.BackgroundColor = ConsoleColor.White; | ||
| Console.ForegroundColor = ConsoleColor.Black; | ||
| Console.CursorVisible = false; | ||
| Console.WriteLine(asciilogo); | ||
| Console.ResetColor(); | ||
| } | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,70 @@ | ||
| using Microsoft.Extensions.DependencyInjection; | ||
| using Microsoft.Extensions.Hosting; | ||
| using Microsoft.Extensions.Logging; | ||
| using Obsidian.GuiConsole.Logger; | ||
| using Obsidian.GuiConsole.Services; | ||
| using Obsidian.GuiConsole.Services.Command; | ||
| using Obsidian.GuiConsole.Window; | ||
| using Obsidian.Hosting; | ||
| using Terminal.Gui.App; | ||
| using Terminal.Gui.Configuration; | ||
|
|
||
| namespace Obsidian.GuiConsole; | ||
|
|
||
| public partial class Program | ||
| { | ||
| public static async Task Main(string[] args) | ||
| { | ||
| //Cool logo,but not render well on my machine -- stevesensei | ||
| DrawLogo(); | ||
|
|
||
| //Init Gui App | ||
| ConfigurationManager.Enable(ConfigLocations.All); | ||
|
|
||
| //Create console window | ||
|
|
||
|
|
||
| //Normal Obsidian setup | ||
| await GenerateConfigFiles(); | ||
| var builder = Host.CreateApplicationBuilder(); | ||
| builder.ConfigureObsidian(); | ||
| if (!Directory.Exists("logs")) | ||
| { | ||
| Directory.CreateDirectory("logs"); | ||
| } | ||
|
|
||
| //Add Obsidian with GUI logger | ||
| var console = new ObsdianConsole(); | ||
|
|
||
| builder.AddObsidianWithGui(x => | ||
| { | ||
| x.AddProvider(new TerminalGuiLoggerProvider(console, LogLevel.Information)); | ||
| return x; | ||
| }); | ||
|
|
||
| //Give the server some time to shut down after CTRL-C or SIGTERM. | ||
| builder.Services.Configure<HostOptions>(opts => | ||
| { | ||
| opts.ShutdownTimeout = TimeSpan.FromSeconds(10); | ||
| }); | ||
| builder.Services.AddSingleton<CommandMiddleware>(); | ||
| var hostApp = builder.Build(); | ||
|
|
||
| IApplication? app = null; | ||
| try | ||
| { | ||
| //Run the application | ||
| await hostApp.StartAsync(); | ||
| var commandMiddleware = hostApp.Services.GetRequiredService<CommandMiddleware>(); | ||
| console.AddCommandMiddleware(commandMiddleware); | ||
| app = Application.Create().Init(); | ||
| app.Run(console); | ||
| } | ||
| finally | ||
| { | ||
| //Cleanup when application stops | ||
| await hostApp.StopAsync(); | ||
| app?.Dispose(); | ||
| } | ||
| } | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,14 @@ | ||
| using Microsoft.Extensions.Logging; | ||
| using Obsidian.API; | ||
| using Obsidian.API.Commands; | ||
|
|
||
| namespace Obsidian.GuiConsole.Services.Command; | ||
|
|
||
| public class CommandMiddleware(IServer server,ILogger<CommandMiddleware> logger) | ||
| { | ||
| public async Task ExecuteCommandFromConsoleAsync(string commandText) | ||
| { | ||
| var commandContext = new CommandContext("/"+commandText, new ConsoleCommandSender(logger), null,server); | ||
| await server.CommandHandler.ProcessCommand(commandContext); | ||
|
stevesensei marked this conversation as resolved.
Outdated
|
||
| } | ||
| } | ||
35 changes: 35 additions & 0 deletions
35
Obsdian.GuiConsole/Services/Command/ConsoleCommandSender.cs
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,35 @@ | ||
| using Microsoft.Extensions.Logging; | ||
| using Obsidian.API; | ||
| using System.Text.RegularExpressions; | ||
|
|
||
| namespace Obsidian.GuiConsole.Services.Command; | ||
|
|
||
| public class ConsoleCommandSender(ILogger<CommandMiddleware> logger): ICommandSender | ||
| { | ||
| public CommandIssuers Issuer { get; } = CommandIssuers.Console; | ||
| public IPlayer? Player { get; } = null; | ||
|
|
||
| public Task SendMessageAsync(ChatMessage message) | ||
| { | ||
| List<string?> messageParts = new(); | ||
| messageParts.Add(message.Text); | ||
| foreach (var extra in message.GetExtras()) | ||
| messageParts.Add(extra.Text); | ||
| //log | ||
| foreach (var messagePart in messageParts) | ||
| { | ||
| if (string.IsNullOrEmpty(messagePart) || messagePart == "\n") | ||
| { | ||
| continue; | ||
| } | ||
| //remove color codes § | ||
| var clearMessage = Regex | ||
| .Replace(messagePart, "§[0-9a-fk-or]", string.Empty, RegexOptions.IgnoreCase); | ||
| //log to console | ||
| logger.LogInformation("[Console] {MessagePart}", clearMessage); | ||
| } | ||
| return Task.CompletedTask; | ||
| } | ||
|
|
||
| public Task SendMessageAsync(ChatMessage message, Guid sender) => throw new NotImplementedException(); | ||
| } |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.