Decompiled source of CrimsonMoon v1.0.3

CrimsonMoon.dll

Decompiled 4 days ago
using System;
using System.Collections;
using System.Collections.Generic;
using System.Diagnostics;
using System.IO;
using System.Reflection;
using System.Runtime.CompilerServices;
using System.Runtime.Versioning;
using System.Security;
using System.Security.Permissions;
using System.Text;
using System.Text.Json;
using BepInEx;
using BepInEx.Configuration;
using BepInEx.Core.Logging.Interpolation;
using BepInEx.Logging;
using BepInEx.Unity.IL2CPP;
using CrimsonMoon.Structs;
using CrimsonMoon.Utils;
using HarmonyLib;
using Il2CppInterop.Runtime;
using Il2CppInterop.Runtime.InteropTypes.Arrays;
using Microsoft.CodeAnalysis;
using ProjectM;
using ProjectM.Shared;
using Stunlock.Core;
using Unity.Collections;
using Unity.Entities;
using Unity.Mathematics;
using Unity.Transforms;
using UnityEngine;
using VAMP;
using VAMP.Attributes;
using VAMP.Utilities;
using VampireCommandFramework;

[assembly: CompilationRelaxations(8)]
[assembly: RuntimeCompatibility(WrapNonExceptionThrows = true)]
[assembly: Debuggable(DebuggableAttribute.DebuggingModes.IgnoreSymbolStoreSequencePoints)]
[assembly: TargetFramework(".NETCoreApp,Version=v6.0", FrameworkDisplayName = ".NET 6.0")]
[assembly: AssemblyCompany("CrimsonMoon")]
[assembly: AssemblyConfiguration("Release")]
[assembly: AssemblyDescription("A sacraficial alter to praise the moon! Honor it in return for a blood moon!")]
[assembly: AssemblyFileVersion("1.0.3.0")]
[assembly: AssemblyInformationalVersion("1.0.3+1.Branch.master.Sha.983cd658b5f13a18ca3689d0ecc27710d33e609c.983cd658b5f13a18ca3689d0ecc27710d33e609c")]
[assembly: AssemblyProduct("CrimsonMoon")]
[assembly: AssemblyTitle("CrimsonMoon")]
[assembly: SecurityPermission(SecurityAction.RequestMinimum, SkipVerification = true)]
[assembly: AssemblyVersion("1.0.3.0")]
[module: UnverifiableCode]
[module: RefSafetyRules(11)]
namespace Microsoft.CodeAnalysis
{
	[CompilerGenerated]
	[Microsoft.CodeAnalysis.Embedded]
	internal sealed class EmbeddedAttribute : Attribute
	{
	}
}
namespace System.Runtime.CompilerServices
{
	[CompilerGenerated]
	[Microsoft.CodeAnalysis.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;
		}
	}
}
public struct Sacrifice
{
	public string PrefabGUID;

	public int Amount;

	public bool TryGetPrefabHash(out int prefabGuid)
	{
		prefabGuid = 0;
		if (string.IsNullOrWhiteSpace(PrefabGUID))
		{
			return false;
		}
		return int.TryParse(PrefabGUID.Trim(), out prefabGuid);
	}
}
namespace CrimsonMoon
{
	[BepInPlugin("CrimsonMoon", "CrimsonMoon", "1.0.3")]
	[BepInDependency(/*Could not decode attribute arguments.*/)]
	[BepInDependency(/*Could not decode attribute arguments.*/)]
	public class Plugin : BasePlugin
	{
		private Harmony _harmony;

		internal static Plugin Instance;

		public static Harmony Harmony => Instance._harmony;

		public static ManualLogSource LogInstance => ((BasePlugin)Instance).Log;

		public static Settings Settings { get; private set; }

		public static Database Database { get; private set; }

		public static DropClearSystem DropClearSystem { get; private set; }

		public static string ConfigFiles => Path.Combine(Paths.ConfigPath, "CrimsonMoon");

