Decompiled source of IHaveArrived v1.0.2

gnp_IHaveArrived/ValheimIHaveArrived.dll

Decompiled 4 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.InteropServices;
using System.Runtime.Versioning;
using BepInEx;
using HarmonyLib;
using UnityEngine;

[assembly: CompilationRelaxations(8)]
[assembly: RuntimeCompatibility(WrapNonExceptionThrows = true)]
[assembly: Debuggable(DebuggableAttribute.DebuggingModes.IgnoreSymbolStoreSequencePoints)]
[assembly: AssemblyTitle("ValheimIHaveArrived")]
[assembly: AssemblyDescription("")]
[assembly: AssemblyConfiguration("")]
[assembly: AssemblyCompany("")]
[assembly: AssemblyProduct("ValheimIHaveArrived")]
[assembly: AssemblyCopyright("Copyright ©  2021")]
[assembly: AssemblyTrademark("")]
[assembly: ComVisible(false)]
[assembly: Guid("ef6adba8-bcc3-4be1-9f9b-2964c344e4d8")]
[assembly: AssemblyFileVersion("1.0.1.0")]
[assembly: TargetFramework(".NETFramework,Version=v4.8", FrameworkDisplayName = ".NET Framework 4.8")]
[assembly: AssemblyVersion("1.0.1.0")]
namespace ValheimIHaveArrived;

internal class ConsoleAndChat
{
	[HarmonyPatch(typeof(Terminal), "InitTerminal")]
	private class AddCommands
	{
		[Serializable]
		[CompilerGenerated]
		private sealed class <>c
		{
			public static readonly <>c <>9 = new <>c();

			public static ConsoleEvent <>9__0_0;

			internal void <Postfix>b__0_0(ConsoleEventArgs args)
			{
				if (args.Args.Length == 1)
				{
					args.Context.AddString("<color=purple>I have arrived! (by givenameplz)</color> [1.0.2]");
					args.Context.AddString("Turn off or change those annoying messages when joining a server/world.");
					args.Context.AddString("");
					args.Context.AddString("arrived add <color=#ff6666>[message:string]</color> <color=#666666>(Add a new message to the list of possible messages to appear.)</color>");
					args.Context.AddString("arrived remove <color=#6666ff>[index:int]</color> <color=#666666>(Remove the message at the given index.)</color>");
					args.Context.AddString("arrived list <color=#666666>(Shows the list of added messages with their respective index.)</color>");
					args.Context.AddString("arrived clear <color=#666666>(Deletes ALL messages, with no confirmation!)</color>");
					args.Context.AddString("arrived reload <color=#666666>(Reloads all messages and settings from the files.)</color>");
					args.Context.AddString("arrived announce <color=#66ff66>[switch:bool]</color> <color=#666666>(Turn announcing on or off when joining a server/world.)</color>");
					args.Context.AddString("");
					args.Context.AddString("<color=orange>EXAMPLE:</color> arrived add I have returned!");
					args.Context.AddString("<color=orange>EXAMPLE:</color> arrived announce false");
					args.Context.AddString("<color=orange>EXAMPLE:</color> arrived list");
					args.Context.AddString("");
					args.Context.AddString("Announcing Arrival: " + (IHaveArrived.AnnounceArrival ? "<color=green>ON</color>" : "<color=red>OFF</color>"));
					args.Context.AddString(string.Format("Saved Messages: <color={0}>{1}</color>", (IHaveArrived.Messages.Count > 0) ? "green" : "red", IHaveArrived.Messages.Count));
					return;
				}
				if (args.HasArgumentAnywhere("announce", 0, true))
				{
					if (args.HasArgumentAnywhere("on", 0, true) || args.HasArgumentAnywhere("true", 0, true))
					{
						IHaveArrived.AnnounceArrival = true;
						args.Context.AddString("Announcing Arrival: <color=green>ON</color>");
					}
					else if (args.HasArgumentAnywhere("off", 0, true) || args.HasArgumentAnywhere("false", 0, true))
					{
						IHaveArrived.AnnounceArrival = false;
						args.Context.AddString("Announcing Arrival: <color=red>OFF</color>");
					}
					else
					{
						args.Context.AddString("<color=red>Error:</color> Invalid value '" + args.Args[1] + "' for 'Announcing Arrival'");
					}
					IHaveArrived.WriteFile();
					return;
				}
				if (args.HasArgumentAnywhere("add", 0, true))
				{
					if (args.Args.Length == 3)
					{
						IHaveArrived.Messages.Add(args.Args[2]);
						args.Context.AddString($"<color=green>New message '{IHaveArrived.ShortDisplayString(args.Args[2])}' has been added at index {IHaveArrived.Messages.Count - 1}.</color>");
						IHaveArrived.WriteFile();
					}
					return;
				}
				if (args.HasArgumentAnywhere("remove", 0, true))
				{
					if (args.Args.Length == 3 && int.TryParse(args.Args[2], out var result) && result < IHaveArrived.Messages.Count)
					{
						args.Context.AddString($"<color=red>Index {result} containing '{IHaveArrived.ShortDisplayString(IHaveArrived.Messages[result])}' has been removed.</color>");
						IHaveArrived.Messages.RemoveAt(result);
						IHaveArrived.WriteFile();
					}
					return;
				}
				if (args.HasArgumentAnywhere("list", 0, true))
				{
					int num = 0;
					{
						foreach (string message in IHaveArrived.Messages)
						{
							args.Context.AddString($"{num++}: {IHaveArrived.ShortDisplayString(message)}");
						}
						return;
					}
				}
				if (args.HasArgumentAnywhere("clear", 0, true))
				{
					IHaveArrived.Messages.Clear();
					args.Context.AddString("<color=red>All arrival messages have been deleted.</color>");
					IHaveArrived.WriteFile();
				}
				else if (args.HasArgumentAnywhere("reload", 0, true))
				{
					args.Context.AddString("<color=green>Messages and Settings have been reloaded.</color>");
					IHaveArrived.LoadFile();
				}
				else
				{
					args.Context.AddString("<color=red>Error:</color> '" + args.FullLine + "' is not a recognized command for IHaveArrived! Type 'arrived' to see a list of valid commands.");
				}
			}
		}

