Decompiled source of BetterArmoryFix v2.1.0

BetterArmoryFix.dll

Decompiled 6 hours 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;

[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("BetterArmoryFix")]
[assembly: AssemblyConfiguration("Debug")]
[assembly: AssemblyFileVersion("1.0.0.0")]
[assembly: AssemblyInformationalVersion("1.0.0")]
[assembly: AssemblyProduct("BetterArmoryFix")]
[assembly: AssemblyTitle("BetterArmoryFix")]
[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 BetterArmoryFix
{
	[BepInPlugin("local.betterarmoryfix", "BetterArmory Fix", "2.1.0")]
	[BepInDependency(/*Could not decode attribute arguments.*/)]
	[BepInDependency(/*Could not decode attribute arguments.*/)]
	public class Plugin : BaseUnityPlugin
	{
		public const string Guid = "local.betterarmoryfix";

		public const string Version = "2.1.0";

		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.betterarmoryfix");
			val.PatchAll(Assembly.GetExecutingAssembly());
			Log.LogInfo((object)"BetterArmoryFix cargado.");
		}
	}
	[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)"[BetterArmoryFix] No se encontró Y4NGZWeaponItem. ¿Cambió BetterArmory de versión? Este parche 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)"[BetterArmoryFix] Contuve un crash en Y4NGZWeaponItem.get_Shotgun() (arma con referencia obsoleta, típicamente 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 LguStoreUseHostSaveDataPatch
	{
		private static FieldInfo _playerIdField;

		private static ulong _originalPlayerId;

		private static bool _didSwap;

		public static MethodBase TargetMethod()
		{
			Type type = AccessTools.TypeByName("MoreShipUpgrades.Managers.LguStore");
			if (type == null)
			{
				ManualLogSource log = Plugin.Log;
				if (log != null)
				{
					log.LogWarning((object)"[BetterArmoryFix] No se encontró MoreShipUpgrades.Managers.LguStore. ¿Cambió el mod de versión? Este parche no se aplicará.");
				}
				return null;
			}
			return AccessTools.Method(type, "UpdateUpgradeBus", (Type[])null, (Type[])null);
		}

		public static void Prefix(object __instance, bool UseLocalSteamID)
		{
			_didSwap = false;
			if (!UseLocalSteamID)
			{
				return;
			}
			try
			{
				if (IsHost())
				{
					return;
				}
				ulong? hostSteamId = GetHostSteamId();
				if (!hostSteamId.HasValue)
				{
					Plugin.Log.LogWarning((object)"[BetterArmoryFix] No pude determinar el SteamID del host; no se pudo forzar la sincronización de stats.");
					return;
				}
				Type type = __instance.GetType();
				if (_playerIdField == null)
				{
					_playerIdField = AccessTools.Field(type, "playerID");
				}
				if (!(_playerIdField == null))
				{
					_originalPlayerId = (ulong)_playerIdField.GetValue(__instance);
					if (_originalPlayerId != hostSteamId.Value)
					{
						_playerIdField.SetValue(__instance, hostSteamId.Value);
						_didSwap = true;
						Plugin.Log.LogInfo((object)("[BetterArmoryFix] Forzando que el cliente cargue las stats del host (SteamID host=" + hostSteamId.Value + ") en vez de su copia personal."));
					}
				}
			}
			catch (Exception ex)
			{
				Plugin.Log.LogWarning((object)("[BetterArmoryFix] No se pudo forzar la sincronización de stats con el host: " + ex.Message));
			}
		}

		public static void Postfix(object __instance)
		{
			if (_didSwap)
			{
				try
				{
					_playerIdField.SetValue(__instance, _originalPlayerId);
				}
				catch
				{
				}
				_didSwap = false;
			}
		}

		private static bool IsHost()
		{
			Type type = AccessTools.TypeByName("Unity.Netcode.NetworkManager");
			object obj = AccessTools.PropertyGetter(type, "Singleton")?.Invoke(null, null);
			if (obj == null)
			{
				return false;
			}
			object obj2 = AccessTools.Property(type, "IsHost")?.GetValue(obj);
			bool flag = default(bool);
			int num;
			if (obj2 is bool)
			{
				flag = (bool)obj2;
				num = 1;
			}
			else
			{
				num = 0;
			}
			return (byte)((uint)num & (flag ? 1u : 0u)) != 0;
		}

		private static ulong? GetHostSteamId()
		{
			Type type = AccessTools.TypeByName("GameNetworkManager");
			object obj = AccessTools.PropertyGetter(type, "Instance")?.Invoke(null, null);
			if (obj == null)
			{
				return null;
			}
			object obj2 = AccessTools.Property(type, "currentLobby")?.GetValue(obj);
			if (obj2 == null)
			{
				return null;
			}
			Type type2 = obj2.GetType();
			object obj3 = AccessTools.Property(type2, "Owner")?.GetValue(obj2);
			if (obj3 == null)
			{
				return null;
			}
			Type type3 = obj3.GetType();
			object obj4 = AccessTools.Property(type3, "Id")?.GetValue(obj3);
			if (obj4 == null)
			{
				return null;
			}
			Type type4 = obj4.GetType();
			FieldInfo fieldInfo = AccessTools.Field(type4, "Value");
			if (fieldInfo != null && fieldInfo.GetValue(obj4) is ulong value)
			{
				return value;
			}
			if (ulong.TryParse(obj4.ToString(), out var result))
			{
				return result;
			}
			return null;
		}
	}
}