Decompiled source of Erenshor Weapon Sets v1.0.1

BepInEx\plugins\WeaponSets\Erenshor-WeaponSets.dll

Decompiled 2 weeks ago
using System;
using System.Diagnostics;
using System.IO;
using System.Reflection;
using System.Runtime.CompilerServices;
using System.Runtime.InteropServices;
using System.Runtime.Versioning;
using BepInEx;
using BepInEx.Configuration;
using BepInEx.Logging;
using TMPro;
using UnityEngine;
using UnityEngine.Events;
using UnityEngine.UI;

[assembly: CompilationRelaxations(8)]
[assembly: RuntimeCompatibility(WrapNonExceptionThrows = true)]
[assembly: Debuggable(DebuggableAttribute.DebuggingModes.IgnoreSymbolStoreSequencePoints)]
[assembly: AssemblyTitle("Erenshor-WeaponSets")]
[assembly: AssemblyDescription("")]
[assembly: AssemblyConfiguration("")]
[assembly: AssemblyCompany("")]
[assembly: AssemblyProduct("Erenshor-WeaponSets")]
[assembly: AssemblyCopyright("Copyright ©  2025")]
[assembly: AssemblyTrademark("")]
[assembly: ComVisible(false)]
[assembly: Guid("7787dbe8-427f-457b-9652-227bfa74e39f")]
[assembly: AssemblyFileVersion("1.0.1")]
[assembly: TargetFramework(".NETFramework,Version=v4.7.2", FrameworkDisplayName = ".NET Framework 4.7.2")]
[assembly: AssemblyVersion("1.0.1.0")]
namespace Erenshor_WeaponSets;

[BepInPlugin("Brad522.WeaponSets", "Weapon Sets", "1.0.1")]
public class WeaponSets : BaseUnityPlugin
{
	public class WeaponSetManager : MonoBehaviour
	{
		public class WeaponSet
		{
			public Item PrimaryWeapon { get; set; }

			public Item OffhandItem { get; set; }

			public int PrimaryQuality { get; set; }

			public int OffhandQuality { get; set; }

			public WeaponSet(Item primaryWeapon, int primaryQuality, Item offhandItem, int offhandQuality)
			{
				PrimaryWeapon = primaryWeapon;
				PrimaryQuality = primaryQuality;
				OffhandItem = offhandItem;
				OffhandQuality = offhandQuality;
			}
		}

		public class WeaponSetSaveData
		{
			public WeaponSetType ActiveSet;

			public string PrimaryItemID;

			public int PrimaryQuality;

			public string OffhandItemID;

			public int OffhandQuality;
		}

		public enum WeaponSetType
		{
			SetA,
			SetB
		}

		public string SaveFolderPath;

		private WeaponSet _inactiveSet;

		public ItemIcon PrimarySlot;

		public ItemIcon OffhandSlot;

		private GameObject playerInvUI;

		private GameObject primarySlotUI;

		private GameObject _tagGroup;

		private Button _tagButtonSetA;

		private Button _tagButtonSetB;

		private bool uiInit;

		public static WeaponSetManager Instance { get; private set; }

		public WeaponSetType ActiveSet { get; private set; }

		private void Awake()
		{
			if ((Object)(object)Instance != (Object)null && (Object)(object)Instance != (Object)(object)this)
			{
				Object.Destroy((Object)(object)this);
				return;
			}
			_inactiveSet = new WeaponSet(GameData.PlayerInv.Empty, 1, GameData.PlayerInv.Empty, 1);
			uiInit = false;
			Instance = this;
		}

