Decompiled source of BloodyMailBox v1.0.3

BloodyMailBox.dll

Decompiled 6 months ago
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.IO;
using System.Linq;
using System.Reflection;
using System.Runtime.CompilerServices;
using System.Runtime.Serialization;
using System.Runtime.Versioning;
using System.Security;
using System.Security.Permissions;
using System.Text.Json;
using BepInEx;
using BepInEx.Configuration;
using BepInEx.Core.Logging.Interpolation;
using BepInEx.Logging;
using BepInEx.Unity.IL2CPP;
using Bloodstone.API;
using Bloodstone.Hooks;
using Bloody.Core.API.v1;
using Bloody.Core.GameData.v1;
using Bloody.Core.Models.v1;
using Bloody.Core.Models.v1.Base;
using BloodyMailBox.Common.Models;
using BloodyMailBox.Exceptions;
using BloodyMailBox.System;
using BloodyMailBox.Utils;
using HarmonyLib;
using Il2CppInterop.Runtime.InteropTypes.Arrays;
using ProjectM;
using ProjectM.Network;
using Stunlock.Network;
using Unity.Collections;
using Unity.Entities;
using VampireCommandFramework;

[assembly: CompilationRelaxations(8)]
[assembly: RuntimeCompatibility(WrapNonExceptionThrows = true)]
[assembly: Debuggable(DebuggableAttribute.DebuggingModes.IgnoreSymbolStoreSequencePoints)]
[assembly: TargetFramework(".NETCoreApp,Version=v6.0", FrameworkDisplayName = "")]
[assembly: AssemblyCompany("BloodyMailBox")]
[assembly: AssemblyConfiguration("Release")]
[assembly: AssemblyDescription("Mod for Ticket Admin System")]
[assembly: AssemblyFileVersion("1.0.3.0")]
[assembly: AssemblyInformationalVersion("1.0.3+3.Branch.master.Sha.1859f76d95336ce63eccca906c1afcfa1a3d432f")]
[assembly: AssemblyProduct("BloodyMailBox")]
[assembly: AssemblyTitle("BloodyMailBox")]
[assembly: SecurityPermission(SecurityAction.RequestMinimum, SkipVerification = true)]
[assembly: AssemblyVersion("1.0.3.0")]
[module: UnverifiableCode]
namespace BloodyMailBox
{
	[BepInPlugin("BloodyMailBox", "BloodyMailBox", "1.0.3")]
	[BepInDependency(/*Could not decode attribute arguments.*/)]
	[BepInDependency(/*Could not decode attribute arguments.*/)]
	[BepInDependency(/*Could not decode attribute arguments.*/)]
	public class Plugin : BasePlugin
	{
		public static ManualLogSource Logger;

		private Harmony _harmony;

		public static ConfigEntry<bool> OwnMessages;

		public static ConfigEntry<int> MaxMessages;

		public override void Load()
		{
			//IL_0006: Unknown result type (might be due to invalid IL or missing references)
			//IL_0010: Expected O, but got Unknown
			//IL_002c: Unknown result type (might be due to invalid IL or missing references)
			//IL_0036: Expected O, but got Unknown
			//IL_003d: Unknown result type (might be due to invalid IL or missing references)
			//IL_0047: Expected O, but got Unknown
			//IL_0058: Unknown result type (might be due to invalid IL or missing references)
			//IL_005e: Expected O, but got Unknown
			_harmony = new Harmony("BloodyMailBox");
			_harmony.PatchAll(Assembly.GetExecutingAssembly());
			CommandRegistry.RegisterAll();
			EventsHandlerSystem.OnInitialize += new OnGameDataInitializedEventHandler(GameDataOnInitialize);
			EventsHandlerSystem.OnDestroy += new OnGameDataDestroyedEventHandler(GameDataOnDestroy);
			InitConfigServer();
			ManualLogSource log = ((BasePlugin)this).Log;
			bool flag = default(bool);
			BepInExInfoLogInterpolatedStringHandler val = new BepInExInfoLogInterpolatedStringHandler(18, 1, ref flag);
			if (flag)
			{
				((BepInExLogInterpolatedStringHandler)val).AppendLiteral("Plugin ");
				((BepInExLogInterpolatedStringHandler)val).AppendFormatted<string>("BloodyMailBox");
				((BepInExLogInterpolatedStringHandler)val).AppendLiteral(" is loaded!");
			}
			log.LogInfo(val);
		}

