Decompiled source of FlamethrowerReconnectFix v1.1.2

FlamethrowerReconnectFix.dll

Decompiled 2 months ago
using System;
using System.Diagnostics;
using System.Reflection;
using System.Runtime.CompilerServices;
using System.Runtime.Versioning;
using BepInEx;
using BepInEx.Logging;
using HarmonyLib;
using Microsoft.CodeAnalysis;
using Unity.Netcode;
using UnityEngine;

[assembly: CompilationRelaxations(8)]
[assembly: RuntimeCompatibility(WrapNonExceptionThrows = true)]
[assembly: Debuggable(DebuggableAttribute.DebuggingModes.Default | DebuggableAttribute.DebuggingModes.DisableOptimizations | DebuggableAttribute.DebuggingModes.IgnoreSymbolStoreSequencePoints | DebuggableAttribute.DebuggingModes.EnableEditAndContinue)]
[assembly: TargetFramework(".NETFramework,Version=v4.7.2", FrameworkDisplayName = ".NET Framework 4.7.2")]
[assembly: AssemblyCompany("FlamethrowerReconnectFix")]
[assembly: AssemblyConfiguration("Debug")]
[assembly: AssemblyFileVersion("1.0.0.0")]
[assembly: AssemblyInformationalVersion("1.0.0")]
[assembly: AssemblyProduct("FlamethrowerReconnectFix")]
[assembly: AssemblyTitle("FlamethrowerReconnectFix")]
[assembly: AssemblyVersion("1.0.0.0")]
[module: RefSafetyRules(11)]
namespace Microsoft.CodeAnalysis
{
	[CompilerGenerated]
	[Embedded]
	internal sealed class EmbeddedAttribute : Attribute
	{
	}
}
namespace System.Runtime.CompilerServices
{
	[CompilerGenerated]
	[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 FlamethrowerReconnectFix
{
	[BepInPlugin("local.flamethrowerreconnectfix", "Flamethrower Reconnect Fix", "1.1.2")]
	[BepInDependency(/*Could not decode attribute arguments.*/)]
	public class Plugin : BaseUnityPlugin
	{
		public const string Guid = "local.flamethrowerreconnectfix";

		public const string Version = "1.1.2";

		internal static ManualLogSource Log;

		private void Awake()
		{
			//IL_0011: Unknown result type (might be due to invalid IL or missing references)
			//IL_0017: Expected O, but got Unknown
			Log = ((BaseUnityPlugin)this).Logger;
			Harmony val = new Harmony("local.flamethrowerreconnectfix");
			val.PatchAll(Assembly.GetExecutingAssembly());
			Log.LogInfo((object)"FlamethrowerReconnectFix cargado. Vigilando reconexiones de red...");
		}
	}
	[HarmonyPatch]
	public static class FlamethrowerRegisterHandlersPatch
	{
		private static CustomMessagingManager _lastSeenMessagingManager;

		private static FieldInfo _registeredManagerField;

		private static bool _resolvedOnce;

		public static MethodBase TargetMethod()
		{
			Type type = AccessTools.TypeByName("Y4NGZUpgrades.Weapons.FlamethrowerStreamDriver");
			if (type == null)
			{
				ManualLogSource log = Plugin.Log;
				if (log != null)
				{
					log.LogWarning((object)"[FlamethrowerReconnectFix] No se encontró FlamethrowerStreamDriver. ¿Cambió BetterArmory de versión? Este punto de enganche no se aplicará.");
				}
				return null;
			}
			return AccessTools.Method(type, "RegisterNetworkHandlers", (Type[])null, (Type[])null);
		}

		public static void Prefix()
		{
			NetworkManager singleton = NetworkManager.Singleton;
			if (!((Object)(object)singleton == (Object)null))
			{
				CustomMessagingManager customMessagingManager = singleton.CustomMessagingManager;
				if (customMessagingManager != null && customMessagingManager != _lastSeenMessagingManager)
				{
					ResetFlamethrowerNetworkRegistration();
					_lastSeenMessagingManager = customMessagingManager;
				}
			}
		}

		private static void ResetFlamethrowerNetworkRegistration()
		{
			if (!_resolvedOnce)
			{
				Type type = AccessTools.TypeByName("Y4NGZUpgrades.Weapons.FlamethrowerStreamDriver");
				_registeredManagerField = AccessTools.Field(type, "_registeredManager");
				_resolvedOnce = true;
			}
			if (!(_registeredManagerField == null))
			{
				_registeredManagerField.SetValue(null, null);
				Plugin.Log.LogInfo((object)"[FlamethrowerReconnectFix] Nuevo CustomMessagingManager detectado (reconexión). Forzando re-registro de los handlers del lanzallamas.");
			}
		}
	}
	[HarmonyPatch]
	public static class ShotgunGetterSafetyPatch
	{
		private static bool _loggedOnce;

		public static MethodBase TargetMethod()
		{
			Type type = AccessTools.TypeByName("Y4NGZUpgrades.Weapons.Y4NGZWeaponItem");
			if (type == null)
			{
				ManualLogSource log = Plugin.Log;
				if (log != null)
				{
					log.LogWarning((object)"[FlamethrowerReconnectFix] No se encontró Y4NGZWeaponItem. ¿Cambió BetterArmory de versión? Este punto de enganche no se aplicará.");
				}
				return null;
			}
			return AccessTools.Method(type, "get_Shotgun", (Type[])null, (Type[])null);
		}

		[HarmonyFinalizer]
		public static Exception Finalizer(Exception __exception, ref object __result)
		{
			if (__exception != null)
			{
				__result = null;
				if (!_loggedOnce)
				{
					_loggedOnce = true;
					Plugin.Log.LogInfo((object)"[FlamethrowerReconnectFix] Contuve un crash en Y4NGZWeaponItem.get_Shotgun() (arma con referencia obsoleta tras una reconexión). Devolviendo null de forma segura en vez de dejar que se propague. (Este aviso solo se muestra una vez por sesión.)");
				}
			}
			return null;
		}
	}
	[HarmonyPatch]
	public static class SetLocalUseDiagnosticPatch
	{
		public static MethodBase TargetMethod()
		{
			Type type = AccessTools.TypeByName("Y4NGZUpgrades.Weapons.FlamethrowerStreamDriver");
			if (type == null)
			{
				return null;
			}
			return AccessTools.Method(type, "SetLocalUse", (Type[])null, (Type[])null);
		}

		public static void Prefix(object __instance, bool firing)
		{
			if (!firing)
			{
				return;
			}
			try
			{
				Type type = __instance.GetType();
				object obj = AccessTools.Field(type, "_shotgun")?.GetValue(__instance);
				if (obj == null)
				{
					Plugin.Log.LogInfo((object)"[FlamethrowerReconnectFix][DIAG] Intento de disparo con _shotgun == null (referencia rota/destruida).");
					return;
				}
				Type type2 = obj.GetType();
				object obj2 = AccessTools.Field(type2, "shellsLoaded")?.GetValue(obj);
				MethodInfo methodInfo = AccessTools.Method(type, "IsEquippedAndAlive", (Type[])null, (Type[])null);
				object obj3 = ((methodInfo != null) ? methodInfo.Invoke(__instance, null) : "N/A");
				object obj4 = AccessTools.Field(type, "_localReloading")?.GetValue(__instance);
				MethodInfo methodInfo2 = AccessTools.Method(type, "IsLocalHolder", (Type[])null, (Type[])null);
				object obj5 = ((methodInfo2 != null) ? methodInfo2.Invoke(__instance, null) : "N/A");
				string text = "N/A";
				string text2 = "N/A";
				try
				{
					object obj6 = AccessTools.Field(type2, "playerHeldBy")?.GetValue(obj);
					text = ((obj6 == null) ? "null" : (AccessTools.Field(obj6.GetType(), "playerUsername")?.GetValue(obj6)?.ToString() ?? obj6.ToString()));
					Type type3 = AccessTools.TypeByName("GameNetworkManager");
					object obj7 = AccessTools.PropertyGetter(type3, "Instance")?.Invoke(null, null);
					object obj8 = ((obj7 == null) ? null : AccessTools.Field(type3, "localPlayerController")?.GetValue(obj7));
					text2 = ((obj8 == null) ? "null" : (AccessTools.Field(obj8.GetType(), "playerUsername")?.GetValue(obj8)?.ToString() ?? obj8.ToString()));
				}
				catch
				{
				}
				string text3 = "N/A";
				string text4 = "N/A";
				string text5 = "N/A";
				try
				{
					object obj10 = AccessTools.Property(AccessTools.TypeByName("Unity.Netcode.NetworkBehaviour"), "NetworkObject")?.GetValue(obj);
					if (obj10 != null)
					{
						text3 = AccessTools.Property(obj10.GetType(), "NetworkObjectId")?.GetValue(obj10)?.ToString() ?? "N/A";
						text5 = AccessTools.Property(obj10.GetType(), "OwnerClientId")?.GetValue(obj10)?.ToString() ?? "N/A";
					}
					text4 = AccessTools.Property(type2, "IsOwner")?.GetValue(obj)?.ToString() ?? "N/A";
				}
				catch
				{
				}
				Plugin.Log.LogInfo((object)("[FlamethrowerReconnectFix][DIAG] Intento de disparo -> shellsLoaded=" + obj2?.ToString() + ", IsEquippedAndAlive=" + obj3?.ToString() + ", IsLocalHolder=" + obj5?.ToString() + ", playerHeldBy=" + text + ", localPlayer=" + text2 + ", _localReloading=" + obj4?.ToString() + ", NetworkObjectId=" + text3 + ", IsOwner=" + text4 + ", OwnerClientId=" + text5));
			}
			catch (Exception ex)
			{
				Plugin.Log.LogWarning((object)("[FlamethrowerReconnectFix][DIAG] No se pudo leer el diagnóstico: " + ex.Message));
			}
		}
	}
	[HarmonyPatch]
	public static class ItemActivateDiagnosticPatch
	{
		public static MethodBase TargetMethod()
		{
			Type type = AccessTools.TypeByName("Y4NGZUpgrades.Weapons.Patches.FlamethrowerPatches");
			if (type == null)
			{
				return null;
			}
			return AccessTools.Method(type, "ItemActivate_Prefix", (Type[])null, (Type[])null);
		}

		public static void Prefix(object __instance, bool buttonDown)
		{
			if (!buttonDown)
			{
				return;
			}
			try
			{
				object obj = null;
				bool flag = false;
				string text = null;
				try
				{
					obj = (typeof(Component).GetMethod("GetComponent", Type.EmptyTypes)?.MakeGenericMethod(AccessTools.TypeByName("Y4NGZUpgrades.Weapons.FlamethrowerStreamDriver")))?.Invoke(__instance, null);
				}
				catch (Exception ex)
				{
					flag = true;
					text = ex.InnerException?.Message ?? ex.Message;
				}
				bool flag2 = obj != null;
				Plugin.Log.LogInfo((object)("[FlamethrowerReconnectFix][DIAG2] Clic en ShotgunItem -> GetComponent<FlamethrowerStreamDriver>() " + (flag ? ("LANZÓ EXCEPCIÓN: " + text) : (flag2 ? "encontrado (OK)" : "devolvió NULL (bug probable)"))));
			}
			catch (Exception ex2)
			{
				Plugin.Log.LogWarning((object)("[FlamethrowerReconnectFix][DIAG2] No se pudo leer el diagnóstico: " + ex2.Message));
			}
		}
	}
	[HarmonyPatch]
	public static class SendUseDiagnosticPatch
	{
		public static MethodBase TargetMethod()
		{
			Type type = AccessTools.TypeByName("Y4NGZUpgrades.Weapons.FlamethrowerStreamDriver");
			if (type == null)
			{
				return null;
			}
			return AccessTools.Method(type, "SendUse", (Type[])null, (Type[])null);
		}

		public static void Prefix(bool firing)
		{
			try
			{
				NetworkManager singleton = NetworkManager.Singleton;
				CustomMessagingManager val = ((singleton != null) ? singleton.CustomMessagingManager : null);
				Plugin.Log.LogInfo((object)("[FlamethrowerReconnectFix][DIAG3] SendUse(firing=" + firing + ") -> NetworkManager.Singleton=" + (((Object)(object)singleton == (Object)null) ? "null" : "OK") + ", CustomMessagingManager=" + ((val == null) ? "null (¡acá estaría el problema!)" : ("OK (hash=" + ((object)val).GetHashCode() + ")")) + ", LocalClientId=" + (((singleton != null) ? singleton.LocalClientId.ToString() : null) ?? "N/A")));
			}
			catch (Exception ex)
			{
				Plugin.Log.LogWarning((object)("[FlamethrowerReconnectFix][DIAG3] No se pudo leer el diagnóstico: " + ex.Message));
			}
		}
	}
}