Decompiled source of GetMyTrophyBack v1.0.11

Landoria.GetMyTrophyBack.dll

Decompiled 3 days ago
using System;
using System.Collections;
using System.Collections.Generic;
using System.Diagnostics;
using System.Linq;
using System.Reflection;
using System.Runtime.CompilerServices;
using System.Runtime.InteropServices;
using System.Runtime.Versioning;
using BepInEx;
using BepInEx.Logging;
using HarmonyLib;
using Landoria.SharedLib;
using Splatform;
using TMPro;
using UnityEngine;

[assembly: CompilationRelaxations(8)]
[assembly: RuntimeCompatibility(WrapNonExceptionThrows = true)]
[assembly: Debuggable(DebuggableAttribute.DebuggingModes.IgnoreSymbolStoreSequencePoints)]
[assembly: AssemblyTitle("Landoria.GetMyTrophyBack")]
[assembly: AssemblyDescription("Returns trophies after guardian power selection.")]
[assembly: AssemblyConfiguration("")]
[assembly: AssemblyCompany("Landoria")]
[assembly: AssemblyProduct("Landoria.GetMyTrophyBack")]
[assembly: AssemblyCopyright("Copyright © 2026 End3rbyte")]
[assembly: AssemblyTrademark("")]
[assembly: ComVisible(false)]
[assembly: Guid("66592EBD-C28B-46B8-996D-6D4BF2FB210F")]
[assembly: AssemblyFileVersion("1.0.11")]
[assembly: AssemblyInformationalVersion("1.0.11")]
[assembly: TargetFramework(".NETFramework,Version=v4.8", FrameworkDisplayName = "")]
[assembly: AssemblyVersion("1.0.11.14005")]
namespace Landoria.GetMyTrophyBack
{
	[BepInPlugin("Landoria.GetMyTrophyBack", "Landoria.GetMyTrophyBack", "1.0.11")]
	public sealed class GetMyTrophyBackPlugin : LandoriaPlugin
	{
		private const string PluginGuid = "Landoria.GetMyTrophyBack";

		private const string PluginName = "Landoria.GetMyTrophyBack";

		private const string PluginVersion = "1.0.11";

		internal static ModLog Log { get; private set; }

		private void Awake()
		{
			Log = InitializePlugin("Landoria.GetMyTrophyBack");
			Log.LogInfo("Landoria.GetMyTrophyBack 1.0.11 is loaded.");
		}

		private void OnDestroy()
		{
			Log?.LogInfo("Landoria.GetMyTrophyBack 1.0.11 is unloaded.");
			ShutdownPlugin();
			Log = null;
		}
	}
	[HarmonyPatch(typeof(ItemStand), "GetHoverText")]
	internal static class ShowInteractionForActivePowerPatch
	{
		private static void Postfix(ItemStand __instance, ref string __result)
		{
			//IL_0058: Unknown result type (might be due to invalid IL or missing references)
			Player localPlayer = Player.m_localPlayer;
			if (!((Object)(object)localPlayer == (Object)null) && !((Object)(object)__instance.m_guardianPower == (Object)null) && __instance.HaveAttachment() && !__instance.m_canBeRemoved && !((MonoBehaviour)__instance).IsInvoking("DelayedPowerActivation") && !(localPlayer.GetGuardianPowerName() != ((Object)__instance.m_guardianPower).name) && PrivateArea.CheckAccess(((Component)__instance).transform.position, 0f, false, false))
			{
				DropTrophyForActivePowerPatch.EnsureInteractionScheduled(__instance);
				if (DropTrophyForActivePowerPatch.IsReady(__instance))
				{
					string tooltipString = __instance.m_guardianPower.GetTooltipString();
					__result = Localization.instance.Localize("<color=orange>" + __instance.m_guardianPower.m_name + "</color>\n" + tooltipString + "\n\n[<color=yellow><b>$KEY_Use</b></color>] $piece_use");
				}
			}
		}
	}
	[HarmonyPatch(typeof(ItemStand), "Interact")]
	internal static class DropTrophyForActivePowerPatch
	{
		private const float InteractionDelaySeconds = 15f;

