From 36bf1f55c3452cdd9d7c168695bb6378b470e7c5 Mon Sep 17 00:00:00 2001 From: Eve <85962933+obvEve@users.noreply.github.com> Date: Sat, 14 Nov 2026 20:40:22 +0100 Subject: [PATCH 1/6] prepare for 4.0 --- SecretAPI.Examples/ExampleEntry.cs | 9 +-- SecretAPI/Attributes/HarmonyPatchCategory.cs | 27 ------- SecretAPI/Config.cs | 11 +++ SecretAPI/Debugging/PatchDebugger.cs | 14 ++++ SecretAPI/Extensions/HarmonyExtensions.cs | 73 ------------------- .../Features/Effects/CustomPlayerEffect.cs | 16 ++-- SecretAPI/Patches/Features/HarmonyLogger.cs | 3 + .../Settings/ReportDefinedSettingPatch.cs | 3 +- SecretAPI/SecretApi.cs | 30 +++++--- 9 files changed, 58 insertions(+), 128 deletions(-) delete mode 100644 SecretAPI/Attributes/HarmonyPatchCategory.cs delete mode 100644 SecretAPI/Extensions/HarmonyExtensions.cs diff --git a/SecretAPI.Examples/ExampleEntry.cs b/SecretAPI.Examples/ExampleEntry.cs index 132eb0d..2f3c49d 100644 --- a/SecretAPI.Examples/ExampleEntry.cs +++ b/SecretAPI.Examples/ExampleEntry.cs @@ -1,11 +1,4 @@ -/* - Temp until 4.0 -*/ -#pragma warning disable SA1200 -global using HarmonyPatchCategoryAttribute = HarmonyLib.HarmonyPatchCategory; -#pragma warning restore SA1200 - -namespace SecretAPI.Examples; +namespace SecretAPI.Examples; using System; using System.Reflection; diff --git a/SecretAPI/Attributes/HarmonyPatchCategory.cs b/SecretAPI/Attributes/HarmonyPatchCategory.cs deleted file mode 100644 index 00026d9..0000000 --- a/SecretAPI/Attributes/HarmonyPatchCategory.cs +++ /dev/null @@ -1,27 +0,0 @@ -namespace SecretAPI.Attributes; - -using System; -using SecretAPI.Extensions; - -/// -/// Category handling for . -/// -[AttributeUsage(AttributeTargets.Class, AllowMultiple = true)] -[Obsolete("Please use HarmonyLib.HarmonyPatchCategoryAttribute - This will be removed in 4.0")] -public class HarmonyPatchCategory : HarmonyLib.HarmonyPatchCategory -{ - /// - /// Initializes a new instance of the class. - /// - /// The category of the patch. - public HarmonyPatchCategory(string category) - : base(category) - { - Category = category; - } - - /// - /// Gets the patch category. - /// - public string Category { get; } -} \ No newline at end of file diff --git a/SecretAPI/Config.cs b/SecretAPI/Config.cs index 1483b11..3f69ab2 100644 --- a/SecretAPI/Config.cs +++ b/SecretAPI/Config.cs @@ -1,5 +1,9 @@ namespace SecretAPI; +using System.ComponentModel; +using SecretAPI.Features.UserSettings; +using UserSettings.ServerSpecific; + /// /// Handles config for SecretAPI. /// @@ -8,5 +12,12 @@ public sealed class Config /// /// Gets or sets a value indicating whether PatchClassProcessor.ReportException should be edited to log instead of throwing exception. /// + [Description("When set to true, will patch PatchClassProcessor.ReportException to log instead of throw.")] public bool LogHarmonyReplaceThrow { get; set; } = false; + + /// + /// Gets or sets a value indicating whether should validate . + /// + [Description("When set to true and CustomSetting is used, will begin validating ServerSpecificSettingsSync.DefinedSettings.")] + public bool ValidateDefinedSettings { get; set; } = true; } \ No newline at end of file diff --git a/SecretAPI/Debugging/PatchDebugger.cs b/SecretAPI/Debugging/PatchDebugger.cs index 2e38b72..02e8ae6 100644 --- a/SecretAPI/Debugging/PatchDebugger.cs +++ b/SecretAPI/Debugging/PatchDebugger.cs @@ -2,6 +2,9 @@ namespace SecretAPI.Debugging; +using System.Collections.Generic; +using System.IO; +using System.Reflection; using SecretAPI.Attributes; /// @@ -16,6 +19,17 @@ internal static class PatchDebugger internal static void Load() { SecretApi.Harmony.PatchAll(SecretApi.Assembly); + IEnumerable patchedMethods = SecretApi.Harmony.GetPatchedMethods(); + int patchCount = 0; + List patchedMethodNames = new(); + foreach (MethodBase patchedMethod in patchedMethods) + { + patchCount++; + patchedMethodNames.Add((patchedMethod.IsStatic ? "static " : "instance ") + (patchedMethod.DeclaringType?.FullName ?? "No Type") + $" : {patchedMethod.Name}"); + } + + patchedMethodNames.Insert(0, $"SecretAPI has patched {patchCount} methods:"); + File.WriteAllLines(Path.Combine(SecretApi.ConfigDirectory.FullName, "debug_patches.txt"), patchedMethodNames); } } diff --git a/SecretAPI/Extensions/HarmonyExtensions.cs b/SecretAPI/Extensions/HarmonyExtensions.cs deleted file mode 100644 index fde6878..0000000 --- a/SecretAPI/Extensions/HarmonyExtensions.cs +++ /dev/null @@ -1,73 +0,0 @@ -namespace SecretAPI.Extensions; - -using System; -using System.Collections.Generic; -using System.Linq; -using System.Reflection; -using HarmonyLib; -using LabApi.Features.Console; -using SecretAPI.Attributes; - -/// -/// Handles patching. -/// -public static class HarmonyExtensions -{ - /// The harmony to use for the patch. - extension(Harmony harmony) - { - /// - /// Patches all methods with the proper . - /// - /// The category to patch. - /// The assembly to find patches in. - [Obsolete("Use Harmony::PatchCategory(Assembly, string)")] - public void PatchCategory(string category, Assembly? assembly = null) - { - assembly ??= Assembly.GetCallingAssembly(); - harmony.PatchCategory(assembly, category); - - /*assembly.GetTypes().Where(type => - { - IEnumerable categories = type.GetCustomAttributes(); - return categories.Any(c => c.info.category == category); - }) - .Do(type => SafePatch(harmony, type));*/ - } - - /// - /// Patches all patches that don't have a . - /// - /// The assembly to look for patches. - [Obsolete("Use Harmony::PatchAllUncategorized(Assembly)")] - public void PatchAllNoCategory(Assembly? assembly = null) - { - assembly ??= Assembly.GetCallingAssembly(); - harmony.PatchAllUncategorized(assembly); - - /*assembly.GetTypes().Where(type => - { - IEnumerable categories = type.GetCustomAttributes(); - return !categories.Any(); - }) - .Do(type => SafePatch(harmony, type));*/ - } - - /// - /// Attempts to safely patch a , logging any errors. - /// - /// The to attempt to patch. - [Obsolete] - public void SafePatch(Type type) - { - try - { - harmony.CreateClassProcessor(type).Patch(); - } - catch (Exception ex) - { - Logger.Error($"[HarmonyExtensions] Failed to safely patch {harmony.Id} ({type.FullName}): {ex}"); - } - } - } -} \ No newline at end of file diff --git a/SecretAPI/Features/Effects/CustomPlayerEffect.cs b/SecretAPI/Features/Effects/CustomPlayerEffect.cs index 058fb1d..2653350 100644 --- a/SecretAPI/Features/Effects/CustomPlayerEffect.cs +++ b/SecretAPI/Features/Effects/CustomPlayerEffect.cs @@ -18,11 +18,12 @@ /// public abstract class CustomPlayerEffect : StatusEffectBase { + private static HashSet registeredEffects = new(); + /// - /// Gets a list of types to register (Must inherit ). + /// Gets a readonly collection of all the registered effects. /// - [Obsolete("This is becoming readonly in 4.0 - Use Register()")] - public static List EffectsToRegister { get; } = []; // TODO 4.0: private static HashSet RegisteredEffects + public static IReadOnlyCollection RegisteredEffects => registeredEffects; /// /// Gets the with this effect. @@ -54,21 +55,18 @@ public static void Register(Type type) return; } -#pragma warning disable CS0618 // Type or member is obsolete - if (EffectsToRegister.Contains(type)) + if (!registeredEffects.Add(type)) { Logger.Error($"[CustomPlayerEffect.Register] {type.FullName} is already registered!"); return; } - - EffectsToRegister.Add(type); } /// public override string ToString() => $"{GetType().Name}: Owner ({Owner}) - Intensity ({Intensity}) - Duration {Duration}"; /// - /// Initializes the to implement . + /// Initializes the to implement . /// /// The effects must be loaded on before is created. [CallOnLoad] @@ -89,7 +87,7 @@ private static void OnSceneLoaded(Scene scene, LoadSceneMode mode) SceneManager.sceneLoaded -= OnSceneLoaded; Transform playerEffects = PrefabStore.Prefab.playerEffectsController.effectsGameObject.transform; - foreach (Type type in EffectsToRegister) + foreach (Type type in RegisteredEffects) { if (!typeof(StatusEffectBase).IsAssignableFrom(type)) { diff --git a/SecretAPI/Patches/Features/HarmonyLogger.cs b/SecretAPI/Patches/Features/HarmonyLogger.cs index 6b74ad5..1d22eb3 100644 --- a/SecretAPI/Patches/Features/HarmonyLogger.cs +++ b/SecretAPI/Patches/Features/HarmonyLogger.cs @@ -9,9 +9,12 @@ /// /// Changes 's ReportException to log instead of throwing the exception. /// +[HarmonyPatchCategory(nameof(SecretAPI))] [HarmonyPatch(typeof(PatchClassProcessor), "ReportException")] internal static class HarmonyLogger { + private static bool Prepare() => SecretApi.Config.LogHarmonyReplaceThrow || SecretApi.ForceHarmonyConditionTrue; + private static IEnumerable Transpiler(IEnumerable instructions) { CodeMatcher matcher = new(instructions); diff --git a/SecretAPI/Patches/Features/Settings/ReportDefinedSettingPatch.cs b/SecretAPI/Patches/Features/Settings/ReportDefinedSettingPatch.cs index acfd1b5..44b706f 100644 --- a/SecretAPI/Patches/Features/Settings/ReportDefinedSettingPatch.cs +++ b/SecretAPI/Patches/Features/Settings/ReportDefinedSettingPatch.cs @@ -3,7 +3,6 @@ using System.Collections.Generic; using HarmonyLib; using NorthwoodLib.Pools; -using SecretAPI.Attributes; using SecretAPI.Features.UserSettings; using UserSettings.ServerSpecific; @@ -14,6 +13,8 @@ [HarmonyPatch(typeof(ServerSpecificSettingsSync), nameof(ServerSpecificSettingsSync.DefinedSettings), MethodType.Setter)] internal static class ReportDefinedSettingPatch { + private static bool Prepare() => SecretApi.Config.ValidateDefinedSettings || SecretApi.ForceHarmonyConditionTrue; + private static void Prefix(ref ServerSpecificSettingBase[] value) { List settings = ListPool.Shared.Rent(); diff --git a/SecretAPI/SecretApi.cs b/SecretAPI/SecretApi.cs index edd0143..359d78d 100644 --- a/SecretAPI/SecretApi.cs +++ b/SecretAPI/SecretApi.cs @@ -1,11 +1,4 @@ -/* - Temp until 4.0 -*/ -#pragma warning disable SA1200 // Using directive should appear within a namespace declaration -global using HarmonyPatchCategoryAttribute = HarmonyLib.HarmonyPatchCategory; -#pragma warning restore SA1200 - -namespace SecretAPI; +namespace SecretAPI; using System; using System.IO; @@ -16,12 +9,23 @@ namespace SecretAPI; using LabApi.Loader.Features.Plugins; using LabApi.Loader.Features.Plugins.Enums; using SecretAPI.Patches.Features; +using SecretAPI.Patches.Features.Settings; /// /// Main class handling loading API. /// public class SecretApi : Plugin { + /// + /// Whether conditional harmony patches should be forced. This is to simply things such as and . + /// + internal const bool ForceHarmonyConditionTrue = +#if DEBUG + true; +#else + false; +#endif + /// public override string Name => "SecretAPI"; @@ -54,6 +58,12 @@ public class SecretApi : Plugin /// internal static Assembly Assembly { get; } = typeof(SecretApi).Assembly; + /// + /// Gets the current loaded. + /// + /// This is null before is called. + internal static new Config Config { get; private set; } = null!; + /// /// Gets the config directory within "SCP Secret Laboratory/LabAPI/configs/SecretAPI" for internal use. /// @@ -62,8 +72,8 @@ public class SecretApi : Plugin /// public override void Enable() { - if (Config.LogHarmonyReplaceThrow) - Harmony.CreateClassProcessor(typeof(HarmonyLogger)).Patch(); + Config = base.Config; + Harmony.PatchCategory(nameof(SecretAPI)); SecretApiGenerated.OnLoad(); } From 72118f0f82cf43707f8209a845387b5b5d28c723 Mon Sep 17 00:00:00 2001 From: Eve <85962933+obvEve@users.noreply.github.com> Date: Sat, 14 Nov 2026 20:45:29 +0100 Subject: [PATCH 2/6] Registry.cs removed --- SecretAPI/Features/Registry.cs | 23 ----------------------- 1 file changed, 23 deletions(-) delete mode 100644 SecretAPI/Features/Registry.cs diff --git a/SecretAPI/Features/Registry.cs b/SecretAPI/Features/Registry.cs deleted file mode 100644 index 8103a70..0000000 --- a/SecretAPI/Features/Registry.cs +++ /dev/null @@ -1,23 +0,0 @@ -namespace SecretAPI.Features; - -using System; -using System.Collections.Generic; - -/// -/// Handles the registration of objects and ids. -/// -/// The type of the registry. -[Obsolete("This will be removed in 4.0")] -public static class Registry -{ - /// - /// Gets the registered objects. - /// - public static List Registered { get; } = []; - - /// - /// Gets or sets the current id iteration. - /// - /// You should increment this with ++CurrentId when using it. - public static int CurrentId { get; set; } -} \ No newline at end of file From c70e13f3410455ea8c94949bdeafb3ae586f67e0 Mon Sep 17 00:00:00 2001 From: Eve <85962933+obvEve@users.noreply.github.com> Date: Sat, 14 Nov 2026 21:00:08 +0100 Subject: [PATCH 3/6] Update to Harmony 2.4.3 from 2.4.2 --- Directory.Build.props | 4 ++++ SecretAPI.Examples/SecretAPI.Examples.csproj | 2 +- SecretAPI/SecretAPI.csproj | 2 +- 3 files changed, 6 insertions(+), 2 deletions(-) diff --git a/Directory.Build.props b/Directory.Build.props index daaab19..0b8746b 100644 --- a/Directory.Build.props +++ b/Directory.Build.props @@ -3,6 +3,10 @@ enable 3.5.0-beta.3 + + + 2.4.3 + obvEve diff --git a/SecretAPI.Examples/SecretAPI.Examples.csproj b/SecretAPI.Examples/SecretAPI.Examples.csproj index cce57e4..d63331a 100644 --- a/SecretAPI.Examples/SecretAPI.Examples.csproj +++ b/SecretAPI.Examples/SecretAPI.Examples.csproj @@ -7,7 +7,7 @@ - + diff --git a/SecretAPI/SecretAPI.csproj b/SecretAPI/SecretAPI.csproj index 1689e92..a4726ac 100644 --- a/SecretAPI/SecretAPI.csproj +++ b/SecretAPI/SecretAPI.csproj @@ -25,7 +25,7 @@ - + From 9c4ef4d2d1c614ba5264dc65ec992168454de19d Mon Sep 17 00:00:00 2001 From: Eve <85962933+obvEve@users.noreply.github.com> Date: Sat, 14 Nov 2026 21:08:50 +0100 Subject: [PATCH 4/6] Remove obsolete --- SecretAPI/Extensions/CodeMatcherExtensions.cs | 9 --------- 1 file changed, 9 deletions(-) diff --git a/SecretAPI/Extensions/CodeMatcherExtensions.cs b/SecretAPI/Extensions/CodeMatcherExtensions.cs index a2b849e..77947e4 100644 --- a/SecretAPI/Extensions/CodeMatcherExtensions.cs +++ b/SecretAPI/Extensions/CodeMatcherExtensions.cs @@ -19,15 +19,6 @@ public static class CodeMatcherExtensions /// The current . public CodeMatcher EndAndBacktrack(int backtrackCount) => matcher.End().Advance(-backtrackCount); - /// - /// Declares a local to be used of a certain . - /// - /// The of the local to declare. - /// The declared. - /// The current . - [Obsolete("Use CodeMatcher::DeclareLocal(Type, out LocalBuilder)")] - public CodeMatcher DeclareLocal(Type localType, out LocalBuilder localBuilder) => matcher.DeclareLocal(localType, out localBuilder); - /// /// Declares a local to be used of a certain . /// From 2c933a5998e0b4a3335ed18f90ca0334e74499dd Mon Sep 17 00:00:00 2001 From: Eve <85962933+obvEve@users.noreply.github.com> Date: Sat, 14 Nov 2026 21:08:57 +0100 Subject: [PATCH 5/6] Fix harmony link --- .github/README.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/.github/README.md b/.github/README.md index 1f11f0e..b95d4ad 100644 --- a/.github/README.md +++ b/.github/README.md @@ -3,7 +3,7 @@ # Dependencies - SecretAPI relies on Harmony to implement features - - You can find the latest version [here](https://github.com/pardeike/Harmony/releases/latest) + - You can find the current version used [here](https://github.com/pardeike/Harmony/releases/tag/v2.4.3) - You should download Harmony-Fat and put the **0harmony.dll** (found in /net48) into your dependencies folder! # Features From 6fa60d551e43eff2adad2e607d311ccd2bcf66b0 Mon Sep 17 00:00:00 2001 From: Eve <85962933+obvEve@users.noreply.github.com> Date: Sat, 14 Nov 2026 21:09:16 +0100 Subject: [PATCH 6/6] Adjust naming --- SecretAPI/Features/DecalHelpers.cs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/SecretAPI/Features/DecalHelpers.cs b/SecretAPI/Features/DecalHelpers.cs index acd9fac..671aa75 100644 --- a/SecretAPI/Features/DecalHelpers.cs +++ b/SecretAPI/Features/DecalHelpers.cs @@ -101,7 +101,7 @@ public static void SpawnDecalFromDirection(Vector3 position, Quaternion directio /// The player to clean up decals for. /// The to cleanup. /// The amount of that decal to cleanup. - public static void CleanUpDecals(this Player player, DecalPoolType decalType, int amount = int.MaxValue) => new DecalCleanupMessage(decalType, amount).SendToHubsConditionally(hub => hub == player.ReferenceHub); + public static void CleanupDecals(this Player player, DecalPoolType decalType, int amount = int.MaxValue) => new DecalCleanupMessage(decalType, amount).SendToHubsConditionally(hub => hub == player.ReferenceHub); private static AutoSyncData GetAutoSyncData() {