Mods\FlipMod.dll

Decompiled 2 weeks ago
using System;
using System.Diagnostics;
using System.Reflection;
using System.Runtime.CompilerServices;
using System.Runtime.Versioning;
using BoneLib;
using BoneLib.BoneMenu;
using Flip;
using Il2CppSLZ.Marrow;
using LabFusion.Entities;
using LabFusion.Network;
using LabFusion.Network.Serialization;
using LabFusion.SDK.Modules;
using MelonLoader;
using MelonLoader.Preferences;
using UnityEngine;

[assembly: CompilationRelaxations(8)]
[assembly: RuntimeCompatibility(WrapNonExceptionThrows = true)]
[assembly: Debuggable(DebuggableAttribute.DebuggingModes.IgnoreSymbolStoreSequencePoints)]
[assembly: MelonInfo(typeof(FlipMod), "Flip", "1.1.0", "Spoon1606", null)]
[assembly: MelonGame("Stress Level Zero", "BONELAB")]
[assembly: MelonOptionalDependencies(new string[] { "RagdollPlayer", "LabFusion" })]
[assembly: TargetFramework(".NETCoreApp,Version=v6.0", FrameworkDisplayName = ".NET 6.0")]
[assembly: AssemblyVersion("0.0.0.0")]
namespace Flip;

public class FlipMod : MelonMod
{
	private static MelonPreferences_Category _prefCategory;

	private static MelonPreferences_Entry<bool> _prefEnabled;

	private static MelonPreferences_Entry<int> _prefStrength;

	private static bool _enabled = true;

	private static int _strength = 6;

	private static bool _fusionAvailable;

	private static Page _mainPage;

	private static BoolElement _enabledElement;

	private static IntElement _strengthElement;

	private const float Deadzone = 0.3f;

	private const float StrengthMultiplier = 100f;

	public override void OnInitializeMelon()
	{
		SetupPreferences();
		SetupBoneMenu();
		TryRegisterFusionModule();
		MelonLogger.Msg("Flip by Spoon1606 loaded! Only works when Ragdoll Player mod is active. Move left thumbstick forward/back while ragdolled.");
	}

	private void TryRegisterFusionModule()
	{
		try
		{
			ModuleManager.LoadModules(Assembly.GetExecutingAssembly());
			_fusionAvailable = true;
			MelonLogger.Msg("LabFusion detected - multiplayer flip sync enabled.");
		}
		catch
		{
			_fusionAvailable = false;
			MelonLogger.Msg("LabFusion not detected - multiplayer sync unavailable.");
		}
	}

	private void SetupPreferences()
	{
		_prefCategory = MelonPreferences.CreateCategory("Flip");
		_prefEnabled = _prefCategory.CreateEntry<bool>("IsEnabled", true, (string)null, (string)null, false, false, (ValueValidator)null, (string)null);
		_prefStrength = _prefCategory.CreateEntry<int>("Strength", 6, (string)null, (string)null, false, false, (ValueValidator)null, (string)null);
		_enabled = _prefEnabled.Value;
		_strength = _prefStrength.Value;
	}

	private void SetupBoneMenu()
	{
		//IL_0019: Unknown result type (might be due to invalid IL or missing references)
		//IL_0034: Unknown result type (might be due to invalid IL or missing references)
		//IL_005e: Unknown result type (might be due to invalid IL or missing references)
		_mainPage = Page.Root.CreatePage("Flip", new Color(0f, 0.8f, 1f), 0, true);
		_enabledElement = _mainPage.CreateBool("Enabled", Color.yellow, _enabled, (Action<bool>)OnSetEnabled);
		_strengthElement = _mainPage.CreateInt("Strength", Color.white, _strength, 1, 1, 50, (Action<int>)OnSetStrength);
	}

	private void OnSetEnabled(bool value)
	{
		_enabled = value;
		_prefEnabled.Value = value;
		_prefCategory.SaveToFile(false);
	}

	private void OnSetStrength(int value)
	{
		_strength = value;
		_prefStrength.Value = value;
		_prefCategory.SaveToFile(false);
	}

	private bool IsRagdollActive()
	{
		PhysicsRig physicsRig = Player.PhysicsRig;
		if ((Object)(object)physicsRig == (Object)null)
		{
			return false;
		}
		bool num = (Object)(object)physicsRig.torso != (Object)null && physicsRig.torso.shutdown;
		bool flag = !physicsRig.ballLocoEnabled;
		return num || flag;
	}

