Decompiled source of IHaveArrived v1.1.0

IHaveArrived.dll

Decompiled 2 weeks ago
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.IO;
using System.Reflection;
using System.Runtime.CompilerServices;
using System.Runtime.InteropServices;
using System.Runtime.Versioning;
using BepInEx;
using BepInEx.Configuration;
using HarmonyLib;
using UnityEngine;

[assembly: CompilationRelaxations(8)]
[assembly: RuntimeCompatibility(WrapNonExceptionThrows = true)]
[assembly: Debuggable(DebuggableAttribute.DebuggingModes.IgnoreSymbolStoreSequencePoints)]
[assembly: AssemblyTitle("IHaveArrived")]
[assembly: AssemblyDescription("")]
[assembly: AssemblyConfiguration("")]
[assembly: AssemblyCompany("Riintouge")]
[assembly: AssemblyProduct("IHaveArrived")]
[assembly: AssemblyCopyright("Copyright © 2026")]
[assembly: AssemblyTrademark("")]
[assembly: ComVisible(false)]
[assembly: Guid("2c76457c-d2c8-4695-a885-25411182c928")]
[assembly: AssemblyFileVersion("1.1.0.0")]
[assembly: TargetFramework(".NETFramework,Version=v4.7.2", FrameworkDisplayName = ".NET Framework 4.7.2")]
[assembly: AssemblyVersion("1.1.0.0")]
namespace IHaveArrived;

[BepInPlugin("com.riintouge.ihavearrived", "I Have Arrived", "1.1.0")]
[BepInProcess("valheim.exe")]
public class IHaveArrived : BaseUnityPlugin
{
	[HarmonyPatch(typeof(Chat))]
	private class ChatPatch
	{
		public static bool DiscardNextDefaultArrivalMessage;

		[HarmonyPrefix]
		[HarmonyPatch(typeof(Chat), "SendText")]
		private static bool SendTextPrefix(Type type, string text)
		{
			//IL_0007: Unknown result type (might be due to invalid IL or missing references)
			//IL_0009: Invalid comparison between Unknown and I4
			if (DiscardNextDefaultArrivalMessage && (int)type == 2 && text == Localization.instance.Localize("$text_player_arrived"))
			{
				DiscardNextDefaultArrivalMessage = false;
				return false;
			}
			return true;
		}
	}

	[HarmonyPatch(typeof(Game))]
	private class GamePatch
	{
		private static Random Rand = new Random();

		private static bool FirstSpawnPending = false;

		[HarmonyPrefix]
		[HarmonyPatch(typeof(Game), "UpdateRespawn")]
		[HarmonyPriority(800)]
		private static void UpdateRespawnPrefix(ref bool ___m_firstSpawn)
		{
			FirstSpawnPending = false;
			if (IsEnabled.Value & ___m_firstSpawn)
			{
				FirstSpawnPending = true;
				ChatPatch.DiscardNextDefaultArrivalMessage = true;
			}
		}

		[HarmonyPostfix]
		[HarmonyPatch(typeof(Game), "UpdateRespawn")]
		private static void UpdateRespawnPostfix()
		{
			if (FirstSpawnPending && (Object)(object)Player.m_localPlayer != (Object)null)
			{
				FirstSpawnPending = false;
				if (AnnounceArrival.Value)
				{
					List<string> list = Common.LoadAnnouncements();
					string text = ((list != null && list.Count > 0) ? list[Rand.Next(0, list.Count - 1)] : Localization.instance.Localize("$text_player_arrived"));
					Chat.instance.SendText((Type)2, text);
				}
			}
		}
	}

	public static ConfigEntry<bool> IsEnabled;

	public static ConfigEntry<bool> LoadOnStart;

	public static ConfigEntry<bool> AnnounceArrival;

	private readonly Harmony Harmony = new Harmony("com.riintouge.ihavearrived");

	private void Awake()
	{
		IsEnabled = ((BaseUnityPlugin)this).Config.Bind<bool>("0 - Core", "Enable", true, "Whether this plugin has any effect when loaded.");
		LoadOnStart = ((BaseUnityPlugin)this).Config.Bind<bool>("0 - Core", "LoadOnStart", true, "Whether this plugin loads on game start.");
		AnnounceArrival = ((BaseUnityPlugin)this).Config.Bind<bool>("1 - Main", "AnnounceArrival", true, "Whether the player shouts a very very Swedish sentence upon joining a world. When false, the player will not announce their arrival.");
		if (LoadOnStart.Value)
		{
			Harmony.PatchAll();
		}
	}
}
internal class Common
{
	public static readonly int AnnouncementLengthLimit = 150;

	public static readonly string AnnouncementsFilePath = Path.Combine(Path.GetDirectoryName(Assembly.GetExecutingAssembly().Location), "announcements.txt");

	public static readonly string MessagesFilePath = Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.ApplicationData), "givenameplz", "ValheimIHaveArrived", "messages.txt");

	public static List<string> LoadAnnouncements()
	{
		List<string> list = LoadAnnouncements(AnnouncementsFilePath);
		if (list == null || list.Count <= 0)
		{
			return LoadAnnouncements(MessagesFilePath);
		}
		return list;
	}

	public static List<string> LoadAnnouncements(string announcementsFilePath)
	{
		if (!File.Exists(announcementsFilePath))
		{
			return null;
		}
		List<string> list = new List<string>();
		string[] array = File.ReadAllLines(announcementsFilePath);
		foreach (string text in array)
		{
			if (!Utility.IsNullOrWhiteSpace(text) && text.Length <= AnnouncementLengthLimit)
			{
				list.Add(text);
			}
		}
		return list;
	}
}