		private void InitConfigServer()
		{
			OwnMessages = ((BasePlugin)this).Config.Bind<bool>("Config", "ownMessages", false, "Allows you to send messages to your mailbox to yourself.");
			MaxMessages = ((BasePlugin)this).Config.Bind<int>("Config", "maxMessages", 20, "Maximum number of messages that a user can have in the mailbox. If the value is 0 they are infinite (Not recommended)");
		}

		private static void GameDataOnInitialize(World world)
		{
			//IL_0007: Unknown result type (might be due to invalid IL or missing references)
			//IL_0011: Expected O, but got Unknown
			EventsHandlerSystem.OnUserConnected += new OnUserConnectedEventHandler(OnUserOnlineSystem.OnUserOnline);
		}

		private static void GameDataOnDestroy()
		{
		}
	}
	public static class MyPluginInfo
	{
		public const string PLUGIN_GUID = "BloodyMailBox";

		public const string PLUGIN_NAME = "BloodyMailBox";

		public const string PLUGIN_VERSION = "1.0.3";
	}
}
namespace BloodyMailBox.Utils
{
	internal class FontColorChat
	{
		public static string Color(string hexColor, string text)
		{
			return $"<color={hexColor}>{text}</color>";
		}

		public static string Red(string text)
		{
			return Color("#E90000", text);
		}

		public static string Blue(string text)
		{
			return Color("#0000ff", text);
		}

		public static string Green(string text)
		{
			return Color("#7FE030", text);
		}

		public static string Yellow(string text)
		{
			return Color("#FBC01E", text);
		}

		public static string White(string text)
		{
			return Color("#FFFFFF", text);
		}
	}
	internal class MailBox
	{
		public static readonly string ConfigPath = Path.Combine(Paths.ConfigPath, "BloodyMailBox");

		public static readonly string MailBoxPath = Path.Combine(ConfigPath, "MailBox");

		public static bool CreateMailBox(string user)
		{
			if (!Directory.Exists(ConfigPath))
			{
				Directory.CreateDirectory(ConfigPath);
			}
			if (!Directory.Exists(MailBoxPath))
			{
				Directory.CreateDirectory(MailBoxPath);
			}
			string path = Path.Combine(MailBoxPath, user + ".json");
			if (!File.Exists(path))
			{
				File.WriteAllText(path, "[]");
				return true;
			}
			return false;
		}

		public static List<MessageModel> ReadMailBox(string user)
		{
			try
			{
				string path = Path.Combine(MailBoxPath, user + ".json");
				if (!File.Exists(path))
				{
					CreateMailBox(user);
					return new List<MessageModel>();
				}
				return JsonSerializer.Deserialize<List<MessageModel>>(File.ReadAllText(path));
			}
			catch
			{
				return new List<MessageModel>();
			}
		}

		public static void SaveMailBox(string user, List<MessageModel> messages)
		{
			string path = Path.Combine(MailBoxPath, user + ".json");
			string contents = JsonSerializer.Serialize(messages);
			File.WriteAllText(path, contents);
		}

		public static void MessageToMailBox(string user, string message, string fromUser)
		{
			MessageModel messageModel = new MessageModel();
			List<MessageModel> list = new List<MessageModel>();
			if (CreateMailBox(user))
			{
				messageModel.Id = 1;
			}
			else
			{
				int num = 0;
				list = ReadMailBox(user);
				if (Plugin.MaxMessages.Value > 0 && list.Count == Plugin.MaxMessages.Value)
				{
					throw new MailBoxException("The user " + FontColorChat.White(user ?? "") + " has no space in the mailbox");
				}
				if (list.Count() > 0)
				{
					num = (from p in list
						orderby p.Id descending
						select p into r
						select r.Id).First();
				}
				messageModel.Id = num + 1;
			}
			messageModel.Author = fromUser;
			messageModel.Body = message;
			messageModel.Open = false;
			list.Add(messageModel);
			SaveMailBox(user, list);
		}

		public static MessageModel SelectMessageFromMailBox(string user, int id)
		{
			new List<MessageModel>();
			if (CreateMailBox(user))
			{
				throw new MailBoxException("The selected message does not exist.");
			}
			return ReadMailBox(user).FirstOrDefault((MessageModel message) => message.Id == id);
		}

		public static bool DeleteMessageFromMailBox(string user, int id)
		{
			SelectMessageFromMailBox(user, id);
			List<MessageModel> list = new List<MessageModel>();
			if (CreateMailBox(user))
			{
				throw new MailBoxException("The selected message does not exist.");
			}
			list = ReadMailBox(user);
			list.RemoveAll((MessageModel message) => message.Id == id);
			SaveMailBox(user, list);
			return true;
		}