		private void OnDestroy()
		{
			SaveData();
			if ((Object)(object)_tagButtonSetA != (Object)null)
			{
				((UnityEventBase)_tagButtonSetA.onClick).RemoveAllListeners();
				Object.Destroy((Object)(object)((Component)_tagButtonSetA).gameObject);
			}
			if ((Object)(object)_tagButtonSetB != (Object)null)
			{
				((UnityEventBase)_tagButtonSetB.onClick).RemoveAllListeners();
				Object.Destroy((Object)(object)((Component)_tagButtonSetB).gameObject);
			}
			if ((Object)(object)_tagGroup != (Object)null)
			{
				Object.Destroy((Object)(object)_tagGroup);
			}
			SaveFolderPath = null;
			PrimarySlot = null;
			OffhandSlot = null;
			Instance = null;
			modInitialized = false;
			playerInvUI = null;
			primarySlotUI = null;
			_tagGroup = null;
			_tagButtonSetA = null;
			_tagButtonSetB = null;
			_inactiveSet = null;
			uiInit = false;
		}

		private void Update()
		{
			if (!uiInit)
			{
				if ((Object)(object)playerInvUI == (Object)null)
				{
					playerInvUI = GameObject.Find("UI/UIElements/InvPar/PlayerInv");
				}
				if ((Object)(object)primarySlotUI == (Object)null)
				{
					primarySlotUI = GameObject.Find("UI/UIElements/InvPar/PlayerInv/Primary");
				}
				if ((Object)(object)playerInvUI != (Object)null && (Object)(object)primarySlotUI != (Object)null && (Object)(object)_tagGroup == (Object)null)
				{
					CreateSetToggleUI(primarySlotUI.transform.parent);
					uiInit = true;
				}
			}
		}

		private void CreateSetToggleUI(Transform parent)
		{
			//IL_0019: Unknown result type (might be due to invalid IL or missing references)
			//IL_0023: Expected O, but got Unknown
			//IL_00ae: Unknown result type (might be due to invalid IL or missing references)
			//IL_00b3: Unknown result type (might be due to invalid IL or missing references)
			//IL_00c0: Unknown result type (might be due to invalid IL or missing references)
			//IL_00c7: 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_00ed: Unknown result type (might be due to invalid IL or missing references)
			//IL_00fc: Unknown result type (might be due to invalid IL or missing references)
			//IL_0101: Unknown result type (might be due to invalid IL or missing references)
			_tagGroup = new GameObject("WeaponSetToggleUI", new Type[1] { typeof(RectTransform) });
			_tagGroup.transform.SetParent(parent, false);
			HorizontalLayoutGroup obj = _tagGroup.AddComponent<HorizontalLayoutGroup>();
			((HorizontalOrVerticalLayoutGroup)obj).spacing = 0f;
			((LayoutGroup)obj).childAlignment = (TextAnchor)4;
			((HorizontalOrVerticalLayoutGroup)obj).childControlHeight = false;
			((HorizontalOrVerticalLayoutGroup)obj).childControlWidth = false;
			((HorizontalOrVerticalLayoutGroup)obj).childForceExpandHeight = false;
			((HorizontalOrVerticalLayoutGroup)obj).childForceExpandWidth = false;
			LayoutElement obj2 = _tagGroup.AddComponent<LayoutElement>();
			obj2.preferredWidth = 20f;
			obj2.preferredHeight = 25f;
			obj2.flexibleWidth = 0f;
			obj2.flexibleHeight = 0f;
			Vector2 anchorMin = primarySlotUI.GetComponent<RectTransform>().anchorMin;
			RectTransform component = _tagGroup.GetComponent<RectTransform>();
			component.anchorMin = anchorMin;
			component.anchorMax = anchorMin;
			component.sizeDelta = new Vector2(50f, 20f);
			component.anchoredPosition = primarySlotUI.GetComponent<RectTransform>().anchoredPosition + new Vector2(0f, 45f);
			_tagButtonSetA = CreateSetButton("1", 0, _tagGroup.transform);
			_tagButtonSetB = CreateSetButton("2", 1, _tagGroup.transform);
			UpdateToggleHighlight();
			_tagGroup.SetActive(true);
		}

