Skip to content
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

Upgrade to .NET 8 #236

Merged
merged 9 commits into from
Dec 13, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions examples/SimpleModule/SimpleModule.csproj
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
<Project Sdk="Microsoft.NET.Sdk">

<PropertyGroup>
<TargetFramework>net7.0</TargetFramework>
<TargetFramework>net8.0</TargetFramework>
<ImplicitUsings>enable</ImplicitUsings>
<Nullable>enable</Nullable>
<AssemblyVersion>1.0.0</AssemblyVersion>
Expand All @@ -22,7 +22,7 @@
</ItemGroup>

<ItemGroup>
<PackageReference Include="SonarAnalyzer.CSharp" Version="9.5.0.73987">
<PackageReference Include="SonarAnalyzer.CSharp" Version="9.15.0.81779">
<PrivateAssets>all</PrivateAssets>
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
</PackageReference>
Expand Down
4 changes: 2 additions & 2 deletions src/EvoSC.CLI/EvoSC.CLI.csproj
Original file line number Diff line number Diff line change
@@ -1,13 +1,13 @@
<Project Sdk="Microsoft.NET.Sdk">

<PropertyGroup>
<TargetFramework>net7.0</TargetFramework>
<TargetFramework>net8.0</TargetFramework>
<ImplicitUsings>enable</ImplicitUsings>
<Nullable>enable</Nullable>
</PropertyGroup>

<ItemGroup>
<PackageReference Include="SonarAnalyzer.CSharp" Version="9.5.0.73987">
<PackageReference Include="SonarAnalyzer.CSharp" Version="9.15.0.81779">
<PrivateAssets>all</PrivateAssets>
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
</PackageReference>
Expand Down
9 changes: 2 additions & 7 deletions src/EvoSC.CLI/EvoScCliApp.cs
Original file line number Diff line number Diff line change
Expand Up @@ -8,19 +8,14 @@ public class EvoScCliApp : IEvoSCApplication
public IStartupPipeline StartupPipeline { get; }
public CancellationToken MainCancellationToken { get; }
public Container Services { get; }

public EvoScCliApp()
{

}

public Task RunAsync()
{
throw new NotImplementedException();
throw new NotSupportedException();
}

public Task ShutdownAsync()
{
throw new NotImplementedException();
throw new NotSupportedException();
}
}
17 changes: 6 additions & 11 deletions src/EvoSC.Commands/Attributes/CommandAliasAttribute.cs
Original file line number Diff line number Diff line change
@@ -1,28 +1,23 @@
namespace EvoSC.Commands.Attributes;

