Decompiled source of RustyHeathensMessenger v0.11.0

RustyHeathensMessages.dll

Decompiled an hour ago
using System;
using System.Diagnostics;
using System.Reflection;
using System.Runtime.CompilerServices;
using System.Runtime.Versioning;
using System.Text;
using BepInEx;
using BepInEx.Configuration;
using HarmonyLib;
using UnityEngine;

[assembly: CompilationRelaxations(8)]
[assembly: RuntimeCompatibility(WrapNonExceptionThrows = true)]
[assembly: Debuggable(DebuggableAttribute.DebuggingModes.Default | DebuggableAttribute.DebuggingModes.DisableOptimizations | DebuggableAttribute.DebuggingModes.IgnoreSymbolStoreSequencePoints | DebuggableAttribute.DebuggingModes.EnableEditAndContinue)]
[assembly: AssemblyCompany("RustyLaserGaming")]
[assembly: AssemblyConfiguration("Release")]
[assembly: AssemblyDescription("Fantasy rune Viking character customization for Valheim.")]
[assembly: AssemblyFileVersion("0.1.0.0")]
[assembly: AssemblyInformationalVersion("0.1.0")]
[assembly: AssemblyProduct("RustyHeathensRuneborn")]
[assembly: AssemblyTitle("RustyHeathensRuneborn")]
[assembly: TargetFramework(".NETFramework,Version=v4.8", FrameworkDisplayName = ".NET Framework 4.8")]
[assembly: AssemblyVersion("0.1.0.0")]
namespace RustyHeathensRuneborn;

[HarmonyPatch(typeof(Character), "OnDeath")]
public static class RunebornBossKillMessagesPatch
{
	private static string _lastBossKey = string.Empty;

	private static float _lastBossMessageTime;

	private static void Postfix(Character __instance)
	{
		if ((Object)(object)__instance == (Object)null || (Object)(object)Player.m_localPlayer == (Object)null || (RunebornPlugin.BossKillMessagesEnabledEntry != null && !RunebornPlugin.BossKillMessagesEnabledEntry.Value))
		{
			return;
		}
		string bossDisplayName = GetBossDisplayName(((Object)((Component)__instance).gameObject).name);
		if (string.IsNullOrEmpty(bossDisplayName))
		{
			return;
		}
		string text = bossDisplayName.ToLowerInvariant();
		if (_lastBossKey == text && Time.time - _lastBossMessageTime < 8f)
		{
			return;
		}
		_lastBossKey = text;
		_lastBossMessageTime = Time.time;
		((Character)Player.m_localPlayer).Message((MessageType)2, "The Rusty Heathens defeated " + bossDisplayName + "!\nValhalla heard the victory roar.", 0, (Sprite)null);
		try
		{
		}
		catch
		{
		}
	}

	private static string GetBossDisplayName(string rawName)
	{
		if (string.IsNullOrEmpty(rawName))
		{
			return string.Empty;
		}
		string text = rawName.ToLowerInvariant();
		if (text.Contains("eikthyr"))
		{
			return "Eikthyr";
		}
		if (text.Contains("gd_king") || text.Contains("theelder"))
		{
			return "The Elder";
		}
		if (text.Contains("bonemass"))
		{
			return "Bonemass";
		}
		if (text.Contains("dragon") || text.Contains("moder"))
		{
			return "Moder";
		}
		if (text.Contains("goblinking") || text.Contains("yagluth"))
		{
			return "Yagluth";
		}
		if (text.Contains("queen") || text.Contains("seekerqueen"))
		{
			return "The Queen";
		}
		if (text.Contains("fader"))
		{
			return "Fader";
		}
		return string.Empty;
	}
}
[HarmonyPatch(typeof(Chat), "SendText")]
public static class RunebornChatCommands
{
	private static int _funnyPickCounter = -1;

	private static int _lastFunnyIndex = -1;

	private static bool Prefix(Type type, string text)
	{
		if (string.IsNullOrWhiteSpace(text))
		{
			return true;
		}
		string command = text.Trim().ToLowerInvariant();
		if (!IsRhmCommand(command))
		{
			return true;
		}
		if (RunebornPlugin.ChatCommandsEnabledEntry != null && !RunebornPlugin.ChatCommandsEnabledEntry.Value)
		{
			return true;
		}
		if (!PlayerCanUseCommands())
		{
			RunebornMessageBroadcast.ShowLocalCenter("COMMAND LOCKED\n\nYou do not have permission to use Rusty Heathens Messenger commands.");
			return false;
		}
		HandleCommand(command);
		return false;
	}

	private static bool IsRhmCommand(string command)
	{
		int result;
		switch (command)
		{
		default:
			result = ((command == "!raid") ? 1 : 0);
			break;
		case "/rhm":
		case "/rhm help":
		case "/rhm streammode":
		case "/rhm streammode on":
		case "/rhm streammode off":
		case "!streammode":
		case "!streammode on":
		case "!streammode off":
		case "/rhm stream":
		case "!stream":
		case "/rhm boss":
		case "!boss":
		case "/rhm base":
		case "!base":
		case "/rhm discord":
		case "!discord":
		case "/rhm ending":
		case "/rhm end":
		case "/rhm stream ending":
		case "!ending":
		case "!end":
		case "!streamending":
		case "/rhm body":
		case "!body":
		case "/rhm raid":
			result = 1;
			break;
		}
		return (byte)result != 0;
	}

	private static bool PlayerCanUseCommands()
	{
		string a = ((RunebornPlugin.CommandPermissionModeEntry != null) ? RunebornPlugin.CommandPermissionModeEntry.Value : "Everyone");
		if (string.Equals(a, "Everyone", StringComparison.OrdinalIgnoreCase))
		{
			return true;
		}
		Player localPlayer = Player.m_localPlayer;
		if ((Object)(object)localPlayer == (Object)null)
		{
			return false;
		}
		if (string.Equals(a, "HostOnly", StringComparison.OrdinalIgnoreCase))
		{
			return (Object)(object)ZNet.instance != (Object)null && ZNet.instance.IsServer();
		}
		if (string.Equals(a, "NamedPlayers", StringComparison.OrdinalIgnoreCase))
		{
			return PlayerNameIsAllowed(localPlayer);
		}
		if (string.Equals(a, "SteamIds", StringComparison.OrdinalIgnoreCase))
		{
			return PlayerSteamIdIsAllowed(localPlayer);
		}
		if (string.Equals(a, "NamedPlayersOrSteamIds", StringComparison.OrdinalIgnoreCase))
		{
			return PlayerNameIsAllowed(localPlayer) || PlayerSteamIdIsAllowed(localPlayer);
		}
		return true;
	}

