Decompiled source of ItemTrashandDuplicateKeys v1.0.1

plugins/ItemTrashKey.dll

Decompiled a day ago
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Linq;
using System.Reflection;
using System.Runtime.CompilerServices;
using System.Runtime.Versioning;
using System.Security;
using System.Security.Permissions;
using System.Text.RegularExpressions;
using BepInEx;
using BepInEx.Configuration;
using BepInEx.Logging;
using HarmonyLib;
using Microsoft.CodeAnalysis;
using Photon.Pun;
using UnityEngine;
using UnityEngine.InputSystem;
using UnityEngine.InputSystem.Controls;
using UnityEngine.SceneManagement;

[assembly: CompilationRelaxations(8)]
[assembly: RuntimeCompatibility(WrapNonExceptionThrows = true)]
[assembly: Debuggable(DebuggableAttribute.DebuggingModes.IgnoreSymbolStoreSequencePoints)]
[assembly: TargetFramework(".NETStandard,Version=v2.1", FrameworkDisplayName = ".NET Standard 2.1")]
[assembly: IgnoresAccessChecksTo("")]
[assembly: AssemblyCompany("Kai")]
[assembly: AssemblyConfiguration("Release")]
[assembly: AssemblyFileVersion("1.0.0.0")]
[assembly: AssemblyInformationalVersion("1.0.0")]
[assembly: AssemblyProduct("ItemTrashKey")]
[assembly: AssemblyTitle("ItemTrashKey")]
[assembly: SecurityPermission(SecurityAction.RequestMinimum, SkipVerification = true)]
[assembly: AssemblyVersion("1.0.0.0")]
[module: UnverifiableCode]
[module: RefSafetyRules(11)]
namespace Microsoft.CodeAnalysis
{
	[CompilerGenerated]
	[Microsoft.CodeAnalysis.Embedded]
	internal sealed class EmbeddedAttribute : Attribute
	{
	}
}
namespace System.Runtime.CompilerServices
{
	[CompilerGenerated]
	[Microsoft.CodeAnalysis.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]
	[Microsoft.CodeAnalysis.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;
		}
	}
	[CompilerGenerated]
	[Microsoft.CodeAnalysis.Embedded]
	[AttributeUsage(AttributeTargets.Module, AllowMultiple = false, Inherited = false)]
	internal sealed class RefSafetyRulesAttribute : Attribute
	{
		public readonly int Version;

		public RefSafetyRulesAttribute(int P_0)
		{
			Version = P_0;
		}
	}
}
namespace ItemTrashKey
{
	[BepInPlugin("Kai.ItemTrashKey", "ItemTrashKey", "1.5")]
	public class ItemTrashKey : BaseUnityPlugin
	{
		public static ConfigEntry<bool> AllowEverywhere;

		public static ConfigEntry<Key> TrashKey;

		public static ConfigEntry<Key> DuplicateKey;

		public static ConfigEntry<float> HoldTimeSetting;

		public static ConfigEntry<bool> SaveDataUpdate;

		public static ConfigEntry<bool> EnableDebugLog;

		private float holdTimer;

		private float guideSwitchTimer;

		private float guideSwitchInterval = 2f;

		private bool isGuideDisplayed = true;

		internal static ItemTrashKey Instance { get; private set; }

		internal static ManualLogSource Logger => Instance._logger;

		private ManualLogSource _logger => ((BaseUnityPlugin)this).Logger;

		internal Harmony? Harmony { get; set; }

		private void Awake()
		{
			Instance = this;
			((Component)this).gameObject.transform.parent = null;
			((Object)((Component)this).gameObject).hideFlags = (HideFlags)61;
			AllowEverywhere = ((BaseUnityPlugin)this).Config.Bind<bool>("TrashKey", "AllowEverywhere", false, "Museum / Manor / Arctic / Wizard でも削除/複製を許可する");
			TrashKey = ((BaseUnityPlugin)this).Config.Bind<Key>("Keys", "TrashKey", (Key)71, "キー設定: 削除用キー");
			DuplicateKey = ((BaseUnityPlugin)this).Config.Bind<Key>("Keys", "DuplicateKey", (Key)70, "キー設定: 複製用キー");
			HoldTimeSetting = ((BaseUnityPlugin)this).Config.Bind<float>("General", "HoldTime", 1f, "長押し時間: アイテムを削除または複製するために必要な長押し時間 (秒)");
			SaveDataUpdate = ((BaseUnityPlugin)this).Config.Bind<bool>("General", "SaveDataUpdate", true, "複製および削除時にセーブデータへ反映するかどうか");
			EnableDebugLog = ((BaseUnityPlugin)this).Config.Bind<bool>("General", "EnableDebugLog", false, "ログ出力を有効にするかどうか");
			Patch();
			Logger.LogInfo((object)$"{((BaseUnityPlugin)this).Info.Metadata.GUID} v{((BaseUnityPlugin)this).Info.Metadata.Version} has loaded!");
		}

		internal void Patch()
		{
			//IL_0019: Unknown result type (might be due to invalid IL or missing references)
			//IL_001e: Unknown result type (might be due to invalid IL or missing references)
			//IL_0020: Expected O, but got Unknown
			//IL_0025: Expected O, but got Unknown
			if (Harmony == null)
			{
				Harmony val = new Harmony(((BaseUnityPlugin)this).Info.Metadata.GUID);
				Harmony val2 = val;
				Harmony = val;
			}
			Harmony.PatchAll();
		}

		private static bool IsLobbyOrTruck()
		{
			//IL_004d: 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)
			try
			{
				RunManager instance = RunManager.instance;
				if ((Object)(object)instance?.levelCurrent != (Object)null)
				{
					string text = ((Object)instance.levelCurrent).name ?? "";
					return text.Contains("Lobby") || text.Contains("Truck");
				}
				Scene activeScene = SceneManager.GetActiveScene();
				string text2 = ((Scene)(ref activeScene)).name ?? "";
				return text2.Contains("Lobby") || text2.Contains("Truck");
			}
			catch
			{
				return false;
			}
		}

		public static void DebugLog(string msg)
		{
			if (EnableDebugLog.Value)
			{
				Logger.LogInfo((object)msg);
			}
		}

		private void RemoveItemCompletely(ItemAttributes attrs)
		{
			if (!SemiFunc.IsMasterClientOrSingleplayer())
			{
				return;
			}
			StatsManager instance = StatsManager.instance;
			if (!((Object)(object)instance == (Object)null))
			{
				string text = FindDictionaryKeyFromAttributes(attrs);
				if (text == null)
				{
					Logger.LogError((object)"[Trash] 削除対象のキーが特定できませんでした。");
					return;
				}
				instance.ItemRemove(attrs.instanceName);
				instance.SaveFileSave();
				DebugLog("[Trash] 完全削除: " + attrs.instanceName + " / " + text);
			}
		}

		private static string NormalizeNameKey(string s)
		{
			if (string.IsNullOrWhiteSpace(s))
			{
				return "";
			}
			s = s.ToLowerInvariant();
			s = s.Replace("(clone)", "");
			s = Regex.Replace(s, "[\\s_\\-]+", "");
			return s;
		}

		private string? FindDictionaryKeyFromAttributes(ItemAttributes attrs)
		{
			if ((Object)(object)attrs == (Object)null)
			{
				return null;
			}
			Dictionary<string, Item> dictionary = StatsManager.instance?.itemDictionary;
			if (dictionary == null)
			{
				return null;
			}
			string text = NormalizeNameKey(attrs.itemName);
			string text2 = NormalizeNameKey(((Object)attrs).name);
			foreach (string key in dictionary.Keys)
			{
				string text3 = NormalizeNameKey(key);
				if (text3 == text || text3 == text2)
				{
					return key;
				}
				if (text3.Contains(text) || text.Contains(text3))
				{
					return key;
				}
				if (text3.Contains(text2) || text2.Contains(text3))
				{
					return key;
				}
			}
			return null;
		}

		private void SpawnItemFromAttributes(ItemAttributes sourceAttrs)
		{
			string text = FindDictionaryKeyFromAttributes(sourceAttrs);
			if (text == null)
			{
				Logger.LogError((object)("[Duplicate] 辞書キーが特定できませんでした: display='" + sourceAttrs.itemName + "', prefab='" + ((Object)sourceAttrs).name + "'"));
			}
			else
			{
				SpawnItemByDictionaryKey(text);
			}
		}

