BLSN.dll

Decompiled a month ago
using System;
using System.Diagnostics;
using System.IO;
using System.Net;
using System.Reflection;
using System.Runtime.CompilerServices;
using System.Runtime.Versioning;
using System.Security;
using System.Security.Permissions;
using System.Text;
using BLSN;
using BoneLib;
using BoneLib.BoneMenu;
using BoneLib.Notifications;
using LabFusion.Network;
using MelonLoader;
using MelonLoader.Utils;
using Newtonsoft.Json;
using UnityEngine;

[assembly: CompilationRelaxations(8)]
[assembly: RuntimeCompatibility(WrapNonExceptionThrows = true)]
[assembly: Debuggable(DebuggableAttribute.DebuggingModes.IgnoreSymbolStoreSequencePoints)]
[assembly: AssemblyTitle("My second silly bone lab mod that uses webhooks to notify discord server when a bonelab server is up")]
[assembly: AssemblyDescription("My second silly bone lab mod that uses webhooks to notify discord server when a bonelab server is up")]
[assembly: AssemblyCompany(null)]
[assembly: AssemblyProduct("BLSN")]
[assembly: AssemblyCopyright("Created by SillyAlex")]
[assembly: AssemblyTrademark(null)]
[assembly: AssemblyFileVersion("1.0.0")]
[assembly: MelonInfo(typeof(global::BLSN.BLSN), "BLSN", "1.0.0", "SillyAlex", null)]
[assembly: MelonColor]
[assembly: MelonGame(null, null)]
[assembly: TargetFramework(".NETFramework,Version=v4.7.2", FrameworkDisplayName = ".NET Framework 4.7.2")]
[assembly: SecurityPermission(SecurityAction.RequestMinimum, SkipVerification = true)]
[assembly: AssemblyVersion("1.0.0.0")]
[module: UnverifiableCode]
namespace BLSN;

public static class BuildInfo
{
	public const string Name = "BLSN";

	public const string Description = "My second silly bone lab mod that uses webhooks to notify discord server when a bonelab server is up";

	public const string Author = "SillyAlex";

	public const string Company = null;

	public const string Version = "1.0.0";

	public const string DownloadLink = null;
}
[Serializable]
public class BLSNConfig
{
	public string webhookUrl = "";

	public bool enableNotifications = true;

	public string serverName = "Bonelab Server";

	public bool sendOnServerStart = true;

	public bool sendOnServerStop = true;

	public string webhookUsername = "BLSN Bot";

	public string webhookAvatarUrl = "https://support.discord.com/hc/user_images/PRywUXcqg0v5DD6s7C3LyQ.jpeg";
}
public class BLSN : MelonMod
{
	public static Page MainPage;

	private static BLSNConfig config;

	private static string configPath;

	private static bool InServer;

	private bool lastServerState;

	public override void OnInitializeMelon()
	{
		configPath = Path.Combine(MelonEnvironment.UserDataDirectory, "BLSN_Config.json");
		LoadConfig();
		SetupMenu();
		Hooking.OnLevelLoaded += OnLevelLoaded;
		((MelonBase)this).LoggerInstance.Msg("BLSN initialized!");
	}

	private void LoadConfig()
	{
		try
		{
			if (File.Exists(configPath))
			{
				config = JsonConvert.DeserializeObject<BLSNConfig>(File.ReadAllText(configPath));
				((MelonBase)this).LoggerInstance.Msg("Config loaded successfully!");
			}
			else
			{
				config = new BLSNConfig();
				SaveConfig();
				((MelonBase)this).LoggerInstance.Msg("Default config created. Please edit BLSN_Config.json in your UserData folder!");
			}
		}
		catch (Exception ex)
		{
			((MelonBase)this).LoggerInstance.Error("Error loading config: " + ex.Message);
			config = new BLSNConfig();
		}
	}

	private void SaveConfig()
	{
		try
		{
			string contents = JsonConvert.SerializeObject((object)config, (Formatting)1);
			File.WriteAllText(configPath, contents);
			((MelonBase)this).LoggerInstance.Msg("Config saved successfully!");
		}
		catch (Exception ex)
		{
			((MelonBase)this).LoggerInstance.Error("Error saving config: " + ex.Message);
		}
	}

