0
0
mirror of https://github.com/sp-tarkov/modules.git synced 2025-02-13 09:50:43 -05:00
modules/project/Aki.Custom/Utils/EasyBundleHelper.cs
Terkoiz 337a0733ae Publicized assembly refactor (!58)
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>
2024-01-13 22:08:29 +00:00

137 lines
4.4 KiB
C#

using System;
using System.Collections.Generic;
using System.Linq;
using System.Reflection;
using System.Threading.Tasks;
using Aki.Reflection.Utils;
using UnityEngine;
using BindableState = BindableState<Diz.DependencyManager.ELoadState>;
namespace Aki.Custom.Utils
{
public class EasyBundleHelper
{
private const BindingFlags NonPublicInstanceFlags = BindingFlags.Instance | BindingFlags.NonPublic;
private static readonly FieldInfo _pathField;
private static readonly FieldInfo _keyWithoutExtensionField;
private static readonly FieldInfo _bundleLockField;
private static readonly PropertyInfo _dependencyKeysProperty;
private static readonly PropertyInfo _keyProperty;
private static readonly PropertyInfo _loadStateProperty;
private static readonly MethodInfo _loadingCoroutineMethod;
private readonly object _instance;
public static readonly Type Type;
static EasyBundleHelper()
{
_ = nameof(IBundleLock.IsLocked);
_ = nameof(BindableState.Bind);
Type = PatchConstants.EftTypes.SingleCustom(x => !x.IsInterface && x.GetProperty("SameNameAsset", PatchConstants.PublicDeclaredFlags) != null);
_pathField = Type.GetField("string_1", NonPublicInstanceFlags);
_keyWithoutExtensionField = Type.GetField("string_0", NonPublicInstanceFlags);
_bundleLockField = Type.GetFields(NonPublicInstanceFlags).FirstOrDefault(x => x.FieldType == typeof(IBundleLock));
_dependencyKeysProperty = Type.GetProperty("DependencyKeys");
_keyProperty = Type.GetProperty("Key");
_loadStateProperty = Type.GetProperty("LoadState");
// Function with 0 params and returns task (usually method_0())
var possibleMethods = Type.GetMethods(PatchConstants.PublicDeclaredFlags).Where(x => x.GetParameters().Length == 0 && x.ReturnType == typeof(Task)).ToArray();
if (possibleMethods.Length > 1)
{
throw new Exception($"Unable to find the Loading Coroutine method as there are multiple possible matches: {string.Join(",", possibleMethods.Select(x => x.Name))}");
}
if (possibleMethods.Length == 0)
{
throw new Exception("Unable to find the Loading Coroutine method as there are no matches");
}
_loadingCoroutineMethod = possibleMethods.Single();
}
public EasyBundleHelper(object easyBundle)
{
_instance = easyBundle;
}
public IEnumerable<string> DependencyKeys
{
get
{
return (IEnumerable<string>)_dependencyKeysProperty.GetValue(_instance);
}
set
{
_dependencyKeysProperty.SetValue(_instance, value);
}
}
public IBundleLock BundleLock
{
get
{
return (IBundleLock)_bundleLockField.GetValue(_instance);
}
set
{
_bundleLockField.SetValue(_instance, value);
}
}
public string Path
{
get
{
return (string)_pathField.GetValue(_instance);
}
set
{
_pathField.SetValue(_instance, value);
}
}
public string Key
{
get
{
return (string)_keyProperty.GetValue(_instance);
}
set
{
_keyProperty.SetValue(_instance, value);
}
}
public BindableState LoadState
{
get
{
return (BindableState)_loadStateProperty.GetValue(_instance);
}
set
{
_loadStateProperty.SetValue(_instance, value);
}
}
public string KeyWithoutExtension
{
get
{
return (string)_keyWithoutExtensionField.GetValue(_instance);
}
set
{
_keyWithoutExtensionField.SetValue(_instance, value);
}
}
public Task LoadingCoroutine(Dictionary<string, AssetBundle> bundles)
{
return (Task)_loadingCoroutineMethod.Invoke(_instance, new object[] { bundles });
}
}
}