		private static void Postfix(Terminal __instance)
		{
			//IL_0032: 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_0023: Unknown result type (might be due to invalid IL or missing references)
			//IL_0029: Expected O, but got Unknown
			object obj = <>c.<>9__0_0;
			if (obj == null)
			{
				ConsoleEvent val = delegate(ConsoleEventArgs args)
				{
					if (args.Args.Length == 1)
					{
						args.Context.AddString("<color=purple>I have arrived! (by givenameplz)</color> [1.0.2]");
						args.Context.AddString("Turn off or change those annoying messages when joining a server/world.");
						args.Context.AddString("");
						args.Context.AddString("arrived add <color=#ff6666>[message:string]</color> <color=#666666>(Add a new message to the list of possible messages to appear.)</color>");
						args.Context.AddString("arrived remove <color=#6666ff>[index:int]</color> <color=#666666>(Remove the message at the given index.)</color>");
						args.Context.AddString("arrived list <color=#666666>(Shows the list of added messages with their respective index.)</color>");
						args.Context.AddString("arrived clear <color=#666666>(Deletes ALL messages, with no confirmation!)</color>");
						args.Context.AddString("arrived reload <color=#666666>(Reloads all messages and settings from the files.)</color>");
						args.Context.AddString("arrived announce <color=#66ff66>[switch:bool]</color> <color=#666666>(Turn announcing on or off when joining a server/world.)</color>");
						args.Context.AddString("");
						args.Context.AddString("<color=orange>EXAMPLE:</color> arrived add I have returned!");
						args.Context.AddString("<color=orange>EXAMPLE:</color> arrived announce false");
						args.Context.AddString("<color=orange>EXAMPLE:</color> arrived list");
						args.Context.AddString("");
						args.Context.AddString("Announcing Arrival: " + (IHaveArrived.AnnounceArrival ? "<color=green>ON</color>" : "<color=red>OFF</color>"));
						args.Context.AddString(string.Format("Saved Messages: <color={0}>{1}</color>", (IHaveArrived.Messages.Count > 0) ? "green" : "red", IHaveArrived.Messages.Count));
					}
					else if (args.HasArgumentAnywhere("announce", 0, true))
					{
						if (args.HasArgumentAnywhere("on", 0, true) || args.HasArgumentAnywhere("true", 0, true))
						{
							IHaveArrived.AnnounceArrival = true;
							args.Context.AddString("Announcing Arrival: <color=green>ON</color>");
						}
						else if (args.HasArgumentAnywhere("off", 0, true) || args.HasArgumentAnywhere("false", 0, true))
						{
							IHaveArrived.AnnounceArrival = false;
							args.Context.AddString("Announcing Arrival: <color=red>OFF</color>");
						}
						else
						{
							args.Context.AddString("<color=red>Error:</color> Invalid value '" + args.Args[1] + "' for 'Announcing Arrival'");
						}
						IHaveArrived.WriteFile();
					}
					else if (args.HasArgumentAnywhere("add", 0, true))
					{
						if (args.Args.Length == 3)
						{
							IHaveArrived.Messages.Add(args.Args[2]);
							args.Context.AddString($"<color=green>New message '{IHaveArrived.ShortDisplayString(args.Args[2])}' has been added at index {IHaveArrived.Messages.Count - 1}.</color>");
							IHaveArrived.WriteFile();
						}
					}
					else if (args.HasArgumentAnywhere("remove", 0, true))
					{
						if (args.Args.Length == 3 && int.TryParse(args.Args[2], out var result) && result < IHaveArrived.Messages.Count)
						{
							args.Context.AddString($"<color=red>Index {result} containing '{IHaveArrived.ShortDisplayString(IHaveArrived.Messages[result])}' has been removed.</color>");
							IHaveArrived.Messages.RemoveAt(result);
							IHaveArrived.WriteFile();
						}
					}
					else
					{
						if (args.HasArgumentAnywhere("list", 0, true))
						{
							int num = 0;
							{
								foreach (string message in IHaveArrived.Messages)
								{
									args.Context.AddString($"{num++}: {IHaveArrived.ShortDisplayString(message)}");
								}
								return;
							}
						}
						if (args.HasArgumentAnywhere("clear", 0, true))
						{
							IHaveArrived.Messages.Clear();
							args.Context.AddString("<color=red>All arrival messages have been deleted.</color>");
							IHaveArrived.WriteFile();
						}
						else if (args.HasArgumentAnywhere("reload", 0, true))
						{
							args.Context.AddString("<color=green>Messages and Settings have been reloaded.</color>");
							IHaveArrived.LoadFile();
						}
						else
						{
							args.Context.AddString("<color=red>Error:</color> '" + args.FullLine + "' is not a recognized command for IHaveArrived! Type 'arrived' to see a list of valid commands.");
						}
					}
				};
				<>c.<>9__0_0 = val;
				obj = (object)val;
			}
			new ConsoleCommand("arrived", "<color=red>[MOD]</color> Turn off or change those annoying messages when joining a server/world.", (ConsoleEvent)obj, false, false, false, false, false, (ConsoleOptionsFetcher)null, false, false, false);
		}
	}
}
public static class DebugLogger
{
	public enum LogTypes
	{
		Default,
		Warning,
		Error
	}

