Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion .github/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
4 changes: 4 additions & 0 deletions Directory.Build.props
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,10 @@
<Nullable>enable</Nullable>
<Version>3.5.0-beta.3</Version> <!-- BetaX = Incorrect (3.5.0-beta3) | Beta.X = Correct (3.5.0-beta.3) -->
</PropertyGroup>

<PropertyGroup Label="Versioning">
<HarmonyVersion>2.4.3</HarmonyVersion>
</PropertyGroup>

<PropertyGroup Label="Nuget">
<Authors>obvEve</Authors>
Expand Down
9 changes: 1 addition & 8 deletions SecretAPI.Examples/ExampleEntry.cs
Original file line number Diff line number Diff line change
@@ -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;
Expand Down
2 changes: 1 addition & 1 deletion SecretAPI.Examples/SecretAPI.Examples.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@

<ItemGroup>
<PackageReference Include="BepInEx.AssemblyPublicizer.MSBuild" Version="0.4.2" PrivateAssets="all" />
<PackageReference Include="Lib.Harmony" Version="2.4.2" />
<PackageReference Include="Lib.Harmony" Version="$(HarmonyVersion)"/>
<PackageReference Include="StyleCop.Analyzers" Version="1.2.0-beta.556" IncludeAssets="All" PrivateAssets="All" />
</ItemGroup>

Expand Down
27 changes: 0 additions & 27 deletions SecretAPI/Attributes/HarmonyPatchCategory.cs

This file was deleted.

11 changes: 11 additions & 0 deletions SecretAPI/Config.cs
Original file line number Diff line number Diff line change
@@ -1,5 +1,9 @@
namespace SecretAPI;

using System.ComponentModel;
using SecretAPI.Features.UserSettings;
using UserSettings.ServerSpecific;

/// <summary>
/// Handles config for SecretAPI.
/// </summary>
Expand All @@ -8,5 +12,12 @@ public sealed class Config
/// <summary>
/// Gets or sets a value indicating whether PatchClassProcessor.ReportException should be edited to log instead of throwing exception.
/// </summary>
[Description("When set to true, will patch PatchClassProcessor.ReportException to log instead of throw.")]
public bool LogHarmonyReplaceThrow { get; set; } = false;

/// <summary>
/// Gets or sets a value indicating whether <see cref="CustomSetting"/> should validate <see cref="ServerSpecificSettingsSync.DefinedSettings"/>.
/// </summary>
[Description("When set to true and CustomSetting is used, will begin validating ServerSpecificSettingsSync.DefinedSettings.")]
public bool ValidateDefinedSettings { get; set; } = true;
}
14 changes: 14 additions & 0 deletions SecretAPI/Debugging/PatchDebugger.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,9 @@

namespace SecretAPI.Debugging;

using System.Collections.Generic;
using System.IO;
using System.Reflection;
using SecretAPI.Attributes;

/// <summary>
Expand All @@ -16,6 +19,17 @@ internal static class PatchDebugger
internal static void Load()
{
SecretApi.Harmony.PatchAll(SecretApi.Assembly);
IEnumerable<MethodBase> patchedMethods = SecretApi.Harmony.GetPatchedMethods();
int patchCount = 0;
List<string> 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);
}
}

Expand Down
9 changes: 0 additions & 9 deletions SecretAPI/Extensions/CodeMatcherExtensions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -19,15 +19,6 @@ public static class CodeMatcherExtensions
/// <returns>The current <see cref="CodeMatcher"/>.</returns>
public CodeMatcher EndAndBacktrack(int backtrackCount) => matcher.End().Advance(-backtrackCount);

/// <summary>
/// Declares a local to be used of a certain <see cref="Type"/>.
/// </summary>
/// <param name="localType">The <see cref="Type"/> of the local to declare.</param>
/// <param name="localBuilder">The <see cref="LocalBuilder"/> declared.</param>
/// <returns>The current <see cref="CodeMatcher"/>.</returns>
[Obsolete("Use CodeMatcher::DeclareLocal(Type, out LocalBuilder)")]
public CodeMatcher DeclareLocal(Type localType, out LocalBuilder localBuilder) => matcher.DeclareLocal(localType, out localBuilder);

/// <summary>
/// Declares a local to be used of a certain <see cref="Type"/>.
/// </summary>
Expand Down
73 changes: 0 additions & 73 deletions SecretAPI/Extensions/HarmonyExtensions.cs

This file was deleted.

