using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Reflection;
using System.Runtime.CompilerServices;
using System.Runtime.Versioning;
using System.Security;
using System.Security.Permissions;
using AutoReloadMod;
using BoneLib;
using HarmonyLib;
using Il2CppSLZ.Marrow;
using MelonLoader;
using MelonLoader.Preferences;
using UnityEngine;
[assembly: CompilationRelaxations(8)]
[assembly: RuntimeCompatibility(WrapNonExceptionThrows = true)]
[assembly: Debuggable(DebuggableAttribute.DebuggingModes.Default | DebuggableAttribute.DebuggingModes.DisableOptimizations | DebuggableAttribute.DebuggingModes.IgnoreSymbolStoreSequencePoints | DebuggableAttribute.DebuggingModes.EnableEditAndContinue)]
[assembly: MelonInfo(typeof(Core), "TagtusVR AutoReload", "1.4.0", "TagtusVR", null)]
[assembly: MelonGame("Stress Level Zero", "BONELAB")]
[assembly: MelonColor(255, 0, 200, 255)]
[assembly: MelonOptionalDependencies(new string[] { "BoneLib" })]
[assembly: TargetFramework(".NETCoreApp,Version=v8.0", FrameworkDisplayName = ".NET 8.0")]
[assembly: AssemblyCompany("TagtusVR_AutoReload")]
[assembly: AssemblyConfiguration("Debug")]
[assembly: AssemblyFileVersion("1.0.0.0")]
[assembly: AssemblyInformationalVersion("1.0.0")]
[assembly: AssemblyProduct("TagtusVR_AutoReload")]
[assembly: AssemblyTitle("TagtusVR_AutoReload")]
[assembly: SecurityPermission(SecurityAction.RequestMinimum, SkipVerification = true)]
[assembly: AssemblyVersion("1.0.0.0")]
[module: UnverifiableCode]
[module: RefSafetyRules(11)]
namespace AutoReloadMod;
public class Core : MelonMod
{
public static Core Instance;
public static bool Enabled = true;
public static float ReloadDelay = 0f;
public static bool OnlyHeldGuns = true;
internal static readonly Dictionary<Gun, float> reloadTimers = new Dictionary<Gun, float>();
internal static readonly HashSet<Gun> currentlyReloading = new HashSet<Gun>();
public override void OnInitializeMelon()
{
Instance = this;
((MelonBase)this).HarmonyInstance.PatchAll();
MelonPreferences_Category val = MelonPreferences.CreateCategory("TagtusVR_AutoReload");
val.CreateEntry<bool>("Enabled", true, (string)null, (string)null, false, false, (ValueValidator)null, (string)null);
val.CreateEntry<float>("ReloadDelay", 0f, (string)null, (string)null, false, false, (ValueValidator)null, (string)null);
val.CreateEntry<bool>("OnlyHeldGuns", true, (string)null, (string)null, false, false, (ValueValidator)null, (string)null);
Enabled = val.GetEntry<bool>("Enabled").Value;
ReloadDelay = Mathf.Clamp(val.GetEntry<float>("ReloadDelay").Value, 0f, 10f);
OnlyHeldGuns = val.GetEntry<bool>("OnlyHeldGuns").Value;
((MelonBase)this).LoggerInstance.Msg("TagtusVR AutoReload v1.4 ready. Change values in MelonPreferences.cfg");
}
public override void OnUpdate()
{
if (!Enabled)
{
return;
}
List<Gun> list = new List<Gun>();
foreach (Gun key in reloadTimers.Keys)
{
if ((Object)(object)key == (Object)null)
{
list.Add(key);
}
}
foreach (Gun item in list)
{
reloadTimers.Remove(item);
currentlyReloading.Remove(item);
}
List<Gun> list2 = new List<Gun>();
if (OnlyHeldGuns)
{
if ((Object)(object)Player.LeftHand != (Object)null)
{
Gun componentInHand = Player.GetComponentInHand<Gun>(Player.LeftHand);
if ((Object)(object)componentInHand != (Object)null)
{
list2.Add(componentInHand);
}
}
if ((Object)(object)Player.RightHand != (Object)null)
{
Gun componentInHand2 = Player.GetComponentInHand<Gun>(Player.RightHand);
if ((Object)(object)componentInHand2 != (Object)null)
{
list2.Add(componentInHand2);
}
}
}
else
{
foreach (Gun item2 in Object.FindObjectsOfType<Gun>())
{
if ((Object)(object)item2 != (Object)null)
{
list2.Add(item2);
}
}
}
foreach (Gun item3 in list2)
{
if ((Object)(object)item3 == (Object)null)
{
continue;
}
int ammo = GetAmmo(item3);
if (ammo > 0)
{
if (currentlyReloading.Contains(item3))
{
currentlyReloading.Remove(item3);
reloadTimers.Remove(item3);
}
}
else if (!currentlyReloading.Contains(item3))
{
currentlyReloading.Add(item3);
reloadTimers[item3] = ReloadDelay;
}
}
List<Gun> list3 = new List<Gun>();
foreach (KeyValuePair<Gun, float> reloadTimer in reloadTimers)
{
if (!((Object)(object)reloadTimer.Key == (Object)null))
{
float num = reloadTimer.Value - Time.deltaTime;
if (num <= 0f)
{
DoRefill(reloadTimer.Key);
list3.Add(reloadTimer.Key);
}
else
{
reloadTimers[reloadTimer.Key] = num;
}
}
}
foreach (Gun item4 in list3)
{
reloadTimers.Remove(item4);
currentlyReloading.Remove(item4);
}
}
internal static int GetAmmo(Gun gun)
{
try
{
return gun.AmmoCount();
}
catch
{
try
{
Magazine componentInChildren = ((Component)gun).GetComponentInChildren<Magazine>();
if ((Object)(object)componentInChildren != (Object)null && componentInChildren.magazineState != null)
{
return componentInChildren.magazineState.AmmoCount;
}
}
catch
{
}
}
return 0;
}
internal static void DoRefill(Gun gun)
{
if ((Object)(object)gun == (Object)null)
{
return;
}
try
{
Magazine componentInChildren = ((Component)gun).GetComponentInChildren<Magazine>();
if ((Object)(object)componentInChildren != (Object)null && componentInChildren.magazineState != null)
{
componentInChildren.magazineState.Refill();
}
}
catch (Exception ex)
{
MelonLogger.Warning("Refill failed: " + ex.Message);
}
}
}
[HarmonyPatch(typeof(Gun), "OnFire")]
public static class OnFirePatch
{
public static void Postfix(Gun __instance)
{
if (Core.Enabled && !((Object)(object)__instance == (Object)null) && Core.GetAmmo(__instance) <= 0 && !Core.currentlyReloading.Contains(__instance))
{
Core.currentlyReloading.Add(__instance);
Core.reloadTimers[__instance] = Core.ReloadDelay;
}
}
}