		private Button CreateSetButton(string label, int index, Transform parent)
		{
			//IL_003b: Unknown result type (might be due to invalid IL or missing references)
			//IL_0041: Expected O, but got Unknown
			//IL_006e: Unknown result type (might be due to invalid IL or missing references)
			//IL_009b: Unknown result type (might be due to invalid IL or missing references)
			//IL_00a0: Unknown result type (might be due to invalid IL or missing references)
			//IL_00c7: Unknown result type (might be due to invalid IL or missing references)
			//IL_0106: Unknown result type (might be due to invalid IL or missing references)
			//IL_015f: Unknown result type (might be due to invalid IL or missing references)
			//IL_0174: Unknown result type (might be due to invalid IL or missing references)
			//IL_0189: Unknown result type (might be due to invalid IL or missing references)
			//IL_011e: Unknown result type (might be due to invalid IL or missing references)
			//IL_0133: Unknown result type (might be due to invalid IL or missing references)
			//IL_0148: Unknown result type (might be due to invalid IL or missing references)
			//IL_01ad: Unknown result type (might be due to invalid IL or missing references)
			//IL_01b7: Expected O, but got Unknown
			GameObject val = new GameObject(label, new Type[3]
			{
				typeof(RectTransform),
				typeof(Image),
				typeof(Button)
			});
			val.transform.SetParent(parent, false);
			Image component = primarySlotUI.GetComponent<Image>();
			Image component2 = val.GetComponent<Image>();
			((Graphic)component2).material = ((Graphic)component).material;
			((Graphic)component2).color = ((Graphic)component).color;
			component2.sprite = component.sprite;
			GameObject val2 = new GameObject("Text", new Type[1] { typeof(TextMeshProUGUI) });
			val2.transform.SetParent(val.transform, false);
			TextMeshProUGUI component3 = val2.GetComponent<TextMeshProUGUI>();
			((TMP_Text)component3).rectTransform.sizeDelta = new Vector2(20f, 25f);
			((Graphic)component3).raycastTarget = false;
			((TMP_Text)component3).text = label;
			((TMP_Text)component3).fontSize = 14f;
			((TMP_Text)component3).alignment = (TextAlignmentOptions)514;
			RectTransform component4 = val.GetComponent<RectTransform>();
			component4.sizeDelta = new Vector2(20f, 25f);
			if (index == 0)
			{
				component4.anchorMin = new Vector2(1f, 0f);
				component4.anchorMax = new Vector2(1f, 0f);
				component4.pivot = new Vector2(1f, 0f);
			}
			else
			{
				component4.anchorMin = new Vector2(0f, 0f);
				component4.anchorMax = new Vector2(0f, 0f);
				component4.pivot = new Vector2(0f, 0f);
			}
			WeaponSetType targetSet = (WeaponSetType)index;
			Button component5 = val.GetComponent<Button>();
			((UnityEvent)component5.onClick).AddListener((UnityAction)delegate
			{
				if (ActiveSet != targetSet)
				{
					SwapSets();
				}
			});
			return component5;
		}

		private void UpdateToggleHighlight()
		{
			//IL_0037: 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_0079: 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)
			if ((Object)(object)_tagButtonSetA != (Object)null)
			{
				((Component)_tagButtonSetA).transform.localScale = (Vector3)((ActiveSet == WeaponSetType.SetA) ? new Vector3(1.2f, 1.2f, 1f) : Vector3.one);
			}
			if ((Object)(object)_tagButtonSetB != (Object)null)
			{
				((Component)_tagButtonSetB).transform.localScale = (Vector3)((ActiveSet == WeaponSetType.SetB) ? new Vector3(1.2f, 1.2f, 1f) : Vector3.one);
			}
		}