		private static readonly HashSet<ItemStand> Waiting = new HashSet<ItemStand>();

		private static readonly HashSet<ItemStand> Ready = new HashSet<ItemStand>();

		internal static bool IsReady(ItemStand itemStand)
		{
			return Ready.Contains(itemStand);
		}

		internal static void EnsureInteractionScheduled(ItemStand itemStand)
		{
			if (!Ready.Contains(itemStand) && Waiting.Add(itemStand))
			{
				((MonoBehaviour)itemStand).StartCoroutine(EnableInteractionAfterDelay(itemStand));
				GetMyTrophyBackPlugin.Log.LogDebug("Scheduled trophy interaction for active power " + ((Object)itemStand.m_guardianPower).name + ".");
			}
		}

		private static bool Prefix(ItemStand __instance, Humanoid user, bool hold, ref bool __result)
		{
			if (!hold && !((Object)(object)__instance.m_guardianPower == (Object)null) && __instance.HaveAttachment())
			{
				Player val = (Player)(object)((user is Player) ? user : null);
				if (val != null && !((Object)(object)val != (Object)(object)Player.m_localPlayer) && !(val.GetGuardianPowerName() != ((Object)__instance.m_guardianPower).name))
				{
					if (!Ready.Remove(__instance))
					{
						EnsureInteractionScheduled(__instance);
						__result = true;
						return false;
					}
					TrophyDropService.RequestDrop(__instance);
					GetMyTrophyBackPlugin.Log.LogDebug("Requested immediate trophy drop for active power " + ((Object)__instance.m_guardianPower).name + ".");
					__result = true;
					return false;
				}
			}
			return true;
		}

		private static IEnumerator EnableInteractionAfterDelay(ItemStand itemStand)
		{
			yield return (object)new WaitForSeconds(15f);
			Waiting.Remove(itemStand);
			Player localPlayer = Player.m_localPlayer;
			if ((Object)(object)itemStand == (Object)null || (Object)(object)localPlayer == (Object)null || (Object)(object)itemStand.m_guardianPower == (Object)null || !itemStand.HaveAttachment() || localPlayer.GetGuardianPowerName() != ((Object)itemStand.m_guardianPower).name)
			{
				Ready.Remove(itemStand);
				yield break;
			}
			Ready.Add(itemStand);
			GetMyTrophyBackPlugin.Log.LogDebug("Enabled trophy interaction for active power " + ((Object)itemStand.m_guardianPower).name + ".");
		}
	}
	[HarmonyPatch(typeof(ItemStand), "DelayedPowerActivation")]
	internal static class DropTrophyAfterPowerActivationPatch
	{
		private static void Postfix(ItemStand __instance)
		{
			Player localPlayer = Player.m_localPlayer;
			if (!((Object)(object)localPlayer == (Object)null) && !((Object)(object)__instance.m_guardianPower == (Object)null) && !(localPlayer.GetGuardianPowerName() != ((Object)__instance.m_guardianPower).name))
			{
				((MonoBehaviour)__instance).StartCoroutine(TrophyDropService.DropAfterDelay(__instance));
				GetMyTrophyBackPlugin.Log.LogDebug("Scheduled trophy drop for " + ((Object)__instance.m_guardianPower).name + ".");
			}
		}
	}
	[HarmonyPatch(typeof(ItemStand), "Awake")]
	internal static class RegisterTrophyDropRpcPatch
	{
		private static void Postfix(ItemStand __instance, ZNetView ___m_nview)
		{
			if (!((Object)(object)__instance.m_guardianPower == (Object)null) && !((Object)(object)___m_nview == (Object)null) && ___m_nview.GetZDO() != null)
			{
				___m_nview.Register("Landoria_DropGuardianTrophy", (Action<long>)delegate(long sender)
				{
					TrophyDropService.HandleDropRequest(__instance, ___m_nview, sender);
				});
				GetMyTrophyBackPlugin.Log.LogDebug("Registered trophy drop RPC for " + ((Object)__instance.m_guardianPower).name + ".");
			}
		}
	}
	internal static class TrophyDropService
	{
		internal const string RpcName = "Landoria_DropGuardianTrophy";

		private const float DropDelaySeconds = 5f;

		internal static IEnumerator DropAfterDelay(ItemStand itemStand)
		{
			yield return (object)new WaitForSeconds(5f);
			RequestDrop(itemStand);
		}

		internal static void RequestDrop(ItemStand itemStand)
		{
			if (!((Object)(object)itemStand == (Object)null) && itemStand.HaveAttachment())
			{
				ZNetView netView = GetNetView(itemStand);
				if ((Object)(object)netView == (Object)null || !netView.IsValid())
				{
					GetMyTrophyBackPlugin.Log.LogWarning("Could not request the trophy drop because its network view is invalid.");
				}
				else
				{
					netView.InvokeRPC("Landoria_DropGuardianTrophy", Array.Empty<object>());
				}
			}
		}

		internal static void HandleDropRequest(ItemStand itemStand, ZNetView netView, long sender)
		{
			if (netView.IsOwner() && itemStand.HaveAttachment())
			{
				ZDO zDO = netView.GetZDO();
				string text = zDO.GetString(ZDOVars.s_item, "");
				GameObject itemPrefab = ObjectDB.instance.GetItemPrefab(text);
				if ((Object)(object)itemPrefab == (Object)null)
				{
					GetMyTrophyBackPlugin.Log.LogWarning("Could not drop missing trophy prefab " + text + ".");
					return;
				}
				SpawnTrophy(itemStand, itemPrefab, zDO);
				ClearAttachment(netView, zDO);
				GetMyTrophyBackPlugin.Log.LogDebug($"Dropped trophy {text} after request by peer {sender}.");
			}
		}

		private static ZNetView GetNetView(ItemStand itemStand)
		{
			if (!Object.op_Implicit((Object)(object)itemStand.m_netViewOverride))
			{
				return ((Component)itemStand).GetComponent<ZNetView>();
			}
			return itemStand.m_netViewOverride;
		}

		private static void SpawnTrophy(ItemStand itemStand, GameObject prefab, ZDO zdo)
		{
			//IL_0013: Unknown result type (might be due to invalid IL or missing references)
			//IL_0018: Unknown result type (might be due to invalid IL or missing references)
			//IL_0019: Unknown result type (might be due to invalid IL or missing references)
			//IL_001f: Unknown result type (might be due to invalid IL or missing references)
			//IL_0024: Unknown result type (might be due to invalid IL or missing references)
			//IL_0025: Unknown result type (might be due to invalid IL or missing references)
			//IL_0066: 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_0079: Unknown result type (might be due to invalid IL or missing references)
			//IL_007f: Unknown result type (might be due to invalid IL or missing references)
			//IL_004b: Unknown result type (might be due to invalid IL or missing references)
			//IL_0055: Unknown result type (might be due to invalid IL or missing references)
			GetAttachmentOffset(prefab, out var position, out var rotation);
			Transform dropSpawnPoint = itemStand.m_dropSpawnPoint;
			GameObject obj = Object.Instantiate<GameObject>(prefab, dropSpawnPoint.position + position, dropSpawnPoint.rotation * rotation);
			obj.GetComponent<ItemDrop>().LoadFromExternalZDO(zdo);
			Rigidbody component = obj.GetComponent<Rigidbody>();
			if ((Object)(object)component != (Object)null)
			{
				component.linearVelocity = Vector3.up * 4f;
			}
			itemStand.m_effects.Create(dropSpawnPoint.position, Quaternion.identity, (Transform)null, 1f, -1, default(ZDOID));
		}

		private static void GetAttachmentOffset(GameObject prefab, out Vector3 position, out Quaternion rotation)
		{
			//IL_0001: Unknown result type (might be due to invalid IL or missing references)
			//IL_0006: Unknown result type (might be due to invalid IL or missing references)
			//IL_000c: 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_004a: Unknown result type (might be due to invalid IL or missing references)
			//IL_004f: Unknown result type (might be due to invalid IL or missing references)
			//IL_0056: 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)
			position = Vector3.zero;
			rotation = Quaternion.identity;
			Transform val = prefab.transform.Find("attach");
			if ((Object)(object)prefab.transform.Find("attachobj") != (Object)null && (Object)(object)val != (Object)null)
			{
				position = val.localPosition;
				rotation = val.localRotation;
			}
		}

		private static void ClearAttachment(ZNetView netView, ZDO zdo)
		{
			zdo.Set(ZDOVars.s_item, "");
			zdo.Set(ZDOVars.s_type, 0, false);
			netView.InvokeRPC(ZNetView.Everybody, "SetVisualItem", new object[4] { "", 0, 0, 0 });
		}
	}
}
namespace Landoria.SharedLib
{
	public abstract class LandoriaPlugin : BaseUnityPlugin
	{
		private Harmony _harmony;

		private bool _patchesApplied;

		protected ModLog InitializePlugin(string pluginGuid)
		{
			//IL_003b: Unknown result type (might be due to invalid IL or missing references)
			//IL_0045: Expected O, but got Unknown
			ModLog modLog = new ModLog(((BaseUnityPlugin)this).Logger);
			Version version = ((object)this).GetType().Assembly.GetName().Version;
			modLog.LogInfo($"AssemblyVersion: {version}.");
			EnsureSharedPatches(modLog);
			_harmony = new Harmony(pluginGuid);
			PatchOwnNamespace(modLog);
			return modLog;
		}

		protected ModLog InitializePlugin(string pluginGuid, Type[] patchTypes)
		{
			//IL_003b: Unknown result type (might be due to invalid IL or missing references)
			//IL_0045: Expected O, but got Unknown
			ModLog modLog = new ModLog(((BaseUnityPlugin)this).Logger);
			Version version = ((object)this).GetType().Assembly.GetName().Version;
			modLog.LogInfo($"AssemblyVersion: {version}.");
			EnsureSharedPatches(modLog);
			_harmony = new Harmony(pluginGuid);
			foreach (Type type in patchTypes)
			{
				_harmony.CreateClassProcessor(type).Patch();
				modLog.LogInfo("Server patch: " + type.Name);
			}
			_patchesApplied = true;
			return modLog;
		}

		private static void EnsureSharedPatches(ModLog log)
		{
			//IL_0028: Unknown result type (might be due to invalid IL or missing references)
			lock (AppDomain.CurrentDomain)
			{
				if (AppDomain.CurrentDomain.GetData("Landoria.SharedLib.ConnectionFailureMenuPatch.v1") == null)
				{
					new Harmony("Landoria.SharedLib").CreateClassProcessor(typeof(ConnectionFailureMenuPatch)).Patch();
					AppDomain.CurrentDomain.SetData("Landoria.SharedLib.ConnectionFailureMenuPatch.v1", true);
					log.LogDebug("Shared connection failure menu patch was applied.");
				}
			}
		}

		protected void PatchOwnNamespace(ModLog log)
		{
			if (_patchesApplied)
			{
				log.LogDebug("Harmony patches are already active; skipping registration.");
				return;
			}
			string text = ((object)this).GetType().Namespace;
			Type[] types = Assembly.GetExecutingAssembly().GetTypes();
			foreach (Type type in types)
			{
				if (type.Namespace == text)
				{
					_harmony.CreateClassProcessor(type).Patch();
				}
			}
			_patchesApplied = true;
			log.LogDebug("Harmony patches were applied for the plugin namespace.");
		}

