Merge pull request #271 from SoyaX/tagCommand

Add tag command to CommandHandler
This commit is contained in:
Ottermandias 2023-01-05 10:45:05 +01:00 committed by GitHub
commit ef962393c4
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23

View file

@ -1,4 +1,5 @@
using System; using System;
using System.Linq;
using Dalamud.Game.Command; using Dalamud.Game.Command;
using Dalamud.Game.Text.SeStringHandling; using Dalamud.Game.Text.SeStringHandling;
using ImGuiNET; using ImGuiNET;
@ -109,6 +110,7 @@ public class CommandHandler : IDisposable
"debug" => SetDebug( arguments ), "debug" => SetDebug( arguments ),
"collection" => SetCollection( arguments ), "collection" => SetCollection( arguments ),
"mod" => SetMod( arguments ), "mod" => SetMod( arguments ),
"tag" => SetTag( arguments ),
_ => PrintHelp( argumentList[ 0 ] ), _ => PrintHelp( argumentList[ 0 ] ),
}; };
} }
@ -137,6 +139,7 @@ public class CommandHandler : IDisposable
Dalamud.Chat.Print( new SeStringBuilder().AddCommand( "collection", "Change your active collection setup. Use without further parameters for more detailed help." ) Dalamud.Chat.Print( new SeStringBuilder().AddCommand( "collection", "Change your active collection setup. Use without further parameters for more detailed help." )
.BuiltString ); .BuiltString );
Dalamud.Chat.Print( new SeStringBuilder().AddCommand( "mod", "Change a specific mods settings. Use without further parameters for more detailed help." ).BuiltString ); Dalamud.Chat.Print( new SeStringBuilder().AddCommand( "mod", "Change a specific mods settings. Use without further parameters for more detailed help." ).BuiltString );
Dalamud.Chat.Print( new SeStringBuilder().AddCommand( "tag", "Change multiple mods settings based on their tags. Use without further parameters for more detailed help." ).BuiltString );
return true; return true;
} }
@ -443,6 +446,101 @@ public class CommandHandler : IDisposable
return false; return false;
} }
private bool SetTag( string arguments )
{
if( arguments.Length == 0 )
{
var seString = new SeStringBuilder()
.AddText( "Use with /penumbra tag " ).AddBlue( "[enable|disable|toggle|inherit]" ).AddText( " " ).AddYellow( "[Collection Name]" ).AddText( " | " )
.AddPurple( "[Local Tag]" );
Dalamud.Chat.Print( seString.BuiltString );
return true;
}
var split = arguments.Split( ' ', 2, StringSplitOptions.TrimEntries | StringSplitOptions.RemoveEmptyEntries );
var nameSplit = split.Length != 2 ? Array.Empty< string >() : split[ 1 ].Split( '|', 2, StringSplitOptions.TrimEntries | StringSplitOptions.RemoveEmptyEntries );
if( nameSplit.Length != 2 )
{
Dalamud.Chat.Print( "Not enough arguments provided." );
return false;
}
var state = split[ 0 ].ToLowerInvariant() switch
{
"enable" => 0,
"enabled" => 0,
"disable" => 1,
"disabled" => 1,
"toggle" => 2,
"inherit" => 3,
"inherited" => 3,
_ => -1,
};
if( state == -1 )
{
Dalamud.Chat.Print( new SeStringBuilder().AddRed( split[ 0 ], true ).AddText( " is not a valid type of setting." ).BuiltString );
return false;
}
if( !GetModCollection( nameSplit[ 0 ], out var collection ) || collection == ModCollection.Empty )
{
return false;
}
var mods = _modManager.Where( m => m.LocalTags.Contains( nameSplit[ 1 ], StringComparer.InvariantCultureIgnoreCase ) ).ToArray();
if( mods.Length == 0 )
{
Dalamud.Chat.Print( new SeStringBuilder().AddText( "The tag " ).AddRed( nameSplit[ 1 ], true ).AddText( " does not match any mods." ).BuiltString );
return false;
}
foreach( var mod in mods )
{
var settings = collection!.Settings[ mod.Index ];
switch( state )
{
case 0:
if( collection.SetModState( mod.Index, true ) )
{
Dalamud.Chat.Print( new SeStringBuilder().AddText( "Enabled mod " ).AddPurple( mod.Name, true ).AddText( " in collection " ).AddYellow( collection.Name, true )
.AddText( "." ).BuiltString );
}
break;
case 1:
if( collection.SetModState( mod.Index, false ) )
{
Dalamud.Chat.Print( new SeStringBuilder().AddText( "Disabled mod " ).AddPurple( mod.Name, true ).AddText( " in collection " ).AddYellow( collection.Name, true )
.AddText( "." ).BuiltString );
}
break;
case 2:
var setting = !( settings?.Enabled ?? false );
if( collection.SetModState( mod.Index, setting ) )
{
Dalamud.Chat.Print( new SeStringBuilder().AddText( setting ? "Enabled mod " : "Disabled mod " ).AddPurple( mod.Name, true ).AddText( " in collection " )
.AddYellow( collection.Name, true )
.AddText( "." ).BuiltString );
}
break;
case 3:
if( collection.SetModInheritance( mod.Index, true ) )
{
Dalamud.Chat.Print( new SeStringBuilder().AddText( "Set mod " ).AddPurple( mod.Name, true ).AddText( " in collection " ).AddYellow( collection.Name, true )
.AddText( " to inherit." ).BuiltString );
}
break;
}
}
return true;
}
private bool GetModCollection( string collectionName, out ModCollection? collection ) private bool GetModCollection( string collectionName, out ModCollection? collection )
{ {
var lowerName = collectionName.ToLowerInvariant(); var lowerName = collectionName.ToLowerInvariant();