using System;
using System.Collections;
using System.Collections.Generic;
using System.Diagnostics;
using System.Linq;
using System.Reflection;
using System.Runtime.CompilerServices;
using System.Runtime.Versioning;
using BepInEx;
using BepInEx.Logging;
using BepInEx.Unity.IL2CPP;
using BepInEx.Unity.IL2CPP.Utils.Collections;
using Enemies;
using FX_EffectSystem;
using GTFO.API;
using Gear;
using HarmonyLib;
using IRF;
using Il2CppInterop.Runtime.Injection;
using Il2CppInterop.Runtime.InteropTypes;
using Il2CppInterop.Runtime.InteropTypes.Arrays;
using Il2CppSystem;
using Il2CppSystem.Collections.Generic;
using MemoryLeakFix.Handler;
using Microsoft.CodeAnalysis;
using UnityEngine;
using UnityEngine.Rendering;
[assembly: CompilationRelaxations(8)]
[assembly: RuntimeCompatibility(WrapNonExceptionThrows = true)]
[assembly: Debuggable(DebuggableAttribute.DebuggingModes.IgnoreSymbolStoreSequencePoints)]
[assembly: TargetFramework(".NETCoreApp,Version=v6.0", FrameworkDisplayName = ".NET 6.0")]
[assembly: AssemblyCompany("MemoryLeakFix")]
[assembly: AssemblyConfiguration("Release")]
[assembly: AssemblyFileVersion("1.0.0.0")]
[assembly: AssemblyInformationalVersion("1.0.0+ff75ba46295b3fe3cfb03ebbdafe88242ec46b8a")]
[assembly: AssemblyProduct("MemoryLeakFix")]
[assembly: AssemblyTitle("MemoryLeakFix")]
[assembly: AssemblyVersion("1.0.0.0")]
namespace Microsoft.CodeAnalysis
{
[CompilerGenerated]
[Microsoft.CodeAnalysis.Embedded]
internal sealed class EmbeddedAttribute : Attribute
{
}
}
namespace System.Runtime.CompilerServices
{
[CompilerGenerated]
[Microsoft.CodeAnalysis.Embedded]
[AttributeUsage(AttributeTargets.Class | AttributeTargets.Property | AttributeTargets.Field | AttributeTargets.Event | AttributeTargets.Parameter | AttributeTargets.ReturnValue | AttributeTargets.GenericParameter, AllowMultiple = false, Inherited = false)]
internal sealed class NullableAttribute : Attribute
{
public readonly byte[] NullableFlags;
public NullableAttribute(byte P_0)
{
NullableFlags = new byte[1] { P_0 };
}
public NullableAttribute(byte[] P_0)
{
NullableFlags = P_0;
}
}
[CompilerGenerated]
[Microsoft.CodeAnalysis.Embedded]
[AttributeUsage(AttributeTargets.Class | AttributeTargets.Struct | AttributeTargets.Method | AttributeTargets.Interface | AttributeTargets.Delegate, AllowMultiple = false, Inherited = false)]
internal sealed class NullableContextAttribute : Attribute
{
public readonly byte Flag;
public NullableContextAttribute(byte P_0)
{
Flag = P_0;
}
}
}
namespace MemoryLeakFix
{
[BepInPlugin("Dinorush.MemoryLeakFix", "MemoryLeakFix", "1.3.1")]
[BepInDependency(/*Could not decode attribute arguments.*/)]
internal sealed class EntryPoint : BasePlugin
{
public const string MODNAME = "MemoryLeakFix";
public override void Load()
{
//IL_0005: Unknown result type (might be due to invalid IL or missing references)
new Harmony("MemoryLeakFix").PatchAll();
((BasePlugin)this).Log.LogMessage((object)"Loaded MemoryLeakFix");
LevelAPI.OnLevelCleanup += OnLevelCleanup;
AssetAPI.OnStartupAssetsLoaded += OnAssetsLoaded;
}
private void OnLevelCleanup()
{
if (Decay.s_poolHandle != null)
{
Decay.s_poolHandle.Clear();
}
FallingObjectHandler.Clear();
}
private void OnAssetsLoaded()
{
FallingObjectHandler.Current.EnsureInit();
}
}
}
namespace MemoryLeakFix.Utils
{
internal static class DinoLogger
{
private static ManualLogSource logger = Logger.CreateLogSource("MemoryLeakFix");
public static void Log(string format, params object[] args)
{
Log(string.Format(format, args));
}
public static void Log(string str)
{
if (logger != null)
{
logger.Log((LogLevel)8, (object)str);
}
}
public static void Warning(string format, params object[] args)
{
Warning(string.Format(format, args));
}
public static void Warning(string str)
{
if (logger != null)
{
logger.Log((LogLevel)4, (object)str);
}
}
public static void Error(string format, params object[] args)
{
Error(string.Format(format, args));
}
public static void Error(string str)
{
if (logger != null)
{
logger.Log((LogLevel)2, (object)str);
}
}
public static void Debug(string format, params object[] args)
{
Debug(string.Format(format, args));
}
public static void Debug(string str)
{
if (logger != null)
{
logger.Log((LogLevel)32, (object)str);
}
}
}
}
namespace MemoryLeakFix.Patches
{
[HarmonyPatch]
internal static class DecayPatches
{
[CompilerGenerated]
private sealed class <DelayedClear>d__2 : IEnumerator<object>, IDisposable, IEnumerator
{
private int <>1__state;
private object <>2__current;
public Decay __instance;
object IEnumerator<object>.Current
{
[DebuggerHidden]
get
{
return <>2__current;
}
}
object IEnumerator.Current
{
[DebuggerHidden]
get
{
return <>2__current;
}
}
[DebuggerHidden]
public <DelayedClear>d__2(int <>1__state)
{
this.<>1__state = <>1__state;
}
[DebuggerHidden]
void IDisposable.Dispose()
{
<>1__state = -2;
}
private bool MoveNext()
{
//IL_001d: Unknown result type (might be due to invalid IL or missing references)
//IL_0027: Expected O, but got Unknown
switch (<>1__state)
{
default:
return false;
case 0:
<>1__state = -1;
<>2__current = (object)new WaitForSeconds(5f);
<>1__state = 1;
return true;
case 1:
<>1__state = -1;
if ((Object)(object)__instance.m_particles != (Object)null)
{
__instance.m_particles.Stop(true, (ParticleSystemStopBehavior)0);
}
__instance.m_playing = true;
return false;
}
}
bool IEnumerator.MoveNext()
{
//ILSpy generated this explicit interface implementation from .override directive in MoveNext
return this.MoveNext();
}
[DebuggerHidden]
void IEnumerator.Reset()
{
throw new NotSupportedException();
}
}
private const float DecayClearDelay = 5f;
[HarmonyPatch(typeof(Decay), "Initialize", new Type[]
{
typeof(SkinnedMeshRenderer),
typeof(List<InstancedRenderFeature>)
})]
[HarmonyPostfix]
private static void AddEndCallback(Decay __instance)
{
Decay __instance2 = __instance;
Decay obj = __instance2;
obj.OnDecaySafeToDespawnRenderer += Action.op_Implicit((Action)delegate
{
((MonoBehaviour)__instance2).StartCoroutine(CollectionExtensions.WrapToIl2Cpp(DelayedClear(__instance2)));
});
}
[IteratorStateMachine(typeof(<DelayedClear>d__2))]
private static IEnumerator DelayedClear(Decay __instance)
{
//yield-return decompiler failed: Unexpected instruction in Iterator.Dispose()
return new <DelayedClear>d__2(0)
{
__instance = __instance
};
}
}
[HarmonyPatch]
internal static class FallingPatches
{
[HarmonyPatch(typeof(GlueClusterGrenadeInstance), "Start")]
[HarmonyPostfix]
private static void GlueGrenadeSpawn(GlueClusterGrenadeInstance __instance)
{
FallingObjectHandler.AddObject(((Component)__instance).gameObject);
}
[HarmonyPatch(typeof(GlowstickInstance), "Setup")]
[HarmonyPostfix]
private static void GlowstickSpawn(GlowstickInstance __instance)
{
FallingObjectHandler.AddObject(((Component)__instance).gameObject);
}
[HarmonyPatch(typeof(FogRepellerInstance), "Start")]
[HarmonyPostfix]
private static void FogRepellerSpawn(FogRepellerInstance __instance)
{
FallingObjectHandler.AddObject(((Component)__instance).gameObject);
}
[HarmonyPatch(typeof(GlueGunProjectile), "Awake")]
[HarmonyPostfix]
private static void GlueGunSpawn(GlueGunProjectile __instance)
{
GlueGunProjectile __instance2 = __instance;
FallingObjectHandler.AddObject(((Component)__instance2).gameObject, delegate
{
if ((Object)(object)__instance2 != (Object)null)
{
ProjectileManager.WantToDestroyGlue(__instance2.SyncID);
}
if ((Object)(object)__instance2 != (Object)null)
{
__instance2.SyncDestroy();
}
});
}
[HarmonyPatch(typeof(BulletWeapon), "DropMagazine")]
[HarmonyWrapSafe]
[HarmonyPostfix]
private static void DropMag(BulletWeapon __instance)
{
Pool magDropPool = __instance.m_magDropPool;
if (magDropPool != null)
{
FallingObjectHandler.AddPool(magDropPool);
FallingObjectHandler.AddObject((magDropPool.m_freeInstances.Count > 0) ? magDropPool.m_freeInstances.First.Value : magDropPool.m_usedInstances.First.Value, (Action<GameObject>?)magDropPool.Return);
}
}
}
[HarmonyPatch]
internal static class IRFRenderPatches
{
private static Camera _drawMeshCamera;
private static bool _overrideCamera;
[HarmonyPatch(typeof(Graphics), "DrawMeshInstancedIndirect", new Type[]
{
typeof(Mesh),
typeof(int),
typeof(Material),
typeof(Bounds),
typeof(ComputeBuffer),
typeof(int),
typeof(MaterialPropertyBlock),
typeof(ShadowCastingMode),
typeof(bool),
typeof(int),
typeof(Camera),
typeof(LightProbeUsage),
typeof(LightProbeProxyVolume)
})]
[HarmonyPrefix]
private static void FixCamera(ref Camera camera)
{
if (_overrideCamera)
{
camera = _drawMeshCamera;
}
}
[HarmonyPatch(typeof(FPSCamera), "OnControllerEnable")]
[HarmonyPostfix]
private static void EnableCamera(FPSCamera __instance)
{
_drawMeshCamera = ((CameraController)__instance).m_camera;
_overrideCamera = true;
}
[HarmonyPatch(typeof(FPSCamera), "OnControllerDisable")]
[HarmonyPostfix]
private static void DisableCamera()
{
_overrideCamera = false;
}
}
[HarmonyPatch]
internal static class ObjectCleanupPatches
{
[HarmonyPatch(typeof(InstancedRenderFeature), "OnDestroy")]
[HarmonyPrefix]
private static void Pre_Destroy(InstancedRenderFeature __instance)
{
if ((Object)(object)((Component)__instance).transform.parent == (Object)null)
{
return;
}
InstancedRenderFeatureRenderer renderer = __instance.m_renderer;
if (renderer != null && (Object)(object)renderer.m_compute != (Object)null)
{
Object.Destroy((Object)(object)renderer.m_compute);
renderer.m_compute = null;
}
if (__instance.m_controller == null)
{
return;
}
foreach (Material item in (Il2CppArrayBase<Material>)(object)__instance.m_controller.Materials)
{
Object.Destroy((Object)(object)item);
}
}
[HarmonyPatch(typeof(EnemyAgent), "OnDestroy")]
[HarmonyPrefix]
private static void Pre_EnemyDestroy(EnemyAgent __instance)
{
FlyerAnimationController componentInChildren = ((Component)__instance).GetComponentInChildren<FlyerAnimationController>(true);
if ((Object)(object)componentInChildren != (Object)null)
{
Object.Destroy((Object)(object)componentInChildren.m_fleshBulbs.m_material);
}
}
[HarmonyPatch(typeof(FX_Manager), "ResetManager")]
[HarmonyPrefix]
private static void Pre_Reset()
{
foreach (FX_PointLight item in (Il2CppArrayBase<FX_PointLight>)(object)FX_Manager.s_effectPointLights)
{
Object.Destroy((Object)(object)((Component)item).gameObject);
}
FX_Manager.s_activeLights.Clear();
FX_Manager.s_freeLights.Clear();
}
[HarmonyPatch(typeof(GS_AfterLevel), "CleanupAfterExpedition")]
[HarmonyPriority(200)]
[HarmonyPostfix]
private static void Post_AllCleanup()
{
foreach (TextAsset item in Object.FindObjectsOfType<TextAsset>())
{
if (((Il2CppArrayBase<byte>)(object)item.bytes).Length == 0)
{
Object.Destroy((Object)(object)item);
}
}
}
}
[HarmonyPatch]
internal static class SoundPatches
{
[HarmonyPatch(typeof(GlueGunProjectile), "SyncDestroy")]
[HarmonyPrefix]
private static void GlueGunSpawn(GlueGunProjectile __instance)
{
__instance.m_sound.Recycle();
}
[HarmonyPatch(typeof(ProjectileBase), "Collision")]
[HarmonyPostfix]
private static void ProjectileDestroy(ProjectileBase __instance)
{
__instance.m_soundPlayer.Recycle();
}
[HarmonyPatch(typeof(StrikerBigTentacle), "OnDead")]
[HarmonyPostfix]
private static void BigTentacleDead(StrikerBigTentacle __instance)
{
__instance.m_tipSound.Recycle();
}
}
}
namespace MemoryLeakFix.Handler
{
internal sealed class FallingObjectHandler : MonoBehaviour
{
private static readonly Action<GameObject> BasicDestroy;
private const int MaxSteps = 20;
private const float UpdateInterval = 1f;
private readonly LinkedList<(GameObject? go, Action<GameObject> destroyFunc)> _objects = new LinkedList<(GameObject, Action<GameObject>)>();
private readonly Dictionary<IntPtr, Pool> _pools = new Dictionary<IntPtr, Pool>();
private float _nextUpdateTime;
private LinkedListNode<(GameObject? go, Action<GameObject> destroyFunc)>? _currentNode;
public static FallingObjectHandler Current { get; private set; }
static FallingObjectHandler()
{
//IL_001f: Unknown result type (might be due to invalid IL or missing references)
//IL_0024: Unknown result type (might be due to invalid IL or missing references)
//IL_002a: Expected O, but got Unknown
BasicDestroy = delegate(GameObject go)
{
Object.Destroy((Object)(object)go);
};
ClassInjector.RegisterTypeInIl2Cpp<FallingObjectHandler>();
GameObject val = new GameObject("MemoryLeakFix_FallingObjectHandler");
Object.DontDestroyOnLoad((Object)val);
Current = val.AddComponent<FallingObjectHandler>();
}
public void EnsureInit()
{
}
public void Awake()
{
Current = this;
}
public static void AddObject(GameObject go, Action<GameObject>? destroyFunc = null)
{
Current._objects.AddLast((go, destroyFunc ?? BasicDestroy));
}
public static void AddPool(Pool pool)
{
Current._pools.TryAdd(((Il2CppObjectBase)pool).Pointer, pool);
}
private void Update()
{
//IL_005a: Unknown result type (might be due to invalid IL or missing references)
if (Clock.Time < _nextUpdateTime)
{
return;
}
if (_currentNode == null)
{
_currentNode = _objects.First;
}
for (int i = 0; i < 20; i++)
{
if (_currentNode == null)
{
break;
}
GameObject item = _currentNode.Value.go;
LinkedListNode<(GameObject, Action<GameObject>)> next = _currentNode.Next;
if ((Object)(object)item != (Object)null && item.transform.position.y < -10000f)
{
_currentNode.Value.destroyFunc(item);
_objects.Remove(_currentNode);
}
else if ((Object)(object)item == (Object)null || !item.active)
{
_objects.Remove(_currentNode);
}
_currentNode = next;
}
if (_currentNode == null)
{
_nextUpdateTime = Clock.Time + 1f;
}
}
private void OnClear()
{
KeyValuePair<IntPtr, Pool>[] array = _pools.ToArray();
for (int i = 0; i < array.Length; i++)
{
KeyValuePair<IntPtr, Pool> keyValuePair = array[i];
var (key, val2) = (KeyValuePair<IntPtr, Pool>)(ref keyValuePair);
if (val2 == null)
{
_pools.Remove(key);
}
else if (val2.m_usedInstances.Count > 0)
{
GameObject[] array2 = (GameObject[])(object)new GameObject[val2.m_usedInstances.Count];
LinkedListNode<GameObject> val3 = val2.m_usedInstances.First;
int num = 0;
while (num < val2.m_usedInstances.Count)
{
array2[num] = val3.Value;
num++;
val3 = val3.Next;
}
GameObject[] array3 = array2;
foreach (GameObject val4 in array3)
{
val2.Return(val4);
}
}
}
_objects.Clear();
_currentNode = null;
}
public static void Clear()
{
Current.OnClear();
}
}
}