Skip to content
Merged
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 SecretAPI.Examples/SecretAPI.Examples.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
<ItemGroup>
<PackageReference Include="BepInEx.AssemblyPublicizer.MSBuild" Version="0.4.2" PrivateAssets="all" />
<PackageReference Include="Lib.Harmony" Version="2.2.2" />
<PackageReference Include="Northwood.LabAPI" Version="1.1.3" IncludeAssets="All" PrivateAssets="All" />
<PackageReference Include="Northwood.LabAPI" Version="1.1.4" IncludeAssets="All" PrivateAssets="All" />
<PackageReference Include="StyleCop.Analyzers" Version="1.2.0-beta.556" IncludeAssets="All" PrivateAssets="All" />
</ItemGroup>

Expand Down
31 changes: 21 additions & 10 deletions SecretAPI/Attribute/CallOnLoadAttribute.cs
Original file line number Diff line number Diff line change
Expand Up @@ -4,12 +4,13 @@
using System.Collections.Generic;
using System.Linq;
using System.Reflection;
using SecretAPI.Features;

/// <summary>
/// Defines the attribute for methods to call on load.
/// </summary>
[AttributeUsage(AttributeTargets.Method)]
public class CallOnLoadAttribute : Attribute
public class CallOnLoadAttribute : Attribute, IPriority
{
/// <summary>
/// Initializes a new instance of the <see cref="CallOnLoadAttribute"/> class.
Expand All @@ -32,32 +33,42 @@ public CallOnLoadAttribute(int priority = 0)
public static void Load(Assembly? assembly = null)
{
assembly ??= Assembly.GetCallingAssembly();
CallAttributeMethodPriority<CallOnLoadAttribute>(assembly);
}

/// <inheritdoc/>
public override bool Equals(object? obj) => obj == this;

/// <inheritdoc/>
public override int GetHashCode() => base.GetHashCode();

/// <summary>
/// Calls the method associated with a Attribute implementing <see cref="IPriority"/>.
/// </summary>
/// <param name="assembly">The assembly to handle calls from.</param>
/// <typeparam name="TAttribute">The attribute required for this to be called.</typeparam>
internal static void CallAttributeMethodPriority<TAttribute>(Assembly assembly)
where TAttribute : Attribute, IPriority
{
const BindingFlags methodFlags = BindingFlags.Static | BindingFlags.Public | BindingFlags.NonPublic;
Dictionary<CallOnLoadAttribute, MethodInfo> methods = new();
Dictionary<TAttribute, MethodInfo> methods = new();

// get all types
foreach (Type type in assembly.GetTypes())
{
// get all static methods
foreach (MethodInfo method in type.GetMethods(methodFlags))
{
CallOnLoadAttribute? attribute = method.GetCustomAttribute<CallOnLoadAttribute>();
TAttribute? attribute = method.GetCustomAttribute<TAttribute>();
if (attribute == null)
continue;

methods.Add(attribute, method);
}
}

foreach (KeyValuePair<CallOnLoadAttribute, MethodInfo> method in methods.OrderBy(static v => v.Key.Priority))
foreach (KeyValuePair<TAttribute, MethodInfo> method in methods.OrderBy(static v => v.Key.Priority))
method.Value.Invoke(null, null);
}

/// <inheritdoc/>
public override bool Equals(object? obj) => obj == this;

/// <inheritdoc/>
public override int GetHashCode() => base.GetHashCode();
}
}
26 changes: 3 additions & 23 deletions SecretAPI/Attribute/CallOnUnloadAttribute.cs
Original file line number Diff line number Diff line change
@@ -1,15 +1,14 @@
namespace SecretAPI.Attribute
{
using System;
using System.Collections.Generic;
using System.Linq;
using System.Reflection;
using SecretAPI.Features;

/// <summary>
/// Defines the attribute for methods to call on unload.
/// </summary>
[AttributeUsage(AttributeTargets.Method)]
public class CallOnUnloadAttribute : Attribute
public class CallOnUnloadAttribute : Attribute, IPriority
{
/// <summary>
/// Initializes a new instance of the <see cref="CallOnUnloadAttribute"/> class.
Expand All @@ -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<CallOnUnloadAttribute, MethodInfo> 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<CallOnUnloadAttribute>();
if (attribute == null)
continue;

methods.Add(attribute, method);
}
}

foreach (KeyValuePair<CallOnUnloadAttribute, MethodInfo> method in methods.OrderBy(static v => v.Key.Priority))
method.Value.Invoke(null, null);
CallOnLoadAttribute.CallAttributeMethodPriority<CallOnUnloadAttribute>(assembly);
}

