Skip to content

Commit

Permalink
feat: compile more regexes at startup time
Browse files Browse the repository at this point in the history
  • Loading branch information
beheh committed May 28, 2024
1 parent 0ddee0d commit b3e85ab
Showing 1 changed file with 31 additions and 39 deletions.
70 changes: 31 additions & 39 deletions Hearthstone Deck Tracker/LogReader/LogConstants.cs
Original file line number Diff line number Diff line change
@@ -1,59 +1,51 @@
#region

using System.Text.RegularExpressions;
using HearthWatcher.LogReader;

#endregion

namespace Hearthstone_Deck_Tracker.LogReader
{
public static class LogConstants
{
public static readonly Regex GoldProgressRegex = new Regex(@"(?<wins>(\d))/3 wins towards 10 gold");
public static readonly Regex UnloadCardRegex = new Regex(@"unloading\ name=(?<id>(\w+_\w+))\ family=CardPrefab\ persistent=False");
public static readonly Regex UnloadBrawlAsset = new Regex(@"unloading name=Tavern_Brawl\ ");
namespace Hearthstone_Deck_Tracker.LogReader;

public static readonly Regex CardMovementRegex =
new Regex(@"\w*(cardId=(?<Id>(\w*))).*(zone\ from\ (?<from>((\w*)\s*)*))((\ )*->\ (?<to>(\w*\s*)*))*.*");
public static class LogConstants
{
// Compiling Regexes takes a long time, but we only do it once and matching will be significantly faster
// Once HDT supports .NET 7 we can start using GeneratedRegex that compiles the Regex at build time
private static Regex CompileRegex(string pattern) => new(pattern, RegexOptions.Compiled);

public static readonly Regex NewChoiceRegex = new Regex(@"Client chooses: .* \((?<id>(.+))\)");
public static readonly Regex GameModeRegex = new Regex(@"prevMode=(?<prev>(\w+)).*currMode=(?<curr>(\w+))");
public static readonly Regex NextGameModeRegex = new Regex(@"prevMode=(?<prev>(\w+)).*nextMode=(?<next>(\w+))");
public static readonly Regex ConnectionRegex = new Regex(@"ConnectAPI\.GotoGameServer -- address=(?<address>(.*)), game=(?<game>(.*)), client=(?<client>(.*)), spectateKey=(?<spectateKey>(.*)),? reconn");
public static readonly Regex GameModeRegex = CompileRegex(@"prevMode=(?<prev>(\w+)).*currMode=(?<curr>(\w+))");
public static readonly Regex NextGameModeRegex = CompileRegex(@"prevMode=(?<prev>(\w+)).*nextMode=(?<next>(\w+))");

public static class PowerTaskList
{
public static readonly Regex BlockStartRegex =
new Regex(@".*BLOCK_START.*BlockType=(?<type>(\w+)).*id=(?<id>\d*).*(cardId=(?<Id>(\w*))).*player=(?<player>\d).*EffectCardId=(?<effectCardId>(.*))\sEffectIndex=.*Target=(?<target>(.+)).*SubOption=(?<subOption>(.+))");
public static class PowerTaskList
{
public static readonly Regex BlockStartRegex =
CompileRegex(@".*BLOCK_START.*BlockType=(?<type>(\w+)).*id=(?<id>\d*).*(cardId=(?<Id>(\w*))).*player=(?<player>\d).*EffectCardId=(?<effectCardId>(.*))\sEffectIndex=.*Target=(?<target>(.+)).*SubOption=(?<subOption>(.+))");

public static readonly Regex CardIdRegex = new Regex(@"cardId=(?<cardId>(\w+))");
public static readonly Regex CreationRegex = new Regex(@"FULL_ENTITY - Updating.*id=(?<id>(\d+)).*zone=(?<zone>(\w+)).*CardID=(?<cardId>(\w*))");
public static readonly Regex CreationTagRegex = new Regex(@"tag=(?<tag>(\w+))\ value=(?<value>(\w+))");
public static readonly Regex CardIdRegex = CompileRegex(@"cardId=(?<cardId>(\w+))");
public static readonly Regex CreationRegex = CompileRegex(@"FULL_ENTITY - Updating.*id=(?<id>(\d+)).*zone=(?<zone>(\w+)).*CardID=(?<cardId>(\w*))");
public static readonly Regex CreationTagRegex = CompileRegex(@"tag=(?<tag>(\w+))\ value=(?<value>(\w+))");

public static readonly Regex EntityRegex =
new Regex(
@"(?=id=(?<id>(\d+)))(?=name=(?<name>(\w+)))?(?=zone=(?<zone>(\w+)))?(?=zonePos=(?<zonePos>(\d+)))?(?=cardId=(?<cardId>(\w+)))?(?=player=(?<player>(\d+)))?(?=type=(?<type>(\w+)))?");
public static readonly Regex EntityRegex =
CompileRegex(@"(?=id=(?<id>(\d+)))(?=name=(?<name>(\w+)))?(?=zone=(?<zone>(\w+)))?(?=zonePos=(?<zonePos>(\d+)))?(?=cardId=(?<cardId>(\w+)))?(?=player=(?<player>(\d+)))?(?=type=(?<type>(\w+)))?");

public static readonly Regex GameEntityRegex = new Regex(@"GameEntity\ EntityID=(?<id>(\d+))");
public static readonly Regex GameEntityRegex = CompileRegex(@"GameEntity\ EntityID=(?<id>(\d+))");

public static readonly Regex PlayerEntityRegex =
new Regex(@"Player\ EntityID=(?<id>(\d+))\ PlayerID=(?<playerId>(\d+))\ GameAccountId=(?<gameAccountId>(.+))");
public static readonly Regex PlayerEntityRegex =
CompileRegex(@"Player\ EntityID=(?<id>(\d+))\ PlayerID=(?<playerId>(\d+))\ GameAccountId=(?<gameAccountId>(.+))");

public static readonly Regex TagChangeRegex =
new Regex(@"TAG_CHANGE\ Entity=(?<entity>(.+))\ tag=(?<tag>(\w+))\ value=(?<value>(\w+))");
public static readonly Regex TagChangeRegex =
CompileRegex(@"TAG_CHANGE\ Entity=(?<entity>(.+))\ tag=(?<tag>(\w+))\ value=(?<value>(\w+))");

public static readonly Regex UpdatingEntityRegex =
new Regex(@"(?<type>(SHOW_ENTITY|CHANGE_ENTITY))\ -\ Updating\ Entity=(?<entity>(.+))\ CardID=(?<cardId>(\w*))");
public static readonly Regex UpdatingEntityRegex =
CompileRegex(@"(?<type>(SHOW_ENTITY|CHANGE_ENTITY))\ -\ Updating\ Entity=(?<entity>(.+))\ CardID=(?<cardId>(\w*))");

public static readonly Regex HideEntityRegex = new Regex(@"HIDE_ENTITY\ -\ .* id=(?<id>(\d+))");
public static readonly Regex HideEntityRegex = CompileRegex(@"HIDE_ENTITY\ -\ .* id=(?<id>(\d+))");

public static readonly Regex ShuffleRegex = new Regex(@"SHUFFLE_DECK\ PlayerID=(?<id>(\d+))");
}
public static readonly Regex ShuffleRegex = CompileRegex(@"SHUFFLE_DECK\ PlayerID=(?<id>(\d+))");
}

public static class Choices
{
public static readonly Regex SendChoicesHeaderRegex = new Regex(@"id=(?<id>(\d+)) ChoiceType=(?<choiceType>(\w+))");
public static readonly Regex SendChoicesBodyRegex = new Regex(@"m_chosenEntities\[(?<index>(\d+))]=.* id=(?<id>(\d+))");
}
public static class Choices
{
public static readonly Regex SendChoicesHeaderRegex = CompileRegex(@"id=(?<id>(\d+)) ChoiceType=(?<choiceType>(\w+))");
public static readonly Regex SendChoicesBodyRegex = CompileRegex(@"m_chosenEntities\[(?<index>(\d+))]=.* id=(?<id>(\d+))");
}
}

0 comments on commit b3e85ab

Please sign in to comment.