Decompiled source of Mechanical Weapon Jamming v1.7.3

mods/gun jamming.dll

Decompiled a month ago
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.IO;
using System.Reflection;
using System.Runtime.CompilerServices;
using System.Runtime.Versioning;
using BoneLib;
using BoneLib.BoneMenu;
using HarmonyLib;
using Il2CppInterop.Runtime.InteropTypes.Arrays;
using Il2CppSLZ.Marrow;
using Il2CppSystem;
using MelonLoader;
using MelonLoader.Utils;
using Microsoft.CodeAnalysis;
using RealJammingSystem;
using UnityEngine;

[assembly: CompilationRelaxations(8)]
[assembly: RuntimeCompatibility(WrapNonExceptionThrows = true)]
[assembly: Debuggable(DebuggableAttribute.DebuggingModes.IgnoreSymbolStoreSequencePoints)]
[assembly: MelonInfo(typeof(JammingMod), "Mechanical Weapon Jamming", "1.8.0", "Picklerick", null)]
[assembly: MelonGame("Stress Level Zero", "BONELAB")]
[assembly: TargetFramework(".NETCoreApp,Version=v6.0", FrameworkDisplayName = ".NET 6.0")]
[assembly: AssemblyVersion("0.0.0.0")]
namespace Microsoft.CodeAnalysis
{
	[CompilerGenerated]
	[Embedded]
	internal sealed class EmbeddedAttribute : Attribute
	{
	}
}
namespace System.Runtime.CompilerServices
{
	[CompilerGenerated]
	[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]
	[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 RealJammingSystem
{
	public class JammingMod : MelonMod
	{
		private static Page? customMenuPage = null;

		public static float globalJamChancePercent = 0.2f;

		public static bool isModEnabled = true;

		public static bool allowMisfires = true;

		public static bool allowCriticalFailures = true;

		public static bool allowSlideLocks = true;

		public static HashSet<int> JammedGunIDs = new HashSet<int>();

		public static HashSet<int> BrokenGunIDs = new HashSet<int>();

		public static AudioClip? customExplosionSound = null;

		public override void OnInitializeMelon()
		{
			Hooking.OnLevelLoaded += delegate
			{
				BuildModConfigurationMenu();
			};
			LoadCustomAudioTrack();
			MelonLogger.Msg("Mechanical Weapon Jamming initialized successfully for Patch 6!");
		}

		private void LoadCustomAudioTrack()
		{
			string text = "gun jamming gun explosion sound add-on.wav";
			string path = Path.Combine(MelonEnvironment.UserDataDirectory, text);
			if (!File.Exists(path))
			{
				path = Path.Combine(MelonEnvironment.ModsDirectory, text);
			}
			if (File.Exists(path))
			{
				try
				{
					byte[] array = File.ReadAllBytes(path);
					int num = BitConverter.ToInt16(array, 22);
					int num2 = BitConverter.ToInt32(array, 24);
					int i;
					for (i = 12; i < array.Length - 8; i++)
					{
						if (array[i] == 100 && array[i + 1] == 97 && array[i + 2] == 116 && array[i + 3] == 97)
						{
							i += 4;
							break;
						}
					}
					int num3 = BitConverter.ToInt32(array, i);
					i += 4;
					if (i >= array.Length || num3 <= 0)
					{
						i = 44;
						num3 = array.Length - 44;
					}
					float[] array2 = new float[num3 / 2];
					for (int j = 0; j < array2.Length; j++)
					{
						int num4 = i + j * 2;
						if (num4 + 1 < array.Length)
						{
							short num5 = BitConverter.ToInt16(array, num4);
							array2[j] = Mathf.Clamp((float)num5 / 32768f * 4f, -1f, 1f);
						}
					}
					customExplosionSound = AudioClip.Create("AmplifiedExplosion", array2.Length / num, num, num2, false);
					customExplosionSound.SetData(Il2CppStructArray<float>.op_Implicit(array2), 0);
					((Object)customExplosionSound).hideFlags = (HideFlags)52;
					MelonLogger.Msg("Loaded and boosted the explosion wav via direct binary stream!");
					return;
				}
				catch (Exception ex)
				{
					MelonLogger.Warning("Binary file loader failed: " + ex.Message);
					return;
				}
			}
			MelonLogger.Warning("[AUDIO FILE MISSING] Place '" + text + "' inside your UserData folder!");
		}

		private void BuildModConfigurationMenu()
		{
			//IL_0012: Unknown result type (might be due to invalid IL or missing references)
			//IL_002d: Unknown result type (might be due to invalid IL or missing references)
			//IL_0066: Unknown result type (might be due to invalid IL or missing references)
			//IL_009f: Unknown result type (might be due to invalid IL or missing references)
			//IL_00d8: Unknown result type (might be due to invalid IL or missing references)
			//IL_0111: Unknown result type (might be due to invalid IL or missing references)
			//IL_0159: Unknown result type (might be due to invalid IL or missing references)
			//IL_018d: Unknown result type (might be due to invalid IL or missing references)
			//IL_019f: Unknown result type (might be due to invalid IL or missing references)
			//IL_01cf: Unknown result type (might be due to invalid IL or missing references)
			//IL_01ff: Unknown result type (might be due to invalid IL or missing references)
			//IL_022f: Unknown result type (might be due to invalid IL or missing references)
			//IL_025f: Unknown result type (might be due to invalid IL or missing references)
			//IL_028f: Unknown result type (might be due to invalid IL or missing references)
			//IL_02bf: Unknown result type (might be due to invalid IL or missing references)
			//IL_02ef: Unknown result type (might be due to invalid IL or missing references)
			//IL_031e: Unknown result type (might be due to invalid IL or missing references)
			if (customMenuPage != null)
			{
				return;
			}
			customMenuPage = Page.Root.CreatePage("Weapon Jam Settings", Color.yellow, 0, true);
			customMenuPage.CreateBool("Mod Enabled", Color.white, isModEnabled, (Action<bool>)delegate(bool value)
			{
				isModEnabled = value;
				if (!value)
				{
					JammedGunIDs.Clear();
					BrokenGunIDs.Clear();
				}
			});
			customMenuPage.CreateBool("Include Misfires", Color.white, allowMisfires, (Action<bool>)delegate(bool value)
			{
				allowMisfires = value;
			});
			customMenuPage.CreateBool("Allow Slide Locks", Color.white, allowSlideLocks, (Action<bool>)delegate(bool value)
			{
				allowSlideLocks = value;
			});
			customMenuPage.CreateBool("Allow Critical Failures", Color.white, allowCriticalFailures, (Action<bool>)delegate(bool value)
			{
				allowCriticalFailures = value;
			});
			customMenuPage.CreateFloat("Jam Probability Rate", Color.white, globalJamChancePercent, 0.05f, 0f, 5f, (Action<float>)delegate(float value)
			{
				globalJamChancePercent = value;
			});
			customMenuPage.CreateFunction("Force Reset All Weapons", Color.green, (Action)delegate
			{
				JammedGunIDs.Clear();
				BrokenGunIDs.Clear();
				MelonLogger.Msg("Cleared all active jams and fixed broken guns");
			});
			Page obj = customMenuPage.CreatePage("Mod Credits & Suggestions", Color.cyan, 0, true);
			obj.CreateFunction("Community Ideas & Feedback:", Color.white, (Action)delegate
			{
			});
			obj.CreateFunction("- shyneee1", Color.yellow, (Action)delegate
			{
			});
			obj.CreateFunction("- kidneykicker42", Color.yellow, (Action)delegate
			{
			});
			obj.CreateFunction("- zekkriel", Color.yellow, (Action)delegate
			{
			});
			obj.CreateFunction("- slap that a** goochbaby", Color.yellow, (Action)delegate
			{
			});
			obj.CreateFunction("- Flakk0", Color.yellow, (Action)delegate
			{
			});
			obj.CreateFunction("- nontendol", Color.yellow, (Action)delegate
			{
			});
			obj.CreateFunction("- fortnitesecks22", Color.yellow, (Action)delegate
			{
			});
			obj.CreateFunction("- \ud83d\udccdHuss Valley", Color.yellow, (Action)delegate
			{
			});
		}
	}
	[HarmonyPatch(typeof(Gun), "Fire")]
	public static class GunFirePrePatch
	{
		[HarmonyPrefix]
		public static bool Prefix(Gun __instance)
		{
			//IL_003a: Unknown result type (might be due to invalid IL or missing references)
			//IL_0040: Invalid comparison between Unknown and I4
			//IL_02a2: Unknown result type (might be due to invalid IL or missing references)
			//IL_02ac: Unknown result type (might be due to invalid IL or missing references)
			//IL_02b8: Unknown result type (might be due to invalid IL or missing references)
			//IL_02c2: Unknown result type (might be due to invalid IL or missing references)
			//IL_00be: Unknown result type (might be due to invalid IL or missing references)
			//IL_00c3: Unknown result type (might be due to invalid IL or missing references)
			//IL_00cf: Unknown result type (might be due to invalid IL or missing references)
			//IL_00d9: Unknown result type (might be due to invalid IL or missing references)
			//IL_0130: Expected O, but got Unknown
			//IL_011c: Unknown result type (might be due to invalid IL or missing references)
			if ((Object)(object)__instance == (Object)null)
			{
				return true;
			}
			int instanceID = ((Object)__instance).GetInstanceID();
			if (JammingMod.BrokenGunIDs.Contains(instanceID))
			{
				return false;
			}
			if (!JammingMod.isModEnabled)
			{
				return true;
			}
			if (JammingMod.JammedGunIDs.Contains(instanceID))
			{
				return false;
			}
			if ((int)__instance.slideState == 4)
			{
				return true;
			}
			double num = JammingMod.globalJamChancePercent;
			if (Convert.ToDouble(Random.Range(0f, 100f)) < num)
			{
				if (JammingMod.allowCriticalFailures && Random.Range(0f, 100f) < 2f)
				{
					JammingMod.BrokenGunIDs.Add(instanceID);
					MelonLogger.Msg(((Object)__instance).name + " exploded!");
					try
					{
						if ((Object)(object)((Component)__instance).transform != (Object)null)
						{
							GameObject val = new GameObject("ExplosionFlash");
							val.transform.position = ((Component)__instance).transform.position;
							Light val2 = val.AddComponent<Light>();
							if ((Object)(object)val2 != (Object)null)
							{
								val2.type = (LightType)2;
								val2.range = 14f;
								val2.intensity = 65f;
								val2.color = new Color(1f, 0.35f, 0f);
							}
							Object.Destroy((Object)val, 0.15f);
						}
					}
					catch (Exception ex)
					{
						MelonLogger.Warning("VFX light flash failed: " + ex.Message);
					}
					try
					{
						if ((Object)(object)JammingMod.customExplosionSound != (Object)null && (Object)(object)Player.RigManager != (Object)null)
						{
							AudioSource val3 = ((Component)Player.RigManager).gameObject.AddComponent<AudioSource>();
							if ((Object)(object)val3 != (Object)null)
							{
								val3.clip = JammingMod.customExplosionSound;
								val3.volume = 1f;
								val3.spatialBlend = 0f;
								val3.Play();
								Object.Destroy((Object)(object)val3, 4f);
							}
						}
						else
						{
							for (int i = 0; i < 10; i++)
							{
								if ((Object)(object)__instance != (Object)null)
								{
									__instance.OnFire();
								}
							}
						}
					}
					catch (Exception ex2)
					{
						MelonLogger.Warning("Headset direct audio stream failed: " + ex2.Message);
					}
					try
					{
						if ((Object)(object)Player.RigManager != (Object)null)
						{
							((Component)Player.RigManager).gameObject.SendMessage("TakeDamage", Object.op_Implicit(30f), (SendMessageOptions)1);
						}
					}
					catch (Exception ex3)
					{
						MelonLogger.Warning("Damage call failed: " + ex3.Message);
					}
					Il2CppArrayBase<Grip> componentsInChildren = ((Component)__instance).GetComponentsInChildren<Grip>();
					if (componentsInChildren != null)
					{
						foreach (Grip item in componentsInChildren)
						{
							if (item != null)
							{
								item.ForceDetach();
							}
						}
					}
					Rigidbody component = ((Component)__instance).GetComponent<Rigidbody>();
					if ((Object)(object)component != (Object)null)
					{
						Transform transform = ((Component)__instance).transform;
						if ((Object)(object)transform != (Object)null)
						{
							component.AddForce(transform.forward * -45f, (ForceMode)1);
							component.AddTorque(Random.insideUnitSphere * 35f, (ForceMode)1);
						}
					}
					return true;
				}
				if (JammingMod.allowMisfires && Random.Range(0f, 100f) < 50f)
				{
					JammingMod.JammedGunIDs.Add(instanceID);
					MelonLogger.Msg(((Object)__instance).name + " misfired!");
					return false;
				}
				if (!JammingMod.allowSlideLocks)
				{
					return true;
				}
				JammingMod.JammedGunIDs.Add(instanceID);
				MelonLogger.Msg(((Object)__instance).name + " stovepiped!");
				__instance.EjectCartridge();
				__instance.slideState = (SlideStates)4;
				__instance.SlideLocked();
				return false;
			}
			return true;
		}
	}
	[HarmonyPatch(typeof(Gun), "SlideRelease")]
	public static class GunSlideReleasePatch
	{
		[HarmonyPrefix]
		public static bool Prefix(Gun? __instance)
		{
			if ((Object)(object)__instance == (Object)null)
			{
				return true;
			}
			int instanceID = ((Object)__instance).GetInstanceID();
			if (JammingMod.JammedGunIDs.Contains(instanceID))
			{
				JammingMod.JammedGunIDs.Remove(instanceID);
			}
			return true;
		}
	}
}