Decompiled source of EuphRagdolls v1.0.3

Mods/EuphRagdolls.dll

Decompiled 14 hours ago
using System;
using System.Collections;
using System.Collections.Generic;
using System.Diagnostics;
using System.Reflection;
using System.Runtime.CompilerServices;
using EuphRagdolls;
using Il2CppInterop.Runtime.InteropTypes.Arrays;
using Il2CppSLZ.Marrow.AI;
using Il2CppSLZ.Marrow.PuppetMasta;
using MelonLoader;
using UnityEngine;

[assembly: CompilationRelaxations(8)]
[assembly: RuntimeCompatibility(WrapNonExceptionThrows = true)]
[assembly: Debuggable(/*Could not decode attribute arguments.*/)]
[assembly: MelonInfo(typeof(NpcRagdollMod), "EuphRagdolls", "1.0.3", "Hap", null)]
[assembly: MelonGame("Stress Level Zero", "BONELAB")]
[assembly: AssemblyVersion("0.0.0.0")]
namespace EuphRagdolls;

public sealed class NpcRagdollMod : MelonMod
{
	private readonly Dictionary<int, float> _originalMuscleWeights = new Dictionary<int, float>();

	private float _nextScanTime;

	private bool _fusionWasActive;

	public override void OnUpdate()
	{
		if (Time.time < _nextScanTime)
		{
			return;
		}
		_nextScanTime = Time.time + 0.5f;
		if (IsFusionLoaded())
		{
			RestoreAllNpcWeights();
			if (!_fusionWasActive)
			{
				((MelonBase)this).LoggerInstance.Msg("Fusion detected; NPC ragdoll behavior paused.");
			}
			_fusionWasActive = true;
		}
		else
		{
			_fusionWasActive = false;
			ApplyRagdollToNpcs();
		}
	}

	private void ApplyRagdollToNpcs()
	{
		Il2CppArrayBase<AIBrain> val = Object.FindObjectsOfType<AIBrain>();
		if (val == null)
		{
			return;
		}
		global::System.Collections.Generic.IEnumerator<AIBrain> enumerator = val.GetEnumerator();
		try
		{
			while (((global::System.Collections.IEnumerator)enumerator).MoveNext())
			{
				AIBrain current = enumerator.Current;
				if (!((Object)(object)current == (Object)null) && !((Object)(object)current.puppetMaster == (Object)null))
				{
					PuppetMaster puppetMaster = current.puppetMaster;
					int instanceID = ((Object)puppetMaster).GetInstanceID();
					if (!_originalMuscleWeights.ContainsKey(instanceID))
					{
						_originalMuscleWeights[instanceID] = puppetMaster.muscleWeight;
					}
					puppetMaster.muscleWeight = 0f;
				}
			}
		}
		finally
		{
			((global::System.IDisposable)enumerator)?.Dispose();
		}
	}

	private void RestoreAllNpcWeights()
	{
		Il2CppArrayBase<AIBrain> val = Object.FindObjectsOfType<AIBrain>();
		if (val == null || _originalMuscleWeights.Count == 0)
		{
			return;
		}
		global::System.Collections.Generic.IEnumerator<AIBrain> enumerator = val.GetEnumerator();
		try
		{
			float muscleWeight = default(float);
			while (((global::System.Collections.IEnumerator)enumerator).MoveNext())
			{
				AIBrain current = enumerator.Current;
				if (!((Object)(object)current == (Object)null) && !((Object)(object)current.puppetMaster == (Object)null))
				{
					PuppetMaster puppetMaster = current.puppetMaster;
					int instanceID = ((Object)puppetMaster).GetInstanceID();
					if (_originalMuscleWeights.TryGetValue(instanceID, ref muscleWeight))
					{
						puppetMaster.muscleWeight = muscleWeight;
					}
				}
			}
		}
		finally
		{
			((global::System.IDisposable)enumerator)?.Dispose();
		}
		_originalMuscleWeights.Clear();
	}

	private static bool IsFusionLoaded()
	{
		Assembly[] assemblies = AppDomain.CurrentDomain.GetAssemblies();
		foreach (Assembly val in assemblies)
		{
			string name = val.GetName().Name;
			if (name != null && name.StartsWith("LabFusion", (StringComparison)5))
			{
				return true;
			}
		}
		return false;
	}
}