using System;
using System.Collections.Generic;
using System.Linq;
using System.Reflection;
using System.Runtime.CompilerServices;
using BepInEx;
using BepInEx.Configuration;
using BepInEx.Logging;
using FishNet;
using UnityEngine;
using UnityEngine.SceneManagement;
[assembly: RuntimeCompatibility(WrapNonExceptionThrows = true)]
[assembly: AssemblyVersion("0.0.0.0")]
namespace RandomPresetSwapper;
[BepInPlugin("yourname.straftat.randompresetswapper", "Random Preset Swapper", "1.0.0")]
public class Plugin : BaseUnityPlugin
{
public const string PluginGuid = "yourname.straftat.randompresetswapper";
public const string PluginName = "Random Preset Swapper";
public const string PluginVersion = "1.0.0";
private static ManualLogSource Log;
private static readonly Random Rng = new Random();
private static ConfigEntry<bool> _enabled;
private static WeaponPresetUtility _cachedUtil;
private void Awake()
{
Log = ((BaseUnityPlugin)this).Logger;
_enabled = ((BaseUnityPlugin)this).Config.Bind<bool>("General", "Enabled", true, "If true, a random saved Weapon Randomiser preset is loaded at the start of every match. Set to false to disable without removing the mod. Can be edited live while the game is running.");
SceneManager.sceneLoaded += OnSceneLoaded;
Log.LogInfo((object)string.Format("{0} v{1} loaded. Enabled = {2}", "Random Preset Swapper", "1.0.0", _enabled.Value));
}
private void OnSceneLoaded(Scene scene, LoadSceneMode mode)
{
//IL_0015: Unknown result type (might be due to invalid IL or missing references)
TryCacheUtility();
if (_enabled.Value)
{
if (!IsGameplayScene(scene))
{
Log.LogInfo((object)$"[RandomPresetSwapper] '{((Scene)(ref scene)).name}' looks like a menu/lobby/victory scene — skipping.");
}
else if (!((Object)(object)InstanceFinder.NetworkManager == (Object)null) && InstanceFinder.NetworkManager.IsServer)
{
ApplyRandomPreset();
}
}
}
private static bool IsGameplayScene(Scene scene)
{
GameManager instance = GameManager.Instance;
if ((Object)(object)instance == (Object)null)
{
return false;
}
if (!string.IsNullOrEmpty(instance.MainMenuSceneName) && ((Scene)(ref scene)).name.Equals(instance.MainMenuSceneName, StringComparison.OrdinalIgnoreCase))
{
return false;
}
if (!string.IsNullOrEmpty(instance.VictorySceneName) && ((Scene)(ref scene)).name.Equals(instance.VictorySceneName, StringComparison.OrdinalIgnoreCase))
{
return false;
}
return true;
}
private static void TryCacheUtility()
{
if (!((Object)(object)_cachedUtil != (Object)null))
{
WeaponPresetManager instance = WeaponPresetManager.Instance;
if ((Object)(object)instance != (Object)null && (Object)(object)instance.weaponPresetUtil != (Object)null)
{
_cachedUtil = instance.weaponPresetUtil;
Log.LogInfo((object)"[RandomPresetSwapper] Cached WeaponPresetUtility reference.");
}
}
}
private static void ApplyRandomPreset()
{
if ((Object)(object)_cachedUtil == (Object)null)
{
Log.LogWarning((object)"[RandomPresetSwapper] No WeaponPresetUtility reference yet — open the Randomise Weapons screen at least once after launching the game so it initialises, then host again.");
return;
}
Dictionary<string, WeaponPreset> presets = _cachedUtil.Presets;
if (presets == null || presets.Count == 0)
{
Log.LogWarning((object)"[RandomPresetSwapper] No saved Weapon Randomiser presets found. Save at least one preset in-game first.");
return;
}
List<string> list = presets.Keys.ToList();
string text = list[Rng.Next(list.Count)];
try
{
_cachedUtil.LoadPreset(text);
Log.LogInfo((object)$"[RandomPresetSwapper] Applied random preset: '{text}' (from {list.Count} saved presets).");
}
catch (Exception arg)
{
Log.LogError((object)$"[RandomPresetSwapper] LoadPreset('{text}') threw an exception: {arg}");
}
}
}