2024-09-16 12:24:52 +08:00
|
|
|
|
using DCFApixels.DragonECS.Internal;
|
|
|
|
|
using System;
|
2024-09-18 17:02:14 +08:00
|
|
|
|
using System.Runtime.InteropServices;
|
2024-09-16 12:24:52 +08:00
|
|
|
|
|
|
|
|
|
namespace DCFApixels.DragonECS
|
|
|
|
|
{
|
|
|
|
|
[AttributeUsage(AttributeTargets.Struct | AttributeTargets.Class | AttributeTargets.Interface, Inherited = false, AllowMultiple = false)]
|
|
|
|
|
public sealed class MetaIDAttribute : EcsMetaAttribute
|
|
|
|
|
{
|
|
|
|
|
public readonly string ID;
|
|
|
|
|
public MetaIDAttribute(string id)
|
|
|
|
|
{
|
|
|
|
|
if (string.IsNullOrEmpty(id))
|
|
|
|
|
{
|
|
|
|
|
Throw.ArgumentNull(nameof(id));
|
|
|
|
|
}
|
2024-09-16 19:29:48 +08:00
|
|
|
|
if (id.Contains(','))
|
2024-09-16 12:24:52 +08:00
|
|
|
|
{
|
2024-09-16 19:29:48 +08:00
|
|
|
|
Throw.ArgumentException($"Аргумент {nameof(id)} не может содержать символ запятой ','");
|
2024-09-16 12:24:52 +08:00
|
|
|
|
}
|
|
|
|
|
ID = id;
|
|
|
|
|
}
|
|
|
|
|
}
|
2024-09-16 19:29:48 +08:00
|
|
|
|
|
|
|
|
|
public static class MetaIDUtility
|
|
|
|
|
{
|
2024-09-18 17:02:14 +08:00
|
|
|
|
[ThreadStatic]
|
|
|
|
|
private static System.Random _randon;
|
|
|
|
|
[ThreadStatic]
|
|
|
|
|
private static byte[] _buffer;
|
|
|
|
|
[ThreadStatic]
|
|
|
|
|
private static bool _isInit;
|
|
|
|
|
|
2024-09-16 19:29:48 +08:00
|
|
|
|
public static unsafe string GenerateNewUniqueID()
|
|
|
|
|
{
|
2024-09-18 17:02:14 +08:00
|
|
|
|
if (_isInit == false)
|
|
|
|
|
{
|
|
|
|
|
IntPtr prt = Marshal.AllocHGlobal(1);
|
|
|
|
|
long alloc = (long)prt;
|
|
|
|
|
Marshal.Release(prt);
|
|
|
|
|
_randon = new System.Random((int)alloc);
|
|
|
|
|
_buffer = new byte[8];
|
|
|
|
|
_isInit = true;
|
|
|
|
|
}
|
2024-09-16 19:29:48 +08:00
|
|
|
|
|
2024-09-18 17:02:14 +08:00
|
|
|
|
byte* hibits = stackalloc byte[8];
|
|
|
|
|
long* hibitsL = (long*)hibits;
|
|
|
|
|
hibitsL[0] = DateTime.Now.Ticks;
|
|
|
|
|
hibitsL[1] = _randon.Next();
|
2024-09-16 19:29:48 +08:00
|
|
|
|
|
2024-09-18 17:02:14 +08:00
|
|
|
|
for (int i = 0; i < 8; i++)
|
2024-09-16 19:29:48 +08:00
|
|
|
|
{
|
2024-09-18 17:02:14 +08:00
|
|
|
|
_buffer[i] = hibits[i];
|
2024-09-16 19:29:48 +08:00
|
|
|
|
}
|
2024-09-18 17:02:14 +08:00
|
|
|
|
|
|
|
|
|
return BitConverter.ToString(_buffer).Replace("-", "");
|
2024-09-16 19:29:48 +08:00
|
|
|
|
}
|
|
|
|
|
public static unsafe string GenerateNewUniqueIDWithAttribute()
|
|
|
|
|
{
|
|
|
|
|
return $"[MetaID(\"{GenerateNewUniqueID()}\")]";
|
|
|
|
|
}
|
|
|
|
|
}
|
2024-09-16 12:24:52 +08:00
|
|
|
|
}
|