From 598aa3951807218685633ed418ea8fa3fd6ac4fe Mon Sep 17 00:00:00 2001 From: Misfiy <85962933+Misfiy@users.noreply.github.com> Date: Tue, 30 Sep 2025 14:09:03 +0200 Subject: [PATCH 1/4] Less duplicate code --- SecretAPI/Attribute/CallOnLoadAttribute.cs | 31 +++++++++++++------- SecretAPI/Attribute/CallOnUnloadAttribute.cs | 26 ++-------------- SecretAPI/Features/IPriority.cs | 13 ++++++++ 3 files changed, 37 insertions(+), 33 deletions(-) create mode 100644 SecretAPI/Features/IPriority.cs diff --git a/SecretAPI/Attribute/CallOnLoadAttribute.cs b/SecretAPI/Attribute/CallOnLoadAttribute.cs index 7c02d9a..cb55356 100644 --- a/SecretAPI/Attribute/CallOnLoadAttribute.cs +++ b/SecretAPI/Attribute/CallOnLoadAttribute.cs @@ -4,12 +4,13 @@ using System.Collections.Generic; using System.Linq; using System.Reflection; + using SecretAPI.Features; /// /// Defines the attribute for methods to call on load. /// [AttributeUsage(AttributeTargets.Method)] - public class CallOnLoadAttribute : Attribute + public class CallOnLoadAttribute : Attribute, IPriority { /// /// Initializes a new instance of the class. @@ -32,9 +33,25 @@ public CallOnLoadAttribute(int priority = 0) public static void Load(Assembly? assembly = null) { assembly ??= Assembly.GetCallingAssembly(); + CallAttributeMethodPriority(assembly); + } + + /// + public override bool Equals(object? obj) => obj == this; + /// + public override int GetHashCode() => base.GetHashCode(); + + /// + /// Calls the method associated with a Attribute implementing . + /// + /// The assembly to handle calls from. + /// The attribute required for this to be called. + internal static void CallAttributeMethodPriority(Assembly assembly) + where TAttribute : Attribute, IPriority + { const BindingFlags methodFlags = BindingFlags.Static | BindingFlags.Public | BindingFlags.NonPublic; - Dictionary methods = new(); + Dictionary methods = new(); // get all types foreach (Type type in assembly.GetTypes()) @@ -42,7 +59,7 @@ public static void Load(Assembly? assembly = null) // get all static methods foreach (MethodInfo method in type.GetMethods(methodFlags)) { - CallOnLoadAttribute? attribute = method.GetCustomAttribute(); + TAttribute? attribute = method.GetCustomAttribute(); if (attribute == null) continue; @@ -50,14 +67,8 @@ public static void Load(Assembly? assembly = null) } } - foreach (KeyValuePair method in methods.OrderBy(static v => v.Key.Priority)) + foreach (KeyValuePair method in methods.OrderBy(static v => v.Key.Priority)) method.Value.Invoke(null, null); } - - /// - public override bool Equals(object? obj) => obj == this; - - /// - public override int GetHashCode() => base.GetHashCode(); } } \ No newline at end of file diff --git a/SecretAPI/Attribute/CallOnUnloadAttribute.cs b/SecretAPI/Attribute/CallOnUnloadAttribute.cs index 2303047..b886d55 100644 --- a/SecretAPI/Attribute/CallOnUnloadAttribute.cs +++ b/SecretAPI/Attribute/CallOnUnloadAttribute.cs @@ -1,15 +1,14 @@ namespace SecretAPI.Attribute { using System; - using System.Collections.Generic; - using System.Linq; using System.Reflection; + using SecretAPI.Features; /// /// Defines the attribute for methods to call on unload. /// [AttributeUsage(AttributeTargets.Method)] - public class CallOnUnloadAttribute : Attribute + public class CallOnUnloadAttribute : Attribute, IPriority { /// /// Initializes a new instance of the class. @@ -32,26 +31,7 @@ public CallOnUnloadAttribute(int priority = 0) public static void Unload(Assembly? assembly = null) { assembly ??= Assembly.GetCallingAssembly(); - - const BindingFlags methodFlags = BindingFlags.Static | BindingFlags.Public | BindingFlags.NonPublic; - Dictionary methods = new(); - - // get all types - foreach (Type type in assembly.GetTypes()) - { - // get all static methods - foreach (MethodInfo method in type.GetMethods(methodFlags)) - { - CallOnUnloadAttribute? attribute = method.GetCustomAttribute(); - if (attribute == null) - continue; - - methods.Add(attribute, method); - } - } - - foreach (KeyValuePair method in methods.OrderBy(static v => v.Key.Priority)) - method.Value.Invoke(null, null); + CallOnLoadAttribute.CallAttributeMethodPriority(assembly); } /// diff --git a/SecretAPI/Features/IPriority.cs b/SecretAPI/Features/IPriority.cs new file mode 100644 index 0000000..f337724 --- /dev/null +++ b/SecretAPI/Features/IPriority.cs @@ -0,0 +1,13 @@ +namespace SecretAPI.Features +{ + /// + /// Handles IPriority. + /// + public interface IPriority + { + /// + /// Gets the current priority. + /// + public int Priority { get; } + } +} \ No newline at end of file From 0d3849c55da45d89031063ee1b212f00a430843c Mon Sep 17 00:00:00 2001 From: Misfiy <85962933+Misfiy@users.noreply.github.com> Date: Sat, 25 Oct 2025 21:42:31 +0200 Subject: [PATCH 2/4] SCP:SL 14.2 update --- SecretAPI.Examples/SecretAPI.Examples.csproj | 2 +- SecretAPI/SecretAPI.csproj | 2 +- SecretAPI/SecretApi.cs | 8 +++++--- 3 files changed, 7 insertions(+), 5 deletions(-) diff --git a/SecretAPI.Examples/SecretAPI.Examples.csproj b/SecretAPI.Examples/SecretAPI.Examples.csproj index a2aeb98..076e87e 100644 --- a/SecretAPI.Examples/SecretAPI.Examples.csproj +++ b/SecretAPI.Examples/SecretAPI.Examples.csproj @@ -9,7 +9,7 @@ - + diff --git a/SecretAPI/SecretAPI.csproj b/SecretAPI/SecretAPI.csproj index 26e8dd1..4ae9c82 100644 --- a/SecretAPI/SecretAPI.csproj +++ b/SecretAPI/SecretAPI.csproj @@ -29,7 +29,7 @@ - + diff --git a/SecretAPI/SecretApi.cs b/SecretAPI/SecretApi.cs index 4c12582..6272099 100644 --- a/SecretAPI/SecretApi.cs +++ b/SecretAPI/SecretApi.cs @@ -3,6 +3,7 @@ using System; using System.Reflection; using HarmonyLib; + using LabApi.Features; using LabApi.Loader.Features.Plugins; using LabApi.Loader.Features.Plugins.Enums; using SecretAPI.Attribute; @@ -28,10 +29,11 @@ public class SecretApi : Plugin public override Version Version { get; } = Assembly.GetName().Version; /// - public override Version RequiredApiVersion { get; } = new(LabApi.Features.LabApiProperties.CompiledVersion); + public override Version RequiredApiVersion => LabApiProperties.CurrentVersion; - /*/// - public override bool IsTransparent => true;*/ + /// + /// We use transparent here because this is an API and should not interfere by itself with game logic. + public override bool IsTransparent => true; /// /// Gets the harmony to use for the API. From 3fa28d9907c5876726e14321c7edf5f6f729ee16 Mon Sep 17 00:00:00 2001 From: Misfiy <85962933+Misfiy@users.noreply.github.com> Date: Sat, 25 Oct 2025 21:51:27 +0200 Subject: [PATCH 3/4] SL 14.2 | Version Bump: 2.0.2 --- SecretAPI/Extensions/CollectionExtensions.cs | 2 ++ SecretAPI/Extensions/RoleExtensions.cs | 26 +++++++++----------- SecretAPI/Extensions/RoomExtensions.cs | 4 ++- SecretAPI/SecretAPI.csproj | 2 +- 4 files changed, 17 insertions(+), 17 deletions(-) diff --git a/SecretAPI/Extensions/CollectionExtensions.cs b/SecretAPI/Extensions/CollectionExtensions.cs index b52b2ce..28e1887 100644 --- a/SecretAPI/Extensions/CollectionExtensions.cs +++ b/SecretAPI/Extensions/CollectionExtensions.cs @@ -1,5 +1,6 @@ namespace SecretAPI.Extensions { + using System; using System.Collections.Generic; using System.Diagnostics.CodeAnalysis; using System.Linq; @@ -16,6 +17,7 @@ public static class CollectionExtensions /// The collection to pull from. /// The Type contained by the collection. /// A random value, default value when empty collection. + /// Will occur if the collection is empty. public static T GetRandomValue(this IEnumerable collection) { TryGetRandomValue(collection, out T? value); diff --git a/SecretAPI/Extensions/RoleExtensions.cs b/SecretAPI/Extensions/RoleExtensions.cs index 2d8bd28..0eb3d46 100644 --- a/SecretAPI/Extensions/RoleExtensions.cs +++ b/SecretAPI/Extensions/RoleExtensions.cs @@ -1,10 +1,11 @@ namespace SecretAPI.Extensions { + using System; using System.Diagnostics.CodeAnalysis; using InventorySystem; - using InventorySystem.Configs; + using LabApi.Features.Extensions; using PlayerRoles; - using PlayerRoles.FirstPersonControl; + using Respawning.Objectives; using UnityEngine; /// @@ -19,16 +20,18 @@ public static class RoleExtensions /// The found. /// The . /// The role base found, else null. + [Obsolete("Use LabApi.Features.Extensions.RoleExtensions.TryGetRoleBase")] public static bool TryGetRoleBase(this RoleTypeId roleTypeId, [NotNullWhen(true)] out T? role) - => PlayerRoleLoader.TryGetRoleTemplate(roleTypeId, out role); + => LabApi.Features.Extensions.RoleExtensions.TryGetRoleBase(roleTypeId, out role); /// /// Gets the color of a . /// /// The role to get color of. /// The color found, if not found then white. + [Obsolete("Use Respawning.Objectives.GetRoleColor")] public static Color GetColor(this RoleTypeId roleTypeId) - => TryGetRoleBase(roleTypeId, out PlayerRoleBase? role) ? role.RoleColor : Color.white; + => roleTypeId.GetRoleColor(); /// /// Tries to get a random spawn point from a . @@ -37,24 +40,17 @@ public static Color GetColor(this RoleTypeId roleTypeId) /// The position found. /// The rotation found. /// Whether a spawnpoint was found. + [Obsolete("Use LabApi.Features.Extensions.RoleExtensions.TryGetRandomSpawnPoint")] public static bool GetRandomSpawnPosition(this RoleTypeId role, out Vector3 position, out float horizontalRot) - { - if (TryGetRoleBase(role, out IFpcRole? fpc)) - return fpc.SpawnpointHandler.TryGetSpawnpoint(out position, out horizontalRot); - - position = Vector3.zero; - horizontalRot = 0f; - return false; - } + => role.TryGetRandomSpawnPoint(out position, out horizontalRot); /// /// Gets the inventory of the specified . /// /// The . /// The found. + [Obsolete("Use LabApi.Features.Extensions.RoleExtensions.GetInventory")] public static InventoryRoleInfo GetInventory(this RoleTypeId role) - => StartingInventories.DefinedInventories.TryGetValue(role, out InventoryRoleInfo info) - ? info - : new InventoryRoleInfo([], []); + => LabApi.Features.Extensions.RoleExtensions.GetInventory(role); } } \ No newline at end of file diff --git a/SecretAPI/Extensions/RoomExtensions.cs b/SecretAPI/Extensions/RoomExtensions.cs index 8ffe917..41629af 100644 --- a/SecretAPI/Extensions/RoomExtensions.cs +++ b/SecretAPI/Extensions/RoomExtensions.cs @@ -14,7 +14,9 @@ public static class RoomExtensions [ RoomName.HczTesla, // Instant death RoomName.EzEvacShelter, // Stuck permanently - RoomName.EzCollapsedTunnel // Stuck permanently + RoomName.EzCollapsedTunnel, // Stuck permanently + RoomName.HczWaysideIncinerator, // Death + RoomName.Hcz096, // Void ]; /// diff --git a/SecretAPI/SecretAPI.csproj b/SecretAPI/SecretAPI.csproj index 4ae9c82..bfa4f3b 100644 --- a/SecretAPI/SecretAPI.csproj +++ b/SecretAPI/SecretAPI.csproj @@ -4,7 +4,7 @@ net48 latest enable - 2.0.1 + 2.0.2 true From 9a737ad3788493c97d2e0a11603e2658442a350f Mon Sep 17 00:00:00 2001 From: Misfiy <85962933+Misfiy@users.noreply.github.com> Date: Sat, 25 Oct 2025 21:53:58 +0200 Subject: [PATCH 4/4] Obsolete: RoleExtensions --- SecretAPI/Extensions/RoleExtensions.cs | 1 + 1 file changed, 1 insertion(+) diff --git a/SecretAPI/Extensions/RoleExtensions.cs b/SecretAPI/Extensions/RoleExtensions.cs index 0eb3d46..e436281 100644 --- a/SecretAPI/Extensions/RoleExtensions.cs +++ b/SecretAPI/Extensions/RoleExtensions.cs @@ -11,6 +11,7 @@ /// /// Extensions related to . /// + [Obsolete("This no longer provides anything that basegame/LabAPI does not")] public static class RoleExtensions { ///