[AttributeUsage(AttributeTargets.Method, AllowMultiple = true)]
public class CommandAliasAttribute : Attribute
public class CommandAliasAttribute(string name, bool hide, params object[] args) : Attribute
{
/// <summary>
/// The name of the alias, which the player must type to call the command.
/// </summary>
public string Name { get; }
public string Name { get; } = name;

/// <summary>
/// Default arguments for this alias, passed to the command.
/// </summary>
public object[] Arguments { get; }
public object[] Arguments { get; } = args;

/// <summary>
/// Whether to hide the chat message that triggered this alias.
/// </summary>
public bool Hide { get; }
public bool Hide { get; } = hide;

public CommandAliasAttribute(string name, bool hide, params object[] args)
{
Name = name;
Arguments = args;
Hide = hide;
}

public CommandAliasAttribute(string name, params object[] args) : this(name, false, args)
{
}
Expand Down
20 changes: 5 additions & 15 deletions src/EvoSC.Commands/CommandAlias.cs
Original file line number Diff line number Diff line change
Expand Up @@ -3,23 +3,13 @@

namespace EvoSC.Commands;

public class CommandAlias : ICommandAlias
public class CommandAlias(string name, bool hide, params object[] args) : ICommandAlias
{
public string Name { get; init; }
public object[] DefaultArgs { get; init; }
public bool Hide { get; }
public string Name { get; init; } = name;
public object[] DefaultArgs { get; init; } = args;
public bool Hide { get; } = hide;

public CommandAlias(string name, bool hide, params object[] args)
public CommandAlias(CommandAliasAttribute attr) : this(attr.Name, attr.Hide, attr.Arguments)
{
Name = name;
DefaultArgs = args;
Hide = hide;
}

public CommandAlias(CommandAliasAttribute attr)
{
Name = attr.Name;
DefaultArgs = attr.Arguments;
Hide = attr.Hide;
}
}
8 changes: 3 additions & 5 deletions src/EvoSC.Commands/CommandInteractionContext.cs
Original file line number Diff line number Diff line change
Expand Up @@ -5,11 +5,9 @@

namespace EvoSC.Commands;

public class CommandInteractionContext : PlayerInteractionContext, ICommandInteractionContext
public class CommandInteractionContext
(IOnlinePlayer player, IControllerContext context) : PlayerInteractionContext(player, context),
ICommandInteractionContext
{
public required IChatCommand CommandExecuted { get; init; }

public CommandInteractionContext(IOnlinePlayer player, IControllerContext context) : base(player, context)
{
}
}
6 changes: 3 additions & 3 deletions src/EvoSC.Commands/EvoSC.Commands.csproj
Original file line number Diff line number Diff line change
@@ -1,16 +1,16 @@
<Project Sdk="Microsoft.NET.Sdk">

<PropertyGroup>
<TargetFramework>net7.0</TargetFramework>
<TargetFramework>net8.0</TargetFramework>
<ImplicitUsings>enable</ImplicitUsings>
<Nullable>enable</Nullable>
<AssemblyName>EvoSC.Commands</AssemblyName>
<RootNamespace>EvoSC.Commands</RootNamespace>
</PropertyGroup>

<ItemGroup>
<PackageReference Include="SimpleInjector" Version="5.4.1" />
<PackageReference Include="SonarAnalyzer.CSharp" Version="9.5.0.73987">
<PackageReference Include="SimpleInjector" Version="5.4.2" />
<PackageReference Include="SonarAnalyzer.CSharp" Version="9.15.0.81779">
<PrivateAssets>all</PrivateAssets>
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
</PackageReference>
Expand Down
11 changes: 2 additions & 9 deletions src/EvoSC.Commands/Exceptions/CommandNotFoundException.cs
Original file line number Diff line number Diff line change
Expand Up @@ -3,14 +3,7 @@
/// <summary>
/// Thrown when a command that does not exist was attempted retrieval.
/// </summary>
public class CommandNotFoundException : CommandParserException
public class CommandNotFoundException(string cmdName, bool intendedCommand) : CommandParserException(intendedCommand)
{
private readonly string _cmdName;

public CommandNotFoundException(string cmdName, bool intendedCommand) : base(intendedCommand)
{
_cmdName = cmdName;
}

public override string Message => $"The command '{_cmdName}' was not found.";
public override string Message => $"The command '{cmdName}' was not found.";
}
11 changes: 2 additions & 9 deletions src/EvoSC.Commands/Exceptions/DuplicateChatCommandException.cs
Original file line number Diff line number Diff line change
Expand Up @@ -3,14 +3,7 @@
/// <summary>
/// Thrown when a command or alias is registered more than one time.
/// </summary>
public class DuplicateChatCommandException : CommandException
public class DuplicateChatCommandException(string name) : CommandException
{
private readonly string _name;

public DuplicateChatCommandException(string name)
{
_name = name;
}

public override string Message => $"Chat command with name '{_name}' already exists.";
public override string Message => $"Chat command with name '{name}' already exists.";
}
11 changes: 2 additions & 9 deletions src/EvoSC.Commands/Exceptions/InvalidCommandArgumentException.cs
Original file line number Diff line number Diff line change
Expand Up @@ -3,14 +3,7 @@
/// <summary>
/// Thrown when the user input has an invalid format for a given command's parameter.
/// </summary>
public class InvalidCommandArgumentException : InvalidCommandFormatException
public class InvalidCommandArgumentException(string name, bool intendedCommand) : InvalidCommandFormatException(intendedCommand)
{
private readonly string _name;

public InvalidCommandArgumentException(string name, bool intendedCommand) : base(intendedCommand)
{
_name = name;
}

public override string Message => $"The argument '{_name}' is in an invalid format.";
public override string Message => $"The argument '{name}' is in an invalid format.";
}
11 changes: 2 additions & 9 deletions src/EvoSC.Commands/Exceptions/NotEnoughArgumentsException.cs
Original file line number Diff line number Diff line change
Expand Up @@ -3,14 +3,7 @@
/// <summary>
/// Thrown when the input does not have enough arguments to call a command's handler.
/// </summary>
public class NotEnoughArgumentsException : CommandParserException
public class NotEnoughArgumentsException(int requiredArgs, bool intendedCommand) : CommandParserException(intendedCommand)
{
private readonly int _requiredArgs;

public NotEnoughArgumentsException(int requiredArgs, bool intendedCommand) : base(intendedCommand)
{
_requiredArgs = requiredArgs;
}

public override string Message => $"The command requires {_requiredArgs} argument(s).";
public override string Message => $"The command requires {requiredArgs} argument(s).";
}
41 changes: 13 additions & 28 deletions src/EvoSC.Commands/Middleware/CommandsMiddleware.cs
Original file line number Diff line number Diff line change
Expand Up @@ -14,26 +14,11 @@

namespace EvoSC.Commands.Middleware;

public class CommandsMiddleware
public class CommandsMiddleware(ActionDelegate next, ILogger<CommandsMiddleware> logger,
IChatCommandManager cmdManager, IControllerManager controllers, IServerClient serverClient,
IActionPipelineManager actionPipeline)
{
private readonly ActionDelegate _next;
private readonly ILogger<CommandsMiddleware> _logger;
private readonly IControllerManager _controllers;
private readonly IServerClient _serverClient;
private readonly IActionPipelineManager _actionPipeline;
private readonly ChatCommandParser _parser;

public CommandsMiddleware(ActionDelegate next, ILogger<CommandsMiddleware> logger,
IChatCommandManager cmdManager, IControllerManager controllers, IServerClient serverClient,
IActionPipelineManager actionPipeline)
{
_next = next;
_logger = logger;
_controllers = controllers;
_serverClient = serverClient;
_actionPipeline = actionPipeline;
_parser = new ChatCommandParser(cmdManager);
}
private readonly ChatCommandParser _parser = new(cmdManager);

async Task HandleUserErrorsAsync(IParserResult result, string playerLogin)
{
Expand All @@ -45,22 +30,22 @@ async Task HandleUserErrorsAsync(IParserResult result, string playerLogin)
}

var message = $"Error: {cmdParserException.Message}";
await _serverClient.SendChatMessageAsync($"Error: {message}", playerLogin);
await serverClient.SendChatMessageAsync($"Error: {message}", playerLogin);
}

if (result.Exception is PlayerNotFoundException playerNotFoundException)
{
await _serverClient.SendChatMessageAsync($"Error: {playerNotFoundException.Message}", playerLogin);
await serverClient.SendChatMessageAsync($"Error: {playerNotFoundException.Message}", playerLogin);
}
else
{
_logger.LogError(result.Exception, "Failed to parse command");
logger.LogError(result.Exception, "Failed to parse command");
}
}

