mirror of
https://github.com/sp-tarkov/modules.git
synced 2025-02-13 09:50:43 -05:00
Depends on SPT-AKI/SPT-AssemblyTool#3 * Refactored Modules for better consistency and general readability, along with preparing the code for a publicized assembly * Added `PublicDeclaredFlags` to `PatchConstants` to cover a set of commonly used flags to get methods post-publicizing * Added a replacement to LINQ's `.Single()` - `.SingleCustom()` which has improved logging to help with debugging Module code. Replaced all `.Single()` usages where applicable * Replaced most method info fetching with `AccessTools` for consistency and better readability, especially in places where methods were being retrieved by their name anyways **NOTE:** As a side effect of publicizing all properties, some property access code such as `Player.Position` will now show "ambiguous reference" errors during compile, due to there being multiple interfaces with the Property name being defined on the class. The way to get around this is to use a cast to an explicit interface Example: ```cs Singleton<GameWorld>.Instance.MainPlayer.Position ``` will now need to be ```cs ((IPlayer)Singleton<GameWorld>.Instance.MainPlayer).Position ``` Co-authored-by: Terkoiz <terkoiz@spt.dev> Reviewed-on: SPT-AKI/Modules#58 Co-authored-by: Terkoiz <terkoiz@noreply.dev.sp-tarkov.com> Co-committed-by: Terkoiz <terkoiz@noreply.dev.sp-tarkov.com>
86 lines
3.5 KiB
C#
86 lines
3.5 KiB
C#
using System;
|
|
using System.Collections.Generic;
|
|
using System.Linq;
|
|
using System.Reflection;
|
|
using Comfort.Common;
|
|
using EFT;
|
|
using FilesChecker;
|
|
|
|
namespace Aki.Reflection.Utils
|
|
{
|
|
public static class PatchConstants
|
|
{
|
|
public static BindingFlags PrivateFlags { get; private set; }
|
|
public static BindingFlags PublicFlags { get; private set; }
|
|
public static BindingFlags PublicDeclaredFlags { get; private set; }
|
|
public static Type[] EftTypes { get; private set; }
|
|
public static Type[] FilesCheckerTypes { get; private set; }
|
|
public static Type LocalGameType { get; private set; }
|
|
public static Type ExfilPointManagerType { get; private set; }
|
|
public static Type SessionInterfaceType { get; private set; }
|
|
public static Type BackendSessionInterfaceType { get; private set; }
|
|
|
|
private static ISession _backEndSession;
|
|
public static ISession BackEndSession
|
|
{
|
|
get
|
|
{
|
|
if (_backEndSession == null)
|
|
{
|
|
_backEndSession = Singleton<ClientApplication<ISession>>.Instance.GetClientBackEndSession();
|
|
}
|
|
|
|
return _backEndSession;
|
|
}
|
|
}
|
|
|
|
static PatchConstants()
|
|
{
|
|
_ = nameof(ISession.GetPhpSessionId);
|
|
|
|
PrivateFlags = BindingFlags.NonPublic | BindingFlags.Instance | BindingFlags.DeclaredOnly;
|
|
PublicFlags = BindingFlags.Public | BindingFlags.Instance;
|
|
PublicDeclaredFlags = BindingFlags.Public | BindingFlags.Instance | BindingFlags.DeclaredOnly;
|
|
EftTypes = typeof(AbstractGame).Assembly.GetTypes();
|
|
FilesCheckerTypes = typeof(ICheckResult).Assembly.GetTypes();
|
|
LocalGameType = EftTypes.SingleCustom(x => x.Name == "LocalGame");
|
|
ExfilPointManagerType = EftTypes.SingleCustom(x => x.GetMethod("InitAllExfiltrationPoints") != null);
|
|
SessionInterfaceType = EftTypes.SingleCustom(x => x.GetMethods().Select(y => y.Name).Contains("GetPhpSessionId") && x.IsInterface);
|
|
BackendSessionInterfaceType = EftTypes.SingleCustom(x => x.GetMethods().Select(y => y.Name).Contains("ChangeProfileStatus") && x.IsInterface);
|
|
}
|
|
|
|
/// <summary>
|
|
/// A custom LINQ .Single() implementation with improved logging for easier patch debugging
|
|
/// </summary>
|
|
/// <returns>A single member of the input sequence that matches the given search pattern</returns>
|
|
/// <exception cref="ArgumentNullException"></exception>
|
|
/// <exception cref="InvalidOperationException"></exception>
|
|
public static T SingleCustom<T>(this IEnumerable<T> types, Func<T, bool> predicate) where T : MemberInfo
|
|
{
|
|
if (types == null)
|
|
{
|
|
throw new ArgumentNullException(nameof(types));
|
|
}
|
|
|
|
if (predicate == null)
|
|
{
|
|
throw new ArgumentNullException(nameof(predicate));
|
|
}
|
|
|
|
var matchingTypes = types.Where(predicate).ToArray();
|
|
|
|
if (matchingTypes.Length > 1)
|
|
{
|
|
throw new InvalidOperationException($"More than one member matches the specified search pattern: {string.Join(", ", matchingTypes.Select(t => t.Name))}");
|
|
}
|
|
|
|
if (matchingTypes.Length == 0)
|
|
{
|
|
throw new InvalidOperationException("No members match the specified search pattern");
|
|
}
|
|
|
|
return matchingTypes[0];
|
|
}
|
|
}
|
|
}
|