	private static bool PlayerNameIsAllowed(Player player)
	{
		string text = ((RunebornPlugin.CommandAllowedPlayerNamesEntry != null) ? RunebornPlugin.CommandAllowedPlayerNamesEntry.Value : string.Empty);
		string playerName = player.GetPlayerName();
		string[] array = text.Split(new char[1] { ',' });
		string[] array2 = array;
		foreach (string text2 in array2)
		{
			if (string.Equals(text2.Trim(), playerName, StringComparison.OrdinalIgnoreCase))
			{
				return true;
			}
		}
		return false;
	}

	private static bool PlayerSteamIdIsAllowed(Player player)
	{
		string text = ((RunebornPlugin.CommandAllowedSteamIdsEntry != null) ? RunebornPlugin.CommandAllowedSteamIdsEntry.Value : string.Empty);
		string b = player.GetPlayerID().ToString();
		string[] array = text.Split(new char[1] { ',' });
		string[] array2 = array;
		foreach (string text2 in array2)
		{
			if (string.Equals(text2.Trim(), b, StringComparison.OrdinalIgnoreCase))
			{
				return true;
			}
		}
		return false;
	}

	private static void HandleCommand(string command)
	{
		if (command == "/rhm" || command == "/rhm help")
		{
			ShowCenter("RUSTY HEATHENS MESSENGER\n\n!stream - Stream starting soon\n!boss - Boss fight starting soon\n!base - Return to base\n!discord - Discord reminder\n!ending or !end - Stream is ending\n!body - Heathen down\n!raid - Incoming raid\n\nPermission modes: Everyone, HostOnly, NamedPlayers.");
			return;
		}
		switch (command)
		{
		default:
			if (!(command == "!streammode off"))
			{
				if (command == "/rhm stream" || command == "!stream")
				{
					ShowConfiguredMessage(RunebornPlugin.StreamTitleEntry, RunebornPlugin.StreamBodyEntry, RunebornPlugin.StreamStyleEntry, "STREAM STARTING SOON", "The Rusty Heathens are gathering.\nGrab your gear, check your food, and prepare for chaos.");
					break;
				}
				if (command == "/rhm boss" || command == "!boss")
				{
					ShowConfiguredMessage(RunebornPlugin.BossStartTitleEntry, RunebornPlugin.BossStartBodyEntry, RunebornPlugin.BossStartStyleEntry, "BOSS FIGHT STARTING SOON", "Return to base, repair your gear, grab food, and wait for the group.");
					break;
				}
				if (command == "/rhm base" || command == "!base")
				{
					ShowConfiguredMessage(RunebornPlugin.BaseTitleEntry, RunebornPlugin.BaseBodyEntry, RunebornPlugin.BaseStyleEntry, "RETURN TO BASE", "Everyone head back to base.\nRegroup before the chaos gets worse.");
					break;
				}
				if (command == "/rhm discord" || command == "!discord")
				{
					ShowConfiguredMessage(RunebornPlugin.DiscordTitleEntry, RunebornPlugin.DiscordBodyEntry, RunebornPlugin.DiscordStyleEntry, "DISCORD REMINDER", "Use Discord for rules, event plans, updates, and stream info.\nMessage RustyLaser if you need help.");
					break;
				}
				switch (command)
				{
				default:
					if (!(command == "!streamending"))
					{
						if (command == "/rhm body" || command == "!body")
						{
							ShowConfiguredMessage(RunebornPlugin.BodyTitleEntry, RunebornPlugin.BodyBodyEntry, RunebornPlugin.BodyStyleEntry, "HEATHEN DOWN", "Head back to base and regroup.\nA Heathen needs help recovering their kit.");
						}
						else if (command == "/rhm raid" || command == "!raid")
						{
							ShowConfiguredMessage(RunebornPlugin.RaidCommandTitleEntry, RunebornPlugin.RaidCommandBodyEntry, RunebornPlugin.RaidCommandStyleEntry, "INCOMING RAID", "Head back to base.\nHelp keep the Rusty Heathens safe.");
						}
						break;
					}
					goto case "/rhm ending";
				case "/rhm ending":
				case "/rhm end":
				case "/rhm stream ending":
				case "!ending":
				case "!end":
					ShowConfiguredMessage(RunebornPlugin.EndingTitleEntry, RunebornPlugin.EndingBodyEntry, RunebornPlugin.EndingStyleEntry, "STREAM IS ENDING", "The stream is ending.\nIf you want to say bye to chat, return to the wall.");
					break;
				}
				break;
			}
			goto case "/rhm streammode";
		case "/rhm streammode":
		case "/rhm streammode on":
		case "/rhm streammode off":
		case "!streammode":
		case "!streammode on":
			HandleStreamModeCommand(command);
			break;
		}
	}

	private static void HandleStreamModeCommand(string command)
	{
		if (RunebornPlugin.StreamModeEnabledEntry != null && !RunebornPlugin.StreamModeEnabledEntry.Value)
		{
			ShowCenter("STREAM MODE DISABLED\n\nStream Mode commands are disabled in the config.");
		}
		else if (command == "/rhm streammode on" || command == "!streammode on")
		{
			RunebornPlugin.StreamModeActive = true;
			string configText = GetConfigText(RunebornPlugin.StreamModeOnTitleEntry, "STREAM MODE ENABLED");
			string configText2 = GetConfigText(RunebornPlugin.StreamModeOnBodyEntry, "Chat is watching.\nKeep it fun, keep it moving, and prepare for Rusty Heathens chaos.");
			ShowCenter(configText + "\n\n" + configText2);
		}
		else if (command == "/rhm streammode off" || command == "!streammode off")
		{
			RunebornPlugin.StreamModeActive = false;
			string configText3 = GetConfigText(RunebornPlugin.StreamModeOffTitleEntry, "STREAM MODE DISABLED");
			string configText4 = GetConfigText(RunebornPlugin.StreamModeOffBodyEntry, "Chat has been released.\nYou may now return to unsupervised bad decisions.");
			ShowCenter(configText3 + "\n\n" + configText4);
		}
		else
		{
			string configText5 = GetConfigText(RunebornPlugin.StreamModeStatusTitleEntry, "STREAM MODE STATUS");
			string text = (RunebornPlugin.StreamModeActive ? GetConfigText(RunebornPlugin.StreamModeStatusOnBodyEntry, "Stream Mode is ON.\nChat is watching the chaos unfold.") : GetConfigText(RunebornPlugin.StreamModeStatusOffBodyEntry, "Stream Mode is OFF.\nThe Heathens are currently causing chaos off-camera."));
			ShowCenter(configText5 + "\n\n" + text);
		}
	}

	private static void ShowConfiguredMessage(ConfigEntry<string> titleEntry, ConfigEntry<string> bodyEntry, ConfigEntry<string> styleEntry, string defaultTitle, string defaultBody)
	{
		string configText = GetConfigText(titleEntry, defaultTitle);
		string configText2 = GetConfigText(bodyEntry, defaultBody);
		string configText3 = GetConfigText(styleEntry, "Normal");
		string text = ApplyMessageStyle(configText, configText2, configText3);
		if (RunebornPlugin.StreamModeActive)
		{
			string configText4 = GetConfigText(RunebornPlugin.StreamModeFooterEntry, "LIVE STREAM MODE: Chat is watching.");
			if (!string.IsNullOrWhiteSpace(configText4))
			{
				text = text + "\n\n" + configText4;
			}
		}
		ShowCenter(text);
	}

	private static string ApplyMessageStyle(string title, string body, string style)
	{
		style = (string.IsNullOrWhiteSpace(style) ? "Normal" : style.Trim());
		if (string.Equals(style, "Epic", StringComparison.OrdinalIgnoreCase))
		{
			return "=== " + title.ToUpperInvariant() + " ===\n\n" + body;
		}
		if (string.Equals(style, "Funny", StringComparison.OrdinalIgnoreCase))
		{
			return GetFunnyFooter(title) + "\n\n" + title.ToUpperInvariant() + "\n\n" + body;
		}
		if (string.Equals(style, "Emergency", StringComparison.OrdinalIgnoreCase))
		{
			return "!!! " + title.ToUpperInvariant() + " !!!\n\n" + body;
		}
		return title + "\n\n" + body;
	}

	private static string GetFunnyHeader(string title)
	{
		string text = title.ToLowerInvariant();
		if (text.Contains("stream starting"))
		{
			return "CHAT'S WATCHING: " + title.ToUpperInvariant();
		}
		if (text.Contains("boss"))
		{
			return "BAD IDEA LOADING: " + title.ToUpperInvariant();
		}
		if (text.Contains("return to base"))
		{
			return "PANIC WALK ENGAGED: " + title.ToUpperInvariant();
		}
		if (text.Contains("discord"))
		{
			return "READ THE RUNES: " + title.ToUpperInvariant();
		}
		if (text.Contains("ending"))
		{
			return "LAST CALL HEATHENS: " + title.ToUpperInvariant();
		}
		if (text.Contains("heathen down"))
		{
			return "CORPSE RUN O'CLOCK: " + title.ToUpperInvariant();
		}
		if (text.Contains("raid"))
		{
			return "PANIC PROFESSIONALLY: " + title.ToUpperInvariant();
		}
		return "RUSTY HEATHENS PSA: " + title.ToUpperInvariant();
	}

	private static string GetFunnyFooter(string title)
	{
		string text = title.ToLowerInvariant();
		if (text.Contains("stream starting"))
		{
			return Pick(new string[5] { "Try to look busy. Chat is watching.", "The mead is warm, the gear is broken, and somehow we are live.", "Everybody act professional. Or at least Viking-adjacent.", "Rusty hit the button. There is no going back now.", "Panda the Chaos Bringer has entered the chat. Prepare accordingly." });
		}
		if (text.Contains("boss"))
		{
			return Pick(new string[5] { "Bring food, bring courage, and maybe bring a backup plan.", "If you forgot rested buff, Odin already judged you.", "This is the part where confidence becomes a corpse run.", "Check your gear now. Regret waits at the boss altar.", "Panda called it a plan. The rest of us call it evidence." });
		}
		if (text.Contains("return to base"))
		{
			return Pick(new string[5] { "If you are lost, follow the screaming.", "Return to base before the base becomes a memory.", "The longship of poor decisions is heading home.", "Regroup now. Wander dramatically later.", "Someone check on Seppy before he finds another creative way to die." });
		}
		if (text.Contains("discord"))
		{
			return Pick(new string[5] { "Yes, this is your reminder before someone says they did not know.", "The runes were posted in Discord. Probably.", "Check Discord before asking Rusty for the third time.", "Important things happen there. Sometimes even plans.", "Discord has the rules. Panda has the chaos. Try not to confuse them." });
		}
		if (text.Contains("ending"))
		{
			return Pick(new string[5] { "Last chance to wave before Rusty hits the button.", "Say bye to chat before Valhalla claims the outro.", "Return to the wall. The ceremonial goodbye awkwardness begins.", "The stream is ending, but the chaos will remember us.", "One final wave before Panda starts another problem." });
		}
		if (text.Contains("heathen down"))
		{
			return Pick(new string[5] { "A kit has fallen. Again. Nobody act surprised.", "Someone has donated gear to the wilderness.", "Corpse run time. Try not to create a second one.", "A Heathen is down. Somebody get Witchy before this becomes a ritual.", "Witchy saw the body and said, 'That looks like a learning experience.'" });
		}
		if (text.Contains("raid"))
		{
			return Pick(new string[5] { "Heathens to the walls. Panic professionally.", "The base is under attack. Try not to decorate the floor with your corpse.", "This is not a drill. Unless it is a troll. Then it is worse.", "Protect the base, the mead, and whatever Seppy is standing near.", "Panda the Chaos Bringer says this is fine. It is not fine." });
		}
		return Pick(new string[5] { "This is probably fine.", "Odin has questions.", "The Rusty Heathens have made another decision.", "Somebody write this down as a lesson.", "Chaos has entered the server." });
	}

	private static string Pick(string[] lines)
	{
		if (lines == null || lines.Length == 0)
		{
			return "This is probably fine.";
		}
		if (lines.Length == 1)
		{
			return lines[0];
		}
		_funnyPickCounter++;
		if (_funnyPickCounter >= lines.Length)
		{
			_funnyPickCounter = 0;
		}
		return lines[_funnyPickCounter];
	}

	private static string GetConfigText(ConfigEntry<string> entry, string fallback)
	{
		if (entry == null)
		{
			return fallback;
		}
		if (string.IsNullOrWhiteSpace(entry.Value))
		{
			return fallback;
		}
		return entry.Value.Replace("\\n", "\n");
	}

	private static void ShowCenter(string message)
	{
		RunebornMessageBroadcast.BroadcastCenter(message);
	}
}
[HarmonyPatch(typeof(Player), "Awake")]
public static class RunebornDeathCounterPlayerAwakePatch
{
	private static void Postfix(Player __instance)
	{
		if (!((Object)(object)__instance == (Object)null) && (Object)(object)((Component)__instance).GetComponent<RunebornDeathCounter>() == (Object)null)
		{
			((Component)__instance).gameObject.AddComponent<RunebornDeathCounter>();
		}
	}
}
public class RunebornDeathCounter : MonoBehaviour
{
	private const string DeathCountKey = "RustyHeathensRuneborn.DeathCount";

	private bool _wasDead;

	private void Update()
	{
		Player localPlayer = Player.m_localPlayer;
		if (!((Object)(object)localPlayer == (Object)null))
		{
			bool flag = ((Character)localPlayer).IsDead();
			if (flag && !_wasDead)
			{
				RegisterDeath(localPlayer);
			}
			_wasDead = flag;
		}
	}

	private static void RegisterDeath(Player player)
	{
		if (RunebornPlugin.DeathMessagesEnabledEntry == null || RunebornPlugin.DeathMessagesEnabledEntry.Value)
		{
			int deathCount = GetDeathCount(player) + 1;
			player.m_customData["RustyHeathensRuneborn.DeathCount"] = deathCount.ToString();
			string funnyDeathMessage = GetFunnyDeathMessage(deathCount);
			((Character)player).Message((MessageType)2, "Death #" + deathCount + "\n" + funnyDeathMessage, 0, (Sprite)null);
		}
	}

	private static int GetDeathCount(Player player)
	{
		if (player.m_customData == null)
		{
			return 0;
		}
		if (!player.m_customData.ContainsKey("RustyHeathensRuneborn.DeathCount"))
		{
			return 0;
		}
		if (!int.TryParse(player.m_customData["RustyHeathensRuneborn.DeathCount"], out var result))
		{
			return 0;
		}
		return result;
	}

	private static string GetFunnyDeathMessage(int deathCount)
	{
		return deathCount switch
		{
			1 => "First blood. Odin noticed.", 
			2 => "The corpse run begins.", 
			3 => "The Rusty Heathens felt that one.", 
			5 => "Five deaths. That is a strategy now.", 
			10 => "Ten deaths. Valhalla has a reserved seat.", 
			25 => "Twenty-five deaths. The ground knows your name.", 
			50 => "Fifty deaths. The saga is mostly tombstones.", 
			100 => "One hundred deaths. Seppy would be proud.", 
			_ => "Another glorious disaster.", 
		};
	}
}
public static class RunebornMessageBroadcast
{
	private const string RpcName = "RustyHeathensMessenger_ShowCenterMessage";

	private static bool _registered;

	public static void EnsureRegistered()
	{
		if (!_registered && ZRoutedRpc.instance != null)
		{
			ZRoutedRpc.instance.Register<string>("RustyHeathensMessenger_ShowCenterMessage", (Action<long, string>)RPC_ShowCenterMessage);
			_registered = true;
		}
	}

	public static void BroadcastCenter(string message)
	{
		EnsureRegistered();
		if (ZRoutedRpc.instance == null)
		{
			ShowLocalCenter(message);
			return;
		}
		ZRoutedRpc.instance.InvokeRoutedRPC(ZRoutedRpc.Everybody, "RustyHeathensMessenger_ShowCenterMessage", new object[1] { message });
	}

	public static void ShowLocalCenter(string message)
	{
		Player localPlayer = Player.m_localPlayer;
		if (!((Object)(object)localPlayer == (Object)null))
		{
			((Character)localPlayer).Message((MessageType)2, message, 0, (Sprite)null);
		}
	}

	private static void RPC_ShowCenterMessage(long sender, string message)
	{
		ShowLocalCenter(message);
	}
}
[BepInPlugin("com.rustylasergaming.heathensmessenger", "Rusty Heathens Messenger", "0.11.0")]
public sealed class RunebornPlugin : BaseUnityPlugin
{
	public const string PluginGuid = "com.rustylasergaming.heathensmessenger";

	public const string PluginName = "Rusty Heathens Messenger";

	public const string PluginVersion = "0.11.0";

	internal static ConfigEntry<bool> ServerReminderEnabledEntry;

	internal static ConfigEntry<bool> RaidWarningsEnabledEntry;

	internal static ConfigEntry<bool> WelcomeMessageEnabledEntry;

	internal static ConfigEntry<bool> BossKillMessagesEnabledEntry;

	internal static ConfigEntry<bool> DeathMessagesEnabledEntry;

	internal static ConfigEntry<bool> ChatCommandsEnabledEntry;

	internal static ConfigEntry<string> CommandPermissionModeEntry;

	internal static ConfigEntry<string> CommandAllowedPlayerNamesEntry;

	internal static ConfigEntry<string> CommandAllowedSteamIdsEntry;

	internal static ConfigEntry<string> WelcomeTitleEntry;

	internal static ConfigEntry<string> WelcomeBodyEntry;

	internal static ConfigEntry<string> ServerReminderTitleEntry;

	internal static ConfigEntry<string> ServerReminderBodyEntry;

	internal static ConfigEntry<string> StreamTitleEntry;

	internal static ConfigEntry<string> StreamBodyEntry;

	internal static ConfigEntry<string> BossStartTitleEntry;

	internal static ConfigEntry<string> BossStartBodyEntry;

	internal static ConfigEntry<string> BaseTitleEntry;

	internal static ConfigEntry<string> BaseBodyEntry;

	internal static ConfigEntry<string> DiscordTitleEntry;

	internal static ConfigEntry<string> DiscordBodyEntry;

	internal static ConfigEntry<string> EndingTitleEntry;

	internal static ConfigEntry<string> EndingBodyEntry;

	internal static ConfigEntry<string> BodyTitleEntry;

	internal static ConfigEntry<string> BodyBodyEntry;

	internal static ConfigEntry<string> RaidCommandTitleEntry;

	internal static ConfigEntry<string> RaidCommandBodyEntry;

	internal static ConfigEntry<string> StreamStyleEntry;

	internal static ConfigEntry<string> BossStartStyleEntry;

	internal static ConfigEntry<string> BaseStyleEntry;

	internal static ConfigEntry<string> DiscordStyleEntry;

	internal static ConfigEntry<string> EndingStyleEntry;

	internal static ConfigEntry<string> BodyStyleEntry;

	internal static ConfigEntry<string> RaidCommandStyleEntry;

	internal static ConfigEntry<bool> StreamModeEnabledEntry;

	internal static ConfigEntry<bool> StreamModeStartsEnabledEntry;

	internal static ConfigEntry<string> StreamModeOnTitleEntry;

	internal static ConfigEntry<string> StreamModeOnBodyEntry;

	internal static ConfigEntry<string> StreamModeOffTitleEntry;

	internal static ConfigEntry<string> StreamModeOffBodyEntry;

	internal static ConfigEntry<string> StreamModeStatusTitleEntry;

	internal static ConfigEntry<string> StreamModeStatusOnBodyEntry;

	internal static ConfigEntry<string> StreamModeStatusOffBodyEntry;

	internal static ConfigEntry<string> StreamModeFooterEntry;

	internal static bool StreamModeActive;

	private Harmony _harmony;

	internal static RunebornPlugin Instance { get; private set; }

	private void Awake()
	{
		//IL_0015: Unknown result type (might be due to invalid IL or missing references)
		//IL_001f: Expected O, but got Unknown
		Instance = this;
		BindConfig();
		_harmony = new Harmony("com.rustylasergaming.heathensmessenger");
		_harmony.PatchAll(Assembly.GetExecutingAssembly());
		((BaseUnityPlugin)this).Logger.LogInfo((object)"Rusty Heathens Messenger 0.11.0 loaded.");
	}

	private void Update()
	{
		RunebornMessageBroadcast.EnsureRegistered();
	}

	private void BindConfig()
	{
		WelcomeMessageEnabledEntry = ((BaseUnityPlugin)this).Config.Bind<bool>("01 - Features", "EnableWelcomeMessage", true, "Show the welcome message when joining a world.");
		ServerReminderEnabledEntry = ((BaseUnityPlugin)this).Config.Bind<bool>("01 - Features", "EnableServerReminder", true, "Show the server reminder message after joining.");
		RaidWarningsEnabledEntry = ((BaseUnityPlugin)this).Config.Bind<bool>("01 - Features", "EnableRaidWarnings", true, "Show raid warning messages.");
		BossKillMessagesEnabledEntry = ((BaseUnityPlugin)this).Config.Bind<bool>("01 - Features", "EnableBossKillMessages", true, "Show boss kill messages.");
		DeathMessagesEnabledEntry = ((BaseUnityPlugin)this).Config.Bind<bool>("01 - Features", "EnableDeathMessages", true, "Show death counter and funny death messages.");
		ChatCommandsEnabledEntry = ((BaseUnityPlugin)this).Config.Bind<bool>("01 - Features", "EnableChatCommands", true, "Enable Rusty Heathens Messenger chat commands.");
		CommandPermissionModeEntry = ((BaseUnityPlugin)this).Config.Bind<string>("02 - Command Permissions", "CommandPermissionMode", "Everyone", "Who can use command messages. Options: Everyone, HostOnly, NamedPlayers, SteamIds, NamedPlayersOrSteamIds.");
		CommandAllowedPlayerNamesEntry = ((BaseUnityPlugin)this).Config.Bind<string>("02 - Command Permissions", "CommandAllowedPlayerNames", "RustyLaser", "Comma-separated player names allowed to use commands when CommandPermissionMode is NamedPlayers.");
		CommandAllowedSteamIdsEntry = ((BaseUnityPlugin)this).Config.Bind<string>("02 - Command Permissions", "CommandAllowedSteamIds", "", "Comma-separated Steam IDs allowed to use commands when CommandPermissionMode is SteamIds or NamedPlayersOrSteamIds.");
		WelcomeTitleEntry = ((BaseUnityPlugin)this).Config.Bind<string>("03 - Welcome Message", "WelcomeTitle", "Welcome to the Rusty Heathens", "Title shown in the welcome message.");
		WelcomeBodyEntry = ((BaseUnityPlugin)this).Config.Bind<string>("03 - Welcome Message", "WelcomeBody", "Survive the chaos. Earn Valhalla.", "Body shown in the welcome message.");
		ServerReminderTitleEntry = ((BaseUnityPlugin)this).Config.Bind<string>("04 - Server Reminder", "ServerReminderTitle", "RUSTY HEATHENS SERVER", "Title shown in the server reminder.");
		ServerReminderBodyEntry = ((BaseUnityPlugin)this).Config.Bind<string>("04 - Server Reminder", "ServerReminderBody", "Respect the crew.\nNo stealing.\nNo boss fights without the group.\nHelp with body recovery when you can.\nJoin Discord for rules, events, and stream info.", "Body shown in the server reminder.");
		StreamTitleEntry = ((BaseUnityPlugin)this).Config.Bind<string>("05 - Chat Commands", "StreamTitle", "STREAM STARTING SOON", "Title for stream starting command.");
		StreamBodyEntry = ((BaseUnityPlugin)this).Config.Bind<string>("05 - Chat Commands", "StreamBody", "The Rusty Heathens are gathering.\nGrab your gear, check your food, and prepare for chaos.", "Body for stream starting command.");
		BossStartTitleEntry = ((BaseUnityPlugin)this).Config.Bind<string>("05 - Chat Commands", "BossStartTitle", "BOSS FIGHT STARTING SOON", "Title for boss fight starting command.");
		BossStartBodyEntry = ((BaseUnityPlugin)this).Config.Bind<string>("05 - Chat Commands", "BossStartBody", "Return to base, repair your gear, grab food, and wait for the group.", "Body for boss fight starting command.");
		BaseTitleEntry = ((BaseUnityPlugin)this).Config.Bind<string>("05 - Chat Commands", "BaseTitle", "RETURN TO BASE", "Title for return to base command.");
		BaseBodyEntry = ((BaseUnityPlugin)this).Config.Bind<string>("05 - Chat Commands", "BaseBody", "Everyone head back to base.\nRegroup before the chaos gets worse.", "Body for return to base command.");
		DiscordTitleEntry = ((BaseUnityPlugin)this).Config.Bind<string>("05 - Chat Commands", "DiscordTitle", "DISCORD REMINDER", "Title for Discord reminder command.");
		DiscordBodyEntry = ((BaseUnityPlugin)this).Config.Bind<string>("05 - Chat Commands", "DiscordBody", "Use Discord for rules, event plans, updates, and stream info.\nMessage RustyLaser if you need help.", "Body for Discord reminder command.");
		EndingTitleEntry = ((BaseUnityPlugin)this).Config.Bind<string>("05 - Chat Commands", "EndingTitle", "STREAM IS ENDING", "Title for stream ending command.");
		EndingBodyEntry = ((BaseUnityPlugin)this).Config.Bind<string>("05 - Chat Commands", "EndingBody", "The stream is ending.\nIf you want to say bye to chat, return to the wall.", "Body for stream ending command.");
		BodyTitleEntry = ((BaseUnityPlugin)this).Config.Bind<string>("05 - Chat Commands", "BodyTitle", "HEATHEN DOWN", "Title for body recovery command.");
		BodyBodyEntry = ((BaseUnityPlugin)this).Config.Bind<string>("05 - Chat Commands", "BodyBody", "Head back to base and regroup.\nA Heathen needs help recovering their kit.", "Body for body recovery command.");
		RaidCommandTitleEntry = ((BaseUnityPlugin)this).Config.Bind<string>("05 - Chat Commands", "RaidTitle", "INCOMING RAID", "Title for incoming raid command.");
		RaidCommandBodyEntry = ((BaseUnityPlugin)this).Config.Bind<string>("05 - Chat Commands", "RaidBody", "Head back to base.\nHelp keep the Rusty Heathens safe.", "Body for incoming raid command.");
		StreamStyleEntry = ((BaseUnityPlugin)this).Config.Bind<string>("06 - Message Styles", "StreamStyle", "Normal", "Style for stream starting command. Options: Normal, Epic, Funny, Emergency.");
		BossStartStyleEntry = ((BaseUnityPlugin)this).Config.Bind<string>("06 - Message Styles", "BossStartStyle", "Epic", "Style for boss fight starting command. Options: Normal, Epic, Funny, Emergency.");
		BaseStyleEntry = ((BaseUnityPlugin)this).Config.Bind<string>("06 - Message Styles", "BaseStyle", "Normal", "Style for return to base command. Options: Normal, Epic, Funny, Emergency.");
		DiscordStyleEntry = ((BaseUnityPlugin)this).Config.Bind<string>("06 - Message Styles", "DiscordStyle", "Normal", "Style for Discord reminder command. Options: Normal, Epic, Funny, Emergency.");
		EndingStyleEntry = ((BaseUnityPlugin)this).Config.Bind<string>("06 - Message Styles", "EndingStyle", "Normal", "Style for stream ending command. Options: Normal, Epic, Funny, Emergency.");
		BodyStyleEntry = ((BaseUnityPlugin)this).Config.Bind<string>("06 - Message Styles", "BodyStyle", "Epic", "Style for body recovery command. Options: Normal, Epic, Funny, Emergency.");
		RaidCommandStyleEntry = ((BaseUnityPlugin)this).Config.Bind<string>("06 - Message Styles", "RaidStyle", "Emergency", "Style for incoming raid command. Options: Normal, Epic, Funny, Emergency.");
		StreamModeEnabledEntry = ((BaseUnityPlugin)this).Config.Bind<bool>("07 - Stream Mode", "EnableStreamMode", true, "Enable or disable Stream Mode commands and stream mode footer messages.");
		StreamModeStartsEnabledEntry = ((BaseUnityPlugin)this).Config.Bind<bool>("07 - Stream Mode", "StreamModeStartsEnabled", false, "If true, Stream Mode starts enabled when the world loads.");
		StreamModeOnTitleEntry = ((BaseUnityPlugin)this).Config.Bind<string>("07 - Stream Mode", "StreamModeOnTitle", "STREAM MODE ENABLED", "Title shown when Stream Mode is turned on.");
		StreamModeOnBodyEntry = ((BaseUnityPlugin)this).Config.Bind<string>("07 - Stream Mode", "StreamModeOnBody", "Chat is watching.\nKeep it fun, keep it moving, and prepare for Rusty Heathens chaos.", "Body shown when Stream Mode is turned on.");
		StreamModeOffTitleEntry = ((BaseUnityPlugin)this).Config.Bind<string>("07 - Stream Mode", "StreamModeOffTitle", "STREAM MODE DISABLED", "Title shown when Stream Mode is turned off.");
		StreamModeOffBodyEntry = ((BaseUnityPlugin)this).Config.Bind<string>("07 - Stream Mode", "StreamModeOffBody", "Chat has been released.\nYou may now return to unsupervised bad decisions.", "Body shown when Stream Mode is turned off.");
		StreamModeStatusTitleEntry = ((BaseUnityPlugin)this).Config.Bind<string>("07 - Stream Mode", "StreamModeStatusTitle", "STREAM MODE STATUS", "Title shown when checking Stream Mode status.");
		StreamModeStatusOnBodyEntry = ((BaseUnityPlugin)this).Config.Bind<string>("07 - Stream Mode", "StreamModeStatusOnBody", "Stream Mode is ON.\nChat is watching the chaos unfold.", "Body shown when Stream Mode is currently on.");
		StreamModeStatusOffBodyEntry = ((BaseUnityPlugin)this).Config.Bind<string>("07 - Stream Mode", "StreamModeStatusOffBody", "Stream Mode is OFF.\nThe Heathens are currently causing chaos off-camera.", "Body shown when Stream Mode is currently off.");
		StreamModeFooterEntry = ((BaseUnityPlugin)this).Config.Bind<string>("07 - Stream Mode", "StreamModeFooter", "LIVE STREAM MODE: Chat is watching.", "Footer added to broadcast command messages while Stream Mode is on.");
		StreamModeActive = StreamModeEnabledEntry.Value && StreamModeStartsEnabledEntry.Value;
		ApplyMessagingSettingsToRuntime();
	}

	internal static void ApplyMessagingSettingsToRuntime()
	{
		if (ServerReminderEnabledEntry != null)
		{
			RunebornServerReminder.ServerReminderEnabled = ServerReminderEnabledEntry.Value;
		}
		if (RaidWarningsEnabledEntry != null)
		{
			RunebornRaidWarnings.RaidWarningsEnabled = RaidWarningsEnabledEntry.Value;
		}
	}
}
public sealed class RunebornRaidWarnings : MonoBehaviour
{
	private Player _player;

	private string _activeRaid = string.Empty;

	private float _nextCheck;

	private float _nextRepeatMessage;

	public static bool RaidWarningsEnabled = true;

	private void Awake()
	{
		_player = ((Component)this).GetComponent<Player>();
	}

	private void Update()
	{
		if (!((Object)(object)_player == (Object)null) && !((Object)(object)Player.m_localPlayer != (Object)(object)_player) && RaidWarningsEnabled && !(Time.time < _nextCheck))
		{
			_nextCheck = Time.time + 1f;
			string currentRaidDisplayName = GetCurrentRaidDisplayName();
			if (!string.IsNullOrEmpty(currentRaidDisplayName) && string.IsNullOrEmpty(_activeRaid))
			{
				_activeRaid = currentRaidDisplayName;
				_nextRepeatMessage = Time.time + 45f;
				ShowCenter("RAID WARNING\n" + currentRaidDisplayName + "\nThe Rusty Heathens are under attack!");
			}
			else if (!string.IsNullOrEmpty(currentRaidDisplayName) && currentRaidDisplayName == _activeRaid && Time.time >= _nextRepeatMessage)
			{
				_nextRepeatMessage = Time.time + 45f;
				ShowCenter("RAID STILL ACTIVE\n" + currentRaidDisplayName + "\nHold the line, Heathens!");
			}
			else if (string.IsNullOrEmpty(currentRaidDisplayName) && !string.IsNullOrEmpty(_activeRaid))
			{
				ShowCenter("RAID CLEARED\nThe Heathens still stand.");
				_activeRaid = string.Empty;
				_nextRepeatMessage = 0f;
			}
		}
	}

	public static string GetCurrentRaidDisplayName()
	{
		object currentRandomEventObject = GetCurrentRandomEventObject();
		if (currentRandomEventObject == null)
		{
			return string.Empty;
		}
		string text = GetStringField(currentRandomEventObject, "m_name");
		if (string.IsNullOrEmpty(text))
		{
			text = currentRandomEventObject.ToString();
		}
		return MakeRaidNameFriendly(text);
	}

	private static object GetCurrentRandomEventObject()
	{
		try
		{
			Type type = Type.GetType("RandEventSystem, Assembly-CSharp");
			if (type == null)
			{
				return null;
			}
			object obj = GetStaticField(type, "instance") ?? GetStaticField(type, "m_instance");
			if (obj == null)
			{
				Object val = Object.FindObjectOfType(type);
				if (val != (Object)null)
				{
					obj = val;
				}
			}
			if (obj == null)
			{
				return null;
			}
			object result;
			if ((result = GetInstanceField(obj, "m_randomEvent")) == null)
			{
				result = GetInstanceField(obj, "m_currentRandomEvent") ?? GetInstanceField(obj, "m_event");
			}
			return result;
		}
		catch
		{
			return null;
		}
	}

	private static object GetStaticField(Type type, string fieldName)
	{
		FieldInfo field = type.GetField(fieldName, BindingFlags.Static | BindingFlags.Public | BindingFlags.NonPublic);
		if (!(field != null))
		{
			return null;
		}
		return field.GetValue(null);
	}

	private static object GetInstanceField(object target, string fieldName)
	{
		if (target == null)
		{
			return null;
		}
		FieldInfo field = target.GetType().GetField(fieldName, BindingFlags.Instance | BindingFlags.Public | BindingFlags.NonPublic);
		if (!(field != null))
		{
			return null;
		}
		return field.GetValue(target);
	}

	private static string GetStringField(object target, string fieldName)
	{
		object instanceField = GetInstanceField(target, fieldName);
		if (instanceField == null)
		{
			return string.Empty;
		}
		return instanceField.ToString();
	}

	private static string MakeRaidNameFriendly(string rawName)
	{
		if (string.IsNullOrEmpty(rawName))
		{
			return "Unknown Raid";
		}
		string text = rawName.ToLowerInvariant();
		if (text.Contains("army_eikthyr"))
		{
			return "Eikthyr's Herd";
		}
		if (text.Contains("army_theelder") || text.Contains("foresttrolls"))
		{
			return "The Forest Moves";
		}
		if (text.Contains("army_bonemass") || text.Contains("skeletons"))
		{
			return "Bonemass' Dead";
		}
		if (text.Contains("army_moder") || text.Contains("wolves"))
		{
			return "The Mountain Hunt";
		}
		if (text.Contains("army_goblin") || text.Contains("goblin"))
		{
			return "The Plains Warband";
		}
		if (text.Contains("army_gjall") || text.Contains("gjall"))
		{
			return "Mistlands Terror";
		}
		if (text.Contains("army_charred") || text.Contains("charred"))
		{
			return "Ashlands Siege";
		}
		return CleanName(rawName);
	}

	private static string CleanName(string rawName)
	{
		string text = rawName.Replace("$event_", string.Empty).Replace("event_", string.Empty).Replace("army_", string.Empty)
			.Replace("_", " ")
			.Trim();
		if (string.IsNullOrEmpty(text))
		{
			return "Unknown Raid";
		}
		StringBuilder stringBuilder = new StringBuilder(text.Length);
		bool flag = true;
		string text2 = text;
		foreach (char c in text2)
		{
			if (char.IsWhiteSpace(c))
			{
				stringBuilder.Append(c);
				flag = true;
			}
			else if (flag)
			{
				stringBuilder.Append(char.ToUpperInvariant(c));
				flag = false;
			}
			else
			{
				stringBuilder.Append(c);
			}
		}
		return stringBuilder.ToString();
	}

	private void ShowCenter(string text)
	{
		if ((Object)(object)_player == (Object)null)
		{
			return;
		}
		try
		{
			((Character)Player.m_localPlayer).Message((MessageType)2, text, 0, (Sprite)null);
		}
		catch
		{
		}
	}
}
[HarmonyPatch(typeof(Player), "Awake")]
public static class RunebornRaidWarningsPlayerAwakePatch
{
	private static void Postfix(Player __instance)
	{
		if (!((Object)(object)__instance == (Object)null) && (Object)(object)((Component)__instance).GetComponent<RunebornRaidWarnings>() == (Object)null)
		{
			((Component)__instance).gameObject.AddComponent<RunebornRaidWarnings>();
		}
	}
}
public sealed class RunebornServerReminder : MonoBehaviour
{
	private static bool _serverReminderShownThisGameSession;

	private Player _player;

	private int _messagesShown;

	private float _nextMessageAt;

	public static bool ServerReminderEnabled = true;

	private const int ReminderRepeatCount = 1;

	private const float ReminderFirstDelay = 16f;

	private const float ReminderRepeatDelay = 6f;

	private const string ReminderText = "RUSTY HEATHENS SERVER\n\nRespect the crew.\nNo stealing.\nNo boss fights without the group.\nHelp with body recovery when you can.\nJoin Discord for rules, events, and updates.\n\nSkal, Heathens!";

	private void Awake()
	{
		_player = ((Component)this).GetComponent<Player>();
		_nextMessageAt = Time.time + 16f;
		if (_serverReminderShownThisGameSession)
		{
			_messagesShown = 1;
		}
	}

	private void Update()
	{
		if (_messagesShown < 1 && ServerReminderEnabled && !((Object)(object)_player == (Object)null) && !((Object)(object)Player.m_localPlayer != (Object)(object)_player) && !(Time.time < _nextMessageAt))
		{
			try
			{
				string text = ((RunebornPlugin.ServerReminderTitleEntry != null) ? RunebornPlugin.ServerReminderTitleEntry.Value : "RUSTY HEATHENS SERVER");
				string text2 = ((RunebornPlugin.ServerReminderBodyEntry != null) ? RunebornPlugin.ServerReminderBodyEntry.Value : "Respect the crew.\nNo stealing.\nNo boss fights without the group.\nHelp with body recovery when you can.\nJoin Discord for rules, events, and stream info.");
				((Character)_player).Message((MessageType)2, text + "\n\n" + text2, 0, (Sprite)null);
			}
			catch
			{
			}
			_messagesShown++;
			_serverReminderShownThisGameSession = true;
			_nextMessageAt = Time.time + 6f;
		}
	}
}
[HarmonyPatch(typeof(Player), "Awake")]
public static class RunebornServerReminderPlayerAwakePatch
{
	private static void Postfix(Player __instance)
	{
		if (!((Object)(object)__instance == (Object)null) && (Object)(object)((Component)__instance).GetComponent<RunebornServerReminder>() == (Object)null)
		{
			((Component)__instance).gameObject.AddComponent<RunebornServerReminder>();
		}
	}
}
public sealed class RunebornWelcomeMessageOnce : MonoBehaviour
{
	private static bool _welcomeShownThisGameSession;

	private Player _player;

	private bool _shown;

	private float _showAt;

	private const string WelcomeText = "Welcome to the Rusty Heathens\nSurvive the chaos. Earn Valhalla.";

	private void Awake()
	{
		_player = ((Component)this).GetComponent<Player>();
		_showAt = Time.time + 5f;
		if (_welcomeShownThisGameSession)
		{
			_shown = true;
		}
	}

	private void Update()
	{
		if (_shown || (Object)(object)_player == (Object)null || (Object)(object)Player.m_localPlayer != (Object)(object)_player || Time.time < _showAt)
		{
			return;
		}
		_shown = true;
		_welcomeShownThisGameSession = true;
		try
		{
			if (RunebornPlugin.WelcomeMessageEnabledEntry == null || RunebornPlugin.WelcomeMessageEnabledEntry.Value)
			{
				string text = ((RunebornPlugin.WelcomeTitleEntry != null) ? RunebornPlugin.WelcomeTitleEntry.Value : "Welcome to the Rusty Heathens");
				string text2 = ((RunebornPlugin.WelcomeBodyEntry != null) ? RunebornPlugin.WelcomeBodyEntry.Value : "Survive the chaos. Earn Valhalla.");
				((Character)_player).Message((MessageType)2, text + "\n" + text2, 0, (Sprite)null);
			}
		}
		catch
		{
		}
	}
}
[HarmonyPatch(typeof(Player), "Awake")]
public static class RunebornWelcomeMessageOncePlayerAwakePatch
{
	private static void Postfix(Player __instance)
	{
		if (!((Object)(object)__instance == (Object)null) && (Object)(object)((Component)__instance).GetComponent<RunebornWelcomeMessageOnce>() == (Object)null)
		{
			((Component)__instance).gameObject.AddComponent<RunebornWelcomeMessageOnce>();
		}
	}
}