mirror of
https://github.com/goatcorp/Dalamud.git
synced 2026-02-15 12:27:43 +01:00
Add Argument parser
This commit is contained in:
parent
a90a5673f2
commit
693babc1e3
8 changed files with 87 additions and 15 deletions
|
|
@ -13,14 +13,19 @@ namespace Dalamud.Bootstrap.SqexArg
|
|||
m_dict = new Dictionary<string, string>();
|
||||
}
|
||||
|
||||
public ArgumentBuilder(IEnumerable<KeyValuePair<string, string>> collection)
|
||||
{
|
||||
m_dict = new Dictionary<string, string>(collection);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Creates an argument builder from the argument (e.g. /T =1234)
|
||||
/// </summary>
|
||||
/// <param name="argument"></param>
|
||||
/// <returns></returns>
|
||||
public static ArgumentBuilder Parse(string argument)
|
||||
public static ArgumentBuilder Parse(ReadOnlySpan<char> argument)
|
||||
{
|
||||
throw new NotImplementedException("TODO");
|
||||
return new ArgumentBuilder(ArgumentParser.Parse(argument));
|
||||
}
|
||||
|
||||
public ArgumentBuilder Add(string key, string value)
|
||||
|
|
|
|||
51
Dalamud.Bootstrap/SqexArg/ArgumentParser.cs
Normal file
51
Dalamud.Bootstrap/SqexArg/ArgumentParser.cs
Normal file
|
|
@ -0,0 +1,51 @@
|
|||
using System;
|
||||
using System.Collections.Generic;
|
||||
using Pidgin;
|
||||
using static Pidgin.Parser;
|
||||
using static Pidgin.Parser<char>;
|
||||
|
||||
namespace Dalamud.Bootstrap.SqexArg
|
||||
{
|
||||
public static class ArgumentParser
|
||||
{
|
||||
private static readonly Parser<char, string> KeyMarkerNoEscape = String(" ");
|
||||
|
||||
private static readonly Parser<char, string> KeyMarkerEscape = String(" /");
|
||||
|
||||
private static readonly Parser<char, string> KeyMarker = Try(KeyMarkerEscape).Or(KeyMarkerNoEscape);
|
||||
|
||||
private static readonly Parser<char, string> ValueMarker = String(" =");
|
||||
|
||||
private static readonly Parser<char, char> EscapedSpace = String(" ").ThenReturn(' ');
|
||||
|
||||
//private static readonly Parser<char, string> String = Try(EscapedSpace).Or(AnyCharExcept(' ')).ManyString();
|
||||
private static readonly Parser<char, string> String = Try(EscapedSpace).Or(AnyCharExcept(' ')).ManyString();
|
||||
|
||||
private static readonly Parser<char, KeyValuePair<string, string>> KeyValue = Map
|
||||
(
|
||||
(_, key, _, value) => new KeyValuePair<string, string>(key, value),
|
||||
KeyMarker, String, ValueMarker, String
|
||||
);
|
||||
|
||||
private static readonly Parser<char, IEnumerable<KeyValuePair<string, string>>> Parser = KeyValue.Many();
|
||||
|
||||
/// <summary>
|
||||
/// Parses the argument
|
||||
/// </summary>
|
||||
/// <param name="input"></param>
|
||||
/// <returns></returns>
|
||||
/// <exception cref="SqexArgException">Thrown when failed to parse the input.</exception>
|
||||
public static IEnumerable<KeyValuePair<string, string>> Parse(ReadOnlySpan<char> input)
|
||||
{
|
||||
var test = KeyMarker.Parse(input);
|
||||
var result = Parser.Parse(input);
|
||||
|
||||
if (!result.Success)
|
||||
{
|
||||
throw new SqexArgException($"Failed to parse the argument.\n{result.Error}");
|
||||
}
|
||||
|
||||
return result.Value;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
@ -1,6 +1,7 @@
|
|||
using System;
|
||||
using System.Buffers;
|
||||
using System.Buffers.Text;
|
||||
using System.Runtime.InteropServices;
|
||||
using System.Text;
|
||||
using System.Text.RegularExpressions;
|
||||
using Dalamud.Bootstrap.Crypto;
|
||||
|
|
@ -90,7 +91,7 @@ namespace Dalamud.Bootstrap.SqexArg
|
|||
// plainText <- utf8Bytes <- encryptedBytes(payload) <- base64(payloadStr)
|
||||
|
||||
// base64: 3 bytes per 4 characters
|
||||
// We also want the size to be aligned with the block size.
|
||||
// We also want the size to be aligned with the block size. Usually this is not a problem but we don't know what payload is actually from.
|
||||
var dataLength = (payload.Length / 4) * 3;
|
||||
var alignedDataLength = AlignBufferLength(dataLength);
|
||||
var encryptedData = new byte[alignedDataLength];
|
||||
|
|
@ -112,9 +113,14 @@ namespace Dalamud.Bootstrap.SqexArg
|
|||
blowfish.Decrypt(encryptedData, decryptedData);
|
||||
|
||||
// utf8Bytes -> C# string
|
||||
var plainText = Encoding.UTF8.GetString(decryptedData[..dataLength]);
|
||||
|
||||
return plainText;
|
||||
// notice that decryptedData is a null terminated string.
|
||||
unsafe
|
||||
{
|
||||
fixed (byte* pDecryptedData = decryptedData)
|
||||
{
|
||||
return Marshal.PtrToStringUTF8(new IntPtr(pDecryptedData));
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue