Use ISubCommand for subcommands

This commit is contained in:
Liza 2023-02-25 00:55:48 +01:00
parent f140fb870c
commit e79e8de6dc
11 changed files with 79 additions and 63 deletions

BIN
Pal.Client/Certificate.pfx Normal file

Binary file not shown.

View File

@ -0,0 +1,10 @@
using System;
using System.Collections.Generic;
namespace Pal.Client.Commands
{
public interface ISubCommand
{
IReadOnlyDictionary<string, Action<string>> GetHandlers();
}
}

View File

@ -1,10 +1,11 @@
using Dalamud.Interface.Windowing; using System;
using System.Collections.Generic;
using Pal.Client.Configuration; using Pal.Client.Configuration;
using Pal.Client.Windows; using Pal.Client.Windows;
namespace Pal.Client.Commands namespace Pal.Client.Commands
{ {
internal class PalConfigCommand internal class PalConfigCommand : ISubCommand
{ {
private readonly IPalacePalConfiguration _configuration; private readonly IPalacePalConfiguration _configuration;
private readonly AgreementWindow _agreementWindow; private readonly AgreementWindow _agreementWindow;
@ -20,7 +21,15 @@ namespace Pal.Client.Commands
_configWindow = configWindow; _configWindow = configWindow;
} }
public void Execute()
public IReadOnlyDictionary<string, Action<string>> GetHandlers()
=> new Dictionary<string, Action<string>>
{
{ "config", _ => Execute() },
{ "", _ => Execute() }
};
private void Execute()
{ {
if (_configuration.FirstUse) if (_configuration.FirstUse)
_agreementWindow.IsOpen = true; _agreementWindow.IsOpen = true;

View File

@ -1,4 +1,5 @@
using System; using System;
using System.Collections.Generic;
using System.Linq; using System.Linq;
using Dalamud.Game.ClientState; using Dalamud.Game.ClientState;
using Pal.Client.DependencyInjection; using Pal.Client.DependencyInjection;
@ -8,7 +9,7 @@ using Pal.Client.Rendering;
namespace Pal.Client.Commands namespace Pal.Client.Commands
{ {
internal sealed class PalNearCommand internal sealed class PalNearCommand : ISubCommand
{ {
private readonly Chat _chat; private readonly Chat _chat;
private readonly ClientState _clientState; private readonly ClientState _clientState;
@ -24,23 +25,14 @@ namespace Pal.Client.Commands
_floorService = floorService; _floorService = floorService;
} }
public void Execute(string arguments)
{
switch (arguments)
{
default:
DebugNearest(_ => true);
break;
case "tnear": public IReadOnlyDictionary<string, Action<string>> GetHandlers()
DebugNearest(m => m.Type == MemoryLocation.EType.Trap); => new Dictionary<string, Action<string>>
break; {
{ "near", _ => DebugNearest(_ => true) },
case "hnear": { "tnear", _ => DebugNearest(m => m.Type == MemoryLocation.EType.Trap) },
DebugNearest(m => m.Type == MemoryLocation.EType.Hoard); { "hnear", _ => DebugNearest(m => m.Type == MemoryLocation.EType.Hoard) },
break; };
}
}
private void DebugNearest(Predicate<PersistentLocation> predicate) private void DebugNearest(Predicate<PersistentLocation> predicate)
{ {
@ -54,7 +46,7 @@ namespace Pal.Client.Commands
var playerPosition = _clientState.LocalPlayer?.Position; var playerPosition = _clientState.LocalPlayer?.Position;
if (playerPosition == null) if (playerPosition == null)
return; return;
_chat.Message($"{playerPosition}"); _chat.Message($"Your position: {playerPosition}");
var nearbyMarkers = state.Locations var nearbyMarkers = state.Locations
.Where(m => predicate(m)) .Where(m => predicate(m))

View File

@ -1,9 +1,10 @@
using System; using System;
using System.Collections.Generic;
using Pal.Client.DependencyInjection; using Pal.Client.DependencyInjection;
namespace Pal.Client.Commands namespace Pal.Client.Commands
{ {
internal sealed class PalStatsCommand internal sealed class PalStatsCommand : ISubCommand
{ {
private readonly StatisticsService _statisticsService; private readonly StatisticsService _statisticsService;
@ -12,7 +13,13 @@ namespace Pal.Client.Commands
_statisticsService = statisticsService; _statisticsService = statisticsService;
} }
public void Execute() public IReadOnlyDictionary<string, Action<string>> GetHandlers()
=> new Dictionary<string, Action<string>>
{
{ "stats", _ => Execute() },
};
private void Execute()
=> _statisticsService.ShowGlobalStatistics(); => _statisticsService.ShowGlobalStatistics();
} }
} }

View File

@ -1,9 +1,11 @@
using ECommons.Schedulers; using System;
using System.Collections.Generic;
using ECommons.Schedulers;
using Pal.Client.Windows; using Pal.Client.Windows;
namespace Pal.Client.Commands namespace Pal.Client.Commands
{ {
internal sealed class PalTestConnectionCommand internal sealed class PalTestConnectionCommand : ISubCommand
{ {
private readonly ConfigWindow _configWindow; private readonly ConfigWindow _configWindow;
@ -12,7 +14,14 @@ namespace Pal.Client.Commands
_configWindow = configWindow; _configWindow = configWindow;
} }
public void Execute() public IReadOnlyDictionary<string, Action<string>> GetHandlers()
=> new Dictionary<string, Action<string>>
{
{ "test-connection", _ => Execute() },
{ "tc", _ => Execute() },
};
private void Execute()
{ {
var _ = new TickScheduler(() => _configWindow.TestConnection()); var _ = new TickScheduler(() => _configWindow.TestConnection());
} }

View File

@ -69,10 +69,7 @@ namespace Pal.Client
_serviceProvider.GetRequiredService<ChatService>(); _serviceProvider.GetRequiredService<ChatService>();
// eager load any commands to find errors now, not when running them // eager load any commands to find errors now, not when running them
_serviceProvider.GetRequiredService<PalConfigCommand>(); _serviceProvider.GetRequiredService<IEnumerable<ISubCommand>>();
_serviceProvider.GetRequiredService<PalNearCommand>();
_serviceProvider.GetRequiredService<PalStatsCommand>();
_serviceProvider.GetRequiredService<PalTestConnectionCommand>();
cancellationToken.ThrowIfCancellationRequested(); cancellationToken.ThrowIfCancellationRequested();

View File

@ -113,10 +113,10 @@ namespace Pal.Client
_serviceCollection.AddTransient<RepoVerification>(); _serviceCollection.AddTransient<RepoVerification>();
// commands // commands
_serviceCollection.AddScoped<PalConfigCommand>(); _serviceCollection.AddScoped<ISubCommand, PalConfigCommand>();
_serviceCollection.AddScoped<PalNearCommand>(); _serviceCollection.AddScoped<ISubCommand, PalNearCommand>();
_serviceCollection.AddScoped<PalStatsCommand>(); _serviceCollection.AddScoped<ISubCommand, PalStatsCommand>();
_serviceCollection.AddScoped<PalTestConnectionCommand>(); _serviceCollection.AddScoped<ISubCommand, PalTestConnectionCommand>();
// territory & marker related services // territory & marker related services
_serviceCollection.AddScoped<TerritoryState>(); _serviceCollection.AddScoped<TerritoryState>();

View File

@ -2,6 +2,7 @@
using Dalamud.Plugin; using Dalamud.Plugin;
using Pal.Client.Rendering; using Pal.Client.Rendering;
using System; using System;
using System.Collections.Generic;
using System.Globalization; using System.Globalization;
using System.Linq; using System.Linq;
using System.Threading; using System.Threading;
@ -165,45 +166,36 @@ namespace Pal.Client
return; return;
} }
var sp = rootScope.ServiceProvider; Action<string> commandHandler = rootScope.ServiceProvider
.GetRequiredService<IEnumerable<ISubCommand>>()
switch (arguments) .SelectMany(cmd => cmd.GetHandlers())
.Where(cmd => cmd.Key == arguments.ToLowerInvariant())
.Select(cmd => cmd.Value)
.SingleOrDefault(missingCommand =>
{ {
case "": chat.Error(string.Format(Localization.Command_pal_UnknownSubcommand, missingCommand,
case "config":
sp.GetRequiredService<PalConfigCommand>().Execute();
break;
case "stats":
sp.GetRequiredService<PalStatsCommand>().Execute();
break;
case "tc":
case "test-connection":
sp.GetRequiredService<PalTestConnectionCommand>().Execute();
break;
case "near":
case "tnear":
case "hnear":
sp.GetRequiredService<PalNearCommand>().Execute(arguments);
break;
default:
chat.Error(string.Format(Localization.Command_pal_UnknownSubcommand, arguments,
command)); command));
break; });
} commandHandler.Invoke(arguments);
} }
catch (Exception e) catch (Exception e)
{ {
_logger.LogError(e, "Could not execute command '{Command}' with arguments '{Arguments}'", command,
arguments);
chat.Error(e.ToString()); chat.Error(e.ToString());
} }
}); });
} }
private void OpenConfigUi() private void OpenConfigUi()
=> _rootScope!.ServiceProvider.GetRequiredService<PalConfigCommand>().Execute(); {
_rootScope!.ServiceProvider.GetRequiredService<IEnumerable<ISubCommand>>()
.SelectMany(cmd => cmd.GetHandlers())
.Where(cmd => cmd.Key == "config")
.Select(cmd => cmd.Value)
.Single()
.Invoke("config");
}
private void LanguageChanged(string languageCode) private void LanguageChanged(string languageCode)
{ {

BIN
Pal.Client/Secrets.cs Normal file

Binary file not shown.

BIN
codesigning.pfx Normal file

Binary file not shown.