Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[API, Modules] Fix registering objects #2686

Merged
merged 15 commits into from
Jul 12, 2024
7 changes: 5 additions & 2 deletions Exiled.API/Features/Core/EObject.cs
Original file line number Diff line number Diff line change
Expand Up @@ -171,14 +171,17 @@ public static Type RegisterObjectType<T>(string name)
/// </summary>
/// <param name="type">The type to register.</param>
/// <param name="name">The name of the registered type.</param>
/// <param name="assembly">The assembly to register object types in.</param>
/// <returns>The registered <see cref="Type"/>.</returns>
public static Type RegisterObjectType(Type type, string name)
public static Type RegisterObjectType(Type type, string name, Assembly assembly = null)
{
assembly ??= Assembly.GetCallingAssembly();

Type matching = GetObjectTypeFromRegisteredTypes(type, name);
if (matching is not null)
return matching;

foreach (Type t in Assembly.GetCallingAssembly().GetTypes()
foreach (Type t in assembly.GetTypes()
.Where(item => item.BaseType == typeof(EObject) || item.IsSubclassOf(typeof(EObject))))
{
if (t.Name != type.Name)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -496,7 +496,7 @@ public static IEnumerable<CustomAbility<T>> EnableAll(Assembly assembly)
if (!customAbility.IsEnabled)
continue;

if (customAbility.TryRegister(attribute))
if (customAbility.TryRegister(assembly, attribute))
customAbilities.Add(customAbility);
}

Expand Down Expand Up @@ -587,9 +587,10 @@ public bool Remove(T entity)
/// <summary>
/// Tries to register a <see cref="CustomAbility{T}"/>.
/// </summary>
/// <param name="assembly">The assembly to register <see cref="CustomAbility{T}"/> from.</param>
/// <param name="attribute">The specified <see cref="CustomAbilityAttribute"/>.</param>
/// <returns><see langword="true"/> if the <see cref="CustomAbility{T}"/> was registered; otherwise, <see langword="false"/>.</returns>
internal bool TryRegister(CustomAbilityAttribute attribute = null)
internal bool TryRegister(Assembly assembly, CustomAbilityAttribute attribute = null)
{
if (!UnorderedRegistered.Contains(this))
{
Expand All @@ -604,7 +605,7 @@ internal bool TryRegister(CustomAbilityAttribute attribute = null)
return false;
}

EObject.RegisterObjectType(BehaviourComponent, Name);
EObject.RegisterObjectType(BehaviourComponent, Name, assembly);
UnorderedRegistered.Add(this);

TypeLookupTable.TryAdd(GetType(), this);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -197,7 +197,7 @@ public static List<CustomEscape> EnableAll(Assembly assembly)
if (!customEscape.IsEnabled)
continue;

if (customEscape.TryRegister(attribute))
if (customEscape.TryRegister(assembly, attribute))
customEscapes.Add(customEscape);
}

Expand Down Expand Up @@ -353,9 +353,10 @@ public void Detach(Player player)
/// <summary>
/// Tries to register a <see cref="CustomEscape"/>.
/// </summary>
/// <param name="assembly">The assembly to register <see cref="CustomEscape"/> from..</param>
/// <param name="attribute">The specified <see cref="CustomEscapeAttribute"/>.</param>
/// <returns><see langword="true"/> if the <see cref="CustomEscape"/> was registered; otherwise, <see langword="false"/>.</returns>
internal bool TryRegister(CustomEscapeAttribute attribute = null)
internal bool TryRegister(Assembly assembly, CustomEscapeAttribute attribute = null)
{
if (!Registered.Contains(this))
{
Expand All @@ -377,7 +378,7 @@ internal bool TryRegister(CustomEscapeAttribute attribute = null)

AllScenariosInternal.AddRange(Scenarios);

EObject.RegisterObjectType(BehaviourComponent, Name);
EObject.RegisterObjectType(BehaviourComponent, Name, assembly);
Registered.Add(this);

TypeLookupTable.TryAdd(GetType(), this);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -225,7 +225,7 @@ public static List<CustomGameMode> EnableAll(Assembly assembly)
continue;
}

if (customGameMode.TryRegister(attribute))
if (customGameMode.TryRegister(assembly, attribute))
customGameModes.Add(customGameMode);
}

Expand All @@ -252,9 +252,10 @@ public static List<CustomGameMode> DisableAll()
/// <summary>
/// Tries to register a <see cref="CustomGameMode"/>.
/// </summary>
/// <param name="assembly">The assembly to register <see cref="CustomGameMode"/> from.</param>
/// <param name="attribute">The specified <see cref="CustomGameModeAttribute"/>.</param>
/// <returns><see langword="true"/> if the <see cref="CustomGameMode"/> was registered; otherwise, <see langword="false"/>.</returns>
internal bool TryRegister(CustomGameModeAttribute attribute = null)
internal bool TryRegister(Assembly assembly, CustomGameModeAttribute attribute = null)
{
if (!Registered.Contains(this))
{
Expand All @@ -275,7 +276,7 @@ internal bool TryRegister(CustomGameModeAttribute attribute = null)
}

foreach (Type t in BehaviourComponents)
EObject.RegisterObjectType(t, typeof(GameState).IsAssignableFrom(t) ? $"GameState-{Name}" : typeof(PlayerState).IsAssignableFrom(t) ? $"PlayerState-{Name}" : Name);
EObject.RegisterObjectType(t, typeof(GameState).IsAssignableFrom(t) ? $"GameState-{Name}" : typeof(PlayerState).IsAssignableFrom(t) ? $"PlayerState-{Name}" : Name, assembly);

Registered.Add(this);

Expand Down
7 changes: 4 additions & 3 deletions Exiled.CustomModules/API/Features/CustomItems/CustomItem.cs
Original file line number Diff line number Diff line change
Expand Up @@ -394,7 +394,7 @@ public static List<CustomItem> EnableAll(Assembly assembly)
if (!customItem.IsEnabled)
continue;

if (customItem.TryRegister(attribute))
if (customItem.TryRegister(assembly, attribute))
customItems.Add(customItem);
}

Expand Down Expand Up @@ -614,9 +614,10 @@ public virtual void Give(Player player, Item item, bool displayMessage = true)
/// <summary>
/// Tries to register a <see cref="CustomItem"/>.
/// </summary>
/// <param name="assembly">The assembly to register items from.</param>
/// <param name="attribute">The specified <see cref="CustomItemAttribute"/>.</param>
/// <returns><see langword="true"/> if the <see cref="CustomItem"/> was registered; otherwise, <see langword="false"/>.</returns>
internal bool TryRegister(CustomItemAttribute attribute = null)
internal bool TryRegister(Assembly assembly, CustomItemAttribute attribute = null)
{
if (!Registered.Contains(this))
{
Expand All @@ -636,7 +637,7 @@ internal bool TryRegister(CustomItemAttribute attribute = null)
return false;
}

EObject.RegisterObjectType(BehaviourComponent, Name);
EObject.RegisterObjectType(BehaviourComponent, Name, assembly);
Registered.Add(this);

TypeLookupTable.TryAdd(GetType(), this);
Expand Down
55 changes: 27 additions & 28 deletions Exiled.CustomModules/API/Features/CustomRoles/CustomRole.cs
Original file line number Diff line number Diff line change
Expand Up @@ -632,7 +632,7 @@ public static List<CustomRole> EnableAll(Assembly assembly)
if (!customRole.IsEnabled)
continue;

if (customRole.TryRegister(attribute))
if (customRole.TryRegister(assembly, attribute))
customRoles.Add(customRole);
}

Expand Down Expand Up @@ -905,42 +905,41 @@ public bool Eject(Pawn player)
/// <summary>
/// Tries to register a <see cref="CustomRole"/>.
/// </summary>
/// <param name="assembly">The assembly to try and register from.</param>
/// <param name="attribute">The specified <see cref="CustomRoleAttribute"/>.</param>
/// <returns><see langword="true"/> if the <see cref="CustomRole"/> was registered; otherwise, <see langword="false"/>.</returns>
internal bool TryRegister(CustomRoleAttribute attribute = null)
internal bool TryRegister(Assembly assembly, CustomRoleAttribute attribute = null)
{
if (!Registered.Contains(this))
if (Registered.Contains(this))
{
if (attribute is not null && Id == 0)
{
if (attribute.Id != 0)
Id = attribute.Id;
else
throw new ArgumentException($"Unable to register {Name}. The ID 0 is reserved for special use.");
}

CustomRole duplicate = Registered.FirstOrDefault(x => x.Id == Id || x.Name == Name || x.BehaviourComponent == BehaviourComponent);
if (duplicate)
{
Log.Warn($"Unable to register {Name}. Another role with the same ID, Name or Behaviour Component already exists: {duplicate.Name}");

return false;
}

EObject.RegisterObjectType(BehaviourComponent, Name);
Registered.Add(this);
Log.Warn($"Unable to register {Name}. Role already exists.");
return false;
}

TypeLookupTable.TryAdd(GetType(), this);
BehaviourLookupTable.TryAdd(BehaviourComponent, this);
IdLookupTable.TryAdd(Id, this);
NameLookupTable.TryAdd(Name, this);
if (attribute is not null && Id == 0)
{
if (attribute.Id != 0)
Id = attribute.Id;
else
throw new ArgumentException($"Unable to register {Name}. The ID 0 is reserved for special use.");
}

return true;
CustomRole duplicate = Registered.FirstOrDefault(x => x.Id == Id || x.Name == Name || x.BehaviourComponent == BehaviourComponent);
if (duplicate)
{
Log.Warn($"Unable to register {Name}. Another role with the same ID, Name or Behaviour Component already exists: {duplicate.Name}");
return false;
}

Log.Warn($"Unable to register {Name}. Role already exists.");
EObject.RegisterObjectType(BehaviourComponent, Name, assembly);
Registered.Add(this);

return false;
TypeLookupTable.TryAdd(GetType(), this);
BehaviourLookupTable.TryAdd(BehaviourComponent, this);
IdLookupTable.TryAdd(Id, this);
NameLookupTable.TryAdd(Name, this);

return true;
}

/// <summary>
Expand Down
Loading