Skip to content

Commit

Permalink
Changed commands
Browse files Browse the repository at this point in the history
  • Loading branch information
RisaDev committed Jan 27, 2024
1 parent 5faa21c commit 18defd3
Show file tree
Hide file tree
Showing 2 changed files with 97 additions and 46 deletions.
142 changes: 97 additions & 45 deletions CustomizePlus/Core/Services/CommandService.cs
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,8 @@
using CustomizePlus.Game.Services;
using CustomizePlus.UI.Windows.MainWindow.Tabs.Templates;
using CustomizePlus.UI.Windows.MainWindow;
using static System.Windows.Forms.AxHost;
using CustomizePlus.Profiles.Data;

namespace CustomizePlus.Core.Services;

Expand Down Expand Up @@ -76,11 +78,8 @@ private void OnMainCommand(string command, string arguments)
{
switch (argumentList[0].ToLowerInvariant())
{
case "apply":
Apply(argument);
return;
case "toggle":
Apply(argument, true);
case "profile":
ProfileCommand(argument);
return;
default:
case "help":
Expand All @@ -100,83 +99,136 @@ private bool PrintHelp(string argument)
else
_chatService.PrintInChat(new SeStringBuilder().AddText("Valid arguments for /customize are:").BuiltString);

_chatService.PrintInChat(new SeStringBuilder().AddCommand("apply", "Applies a given profile for a given character. Use without arguments for help.")
_chatService.PrintInChat(new SeStringBuilder().AddCommand("profile", "Change the state of profiles. Use without arguments for help.")
.BuiltString);
_chatService.PrintInChat(new SeStringBuilder().AddCommand("toggle", "Toggles a given profile for a given character. Use without arguments for help.")
.BuiltString);
return true;
}

private void Apply(string argument, bool toggle = false)
private void ProfileCommand(string argument)
{
var argumentList = argument.Split(',', 2, StringSplitOptions.RemoveEmptyEntries | StringSplitOptions.TrimEntries);
if (argumentList.Length != 2)
var argumentList = argument.Split(' ', 2, StringSplitOptions.RemoveEmptyEntries | StringSplitOptions.TrimEntries);
string[]? subArgumentList = null;

if(argumentList.Length == 2)
subArgumentList = argumentList[1].Split(',', 2, StringSplitOptions.RemoveEmptyEntries | StringSplitOptions.TrimEntries);

bool isTurningOffAllProfiles = subArgumentList != null && subArgumentList.Length != 2 && argumentList[0] == "disable";

if (argumentList.Length != 2 || subArgumentList == null || (subArgumentList.Length != 2 && !isTurningOffAllProfiles))
{
_chatService.PrintInChat(new SeStringBuilder().AddText($"Usage: /customize {(toggle ? "toggle" : "apply")} ").AddBlue("Character Name", true)
_chatService.PrintInChat(new SeStringBuilder().AddText($"Usage: /customize profile ")
.AddBlue("enable, disable or toggle", true)
.AddText(" ")
.AddRed("Character Name", true)
.AddText(",")
.AddRed("Profile Name", true)
.AddYellow("Profile Name", true)
.BuiltString);
_chatService.PrintInChat(new SeStringBuilder().AddText(" 》 ").AddBlue("disable", true)
.AddText(" option can also be used without supplying profile name to turn off currently active profile for the character").BuiltString);
_chatService.PrintInChat(new SeStringBuilder().AddText(" 》 ")
.AddBlue("Character Name", true).AddText("can be either full character name or one of the following:").BuiltString);
.AddRed("Character Name", true).AddText(" can be either full character name or one of the following:").BuiltString);
_chatService.PrintInChat(new SeStringBuilder().AddText(" 》 To apply to yourself: ").AddBlue("<me>").AddText(", ").AddBlue("self").BuiltString);
_chatService.PrintInChat(new SeStringBuilder().AddText(" 》 To apply to target: ").AddBlue("<t>").AddText(", ").AddBlue("target").BuiltString);
return;
}

string charaName = "", profName = "";
string characterName = "", profileName = "";

try
{
(charaName, profName) = argumentList switch { var a => (a[0].Trim(), a[1].Trim()) };
if (!_profileManager.Profiles.Any())
{
_chatService.PrintInChat(new SeStringBuilder().AddText("This command cannot be executed because no profiles exist").BuiltString);
return;
}

bool? state = null;
switch (argumentList[0].ToLowerInvariant())
{
case "enabled":
case "enable":
case "on":
case "true":
state = true;
break;
case "disabled":
case "disable":
case "off":
case "false":
state = false;
break;
case "toggle":
case "switch":
break;
}

Profile? targetProfile = null;

charaName = charaName switch
characterName = subArgumentList[0].Trim();
characterName = characterName switch
{
"<me>" => _gameObjectService.GetCurrentPlayerName() ?? string.Empty,
"self" => _gameObjectService.GetCurrentPlayerName() ?? string.Empty,
"<t>" => _gameObjectService.GetCurrentPlayerTargetName() ?? string.Empty,
"target" => _gameObjectService.GetCurrentPlayerTargetName() ?? string.Empty,
_ => charaName,
_ => characterName,
};

if (!_profileManager.Profiles.Any())
if (!isTurningOffAllProfiles)
{
_chatService.PrintInChat(
$"Can't {(toggle ? "toggle" : "apply")} profile \"{profName}\" for character \"{charaName}\" because no profiles exist", ChatService.ChatMessageColor.Error);
return;
profileName = subArgumentList[1].Trim();
targetProfile = _profileManager.Profiles.FirstOrDefault(x => x.Name == profileName && x.CharacterName == characterName);
}
else
targetProfile = _profileManager.Profiles.FirstOrDefault(x => x.CharacterName == characterName && x.Enabled);

if (_profileManager.Profiles.Count(x => x.Name == profName && x.CharacterName == charaName) > 1)
if (targetProfile == null)
{
_logger.Warning(
$"Found more than one profile matching profile \"{profName}\" and character \"{charaName}\". Using first match.");
_chatService.PrintInChat(new SeStringBuilder()
.AddText("Cannot execute command because profile ")
.AddYellow(string.IsNullOrWhiteSpace(profileName) ? "[Any enabled profile]" : profileName)
.AddText(" for ")
.AddRed(characterName)
.AddText(" was not found").BuiltString);
return;
}

var outProf = _profileManager.Profiles.FirstOrDefault(x => x.Name == profName && x.CharacterName == charaName);

if (outProf == null)
if (state != null)
{
_chatService.PrintInChat(
$"Can't {(toggle ? "toggle" : "apply")} profile \"{(string.IsNullOrWhiteSpace(profName) ? "empty (none provided)" : profName)}\" " +
$"for Character \"{(string.IsNullOrWhiteSpace(charaName) ? "empty (none provided)" : charaName)}\"\n" +
"Check if the profile and character names were provided correctly and said profile exists for chosen character", ChatService.ChatMessageColor.Error);
return;
}
if(targetProfile.Enabled == state)
{
_chatService.PrintInChat(new SeStringBuilder()
.AddText("Profile ")
.AddYellow(targetProfile.Name)
.AddText(" for ")
.AddBlue(characterName)
.AddText(" is already ")
.AddGreen((bool)state ? "enabled" : "disabled").BuiltString);
return;
}

if (!toggle)
_profileManager.SetEnabled(outProf, true);
_profileManager.SetEnabled(targetProfile, (bool)state);
}
else
_profileManager.SetEnabled(outProf, !outProf.Enabled);

_chatService.PrintInChat(
$"{outProf.Name} was successfully {(toggle ? "toggled" : "applied")} for {outProf.CharacterName}", ChatService.ChatMessageColor.Info);
_profileManager.SetEnabled(targetProfile, !targetProfile.Enabled);

_chatService.PrintInChat(new SeStringBuilder()
.AddText("Profile ")
.AddYellow(targetProfile.Name)
.AddText(" was successfully ")
.AddBlue(state != null ? ((bool)state ? "enabled" : "disabled") : "toggled")
.AddText(" for ")
.AddRed(targetProfile.CharacterName).BuiltString);
}
catch (Exception e)
{
_chatService.PrintInChat($"Error while {(toggle ? "toggling" : "applying")} profile, details are available in dalamud log", ChatService.ChatMessageColor.Error);
_logger.Error($"Error {(toggle ? "toggling" : "applying")} profile by command: \n" +
$"Profile name \"{(string.IsNullOrWhiteSpace(profName) ? "empty (none provided)" : profName)}\"\n" +
$"Character name \"{(string.IsNullOrWhiteSpace(charaName) ? "empty (none provided)" : charaName)}\"\n" +
$"Error: {e}");
_chatService.PrintInChat(new SeStringBuilder()
.AddRed($"Error while changing state of profile, details are available in dalamud log").BuiltString);

_logger.Error($"Error while changing state of profile by command:\n" +
$"Profile name \"{(string.IsNullOrWhiteSpace(profileName) ? "empty (none provided)" : profileName)}\"\n" +
$"Character name \"{(string.IsNullOrWhiteSpace(characterName) ? "empty (none provided)" : characterName)}\"\n" +
$"Arguments: {argument}\nError: {e}");
}
}
}
1 change: 0 additions & 1 deletion CustomizePlus/UI/Windows/CPlusChangeLog.cs
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,6 @@ private static void Add2_0_0_0(Changelog log)
.RegisterEntry("All bone edits are now stored in templates which can be used by multiple profiles and single profile can reference unlimited number of templates.", 2)

.RegisterImportant("Chat commands have been changed, refer to \"/customize help\" for information about available commands.", 1)
.RegisterEntry("Added \"toggle\" chat command which can be used to toggle given profile on a given character.", 2)

.RegisterEntry("Profiles can be applied to summons, mounts and pets without any limitations.", 1)
.RegisterImportant("Root scaling of mounts is not available for now.", 2)
Expand Down

0 comments on commit 18defd3

Please sign in to comment.