		public void SwapSets()
		{
			if (!((Object)(object)PrimarySlot == (Object)null) && !((Object)(object)OffhandSlot == (Object)null))
			{
				Item myItem = PrimarySlot.MyItem;
				int quantity = PrimarySlot.Quantity;
				Item myItem2 = OffhandSlot.MyItem;
				int quantity2 = OffhandSlot.Quantity;
				PrimarySlot.MyItem = _inactiveSet.PrimaryWeapon;
				if ((Object)(object)PrimarySlot.MyItem != (Object)null)
				{
					PrimarySlot.Quantity = _inactiveSet.PrimaryQuality;
				}
				OffhandSlot.MyItem = _inactiveSet.OffhandItem;
				if ((Object)(object)OffhandSlot.MyItem != (Object)null)
				{
					OffhandSlot.Quantity = _inactiveSet.OffhandQuality;
				}
				_inactiveSet.PrimaryWeapon = myItem;
				_inactiveSet.PrimaryQuality = quantity;
				_inactiveSet.OffhandItem = myItem2;
				_inactiveSet.OffhandQuality = quantity2;
				PrimarySlot.UpdateSlotImage();
				OffhandSlot.UpdateSlotImage();
				GameData.PlayerInv.UpdatePlayerInventory();
				SaveData();
				ActiveSet = ((ActiveSet == WeaponSetType.SetA) ? WeaponSetType.SetB : WeaponSetType.SetA);
				UpdateToggleHighlight();
				UpdateSocialLog.LogAdd($"Equipped weapon set {(int)(ActiveSet + 1)}", "yellow");
			}
		}

		public void SaveData()
		{
			WeaponSetSaveData weaponSetSaveData = new WeaponSetSaveData
			{
				ActiveSet = ActiveSet,
				PrimaryItemID = (((BaseScriptableObject)(_inactiveSet.PrimaryWeapon?)).Id ?? string.Empty),
				PrimaryQuality = _inactiveSet.PrimaryQuality,
				OffhandItemID = (((BaseScriptableObject)(_inactiveSet.OffhandItem?)).Id ?? string.Empty),
				OffhandQuality = _inactiveSet.OffhandQuality
			};
			try
			{
				string contents = JsonUtility.ToJson((object)weaponSetSaveData, true);
				File.WriteAllText(SaveFolderPath, contents);
			}
			catch (Exception ex)
			{
				Log.LogError((object)("[WeaponSets] Failed to save weapon set data: " + ex.Message));
			}
		}

		public void LoadSavedData()
		{
			try
			{
				WeaponSetSaveData weaponSetSaveData = JsonUtility.FromJson<WeaponSetSaveData>(File.ReadAllText(SaveFolderPath));
				if (weaponSetSaveData == null)
				{
					Log.LogError((object)"[WeaponSets] Failed to parse saved data.");
				}
				_inactiveSet = new WeaponSet(GameData.ItemDB.GetItemByID(weaponSetSaveData.PrimaryItemID), weaponSetSaveData.PrimaryQuality, GameData.ItemDB.GetItemByID(weaponSetSaveData.OffhandItemID), weaponSetSaveData.OffhandQuality);
				ActiveSet = weaponSetSaveData.ActiveSet;
			}
			catch (Exception ex)
			{
				Log.LogError((object)("[WeaponSets] Error loading saved data: " + ex.Message));
			}
		}
	}

	internal const string ModName = "WeaponSets";

	internal const string ModVersion = "1.0.1";

	internal const string ModDescription = "Weapon Sets";

	internal const string Author = "Brad522";

	private const string ModGUID = "Brad522.WeaponSets";

	private ConfigEntry<KeyboardShortcut> _swapKey;

	private bool _swapKeyDownLastFrame;

	internal static ManualLogSource Log;

	public static bool modInitialized;

	public void Awake()
	{
		//IL_0029: Unknown result type (might be due to invalid IL or missing references)
		Log = ((BaseUnityPlugin)this).Logger;
		modInitialized = false;
		_swapKey = ((BaseUnityPlugin)this).Config.Bind<KeyboardShortcut>("General", "Hotkey", new KeyboardShortcut((KeyCode)122, Array.Empty<KeyCode>()), "Hotkey used to swap weapon sets. Default is Z.");
		_swapKeyDownLastFrame = false;
	}

