DragonECS/src/Utils/AllowedInWorldsAttribute.cs

45 lines
1.5 KiB
C#
Raw Normal View History

2025-03-14 16:53:25 +08:00
#if DISABLE_DEBUG
#undef DEBUG
#endif
2025-05-18 10:52:24 +08:00
using DCFApixels.DragonECS.Core.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(EcsWorld world, Type componentType)
2025-03-10 13:00:30 +08:00
{
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;
}
}
2025-03-14 21:57:52 +08:00
throw new InvalidOperationException($"Using component {componentType.ToMeta().TypeName} is not allowed in the {worldType.ToMeta().TypeName} world.");
2025-03-10 13:00:30 +08:00
}
}
}
}