Decompiled source of CARTIdentityFix v1.0.0

CARTIdentityFix.dll

Decompiled a day ago
using System;
using System.Collections.Generic;
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 UnityEngine;

[assembly: CompilationRelaxations(8)]
[assembly: RuntimeCompatibility(WrapNonExceptionThrows = true)]
[assembly: Debuggable(DebuggableAttribute.DebuggingModes.IgnoreSymbolStoreSequencePoints)]
[assembly: TargetFramework(".NETStandard,Version=v2.1", FrameworkDisplayName = ".NET Standard 2.1")]
[assembly: AssemblyCompany("CARTIdentityFix")]
[assembly: AssemblyConfiguration("Release")]
[assembly: AssemblyFileVersion("1.0.0.0")]
[assembly: AssemblyInformationalVersion("1.0.0")]
[assembly: AssemblyProduct("CARTIdentityFix")]
[assembly: AssemblyTitle("CARTIdentityFix")]
[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 CARTIdentityFix
{
	internal static class CartIdentityGuard
	{
		private const string PickupableCartIconSyncType = "PickupableCart.LargeCartIconSync";

		private static readonly HashSet<int> ProtectedCartIds = new HashSet<int>();

		internal static void PreserveExistingIdentity(GameObject cartObject, string stage)
		{
			if (!Object.op_Implicit((Object)(object)cartObject))
			{
				return;
			}
			PhysGrabCart component = cartObject.GetComponent<PhysGrabCart>();
			if (!Object.op_Implicit((Object)(object)component) || component.isSmallCart)
			{
				return;
			}
			ItemAttributes[] components = cartObject.GetComponents<ItemAttributes>();
			if (components.Length != 0 && !Object.op_Implicit((Object)(object)cartObject.GetComponent<ItemEquippable>()))
			{
				cartObject.AddComponent<ItemEquippable>();
				bool flag = TryAddPickupableCartIconSync(cartObject);
				int instanceID = ((Object)cartObject).GetInstanceID();
				if (ProtectedCartIds.Add(instanceID))
				{
					string identityName = GetIdentityName(components[0]);
					Plugin.Log.LogInfo((object)("Preserved cart identity '" + identityName + "' on '" + ((Object)cartObject).name + "' during " + stage + "; added inventory support without another ItemAttributes component" + (flag ? " and retained PickupableCart's inventory icon." : ".")));
				}
			}
		}

		private static bool TryAddPickupableCartIconSync(GameObject cartObject)
		{
			Type type = AccessTools.TypeByName("PickupableCart.LargeCartIconSync");
			if (type == null || !typeof(Component).IsAssignableFrom(type))
			{
				Plugin.Log.LogWarning((object)"PickupableCart's icon sync component was not found. Cart identity is still protected, but its inventory icon may be unavailable.");
				return false;
			}
			if (!Object.op_Implicit((Object)(object)cartObject.GetComponent(type)))
			{
				cartObject.AddComponent(type);
			}
			return true;
		}

		private static string GetIdentityName(ItemAttributes itemAttributes)
		{
			if (Object.op_Implicit((Object)(object)itemAttributes) && Object.op_Implicit((Object)(object)itemAttributes.item) && !string.IsNullOrWhiteSpace(itemAttributes.item.itemName))
			{
				return itemAttributes.item.itemName;
			}
			return "pending item data";
		}
	}
	[HarmonyPatch(typeof(ItemAttributes), "Awake")]
	internal static class ItemAttributesAwakePatch
	{
		[HarmonyPrefix]
		[HarmonyPriority(800)]
		[HarmonyBefore(new string[] { "McHorse.PickupableCart" })]
		private static void PreserveBeforeItemInitialization(ItemAttributes __instance)
		{
			CartIdentityGuard.PreserveExistingIdentity(((Component)__instance).gameObject, "ItemAttributes.Awake");
		}
	}
	[HarmonyPatch(typeof(PhysGrabCart), "Start")]
	internal static class PhysGrabCartStartPatch
	{
		[HarmonyPrefix]
		[HarmonyPriority(800)]
		[HarmonyBefore(new string[] { "McHorse.PickupableCart" })]
		private static void PreserveBeforePickupableCartInjection(PhysGrabCart __instance)
		{
			CartIdentityGuard.PreserveExistingIdentity(((Component)__instance).gameObject, "PhysGrabCart.Start");
		}
	}
	[BepInPlugin("mathe.cartidentityfix", "C.A.R.T. Identity Fix", "1.0.0")]
	[BepInDependency(/*Could not decode attribute arguments.*/)]
	public sealed class Plugin : BaseUnityPlugin
	{
		public const string PluginGuid = "mathe.cartidentityfix";

		public const string PluginName = "C.A.R.T. Identity Fix";

		public const string PluginVersion = "1.0.0";

		public const string PickupableCartGuid = "McHorse.PickupableCart";

		private Harmony _harmony;

		internal static ManualLogSource Log { get; private set; }

		private void Awake()
		{
			//IL_0011: Unknown result type (might be due to invalid IL or missing references)
			//IL_001b: Expected O, but got Unknown
			Log = ((BaseUnityPlugin)this).Logger;
			_harmony = new Harmony("mathe.cartidentityfix");
			_harmony.PatchAll(typeof(Plugin).Assembly);
			Log.LogInfo((object)"C.A.R.T. Identity Fix is active. Existing cart identities will be preserved before PickupableCart injection.");
		}

		private void OnDestroy()
		{
			Harmony harmony = _harmony;
			if (harmony != null)
			{
				harmony.UnpatchSelf();
			}
		}
	}
}