Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
75 changes: 75 additions & 0 deletions SecretAPI/Extensions/CodeMatcherExtensions.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,75 @@
namespace SecretAPI.Extensions;

using System;
using System.Linq;
using System.Reflection.Emit;
using HarmonyLib;

/// <summary>
/// Extensions related to <see cref="CodeMatcher"/>.
/// </summary>
public static class CodeMatcherExtensions
{
extension(CodeMatcher matcher)
{
/// <summary>
/// Sets <see cref="CodeMatcher.Pos"/> to the end and then backtracks X amount.
/// </summary>
/// <param name="backtrackCount">The amount to reverse. Must be positive number.</param>
/// <returns>The current <see cref="CodeMatcher"/>.</returns>
public CodeMatcher EndAndBacktrack(int backtrackCount)
{
matcher.End();
matcher.Advance(-backtrackCount);
return matcher;
}

/// <summary>
/// Declares a local to be used of a certain <see cref="Type"/>.
/// </summary>
/// <param name="localType">The <see cref="Type"/> of the local to declare.</param>
/// <param name="localBuilder">The <see cref="LocalBuilder"/> declared.</param>
/// <returns>The current <see cref="CodeMatcher"/>.</returns>
public CodeMatcher DeclareLocal(Type localType, out LocalBuilder localBuilder)
{
localBuilder = matcher.generator.DeclareLocal(localType);
return matcher;
}

/// <summary>
/// Declares a local to be used of a certain <see cref="Type"/>.
/// </summary>
/// <param name="localType">The <see cref="Type"/> of the local to declare.</param>
/// <param name="localIndex">The index of the local declared.</param>
/// <returns>The current <see cref="CodeMatcher"/>.</returns>
public CodeMatcher DeclareLocal(Type localType, out int localIndex)
{
DeclareLocal(matcher, localType, out LocalBuilder builder);
localIndex = builder.LocalIndex;
return matcher;
}

/// <summary>
/// Gets the first label at the current position.
/// </summary>
/// <param name="label">The first label at the current position.</param>
/// <returns>The current <see cref="CodeMatcher"/>.</returns>
public CodeMatcher GetFirstLabel(out Label label)
{
label = matcher.Labels.FirstOrDefault();
return matcher;
}

/// <summary>
/// Gets the first label at a specific position.
/// </summary>
/// <param name="position">The position to get label at.</param>
/// <param name="label">The label at the position.</param>
/// <returns>The current <see cref="CodeMatcher"/>.</returns>
public CodeMatcher GetFirstLabelAt(int position, out Label label)
{
label = matcher.codes[position].labels.FirstOrDefault();
return matcher;
}
}
}
2 changes: 1 addition & 1 deletion SecretAPI/SecretAPI.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@
<!--
<PackageReference Include="Northwood.LabAPI" Version="1.1.4" IncludeAssets="All" PrivateAssets="All" />
-->
<PackageReference Include="Lib.Harmony" Version="2.2.2" />
<PackageReference Include="Lib.Harmony" Version="2.2.2" Publicize="true" />
<PackageReference Include="StyleCop.Analyzers" Version="1.2.0-beta.556" IncludeAssets="All" PrivateAssets="All" />
</ItemGroup>

Expand Down
Loading