Skip to content
Open

3.0 #82

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
1 change: 0 additions & 1 deletion SecretAPI.Examples/ExampleEntry.cs
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@
using HarmonyLib;
using LabApi.Loader.Features.Plugins;
using SecretAPI.Examples.Settings;
using SecretAPI.Extensions;
using SecretAPI.Features.UserSettings;

/// <summary>
Expand Down
2 changes: 1 addition & 1 deletion SecretAPI.Examples/Patches/ExamplePatch.cs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
namespace SecretAPI.Examples.Patches
{
using SecretAPI.Attribute;
using SecretAPI.Attributes;

/// <summary>
/// An example harmony patch.
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
namespace SecretAPI.Attribute
namespace SecretAPI.Attributes
{
using System;
using System.Collections.Generic;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
namespace SecretAPI.Attribute
namespace SecretAPI.Attributes
{
using System;
using System.Reflection;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
namespace SecretAPI.Attribute
namespace SecretAPI.Attributes
{
using System;
using SecretAPI.Extensions;
Expand Down
8 changes: 4 additions & 4 deletions SecretAPI/Enums/DoorPermissionCheck.cs
Original file line number Diff line number Diff line change
Expand Up @@ -21,22 +21,22 @@ public enum DoorPermissionCheck
/// <summary>
/// Used to consider <see cref="AdminFlags.BypassMode"/>.
/// </summary>
Bypass = 1,
Bypass = 1 << 0,

/// <summary>
/// Used to consider the player's <see cref="PlayerRoleBase"/>.
/// </summary>
Role = 2,
Role = 1 << 1,

/// <summary>
/// Used to consider the player's <see cref="Player.CurrentItem"/>.
/// </summary>
CurrentItem = 4,
CurrentItem = 1 << 2,

/// <summary>
/// Used to consider the player's inventory, not including the item they are holding.
/// </summary>
InventoryExcludingCurrent = 8,
InventoryExcludingCurrent = 1 << 3,

/// <summary>
/// Used to consider the player's ENTIRE inventory.
Expand Down
51 changes: 26 additions & 25 deletions SecretAPI/Extensions/CollectionExtensions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -11,37 +11,38 @@
/// </summary>
public static class CollectionExtensions
{
/// <summary>
/// Gets a random value from the collection.
/// </summary>
/// <param name="collection">The collection to pull from.</param>
/// <typeparam name="T">The Type contained by the collection.</typeparam>
/// <returns>A random value, default value when empty collection.</returns>
/// <exception cref="ArgumentOutOfRangeException">Will occur if the collection is empty.</exception>
public static T GetRandomValue<T>(this IEnumerable<T> collection)
extension<T>(IEnumerable<T> collection)
{
TryGetRandomValue(collection, out T? value);
return value!;
}

/// <summary>
/// Tries to get a random value from <see cref="IEnumerable{T}"/>.
/// </summary>
/// <param name="collection">The <see cref="IEnumerable{T}"/> to try and get a random value from.</param>
/// <param name="value">The value that was found. Default if none could be found.</param>
/// <typeparam name="T">The type contained within the <see cref="IEnumerable{T}"/>.</typeparam>
/// <returns>Whether a non-null value was found.</returns>
public static bool TryGetRandomValue<T>(this IEnumerable<T> collection, [NotNullWhen(true)] out T? value)
{
IList<T> list = collection as IList<T> ?? collection.ToList();
if (list.Count == 0)
/// <summary>
/// Gets a random value from the collection.
/// </summary>
/// <returns>A random value, default value when empty collection.</returns>
/// <exception cref="ArgumentOutOfRangeException">Will occur if the collection is empty.</exception>
public T GetRandomValue()
{
value = default;
return false;
TryGetRandomValue(collection, out T? value);
return value!;
}

value = list[Random.Range(0, list.Count)];
return value != null;
/// <summary>
/// Tries to get a random value from <see cref="IEnumerable{T}"/>.
/// </summary>
/// <param name="value">The value that was found. Default if none could be found.</param>
/// <returns>Whether a non-null value was found.</returns>
public bool TryGetRandomValue([NotNullWhen(true)] out T? value)
{
IList<T> list = collection as IList<T> ?? collection.ToList();
if (list.Count == 0)
{
value = default;
return false;
}

value = list[Random.Range(0, list.Count)];
return value != null;
}
}
}
}
87 changes: 44 additions & 43 deletions SecretAPI/Extensions/HarmonyExtensions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -6,62 +6,63 @@
using System.Reflection;
using HarmonyLib;
using LabApi.Features.Console;
using SecretAPI.Attribute;
using SecretAPI.Attributes;

/// <summary>
/// Handles patching.
/// </summary>
public static class HarmonyExtensions
{
/// <summary>
/// Patches all methods with the proper <see cref="HarmonyPatchCategory"/>.
/// </summary>
/// <param name="harmony">The harmony to use for the patch.</param>
/// <param name="category">The category to patch.</param>
/// <param name="assembly">The assembly to find patches in.</param>
public static void PatchCategory(this Harmony harmony, string category, Assembly? assembly = null)
extension(Harmony harmony)
{
assembly ??= Assembly.GetCallingAssembly();

assembly.GetTypes().Where(type =>
{
IEnumerable<HarmonyPatchCategory> categories = type.GetCustomAttributes<HarmonyPatchCategory>();
return categories.Any(c => c.Category == category);
})
.Do(type => SafePatch(harmony, type));
}

/// <summary>
/// Patches all patches that don't have a <see cref="HarmonyPatchCategory"/>.
/// </summary>
/// <param name="harmony">The harmony to use for the patch.</param>
/// <param name="assembly">The assembly to look for patches.</param>
public static void PatchAllNoCategory(this Harmony harmony, Assembly? assembly = null)
{
assembly ??= Assembly.GetCallingAssembly();
/// <summary>
/// Patches all methods with the proper <see cref="HarmonyPatchCategory"/>.
/// </summary>
/// <param name="category">The category to patch.</param>
/// <param name="assembly">The assembly to find patches in.</param>
public void PatchCategory(string category, Assembly? assembly = null)
{
assembly ??= Assembly.GetCallingAssembly();

assembly.GetTypes().Where(type =>
{
IEnumerable<HarmonyPatchCategory> categories = type.GetCustomAttributes<HarmonyPatchCategory>();
return !categories.Any();
})
.Do(type => SafePatch(harmony, type));
}
assembly.GetTypes().Where(type =>
{
IEnumerable<HarmonyPatchCategory> categories = type.GetCustomAttributes<HarmonyPatchCategory>();
return categories.Any(c => c.Category == category);
})
.Do(type => SafePatch(harmony, type));
}

/// <summary>
/// Attempts to safely patch a <see cref="Type"/>, logging any errors.
/// </summary>
/// <param name="harmony">The harmony to use for the patch.</param>
/// <param name="type">The <see cref="Type"/> to attempt to patch.</param>
public static void SafePatch(this Harmony harmony, Type type)
{
try
/// <summary>
/// Patches all patches that don't have a <see cref="HarmonyPatchCategory"/>.
/// </summary>
/// <param name="assembly">The assembly to look for patches.</param>
public void PatchAllNoCategory(Assembly? assembly = null)
{
harmony.CreateClassProcessor(type).Patch();
assembly ??= Assembly.GetCallingAssembly();

assembly.GetTypes().Where(type =>
{
IEnumerable<HarmonyPatchCategory> categories = type.GetCustomAttributes<HarmonyPatchCategory>();
return !categories.Any();
})
.Do(type => SafePatch(harmony, type));
}
catch (Exception ex)

/// <summary>
/// Attempts to safely patch a <see cref="Type"/>, logging any errors.
/// </summary>
/// <param name="type">The <see cref="Type"/> to attempt to patch.</param>
public void SafePatch(Type type)
{
Logger.Error($"[HarmonyExtensions] failed to safely patch {harmony.Id} ({type.FullName}): {ex}");
try
{
harmony.CreateClassProcessor(type).Patch();
}
catch (Exception ex)
{
Logger.Error($"[HarmonyExtensions] failed to safely patch {harmony.Id} ({type.FullName}): {ex}");
}
}
}
}
Expand Down
20 changes: 0 additions & 20 deletions SecretAPI/Extensions/MirrorExtensions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -11,26 +11,6 @@
/// </summary>
public static class MirrorExtensions
{
/// <summary>
/// Sends a fake cassie message to a player.
/// </summary>
/// <param name="target">The target to send the cassie message to.</param>
/// <param name="message">The message to send.</param>
/// <param name="isHeld">Whether the cassie is held.</param>
/// <param name="isNoisy">Whether the cassie is noisy.</param>
/// <param name="isSubtitles">Whether there is subtitles on the cassie.</param>
/// <param name="customSubtitles">The custom subtitles to use for the cassie.</param>
[Obsolete("Due to NW changes to Cassie, this is no longer functional.")]
public static void SendFakeCassieMessage(
this Player target,
string message,
bool isHeld = false,
bool isNoisy = true,
bool isSubtitles = true,
string customSubtitles = "")
{
}

/// <summary>
/// Send a fake rpc message to a player.
/// </summary>
Expand Down
Loading