	public static bool Enabled = true;

	public static string Prefix = "";

	public static LogTypes MinLogLevel = LogTypes.Warning;

	public static void Log(string str)
	{
		Log(str, LogTypes.Default);
	}

	public static void LogWarning(string str)
	{
		Log(str, LogTypes.Warning);
	}

	public static void LogError(string str)
	{
		Log(str, LogTypes.Error);
	}

	private static void Log(string str, LogTypes type = LogTypes.Default)
	{
		if (Enabled && type >= MinLogLevel)
		{
			str = Prefix + " " + str;
			switch (type)
			{
			case LogTypes.Default:
				Debug.Log((object)str);
				break;
			case LogTypes.Warning:
				Debug.LogWarning((object)str);
				break;
			case LogTypes.Error:
				Debug.LogError((object)str);
				break;
			}
		}
	}
}
[BepInPlugin("de.givenameplz.ihavearrived", "I have arrived! (by givenameplz)", "1.0.2")]
[HarmonyPatch]
public class IHaveArrived : BaseUnityPlugin
{
	public const string GUID = "de.givenameplz.ihavearrived";

	public const string DisplayName = "I have arrived! (by givenameplz)";

	public const string Version = "1.0.2";

	public const string Description = "Turn off or change those annoying messages when joining a server/world.";

