Compare commits
2
Commits
0.40
...
feat/script
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
45f55f94cd | ||
|
|
d4aae4508a |
No files matched your search
@@ -29,3 +29,4 @@ node_modules/
|
||||
|
||||
# Rider
|
||||
.idea
|
||||
_ReSharper.Caches/
|
||||
@@ -0,0 +1,19 @@
|
||||
<Project Sdk="MSTest.Sdk/3.6.4">
|
||||
|
||||
<PropertyGroup>
|
||||
<TargetFramework>net9.0</TargetFramework>
|
||||
<LangVersion>latest</LangVersion>
|
||||
<ImplicitUsings>enable</ImplicitUsings>
|
||||
<Nullable>enable</Nullable>
|
||||
<UseVSTest>true</UseVSTest>
|
||||
</PropertyGroup>
|
||||
|
||||
<ItemGroup>
|
||||
<PackageReference Include="coverlet.collector" Version="6.0.2" />
|
||||
</ItemGroup>
|
||||
|
||||
<ItemGroup>
|
||||
<ProjectReference Include="..\BetterGenshinImpact.CombatScript\BetterGenshinImpact.CombatScript.csproj" />
|
||||
</ItemGroup>
|
||||
|
||||
</Project>
|
||||
@@ -0,0 +1,52 @@
|
||||
namespace BetterGenshinImpact.CombatScript.Test;
|
||||
|
||||
[TestClass]
|
||||
public sealed class ScriptEmitTest
|
||||
{
|
||||
[TestMethod]
|
||||
public void TestEmit()
|
||||
{
|
||||
ScriptUnit scriptUnit = new(
|
||||
[
|
||||
new CommentSymbol("测试注释"),
|
||||
new LineBreakTriviaSymbol(),
|
||||
new AvatarInstructionListSymbol(new("钟离"), [new SpaceTriviaSymbol()], new(
|
||||
[
|
||||
new WalkSymbol(WalkDirection.Backward, [new DoubleSymbol(0.1)], [], new CommaTriviaSymbol()),
|
||||
new SkillSymbol(true, [new HoldSymbol()], [new SpaceTriviaSymbol()], new CommaTriviaSymbol()),
|
||||
new WaitSymbol([new DoubleSymbol(0.3)], [new SpaceTriviaSymbol()], new CommaTriviaSymbol()),
|
||||
new WalkSymbol(WalkDirection.Forward, [new DoubleSymbol(0.1)], [new SpaceTriviaSymbol()], default),
|
||||
])),
|
||||
new LineBreakTriviaSymbol(),
|
||||
new AvatarInstructionListSymbol(new("芙宁娜"), [new SpaceTriviaSymbol()], new(
|
||||
[
|
||||
new SkillSymbol(true, [], new CommaTriviaSymbol()),
|
||||
new BurstSymbol(true, [new SpaceTriviaSymbol()], default)
|
||||
])),
|
||||
new LineBreakTriviaSymbol(),
|
||||
new AvatarInstructionListSymbol(new("行秋"), [new SpaceTriviaSymbol()], new(
|
||||
[
|
||||
new SkillSymbol(true, [], new CommaTriviaSymbol()),
|
||||
new BurstSymbol(true, [new SpaceTriviaSymbol()], new CommaTriviaSymbol()),
|
||||
new SkillSymbol(true, [new SpaceTriviaSymbol()], default),
|
||||
])),
|
||||
]);
|
||||
|
||||
Console.WriteLine(scriptUnit.Emit(new SymbolEmitter()));
|
||||
}
|
||||
|
||||
[TestMethod]
|
||||
public void TestParse()
|
||||
{
|
||||
ReadOnlySpan<char> raw = """
|
||||
// 测试注释
|
||||
钟离 s(0.1),e(hold),wait(0.3),w(0.1)
|
||||
芙宁娜 e,q
|
||||
|
||||
行秋 e,q,e
|
||||
""";
|
||||
ScriptUnit scriptUnit = SymbolParser.Parse(raw);
|
||||
|
||||
Console.WriteLine(scriptUnit.Emit(new SymbolEmitter()));
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,31 @@
|
||||
using System;
|
||||
using System.Collections.Immutable;
|
||||
|
||||
namespace BetterGenshinImpact.CombatScript;
|
||||
|
||||
public class AttackSymbol : InstructionSymbol, IInstructionSymbolHasDuration
|
||||
{
|
||||
public AttackSymbol(ImmutableArray<IParameterSymbol> parameterList, ImmutableArray<TriviaSymbol> leadingTriviaList, TriviaSymbol? tailingTrivia)
|
||||
: base("attack", parameterList, leadingTriviaList, tailingTrivia)
|
||||
{
|
||||
InstructionThrowHelper.ThrowIfParameterListIsDefault(parameterList);
|
||||
InstructionThrowHelper.ThrowIfParameterListCountNotCorrect(parameterList, [0, 1]);
|
||||
|
||||
if (parameterList.Length is 1)
|
||||
{
|
||||
InstructionThrowHelper.ThrowIfParameterAtIndexIsNot(parameterList, 0, out DoubleSymbol doubleSymbol);
|
||||
|
||||
HasDuration = true;
|
||||
Duration = TimeSpan.FromSeconds(doubleSymbol.Value);
|
||||
}
|
||||
}
|
||||
|
||||
public AttackSymbol(ImmutableArray<TriviaSymbol> leadingTriviaList, TriviaSymbol? tailingTrivia)
|
||||
: base("attack", leadingTriviaList, tailingTrivia)
|
||||
{
|
||||
}
|
||||
|
||||
public bool HasDuration { get; }
|
||||
|
||||
public TimeSpan Duration { get; }
|
||||
}
|
||||
@@ -0,0 +1,24 @@
|
||||
using System.Collections.Immutable;
|
||||
|
||||
namespace BetterGenshinImpact.CombatScript;
|
||||
|
||||
public sealed class AvatarInstructionListSymbol : BaseSymbol
|
||||
{
|
||||
public AvatarInstructionListSymbol(AvatarSymbol avatar, ImmutableArray<TriviaSymbol> triviaList, InstructionListSymbol instructionList)
|
||||
{
|
||||
Avatar = avatar;
|
||||
TriviaList = triviaList;
|
||||
InstructionList = instructionList;
|
||||
}
|
||||
|
||||
public AvatarSymbol Avatar { get; }
|
||||
|
||||
public ImmutableArray<TriviaSymbol> TriviaList { get; }
|
||||
|
||||
public InstructionListSymbol InstructionList { get; }
|
||||
|
||||
public override void Emit(ISymbolEmitter emitter)
|
||||
{
|
||||
emitter.Append(Avatar).Append(TriviaList).Append(InstructionList);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,16 @@
|
||||
namespace BetterGenshinImpact.CombatScript;
|
||||
|
||||
public sealed class AvatarSymbol : BaseSymbol
|
||||
{
|
||||
public AvatarSymbol(string name)
|
||||
{
|
||||
Name = name;
|
||||
}
|
||||
|
||||
public string Name { get; }
|
||||
|
||||
public override void Emit(ISymbolEmitter emitter)
|
||||
{
|
||||
emitter.Append(Name);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,6 @@
|
||||
namespace BetterGenshinImpact.CombatScript;
|
||||
|
||||
public abstract class BaseSymbol : ISymbol
|
||||
{
|
||||
public abstract void Emit(ISymbolEmitter emitter);
|
||||
}
|
||||
@@ -0,0 +1,9 @@
|
||||
<Project Sdk="Microsoft.NET.Sdk">
|
||||
|
||||
<PropertyGroup>
|
||||
<TargetFramework>net9.0</TargetFramework>
|
||||
<ImplicitUsings>disable</ImplicitUsings>
|
||||
<Nullable>enable</Nullable>
|
||||
</PropertyGroup>
|
||||
|
||||
</Project>
|
||||
@@ -0,0 +1,25 @@
|
||||
using System.Collections.Immutable;
|
||||
|
||||
namespace BetterGenshinImpact.CombatScript;
|
||||
|
||||
public class BurstSymbol : InstructionSymbol, IInstructionSymbolHasAlias
|
||||
{
|
||||
public BurstSymbol(bool isAlias, ImmutableArray<IParameterSymbol> parameterList, ImmutableArray<TriviaSymbol> leadingTriviaList, TriviaSymbol? tailingTrivia)
|
||||
: base("burst", parameterList, leadingTriviaList, tailingTrivia)
|
||||
{
|
||||
InstructionThrowHelper.ThrowIfParameterListIsDefault(parameterList);
|
||||
InstructionThrowHelper.ThrowIfParameterListCountNotCorrect(parameterList, [0]);
|
||||
|
||||
IsAlias = isAlias;
|
||||
}
|
||||
|
||||
public BurstSymbol(bool isAlias, ImmutableArray<TriviaSymbol> leadingTriviaList, TriviaSymbol? tailingTrivia)
|
||||
: base("burst", leadingTriviaList, tailingTrivia)
|
||||
{
|
||||
IsAlias = isAlias;
|
||||
}
|
||||
|
||||
public string AliasName { get; } = "q";
|
||||
|
||||
public bool IsAlias { get; }
|
||||
}
|
||||
@@ -0,0 +1,31 @@
|
||||
using System;
|
||||
using System.Collections.Immutable;
|
||||
|
||||
namespace BetterGenshinImpact.CombatScript;
|
||||
|
||||
public class ChargeSymbol : InstructionSymbol, IInstructionSymbolHasDuration
|
||||
{
|
||||
public ChargeSymbol(ImmutableArray<IParameterSymbol> parameterList, ImmutableArray<TriviaSymbol> leadingTriviaList, TriviaSymbol? tailingTrivia)
|
||||
: base("charge", parameterList, leadingTriviaList, tailingTrivia)
|
||||
{
|
||||
InstructionThrowHelper.ThrowIfParameterListIsDefault(parameterList);
|
||||
InstructionThrowHelper.ThrowIfParameterListCountNotCorrect(parameterList, [0, 1]);
|
||||
|
||||
if (parameterList.Length is 1)
|
||||
{
|
||||
InstructionThrowHelper.ThrowIfParameterAtIndexIsNot(parameterList, 0, out DoubleSymbol doubleSymbol);
|
||||
|
||||
HasDuration = true;
|
||||
Duration = TimeSpan.FromSeconds(doubleSymbol.Value);
|
||||
}
|
||||
}
|
||||
|
||||
public ChargeSymbol(ImmutableArray<TriviaSymbol> leadingTriviaList, TriviaSymbol? tailingTrivia)
|
||||
: base("charge", leadingTriviaList, tailingTrivia)
|
||||
{
|
||||
}
|
||||
|
||||
public bool HasDuration { get; }
|
||||
|
||||
public TimeSpan Duration { get; }
|
||||
}
|
||||
@@ -0,0 +1,17 @@
|
||||
namespace BetterGenshinImpact.CombatScript;
|
||||
|
||||
public sealed class CommaTriviaSymbol : TriviaSymbol
|
||||
{
|
||||
public override void Emit(ISymbolEmitter emitter)
|
||||
{
|
||||
emitter.Append(',');
|
||||
}
|
||||
}
|
||||
|
||||
public sealed class SemicolonTriviaSymbol : TriviaSymbol
|
||||
{
|
||||
public override void Emit(ISymbolEmitter emitter)
|
||||
{
|
||||
emitter.Append(';');
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,16 @@
|
||||
namespace BetterGenshinImpact.CombatScript;
|
||||
|
||||
public sealed class CommentSymbol : TriviaSymbol
|
||||
{
|
||||
public CommentSymbol(string comment)
|
||||
{
|
||||
Comment = comment;
|
||||
}
|
||||
|
||||
public string Comment { get; }
|
||||
|
||||
public override void Emit(ISymbolEmitter emitter)
|
||||
{
|
||||
emitter.Append("//").Append(Comment);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,31 @@
|
||||
using System;
|
||||
using System.Collections.Immutable;
|
||||
|
||||
namespace BetterGenshinImpact.CombatScript;
|
||||
|
||||
public class DashSymbol : InstructionSymbol, IInstructionSymbolHasDuration
|
||||
{
|
||||
public DashSymbol(ImmutableArray<IParameterSymbol> parameterList, ImmutableArray<TriviaSymbol> leadingTriviaList, TriviaSymbol? tailingTrivia)
|
||||
: base("dash", parameterList, leadingTriviaList, tailingTrivia)
|
||||
{
|
||||
InstructionThrowHelper.ThrowIfParameterListIsDefault(parameterList);
|
||||
InstructionThrowHelper.ThrowIfParameterListCountNotCorrect(parameterList, [0, 1]);
|
||||
|
||||
if (parameterList.Length is 1)
|
||||
{
|
||||
InstructionThrowHelper.ThrowIfParameterAtIndexIsNot(parameterList, 0, out DoubleSymbol doubleSymbol);
|
||||
|
||||
HasDuration = true;
|
||||
Duration = TimeSpan.FromSeconds(doubleSymbol.Value);
|
||||
}
|
||||
}
|
||||
|
||||
public DashSymbol(ImmutableArray<TriviaSymbol> leadingTriviaList, TriviaSymbol? tailingTrivia)
|
||||
: base("dash", leadingTriviaList, tailingTrivia)
|
||||
{
|
||||
}
|
||||
|
||||
public bool HasDuration { get; }
|
||||
|
||||
public TimeSpan Duration { get; }
|
||||
}
|
||||
@@ -0,0 +1,16 @@
|
||||
namespace BetterGenshinImpact.CombatScript;
|
||||
|
||||
public sealed class DoubleSymbol : BaseSymbol, IParameterSymbol
|
||||
{
|
||||
public DoubleSymbol(double value)
|
||||
{
|
||||
Value = value;
|
||||
}
|
||||
|
||||
public double Value { get; }
|
||||
|
||||
public override void Emit(ISymbolEmitter emitter)
|
||||
{
|
||||
emitter.Append(Value);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,9 @@
|
||||
namespace BetterGenshinImpact.CombatScript;
|
||||
|
||||
public sealed class HoldSymbol : BaseSymbol, IParameterSymbol
|
||||
{
|
||||
public override void Emit(ISymbolEmitter emitter)
|
||||
{
|
||||
emitter.Append("hold");
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,8 @@
|
||||
namespace BetterGenshinImpact.CombatScript;
|
||||
|
||||
public interface IInstructionSymbolHasAlias
|
||||
{
|
||||
public string AliasName { get; }
|
||||
|
||||
public bool IsAlias { get; }
|
||||
}
|
||||
@@ -0,0 +1,10 @@
|
||||
using System;
|
||||
|
||||
namespace BetterGenshinImpact.CombatScript;
|
||||
|
||||
public interface IInstructionSymbolHasDuration
|
||||
{
|
||||
public bool HasDuration { get; }
|
||||
|
||||
public TimeSpan Duration { get; }
|
||||
}
|
||||
@@ -0,0 +1,5 @@
|
||||
namespace BetterGenshinImpact.CombatScript;
|
||||
|
||||
public interface IParameterSymbol : ISymbol
|
||||
{
|
||||
}
|
||||
@@ -0,0 +1,6 @@
|
||||
namespace BetterGenshinImpact.CombatScript;
|
||||
|
||||
public interface ISymbol
|
||||
{
|
||||
void Emit(ISymbolEmitter emitter);
|
||||
}
|
||||
@@ -0,0 +1,14 @@
|
||||
namespace BetterGenshinImpact.CombatScript;
|
||||
|
||||
public interface ISymbolEmitter
|
||||
{
|
||||
string Emit();
|
||||
|
||||
ISymbolEmitter Append(char value);
|
||||
|
||||
ISymbolEmitter Append(char value, int repeatCount);
|
||||
|
||||
ISymbolEmitter Append(double value);
|
||||
|
||||
ISymbolEmitter Append(string value);
|
||||
}
|
||||
@@ -0,0 +1,18 @@
|
||||
using System.Collections.Immutable;
|
||||
|
||||
namespace BetterGenshinImpact.CombatScript;
|
||||
|
||||
public sealed class InstructionListSymbol : BaseSymbol
|
||||
{
|
||||
public InstructionListSymbol(ImmutableArray<InstructionSymbol> instructions)
|
||||
{
|
||||
Instructions = instructions;
|
||||
}
|
||||
|
||||
public ImmutableArray<InstructionSymbol> Instructions { get; }
|
||||
|
||||
public override void Emit(ISymbolEmitter emitter)
|
||||
{
|
||||
emitter.Append(Instructions);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,59 @@
|
||||
using System.Collections.Immutable;
|
||||
|
||||
namespace BetterGenshinImpact.CombatScript;
|
||||
|
||||
public abstract class InstructionSymbol : BaseSymbol
|
||||
{
|
||||
protected InstructionSymbol(string name, ImmutableArray<TriviaSymbol> leadingTriviaList, TriviaSymbol? tailingTrivia)
|
||||
{
|
||||
Name = name;
|
||||
HasParameterList = false;
|
||||
LeadingTriviaList = leadingTriviaList;
|
||||
TailingTrivia = tailingTrivia;
|
||||
}
|
||||
|
||||
protected InstructionSymbol(string name, ImmutableArray<IParameterSymbol> parameterList, ImmutableArray<TriviaSymbol> leadingTriviaList, TriviaSymbol? tailingTrivia)
|
||||
{
|
||||
Name = name;
|
||||
HasParameterList = true;
|
||||
ParameterList = parameterList;
|
||||
LeadingTriviaList = leadingTriviaList;
|
||||
TailingTrivia = tailingTrivia;
|
||||
}
|
||||
|
||||
public string Name { get; }
|
||||
|
||||
public bool HasParameterList { get; }
|
||||
|
||||
public ImmutableArray<IParameterSymbol> ParameterList { get; }
|
||||
|
||||
public ImmutableArray<TriviaSymbol> LeadingTriviaList { get; }
|
||||
|
||||
public TriviaSymbol? TailingTrivia { get; set; }
|
||||
|
||||
public override void Emit(ISymbolEmitter emitter)
|
||||
{
|
||||
emitter.Append(LeadingTriviaList);
|
||||
|
||||
if (this is IInstructionSymbolHasAlias {IsAlias: true } hasAlias)
|
||||
{
|
||||
emitter.Append(hasAlias.AliasName);
|
||||
}
|
||||
else
|
||||
{
|
||||
emitter.Append(Name);
|
||||
}
|
||||
|
||||
if (HasParameterList)
|
||||
{
|
||||
emitter.Append('(');
|
||||
emitter.Append(ParameterList);
|
||||
emitter.Append(')');
|
||||
}
|
||||
|
||||
if (TailingTrivia is not null)
|
||||
{
|
||||
emitter.Append(TailingTrivia);
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,35 @@
|
||||
using System;
|
||||
using System.Collections.Frozen;
|
||||
using System.Collections.Immutable;
|
||||
|
||||
namespace BetterGenshinImpact.CombatScript;
|
||||
|
||||
public static class InstructionThrowHelper
|
||||
{
|
||||
public static void ThrowIfParameterListIsDefault(ImmutableArray<IParameterSymbol> parameterList)
|
||||
{
|
||||
if (parameterList.IsDefault)
|
||||
{
|
||||
throw new ArgumentException($"Parameter list can not be default.");
|
||||
}
|
||||
}
|
||||
|
||||
public static void ThrowIfParameterListCountNotCorrect(ImmutableArray<IParameterSymbol> parameterList, FrozenSet<int> allowedCounts)
|
||||
{
|
||||
if (!allowedCounts.Contains(parameterList.Length))
|
||||
{
|
||||
throw new ArgumentException($"Instruction parameter count not correct.");
|
||||
}
|
||||
}
|
||||
|
||||
public static void ThrowIfParameterAtIndexIsNot<T>(ImmutableArray<IParameterSymbol> parameterList, int index, out T symbol)
|
||||
where T : IParameterSymbol
|
||||
{
|
||||
if (parameterList.Length <= index || parameterList[index] is not T result)
|
||||
{
|
||||
throw new ArgumentException($"Instruction's parameter at index {index} must be a {typeof(T).Name}.");
|
||||
}
|
||||
|
||||
symbol = result;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,25 @@
|
||||
using System.Collections.Immutable;
|
||||
|
||||
namespace BetterGenshinImpact.CombatScript;
|
||||
|
||||
public class JumpSymbol : InstructionSymbol, IInstructionSymbolHasAlias
|
||||
{
|
||||
public JumpSymbol(bool isAlias, ImmutableArray<IParameterSymbol> parameterList, ImmutableArray<TriviaSymbol> leadingTriviaList, TriviaSymbol? tailingTrivia)
|
||||
: base("jump", parameterList, leadingTriviaList, tailingTrivia)
|
||||
{
|
||||
InstructionThrowHelper.ThrowIfParameterListIsDefault(parameterList);
|
||||
InstructionThrowHelper.ThrowIfParameterListCountNotCorrect(parameterList, [0]);
|
||||
|
||||
IsAlias = isAlias;
|
||||
}
|
||||
|
||||
public JumpSymbol(bool isAlias, ImmutableArray<TriviaSymbol> leadingTriviaList, TriviaSymbol? tailingTrivia)
|
||||
: base("jump", leadingTriviaList, tailingTrivia)
|
||||
{
|
||||
IsAlias = isAlias;
|
||||
}
|
||||
|
||||
public string AliasName { get; } = "j";
|
||||
|
||||
public bool IsAlias { get; }
|
||||
}
|
||||
@@ -0,0 +1,28 @@
|
||||
using System;
|
||||
|
||||
namespace BetterGenshinImpact.CombatScript;
|
||||
|
||||
public class LineBreakTriviaSymbol : TriviaSymbol
|
||||
{
|
||||
private readonly string lineBreak;
|
||||
|
||||
public LineBreakTriviaSymbol()
|
||||
{
|
||||
lineBreak = Environment.NewLine;
|
||||
}
|
||||
|
||||
public LineBreakTriviaSymbol(string lineBreak)
|
||||
{
|
||||
if (lineBreak is not ("\r\n" or "\n" or "\r" or ";"))
|
||||
{
|
||||
throw new ArgumentException("Invalid line break.");
|
||||
}
|
||||
|
||||
this.lineBreak = lineBreak;
|
||||
}
|
||||
|
||||
public override void Emit(ISymbolEmitter emitter)
|
||||
{
|
||||
emitter.Append(lineBreak);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,23 @@
|
||||
using System.Collections.Immutable;
|
||||
|
||||
namespace BetterGenshinImpact.CombatScript;
|
||||
|
||||
public sealed class ScriptUnit
|
||||
{
|
||||
public ScriptUnit(ImmutableArray<ISymbol> symbols)
|
||||
{
|
||||
Symbols = symbols;
|
||||
}
|
||||
|
||||
public ImmutableArray<ISymbol> Symbols { get; }
|
||||
|
||||
public string Emit(ISymbolEmitter emitter)
|
||||
{
|
||||
foreach (ref readonly ISymbol symbol in Symbols.AsSpan())
|
||||
{
|
||||
symbol.Emit(emitter);
|
||||
}
|
||||
|
||||
return emitter.Emit();
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,30 @@
|
||||
using System.Collections.Immutable;
|
||||
|
||||
namespace BetterGenshinImpact.CombatScript;
|
||||
|
||||
public class SkillSymbol : InstructionSymbol, IInstructionSymbolHasAlias
|
||||
{
|
||||
public SkillSymbol(bool isAlias, ImmutableArray<IParameterSymbol> parameterList, ImmutableArray<TriviaSymbol> leadingTriviaList, TriviaSymbol? tailingTrivia)
|
||||
: base("skill", parameterList, leadingTriviaList, tailingTrivia)
|
||||
{
|
||||
InstructionThrowHelper.ThrowIfParameterListIsDefault(parameterList);
|
||||
InstructionThrowHelper.ThrowIfParameterListCountNotCorrect(parameterList, [0, 1]);
|
||||
|
||||
if (parameterList.Length is 1)
|
||||
{
|
||||
InstructionThrowHelper.ThrowIfParameterAtIndexIsNot(parameterList, 0, out HoldSymbol _);
|
||||
}
|
||||
|
||||
IsAlias = isAlias;
|
||||
}
|
||||
|
||||
public SkillSymbol(bool isAlias, ImmutableArray<TriviaSymbol> leadingTriviaList, TriviaSymbol? tailingTrivia)
|
||||
: base("skill", leadingTriviaList, tailingTrivia)
|
||||
{
|
||||
IsAlias = isAlias;
|
||||
}
|
||||
|
||||
public string AliasName { get; } = "e";
|
||||
|
||||
public bool IsAlias { get; }
|
||||
}
|
||||
@@ -0,0 +1,21 @@
|
||||
namespace BetterGenshinImpact.CombatScript;
|
||||
|
||||
public class SpaceTriviaSymbol : TriviaSymbol
|
||||
{
|
||||
private readonly int count;
|
||||
|
||||
public SpaceTriviaSymbol()
|
||||
{
|
||||
count = 1;
|
||||
}
|
||||
|
||||
public SpaceTriviaSymbol(int count)
|
||||
{
|
||||
this.count = count;
|
||||
}
|
||||
|
||||
public override void Emit(ISymbolEmitter emitter)
|
||||
{
|
||||
emitter.Append(' ', count);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,37 @@
|
||||
using System.Text;
|
||||
|
||||
namespace BetterGenshinImpact.CombatScript;
|
||||
|
||||
public sealed class SymbolEmitter : ISymbolEmitter
|
||||
{
|
||||
private readonly StringBuilder builder = new();
|
||||
|
||||
public string Emit()
|
||||
{
|
||||
return builder.ToString();
|
||||
}
|
||||
|
||||
public ISymbolEmitter Append(char value)
|
||||
{
|
||||
builder.Append(value);
|
||||
return this;
|
||||
}
|
||||
|
||||
public ISymbolEmitter Append(char value, int repeatCount)
|
||||
{
|
||||
builder.Append(value, repeatCount);
|
||||
return this;
|
||||
}
|
||||
|
||||
public ISymbolEmitter Append(double value)
|
||||
{
|
||||
builder.Append(value);
|
||||
return this;
|
||||
}
|
||||
|
||||
public ISymbolEmitter Append(string value)
|
||||
{
|
||||
builder.Append(value);
|
||||
return this;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,23 @@
|
||||
using System.Collections.Immutable;
|
||||
|
||||
namespace BetterGenshinImpact.CombatScript;
|
||||
|
||||
public static class SymbolEmitterExtensions
|
||||
{
|
||||
public static ISymbolEmitter Append(this ISymbolEmitter emitter, ISymbol symbol)
|
||||
{
|
||||
symbol.Emit(emitter);
|
||||
return emitter;
|
||||
}
|
||||
|
||||
public static ISymbolEmitter Append<TSymbol>(this ISymbolEmitter emitter, ImmutableArray<TSymbol> symbolList)
|
||||
where TSymbol : ISymbol
|
||||
{
|
||||
foreach(ref readonly TSymbol symbol in symbolList.AsSpan())
|
||||
{
|
||||
symbol.Emit(emitter);
|
||||
}
|
||||
|
||||
return emitter;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,193 @@
|
||||
using System;
|
||||
using System.Collections.Immutable;
|
||||
using System.Runtime.CompilerServices;
|
||||
|
||||
namespace BetterGenshinImpact.CombatScript;
|
||||
|
||||
public static class SymbolParser
|
||||
{
|
||||
public static ScriptUnit Parse(ReadOnlySpan<char> raw)
|
||||
{
|
||||
ImmutableArray<ISymbol>.Builder symbols = ImmutableArray.CreateBuilder<ISymbol>();
|
||||
ParseLines(raw, symbols);
|
||||
return new(symbols.ToImmutable());
|
||||
}
|
||||
|
||||
private static void ParseLines(ReadOnlySpan<char> raw, ImmutableArray<ISymbol>.Builder symbols)
|
||||
{
|
||||
bool skipNextRange = false;
|
||||
ref readonly char end = ref raw[^1];
|
||||
|
||||
foreach(Range range in raw.SplitAny(['\r', '\n', ';']))
|
||||
{
|
||||
if (skipNextRange)
|
||||
{
|
||||
skipNextRange = false;
|
||||
continue;
|
||||
}
|
||||
|
||||
int offset = range.End.GetOffset(raw.Length);
|
||||
if (offset >= raw.Length)
|
||||
{
|
||||
break;
|
||||
}
|
||||
|
||||
ref readonly char peek = ref raw[offset];
|
||||
|
||||
LineBreakTriviaSymbol lineBreakTrivia;
|
||||
if (peek is '\r')
|
||||
{
|
||||
if (Unsafe.IsAddressLessThan(in peek, in end) && Unsafe.Add(ref Unsafe.AsRef(in peek), 1) is '\n')
|
||||
{
|
||||
// It's a CRLF
|
||||
lineBreakTrivia = new("\r\n");
|
||||
skipNextRange = true;
|
||||
}
|
||||
else
|
||||
{
|
||||
// It's a CR
|
||||
lineBreakTrivia = new("\r");
|
||||
}
|
||||
}
|
||||
else if (peek is '\n')
|
||||
{
|
||||
// It's a LF
|
||||
lineBreakTrivia = new("\n");
|
||||
}
|
||||
else if (peek is ';')
|
||||
{
|
||||
lineBreakTrivia = new(";");
|
||||
}
|
||||
else
|
||||
{
|
||||
throw new InvalidOperationException($"Failed to parse line break trivia at {range}.");
|
||||
}
|
||||
|
||||
if (ParseLine(raw[range]) is { } symbol)
|
||||
{
|
||||
symbols.Add(symbol);
|
||||
}
|
||||
|
||||
symbols.Add(lineBreakTrivia);
|
||||
}
|
||||
}
|
||||
|
||||
private static ISymbol? ParseLine(ReadOnlySpan<char> raw)
|
||||
{
|
||||
if (raw.IsEmpty)
|
||||
{
|
||||
return default;
|
||||
}
|
||||
|
||||
if (raw.StartsWith("//"))
|
||||
{
|
||||
return new CommentSymbol(raw[2..].ToString());
|
||||
}
|
||||
|
||||
int indexOfSpace = raw.IndexOf(' ');
|
||||
ReadOnlySpan<char> avatarIdentifier = raw[..indexOfSpace];
|
||||
AvatarSymbol avatarSymbol = new(avatarIdentifier.ToString());
|
||||
ImmutableArray<TriviaSymbol> triviaList = ParseTriviaList(raw[indexOfSpace..], out int advanced);
|
||||
InstructionListSymbol listSymbol = ParseInstructionList(raw[(indexOfSpace + advanced)..]);
|
||||
return new AvatarInstructionListSymbol(avatarSymbol, triviaList, listSymbol);
|
||||
}
|
||||
|
||||
private static InstructionListSymbol ParseInstructionList(ReadOnlySpan<char> raw)
|
||||
{
|
||||
ImmutableArray<InstructionSymbol>.Builder builder = ImmutableArray.CreateBuilder<InstructionSymbol>();
|
||||
|
||||
foreach (Range range in raw.Split(','))
|
||||
{
|
||||
ReadOnlySpan<char> current = raw[range];
|
||||
ImmutableArray<TriviaSymbol> leadingTriviaList = ParseTriviaList(current, out int advanced);
|
||||
|
||||
if (ParseInstruction(current[advanced..], leadingTriviaList) is { } symbol)
|
||||
{
|
||||
builder.Add(symbol);
|
||||
}
|
||||
}
|
||||
|
||||
return new(builder.ToImmutable());
|
||||
}
|
||||
|
||||
private static InstructionSymbol? ParseInstruction(ReadOnlySpan<char> raw, ImmutableArray<TriviaSymbol> leadingTriviaList, TriviaSymbol? tailingTrivia)
|
||||
{
|
||||
if (raw.IsEmpty)
|
||||
{
|
||||
return default;
|
||||
}
|
||||
|
||||
switch (raw[0])
|
||||
{
|
||||
// attack|a
|
||||
case 'a':
|
||||
break;
|
||||
// burst
|
||||
case 'b':
|
||||
return new BurstSymbol(false, [], leadingTriviaList, tailingTrivia);
|
||||
break;
|
||||
// charge
|
||||
case 'c':
|
||||
break;
|
||||
// dash|d
|
||||
case 'd':
|
||||
break;
|
||||
// jump|j
|
||||
case 'j':
|
||||
break;
|
||||
// q
|
||||
case 'q':
|
||||
return new BurstSymbol(true, [], leadingTriviaList, tailingTrivia);
|
||||
break;
|
||||
// skill|s
|
||||
case 's':
|
||||
break;
|
||||
// wait|walk|w
|
||||
case 'w':
|
||||
break;
|
||||
}
|
||||
|
||||
return default;
|
||||
}
|
||||
|
||||
// Handle ','&' '
|
||||
private static ImmutableArray<TriviaSymbol> ParseTriviaList(ReadOnlySpan<char> raw, out int advanced)
|
||||
{
|
||||
advanced = 0;
|
||||
if (raw.IsEmpty)
|
||||
{
|
||||
return ImmutableArray<TriviaSymbol>.Empty;
|
||||
}
|
||||
|
||||
ImmutableArray<TriviaSymbol>.Builder builder = ImmutableArray.CreateBuilder<TriviaSymbol>();
|
||||
|
||||
int spaceCount = 0;
|
||||
foreach (ref readonly char value in raw)
|
||||
{
|
||||
advanced++;
|
||||
if (value is ' ')
|
||||
{
|
||||
spaceCount++;
|
||||
}
|
||||
else
|
||||
{
|
||||
if (spaceCount is not 0)
|
||||
{
|
||||
builder.Add(new SpaceTriviaSymbol(spaceCount));
|
||||
spaceCount = 0;
|
||||
}
|
||||
|
||||
if (value is ',')
|
||||
{
|
||||
builder.Add(new CommaTriviaSymbol());
|
||||
}
|
||||
else
|
||||
{
|
||||
break;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
return builder.ToImmutable();
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,5 @@
|
||||
namespace BetterGenshinImpact.CombatScript;
|
||||
|
||||
public abstract class TriviaSymbol : BaseSymbol
|
||||
{
|
||||
}
|
||||
@@ -0,0 +1,23 @@
|
||||
using System;
|
||||
using System.Collections.Immutable;
|
||||
|
||||
namespace BetterGenshinImpact.CombatScript;
|
||||
|
||||
public class WaitSymbol : InstructionSymbol, IInstructionSymbolHasDuration
|
||||
{
|
||||
public WaitSymbol(ImmutableArray<IParameterSymbol> parameterList, ImmutableArray<TriviaSymbol> leadingTriviaList, TriviaSymbol? tailingTrivia)
|
||||
: base("wait", parameterList, leadingTriviaList, tailingTrivia)
|
||||
{
|
||||
InstructionThrowHelper.ThrowIfParameterListIsDefault(parameterList);
|
||||
InstructionThrowHelper.ThrowIfParameterListCountNotCorrect(parameterList, [1]);
|
||||
|
||||
InstructionThrowHelper.ThrowIfParameterAtIndexIsNot(parameterList, 0, out DoubleSymbol doubleSymbol);
|
||||
|
||||
HasDuration = true;
|
||||
Duration = TimeSpan.FromSeconds(doubleSymbol.Value);
|
||||
}
|
||||
|
||||
public bool HasDuration { get; }
|
||||
|
||||
public TimeSpan Duration { get; }
|
||||
}
|
||||
@@ -0,0 +1,9 @@
|
||||
namespace BetterGenshinImpact.CombatScript;
|
||||
|
||||
public enum WalkDirection
|
||||
{
|
||||
Forward,
|
||||
Backward,
|
||||
Left,
|
||||
Right
|
||||
}
|
||||
@@ -0,0 +1,70 @@
|
||||
using System;
|
||||
using System.Collections.Immutable;
|
||||
|
||||
namespace BetterGenshinImpact.CombatScript;
|
||||
|
||||
public class WalkSymbol : InstructionSymbol, IInstructionSymbolHasAlias, IInstructionSymbolHasDuration, IParameterSymbol
|
||||
{
|
||||
public WalkSymbol(WalkDirection direction, ImmutableArray<IParameterSymbol> parameterList, ImmutableArray<TriviaSymbol> leadingTriviaList, TriviaSymbol? tailingTrivia)
|
||||
: base("walk", parameterList, leadingTriviaList, tailingTrivia)
|
||||
{
|
||||
InstructionThrowHelper.ThrowIfParameterListIsDefault(parameterList);
|
||||
InstructionThrowHelper.ThrowIfParameterListCountNotCorrect(parameterList, [1]);
|
||||
|
||||
InstructionThrowHelper.ThrowIfParameterAtIndexIsNot(parameterList, 0, out DoubleSymbol doubleSymbol);
|
||||
|
||||
HasDuration = true;
|
||||
Duration = TimeSpan.FromSeconds(doubleSymbol.Value);
|
||||
|
||||
Direction = direction;
|
||||
|
||||
IsAlias = true;
|
||||
}
|
||||
|
||||
// Used for parameter
|
||||
public WalkSymbol(WalkDirection direction, ImmutableArray<TriviaSymbol> leadingTriviaList, TriviaSymbol? tailingTrivia)
|
||||
: base("walk", leadingTriviaList, tailingTrivia)
|
||||
{
|
||||
Direction = direction;
|
||||
IsAlias = true;
|
||||
}
|
||||
|
||||
public WalkSymbol(ImmutableArray<IParameterSymbol> parameterList, ImmutableArray<TriviaSymbol> leadingTriviaList, TriviaSymbol? tailingTrivia)
|
||||
: base("walk", parameterList, leadingTriviaList, tailingTrivia)
|
||||
{
|
||||
InstructionThrowHelper.ThrowIfParameterListIsDefault(parameterList);
|
||||
InstructionThrowHelper.ThrowIfParameterListCountNotCorrect(parameterList, [2]);
|
||||
|
||||
InstructionThrowHelper.ThrowIfParameterAtIndexIsNot(parameterList, 0, out WalkSymbol directionAlias);
|
||||
InstructionThrowHelper.ThrowIfParameterAtIndexIsNot(parameterList, 1, out DoubleSymbol doubleSymbol);
|
||||
|
||||
Direction = directionAlias.Direction;
|
||||
HasDuration = true;
|
||||
Duration = TimeSpan.FromSeconds(doubleSymbol.Value);
|
||||
|
||||
IsAlias = false;
|
||||
}
|
||||
|
||||
public string AliasName
|
||||
{
|
||||
get
|
||||
{
|
||||
return Direction switch
|
||||
{
|
||||
WalkDirection.Forward => "w",
|
||||
WalkDirection.Backward => "s",
|
||||
WalkDirection.Left => "a",
|
||||
WalkDirection.Right => "d",
|
||||
_ => throw new ArgumentOutOfRangeException(),
|
||||
};
|
||||
}
|
||||
}
|
||||
|
||||
public bool IsAlias { get; }
|
||||
|
||||
public WalkDirection Direction { get; }
|
||||
|
||||
public bool HasDuration { get; }
|
||||
|
||||
public TimeSpan Duration { get; }
|
||||
}
|
||||
@@ -19,6 +19,10 @@ Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "BetterGenshinImpact.Test",
|
||||
EndProject
|
||||
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "Fischless.WindowsInput", "Fischless.WindowsInput\Fischless.WindowsInput.csproj", "{9D00BC7A-9280-4AC9-8951-4502EDB71B76}"
|
||||
EndProject
|
||||
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "BetterGenshinImpact.CombatScript", "BetterGenshinImpact.CombatScript\BetterGenshinImpact.CombatScript.csproj", "{57C278F9-F62A-480A-B929-B0E5D8174194}"
|
||||
EndProject
|
||||
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "BetterGenshinImpact.CombatScript.Test", "BetterGenshinImpact.CombatScript.Test\BetterGenshinImpact.CombatScript.Test.csproj", "{09427DA8-665E-441A-A576-C11FBC6F9E5C}"
|
||||
EndProject
|
||||
Global
|
||||
GlobalSection(SolutionConfigurationPlatforms) = preSolution
|
||||
Debug|Any CPU = Debug|Any CPU
|
||||
@@ -83,6 +87,22 @@ Global
|
||||
{9D00BC7A-9280-4AC9-8951-4502EDB71B76}.Release|Any CPU.Build.0 = Release|x64
|
||||
{9D00BC7A-9280-4AC9-8951-4502EDB71B76}.Release|x64.ActiveCfg = Release|x64
|
||||
{9D00BC7A-9280-4AC9-8951-4502EDB71B76}.Release|x64.Build.0 = Release|x64
|
||||
{57C278F9-F62A-480A-B929-B0E5D8174194}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
|
||||
{57C278F9-F62A-480A-B929-B0E5D8174194}.Debug|Any CPU.Build.0 = Debug|Any CPU
|
||||
{57C278F9-F62A-480A-B929-B0E5D8174194}.Debug|x64.ActiveCfg = Debug|Any CPU
|
||||
{57C278F9-F62A-480A-B929-B0E5D8174194}.Debug|x64.Build.0 = Debug|Any CPU
|
||||
{57C278F9-F62A-480A-B929-B0E5D8174194}.Release|Any CPU.ActiveCfg = Release|Any CPU
|
||||
{57C278F9-F62A-480A-B929-B0E5D8174194}.Release|Any CPU.Build.0 = Release|Any CPU
|
||||
{57C278F9-F62A-480A-B929-B0E5D8174194}.Release|x64.ActiveCfg = Release|Any CPU
|
||||
{57C278F9-F62A-480A-B929-B0E5D8174194}.Release|x64.Build.0 = Release|Any CPU
|
||||
{09427DA8-665E-441A-A576-C11FBC6F9E5C}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
|
||||
{09427DA8-665E-441A-A576-C11FBC6F9E5C}.Debug|Any CPU.Build.0 = Debug|Any CPU
|
||||
{09427DA8-665E-441A-A576-C11FBC6F9E5C}.Debug|x64.ActiveCfg = Debug|Any CPU
|
||||
{09427DA8-665E-441A-A576-C11FBC6F9E5C}.Debug|x64.Build.0 = Debug|Any CPU
|
||||
{09427DA8-665E-441A-A576-C11FBC6F9E5C}.Release|Any CPU.ActiveCfg = Release|Any CPU
|
||||
{09427DA8-665E-441A-A576-C11FBC6F9E5C}.Release|Any CPU.Build.0 = Release|Any CPU
|
||||
{09427DA8-665E-441A-A576-C11FBC6F9E5C}.Release|x64.ActiveCfg = Release|Any CPU
|
||||
{09427DA8-665E-441A-A576-C11FBC6F9E5C}.Release|x64.Build.0 = Release|Any CPU
|
||||
EndGlobalSection
|
||||
GlobalSection(SolutionProperties) = preSolution
|
||||
HideSolutionNode = FALSE
|
||||
|
||||
Reference in new issue
Block a user