add metaID for generic types

This commit is contained in:
Mikhail 2024-10-12 20:06:21 +08:00
parent e34016a312
commit 2fd88037f8
2 changed files with 24 additions and 9 deletions

View File

@ -1,6 +1,7 @@
using DCFApixels.DragonECS.Internal;
using System;
using System.Runtime.InteropServices;
using System.Text.RegularExpressions;
namespace DCFApixels.DragonECS
{
@ -14,16 +15,16 @@ namespace DCFApixels.DragonECS
{
Throw.ArgumentNull(nameof(id));
}
if (id.Contains(','))
if (MetaID.IsGenericID(id) == false)
{
Throw.ArgumentException($"Аргумент {nameof(id)} не может содержать символ запятой ','");
Throw.ArgumentException($"Иентификатор {id} содержит не допустимые символы: ,<>");
}
id = string.Intern(id);
ID = id;
}
}
public static class MetaIDUtility
public static class MetaID
{
[ThreadStatic]
private static Random _randon;
@ -32,6 +33,11 @@ namespace DCFApixels.DragonECS
[ThreadStatic]
private static bool _isInit;
public static bool IsGenericID(string id)
{
return Regex.IsMatch(id, @"^[^,<>\s]*$");
}
public static unsafe string GenerateNewUniqueID()
{
if (_isInit == false)
@ -56,9 +62,13 @@ namespace DCFApixels.DragonECS
return BitConverter.ToString(_buffer).Replace("-", "");
}
public static unsafe string GenerateNewUniqueIDWithAttribute()
public static string IDToAttribute(string id)
{
return $"[MetaID(\"{GenerateNewUniqueID()}\")]";
return $"[MetaID(\"id\")]";
}
public static string GenerateNewUniqueIDWithAttribute()
{
return IDToAttribute(GenerateNewUniqueID());
}
}
}

View File

@ -498,11 +498,16 @@ namespace DCFApixels.DragonECS
}
else
{
if (_idTypePairs.TryGetValue(atr.ID, out Type otherType) && type != otherType) //этот ексепшен не работает, так как атрибуты не кешируются а пересоздаются
string id = atr.ID;
if (type.IsGenericType && type.IsGenericTypeDefinition == false)
{
Throw.Exception($"Types {type.Name} and {otherType.Name} have duplicate MetaID identifiers.");
id = $"{id}<{string.Join(", ", type.GetGenericArguments().Select(o => GetMetaID(o)))}>";
}
_idTypePairs.Add(atr.ID, type);
if (_idTypePairs.TryGetValue(id, out Type otherType) && type != otherType) //этот ексепшен не работает, так как атрибуты не кешируются а пересоздаются
{
Throw.Exception($"Types {type.ToMeta().TypeName} and {otherType.ToMeta().TypeName} have duplicate {atr.ID} MetaID.");
}
_idTypePairs[atr.ID] = type;
return atr.ID;
}
#else