mirror of
https://github.com/DCFApixels/DragonECS.git
synced 2025-11-12 16:45:55 +08:00
Add MetaIDAttribute
This commit is contained in:
parent
7fe5263825
commit
d62dfed0d5
@ -1,13 +1,12 @@
|
||||
using DCFApixels.DragonECS.Internal;
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
|
||||
namespace DCFApixels.DragonECS
|
||||
{
|
||||
[AttributeUsage(AttributeTargets.Struct | AttributeTargets.Class | AttributeTargets.Interface, Inherited = false, AllowMultiple = false)]
|
||||
public sealed class MetaIDAttribute : EcsMetaAttribute
|
||||
{
|
||||
private static HashSet<string> _ids;
|
||||
//private static HashSet<string> _ids = new HashSet<string>();
|
||||
|
||||
public readonly string ID;
|
||||
public MetaIDAttribute(string id)
|
||||
@ -16,12 +15,41 @@ namespace DCFApixels.DragonECS
|
||||
{
|
||||
Throw.ArgumentNull(nameof(id));
|
||||
}
|
||||
if (_ids.Add(id))
|
||||
if (id.Contains(','))
|
||||
{
|
||||
//TODO перевести ексепшен
|
||||
Throw.ArgumentException($"Дублирование MetaID: {ID}");
|
||||
Throw.ArgumentException($"Аргумент {nameof(id)} не может содержать символ запятой ','");
|
||||
}
|
||||
//if (_ids.Add(id) == false) //этот ексепшен не работает, так как атрибуты не кешируются а пересоздаются
|
||||
//{
|
||||
// //TODO перевести ексепшен
|
||||
// Throw.ArgumentException($"Дублирование MetaID: {id}");
|
||||
//}
|
||||
ID = id;
|
||||
}
|
||||
}
|
||||
|
||||
public static class MetaIDUtility
|
||||
{
|
||||
public static unsafe string GenerateNewUniqueID()
|
||||
{
|
||||
long ticks = DateTime.Now.Ticks;
|
||||
byte* hibits = stackalloc byte[8];
|
||||
hibits = (byte*)ticks;
|
||||
|
||||
byte[] byteArray = Guid.NewGuid().ToByteArray();
|
||||
|
||||
fixed (byte* ptr = byteArray)
|
||||
{
|
||||
for (int i = 0; i < 8; i++)
|
||||
{
|
||||
byteArray[i] = hibits[i];
|
||||
}
|
||||
}
|
||||
return BitConverter.ToString(byteArray).Replace("-", "");
|
||||
}
|
||||
public static unsafe string GenerateNewUniqueIDWithAttribute()
|
||||
{
|
||||
return $"[MetaID(\"{GenerateNewUniqueID()}\")]";
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@ -65,6 +65,25 @@ namespace DCFApixels.DragonECS
|
||||
get { return null; }
|
||||
}
|
||||
|
||||
private static bool CheckEcsMemener(Type checkedType)
|
||||
{
|
||||
#if (DEBUG && !DISABLE_DEBUG) || !REFLECTION_DISABLED
|
||||
return checkedType.IsInterface == false && checkedType.IsAbstract == false && typeof(IEcsMember).IsAssignableFrom(checkedType);
|
||||
#else
|
||||
EcsDebug.PrintWarning($"Reflection is not available, the {nameof(MetaGenerator)}.{nameof(GetTags)} method does not work.");
|
||||
return false;
|
||||
#endif
|
||||
}
|
||||
public static bool IsHasMeta(Type type)
|
||||
{
|
||||
#if (DEBUG && !DISABLE_DEBUG) || !REFLECTION_DISABLED
|
||||
return (CheckEcsMemener(type) || Attribute.GetCustomAttributes(type, typeof(EcsMetaAttribute), false).Length > 0);
|
||||
#else
|
||||
EcsDebug.PrintWarning($"Reflection is not available, the {nameof(MetaGenerator)}.{nameof(GetTags)} method does not work.");
|
||||
return false;
|
||||
#endif
|
||||
}
|
||||
|
||||
#region Constructors
|
||||
public static TypeMeta Get(Type type)
|
||||
{
|
||||
|
||||
Loading…
Reference in New Issue
Block a user