		public static bool DeleteAllMessageFromMailBox(string user)
		{
			List<MessageModel> messages = new List<MessageModel>();
			if (CreateMailBox(user))
			{
				throw new MailBoxException("You don't have any messages to delete.");
			}
			SaveMailBox(user, messages);
			return true;
		}

		public static bool SetOpenedMessageFromMailBox(string user, int id)
		{
			SelectMessageFromMailBox(user, id);
			List<MessageModel> list = new List<MessageModel>();
			if (CreateMailBox(user))
			{
				throw new MailBoxException("The selected message does not exist.");
			}
			list = ReadMailBox(user);
			list.Where((MessageModel message) => message.Id == id).ToList().ForEach(delegate(MessageModel message)
			{
				message.Open = true;
			});
			SaveMailBox(user, list);
			return true;
		}
	}
}
namespace BloodyMailBox.System
{
	public class OnUserOnlineSystem
	{
		internal static void OnUserOnline(ServerBootstrapSystem sender, NetConnectionId netConnectionId)
		{
			//IL_001e: Unknown result type (might be due to invalid IL or missing references)
			//IL_0023: Unknown result type (might be due to invalid IL or missing references)
			//IL_002a: Unknown result type (might be due to invalid IL or missing references)
			int num = sender._NetEndPointToApprovedUserIndex[netConnectionId];
			Entity userEntity = ((Il2CppArrayBase<ServerClient>)(object)sender._ApprovedUsersLookup)[num].UserEntity;
			UserModel userModel = GameData.Users.FromEntity(userEntity);
			CoroutineHandler.StartGenericCoroutine((Action)delegate
			{
				OnTimedAuto(userModel);
			}, 10f);
		}

		private static void OnTimedAuto(UserModel user)
		{
			//IL_01df: Unknown result type (might be due to invalid IL or missing references)
			//IL_01ef: 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_004d: 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_0065: Unknown result type (might be due to invalid IL or missing references)
			//IL_0222: Unknown result type (might be due to invalid IL or missing references)
			//IL_0232: Unknown result type (might be due to invalid IL or missing references)
			//IL_009b: Unknown result type (might be due to invalid IL or missing references)
			//IL_00ab: Unknown result type (might be due to invalid IL or missing references)
			//IL_01b7: Unknown result type (might be due to invalid IL or missing references)
			//IL_01c7: Unknown result type (might be due to invalid IL or missing references)
			//IL_00ec: Unknown result type (might be due to invalid IL or missing references)
			//IL_00fc: Unknown result type (might be due to invalid IL or missing references)
			List<MessageModel> list = MailBox.ReadMailBox(user.CharacterName.ToString().ToLower());
			IEnumerable<MessageModel> enumerable = list.Where((MessageModel message) => !message.Open);
			int num = enumerable.Count();
			EntityManager entityManager = VWorld.Server.EntityManager;
			if (num > 0)
			{
				ServerChatUtils.SendSystemMessageToClient(entityManager, ((EntityModel)user).Internals.User.Value, "You have " + FontColorChat.White($"{num}") + " unread messages in your mailbox.");
				try
				{
					ServerChatUtils.SendSystemMessageToClient(entityManager, ((EntityModel)user).Internals.User.Value, FontColorChat.Yellow("------------ MailBox ------------"));
					foreach (MessageModel item in enumerable)
					{
						string text = (item.Open ? "Read" : "Unread");
						ServerChatUtils.SendSystemMessageToClient(entityManager, ((EntityModel)user).Internals.User.Value, FontColorChat.Yellow($"[{FontColorChat.White($"{item.Id}")}][{FontColorChat.White(text ?? "")}] Message Author: {FontColorChat.White(item.Author ?? "")}"));
					}
					ServerChatUtils.SendSystemMessageToClient(entityManager, ((EntityModel)user).Internals.User.Value, FontColorChat.Yellow("------------ End MailBox ------------"));
				}
				catch (MailBoxException ex)
				{
					ServerChatUtils.SendSystemMessageToClient(entityManager, ((EntityModel)user).Internals.User.Value, FontColorChat.Red(ex.Message ?? ""));
				}
			}
			if (list.Count == Plugin.MaxMessages.Value)
			{
				ServerChatUtils.SendSystemMessageToClient(entityManager, ((EntityModel)user).Internals.User.Value, "\r\n " + FontColorChat.White("ATTENTION") + " \r\n " + FontColorChat.Red("Your mailbox is full, delete messages to receive more."));
			}
		}
	}
}
namespace BloodyMailBox.Server.Timer
{
	public class AutoLoadNewMessages
	{
		private bool _enabled;

