Update MetaIDAttribute.cs

This commit is contained in:
DCFApixels 2025-03-19 16:16:33 +08:00
parent b008643900
commit 70520d38c6

View File

@ -21,25 +21,124 @@ namespace DCFApixels.DragonECS
{ {
if (string.IsNullOrEmpty(id)) if (string.IsNullOrEmpty(id))
{ {
Throw.ArgumentNull(nameof(id)); EcsDebug.PrintError("The identifier cannot be empty or null");
id = string.Empty;
} }
if (MetaID.IsGenericID(id) == false) if (MetaID.IsValidID(id) == false)
{ {
Throw.ArgumentException($"Identifier {id} contains invalid characters: ,<>"); EcsDebug.PrintError($"Identifier {id} contains invalid characters. Allowed charset: {MetaID.ALLOWED_CHARSET}");
id = string.Empty;
} }
id = string.Intern(id);
ID = id; ID = id;
} }
} }
public static class MetaID public static unsafe class MetaID
{ {
public const string ALLOWED_CHARSET = "_0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ";
public static bool IsFixedNameType(Type type)
{
if (type.IsPrimitive)
{
return true;
}
if(type == typeof(string))
{
return true;
}
return false;
}
public static bool IsGenericID(string id)
{
return Regex.IsMatch(id, @"^[^,<>\s]*$");
}
public static bool IsValidID(string input)
{
return input[input.Length - 1] == '>' || Regex.IsMatch(input, @"^[a-zA-Z0-9_]+$");
}
[ThreadStatic] [ThreadStatic]
private static Random _randon; private static uint _randonState;
[ThreadStatic] public static string GenerateNewUniqueID()
private static byte[] _buffer; {
[ThreadStatic] const int BYTES = 16;
private static bool _isInit; const int CHARS = BYTES * 2;
const string CHARSET = "0123456789ABCDEF";
if (_randonState == 0)
{
IntPtr prt = Marshal.AllocHGlobal(1);
long alloc = (long)prt;
Marshal.FreeHGlobal(prt);
_randonState = (uint)alloc ^ (uint)DateTime.Now.Millisecond;
}
byte* bytes = stackalloc byte[BYTES];
Span<byte> x = new Span<byte>(bytes, BYTES);
long* bytesLong = (long*)bytes;
uint* bytesUInt = (uint*)bytes;
bytesLong[0] = DateTime.Now.Ticks;
_randonState = BitsUtility.NextXorShiftState(_randonState);
bytesUInt[2] = _randonState;
_randonState = BitsUtility.NextXorShiftState(_randonState);
bytesUInt[3] = _randonState;
char* str = stackalloc char[CHARS];
for (int i = 0, j = 0; i < BYTES; i++)
{
byte b = bytes[i];
str[j++] = CHARSET[b & 0x0000_000F];
str[j++] = CHARSET[(b >> 4) & 0x0000_000F];
}
return new string(str, 0, CHARS);
}
public static string IDToAttribute(string id)
{
return $"[MetaID(\"{id}\")]";
}
public static string ConvertIDToTypeName(string id)
{
id = id.Replace("_1", "__");
id = id.Replace("_2", "__");
id = id.Replace("_3", "__");
id = id.Replace("<", "_1");
id = id.Replace(">", "_2");
id = id.Replace(",", "_3");
return "_" + id;
}
public static string ParseIDFromTypeName(string name)
{
char* buffer = TempBuffer<char>.Get(name.Length);
int count = 0;
//skip name[0] char
for (int i = 1, iMax = name.Length; i < iMax; i++)
{
char current = name[i];
if (current == '_')
{
if (++i >= iMax) { break; }
current = name[i];
switch (current)
{
case '1': current = '<'; break;
case '2': current = '>'; break;
case '3': current = ','; break;
}
}
buffer[count++] = current;
}
return new string(buffer, 0, count);
}
public static string GenerateNewUniqueIDWithAttribute()
{
return IDToAttribute(GenerateNewUniqueID());
}
public static bool TryFindMetaIDCollisions(IEnumerable<TypeMeta> metas, out CollisionList collisions) public static bool TryFindMetaIDCollisions(IEnumerable<TypeMeta> metas, out CollisionList collisions)
@ -52,44 +151,6 @@ namespace DCFApixels.DragonECS
return new CollisionList(metas); return new CollisionList(metas);
} }
public static bool IsGenericID(string id)
{
return Regex.IsMatch(id, @"^[^,<>\s]*$");
}
public static unsafe string GenerateNewUniqueID()
{
if (_isInit == false)
{
IntPtr prt = Marshal.AllocHGlobal(1);
long alloc = (long)prt;
Marshal.Release(prt);
_randon = new Random((int)alloc);
_buffer = new byte[8];
_isInit = true;
}
byte* hibits = stackalloc byte[8];
long* hibitsL = (long*)hibits;
hibitsL[0] = DateTime.Now.Ticks;
hibitsL[1] = _randon.Next();
for (int i = 0; i < 8; i++)
{
_buffer[i] = hibits[i];
}
return BitConverter.ToString(_buffer).Replace("-", "");
}
public static string IDToAttribute(string id)
{
return $"[MetaID(\"id\")]";
}
public static string GenerateNewUniqueIDWithAttribute()
{
return IDToAttribute(GenerateNewUniqueID());
}
#region CollisionList #region CollisionList
[DebuggerTypeProxy(typeof(DebuggerProxy))] [DebuggerTypeProxy(typeof(DebuggerProxy))]
[DebuggerDisplay("HasAnyCollision: {IsHasAnyCollision} ListsCount: {ListsCount}")] [DebuggerDisplay("HasAnyCollision: {IsHasAnyCollision} ListsCount: {ListsCount}")]