using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Reflection;
using System.Runtime.CompilerServices;
using System.Runtime.Versioning;
using BoneLib;
using BoneLib.BoneMenu;
using BoneLib.Notifications;
using HarmonyLib;
using Il2CppInterop.Runtime.InteropTypes.Arrays;
using Il2CppSLZ.Marrow;
using Il2CppSLZ.Marrow.Data;
using MelonLoader;
using Microsoft.CodeAnalysis;
using UnityEngine;
using WeaponConfig;
[assembly: CompilationRelaxations(8)]
[assembly: RuntimeCompatibility(WrapNonExceptionThrows = true)]
[assembly: Debuggable(DebuggableAttribute.DebuggingModes.IgnoreSymbolStoreSequencePoints)]
[assembly: MelonInfo(typeof(Core), "WeaponConfig", "1.0.0", "rz", null)]
[assembly: MelonGame("Stress Level Zero", "BONELAB")]
[assembly: TargetFramework(".NETCoreApp,Version=v6.0", FrameworkDisplayName = ".NET 6.0")]
[assembly: AssemblyCompany("WeaponConfig")]
[assembly: AssemblyConfiguration("Debug")]
[assembly: AssemblyFileVersion("1.0.0.0")]
[assembly: AssemblyInformationalVersion("1.0.0")]
[assembly: AssemblyProduct("WeaponConfig")]
[assembly: AssemblyTitle("WeaponConfig")]
[assembly: AssemblyVersion("1.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 WeaponConfig
{
public class Core : MelonMod
{
private record Snap(float Rpm, float Velocity, bool Silenced);
private static bool _built = false;
private static Page _root;
private static float _rpm = 600f;
private static float _velocity = 400f;
private static bool _silenced = false;
private static Page _currentGunPage = null;
private static FloatElement _rpmEl;
private static FloatElement _velEl;
private static FunctionElement _statusEl;
private static FunctionElement _fireModeEl;
internal static Gun HeldGun = null;
private static string _heldName = "";
private static readonly Dictionary<int, Snap> _snapshots = new Dictionary<int, Snap>();
private static readonly List<Gun> _allGuns = new List<Gun>();
private static bool IsSpawnGun(Gun g)
{
if ((Object)(object)g == (Object)null)
{
return true;
}
string text = ((Object)((Component)g).gameObject).name.ToUpper();
if (!text.Contains("SPAWN_GUN"))
{
return text.Contains("DUMMYGUN");
}
return true;
}
internal static void _allGunsAdd(Gun gun)
{
if (!((Object)(object)gun == (Object)null) && !IsSpawnGun(gun) && !_allGuns.Exists((Gun g) => (Object)(object)g != (Object)null && ((Object)g).GetInstanceID() == ((Object)gun).GetInstanceID()))
{
_allGuns.Add(gun);
}
}
public override void OnInitializeMelon()
{
Hooking.OnUIRigCreated += BuildMenu;
Hooking.OnLevelLoaded += delegate
{
HeldGun = null;
_heldName = "";
_allGuns.Clear();
_snapshots.Clear();
ClearGunPage();
};
Hooking.OnGrabObject += OnGrab;
Hooking.OnReleaseObject += OnRelease;
((MelonBase)this).HarmonyInstance.PatchAll();
}
private static void OnGrab(GameObject obj, Hand hand)
{
//IL_003e: Unknown result type (might be due to invalid IL or missing references)
if ((Object)(object)obj == (Object)null)
{
return;
}
Gun val = obj.GetComponentInChildren<Gun>() ?? obj.GetComponentInParent<Gun>() ?? ((Component)obj.transform.root).GetComponentInChildren<Gun>();
if ((Object)(object)val == (Object)null)
{
foreach (Collider item in (Il2CppArrayBase<Collider>)(object)Physics.OverlapSphere(obj.transform.position, 0.5f))
{
if (!((Object)(object)item == (Object)null))
{
val = ((Component)item).GetComponentInParent<Gun>() ?? ((Component)item).GetComponentInChildren<Gun>();
if ((Object)(object)val != (Object)null)
{
break;
}
}
}
}
if (!((Object)(object)val == (Object)null) && !IsSpawnGun(val))
{
SetHeld(val);
}
}
private static void OnRelease(Hand hand)
{
if (!((Object)(object)hand == (Object)null) && !((Object)(object)HeldGun == (Object)null))
{
HeldGun = null;
_heldName = "";
ClearGunPage();
}
}
private static void ClearGunPage()
{
if (_currentGunPage != null && _root != null)
{
_root.RemovePage(_currentGunPage);
_currentGunPage = null;
_rpmEl = null;
_velEl = null;
}
if (_statusEl != null)
{
((Element)_statusEl).ElementName = "No gun held.";
}
}
private static void CreateGunPage(string name)
{
//IL_0013: Unknown result type (might be due to invalid IL or missing references)
//IL_002e: Unknown result type (might be due to invalid IL or missing references)
//IL_007a: Unknown result type (might be due to invalid IL or missing references)
//IL_00c6: Unknown result type (might be due to invalid IL or missing references)
//IL_00ff: Unknown result type (might be due to invalid IL or missing references)
//IL_0146: Unknown result type (might be due to invalid IL or missing references)
ClearGunPage();
if (_root == null)
{
return;
}
_currentGunPage = _root.CreatePage(name, Color.yellow, 0, true);
_rpmEl = _currentGunPage.CreateFloat("Fire Rate (RPM)", Color.white, _rpm, 100f, 100f, 99999f, (Action<float>)delegate(float v)
{
_rpm = v;
Gun heldGun = HeldGun;
if (heldGun != null)
{
Extensions.SetRpm(heldGun, v);
}
});
_velEl = _currentGunPage.CreateFloat("Muzzle Velocity (m/s)", Color.white, _velocity, 50f, 0f, 5000f, (Action<float>)delegate(float v)
{
_velocity = v;
if ((Object)(object)HeldGun != (Object)null)
{
HeldGun.muzzleVelocity = v;
}
});
_currentGunPage.CreateBool("Silenced", Color.cyan, _silenced, (Action<bool>)delegate(bool v)
{
_silenced = v;
});
_fireModeEl = _currentGunPage.CreateFunction("Click me to change fire mode.", Color.white, (Action)delegate
{
//IL_000b: Unknown result type (might be due to invalid IL or missing references)
//IL_0010: Unknown result type (might be due to invalid IL or missing references)
//IL_0011: Unknown result type (might be due to invalid IL or missing references)
//IL_0013: Invalid comparison between Unknown and I4
//IL_001a: Unknown result type (might be due to invalid IL or missing references)
//IL_0016: Unknown result type (might be due to invalid IL or missing references)
//IL_001c: Unknown result type (might be due to invalid IL or missing references)
//IL_0037: Unknown result type (might be due to invalid IL or missing references)
Gun heldGun = HeldGun;
FireMode fireMode = HeldGun.fireMode;
FireMode fireMode2 = (((int)fireMode != 1) ? ((FireMode)1) : ((FireMode)2));
heldGun.fireMode = fireMode2;
((Element)_fireModeEl).ElementName = $"{HeldGun.fireMode}";
});
_currentGunPage.CreateFunction("Reset Gun", new Color(1f, 0.5f, 0.2f), (Action)delegate
{
Gun heldGun = HeldGun;
if ((Object)(object)heldGun == (Object)null)
{
Notify((NotificationType)1, "No gun held.");
}
else
{
int instanceID = ((Object)heldGun).GetInstanceID();
if (!_snapshots.TryGetValue(instanceID, out Snap value))
{
Notify((NotificationType)1, "No snapshot saved.");
}
else
{
Extensions.SetRpm(heldGun, value.Rpm);
heldGun.muzzleVelocity = value.Velocity;
heldGun.isSilenced = value.Silenced;
_snapshots.Remove(instanceID);
if (_rpmEl != null)
{
_rpmEl.Value = value.Rpm;
}
if (_velEl != null)
{
_velEl.Value = value.Velocity;
}
Notify((NotificationType)3, "Gun restored.");
}
}
});
if (_statusEl != null)
{
((Element)_statusEl).ElementName = "Gun Detected";
}
}
internal static void SetHeld(Gun gun)
{
if (!((Object)(object)gun == (Object)null) && !IsSpawnGun(gun))
{
string text = ((Object)((Component)gun).gameObject).name.Replace("(Clone)", "").Trim();
bool flag = (Object)(object)HeldGun != (Object)null && ((Object)HeldGun).GetInstanceID() == ((Object)gun).GetInstanceID();
HeldGun = gun;
_heldName = text;
int id = ((Object)gun).GetInstanceID();
if (!_snapshots.ContainsKey(id))
{
_snapshots[id] = new Snap(gun.roundsPerMinute, gun.muzzleVelocity, gun.isSilenced);
}
if (!_allGuns.Exists((Gun g) => (Object)(object)g != (Object)null && ((Object)g).GetInstanceID() == id))
{
_allGuns.Add(gun);
}
if (!flag)
{
CreateGunPage(text);
}
if (_rpmEl != null)
{
_rpmEl.Value = gun.roundsPerMinute;
}
if (_velEl != null)
{
_velEl.Value = gun.muzzleVelocity;
}
}
}
public override void OnUpdate()
{
//IL_003d: Unknown result type (might be due to invalid IL or missing references)
//IL_005b: 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_0060: Unknown result type (might be due to invalid IL or missing references)
//IL_008c: Unknown result type (might be due to invalid IL or missing references)
//IL_00aa: Unknown result type (might be due to invalid IL or missing references)
//IL_00a1: Unknown result type (might be due to invalid IL or missing references)
//IL_00af: Unknown result type (might be due to invalid IL or missing references)
//IL_00eb: Unknown result type (might be due to invalid IL or missing references)
//IL_00f0: Unknown result type (might be due to invalid IL or missing references)
//IL_00fd: Unknown result type (might be due to invalid IL or missing references)
//IL_0102: Unknown result type (might be due to invalid IL or missing references)
PhysicsRig physicsRig = Player.PhysicsRig;
if ((Object)(object)physicsRig != (Object)null)
{
Hand rightHand = physicsRig.rightHand;
Vector3? obj;
if (rightHand == null)
{
obj = null;
}
else
{
Transform transform = ((Component)rightHand).transform;
obj = ((transform != null) ? new Vector3?(transform.position) : ((Vector3?)null));
}
Vector3 val = (Vector3)(((??)obj) ?? Vector3.zero);
Hand leftHand = physicsRig.leftHand;
Vector3? obj2;
if (leftHand == null)
{
obj2 = null;
}
else
{
Transform transform2 = ((Component)leftHand).transform;
obj2 = ((transform2 != null) ? new Vector3?(transform2.position) : ((Vector3?)null));
}
Vector3 val2 = (Vector3)(((??)obj2) ?? Vector3.zero);
Gun val3 = null;
float num = 0.35f;
foreach (Gun allGun in _allGuns)
{
if (!((Object)(object)allGun == (Object)null) && !IsSpawnGun(allGun))
{
float num2 = Mathf.Min(Vector3.Distance(((Component)allGun).transform.position, val), Vector3.Distance(((Component)allGun).transform.position, val2));
if (num2 < num)
{
num = num2;
val3 = allGun;
}
}
}
if ((Object)(object)val3 != (Object)null && (Object)(object)val3 != (Object)(object)HeldGun)
{
SetHeld(val3);
}
else if ((Object)(object)val3 == (Object)null && (Object)(object)HeldGun != (Object)null)
{
HeldGun = null;
_heldName = "";
ClearGunPage();
}
}
Gun heldGun = HeldGun;
if ((Object)(object)heldGun == (Object)null)
{
return;
}
if (_silenced)
{
heldGun.isSilenced = true;
{
foreach (AudioSource componentsInChild in ((Component)heldGun).GetComponentsInChildren<AudioSource>())
{
componentsInChild.volume = 0f;
}
return;
}
}
heldGun.isSilenced = false;
foreach (AudioSource componentsInChild2 in ((Component)heldGun).GetComponentsInChildren<AudioSource>())
{
if (componentsInChild2.volume == 0f)
{
componentsInChild2.volume = 1f;
}
}
}
private static void BuildMenu()
{
//IL_0018: Unknown result type (might be due to invalid IL or missing references)
//IL_0033: Unknown result type (might be due to invalid IL or missing references)
//IL_006b: 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)
if (_built)
{
return;
}
_built = true;
_root = Page.Root.CreatePage("WeaponConfig", Color.red, 0, true);
_statusEl = _root.CreateFunction("No gun held.", Color.grey, (Action)delegate
{
});
_root.CreateFunction("Fill All Ammo Types", Color.white, (Action)delegate
{
AmmoInventory instance = AmmoInventory.Instance;
if (!((Object)(object)instance == (Object)null))
{
AmmoGroup lightAmmoGroup = instance.lightAmmoGroup;
if (lightAmmoGroup != null && ((Il2CppArrayBase<CartridgeData>)(object)lightAmmoGroup.cartridges)?.Length > 0)
{
instance.AddCartridge(((Il2CppArrayBase<CartridgeData>)(object)instance.lightAmmoGroup.cartridges)[0], 999);
}
AmmoGroup mediumAmmoGroup = instance.mediumAmmoGroup;
if (mediumAmmoGroup != null && ((Il2CppArrayBase<CartridgeData>)(object)mediumAmmoGroup.cartridges)?.Length > 0)
{
instance.AddCartridge(((Il2CppArrayBase<CartridgeData>)(object)instance.mediumAmmoGroup.cartridges)[0], 999);
}
AmmoGroup heavyAmmoGroup = instance.heavyAmmoGroup;
if (heavyAmmoGroup != null && ((Il2CppArrayBase<CartridgeData>)(object)heavyAmmoGroup.cartridges)?.Length > 0)
{
instance.AddCartridge(((Il2CppArrayBase<CartridgeData>)(object)instance.heavyAmmoGroup.cartridges)[0], 999);
}
}
});
_root.CreateFunction("_____________________", Color.gray, (Action)delegate
{
});
}
private static void Notify(NotificationType type, string msg)
{
//IL_0000: Unknown result type (might be due to invalid IL or missing references)
//IL_0005: Unknown result type (might be due to invalid IL or missing references)
//IL_000b: Unknown result type (might be due to invalid IL or missing references)
//IL_0010: Unknown result type (might be due to invalid IL or missing references)
//IL_0015: Unknown result type (might be due to invalid IL or missing references)
//IL_0017: Unknown result type (might be due to invalid IL or missing references)
//IL_001c: Unknown result type (might be due to invalid IL or missing references)
//IL_0021: Unknown result type (might be due to invalid IL or missing references)
//IL_0022: Unknown result type (might be due to invalid IL or missing references)
//IL_0023: Unknown result type (might be due to invalid IL or missing references)
//IL_0028: Unknown result type (might be due to invalid IL or missing references)
//IL_002f: Unknown result type (might be due to invalid IL or missing references)
//IL_003f: Expected O, but got Unknown
Notifier.Send(new Notification
{
Title = NotificationText.op_Implicit("WeaponConfig"),
Message = NotificationText.op_Implicit(msg),
Type = type,
ShowTitleOnPopup = true,
PopupLength = 2.5f
});
}
}
[HarmonyPatch(typeof(Gun), "Fire")]
internal static class PatchGunFire
{
private static void Prefix(Gun __instance)
{
Core.SetHeld(__instance);
}
}
[HarmonyPatch(typeof(Gun), "OnPoolInitialize")]
internal static class PatchGunInit
{
private static void Postfix(Gun __instance)
{
if (!((Object)(object)__instance == (Object)null))
{
Core._allGunsAdd(__instance);
}
}
}
}