	public override void OnUpdate()
	{
		//IL_003e: Unknown result type (might be due to invalid IL or missing references)
		if (!_enabled || (Object)(object)Player.RigManager == (Object)null || (Object)(object)Player.PhysicsRig == (Object)null || !IsRagdollActive())
		{
			return;
		}
		BaseController leftController = Player.LeftController;
		if (!((Object)(object)leftController == (Object)null))
		{
			float y = leftController.GetThumbStickAxis().y;
			if (!(Mathf.Abs(y) < 0.3f))
			{
				PerformFlip(y);
			}
		}
	}

	private void PerformFlip(float direction)
	{
		//IL_0047: Unknown result type (might be due to invalid IL or missing references)
		//IL_004d: Unknown result type (might be due to invalid IL or missing references)
		//IL_0052: Unknown result type (might be due to invalid IL or missing references)
		//IL_0055: Unknown result type (might be due to invalid IL or missing references)
		//IL_0063: Unknown result type (might be due to invalid IL or missing references)
		//IL_006a: Unknown result type (might be due to invalid IL or missing references)
		PhysicsRig physicsRig = Player.PhysicsRig;
		if ((Object)(object)physicsRig == (Object)null)
		{
			return;
		}
		Transform pelvis = ((Rig)physicsRig).m_pelvis;
		if ((Object)(object)pelvis == (Object)null)
		{
			return;
		}
		Rigidbody component = ((Component)pelvis).GetComponent<Rigidbody>();
		if (!((Object)(object)component == (Object)null))
		{
			float num = direction * (float)_strength * 100f;
			Vector3 angularVelocity = (component.angularVelocity = ((Component)pelvis).transform.right * num);
			if (_fusionAvailable)
			{
				MessageRelay.RelayModule<FlipSyncMessage, FlipSyncData>(FlipSyncData.Create(angularVelocity), CommonMessageRoutes.UnreliableToOtherClients);
			}
		}
	}

	public override void OnPreferencesLoaded()
	{
		_enabled = _prefEnabled.Value;
		_strength = _prefStrength.Value;
		if (_enabledElement != null)
		{
			_enabledElement.Value = _enabled;
		}
		if (_strengthElement != null)
		{
			_strengthElement.Value = _strength;
		}
	}
}
public class FlipModule : Module
{
	public override string Name => "Flip";

	public override string Author => "Spoon1606";

	public override Version Version => new Version(1, 1, 0);

	public override ConsoleColor Color => ConsoleColor.Cyan;

	protected override void OnModuleRegistered()
	{
		ModuleMessageManager.LoadHandlers(Assembly.GetExecutingAssembly());
	}
}
public class FlipSyncData : INetSerializable
{
	public float x;

	public float y;

	public float z;

	public int? GetSize()
	{
		return 12;
	}

	public void Serialize(INetSerializer serializer)
	{
		serializer.SerializeValue(ref x);
		serializer.SerializeValue(ref y);
		serializer.SerializeValue(ref z);
	}

	public static FlipSyncData Create(Vector3 angularVelocity)
	{
		//IL_0006: Unknown result type (might be due to invalid IL or missing references)
		//IL_0012: Unknown result type (might be due to invalid IL or missing references)
		//IL_001e: Unknown result type (might be due to invalid IL or missing references)
		return new FlipSyncData
		{
			x = angularVelocity.x,
			y = angularVelocity.y,
			z = angularVelocity.z
		};
	}
}
[SkipHandleWhileLoading]
public class FlipSyncMessage : ModuleMessageHandler
{
	protected override void OnHandleMessage(ReceivedMessage received)
	{
		//IL_00a8: Unknown result type (might be due to invalid IL or missing references)
		FlipSyncData flipSyncData = ((ReceivedMessage)(ref received)).ReadData<FlipSyncData>();
		byte? sender = ((ReceivedMessage)(ref received)).Sender;
		NetworkPlayer val = default(NetworkPlayer);
		if (!sender.HasValue || !NetworkPlayerManager.TryGetPlayer(sender.Value, ref val) || val.NetworkEntity.IsOwner || !val.HasRig)
		{
			return;
		}
		RigManager rigManager = val.RigRefs.RigManager;
		if ((Object)(object)rigManager == (Object)null)
		{
			return;
		}
		PhysicsRig physicsRig = rigManager.physicsRig;
		if ((Object)(object)physicsRig == (Object)null)
		{
			return;
		}
		Transform pelvis = ((Rig)physicsRig).m_pelvis;
		if (!((Object)(object)pelvis == (Object)null))
		{
			Rigidbody component = ((Component)pelvis).GetComponent<Rigidbody>();
			if (!((Object)(object)component == (Object)null))
			{
				component.angularVelocity = new Vector3(flipSyncData.x, flipSyncData.y, flipSyncData.z);
			}
		}
	}
}