diff --git a/Exiled.API/Features/Core/EObject.cs b/Exiled.API/Features/Core/EObject.cs index 61277c552f..85fa0ad639 100644 --- a/Exiled.API/Features/Core/EObject.cs +++ b/Exiled.API/Features/Core/EObject.cs @@ -171,14 +171,17 @@ public static Type RegisterObjectType(string name) /// /// The type to register. /// The name of the registered type. + /// The assembly to register object types in. /// The registered . - 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) diff --git a/Exiled.CustomModules/API/Features/CustomAbilities/CustomAbility.cs b/Exiled.CustomModules/API/Features/CustomAbilities/CustomAbility.cs index 849b892583..6930618c1b 100644 --- a/Exiled.CustomModules/API/Features/CustomAbilities/CustomAbility.cs +++ b/Exiled.CustomModules/API/Features/CustomAbilities/CustomAbility.cs @@ -496,7 +496,7 @@ public static IEnumerable> EnableAll(Assembly assembly) if (!customAbility.IsEnabled) continue; - if (customAbility.TryRegister(attribute)) + if (customAbility.TryRegister(assembly, attribute)) customAbilities.Add(customAbility); } @@ -587,9 +587,10 @@ public bool Remove(T entity) /// /// Tries to register a . /// + /// The assembly to register from. /// The specified . /// if the was registered; otherwise, . - internal bool TryRegister(CustomAbilityAttribute attribute = null) + internal bool TryRegister(Assembly assembly, CustomAbilityAttribute attribute = null) { if (!UnorderedRegistered.Contains(this)) { @@ -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); diff --git a/Exiled.CustomModules/API/Features/CustomEscapes/CustomEscape.cs b/Exiled.CustomModules/API/Features/CustomEscapes/CustomEscape.cs index bc5af2ae3a..24d3467477 100644 --- a/Exiled.CustomModules/API/Features/CustomEscapes/CustomEscape.cs +++ b/Exiled.CustomModules/API/Features/CustomEscapes/CustomEscape.cs @@ -197,7 +197,7 @@ public static List EnableAll(Assembly assembly) if (!customEscape.IsEnabled) continue; - if (customEscape.TryRegister(attribute)) + if (customEscape.TryRegister(assembly, attribute)) customEscapes.Add(customEscape); } @@ -353,9 +353,10 @@ public void Detach(Player player) /// /// Tries to register a . /// + /// The assembly to register from.. /// The specified . /// if the was registered; otherwise, . - internal bool TryRegister(CustomEscapeAttribute attribute = null) + internal bool TryRegister(Assembly assembly, CustomEscapeAttribute attribute = null) { if (!Registered.Contains(this)) { @@ -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); diff --git a/Exiled.CustomModules/API/Features/CustomGamemodes/CustomGameMode.cs b/Exiled.CustomModules/API/Features/CustomGamemodes/CustomGameMode.cs index 262ccc61bc..da26a3062f 100644 --- a/Exiled.CustomModules/API/Features/CustomGamemodes/CustomGameMode.cs +++ b/Exiled.CustomModules/API/Features/CustomGamemodes/CustomGameMode.cs @@ -225,7 +225,7 @@ public static List EnableAll(Assembly assembly) continue; } - if (customGameMode.TryRegister(attribute)) + if (customGameMode.TryRegister(assembly, attribute)) customGameModes.Add(customGameMode); } @@ -252,9 +252,10 @@ public static List DisableAll() /// /// Tries to register a . /// + /// The assembly to register from. /// The specified . /// if the was registered; otherwise, . - internal bool TryRegister(CustomGameModeAttribute attribute = null) + internal bool TryRegister(Assembly assembly, CustomGameModeAttribute attribute = null) { if (!Registered.Contains(this)) { @@ -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); diff --git a/Exiled.CustomModules/API/Features/CustomItems/CustomItem.cs b/Exiled.CustomModules/API/Features/CustomItems/CustomItem.cs index 57bc64e885..8039e4c441 100644 --- a/Exiled.CustomModules/API/Features/CustomItems/CustomItem.cs +++ b/Exiled.CustomModules/API/Features/CustomItems/CustomItem.cs @@ -394,7 +394,7 @@ public static List EnableAll(Assembly assembly) if (!customItem.IsEnabled) continue; - if (customItem.TryRegister(attribute)) + if (customItem.TryRegister(assembly, attribute)) customItems.Add(customItem); } @@ -614,9 +614,10 @@ public virtual void Give(Player player, Item item, bool displayMessage = true) /// /// Tries to register a . /// + /// The assembly to register items from. /// The specified . /// if the was registered; otherwise, . - internal bool TryRegister(CustomItemAttribute attribute = null) + internal bool TryRegister(Assembly assembly, CustomItemAttribute attribute = null) { if (!Registered.Contains(this)) { @@ -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); diff --git a/Exiled.CustomModules/API/Features/CustomRoles/CustomRole.cs b/Exiled.CustomModules/API/Features/CustomRoles/CustomRole.cs index 7346ae63f7..ebff580130 100644 --- a/Exiled.CustomModules/API/Features/CustomRoles/CustomRole.cs +++ b/Exiled.CustomModules/API/Features/CustomRoles/CustomRole.cs @@ -632,7 +632,7 @@ public static List EnableAll(Assembly assembly) if (!customRole.IsEnabled) continue; - if (customRole.TryRegister(attribute)) + if (customRole.TryRegister(assembly, attribute)) customRoles.Add(customRole); } @@ -905,42 +905,41 @@ public bool Eject(Pawn player) /// /// Tries to register a . /// + /// The assembly to try and register from. /// The specified . /// if the was registered; otherwise, . - 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; } ///