forked from daemitus/SomethingNeedDoing
-
Notifications
You must be signed in to change notification settings - Fork 31
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
6 changed files
with
271 additions
and
9 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,81 @@ | ||
using System; | ||
using System.Linq; | ||
using System.Text.RegularExpressions; | ||
using System.Threading; | ||
using System.Threading.Tasks; | ||
|
||
using Dalamud.Game.ClientState.Keys; | ||
using Dalamud.Logging; | ||
using SomethingNeedDoing.Exceptions; | ||
using SomethingNeedDoing.Grammar.Modifiers; | ||
using SomethingNeedDoing.Misc; | ||
|
||
namespace SomethingNeedDoing.Grammar.Commands; | ||
|
||
/// <summary> | ||
/// The /send command. | ||
/// </summary> | ||
internal class HoldCommand : MacroCommand | ||
{ | ||
private static readonly Regex Regex = new(@"^/hold\s+(?<name>.*?)\s*$", RegexOptions.Compiled | RegexOptions.IgnoreCase); | ||
|
||
private readonly VirtualKey[] vkCodes; | ||
|
||
/// <summary> | ||
/// Initializes a new instance of the <see cref="HoldCommand"/> class. | ||
/// </summary> | ||
/// <param name="text">Original text.</param> | ||
/// <param name="vkCodes">VirtualKey codes.</param> | ||
/// <param name="wait">Wait value.</param> | ||
private HoldCommand(string text, VirtualKey[] vkCodes, WaitModifier wait) | ||
: base(text, wait) | ||
{ | ||
this.vkCodes = vkCodes; | ||
} | ||
|
||
/// <summary> | ||
/// Parse the text as a command. | ||
/// </summary> | ||
/// <param name="text">Text to parse.</param> | ||
/// <returns>A parsed command.</returns> | ||
public static HoldCommand Parse(string text) | ||
{ | ||
_ = WaitModifier.TryParse(ref text, out var waitModifier); | ||
|
||
var match = Regex.Match(text); | ||
if (!match.Success) | ||
throw new MacroSyntaxError(text); | ||
|
||
var nameValue = ExtractAndUnquote(match, "name"); | ||
var vkCodes = nameValue.Split("+") | ||
.Select(name => | ||
{ | ||
if (!Enum.TryParse<VirtualKey>(name, true, out var vkCode)) | ||
throw new MacroCommandError("Invalid virtual key"); | ||
return vkCode; | ||
}) | ||
.ToArray(); | ||
|
||
return new HoldCommand(text, vkCodes, waitModifier); | ||
} | ||
|
||
/// <inheritdoc/> | ||
public async override Task Execute(ActiveMacro macro, CancellationToken token) | ||
{ | ||
Service.Log.Debug($"Executing: {this.Text}"); | ||
|
||
if (this.vkCodes.Length == 1) | ||
{ | ||
Keyboard.Hold(this.vkCodes[0]); | ||
} | ||
else | ||
{ | ||
var key = this.vkCodes.Last(); | ||
var mods = this.vkCodes.SkipLast(1); | ||
Keyboard.Hold(key, mods); | ||
} | ||
|
||
await this.PerformWait(token); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,81 @@ | ||
using System; | ||
using System.Linq; | ||
using System.Text.RegularExpressions; | ||
using System.Threading; | ||
using System.Threading.Tasks; | ||
|
||
using Dalamud.Game.ClientState.Keys; | ||
using Dalamud.Logging; | ||
using SomethingNeedDoing.Exceptions; | ||
using SomethingNeedDoing.Grammar.Modifiers; | ||
using SomethingNeedDoing.Misc; | ||
|
||
namespace SomethingNeedDoing.Grammar.Commands; | ||
|
||
/// <summary> | ||
/// The /send command. | ||
/// </summary> | ||
internal class ReleaseCommand : MacroCommand | ||
{ | ||
private static readonly Regex Regex = new(@"^/release\s+(?<name>.*?)\s*$", RegexOptions.Compiled | RegexOptions.IgnoreCase); | ||
|
||
private readonly VirtualKey[] vkCodes; | ||
|
||
/// <summary> | ||
/// Initializes a new instance of the <see cref="ReleaseCommand"/> class. | ||
/// </summary> | ||
/// <param name="text">Original text.</param> | ||
/// <param name="vkCodes">VirtualKey codes.</param> | ||
/// <param name="wait">Wait value.</param> | ||
private ReleaseCommand(string text, VirtualKey[] vkCodes, WaitModifier wait) | ||
: base(text, wait) | ||
{ | ||
this.vkCodes = vkCodes; | ||
} | ||
|
||
/// <summary> | ||
/// Parse the text as a command. | ||
/// </summary> | ||
/// <param name="text">Text to parse.</param> | ||
/// <returns>A parsed command.</returns> | ||
public static ReleaseCommand Parse(string text) | ||
{ | ||
_ = WaitModifier.TryParse(ref text, out var waitModifier); | ||
|
||
var match = Regex.Match(text); | ||
if (!match.Success) | ||
throw new MacroSyntaxError(text); | ||
|
||
var nameValue = ExtractAndUnquote(match, "name"); | ||
var vkCodes = nameValue.Split("+") | ||
.Select(name => | ||
{ | ||
if (!Enum.TryParse<VirtualKey>(name, true, out var vkCode)) | ||
throw new MacroCommandError("Invalid virtual key"); | ||
return vkCode; | ||
}) | ||
.ToArray(); | ||
|
||
return new ReleaseCommand(text, vkCodes, waitModifier); | ||
} | ||
|
||
/// <inheritdoc/> | ||
public async override Task Execute(ActiveMacro macro, CancellationToken token) | ||
{ | ||
Service.Log.Debug($"Executing: {this.Text}"); | ||
|
||
if (this.vkCodes.Length == 1) | ||
{ | ||
Keyboard.Release(this.vkCodes[0]); | ||
} | ||
else | ||
{ | ||
var key = this.vkCodes.Last(); | ||
var mods = this.vkCodes.SkipLast(1); | ||
Keyboard.Release(key, mods); | ||
} | ||
|
||
await this.PerformWait(token); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters