Skip to content

Commit

Permalink
misc: chore: Move all GeneratedRegex methods into one static class wi…
Browse files Browse the repository at this point in the history
…th static instance accessors.
  • Loading branch information
GreemDev committed Feb 1, 2025
1 parent 9f94aa1 commit 38ef65a
Show file tree
Hide file tree
Showing 9 changed files with 139 additions and 76 deletions.
118 changes: 118 additions & 0 deletions src/Ryujinx.Common/Helpers/Patterns.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,118 @@
using System.Text.RegularExpressions;

namespace Ryujinx.Common.Helper
{
public static partial class Patterns
{
#region Accessors

public static readonly Regex Numeric = NumericRegex();

public static readonly Regex AmdGcn = AmdGcnRegex();
public static readonly Regex NvidiaConsumerClass = NvidiaConsumerClassRegex();

public static readonly Regex DomainLp1Ns = DomainLp1NsRegex();
public static readonly Regex DomainLp1Lp1Npln = DomainLp1Lp1NplnRegex();
public static readonly Regex DomainLp1Znc = DomainLp1ZncRegex();
public static readonly Regex DomainSbApi = DomainSbApiRegex();
public static readonly Regex DomainSbAccounts = DomainSbAccountsRegex();
public static readonly Regex DomainAccounts = DomainAccountsRegex();

public static readonly Regex Module = ModuleRegex();
public static readonly Regex FsSdk = FsSdkRegex();
public static readonly Regex SdkMw = SdkMwRegex();

// ReSharper disable once InconsistentNaming
public static readonly Regex CJK = CJKRegex();

public static readonly Regex LdnPassphrase = LdnPassphraseRegex();

public static readonly Regex CleanText = CleanTextRegex();

#endregion

#region Generated pattern stubs

#region Numeric validation

[GeneratedRegex("[0-9]|.")]
internal static partial Regex NumericRegex();

#endregion

#region GPU names

[GeneratedRegex(
"Radeon (((HD|R(5|7|9|X)) )?((M?[2-6]\\d{2}(\\D|$))|([7-8]\\d{3}(\\D|$))|Fury|Nano))|(Pro Duo)")]
internal static partial Regex AmdGcnRegex();

[GeneratedRegex("NVIDIA GeForce (R|G)?TX? (\\d{3}\\d?)M?")]
internal static partial Regex NvidiaConsumerClassRegex();

#endregion

#region DNS blocking

public static readonly Regex[] BlockedHosts =
[
DomainLp1Ns,
DomainLp1Lp1Npln,
DomainLp1Znc,
DomainSbApi,
DomainSbAccounts,
DomainAccounts
];

const RegexOptions DnsRegexOpts =
RegexOptions.CultureInvariant | RegexOptions.IgnoreCase | RegexOptions.ExplicitCapture;

[GeneratedRegex(@"^(.*)\-lp1\.(n|s)\.n\.srv\.nintendo\.net$", DnsRegexOpts)]
internal static partial Regex DomainLp1NsRegex();

[GeneratedRegex(@"^(.*)\-lp1\.lp1\.t\.npln\.srv\.nintendo\.net$", DnsRegexOpts)]
internal static partial Regex DomainLp1Lp1NplnRegex();

[GeneratedRegex(@"^(.*)\-lp1\.(znc|p)\.srv\.nintendo\.net$", DnsRegexOpts)]
internal static partial Regex DomainLp1ZncRegex();

[GeneratedRegex(@"^(.*)\-sb\-api\.accounts\.nintendo\.com$", DnsRegexOpts)]
internal static partial Regex DomainSbApiRegex();

[GeneratedRegex(@"^(.*)\-sb\.accounts\.nintendo\.com$", DnsRegexOpts)]
internal static partial Regex DomainSbAccountsRegex();

[GeneratedRegex(@"^accounts\.nintendo\.com$", DnsRegexOpts)]
internal static partial Regex DomainAccountsRegex();

#endregion

#region Executable information

[GeneratedRegex(@"[a-z]:[\\/][ -~]{5,}\.nss", RegexOptions.IgnoreCase | RegexOptions.CultureInvariant)]
internal static partial Regex ModuleRegex();

[GeneratedRegex(@"sdk_version: ([0-9.]*)")]
internal static partial Regex FsSdkRegex();

[GeneratedRegex(@"SDK MW[ -~]*")]
internal static partial Regex SdkMwRegex();

#endregion

#region CJK

[GeneratedRegex(
"\\p{IsHangulJamo}|\\p{IsCJKRadicalsSupplement}|\\p{IsCJKSymbolsandPunctuation}|\\p{IsEnclosedCJKLettersandMonths}|\\p{IsCJKCompatibility}|\\p{IsCJKUnifiedIdeographsExtensionA}|\\p{IsCJKUnifiedIdeographs}|\\p{IsHangulSyllables}|\\p{IsCJKCompatibilityForms}")]
private static partial Regex CJKRegex();

#endregion

[GeneratedRegex("Ryujinx-[0-9a-f]{8}")]
private static partial Regex LdnPassphraseRegex();

[GeneratedRegex(@"[^\u0000\u0009\u000A\u000D\u0020-\uFFFF]..")]
private static partial Regex CleanTextRegex();

#endregion
}
}
8 changes: 1 addition & 7 deletions src/Ryujinx.Graphics.Vulkan/Vendor.cs
Original file line number Diff line number Diff line change
Expand Up @@ -16,14 +16,8 @@ enum Vendor
Unknown,
}