		protected void ShutdownPlugin()
		{
			if (_patchesApplied)
			{
				Harmony harmony = _harmony;
				if (harmony != null)
				{
					harmony.UnpatchSelf();
				}
				_patchesApplied = false;
			}
		}
	}
	public static class ConnectionFailureMessages
	{
		private const string StateKey = "Landoria.SharedLib.ConnectionFailureMessages.v1";

		private static readonly ManualLogSource Log = Logger.CreateLogSource("Landoria.ConnectionFailureMessages");

		public static void Push(string source, string message)
		{
			Push(source, message, (string)null);
		}

		public static void Push(string source, string userMessage, string systemMessage)
		{
			Push(source, userMessage, systemMessage, null);
		}

		public static void Push(string source, string userMessage, Exception exception)
		{
			Push(source, userMessage, exception?.Message, exception);
		}

		public static void Push(string source, string userMessage, string systemMessage, Exception exception)
		{
			string text = DisplayMessage(userMessage, systemMessage ?? exception?.Message);
			if (string.IsNullOrWhiteSpace(source) || text == null)
			{
				return;
			}
			bool flag = false;
			lock (AppDomain.CurrentDomain)
			{
				Stack<Tuple<string, string, string>> messages = GetMessages();
				Tuple<string, string, string> item = Tuple.Create(source, text, systemMessage);
				if (!messages.Contains(item))
				{
					messages.Push(item);
					flag = true;
				}
			}
			if (flag)
			{
				LogQueued(source, text, systemMessage, exception);
			}
		}

		public static void Clear(string source)
		{
			if (string.IsNullOrWhiteSpace(source))
			{
				return;
			}
			lock (AppDomain.CurrentDomain)
			{
				Stack<Tuple<string, string, string>> messages = GetMessages();
				Stack<Tuple<string, string, string>> stack = new Stack<Tuple<string, string, string>>();
				while (messages.Count > 0)
				{
					Tuple<string, string, string> tuple = messages.Pop();
					if (tuple.Item1 != source)
					{
						stack.Push(tuple);
					}
				}
				while (stack.Count > 0)
				{
					messages.Push(stack.Pop());
				}
			}
		}

		internal static bool TryPopAll(out string message)
		{
			lock (AppDomain.CurrentDomain)
			{
				Stack<Tuple<string, string, string>> messages = GetMessages();
				if (messages.Count > 0)
				{
					List<string> list = new List<string>();
					while (messages.Count > 0)
					{
						list.Add(messages.Pop().Item2);
					}
					message = string.Join("\n", list);
					return true;
				}
			}
			message = null;
			return false;
		}

		private static Stack<Tuple<string, string, string>> GetMessages()
		{
			if (AppDomain.CurrentDomain.GetData("Landoria.SharedLib.ConnectionFailureMessages.v1") is Stack<Tuple<string, string, string>> result)
			{
				return result;
			}
			Stack<Tuple<string, string, string>> stack = new Stack<Tuple<string, string, string>>();
			AppDomain.CurrentDomain.SetData("Landoria.SharedLib.ConnectionFailureMessages.v1", stack);
			return stack;
		}

		private static string DisplayMessage(string userMessage, string systemMessage)
		{
			if (!string.IsNullOrWhiteSpace(userMessage))
			{
				return userMessage;
			}
			if (!string.IsNullOrWhiteSpace(systemMessage))
			{
				return systemMessage;
			}
			return null;
		}

		private static void LogQueued(string source, string userMessage, string systemMessage, Exception exception)
		{
			string text = exception?.ToString() ?? Environment.StackTrace;
			if (string.IsNullOrWhiteSpace(systemMessage) || systemMessage == userMessage)
			{
				Log.LogWarning((object)("Queued connection failure message from " + source + ": " + userMessage + "\nDiagnostic stack:\n" + text));
			}
			else
			{
				Log.LogWarning((object)("Queued connection failure message from " + source + ": userMessage=" + userMessage + "; systemMessage=" + systemMessage + "\nDiagnostic stack:\n" + text));
			}
		}
	}
	[HarmonyPatch]
	internal static class ConnectionFailureMenuPatch
	{
		[HarmonyPostfix]
		[HarmonyPatch(typeof(FejdStartup), "ShowConnectError")]
		private static void ShowConnectError(TMP_Text ___m_connectionFailedError)
		{
			if (ConnectionFailureMessages.TryPopAll(out var message))
			{
				___m_connectionFailedError.text = message;
			}
		}

