mirror of
https://github.com/goatcorp/Dalamud.git
synced 2025-12-13 12:14:16 +01:00
refactor: use client struct for character name check (#1840)
This commit is contained in:
parent
80555d92ec
commit
a35ae5fdf3
1 changed files with 7 additions and 27 deletions
|
|
@ -1,5 +1,6 @@
|
||||||
using System.Diagnostics.CodeAnalysis;
|
using System.Diagnostics.CodeAnalysis;
|
||||||
using System.Linq;
|
using System.Linq;
|
||||||
|
using System.Runtime.CompilerServices;
|
||||||
|
|
||||||
namespace Dalamud.Utility;
|
namespace Dalamud.Utility;
|
||||||
|
|
||||||
|
|
@ -31,36 +32,15 @@ public static class StringExtensions
|
||||||
public static bool IsNullOrWhitespace([NotNullWhen(false)] this string? value) => string.IsNullOrWhiteSpace(value);
|
public static bool IsNullOrWhitespace([NotNullWhen(false)] this string? value) => string.IsNullOrWhiteSpace(value);
|
||||||
|
|
||||||
/// <summary>
|
/// <summary>
|
||||||
/// Validate if character name is valid.
|
/// Validate if character name is valid using game check.
|
||||||
/// Both forename and surname must be between 2 and 15 characters and not total more than 20 characters combined.
|
|
||||||
/// Only letters, hyphens, and apostrophes can be used.
|
|
||||||
/// The first character of either name must be a letter.
|
|
||||||
/// Hyphens cannot be used in succession or placed immediately before or after apostrophes.
|
|
||||||
/// </summary>
|
/// </summary>
|
||||||
/// <param name="value">character name to validate.</param>
|
/// <param name="value">character name to validate.</param>
|
||||||
|
/// <param name="includeLegacy">include legacy names (combined can be 30 instead of 20).</param>
|
||||||
/// <returns>indicator if character is name is valid.</returns>
|
/// <returns>indicator if character is name is valid.</returns>
|
||||||
public static bool IsValidCharacterName(this string value)
|
public static bool IsValidCharacterName(this string value, bool includeLegacy = true)
|
||||||
{
|
{
|
||||||
if (string.IsNullOrEmpty(value)) return false;
|
if (string.IsNullOrEmpty(value)) return false;
|
||||||
if (value.Length > 21) return false; // add 1 to allow for space
|
if (!FFXIVClientStructs.FFXIV.Client.UI.UIModule.IsPlayerCharacterName(value)) return false;
|
||||||
var names = value.Split(' ');
|
return includeLegacy || value.Length <= 21;
|
||||||
if (names.Length != 2) return false;
|
|
||||||
var forename = names[0];
|
|
||||||
var surname = names[1];
|
|
||||||
if (!IsValidName(forename)) return false;
|
|
||||||
if (!IsValidName(surname)) return false;
|
|
||||||
return true;
|
|
||||||
}
|
|
||||||
|
|
||||||
private static bool IsValidName(string name)
|
|
||||||
{
|
|
||||||
if (name.Length is < 2 or > 15) return false;
|
|
||||||
if (name.Any(c => !char.IsLetter(c) && !c.Equals('\'') && !c.Equals('-'))) return false;
|
|
||||||
if (!char.IsLetter(name[0])) return false;
|
|
||||||
if (!char.IsUpper(name[0])) return false;
|
|
||||||
if (name.Contains("--")) return false;
|
|
||||||
if (name.Contains("\'-")) return false;
|
|
||||||
if (name.Contains("-\'")) return false;
|
|
||||||
return true;
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue