Decompiled source of Incognito Mode v0.4.0

Plugins/VentureValheim.IncognitoMode.dll

Decompiled 6 months 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 System.Security;
using System.Security.Permissions;
using BepInEx;
using BepInEx.Configuration;
using BepInEx.Logging;
using HarmonyLib;
using Microsoft.CodeAnalysis;
using UnityEngine;

[assembly: CompilationRelaxations(8)]
[assembly: RuntimeCompatibility(WrapNonExceptionThrows = true)]
[assembly: Debuggable(DebuggableAttribute.DebuggingModes.IgnoreSymbolStoreSequencePoints)]
[assembly: AssemblyTitle("VentureValheim.IncognitoMode")]
[assembly: AssemblyDescription("")]
[assembly: AssemblyConfiguration("")]
[assembly: AssemblyCompany("")]
[assembly: AssemblyProduct("VentureValheim.IncognitoMode")]
[assembly: AssemblyCopyright("Copyright ©  2022")]
[assembly: AssemblyTrademark("")]
[assembly: ComVisible(false)]
[assembly: Guid("7BDF2320-F171-4843-84B4-95C86CD03E77")]
[assembly: AssemblyFileVersion("0.4.0")]
[assembly: TargetFramework(".NETFramework,Version=v4.7.2", FrameworkDisplayName = ".NET Framework 4.7.2")]
[assembly: SecurityPermission(SecurityAction.RequestMinimum, SkipVerification = true)]
[assembly: AssemblyVersion("0.4.0.0")]
[module: UnverifiableCode]
namespace Microsoft.CodeAnalysis
{
	[CompilerGenerated]
	[Microsoft.CodeAnalysis.Embedded]
	internal sealed class EmbeddedAttribute : Attribute
	{
	}
}
namespace System.Runtime.CompilerServices
{
	[CompilerGenerated]
	[Microsoft.CodeAnalysis.Embedded]
	[AttributeUsage(AttributeTargets.Class | AttributeTargets.Property | AttributeTargets.Field | AttributeTargets.Event | AttributeTargets.Parameter | AttributeTargets.ReturnValue | AttributeTargets.GenericParameter, AllowMultiple = false, Inherited = false)]
	internal sealed class NullableAttribute : Attribute
	{
		public readonly byte[] NullableFlags;

		public NullableAttribute(byte P_0)
		{
			NullableFlags = new byte[1] { P_0 };
		}

		public NullableAttribute(byte[] P_0)
		{
			NullableFlags = P_0;
		}
	}
	[CompilerGenerated]
	[Microsoft.CodeAnalysis.Embedded]
	[AttributeUsage(AttributeTargets.Class | AttributeTargets.Struct | AttributeTargets.Method | AttributeTargets.Interface | AttributeTargets.Delegate, AllowMultiple = false, Inherited = false)]
	internal sealed class NullableContextAttribute : Attribute
	{
		public readonly byte Flag;

		public NullableContextAttribute(byte P_0)
		{
			Flag = P_0;
		}
	}
}
namespace VentureValheim.IncognitoMode
{
	[BepInDependency(/*Could not decode attribute arguments.*/)]
	[BepInPlugin("com.orianaventure.mod.IncognitoMode", "IncognitoMode", "0.4.0")]
	public class IncognitoModePlugin : BaseUnityPlugin
	{
		private const string ModName = "IncognitoMode";

		private const string ModVersion = "0.4.0";

		private const string Author = "com.orianaventure.mod";

		private const string ModGUID = "com.orianaventure.mod.IncognitoMode";

		private static string ConfigFileName = "com.orianaventure.mod.IncognitoMode.cfg";

		private static string ConfigFileFullPath;

		private readonly Harmony HarmonyInstance = new Harmony("com.orianaventure.mod.IncognitoMode");

		public static readonly ManualLogSource IncognitoModeLogger;

		internal static ConfigEntry<string> CE_HiddenByItems;

		internal static ConfigEntry<string> CE_HiddenDisplayName;

		internal static ConfigEntry<bool> CE_HideNameInChat;

		internal static ConfigEntry<bool> CE_HidePlatformTag;

		private readonly ConfigurationManagerAttributes AdminConfig = new ConfigurationManagerAttributes
		{
			IsAdminOnly = true
		};

		private readonly ConfigurationManagerAttributes ClientConfig = new ConfigurationManagerAttributes
		{
			IsAdminOnly = false
		};

		public static string GetHiddenByItems()
		{
			return CE_HiddenByItems.Value;
		}

		public static string GetHiddenDisplayName()
		{
			return CE_HiddenDisplayName.Value;
		}

		public static bool GetHideNameInChat()
		{
			return CE_HideNameInChat.Value;
		}

		public static bool GetHidePlatformTag()
		{
			return CE_HidePlatformTag.Value;
		}

		private void AddConfig<T>(string key, string section, string description, bool synced, T value, ref ConfigEntry<T> configEntry)
		{
			//IL_0033: Unknown result type (might be due to invalid IL or missing references)
			//IL_003d: Expected O, but got Unknown
			string extendedDescription = GetExtendedDescription(description, synced);
			configEntry = ((BaseUnityPlugin)this).Config.Bind<T>(section, key, value, new ConfigDescription(extendedDescription, (AcceptableValueBase)null, new object[1] { synced ? AdminConfig : ClientConfig }));
		}

		public string GetExtendedDescription(string description, bool synchronizedSetting)
		{
			return description + (synchronizedSetting ? " [Synced with Server]" : " [Not Synced with Server]");
		}

		public void Awake()
		{
			AddConfig("HiddenByItems", "General", "Prefab names of helmet/shoulder items that can hide a Player's name (comma-separated string).", synced: true, "HelmetRoot, HelmetFenring, HelmetPadded, HelmetMage_Ashlands, HelmetFlametal", ref CE_HiddenByItems);
			AddConfig("HiddenDisplayName", "General", "The hidden Player's display name (string).", synced: true, "???", ref CE_HiddenDisplayName);
			AddConfig("HideNameInChat", "General", "When hidden also hides the name in chat (boolean).", synced: true, value: true, ref CE_HideNameInChat);
			AddConfig("HidePlatformTag", "General", "When hidden also hides steam/xbox platform tags (boolean).", synced: true, value: false, ref CE_HidePlatformTag);
			IncognitoModeLogger.LogInfo((object)"I wear a mask, and that mask is not to hide who I am, but to create who I am.");
			Assembly executingAssembly = Assembly.GetExecutingAssembly();
			HarmonyInstance.PatchAll(executingAssembly);
			SetupWatcher();
		}

		private void OnDestroy()
		{
			((BaseUnityPlugin)this).Config.Save();
		}

		private void SetupWatcher()
		{
			FileSystemWatcher fileSystemWatcher = new FileSystemWatcher(Paths.ConfigPath, ConfigFileName);
			fileSystemWatcher.Changed += ReadConfigValues;
			fileSystemWatcher.Created += ReadConfigValues;
			fileSystemWatcher.Renamed += ReadConfigValues;
			fileSystemWatcher.IncludeSubdirectories = true;
			fileSystemWatcher.SynchronizingObject = ThreadingHelper.SynchronizingObject;
			fileSystemWatcher.EnableRaisingEvents = true;
		}

		private void ReadConfigValues(object sender, FileSystemEventArgs e)
		{
			if (!File.Exists(ConfigFileFullPath))
			{
				return;
			}
			try
			{
				IncognitoModeLogger.LogDebug((object)"Attempting to reload configuration...");
				((BaseUnityPlugin)this).Config.Reload();
			}
			catch
			{
				IncognitoModeLogger.LogError((object)("There was an issue loading " + ConfigFileName));
			}
		}

		static IncognitoModePlugin()
		{
			string configPath = Paths.ConfigPath;
			char directorySeparatorChar = Path.DirectorySeparatorChar;
			ConfigFileFullPath = configPath + directorySeparatorChar + ConfigFileName;
			IncognitoModeLogger = Logger.CreateLogSource("IncognitoMode");
			CE_HiddenByItems = null;
			CE_HiddenDisplayName = null;
			CE_HideNameInChat = null;
			CE_HidePlatformTag = null;
		}
	}
	public class IncognitoMode
	{
		[HarmonyPatch(typeof(Player), "GetHoverName")]
		public static class Patch_Player_GetHoverName
		{
			private static void Postfix(Player __instance, ref string __result)
			{
				if (!((Object)(object)((Character)__instance).m_nview == (Object)null))
				{
					ZDO zDO = ((Character)__instance).m_nview.GetZDO();
					if (zDO != null && zDO.GetBool("VV_NameHidden", false))
					{
						__result = GetDisplayName();
					}
				}
			}
		}

		[HarmonyPatch(typeof(Player), "SetupVisEquipment")]
		public static class Patch_Player_SetupVisEquipment
		{
			private static void Postfix(Player __instance)
			{
				if ((Object)(object)((Character)__instance).m_nview == (Object)null || !((Character)__instance).m_nview.IsOwner())
				{
					return;
				}
				ZDO zDO = ((Character)__instance).m_nview.GetZDO();
				if (zDO != null)
				{
					Instance.Update();
					bool flag = false;
					ItemData helmetItem = ((Humanoid)__instance).m_helmetItem;
					ItemData shoulderItem = ((Humanoid)__instance).m_shoulderItem;
					ItemData utilityItem = ((Humanoid)__instance).m_utilityItem;
					if (helmetItem != null && (Object)(object)helmetItem.m_dropPrefab != (Object)null && Instance.ItemPrefabs.Contains(((Object)helmetItem.m_dropPrefab).name))
					{
						flag = true;
					}
					else if (shoulderItem != null && (Object)(object)shoulderItem.m_dropPrefab != (Object)null && Instance.ItemPrefabs.Contains(((Object)shoulderItem.m_dropPrefab).name))
					{
						flag = true;
					}
					else if (utilityItem != null && (Object)(object)utilityItem.m_dropPrefab != (Object)null && Instance.ItemPrefabs.Contains(((Object)utilityItem.m_dropPrefab).name))
					{
						flag = true;
					}
					zDO.Set("VV_NameHidden", flag);
				}
			}
		}

		[HarmonyPriority(500)]
		[HarmonyPatch(typeof(UserInfo), "GetDisplayName")]
		public static class Patch_UserInfo_GetDisplayName
		{
			private static void Prefix(ref UserInfo __instance)
			{
				if (!IncognitoModePlugin.GetHideNameInChat())
				{
					return;
				}
				Player val = null;
				List<Player> s_players = Player.s_players;
				for (int i = 0; i < s_players.Count; i++)
				{
					if (s_players[i].GetPlayerName().Equals(__instance.Name))
					{
						val = s_players[i];
						break;
					}
				}
				bool flag = false;
				if ((Object)(object)val != (Object)null && (Object)(object)((Character)val).m_nview != (Object)null)
				{
					ZDO zDO = ((Character)val).m_nview.GetZDO();
					if (zDO != null)
					{
						flag = zDO.GetBool("VV_NameHidden", false);
					}
				}
				if (flag)
				{
					__instance.Name = GetDisplayName();
					if (IncognitoModePlugin.GetHidePlatformTag())
					{
						__instance.Gamertag = "";
					}
				}
			}
		}

		private static readonly IncognitoMode _instance = new IncognitoMode();

		private const string NAME_HIDDEN = "VV_NameHidden";

		private string ItemPrefabsString = "";

		private HashSet<string> ItemPrefabs = new HashSet<string>();

		public static IncognitoMode Instance => _instance;

		private IncognitoMode()
		{
		}

		public static string GetDisplayName()
		{
			return IncognitoModePlugin.GetHiddenDisplayName() ?? "???";
		}

		public void Update()
		{
			if (IncognitoModePlugin.GetHiddenByItems().Equals(Instance.ItemPrefabsString))
			{
				return;
			}
			ItemPrefabsString = IncognitoModePlugin.GetHiddenByItems();
			ItemPrefabs = new HashSet<string>();
			if (!Utility.IsNullOrWhiteSpace(ItemPrefabsString))
			{
				string[] array = ItemPrefabsString.Split(new char[1] { ',' });
				for (int i = 0; i < array.Length; i++)
				{
					ItemPrefabs.Add(array[i].Trim());
				}
			}
		}
	}
}