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>
92 lines
3.7 KiB
C#
92 lines
3.7 KiB
C#
using System.Linq;
|
|
using System.Reflection;
|
|
using System.Threading.Tasks;
|
|
using EFT;
|
|
using Aki.Reflection.Patching;
|
|
using Aki.Reflection.Utils;
|
|
using Aki.SinglePlayer.Models.RaidFix;
|
|
using System;
|
|
using System.Collections.Generic;
|
|
using HarmonyLib;
|
|
|
|
namespace Aki.SinglePlayer.Patches.RaidFix
|
|
{
|
|
public class GetNewBotTemplatesPatch : ModulePatch
|
|
{
|
|
static GetNewBotTemplatesPatch()
|
|
{
|
|
_ = nameof(IGetProfileData.PrepareToLoadBackend);
|
|
_ = nameof(BotsPresets.GetNewProfile);
|
|
_ = nameof(PoolManager.LoadBundlesAndCreatePools);
|
|
_ = nameof(JobPriority.General);
|
|
}
|
|
|
|
/// <summary>
|
|
/// Looking for CreateProfile()
|
|
/// </summary>
|
|
/// <returns></returns>
|
|
protected override MethodBase GetTargetMethod()
|
|
{
|
|
return AccessTools.DeclaredMethod(typeof(BotsPresets), nameof(BotsPresets.CreateProfile));
|
|
}
|
|
|
|
// Unused, but left here in case patch breaks and finding the intended method is difficult
|
|
private bool IsTargetMethod(MethodInfo mi)
|
|
{
|
|
var parameters = mi.GetParameters();
|
|
return (parameters.Length == 3
|
|
&& parameters[0].Name == "data"
|
|
&& parameters[1].Name == "cancellationToken"
|
|
&& parameters[2].Name == "withDelete");
|
|
}
|
|
|
|
/// <summary>
|
|
/// BotsPresets.GetNewProfile()
|
|
/// </summary>
|
|
[PatchPrefix]
|
|
private static bool PatchPrefix(ref Task<Profile> __result, BotsPresets __instance, List<Profile> ___list_0, GClass588 data, ref bool withDelete)
|
|
{
|
|
/*
|
|
When client wants new bot and GetNewProfile() return null (if not more available templates or they don't satisfy by Role and Difficulty condition)
|
|
then client gets new piece of WaveInfo collection (with Limit = 30 by default) and make request to server
|
|
but use only first value in response (this creates a lot of garbage and cause freezes)
|
|
after patch we request only 1 template from server along with other patches this one causes to call data.PrepareToLoadBackend(1) gets the result with required role and difficulty:
|
|
new[] { new WaveInfo() { Limit = 1, Role = role, Difficulty = difficulty } }
|
|
then perform request to server and get only first value of resulting single element collection
|
|
*/
|
|
|
|
try
|
|
{
|
|
// Force true to ensure bot profile is deleted after use
|
|
__instance.GetNewProfile(data, true);
|
|
}
|
|
catch (Exception e)
|
|
{
|
|
Logger.LogDebug($"GetNewBotTemplatesPatch() getNewProfile() failed: {e.Message} {e.InnerException}");
|
|
throw;
|
|
}
|
|
|
|
// Load from server
|
|
var source = data.PrepareToLoadBackend(1).Take(1).ToList();
|
|
|
|
var taskScheduler = TaskScheduler.FromCurrentSynchronizationContext();
|
|
var taskAwaiter = (Task<Profile>)null;
|
|
taskAwaiter = PatchConstants.BackEndSession.LoadBots(source).ContinueWith(GetFirstResult, taskScheduler);
|
|
|
|
// Load bundles for bot profile
|
|
var continuation = new BundleLoader(taskScheduler);
|
|
__result = taskAwaiter.ContinueWith(continuation.LoadBundles, taskScheduler).Unwrap();
|
|
|
|
return false;
|
|
}
|
|
|
|
private static Profile GetFirstResult(Task<Profile[]> task)
|
|
{
|
|
var result = task.Result[0];
|
|
Logger.LogInfo($"{DateTime.Now:T} Loading bot: {result.Info.Nickname} profile from server. Role: {result.Info.Settings.Role} Side: {result.Side}");
|
|
|
|
return result;
|
|
}
|
|
}
|
|
}
|