		private void SpawnItemByDictionaryKey(string dictKey)
		{
			//IL_008b: Unknown result type (might be due to invalid IL or missing references)
			//IL_0091: 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_00a5: 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_00b4: Unknown result type (might be due to invalid IL or missing references)
			//IL_00b9: Unknown result type (might be due to invalid IL or missing references)
			//IL_00bb: Unknown result type (might be due to invalid IL or missing references)
			//IL_00c1: 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_00cb: Unknown result type (might be due to invalid IL or missing references)
			//IL_00f4: Unknown result type (might be due to invalid IL or missing references)
			//IL_00f5: Unknown result type (might be due to invalid IL or missing references)
			//IL_00df: Unknown result type (might be due to invalid IL or missing references)
			//IL_00e0: Unknown result type (might be due to invalid IL or missing references)
			StatsManager instance = StatsManager.instance;
			if ((Object)(object)instance == (Object)null)
			{
				Logger.LogError((object)"[Duplicate] StatsManager.instance が null です。");
				return;
			}
			if (!instance.itemDictionary.TryGetValue(dictKey, out var value))
			{
				Logger.LogError((object)("[Duplicate] itemDictionary に '" + dictKey + "' が見つかりません。"));
				return;
			}
			Camera main = Camera.main;
			object obj = ((main != null) ? ((Component)main).transform : null);
			if (obj == null)
			{
				PlayerController instance2 = PlayerController.instance;
				obj = ((instance2 != null) ? ((Component)instance2).transform : null);
			}
			Transform val = (Transform)obj;
			if ((Object)(object)val == (Object)null)
			{
				Logger.LogError((object)"[Duplicate] カメラまたはプレイヤーが見つかりません。");
				return;
			}
			Vector3 val2 = val.position + val.forward * 3f + Vector3.up * 0.5f;
			Quaternion val3 = val.rotation * value.spawnRotationOffset;
			GameObject val4 = (SemiFunc.IsMultiplayer() ? PhotonNetwork.InstantiateRoomObject(value.prefab.ResourcePath, val2, val3, (byte)0, (object[])null) : Object.Instantiate<GameObject>(value.prefab.Prefab, val2, val3));
			ItemAttributes component = val4.GetComponent<ItemAttributes>();
			if ((Object)(object)component == (Object)null)
			{
				Logger.LogError((object)"[Duplicate] 生成されたアイテムに ItemAttributes がありません。");
			}
			else if (SaveDataUpdate.Value)
			{
				instance.ItemPurchase(dictKey);
				int num = (((Object)(object)component.photonView != (Object)null) ? component.photonView.ViewID : (-1));
				instance.ItemAdd(dictKey, component, num);
				instance.SaveFileSave();
				DebugLog("[Duplicate] '" + dictKey + "' を複製し、インスタンス '" + component.instanceName + "' として登録&セーブしました。");
			}
			else
			{
				DebugLog("[Duplicate] '" + dictKey + "' をスポーン(セーブ更新なし)。インスタンス名: " + component.instanceName);
			}
		}

		private void DuplicateValuable(ValuableObject val)
		{
			//IL_0053: Unknown result type (might be due to invalid IL or missing references)
			//IL_0059: 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)
			//IL_0068: Unknown result type (might be due to invalid IL or missing references)
			//IL_006d: Unknown result type (might be due to invalid IL or missing references)
			//IL_0073: Unknown result type (might be due to invalid IL or missing references)
			//IL_0079: Expected O, but got Unknown
			//IL_007f: Unknown result type (might be due to invalid IL or missing references)
			//IL_008b: Unknown result type (might be due to invalid IL or missing references)
			PrefabRef val2 = FindValuablePrefab(val);
			if (val2 == null)
			{
				DebugLog("[Duplicate Valuable] Skip: PrefabRef not found for " + ((Object)val).name);
				return;
			}
			Camera main = Camera.main;
			object obj = ((main != null) ? ((Component)main).transform : null);
			if (obj == null)
			{
				PlayerController instance = PlayerController.instance;
				obj = ((instance != null) ? ((Component)instance).transform : null);
			}
			Transform val3 = (Transform)obj;
			if (!((Object)(object)val3 == (Object)null))
			{
				Vector3 position = val3.position + val3.forward * 2f;
				GameObject val4 = new GameObject("[ItemTrashKeyValuableVolume]");
				val4.transform.position = position;
				val4.transform.rotation = Quaternion.identity;
				ValuableVolume val5 = val4.AddComponent<ValuableVolume>();
				ValuableDirector.instance.Spawn(val2, val5, val2.ResourcePath);
				DebugLog("[Duplicate Valuable] Spawned by director: " + val2.PrefabName);
				Object.Destroy((Object)(object)val4);
			}
		}

