mirror of
https://github.com/DCFApixels/DragonECS.git
synced 2025-09-19 02:24:37 +08:00
44 lines
1.5 KiB
C#
44 lines
1.5 KiB
C#
![]() |
using DCFApixels.DragonECS.Internal;
|
|||
|
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.");
|
|||
|
}
|
|||
|
}
|
|||
|
}
|
|||
|
}
|