	private void SetupMenu()
	{
		//IL_000a: Unknown result type (might be due to invalid IL or missing references)
		//IL_0025: Unknown result type (might be due to invalid IL or missing references)
		//IL_0059: 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_00ae: Unknown result type (might be due to invalid IL or missing references)
		MainPage = Page.Root.CreatePage("<color=#7289DA>B</color><color=#6678B5>L</color><color=#5A6791>S</color><color=#4E566D>N</color>", Color.white, 0, true);
		MainPage.CreateFunction("Config Status", Color.cyan, (Action)delegate
		{
			//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_0029: 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_0049: 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_0069: Unknown result type (might be due to invalid IL or missing references)
			//IL_006e: 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_0085: Expected O, but got Unknown
			string text = (string.IsNullOrEmpty(config.webhookUrl) ? "Webhook not configured" : "Webhook configured");
			Notifier.Send(new Notification
			{
				Title = NotificationText.op_Implicit("BLSN Config Status"),
				Message = NotificationText.op_Implicit(text + "\nConfig file: " + Path.GetFileName(configPath)),
				Type = (NotificationType)(string.IsNullOrEmpty(config.webhookUrl) ? 1 : 0),
				PopupLength = 4f,
				ShowTitleOnPopup = true
			});
		});
		MainPage.CreateFunction("Send Test Message", Color.green, (Action)delegate
		{
			//IL_0011: 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_001c: Unknown result type (might be due to invalid IL or missing references)
			//IL_0021: Unknown result type (might be due to invalid IL or missing references)
			//IL_0026: 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_0036: 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_0048: Unknown result type (might be due to invalid IL or missing references)
			//IL_0054: Expected O, but got Unknown
			if (string.IsNullOrEmpty(config.webhookUrl))
			{
				Notifier.Send(new Notification
				{
					Title = NotificationText.op_Implicit("BLSN Error"),
					Message = NotificationText.op_Implicit("Please configure your webhook URL first!"),
					Type = (NotificationType)2,
					PopupLength = 3f,
					ShowTitleOnPopup = true
				});
			}
			else
			{
				SendDiscordMessage("Test message from BLSN mod!");
			}
		});
		MainPage.CreateFunction("Reload Config", Color.yellow, (Action)delegate
		{
			//IL_0006: Unknown result type (might be due to invalid IL or missing references)
			//IL_000b: Unknown result type (might be due to invalid IL or missing references)
			//IL_0011: 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_001b: Unknown result type (might be due to invalid IL or missing references)
			//IL_0021: Unknown result type (might be due to invalid IL or missing references)
			//IL_0026: Unknown result type (might be due to invalid IL or missing references)
			//IL_002b: 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_003d: Unknown result type (might be due to invalid IL or missing references)
			//IL_0049: Expected O, but got Unknown
			LoadConfig();
			Notifier.Send(new Notification
			{
				Title = NotificationText.op_Implicit("BLSN"),
				Message = NotificationText.op_Implicit("Configuration reloaded!"),
				Type = (NotificationType)0,
				PopupLength = 2f,
				ShowTitleOnPopup = true
			});
		});
		MainPage.CreateFunction("Open Config Folder", Color.white, (Action)delegate
		{
			try
			{
				Process.Start("explorer.exe", MelonEnvironment.UserDataDirectory);
			}
			catch (Exception ex)
			{
				MelonLogger.Error("Error opening config folder: " + ex.Message);
			}
		});
	}

