Use ISubCommand for subcommands

rendering
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.Windows;
namespace Pal.Client.Commands
{
internal class PalConfigCommand
internal class PalConfigCommand : ISubCommand
{
private readonly IPalacePalConfiguration _configuration;
private readonly AgreementWindow _agreementWindow;
@ -20,7 +21,15 @@ namespace Pal.Client.Commands
_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)
_agreementWindow.IsOpen = true;

View File

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

View File

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

View File

@ -1,9 +1,11 @@
using ECommons.Schedulers;
using System;
using System.Collections.Generic;
using ECommons.Schedulers;
using Pal.Client.Windows;
namespace Pal.Client.Commands
{
internal sealed class PalTestConnectionCommand
internal sealed class PalTestConnectionCommand : ISubCommand
{
private readonly ConfigWindow _configWindow;
@ -12,7 +14,14 @@ namespace Pal.Client.Commands
_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());
}

View File

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

View File

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

View File

@ -2,6 +2,7 @@
using Dalamud.Plugin;
using Pal.Client.Rendering;
using System;
using System.Collections.Generic;
using System.Globalization;
using System.Linq;
using System.Threading;
@ -165,45 +166,36 @@ namespace Pal.Client
return;
}
var sp = rootScope.ServiceProvider;
switch (arguments)
{
case "":
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,
Action<string> commandHandler = rootScope.ServiceProvider
.GetRequiredService<IEnumerable<ISubCommand>>()
.SelectMany(cmd => cmd.GetHandlers())
.Where(cmd => cmd.Key == arguments.ToLowerInvariant())
.Select(cmd => cmd.Value)
.SingleOrDefault(missingCommand =>
{
chat.Error(string.Format(Localization.Command_pal_UnknownSubcommand, missingCommand,
command));
break;
}
});
commandHandler.Invoke(arguments);
}
catch (Exception e)
{
_logger.LogError(e, "Could not execute command '{Command}' with arguments '{Arguments}'", command,
arguments);
chat.Error(e.ToString());
}
});
}
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)
{

BIN
Pal.Client/Secrets.cs Normal file

Binary file not shown.

BIN
codesigning.pfx Normal file

Binary file not shown.