		public override void Load()
		{
			//IL_0026: Unknown result type (might be due to invalid IL or missing references)
			//IL_0030: Expected O, but got Unknown
			Instance = this;
			Database = new Database();
			Settings = new Settings(((BasePlugin)this).Config);
			_harmony = new Harmony("CrimsonMoon");
			_harmony.PatchAll(Assembly.GetExecutingAssembly());
			CommandRegistry.RegisterAll();
			Events.OnCoreLoaded = (Action)Delegate.Combine(Events.OnCoreLoaded, new Action(Loaded));
		}

		private void Loaded()
		{
			//IL_0015: Unknown result type (might be due to invalid IL or missing references)
			//IL_001b: Expected O, but got Unknown
			DropClearSystem = new DropClearSystem();
			ManualLogSource log = ((BasePlugin)this).Log;
			bool flag = default(bool);
			BepInExInfoLogInterpolatedStringHandler val = new BepInExInfoLogInterpolatedStringHandler(27, 2, ref flag);
			if (flag)
			{
				((BepInExLogInterpolatedStringHandler)val).AppendLiteral("Plugin ");
				((BepInExLogInterpolatedStringHandler)val).AppendFormatted<string>("CrimsonMoon");
				((BepInExLogInterpolatedStringHandler)val).AppendLiteral(" version ");
				((BepInExLogInterpolatedStringHandler)val).AppendFormatted<string>("1.0.3");
				((BepInExLogInterpolatedStringHandler)val).AppendLiteral(" is loaded!");
			}
			log.LogInfo(val);
		}

		public override bool Unload()
		{
			CommandRegistry.UnregisterAssembly();
			Harmony harmony = _harmony;
			if (harmony != null)
			{
				harmony.UnpatchSelf();
			}
			return true;
		}
	}
	public static class MyPluginInfo
	{
		public const string PLUGIN_GUID = "CrimsonMoon";

		public const string PLUGIN_NAME = "CrimsonMoon";

		public const string PLUGIN_VERSION = "1.0.3";
	}
}
namespace CrimsonMoon.Utils
{
	public class DropClearSystem
	{
		private EntityQuery dropItemQuery;

		public DropClearSystem()
		{
			//IL_0006: Unknown result type (might be due to invalid IL or missing references)
			//IL_000c: Expected O, but got Unknown
			//IL_001b: Unknown result type (might be due to invalid IL or missing references)
			//IL_0020: 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_0032: Unknown result type (might be due to invalid IL or missing references)
			//IL_003f: Unknown result type (might be due to invalid IL or missing references)
			//IL_0044: 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_0074: 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_0092: 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_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)
			EntityQueryDesc val = new EntityQueryDesc();
			val.All = Il2CppStructArray<ComponentType>.op_Implicit((ComponentType[])(object)new ComponentType[4]
			{
				new ComponentType(Il2CppType.Of<ItemPickup>(), (AccessMode)0),
				new ComponentType(Il2CppType.Of<PrefabGUID>(), (AccessMode)0),
				new ComponentType(Il2CppType.Of<Translation>(), (AccessMode)0),
				new ComponentType(Il2CppType.Of<DestroyWhenNoCharacterNearbyAfterDuration>(), (AccessMode)0)
			});
			val.None = Il2CppStructArray<ComponentType>.op_Implicit((ComponentType[])(object)new ComponentType[1]
			{
				new ComponentType(Il2CppType.Of<AttachedBuffer>(), (AccessMode)1)
			});
			val.Options = (EntityQueryOptions)2;
			EntityQueryDesc val2 = val;
			EntityManager entityManager = Core.EntityManager;
			dropItemQuery = ((EntityManager)(ref entityManager)).CreateEntityQuery(val2.All);
		}

		private IEnumerable<Entity> GetDropItems()
		{
			NativeArray<Entity> entities = ((EntityQuery)(ref dropItemQuery)).ToEntityArray(AllocatorHandle.op_Implicit((Allocator)2));
			Enumerator<Entity> enumerator = entities.GetEnumerator();
			while (enumerator.MoveNext())
			{
				Entity current = enumerator.Current;
				EntityUtil.Read<PrefabGUID>(current);
				yield return current;
			}
			entities.Dispose();
		}