		private bool _isRunning;

		private DateTime _lastRunTime;

		private TimeSpan _delay;

		private Action<World> _action;

		private Func<object, TimeSpan> _delayAction;

		public void Start(Action<World> action, TimeSpan delay)
		{
			//IL_002d: Unknown result type (might be due to invalid IL or missing references)
			//IL_0037: Expected O, but got Unknown
			_delay = delay;
			_lastRunTime = DateTime.UtcNow - delay;
			_action = action;
			_enabled = true;
			GameFrame.OnUpdate += new GameFrameUpdateEventHandler(GameFrame_OnUpdate);
		}

		public void Start(Action<World> action, Func<object, TimeSpan> delayAction)
		{
			//IL_003e: Unknown result type (might be due to invalid IL or missing references)
			//IL_0048: Expected O, but got Unknown
			_delayAction = delayAction;
			_delay = _delayAction(1);
			_lastRunTime = DateTime.UtcNow;
			_action = action;
			_enabled = true;
			GameFrame.OnUpdate += new GameFrameUpdateEventHandler(GameFrame_OnUpdate);
		}

		private void GameFrame_OnUpdate()
		{
			Update(World.DefaultGameObjectInjectionWorld);
		}

		private void Update(World world)
		{
			if (!_enabled || _isRunning || _lastRunTime + _delay >= DateTime.UtcNow)
			{
				return;
			}
			_isRunning = true;
			try
			{
				_action(world);
			}
			catch (Exception ex)
			{
				Plugin.Logger.LogError((object)ex);
			}
			finally
			{
				int num = 30;
				if (_delayAction != null)
				{
					_delay = _delayAction(num);
				}
				_lastRunTime = DateTime.UtcNow;
				_isRunning = false;
			}
		}

		public void Stop()
		{
			//IL_0007: Unknown result type (might be due to invalid IL or missing references)
			//IL_0011: Expected O, but got Unknown
			GameFrame.OnUpdate -= new GameFrameUpdateEventHandler(GameFrame_OnUpdate);
			_enabled = false;
		}

		public void Dispose()
		{
			if (_enabled)
			{
				Stop();
			}
		}
	}
}
namespace BloodyMailBox.Server.Commands
{
	[CommandGroup("mailbox", null)]
	internal class MailBoxCommands
	{
		[Command("send", null, "<UserNick> \"<Message>\"", "Send a message to the mailbox of a server user", null, false)]
		public static void SendMail(ChatCommandContext ctx, string name, string message)
		{
			//IL_0058: Unknown result type (might be due to invalid IL or missing references)
			//IL_005d: 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_00bc: Unknown result type (might be due to invalid IL or missing references)
			//IL_010f: Unknown result type (might be due to invalid IL or missing references)
			//IL_0122: Unknown result type (might be due to invalid IL or missing references)
			//IL_0138: Unknown result type (might be due to invalid IL or missing references)
			//IL_013d: Unknown result type (might be due to invalid IL or missing references)
			UserModel val = GameData.Users.All.FirstOrDefault((Func<UserModel, bool>)((UserModel user) => user.CharacterName.ToString().ToLower() == name.ToString().ToLower())) ?? throw ctx.Error("User " + FontColorChat.White(name ?? "") + " does not exist on this server.");
			User user2 = ctx.User;
			if (((object)(FixedString64Bytes)(ref user2.CharacterName)).ToString().ToLower() == name.ToString().ToLower() && !Plugin.OwnMessages.Value)
			{
				throw ctx.Error("You are not allowed to send messages to yourself.");
			}
			try
			{
				string user3 = name.ToString().ToLower();
				user2 = ctx.User;
				MailBox.MessageToMailBox(user3, message, ((object)(FixedString64Bytes)(ref user2.CharacterName)).ToString());
				ctx.Reply(FontColorChat.Yellow("Message sent to user " + FontColorChat.White(name ?? "") + " successfully."));
				if (val.IsConnected)
				{
					ServerChatUtils.SendSystemMessageToClient(VWorld.Server.EntityManager, ((EntityModel)val).Internals.User.Value, "You have a new message from " + FontColorChat.White($"{ctx.User.CharacterName}") + " in your mailbox.");
				}
			}
			catch (MailBoxException ex)
			{
				throw ctx.Error(ex.Message);
			}
		}