	public void OnDestroy()
	{
		if ((Object)(object)GameData.PlayerInv != (Object)null)
		{
			WeaponSetManager component = ((Component)GameData.PlayerInv).gameObject.GetComponent<WeaponSetManager>();
			if ((Object)(object)component != (Object)null)
			{
				Object.DestroyImmediate((Object)(object)component);
			}
		}
		Log = null;
		modInitialized = false;
		_swapKey = null;
		_swapKeyDownLastFrame = false;
	}

	public void Update()
	{
		//IL_0054: Unknown result type (might be due to invalid IL or missing references)
		if (!modInitialized)
		{
			modInitialized = TryInit();
			if (!modInitialized)
			{
				return;
			}
			if (!string.IsNullOrEmpty(WeaponSetManager.Instance.SaveFolderPath) && File.Exists(WeaponSetManager.Instance.SaveFolderPath))
			{
				WeaponSetManager.Instance.LoadSavedData();
			}
		}
		if (modInitialized && WasJustPressed(_swapKey.Value, ref _swapKeyDownLastFrame))
		{
			WeaponSetManager.Instance.SwapSets();
		}
	}

	private bool TryInit()
	{
		if (GameData.InCharSelect)
		{
			return false;
		}
		if ((Object)(object)GameData.PlayerInv == (Object)null || (Object)(object)GameData.PlayerStats == (Object)null)
		{
			return false;
		}
		if ((Object)(object)((Component)GameData.PlayerInv).gameObject.GetComponent<WeaponSetManager>() == (Object)null)
		{
			((Component)GameData.PlayerInv).gameObject.AddComponent<WeaponSetManager>();
		}
		if (WeaponSetManager.Instance.SaveFolderPath == null)
		{
			WeaponSetManager.Instance.SaveFolderPath = GetSavePath(GameData.PlayerStats.MyName);
		}
		if ((Object)(object)WeaponSetManager.Instance.PrimarySlot == (Object)null)
		{
			WeaponSetManager.Instance.PrimarySlot = FindEquipmentSlot((SlotType)13);
		}
		if ((Object)(object)WeaponSetManager.Instance.OffhandSlot == (Object)null)
		{
			WeaponSetManager.Instance.OffhandSlot = FindEquipmentSlot((SlotType)14);
		}
		return CheckInit();
	}

	private bool CheckInit()
	{
		if (WeaponSetManager.Instance.SaveFolderPath != null && (Object)(object)WeaponSetManager.Instance.PrimarySlot != (Object)null)
		{
			return (Object)(object)WeaponSetManager.Instance.OffhandSlot != (Object)null;
		}
		return false;
	}

	public ItemIcon FindEquipmentSlot(SlotType slotType)
	{
		//IL_001b: Unknown result type (might be due to invalid IL or missing references)
		//IL_0020: Unknown result type (might be due to invalid IL or missing references)
		foreach (ItemIcon equipmentSlot in GameData.PlayerInv.EquipmentSlots)
		{
			if (equipmentSlot.ThisSlotType == slotType)
			{
				return equipmentSlot;
			}
		}
		return null;
	}

	private string GetSavePath(string characterName)
	{
		string text = Path.Combine(Path.GetDirectoryName(((BaseUnityPlugin)this).Info.Location), "SaveData");
		if (!Directory.Exists(text))
		{
			Directory.CreateDirectory(text);
		}
		string path = characterName + "_WeaponSet.json";
		return Path.Combine(text, path);
	}

	private bool WasJustPressed(KeyboardShortcut shortcut, ref bool lastState)
	{
		bool flag = ((KeyboardShortcut)(ref shortcut)).IsDown();
		bool result = flag && !lastState;
		lastState = flag;
		return result;
	}
}