		private int ClearDropItems(Func<Entity, bool> shouldClear = null)
		{
			//IL_0014: 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_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_0037: Unknown result type (might be due to invalid IL or missing references)
			//IL_001e: Unknown result type (might be due to invalid IL or missing references)
			//IL_00a1: Unknown result type (might be due to invalid IL or missing references)
			//IL_00a6: Unknown result type (might be due to invalid IL or missing references)
			//IL_0043: Unknown result type (might be due to invalid IL or missing references)
			//IL_0048: Unknown result type (might be due to invalid IL or missing references)
			//IL_004e: Unknown result type (might be due to invalid IL or missing references)
			//IL_0053: 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)
			//IL_0057: 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_0060: Unknown result type (might be due to invalid IL or missing references)
			//IL_0065: Unknown result type (might be due to invalid IL or missing references)
			//IL_0076: Unknown result type (might be due to invalid IL or missing references)
			//IL_007b: 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_0086: Unknown result type (might be due to invalid IL or missing references)
			int num = 0;
			DynamicBuffer<InventoryBuffer> val = default(DynamicBuffer<InventoryBuffer>);
			foreach (Entity dropItem in GetDropItems())
			{
				if (shouldClear != null && !shouldClear(dropItem))
				{
					continue;
				}
				EntityManager entityManager = Core.Server.EntityManager;
				if (((EntityManager)(ref entityManager)).TryGetBuffer<InventoryBuffer>(dropItem, ref val))
				{
					Enumerator<InventoryBuffer> enumerator2 = val.GetEnumerator();
					while (enumerator2.MoveNext())
					{
						InventoryBuffer current2 = enumerator2.Current;
						NetworkedEntity itemEntity = current2.ItemEntity;
						if (!(((NetworkedEntity)(ref itemEntity)).GetEntityOnServer() == Entity.Null))
						{
							EntityManager entityManager2 = Core.Server.EntityManager;
							itemEntity = current2.ItemEntity;
							DestroyUtility.Destroy(entityManager2, ((NetworkedEntity)(ref itemEntity)).GetEntityOnServer(), (DestroyDebugReason)0, (string)null, 0);
						}
					}
				}
				DestroyUtility.Destroy(Core.Server.EntityManager, dropItem, (DestroyDebugReason)0, (string)null, 0);
				num++;
			}
			return num;
		}

		public int ClearDropItemsInRadius(float3 pos, float radius)
		{
			//IL_0010: Unknown result type (might be due to invalid IL or missing references)
			//IL_0015: Unknown result type (might be due to invalid IL or missing references)
			float2 posXZ = ((float3)(ref pos)).xz;
			return ClearDropItems(delegate(Entity entity)
			{
				//IL_0000: Unknown result type (might be due to invalid IL or missing references)
				//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_0008: Unknown result type (might be due to invalid IL or missing references)
				//IL_0014: Unknown result type (might be due to invalid IL or missing references)
				Translation val = EntityUtil.Read<Translation>(entity);
				return math.distance(posXZ, ((float3)(ref val.Value)).xz) <= radius;
			});
		}

