diff --git a/project/SPT.Custom/Patches/MemoryCollectionPatch.cs b/project/SPT.Custom/Patches/MemoryCollectionPatch.cs new file mode 100644 index 0000000..1260986 --- /dev/null +++ b/project/SPT.Custom/Patches/MemoryCollectionPatch.cs @@ -0,0 +1,29 @@ +using System.Reflection; +using Comfort.Common; +using EFT; +using EFT.UI; +using EFT.UI.Screens; +using HarmonyLib; +using SPT.Reflection.Patching; +using UnityEngine.Scripting; + +namespace SPT.Custom.Patches; + +public class MemoryCollectionPatch : ModulePatch +{ + protected override MethodBase GetTargetMethod() + { + return AccessTools.Method(typeof(MenuTaskBar), nameof(MenuTaskBar.OnScreenChanged)); + } + + [PatchPostfix] + public static void PatchPostfix(EEftScreenType eftScreenType) + { + if (eftScreenType != EEftScreenType.Inventory || !Singleton.Instantiated) return; + + GarbageCollector.GCMode = GarbageCollector.Mode.Enabled; + + // 25000000 Nanoseconds is 25 Milliseconds. + GarbageCollector.CollectIncremental(25000000L); + } +} \ No newline at end of file diff --git a/project/SPT.Custom/SPTCustomPlugin.cs b/project/SPT.Custom/SPTCustomPlugin.cs index 984b787..3a19ade 100644 --- a/project/SPT.Custom/SPTCustomPlugin.cs +++ b/project/SPT.Custom/SPTCustomPlugin.cs @@ -12,12 +12,9 @@ namespace SPT.Custom [BepInPlugin("com.SPT.custom", "SPT.Custom", SPTPluginInfo.PLUGIN_VERSION)] public class SPTCustomPlugin : BaseUnityPlugin { - internal static ManualLogSource Log; - public void Awake() { Logger.LogInfo("Loading: SPT.Custom"); - Log = Logger; try { @@ -46,9 +43,9 @@ namespace SPT.Custom new FixBossesHavingNoFollowersOnMediumAiAmount().Enable(); new FixAirdropCrashPatch().Enable(); //new AllowAirdropsInPvEPatch().Enable(); + new MemoryCollectionPatch().Enable(); HookObject.AddOrGetComponent(); - HookObject.AddOrGetComponent(); } catch (Exception ex) { diff --git a/project/SPT.Custom/Utils/MemoryManager.cs b/project/SPT.Custom/Utils/MemoryManager.cs deleted file mode 100644 index 092ffc9..0000000 --- a/project/SPT.Custom/Utils/MemoryManager.cs +++ /dev/null @@ -1,36 +0,0 @@ -using System; -using System.Collections; -using System.Threading.Tasks; -using UnityEngine; -using UnityEngine.Scripting; - -namespace SPT.Custom.Utils; - -public class MemoryManager : MonoBehaviour -{ - private WaitForSecondsRealtime _gcCollectionTime = new(30f); - - public void Awake() - { - StartCoroutine(MemoryManagerCoroutine()); - } - - private IEnumerator MemoryManagerCoroutine() - { - yield return _gcCollectionTime; - - // SPTCustomPlugin.Log.LogDebug($"Allocated Mananged Memory {GC.GetTotalMemory(false) / 1024f / 1024f} MB"); - - Task.Run(CollectMemory); - - StartCoroutine(MemoryManagerCoroutine()); - } - - private Task CollectMemory() - { - GarbageCollector.GCMode = GarbageCollector.Mode.Enabled; - GC.Collect(2, GCCollectionMode.Optimized, false, true); - - return Task.CompletedTask; - } -} \ No newline at end of file