Files
SPT-Server-Build/Libraries/SPTarkov.Server.Core/Extensions/MongoIdExtensions.cs
T
Jesse 54f0d0779c Convert TemplateItem to MongoId (#436)
* Convert TemplateItem to MongoId

* Push new extensions

* Handle null mongoid's being passed to regex

* Handle null strings, fixes item events

* Updated loot generation to work with new property `composedKey`

Fixed typo in `SlotId`

* Fix missing method after merge

* Remove duplicately named MongoIDExtensions?

* Fixed location loot generation to handle impending loot json changes

* Updated location JSONs with new properties (excluding lighthouse loose loot)

* Fixed build issue with ItemTplGenerator

* use correct handing for new mongo ids

* Added helper method to improve readability

---------

Co-authored-by: Chomp <dev@dev.sp-tarkov.com>
2025-07-02 10:14:04 +01:00

74 lines
2.3 KiB
C#

using SPTarkov.Server.Core.Models.Common;
namespace SPTarkov.Server.Core.Extensions
{
public static class MongoIdExtensions
{
//Temporary, but necessary
public static IEnumerable<MongoId> ToMongoIds(this IEnumerable<string> source)
{
return source.Select(s => (MongoId)s);
}
/// <summary>
/// Determines whether the specified <see cref="MongoId"/> is a valid 24-character hexadecimal string,
/// which is the standard format for MongoDB ObjectIds.
/// </summary>
/// <param name="mongoId">The <see cref="MongoId"/> to validate.</param>
/// <returns><see langword="true"/> if the <paramref name="mongoId"/> is a valid MongoDB ObjectId; otherwise, <see langword="false"/>.</returns>
public static bool IsValidMongoId(this MongoId mongoId)
{
var span = mongoId.ToString().AsSpan();
if (span.Length != 24)
{
return false;
}
for (var i = 0; i < 24; i++)
{
var c = span[i];
var isHex =
(c >= '0' && c <= '9') || (c >= 'a' && c <= 'f') || (c >= 'A' && c <= 'F');
if (!isHex)
{
return false;
}
}
return true;
}
/// <summary>
/// Determines whether the specified string is a valid 24-character hexadecimal representation
/// of a MongoDB ObjectId.
/// </summary>
/// <param name="mongoId">The string to validate as a MongoDB ObjectId.</param>
/// <returns><see langword="true"/> if the <paramref name="mongoId"/> is a valid MongoDB ObjectId; otherwise, <see langword="false"/>.</returns>
public static bool IsValidMongoId(this string mongoId)
{
var span = mongoId.AsSpan();
if (span.Length != 24)
{
return false;
}
for (var i = 0; i < 24; i++)
{
var c = span[i];
var isHex =
(c >= '0' && c <= '9') || (c >= 'a' && c <= 'f') || (c >= 'A' && c <= 'F');
if (!isHex)
{
return false;
}
}
return true;
}
}
}