Merge pull request #1832 from goatcorp/apiX-rollup

[apiX] Rollup changes from master
This commit is contained in:
goat 2024-06-16 13:01:50 +02:00 committed by GitHub
commit 80555d92ec
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
28 changed files with 1526 additions and 311 deletions

View file

@ -270,7 +270,9 @@ internal partial class ConsoleManager : IServiceType
for (var i = parsedArguments.Count; i < entry.ValidArguments.Count; i++)
{
var argument = entry.ValidArguments[i];
if (argument.DefaultValue == null)
// If the default value is DBNull, we need to error out as that means it was not specified
if (argument.DefaultValue == DBNull.Value)
{
Log.Error("Not enough arguments for command {CommandName}", entryName);
PrintUsage(entry);
@ -382,11 +384,8 @@ internal partial class ConsoleManager : IServiceType
/// <param name="defaultValue">The default value to use if none is specified.</param>
/// <returns>An <see cref="ArgumentInfo"/> instance.</returns>
/// <exception cref="ArgumentException">Thrown if the given type cannot be handled by the console system.</exception>
protected static ArgumentInfo TypeToArgument(Type type, object? defaultValue = null)
protected static ArgumentInfo TypeToArgument(Type type, object? defaultValue)
{
// If the default value is DBNull, we want to treat it as null
defaultValue = defaultValue == DBNull.Value ? null : defaultValue;
if (type == typeof(string))
return new ArgumentInfo(ConsoleArgumentType.String, defaultValue);
@ -490,7 +489,7 @@ internal partial class ConsoleManager : IServiceType
public ConsoleVariable(string name, string description)
: base(name, description)
{
this.ValidArguments = new List<ArgumentInfo> { TypeToArgument(typeof(T)) };
this.ValidArguments = new List<ArgumentInfo> { TypeToArgument(typeof(T), null) };
}
/// <inheritdoc/>
@ -500,7 +499,20 @@ internal partial class ConsoleManager : IServiceType
public override bool Invoke(IEnumerable<object> arguments)
{
var first = arguments.FirstOrDefault();
if (first == null || first.GetType() != typeof(T))
if (first == null)
{
// Invert the value if it's a boolean
if (this.Value is bool boolValue)
{
this.Value = (T)(object)!boolValue;
}
Log.WriteLog(LogEventLevel.Information, "{VariableName} = {VariableValue}", null, this.Name, this.Value);
return true;
}
if (first.GetType() != typeof(T))
throw new ArgumentException($"Console variable must be set with an argument of type {typeof(T).Name}.");
this.Value = (T)first;

View file

@ -21,7 +21,8 @@ namespace Dalamud.Console;
#pragma warning restore SA1015
public class ConsoleManagerPluginScoped : IConsole, IInternalDisposableService
{
private readonly ConsoleManager console;
[ServiceManager.ServiceDependency]
private readonly ConsoleManager console = Service<ConsoleManager>.Get();
private readonly List<IConsoleEntry> trackedEntries = new();
@ -29,12 +30,9 @@ public class ConsoleManagerPluginScoped : IConsole, IInternalDisposableService
/// Initializes a new instance of the <see cref="ConsoleManagerPluginScoped"/> class.
/// </summary>
/// <param name="plugin">The plugin this service belongs to.</param>
/// <param name="console">The console manager.</param>
[ServiceManager.ServiceConstructor]
internal ConsoleManagerPluginScoped(LocalPlugin plugin, ConsoleManager console)
internal ConsoleManagerPluginScoped(LocalPlugin plugin)
{
this.console = console;
this.Prefix = ConsoleManagerPluginUtil.GetSanitizedNamespaceName(plugin.InternalName);
}