	private static readonly string AppDataPath = Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.ApplicationData), "givenameplz", "ValheimIHaveArrived");

	private static readonly string FilenameMessages = Path.Combine(AppDataPath, "messages.txt");

	private static readonly string FilenameSettings = Path.Combine(AppDataPath, "settings.cfg");

	public const BindingFlags Flags = BindingFlags.Instance | BindingFlags.Static | BindingFlags.Public | BindingFlags.NonPublic;

	private static bool FirstLogin = false;

	public static List<string> Messages = new List<string>();

	public static bool AnnounceArrival = AnnounceArrivalDefault;

	public static readonly bool AnnounceArrivalDefault = true;

	public static Random Rand = new Random();

	private void Awake()
	{
		//IL_000f: Unknown result type (might be due to invalid IL or missing references)
		DebugLogger.Prefix = "de.givenameplz.ihavearrived";
		new Harmony("de.givenameplz.ihavearrived").PatchAll();
		DebugLogger.Log("I have arrived!");
		Directory.CreateDirectory(AppDataPath);
		LoadFile();
	}

	public static void LoadFile()
	{
		Messages = new List<string>();
		string[] array;
		if (File.Exists(FilenameMessages))
		{
			array = File.ReadAllLines(FilenameMessages);
			foreach (string text in array)
			{
				Messages.Add(text);
				DebugLogger.Log("Messages: " + ShortDisplayString(text));
			}
		}
		AnnounceArrival = AnnounceArrivalDefault;
		if (!File.Exists(FilenameSettings))
		{
			return;
		}
		array = File.ReadAllLines(FilenameSettings);
		for (int i = 0; i < array.Length; i++)
		{
			string[] array2 = array[i].Split(new char[1] { '=' }, 2);
			if (array2.Length == 2 && array2[0] == "announce" && bool.TryParse(array2[1], out var result))
			{
				AnnounceArrival = result;
				DebugLogger.Log("Setting: " + array2[0] + "=" + array2[1]);
			}
		}
	}

	public static void WriteFile()
	{
		File.WriteAllLines(FilenameMessages, Messages.ToList());
		string[] contents = new string[1] { "announce=" + (AnnounceArrival ? "true" : "false") };
		File.WriteAllLines(FilenameSettings, contents);
	}

	public static string ShortDisplayString(string str)
	{
		string text = "";
		int num = 150;
		int length = 100;
		int num2 = 25;
		if (str.Length > num)
		{
			string text2 = str.Substring(0, length);
			string text3 = str.Substring(str.Length - num2);
			text = text + text2 + "...";
			return text + text3;
		}
		return str;
	}

	[HarmonyPrefix]
	[HarmonyPatch(typeof(Game), "UpdateRespawn")]
	private static void PreUpdate(ref Game __instance)
	{
		if ((bool)typeof(Game).GetField("m_firstSpawn", BindingFlags.Instance | BindingFlags.Static | BindingFlags.Public | BindingFlags.NonPublic).GetValue(__instance))
		{
			typeof(Game).GetField("m_firstSpawn", BindingFlags.Instance | BindingFlags.Static | BindingFlags.Public | BindingFlags.NonPublic).SetValue(__instance, false);
			FirstLogin = true;
		}
	}

	[HarmonyPostfix]
	[HarmonyPatch(typeof(Game), "UpdateRespawn")]
	private static void PostUpdate(ref Game __instance)
	{
		if (FirstLogin && (Object)(object)Player.m_localPlayer != (Object)null)
		{
			FirstLogin = false;
			if (AnnounceArrival)
			{
				string text = ((Messages.Count > 0) ? Messages[Rand.Next(0, Messages.Count - 1)] : "I have arrived!");
				Chat.instance.SendText((Type)2, text);
			}
		}
	}
}