		[Command("read", null, "<idMessage>", "Read a certain message from your mailbox", null, false)]
		public static void ReadMessageFromMailBox(ChatCommandContext ctx, int id)
		{
			//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_00fb: Unknown result type (might be due to invalid IL or missing references)
			//IL_0100: Unknown result type (might be due to invalid IL or missing references)
			try
			{
				User user = ctx.User;
				MessageModel messageModel = MailBox.SelectMessageFromMailBox(((object)(FixedString64Bytes)(ref user.CharacterName)).ToString().ToLower(), id);
				ctx.Reply(FontColorChat.Yellow("------------ Message " + FontColorChat.White($"{id}") + " ------------"));
				ctx.Reply(FontColorChat.Yellow("Message Author: " + FontColorChat.White(messageModel.Author ?? "")));
				ctx.Reply(FontColorChat.Yellow("Message Body:"));
				ctx.Reply(FontColorChat.Yellow(FontColorChat.White(messageModel.Body ?? "") ?? ""));
				ctx.Reply(FontColorChat.Yellow("------------ End Message " + FontColorChat.White($"{id}") + " ------------"));
				if (!messageModel.Open)
				{
					user = ctx.User;
					MailBox.SetOpenedMessageFromMailBox(((object)(FixedString64Bytes)(ref user.CharacterName)).ToString().ToLower(), id);
				}
			}
			catch (MailBoxException ex)
			{
				throw ctx.Error(ex.Message);
			}
		}

		[Command("list", null, "", "List of all your messages in the mailbox", null, false)]
		public static void ListMail(ChatCommandContext ctx)
		{
			//IL_0007: Unknown result type (might be due to invalid IL or missing references)
			//IL_000c: Unknown result type (might be due to invalid IL or missing references)
			try
			{
				string text = "";
				User user = ctx.User;
				List<MessageModel> list = MailBox.ReadMailBox(((object)(FixedString64Bytes)(ref user.CharacterName)).ToString().ToLower());
				if (Plugin.MaxMessages.Value > 0)
				{
					ctx.Reply(FontColorChat.Yellow($"------------ MailBox [{FontColorChat.White($"{list.Count()}")}/{FontColorChat.White($"{Plugin.MaxMessages.Value}")}] ------------"));
				}
				else
				{
					ctx.Reply(FontColorChat.Yellow("------------ MailBox ------------"));
				}
				foreach (MessageModel item in list)
				{
					text = (item.Open ? "Read" : "Unread");
					ctx.Reply(FontColorChat.Yellow($"[{FontColorChat.White($"{item.Id}")}][{FontColorChat.White(text ?? "")}] Message Author: {FontColorChat.White(item.Author ?? "")}"));
				}
				ctx.Reply(FontColorChat.Yellow("------------ End MailBox ------------"));
			}
			catch (MailBoxException ex)
			{
				throw ctx.Error(ex.Message);
			}
		}

		[Command("delete", null, "<idMessage>", "Delete a certain message from your mailbox", null, false)]
		public static void DeleteMail(ChatCommandContext ctx, int id)
		{
			//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)
			try
			{
				User user = ctx.User;
				MailBox.DeleteMessageFromMailBox(((object)(FixedString64Bytes)(ref user.CharacterName)).ToString().ToLower(), id);
				ctx.Reply(FontColorChat.Yellow("Message " + FontColorChat.White($"{id}") + " deleted"));
			}
			catch (MailBoxException ex)
			{
				throw ctx.Error(ex.Message);
			}
		}

		[Command("deleteall", null, "", "Delete all messages from your mailbox", null, false)]
		public static void DeleteAllMessagesFromMailBox(ChatCommandContext ctx)
		{
			//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)
			try
			{
				User user = ctx.User;
				MailBox.DeleteAllMessageFromMailBox(((object)(FixedString64Bytes)(ref user.CharacterName)).ToString().ToLower());
				ctx.Reply(FontColorChat.Yellow("All messages in the mailbox have been deleted"));
			}
			catch (MailBoxException ex)
			{
				throw ctx.Error(ex.Message);
			}
		}
	}
}
namespace BloodyMailBox.Exceptions
{
	[Serializable]
	internal class MailBoxException : Exception
	{
		public MailBoxException()
		{
		}

		public MailBoxException(string message)
			: base(message)
		{
		}

		public MailBoxException(string message, Exception innerException)
			: base(message, innerException)
		{
		}

		protected MailBoxException(SerializationInfo info, StreamingContext context)
			: base(info, context)
		{
		}
	}
}
namespace BloodyMailBox.Common.Models
{
	internal class MessageModel
	{
		public int Id { get; set; }

		public string Author { get; set; }

		public string Body { get; set; }

		public bool Open { get; set; }
	}
}