		public Dictionary<int, int> GetDropCountsInRadius(float3 pos, float radius)
		{
			//IL_0008: 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_0015: Unknown result type (might be due to invalid IL or missing references)
			//IL_001a: 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_0022: Unknown result type (might be due to invalid IL or missing references)
			//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_0031: 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_0035: Unknown result type (might be due to invalid IL or missing references)
			//IL_003a: Unknown result type (might be due to invalid IL or missing references)
			//IL_003c: Unknown result type (might be due to invalid IL or missing references)
			//IL_0044: 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_005c: Unknown result type (might be due to invalid IL or missing references)
			//IL_0065: 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)
			Dictionary<int, int> dictionary = new Dictionary<int, int>();
			float2 xz = ((float3)(ref pos)).xz;
			NativeArray<Entity> val = ((EntityQuery)(ref dropItemQuery)).ToEntityArray(AllocatorHandle.op_Implicit((Allocator)2));
			try
			{
				Enumerator<Entity> enumerator = val.GetEnumerator();
				ItemPickup val3 = default(ItemPickup);
				while (enumerator.MoveNext())
				{
					Entity current = enumerator.Current;
					Translation val2 = EntityUtil.Read<Translation>(current);
					if (!(math.distance(xz, ((float3)(ref val2.Value)).xz) > radius) && EntityUtil.TryGetComponent<ItemPickup>(current, ref val3))
					{
						int itemAmount = val3.ItemAmount;
						int value = val3.ItemId._Value;
						if (dictionary.TryGetValue(value, out var value2))
						{
							dictionary[value] = value2 + itemAmount;
						}
						else
						{
							dictionary[value] = itemAmount;
						}
					}
				}
				return dictionary;
			}
			finally
			{
				val.Dispose();
			}
		}
	}
}
namespace CrimsonMoon.Structs
{
	public class Database
	{
		[FileReload("SacrificesReload")]
		public static string SacrificesFilePath = Path.Combine(Plugin.ConfigFiles, "sacrifices.json");

		public static List<Sacrifice> SacrificeList = new List<Sacrifice>();

		public Database()
		{
			LoadDatabase();
		}

		private static void LoadDatabase()
		{
			if (!Directory.Exists(Plugin.ConfigFiles))
			{
				Directory.CreateDirectory(Plugin.ConfigFiles);
			}
			SacrificesReload();
		}

		public static void SacrificesReload()
		{
			try
			{
				if (File.Exists(SacrificesFilePath))
				{
					SacrificeList = JsonSerializer.Deserialize<List<Sacrifice>>(File.ReadAllText(SacrificesFilePath, new UTF8Encoding(encoderShouldEmitUTF8Identifier: false)), JsonUtil.PrettyJsonOptions) ?? new List<Sacrifice>();
				}
				else
				{
					SacrificeList = new List<Sacrifice>(1)
					{
						new Sacrifice
						{
							PrefabGUID = "1566989408",
							Amount = 3
						}
					};
				}
				SaveSacrifices();
			}
			catch
			{
				Plugin.LogInstance.LogError((object)"Failed to load sacrifices database!");
				SacrificeList = new List<Sacrifice>();
			}
		}

		private static void SaveSacrifices()
		{
			string text = "You can specify a list of Sacrifices required to trigger the blood moon." + Environment.NewLine + "You can search for item hash guids at https://vrising.gaming.tools/items.";
			string contents = JsonUtil.SerializeWithComments<List<Sacrifice>>(SacrificeList, text, JsonUtil.PrettyJsonOptions);
			File.WriteAllText(SacrificesFilePath, contents, new UTF8Encoding(encoderShouldEmitUTF8Identifier: false));
		}
	}
	public readonly struct Settings
	{
		internal readonly ConfigFile CONFIG;

		internal readonly ConfigEntry<bool> ENABLE_MOD;

		internal readonly ConfigEntry<double> ALTER_LOCATION_X;

		internal readonly ConfigEntry<double> ALTER_LOCATION_Y;

		internal readonly ConfigEntry<double> ALTER_LOCATION_Z;

		internal readonly ConfigEntry<string> message;

		public static Vector3 GetAltarPosition(Settings settings)
		{
			//IL_0024: Unknown result type (might be due to invalid IL or missing references)
			return new Vector3((float)settings.ALTER_LOCATION_X.Value, (float)settings.ALTER_LOCATION_Y.Value, (float)settings.ALTER_LOCATION_Z.Value);
		}

		public Settings(ConfigFile config)
		{
			CONFIG = config;
			ENABLE_MOD = config.Bind<bool>("Config", "EnableMod", true, "Enable or disable the mod");
			ALTER_LOCATION_X = config.Bind<double>("Alter", "AlterLocation_X", -1585.3368, "The float3 position of the alter, accepts sacrafices within 10 distance");
			ALTER_LOCATION_Y = config.Bind<double>("Alter", "AlterLocation_Y", -3.8146, (ConfigDescription)null);
			ALTER_LOCATION_Z = config.Bind<double>("Alter", "AlterLocation_Z", -937.0102, (ConfigDescription)null);
			message = config.Bind<string>("Messages", "BloodAlterActivated", "The Blood Alter runs red!", "Message sent to all players when the blood alter is activated.");
		}
	}
}
namespace CrimsonMoon.Hooks
{
	[HarmonyPatch]
	internal class Sacrafice
	{
		[HarmonyPatch(typeof(DropItemThrowSystem), "OnUpdate")]
		public class DropItemThrowSystem_Prefix
		{
			private static DateTime _lastDebugMessageUtc;