		private PrefabRef? FindValuablePrefab(ValuableObject val)
		{
			string text = NormalizeNameKey(((Object)val).name);
			foreach (LevelValuables valuablePreset in RunManager.instance.levelCurrent.ValuablePresets)
			{
				foreach (PrefabRef item in valuablePreset.tiny.Concat(valuablePreset.small).Concat(valuablePreset.medium).Concat(valuablePreset.big)
					.Concat(valuablePreset.wide)
					.Concat(valuablePreset.tall)
					.Concat(valuablePreset.veryTall))
				{
					string text2 = NormalizeNameKey(item.PrefabName);
					if (text2 == text || text.Contains(text2) || text2.Contains(text))
					{
						return item;
					}
				}
			}
			return null;
		}

		private void DeleteValuable(ValuableObject val)
		{
			if (SemiFunc.IsMultiplayer())
			{
				PhotonNetwork.Destroy(((Component)val).gameObject);
			}
			else
			{
				Object.Destroy((Object)(object)((Component)val).gameObject);
			}
			DebugLog("[Trash Valuable] " + ((Object)val).name);
		}

		private void Update()
		{
			//IL_015a: Unknown result type (might be due to invalid IL or missing references)
			//IL_020c: Unknown result type (might be due to invalid IL or missing references)
			//IL_018a: Unknown result type (might be due to invalid IL or missing references)
			//IL_0137: 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)
			//IL_010d: Unknown result type (might be due to invalid IL or missing references)
			//IL_011c: Unknown result type (might be due to invalid IL or missing references)
			//IL_0239: Unknown result type (might be due to invalid IL or missing references)
			if (!SemiFunc.IsMasterClientOrSingleplayer())
			{
				return;
			}
			if (!AllowEverywhere.Value && !IsLobbyOrTruck())
			{
				holdTimer = 0f;
				return;
			}
			PhysGrabObject val = PhysGrabber.instance?.grabbedPhysGrabObject;
			if ((Object)(object)val == (Object)null)
			{
				holdTimer = 0f;
			}
			else
			{
				if ((Object)(object)((Component)val).GetComponent<PlayerDeathHead>() != (Object)null || (Object)(object)((Component)val).GetComponent<PlayerAvatar>() != (Object)null)
				{
					return;
				}
				ItemAttributes component = ((Component)val).GetComponent<ItemAttributes>();
				ValuableObject component2 = ((Component)val).GetComponent<ValuableObject>();
				if ((Object)(object)component == (Object)null && (Object)(object)component2 == (Object)null)
				{
					holdTimer = 0f;
					return;
				}
				string text = (((Object)(object)component != (Object)null) ? component.instanceName : "(valuable)");
				guideSwitchTimer += Time.deltaTime;
				if (guideSwitchTimer >= guideSwitchInterval)
				{
					guideSwitchTimer = 0f;
					isGuideDisplayed = !isGuideDisplayed;
				}
				if (holdTimer == 0f)
				{
					if (isGuideDisplayed)
					{
						ItemInfoExtraUI.instance.ItemInfoText($"[{TrashKey.Value}] HOLD TO DESTROY", Color.red);
					}
					else
					{
						ItemInfoExtraUI.instance.ItemInfoText($"[{DuplicateKey.Value}] HOLD TO DUPLICATE", Color.green);
					}
				}
				if (((ButtonControl)Keyboard.current[TrashKey.Value]).isPressed)
				{
					holdTimer += Time.deltaTime;
					ItemInfoExtraUI.instance.ItemInfoText("DESTROYING...", Color.red);
					if (!(holdTimer >= HoldTimeSetting.Value))
					{
						return;
					}
					if ((Object)(object)component != (Object)null)
					{
						if (SaveDataUpdate.Value)
						{
							RemoveItemCompletely(component);
						}
						if (SemiFunc.IsMultiplayer())
						{
							PhotonNetwork.Destroy(((Component)val).gameObject);
						}
						else
						{
							Object.Destroy((Object)(object)((Component)val).gameObject);
						}
					}
					else if ((Object)(object)component2 != (Object)null)
					{
						DeleteValuable(component2);
					}
					holdTimer = 0f;
				}
				else if (((ButtonControl)Keyboard.current[DuplicateKey.Value]).isPressed)
				{
					holdTimer += Time.deltaTime;
					ItemInfoExtraUI.instance.ItemInfoText("DUPLICATING...", Color.green);
					if (holdTimer >= HoldTimeSetting.Value)
					{
						if ((Object)(object)component != (Object)null)
						{
							SpawnItemFromAttributes(component);
						}
						else if ((Object)(object)component2 != (Object)null)
						{
							DuplicateValuable(component2);
						}
						holdTimer = 0f;
					}
				}
				else
				{
					holdTimer = 0f;
				}
			}
		}
	}
}