		[HarmonyPostfix]
		[HarmonyPatch(typeof(FejdStartup), "Start")]
		private static void Start(GameObject ___m_connectionFailedPanel, TMP_Text ___m_connectionFailedError)
		{
			ShowNext(___m_connectionFailedPanel, ___m_connectionFailedError);
		}

		[HarmonyPostfix]
		[HarmonyPatch(typeof(FejdStartup), "OnConnectionFailedOk")]
		private static void OnConnectionFailedOk(GameObject ___m_connectionFailedPanel, TMP_Text ___m_connectionFailedError)
		{
			ShowNext(___m_connectionFailedPanel, ___m_connectionFailedError);
		}

		private static void ShowNext(GameObject panel, TMP_Text text)
		{
			if (ConnectionFailureMessages.TryPopAll(out var message))
			{
				text.text = message;
				panel.SetActive(true);
			}
		}
	}
	public sealed class ModLog
	{
		private readonly ManualLogSource _logger;

		public ModLog(ManualLogSource logger)
		{
			_logger = logger;
		}

		public void LogFatal(object message)
		{
			Write((LogLevel)1, message);
		}

		public void LogError(object message)
		{
			Write((LogLevel)2, message);
		}

		public void LogWarning(object message)
		{
			Write((LogLevel)4, message);
		}

		public void LogMessage(object message)
		{
			Write((LogLevel)8, message);
		}

		public void LogInfo(object message)
		{
			Write((LogLevel)16, message);
		}

		public void LogDebug(object message)
		{
			Write((LogLevel)32, message);
		}

		public void Log(LogLevel level, object message)
		{
			//IL_0001: Unknown result type (might be due to invalid IL or missing references)
			Write(level, message);
		}

		private void Write(LogLevel level, object message)
		{
			//IL_0019: Unknown result type (might be due to invalid IL or missing references)
			string arg = DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss.fff");
			_logger.Log(level, (object)$"[{arg}] {message}");
		}
	}
	public static class PlatformPlayerIdentity
	{
		public unsafe static string Resolve(PlayerInfo player)
		{
			//IL_0022: Unknown result type (might be due to invalid IL or missing references)
			string text = ((object)(*(PlatformUserID*)(&player.m_userInfo.m_id))/*cast due to .constrained prefix*/).ToString();
			if (!string.IsNullOrWhiteSpace(text))
			{
				return text;
			}
			ZNetPeer obj = FindPeer(player);
			if (obj == null)
			{
				return null;
			}
			ISocket socket = obj.m_socket;
			if (socket == null)
			{
				return null;
			}
			return socket.GetHostName();
		}

		public static ZNetPeer FindPeer(PlayerInfo player)
		{
			//IL_0007: Unknown result type (might be due to invalid IL or missing references)
			//IL_0008: Unknown result type (might be due to invalid IL or missing references)
			if ((Object)(object)ZNet.instance == (Object)null || ((ZDOID)(ref player.m_characterID)).IsNone())
			{
				return null;
			}
			return ((IEnumerable<ZNetPeer>)ZNet.instance.GetPeers()).FirstOrDefault((Func<ZNetPeer, bool>)((ZNetPeer peer) => peer != null && ((ZDOID)(ref peer.m_characterID)).Equals(player.m_characterID)));
		}
	}
	public static class ServerRole
	{
		public static bool IsDedicatedServer
		{
			get
			{
				if ((Object)(object)ZNet.instance != (Object)null && ZNet.instance.IsServer())
				{
					return ZNet.instance.IsDedicated();
				}
				return false;
			}
		}
	}
}