static partial class VendorUtils
static class VendorUtils
{
[GeneratedRegex("Radeon (((HD|R(5|7|9|X)) )?((M?[2-6]\\d{2}(\\D|$))|([7-8]\\d{3}(\\D|$))|Fury|Nano))|(Pro Duo)")]
public static partial Regex AmdGcnRegex();

[GeneratedRegex("NVIDIA GeForce (R|G)?TX? (\\d{3}\\d?)M?")]
public static partial Regex NvidiaConsumerClassRegex();

public static Vendor FromId(uint id)
{
return id switch
Expand Down
5 changes: 3 additions & 2 deletions src/Ryujinx.Graphics.Vulkan/VulkanRenderer.cs
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
using Gommon;
using Ryujinx.Common.Configuration;
using Ryujinx.Common.Helper;
using Ryujinx.Common.Logging;
using Ryujinx.Graphics.GAL;
using Ryujinx.Graphics.Shader;
Expand Down Expand Up @@ -375,11 +376,11 @@ private unsafe void LoadFeatures(uint maxQueueCount, uint queueFamilyIndex)

GpuVersion = $"Vulkan v{ParseStandardVulkanVersion(properties.ApiVersion)}, Driver v{ParseDriverVersion(ref properties)}";

IsAmdGcn = !IsMoltenVk && Vendor == Vendor.Amd && VendorUtils.AmdGcnRegex().IsMatch(GpuRenderer);
IsAmdGcn = !IsMoltenVk && Vendor == Vendor.Amd && Patterns.AmdGcn.IsMatch(GpuRenderer);

if (Vendor == Vendor.Nvidia)
{
Match match = VendorUtils.NvidiaConsumerClassRegex().Match(GpuRenderer);
Match match = Patterns.NvidiaConsumerClass.Match(GpuRenderer);

if (match != null && int.TryParse(match.Groups[2].Value, out int gpuNumber))
{
Expand Down
6 changes: 2 additions & 4 deletions src/Ryujinx.HLE/HOS/Applets/Error/ErrorApplet.cs
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
using LibHac.Ncm;
using LibHac.Tools.FsSystem;
using LibHac.Tools.FsSystem.NcaUtils;
using Ryujinx.Common.Helper;
using Ryujinx.Common.Logging;
using Ryujinx.HLE.HOS.Services.Am.AppletAE;
using Ryujinx.HLE.HOS.SystemState;
Expand All @@ -30,9 +31,6 @@ internal partial class ErrorApplet : IApplet

public event EventHandler AppletStateChanged;

[GeneratedRegex(@"[^\u0000\u0009\u000A\u000D\u0020-\uFFFF]..")]
private static partial Regex CleanTextRegex();

public ErrorApplet(Horizon horizon)
{
_horizon = horizon;
Expand Down Expand Up @@ -107,7 +105,7 @@ private static string SystemLanguageToLanguageKey(SystemLanguage systemLanguage)

private static string CleanText(string value)
{
return CleanTextRegex().Replace(value, string.Empty).Replace("\0", string.Empty);
return Patterns.CleanText.Replace(value, string.Empty).Replace("\0", string.Empty);
}

private string GetMessageText(uint module, uint description, string key)
Expand Down
Original file line number Diff line number Diff line change
@@ -1,17 +1,9 @@
using System.Text.RegularExpressions;
using Ryujinx.Common.Helper;

namespace Ryujinx.HLE.HOS.Applets.SoftwareKeyboard
{
public static partial class CJKCharacterValidation
public static class CJKCharacterValidation
{
public static bool IsCJK(char value)
{
Regex regex = CJKRegex();

return regex.IsMatch(value.ToString());
}

[GeneratedRegex("\\p{IsHangulJamo}|\\p{IsCJKRadicalsSupplement}|\\p{IsCJKSymbolsandPunctuation}|\\p{IsEnclosedCJKLettersandMonths}|\\p{IsCJKCompatibility}|\\p{IsCJKUnifiedIdeographsExtensionA}|\\p{IsCJKUnifiedIdeographs}|\\p{IsHangulSyllables}|\\p{IsCJKCompatibilityForms}")]
private static partial Regex CJKRegex();
public static bool IsCJK(char value) => Patterns.CJK.IsMatch(value.ToString());
}
}
Original file line number Diff line number Diff line change
@@ -1,17 +1,9 @@
using System.Text.RegularExpressions;
using Ryujinx.Common.Helper;

namespace Ryujinx.HLE.HOS.Applets.SoftwareKeyboard
{
public static partial class NumericCharacterValidation
public static class NumericCharacterValidation
{
public static bool IsNumeric(char value)
{
Regex regex = NumericRegex();

return regex.IsMatch(value.ToString());
}

[GeneratedRegex("[0-9]|.")]
private static partial Regex NumericRegex();
public static bool IsNumeric(char value) => Patterns.Numeric.IsMatch(value.ToString());
}
}
Original file line number Diff line number Diff line change
@@ -1,37 +1,13 @@
using Ryujinx.Common.Helper;
using System.Text.RegularExpressions;

namespace Ryujinx.HLE.HOS.Services.Sockets.Sfdnsres.Proxy
{
static partial class DnsBlacklist
static class DnsBlacklist
{
const RegexOptions RegexOpts = RegexOptions.CultureInvariant | RegexOptions.IgnoreCase | RegexOptions.ExplicitCapture;

[GeneratedRegex(@"^(.*)\-lp1\.(n|s)\.n\.srv\.nintendo\.net$", RegexOpts)]
private static partial Regex BlockedHost1();
[GeneratedRegex(@"^(.*)\-lp1\.lp1\.t\.npln\.srv\.nintendo\.net$", RegexOpts)]
private static partial Regex BlockedHost2();
[GeneratedRegex(@"^(.*)\-lp1\.(znc|p)\.srv\.nintendo\.net$", RegexOpts)]
private static partial Regex BlockedHost3();
[GeneratedRegex(@"^(.*)\-sb\-api\.accounts\.nintendo\.com$", RegexOpts)]
private static partial Regex BlockedHost4();
[GeneratedRegex(@"^(.*)\-sb\.accounts\.nintendo\.com$", RegexOpts)]
private static partial Regex BlockedHost5();
[GeneratedRegex(@"^accounts\.nintendo\.com$", RegexOpts)]
private static partial Regex BlockedHost6();

private static readonly Regex[] _blockedHosts =
[
BlockedHost1(),
BlockedHost2(),
BlockedHost3(),
BlockedHost4(),
BlockedHost5(),
BlockedHost6()
];

public static bool IsHostBlocked(string host)
{
foreach (Regex regex in _blockedHosts)
foreach (Regex regex in Patterns.BlockedHosts)
{
if (regex.IsMatch(host))
{
Expand Down
14 changes: 4 additions & 10 deletions src/Ryujinx.HLE/Loaders/Executables/NsoExecutable.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
using LibHac.Fs;
using LibHac.Loader;
using LibHac.Tools.FsSystem;
using Ryujinx.Common.Helper;
using Ryujinx.Common.Logging;
using System;
using System.Text;
Expand Down Expand Up @@ -29,13 +30,6 @@ partial class NsoExecutable : IExecutable
public string Name;
public Array32<byte> BuildId;

[GeneratedRegex(@"[a-z]:[\\/][ -~]{5,}\.nss", RegexOptions.IgnoreCase | RegexOptions.CultureInvariant)]
private static partial Regex ModuleRegex();
[GeneratedRegex(@"sdk_version: ([0-9.]*)")]
private static partial Regex FsSdkRegex();
[GeneratedRegex(@"SDK MW[ -~]*")]
private static partial Regex SdkMwRegex();

public NsoExecutable(IStorage inStorage, string name = null)
{
NsoReader reader = new();
Expand Down Expand Up @@ -90,7 +84,7 @@ private void PrintRoSectionInfo()

if (string.IsNullOrEmpty(modulePath))
{
Match moduleMatch = ModuleRegex().Match(rawTextBuffer);
Match moduleMatch = Patterns.Module.Match(rawTextBuffer);
if (moduleMatch.Success)
{
modulePath = moduleMatch.Value;
Expand All @@ -99,13 +93,13 @@ private void PrintRoSectionInfo()

stringBuilder.AppendLine($" Module: {modulePath}");

Match fsSdkMatch = FsSdkRegex().Match(rawTextBuffer);
Match fsSdkMatch = Patterns.FsSdk.Match(rawTextBuffer);
if (fsSdkMatch.Success)
{
stringBuilder.AppendLine($" FS SDK Version: {fsSdkMatch.Value.Replace("sdk_version: ", string.Empty)}");
}

MatchCollection sdkMwMatches = SdkMwRegex().Matches(rawTextBuffer);
MatchCollection sdkMwMatches = Patterns.SdkMw.Matches(rawTextBuffer);
if (sdkMwMatches.Count != 0)
{
string libHeader = " SDK Libraries: ";
Expand Down
6 changes: 2 additions & 4 deletions src/Ryujinx/UI/ViewModels/SettingsViewModel.cs
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
using Ryujinx.Common.Configuration;
using Ryujinx.Common.Configuration.Multiplayer;
using Ryujinx.Common.GraphicsDriver;
using Ryujinx.Common.Helper;
using Ryujinx.Common.Logging;
using Ryujinx.Graphics.GAL;
using Ryujinx.Graphics.Vulkan;
Expand Down Expand Up @@ -330,9 +331,6 @@ public int MultiplayerModeIndex
}
}

[GeneratedRegex("Ryujinx-[0-9a-f]{8}")]
private static partial Regex LdnPassphraseRegex();

public bool IsInvalidLdnPassphraseVisible { get; set; }

public SettingsViewModel(VirtualFileSystem virtualFileSystem, ContentManager contentManager) : this()
Expand Down Expand Up @@ -470,7 +468,7 @@ await Dispatcher.UIThread.InvokeAsync(() =>

private bool ValidateLdnPassphrase(string passphrase)
{
return string.IsNullOrEmpty(passphrase) || (passphrase.Length == 16 && LdnPassphraseRegex().IsMatch(passphrase));
return string.IsNullOrEmpty(passphrase) || (passphrase.Length == 16 && Patterns.LdnPassphrase.IsMatch(passphrase));
}

public void ValidateAndSetTimeZone(string location)
Expand Down

0 comments on commit 38ef65a

Please sign in to comment.