			private static Coroutine Running;

			public static void Prefix(DropItemThrowSystem __instance)
			{
				if (Plugin.Database != null && Plugin.DropClearSystem != null && Running == null)
				{
					Running = Core.StartCoroutine(DelayCheck());
				}
			}

			private static IEnumerator DelayCheck()
			{
				yield return (object)new WaitForSeconds(0.3f);
				List<Sacrifice> sacrificeList = Database.SacrificeList;
				if (sacrificeList == null || sacrificeList.Count == 0)
				{
					Running = null;
					yield break;
				}
				Vector3 altarPosition = Settings.GetAltarPosition(Plugin.Settings);
				Dictionary<int, int> dropCountsInRadius = Plugin.DropClearSystem.GetDropCountsInRadius(new float3(altarPosition.x, altarPosition.y, altarPosition.z), 10f);
				if (dropCountsInRadius.Count == 0)
				{
					Running = null;
					yield break;
				}
				bool flag = default(bool);
				foreach (Sacrifice item in sacrificeList)
				{
					if (!item.TryGetPrefabHash(out var prefabGuid))
					{
						ManualLogSource logInstance = Plugin.LogInstance;
						BepInExWarningLogInterpolatedStringHandler val = new BepInExWarningLogInterpolatedStringHandler(50, 1, ref flag);
						if (flag)
						{
							((BepInExLogInterpolatedStringHandler)val).AppendLiteral("Invalid sacrifice PrefabGUID '");
							((BepInExLogInterpolatedStringHandler)val).AppendFormatted<string>(item.PrefabGUID);
							((BepInExLogInterpolatedStringHandler)val).AppendLiteral("' in sacrifices.json");
						}
						logInstance.LogWarning(val);
						Running = null;
						yield break;
					}
					if (!dropCountsInRadius.TryGetValue(prefabGuid, out var value) || value < item.Amount)
					{
						Running = null;
						yield break;
					}
				}
				ChatUtil.SystemSendAll(Plugin.Settings.message.Value);
				Core.Server.GetExistingSystemManaged<DebugEventsSystem>().JumpToNextBloodMoon();
				if (Plugin.DropClearSystem != null)
				{
					Core.StartCoroutine(Clear(altarPosition, 20));
				}
				Running = null;
			}

			private static IEnumerator Clear(Vector3 vector, int radius)
			{
				//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)
				yield return (object)new WaitForSeconds(1f);
				if (Plugin.DropClearSystem != null)
				{
					Plugin.DropClearSystem.ClearDropItemsInRadius(new float3(vector.x, vector.y, vector.z), radius);
				}
			}
		}
	}
}
namespace CrimsonMoon.Commands
{
	[CommandGroup("crimsonmoon", "cmoon")]
	internal class BloodMoon
	{
		[Command("trigger", "t", null, "Forces next moon to be blood", null, true)]
		public static void TriggerBloodMoon(ChatCommandContext ctx)
		{
			Core.Server.GetExistingSystemManaged<DebugEventsSystem>().JumpToNextBloodMoon();
			ctx.Reply("Jumping to Next Blood Moon");
		}
	}
}