Decompiled source of BloodDrop v1.0.3

BloodDrop.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 System.Security;
using System.Security.Permissions;
using BepInEx;
using BepInEx.Configuration;
using BepInEx.Unity.IL2CPP;
using BloodDrop.Configuration;
using BloodDrop.Patches;
using BloodDrop.Services;
using HarmonyLib;
using Il2CppInterop.Runtime.InteropTypes;
using Microsoft.CodeAnalysis;
using ProjectM;
using Stunlock.Core;
using Unity.Collections;
using Unity.Entities;
using Unity.Mathematics;
using Unity.Transforms;

[assembly: CompilationRelaxations(8)]
[assembly: RuntimeCompatibility(WrapNonExceptionThrows = true)]
[assembly: Debuggable(DebuggableAttribute.DebuggingModes.IgnoreSymbolStoreSequencePoints)]
[assembly: TargetFramework(".NETCoreApp,Version=v6.0", FrameworkDisplayName = ".NET 6.0")]
[assembly: AssemblyCompany("BloodDrop")]
[assembly: AssemblyConfiguration("Release")]
[assembly: AssemblyFileVersion("1.0.3.0")]
[assembly: AssemblyInformationalVersion("1.0.3+43bcbdd000886ceda305cb2cc2142f515dcb2ddd")]
[assembly: AssemblyProduct("BloodDrop")]
[assembly: AssemblyTitle("BloodDrop")]
[assembly: SecurityPermission(SecurityAction.RequestMinimum, SkipVerification = true)]
[assembly: AssemblyVersion("1.0.3.0")]
[module: UnverifiableCode]
[module: RefSafetyRules(11)]
namespace Microsoft.CodeAnalysis
{
	[CompilerGenerated]
	[Embedded]
	internal sealed class EmbeddedAttribute : Attribute
	{
	}
}
namespace System.Runtime.CompilerServices
{
	[CompilerGenerated]
	[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]
	[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]
	[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 BloodDrop
{
	[BepInPlugin("BloodDrop", "BloodDrop", "1.0.3")]
	public sealed class Plugin : BasePlugin
	{
		public const string Id = "BloodDrop";

		public const string Name = "BloodDrop";

		public const string Version = "1.0.3";

		internal static Plugin Instance;

		internal DropSettings Settings;

		private Harmony? harmony;

		public override void Load()
		{
			//IL_001d: Unknown result type (might be due to invalid IL or missing references)
			//IL_0027: Expected O, but got Unknown
			Instance = this;
			Settings = new DropSettings(((BasePlugin)this).Config);
			harmony = new Harmony("BloodDrop");
			harmony.PatchAll(typeof(Plugin).Assembly);
		}

		public override bool Unload()
		{
			Harmony? obj = harmony;
			if (obj != null)
			{
				obj.UnpatchSelf();
			}
			DeathEventPatch.Reset();
			return true;
		}
	}
}
namespace BloodDrop.Services
{
	public static class BloodDropService
	{
		public static readonly PrefabGUID Bottle = new PrefabGUID(828432508);

		private const int NoneBloodType = 447918373;

		private const int VBloodType = -338774148;

		public static void OnDeath(World world, DeathEvent death)
		{
			//IL_0027: Unknown result type (might be due to invalid IL or missing references)
			//IL_002c: Unknown result type (might be due to invalid IL or missing references)
			//IL_002d: Unknown result type (might be due to invalid IL or missing references)
			//IL_002e: Unknown result type (might be due to invalid IL or missing references)
			//IL_0033: Unknown result type (might be due to invalid IL or missing references)
			//IL_0036: Unknown result type (might be due to invalid IL or missing references)
			//IL_0040: 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_0054: Unknown result type (might be due to invalid IL or missing references)
			//IL_005e: 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_0072: Unknown result type (might be due to invalid IL or missing references)
			//IL_007d: Unknown result type (might be due to invalid IL or missing references)
			//IL_007e: Unknown result type (might be due to invalid IL or missing references)
			//IL_0083: 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)
			//IL_0090: Unknown result type (might be due to invalid IL or missing references)
			//IL_00b7: Unknown result type (might be due to invalid IL or missing references)
			//IL_00c4: Unknown result type (might be due to invalid IL or missing references)
			//IL_00d1: 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)
			//IL_00e7: Invalid comparison between Unknown and I4
			//IL_00e9: Unknown result type (might be due to invalid IL or missing references)
			//IL_00ea: Unknown result type (might be due to invalid IL or missing references)
			//IL_00f1: Invalid comparison between Unknown and I4
			//IL_013d: Unknown result type (might be due to invalid IL or missing references)
			//IL_013e: Unknown result type (might be due to invalid IL or missing references)
			//IL_0143: Unknown result type (might be due to invalid IL or missing references)
			//IL_0148: Unknown result type (might be due to invalid IL or missing references)
			//IL_014b: Unknown result type (might be due to invalid IL or missing references)
			//IL_014d: Unknown result type (might be due to invalid IL or missing references)
			//IL_014f: Unknown result type (might be due to invalid IL or missing references)
			//IL_0155: Unknown result type (might be due to invalid IL or missing references)
			DropSettings settings = Plugin.Instance.Settings;
			if (world.Name != "Server" || !settings.CanDrop)
			{
				return;
			}
			EntityManager entityManager = world.EntityManager;
			Entity died = death.Died;
			if (((EntityManager)(ref entityManager)).Exists(died) && !((EntityManager)(ref entityManager)).HasComponent<PlayerCharacter>(died) && !((EntityManager)(ref entityManager)).HasComponent<VBloodUnit>(died) && !((EntityManager)(ref entityManager)).HasComponent<VBloodConsumeSource>(died) && !((EntityManager)(ref entityManager)).HasComponent<VBloodUnitTagComponent>(died) && ((EntityManager)(ref entityManager)).HasComponent<BloodConsumeSource>(died) && ((EntityManager)(ref entityManager)).HasComponent<Translation>(died))
			{
				BloodConsumeSource componentData = ((EntityManager)(ref entityManager)).GetComponentData<BloodConsumeSource>(died);
				PrefabGUID value = ((ModifiablePrefabGUID)(ref componentData.UnitBloodType)).Value;
				if (((PrefabGUID)(ref value)).GuidHash != 0 && ((PrefabGUID)(ref value)).GuidHash != 447918373 && ((PrefabGUID)(ref value)).GuidHash != -338774148 && float.IsFinite(componentData.BloodQuality) && !(componentData.BloodQuality < 0f) && !(componentData.BloodQuality > 100f) && (((int)death.StatChangeReason != 22 && (int)death.StatChangeReason != 23) || settings.Feeding.Value) && (!(settings.Chance.Value < 100f) || !(Random.Shared.NextDouble() * 100.0 >= (double)settings.Chance.Value)))
				{
					float3 value2 = ((EntityManager)(ref entityManager)).GetComponentData<Translation>(died).Value;
					CreateDrop(world, value2, value, componentData.BloodQuality);
				}
			}
		}

		public static Entity CreateDrop(World world, float3 position, PrefabGUID bloodType, float quality)
		{
			//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_000d: Unknown result type (might be due to invalid IL or missing references)
			//IL_0012: Unknown result type (might be due to invalid IL or missing references)
			//IL_0016: Unknown result type (might be due to invalid IL or missing references)
			//IL_00ca: Unknown result type (might be due to invalid IL or missing references)
			//IL_0031: Unknown result type (might be due to invalid IL or missing references)
			//IL_0032: Unknown result type (might be due to invalid IL or missing references)
			//IL_0037: Unknown result type (might be due to invalid IL or missing references)
			//IL_0038: Unknown result type (might be due to invalid IL or missing references)
			//IL_003d: Unknown result type (might be due to invalid IL or missing references)
			//IL_00dc: Unknown result type (might be due to invalid IL or missing references)
			//IL_00d4: Unknown result type (might be due to invalid IL or missing references)
			//IL_0040: Unknown result type (might be due to invalid IL or missing references)
			//IL_0050: Unknown result type (might be due to invalid IL or missing references)
			//IL_0051: 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_0058: Unknown result type (might be due to invalid IL or missing references)
			//IL_005c: Unknown result type (might be due to invalid IL or missing references)
			//IL_00e6: Unknown result type (might be due to invalid IL or missing references)
			//IL_0072: 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_0066: Unknown result type (might be due to invalid IL or missing references)
			//IL_0067: Unknown result type (might be due to invalid IL or missing references)
			//IL_007d: Unknown result type (might be due to invalid IL or missing references)
			//IL_0082: Unknown result type (might be due to invalid IL or missing references)
			//IL_0085: Unknown result type (might be due to invalid IL or missing references)
			//IL_0088: Unknown result type (might be due to invalid IL or missing references)
			//IL_0090: 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_0098: Unknown result type (might be due to invalid IL or missing references)
			//IL_0099: 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_00ba: Unknown result type (might be due to invalid IL or missing references)
			//IL_00c2: Unknown result type (might be due to invalid IL or missing references)
			//IL_00c3: Unknown result type (might be due to invalid IL or missing references)
			//IL_00ee: Unknown result type (might be due to invalid IL or missing references)
			EntityManager entityManager = world.EntityManager;
			Entity val = default(Entity);
			if (!world.GetExistingSystemManaged<PrefabCollectionSystem>()._PrefabGuidToEntityMap.TryGetValue(Bottle, ref val))
			{
				throw new InvalidOperationException("Native blood bottle prefab is unavailable.");
			}
			Entity val2 = ((EntityManager)(ref entityManager)).Instantiate(val);
			Entity val3 = Entity.Null;
			try
			{
				StoredBlood val4 = new StoredBlood
				{
					BloodQuality = quality,
					PrimaryBloodType = bloodType
				};
				if (((EntityManager)(ref entityManager)).HasComponent<StoredBlood>(val2))
				{
					((EntityManager)(ref entityManager)).SetComponentData<StoredBlood>(val2, val4);
				}
				else
				{
					((EntityManager)(ref entityManager)).AddComponentData<StoredBlood>(val2, val4);
				}
				val3 = ((EntityManager)(ref entityManager)).CreateEntity();
				((EntityManager)(ref entityManager)).AddComponentData<DropItemAroundPosition>(val3, new DropItemAroundPosition
				{
					Position = position,
					ItemEntity = val2,
					ItemHash = Bottle,
					Amount = 1,
					DontMerge = true
				});
				return val2;
			}
			catch
			{
				if (((EntityManager)(ref entityManager)).Exists(val3))
				{
					((EntityManager)(ref entityManager)).DestroyEntity(val3);
				}
				if (((EntityManager)(ref entityManager)).Exists(val2))
				{
					((EntityManager)(ref entityManager)).DestroyEntity(val2);
				}
				throw;
			}
		}
	}
}
namespace BloodDrop.Patches
{
	[HarmonyPatch(typeof(DeathEventListenerSystem), "OnUpdate")]
	internal static class DeathEventPatch
	{
		private const int MaximumRememberedDeaths = 16384;

		private static IntPtr currentWorld;

		private static readonly HashSet<Entity> seen = new HashSet<Entity>();

		private static readonly Queue<Entity> order = new Queue<Entity>();

		internal static void Reset()
		{
			currentWorld = IntPtr.Zero;
			seen.Clear();
			order.Clear();
		}

		[HarmonyPrefix]
		private static void Prefix(DeathEventListenerSystem __instance)
		{
			//IL_0002: Unknown result type (might be due to invalid IL or missing references)
			//IL_0097: Unknown result type (might be due to invalid IL or missing references)
			//IL_009c: Unknown result type (might be due to invalid IL or missing references)
			//IL_009e: Unknown result type (might be due to invalid IL or missing references)
			//IL_00a3: Unknown result type (might be due to invalid IL or missing references)
			//IL_00a7: Unknown result type (might be due to invalid IL or missing references)
			//IL_00a8: Unknown result type (might be due to invalid IL or missing references)
			//IL_00ad: 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_00b6: Unknown result type (might be due to invalid IL or missing references)
			//IL_0062: Unknown result type (might be due to invalid IL or missing references)
			//IL_0067: Unknown result type (might be due to invalid IL or missing references)
			//IL_00c9: 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_0077: Unknown result type (might be due to invalid IL or missing references)
			//IL_007c: Unknown result type (might be due to invalid IL or missing references)
			//IL_0080: Unknown result type (might be due to invalid IL or missing references)
			//IL_0085: Unknown result type (might be due to invalid IL or missing references)
			//IL_008a: Unknown result type (might be due to invalid IL or missing references)
			//IL_008d: Unknown result type (might be due to invalid IL or missing references)
			//IL_0092: Unknown result type (might be due to invalid IL or missing references)
			//IL_00e1: Unknown result type (might be due to invalid IL or missing references)
			//IL_0103: Unknown result type (might be due to invalid IL or missing references)
			NativeArray<Entity> val = default(NativeArray<Entity>);
			try
			{
				if (!Plugin.Instance.Settings.CanDrop || ((ComponentSystemBase)__instance).World.Name != "Server")
				{
					return;
				}
				if (currentWorld != ((Il2CppObjectBase)((ComponentSystemBase)__instance).World).Pointer)
				{
					Reset();
					currentWorld = ((Il2CppObjectBase)((ComponentSystemBase)__instance).World).Pointer;
				}
				EntityQuery deathEventQuery = __instance._DeathEventQuery;
				if (((EntityQuery)(ref deathEventQuery)).IsEmpty)
				{
					return;
				}
				deathEventQuery = __instance._DeathEventQuery;
				val = ((EntityQuery)(ref deathEventQuery)).ToEntityArray(AllocatorHandle.op_Implicit((Allocator)2));
				Enumerator<Entity> enumerator = val.GetEnumerator();
				while (enumerator.MoveNext())
				{
					Entity current = enumerator.Current;
					try
					{
						EntityManager entityManager = ((ComponentSystemBase)__instance).EntityManager;
						DeathEvent componentData = ((EntityManager)(ref entityManager)).GetComponentData<DeathEvent>(current);
						if (seen.Add(componentData.Died))
						{
							order.Enqueue(componentData.Died);
							while (order.Count > 16384)
							{
								seen.Remove(order.Dequeue());
							}
							BloodDropService.OnDeath(((ComponentSystemBase)__instance).World, componentData);
						}
					}
					catch (Exception)
					{
					}
				}
			}
			catch (Exception)
			{
			}
			finally
			{
				if (val.IsCreated)
				{
					val.Dispose();
				}
			}
		}
	}
}
namespace BloodDrop.Configuration
{
	internal sealed class DropSettings
	{
		internal ConfigEntry<bool> Enabled { get; }

		internal ConfigEntry<float> Chance { get; }

		internal ConfigEntry<bool> Feeding { get; }

		internal bool CanDrop
		{
			get
			{
				if (Enabled.Value && float.IsFinite(Chance.Value))
				{
					return Chance.Value > 0f;
				}
				return false;
			}
		}

		internal DropSettings(ConfigFile config)
		{
			//IL_004c: Unknown result type (might be due to invalid IL or missing references)
			//IL_0056: Expected O, but got Unknown
			Enabled = config.Bind<bool>("BloodDrops", "Enabled", true, "Allow ordinary NPC blood bottle drops. Players and V Bloods are always excluded.");
			Chance = config.Bind<float>("BloodDrops", "DropChancePercent", 100f, new ConfigDescription("Chance per eligible death. 100 always drops one bottle; 0 never drops. 100 is the testing default.", (AcceptableValueBase)(object)new AcceptableValueRange<float>(0f, 100f), Array.Empty<object>()));
			Feeding = config.Bind<bool>("BloodDrops", "DropOnFeedingKills", true, "Also roll for a bottle when feeding kills an NPC.");
		}
	}
}