	private static void SendDiscordMessage(string message)
	{
		//IL_010a: 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_0115: Unknown result type (might be due to invalid IL or missing references)
		//IL_011a: Unknown result type (might be due to invalid IL or missing references)
		//IL_011f: Unknown result type (might be due to invalid IL or missing references)
		//IL_0125: Unknown result type (might be due to invalid IL or missing references)
		//IL_012a: Unknown result type (might be due to invalid IL or missing references)
		//IL_012f: Unknown result type (might be due to invalid IL or missing references)
		//IL_0131: Unknown result type (might be due to invalid IL or missing references)
		//IL_0136: Unknown result type (might be due to invalid IL or missing references)
		//IL_0141: Unknown result type (might be due to invalid IL or missing references)
		//IL_014d: Expected O, but got Unknown
		if (string.IsNullOrEmpty(config.webhookUrl))
		{
			MelonLogger.Error("Webhook URL is not configured!");
			return;
		}
		if (!config.enableNotifications)
		{
			MelonLogger.Msg("Notifications are disabled in config.");
			return;
		}
		try
		{
			using WebClient webClient = new WebClient();
			webClient.Headers.Add("Content-Type", "application/json");
			string s = JsonConvert.SerializeObject((object)new
			{
				content = message,
				username = config.webhookUsername,
				avatar_url = config.webhookAvatarUrl,
				embeds = new[]
				{
					new
					{
						title = config.serverName,
						description = message,
						color = 7506394,
						timestamp = DateTime.UtcNow.ToString("yyyy-MM-ddTHH:mm:ss.fffZ"),
						footer = new
						{
							text = "BLSN Mod v1.0.0"
						},
						thumbnail = new
						{
							url = config.webhookAvatarUrl
						}
					}
				}
			});
			byte[] bytes = Encoding.UTF8.GetBytes(s);
			webClient.UploadDataAsync(new Uri(config.webhookUrl), bytes);
			MelonLogger.Msg("Discord message sent successfully!");
		}
		catch (Exception ex)
		{
			MelonLogger.Error("Error sending Discord message: " + ex.Message);
			Notifier.Send(new Notification
			{
				Title = NotificationText.op_Implicit("BLSN Error"),
				Message = NotificationText.op_Implicit("Failed to send Discord message. Check console for details."),
				Type = (NotificationType)2,
				PopupLength = 4f,
				ShowTitleOnPopup = true
			});
		}
	}

	private void OnLevelLoaded(LevelInfo info)
	{
		//IL_0000: Unknown result type (might be due to invalid IL or missing references)
		//IL_0005: Unknown result type (might be due to invalid IL or missing references)
		//IL_000b: Unknown result type (might be due to invalid IL or missing references)
		//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)
		//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_0038: 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_003f: 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_0056: Expected O, but got Unknown
		Notifier.Send(new Notification
		{
			Title = NotificationText.op_Implicit("BLSN | Ready"),
			Message = NotificationText.op_Implicit(config.enableNotifications ? "BLSN is ready and notifications are enabled!" : "BLSN is ready but notifications are disabled."),
			Type = (NotificationType)3,
			PopupLength = 3f,
			ShowTitleOnPopup = true
		});
	}

	public static void HostBYEBYENOT()
	{
		try
		{
			if (config.sendOnServerStop && config.enableNotifications && !string.IsNullOrEmpty(config.webhookUrl))
			{
				SendDiscordMessage("Server is down!");
			}
		}
		catch (Exception ex)
		{
			MelonLogger.Error("Error sending server stop message: " + ex.Message);
		}
	}

	public static void HostHELLONOT()
	{
		if (config.sendOnServerStart && config.enableNotifications && !string.IsNullOrEmpty(config.webhookUrl) && InServer && ValidateServerConnection())
		{
			try
			{
				SendDiscordMessage("Server is up!");
			}
			catch (Exception ex)
			{
				MelonLogger.Error("Error sending server start message: " + ex.Message);
			}
		}
	}

	public override void OnUpdate()
	{
		bool hasServer = NetworkInfo.HasServer;
		if (lastServerState && !hasServer)
		{
			InServer = false;
			HostBYEBYENOT();
		}
		if (!lastServerState && hasServer)
		{
			InServer = true;
			HostHELLONOT();
		}
		lastServerState = hasServer;
	}

	private static bool ValidateServerConnection()
	{
		if (!NetworkInfo.HasServer)
		{
			MelonLogger.Warning("Not connected to Fusion server");
			return false;
		}
		if (!NetworkInfo.IsHost)
		{
			MelonLogger.Warning("User is not the server host");
			return false;
		}
		return true;
	}
}