private async Task ExecuteCommandAsync(IChatCommand cmd, object[] args, ChatRouterPipelineContext routerContext)
{
var (controller, context) = _controllers.CreateInstance(cmd.ControllerType);
var (controller, context) = controllers.CreateInstance(cmd.ControllerType);

var playerInteractionContext = new CommandInteractionContext((IOnlinePlayer)routerContext.Player, context)
{
Expand All @@ -71,7 +56,7 @@ private async Task ExecuteCommandAsync(IChatCommand cmd, object[] args, ChatRout
var contextService = context.ServiceScope.GetInstance<IContextService>();
contextService.UpdateContext(playerInteractionContext);

var actionChain = _actionPipeline.BuildChain(PipelineType.ControllerAction, _ =>
var actionChain = actionPipeline.BuildChain(PipelineType.ControllerAction, _ =>
(Task?)cmd.HandlerMethod.Invoke(controller, args) ?? Task.CompletedTask
);

Expand All @@ -93,7 +78,7 @@ private async Task ExecuteCommandAsync(IChatCommand cmd, object[] args, ChatRout
}
else if (cmd.Permission != null)
{
_logger.LogWarning("Command '{Name}' has permissions set but does not activate an audit", cmd.Name);
logger.LogWarning("Command '{Name}' has permissions set but does not activate an audit", cmd.Name);
}
}
}
Expand Down Expand Up @@ -133,16 +118,16 @@ public async Task ExecuteAsync(ChatRouterPipelineContext context)
}
else
{
_logger.LogError(
logger.LogError(
"An unknown error occured while trying to parse command. No exception thrown, but parsing failed");
}
}
catch (Exception ex)
{
_logger.LogCritical(ex, "A fatal error occured while trying to handle chat command");
logger.LogCritical(ex, "A fatal error occured while trying to handle chat command");
throw;
}

await _next(context);
await next(context);
}
}
21 changes: 5 additions & 16 deletions src/EvoSC.Commands/Middleware/CommandsPermissionMiddleware.cs
Original file line number Diff line number Diff line change
Expand Up @@ -6,37 +6,26 @@