/// <inheritdoc/>
Expand Down
2 changes: 2 additions & 0 deletions SecretAPI/Extensions/CollectionExtensions.cs
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
namespace SecretAPI.Extensions
{
using System;
using System.Collections.Generic;
using System.Diagnostics.CodeAnalysis;
using System.Linq;
Expand All @@ -16,6 +17,7 @@ public static class CollectionExtensions
/// <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)
{
TryGetRandomValue(collection, out T? value);
Expand Down
27 changes: 12 additions & 15 deletions SecretAPI/Extensions/RoleExtensions.cs
Original file line number Diff line number Diff line change
@@ -1,15 +1,17 @@
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;

/// <summary>
/// Extensions related to <see cref="RoleTypeId"/>.
/// </summary>
[Obsolete("This no longer provides anything that basegame/LabAPI does not")]
public static class RoleExtensions
{
/// <summary>
Expand All @@ -19,16 +21,18 @@ public static class RoleExtensions
/// <param name="role">The <see cref="PlayerRoleBase"/> found.</param>
/// <typeparam name="T">The <see cref="PlayerRoleBase"/>.</typeparam>
/// <returns>The role base found, else null. </returns>
[Obsolete("Use LabApi.Features.Extensions.RoleExtensions.TryGetRoleBase")]
public static bool TryGetRoleBase<T>(this RoleTypeId roleTypeId, [NotNullWhen(true)] out T? role)
=> PlayerRoleLoader.TryGetRoleTemplate(roleTypeId, out role);
=> LabApi.Features.Extensions.RoleExtensions.TryGetRoleBase(roleTypeId, out role);

/// <summary>
/// Gets the color of a <see cref="RoleTypeId"/>.
/// </summary>
/// <param name="roleTypeId">The role to get color of.</param>
/// <returns>The color found, if not found then white.</returns>
[Obsolete("Use Respawning.Objectives.GetRoleColor")]
public static Color GetColor(this RoleTypeId roleTypeId)
=> TryGetRoleBase(roleTypeId, out PlayerRoleBase? role) ? role.RoleColor : Color.white;
=> roleTypeId.GetRoleColor();

/// <summary>
/// Tries to get a random spawn point from a <see cref="RoleTypeId"/>.
Expand All @@ -37,24 +41,17 @@ public static Color GetColor(this RoleTypeId roleTypeId)
/// <param name="position">The position found.</param>
/// <param name="horizontalRot">The rotation found.</param>
/// <returns>Whether a spawnpoint was found.</returns>
[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);

/// <summary>
/// Gets the inventory of the specified <see cref="RoleTypeId"/>.
/// </summary>
/// <param name="role">The <see cref="RoleTypeId"/>.</param>
/// <returns>The <see cref="InventoryRoleInfo"/> found.</returns>
[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);
}
}
4 changes: 3 additions & 1 deletion SecretAPI/Extensions/RoomExtensions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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
];

/// <summary>
Expand Down
13 changes: 13 additions & 0 deletions SecretAPI/Features/IPriority.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
namespace SecretAPI.Features
{
/// <summary>
/// Handles IPriority.
/// </summary>
public interface IPriority
{
/// <summary>
/// Gets the current priority.
/// </summary>
public int Priority { get; }
}
}
4 changes: 2 additions & 2 deletions SecretAPI/SecretAPI.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
<TargetFramework>net48</TargetFramework>
<LangVersion>latest</LangVersion>
<Nullable>enable</Nullable>
<Version>2.0.1</Version>
<Version>2.0.2</Version>
<AllowUnsafeBlocks>true</AllowUnsafeBlocks>
</PropertyGroup>

Expand All @@ -29,7 +29,7 @@

<ItemGroup>
<PackageReference Include="BepInEx.AssemblyPublicizer.MSBuild" Version="0.4.2" PrivateAssets="all" />
<PackageReference Include="Northwood.LabAPI" Version="1.1.3" IncludeAssets="All" PrivateAssets="All" />
<PackageReference Include="Northwood.LabAPI" Version="1.1.4" IncludeAssets="All" PrivateAssets="All" />
<PackageReference Include="Lib.Harmony" Version="2.2.2" />
<PackageReference Include="StyleCop.Analyzers" Version="1.2.0-beta.556" IncludeAssets="All" PrivateAssets="All" />
</ItemGroup>
Expand Down
8 changes: 5 additions & 3 deletions SecretAPI/SecretApi.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -28,10 +29,11 @@ public class SecretApi : Plugin
public override Version Version { get; } = Assembly.GetName().Version;

/// <inheritdoc/>
public override Version RequiredApiVersion { get; } = new(LabApi.Features.LabApiProperties.CompiledVersion);
public override Version RequiredApiVersion => LabApiProperties.CurrentVersion;

/*/// <inheritdoc />
public override bool IsTransparent => true;*/
/// <inheritdoc />
/// <remarks>We use transparent here because this is an API and should not interfere by itself with game logic.</remarks>
public override bool IsTransparent => true;

/// <summary>
/// Gets the harmony to use for the API.
Expand Down