2 changes: 1 addition & 1 deletion SecretAPI/Features/DecalHelpers.cs
Original file line number Diff line number Diff line change
Expand Up @@ -101,7 +101,7 @@ public static void SpawnDecalFromDirection(Vector3 position, Quaternion directio
/// <param name="player">The player to clean up decals for.</param>
/// <param name="decalType">The <see cref="DecalPoolType"/> to cleanup.</param>
/// <param name="amount">The amount of that decal to cleanup.</param>
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()
{
Expand Down
16 changes: 7 additions & 9 deletions SecretAPI/Features/Effects/CustomPlayerEffect.cs
Original file line number Diff line number Diff line change
Expand Up @@ -18,11 +18,12 @@
/// </summary>
public abstract class CustomPlayerEffect : StatusEffectBase
{
private static HashSet<Type> registeredEffects = new();

/// <summary>
/// Gets a list of types to register (Must inherit <see cref="StatusEffectBase"/>).
/// Gets a readonly collection of all the registered effects.
/// </summary>
[Obsolete("This is becoming readonly in 4.0 - Use Register<T>()")]
public static List<Type> EffectsToRegister { get; } = []; // TODO 4.0: private static HashSet<Type> RegisteredEffects
public static IReadOnlyCollection<Type> RegisteredEffects => registeredEffects;

/// <summary>
/// Gets the <see cref="Player"/> with this effect.
Expand Down Expand Up @@ -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);
}

/// <inheritdoc/>
public override string ToString() => $"{GetType().Name}: Owner ({Owner}) - Intensity ({Intensity}) - Duration {Duration}";

/// <summary>
/// Initializes the <see cref="CustomPlayerEffect"/> to implement <see cref="EffectsToRegister"/>.
/// Initializes the <see cref="CustomPlayerEffect"/> to implement <see cref="RegisteredEffects"/>.
/// </summary>
/// <remarks>The effects must be loaded on before <see cref="Server.Host"/> is created.</remarks>
[CallOnLoad]
Expand All @@ -89,7 +87,7 @@ private static void OnSceneLoaded(Scene scene, LoadSceneMode mode)
SceneManager.sceneLoaded -= OnSceneLoaded;

Transform playerEffects = PrefabStore<ReferenceHub>.Prefab.playerEffectsController.effectsGameObject.transform;
foreach (Type type in EffectsToRegister)
foreach (Type type in RegisteredEffects)
{
if (!typeof(StatusEffectBase).IsAssignableFrom(type))
{
Expand Down
23 changes: 0 additions & 23 deletions SecretAPI/Features/Registry.cs

This file was deleted.

3 changes: 3 additions & 0 deletions SecretAPI/Patches/Features/HarmonyLogger.cs
Original file line number Diff line number Diff line change
Expand Up @@ -9,9 +9,12 @@
/// <summary>
/// Changes <see cref="PatchClassProcessor"/>'s ReportException to log instead of throwing the exception.
/// </summary>
[HarmonyPatchCategory(nameof(SecretAPI))]
[HarmonyPatch(typeof(PatchClassProcessor), "ReportException")]
internal static class HarmonyLogger
{
private static bool Prepare() => SecretApi.Config.LogHarmonyReplaceThrow || SecretApi.ForceHarmonyConditionTrue;

private static IEnumerable<CodeInstruction> Transpiler(IEnumerable<CodeInstruction> instructions)
{
CodeMatcher matcher = new(instructions);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@
using System.Collections.Generic;
using HarmonyLib;
using NorthwoodLib.Pools;
using SecretAPI.Attributes;
using SecretAPI.Features.UserSettings;
using UserSettings.ServerSpecific;

Expand All @@ -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<ServerSpecificSettingBase> settings = ListPool<ServerSpecificSettingBase>.Shared.Rent();
Expand Down
2 changes: 1 addition & 1 deletion SecretAPI/SecretAPI.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@

<ItemGroup>
<PackageReference Include="BepInEx.AssemblyPublicizer.MSBuild" Version="0.4.2" PrivateAssets="all" />
<PackageReference Include="Lib.Harmony" Version="2.4.2"/> <!-- Do not publicize Harmony as it will publicize internal monomod references -->
<PackageReference Include="Lib.Harmony" Version="$(HarmonyVersion)"/> <!-- Do not publicize Harmony as it will publicize internal monomod references -->
<PackageReference Include="StyleCop.Analyzers" Version="1.2.0-beta.556" IncludeAssets="All" PrivateAssets="All" />
</ItemGroup>

Expand Down
30 changes: 20 additions & 10 deletions SecretAPI/SecretApi.cs
Original file line number Diff line number Diff line change
@@ -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;
Expand All @@ -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;

/// <summary>
/// Main class handling loading API.
/// </summary>
public class SecretApi : Plugin<Config>
{
/// <summary>
/// Whether conditional harmony patches should be forced. This is to simply things such as <see cref="ReportDefinedSettingPatch"/> and <see cref="HarmonyLogger"/>.
/// </summary>
internal const bool ForceHarmonyConditionTrue =
#if DEBUG
true;
#else
false;
#endif

/// <inheritdoc/>
public override string Name => "SecretAPI";

Expand Down Expand Up @@ -54,6 +58,12 @@ public class SecretApi : Plugin<Config>
/// </summary>
internal static Assembly Assembly { get; } = typeof(SecretApi).Assembly;

/// <summary>
/// Gets the current <see cref="Config"/> loaded.
/// </summary>
/// <remarks>This is null before <see cref="Enable"/> is called.</remarks>
internal static new Config Config { get; private set; } = null!;

/// <summary>
/// Gets the config directory within "SCP Secret Laboratory/LabAPI/configs/SecretAPI" for internal use.
/// </summary>
Expand All @@ -62,8 +72,8 @@ public class SecretApi : Plugin<Config>
/// <inheritdoc/>
public override void Enable()
{
if (Config.LogHarmonyReplaceThrow)
Harmony.CreateClassProcessor(typeof(HarmonyLogger)).Patch();
Config = base.Config;
Harmony.PatchCategory(nameof(SecretAPI));

SecretApiGenerated.OnLoad();
}
Expand Down
Loading