namespace EvoSC.Commands.Middleware;

public class CommandsPermissionMiddleware
public class CommandsPermissionMiddleware(ActionDelegate next, IPermissionManager permissions, IServerClient server)
{
private readonly ActionDelegate _next;
private readonly IPermissionManager _permissions;
private readonly IServerClient _server;

public CommandsPermissionMiddleware(ActionDelegate next, IPermissionManager permissions, IServerClient server)
{
_next = next;
_permissions = permissions;
_server = server;
}

public async Task ExecuteAsync(IControllerContext context)
{
var cmdContext = context as CommandInteractionContext;

if (cmdContext == null)
{
// not a command interaction
await _next(context);
await next(context);
return;
}

if (cmdContext.CommandExecuted.Permission == null ||
await _permissions.HasPermissionAsync(cmdContext.Player, cmdContext.CommandExecuted.Permission))
await permissions.HasPermissionAsync(cmdContext.Player, cmdContext.CommandExecuted.Permission))
{
await _next(context);
await next(context);
return;
}

await _server.SendChatMessageAsync("Insufficient permissions to run this command.", cmdContext.Player);
await server.SendChatMessageAsync("Insufficient permissions to run this command.", cmdContext.Player);
}
}
13 changes: 3 additions & 10 deletions src/EvoSC.Commands/Parser/ChatCommandParser.cs
Original file line number Diff line number Diff line change
Expand Up @@ -3,17 +3,10 @@

namespace EvoSC.Commands.Parser;

public class ChatCommandParser
public class ChatCommandParser(IChatCommandManager cmdManager)
{
private readonly IChatCommandManager _cmdManager;

public static string CommandPrefix = "/";

public ChatCommandParser(IChatCommandManager cmdManager)
{
_cmdManager = cmdManager;
}

public async Task<IParserResult> ParseAsync(string userInput)
{
try
Expand All @@ -27,7 +20,7 @@ public async Task<IParserResult> ParseAsync(string userInput)

var cmdAlias = parts[0];
bool intendedCommand = cmdAlias.StartsWith(CommandPrefix, StringComparison.Ordinal);
var cmd = _cmdManager.FindCommand(cmdAlias, false);
var cmd = cmdManager.FindCommand(cmdAlias, false);

if (cmd == null)
{
Expand Down Expand Up @@ -77,7 +70,7 @@ private async Task<List<object>> ConvertArgumentsAsync(IChatCommand cmd, string[
var parameter = cmd.Parameters[i];
try
{
var value = await _cmdManager.ValueReader.ConvertValueAsync(parameter.Type, parts[i - aliasArgCount]);
var value = await cmdManager.ValueReader.ConvertValueAsync(parameter.Type, parts[i - aliasArgCount]);
convertedArgs.Add(value);
}
catch (FormatException e)
Expand Down
Loading
Loading