2025-03-14 16:53:25 +08:00
|
|
|
|
#if DISABLE_DEBUG
|
|
|
|
|
#undef DEBUG
|
|
|
|
|
#endif
|
|
|
|
|
using DCFApixels.DragonECS.Internal;
|
2025-03-10 13:00:30 +08:00
|
|
|
|
using System;
|
|
|
|
|
|
|
|
|
|
namespace DCFApixels.DragonECS
|
|
|
|
|
{
|
|
|
|
|
[AttributeUsage(AttributeTargets.Struct | AttributeTargets.Class, Inherited = false, AllowMultiple = true)]
|
|
|
|
|
public sealed class AllowedInWorldsAttribute : Attribute
|
|
|
|
|
{
|
|
|
|
|
public object[] AllowedWorlds;
|
|
|
|
|
public AllowedInWorldsAttribute(params object[] allowedWorlds)
|
|
|
|
|
{
|
|
|
|
|
AllowedWorlds = allowedWorlds;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
public static void CheckAllows<T>(EcsWorld world)
|
|
|
|
|
{
|
|
|
|
|
Type componentType = typeof(T);
|
|
|
|
|
Type worldType = world.GetType();
|
|
|
|
|
if (componentType.TryGetAttribute(out AllowedInWorldsAttribute attribute))
|
|
|
|
|
{
|
|
|
|
|
foreach (var worldTag in attribute.AllowedWorlds)
|
|
|
|
|
{
|
|
|
|
|
bool result = false;
|
|
|
|
|
if (worldTag is Type worldTypeTag)
|
|
|
|
|
{
|
|
|
|
|
result = worldTypeTag == worldType;
|
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
|
|
|
|
string worldStringTag = worldTag.ToString();
|
|
|
|
|
result = world.Name == worldStringTag;
|
|
|
|
|
}
|
|
|
|
|
if (result)
|
|
|
|
|
{
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
throw new EcsFrameworkException($"Using component {componentType.ToMeta().TypeName} is not allowed in the {worldType.ToMeta().TypeName} world.");
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|