Decompiled source of Pseudonym v1.3.0

Pseudonym.dll

Decompiled 2 weeks ago
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Globalization;
using System.Reflection;
using System.Reflection.Emit;
using System.Runtime.CompilerServices;
using System.Runtime.InteropServices;
using System.Runtime.Versioning;
using System.Security;
using System.Security.Permissions;
using System.Text.RegularExpressions;
using BepInEx;
using BepInEx.Configuration;
using BepInEx.Logging;
using ComfyLib;
using GUIFramework;
using HarmonyLib;
using TMPro;
using UnityEngine;
using UnityEngine.Events;
using UnityEngine.UI;

[assembly: CompilationRelaxations(8)]
[assembly: RuntimeCompatibility(WrapNonExceptionThrows = true)]
[assembly: Debuggable(DebuggableAttribute.DebuggingModes.IgnoreSymbolStoreSequencePoints)]
[assembly: AssemblyTitle("Pseudonym")]
[assembly: AssemblyDescription("")]
[assembly: AssemblyConfiguration("")]
[assembly: AssemblyCompany("")]
[assembly: AssemblyProduct("Pseudonym")]
[assembly: AssemblyCopyright("Copyright © 2022")]
[assembly: AssemblyTrademark("")]
[assembly: ComVisible(false)]
[assembly: Guid("9f7d622d-a609-436a-ab50-ce9ba5f53cad")]
[assembly: AssemblyFileVersion("1.3.0")]
[assembly: TargetFramework(".NETFramework,Version=v4.8", FrameworkDisplayName = ".NET Framework 4.8")]
[assembly: SecurityPermission(SecurityAction.RequestMinimum, SkipVerification = true)]
[assembly: AssemblyVersion("1.3.0.0")]
[module: UnverifiableCode]
namespace Pseudonym
{
	[HarmonyPatch(typeof(FejdStartup))]
	internal static class FejdStartupPatch
	{
		private static Button _editButton;

		private static PlayerProfile _editingPlayerProfile;

		private static TextMeshProUGUI _newCharacterPanelTopicText;

		private static ButtonClickedEvent _onNewCharacterDoneEvent;

		private static PlayerCustomizaton _playerCustomization;

		private static string _topicText;

		private static int _characterLimit;

		private static ContentType _contentType;

		private static readonly Regex _nameRegex = new Regex("[a-zA-Z0-9 _]", RegexOptions.CultureInvariant);

		[HarmonyPostfix]
		[HarmonyPatch("Start")]
		private static void StartPostfix(ref FejdStartup __instance)
		{
			//IL_005f: Unknown result type (might be due to invalid IL or missing references)
			//IL_0064: Unknown result type (might be due to invalid IL or missing references)
			CreateEditButton(__instance);
			_newCharacterPanelTopicText = ((Component)__instance.m_newCharacterPanel.transform.Find("Topic")).GetComponent<TextMeshProUGUI>();
			_topicText = ((TMP_Text)_newCharacterPanelTopicText).text;
			_onNewCharacterDoneEvent = __instance.m_csNewCharacterDone.onClick;
			_characterLimit = ((TMP_InputField)__instance.m_csNewCharacterName).characterLimit;
			_contentType = ((TMP_InputField)__instance.m_csNewCharacterName).contentType;
			_playerCustomization = __instance.m_newCharacterPanel.GetComponentInChildren<PlayerCustomizaton>(true);
		}

		private static void CreateEditButton(FejdStartup fejdStartup)
		{
			//IL_0061: Unknown result type (might be due to invalid IL or missing references)
			//IL_006b: Expected O, but got Unknown
			//IL_008a: Unknown result type (might be due to invalid IL or missing references)
			//IL_0099: Unknown result type (might be due to invalid IL or missing references)
			//IL_009e: Unknown result type (might be due to invalid IL or missing references)
			Object.Destroy((Object)(object)_editButton);
			_editButton = Object.Instantiate<Button>(fejdStartup.m_csNewButton, ((Component)fejdStartup.m_csNewButton).transform.parent);
			((UnityEventBase)_editButton.onClick).RemoveAllListeners();
			((UnityEvent)_editButton.onClick).AddListener((UnityAction)delegate
			{
				OnEditCharacter(fejdStartup);
			});
			((TMP_Text)((Component)_editButton).GetComponentInChildren<TextMeshProUGUI>()).text = "Edit";
			RectTransform component = ((Component)_editButton).GetComponent<RectTransform>();
			component.anchoredPosition += new Vector2(190f, 0f);
		}

		[HarmonyPostfix]
		[HarmonyPatch("UpdateCharacterList")]
		private static void UpdateCharacterList(FejdStartup __instance)
		{
			Button obj = _editButton.Ref<Button>();
			if (obj != null)
			{
				((Component)obj).gameObject.SetActive(__instance.m_profiles.Count > 0);
			}
		}

		private static void OnEditCharacter(FejdStartup fejdStartup)
		{
			//IL_0092: Unknown result type (might be due to invalid IL or missing references)
			//IL_009c: Expected O, but got Unknown
			//IL_00b3: Unknown result type (might be due to invalid IL or missing references)
			//IL_00bd: Expected O, but got Unknown
			//IL_00f8: Unknown result type (might be due to invalid IL or missing references)
			//IL_0102: Expected O, but got Unknown
			//IL_0102: Unknown result type (might be due to invalid IL or missing references)
			//IL_010c: Expected O, but got Unknown
			if (fejdStartup.TryGetPlayerProfile(out var profile))
			{
				Pseudonym.LogInfo("Editing existing player: " + profile.GetName());
				_editingPlayerProfile = profile;
				fejdStartup.m_newCharacterPanel.SetActive(true);
				fejdStartup.m_newCharacterError.SetActive(false);
				fejdStartup.m_selectCharacterPanel.SetActive(false);
				((TMP_Text)_newCharacterPanelTopicText).text = "Edit Character: " + profile.GetName();
				fejdStartup.m_csNewCharacterDone.onClick = new ButtonClickedEvent();
				((UnityEvent)fejdStartup.m_csNewCharacterDone.onClick).AddListener((UnityAction)delegate
				{
					OnEditCharacterDone(fejdStartup);
				});
				((TMP_InputField)fejdStartup.m_csNewCharacterName).characterLimit = 20;
				((TMP_InputField)fejdStartup.m_csNewCharacterName).contentType = (ContentType)0;
				GuiInputField csNewCharacterName = fejdStartup.m_csNewCharacterName;
				((TMP_InputField)csNewCharacterName).onValidateInput = (OnValidateInput)Delegate.Combine((Delegate?)(object)((TMP_InputField)csNewCharacterName).onValidateInput, (Delegate?)new OnValidateInput(OnEditCharacterNameValidateInput));
				((TMP_InputField)fejdStartup.m_csNewCharacterName).text = profile.GetName();
				fejdStartup.SetupCharacterPreview(profile);
				GameObject obj = fejdStartup.m_playerInstance.Ref<GameObject>();
				SetupPlayerCustomization((obj != null) ? obj.GetComponent<Player>() : null, _playerCustomization);
			}
			else
			{
				_editingPlayerProfile = null;
			}
		}

		private static char OnEditCharacterNameValidateInput(string text, int charIndex, char addedChar)
		{
			if (!_nameRegex.IsMatch(char.ToString(addedChar)) || (charIndex <= 0 && addedChar == ' '))
			{
				return '\0';
			}
			return addedChar;
		}

		private static void SetupPlayerCustomization(Player player, PlayerCustomizaton customization)
		{
			//IL_003f: Unknown result type (might be due to invalid IL or missing references)
			//IL_0044: 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_004f: Unknown result type (might be due to invalid IL or missing references)
			//IL_0055: Unknown result type (might be due to invalid IL or missing references)
			//IL_005a: Unknown result type (might be due to invalid IL or missing references)
			//IL_005f: Unknown result type (might be due to invalid IL or missing references)
			//IL_0083: Unknown result type (might be due to invalid IL or missing references)
			//IL_0089: Unknown result type (might be due to invalid IL or missing references)
			//IL_008e: Unknown result type (might be due to invalid IL or missing references)
			//IL_0093: Unknown result type (might be due to invalid IL or missing references)
			//IL_0095: Unknown result type (might be due to invalid IL or missing references)
			//IL_009a: Unknown result type (might be due to invalid IL or missing references)
			//IL_00a0: Unknown result type (might be due to invalid IL or missing references)
			//IL_00a5: Unknown result type (might be due to invalid IL or missing references)
			//IL_00aa: Unknown result type (might be due to invalid IL or missing references)
			//IL_00ab: Unknown result type (might be due to invalid IL or missing references)
			if (Object.op_Implicit((Object)(object)player) && Object.op_Implicit((Object)(object)customization))
			{
				customization.m_maleToggle.SetIsOnWithoutNotify(player.m_modelIndex == 0);
				customization.m_femaleToggle.SetIsOnWithoutNotify(player.m_modelIndex == 1);
				float valueWithoutNotify = InverseLerp(Color.op_Implicit(customization.m_skinColor0), Color.op_Implicit(customization.m_skinColor1), Color.op_Implicit(Utils.Vec3ToColor(player.m_skinColor)));
				customization.m_skinHue.SetValueWithoutNotify(valueWithoutNotify);
				float x = player.m_hairColor.x;
				Color val = Utils.Vec3ToColor(player.m_hairColor / x);
				float valueWithoutNotify2 = InverseLerp(Color.op_Implicit(customization.m_hairColor0), Color.op_Implicit(customization.m_hairColor1), Color.op_Implicit(val));
				customization.m_hairTone.SetValueWithoutNotify(valueWithoutNotify2);
				customization.m_hairLevel.SetValueWithoutNotify(Mathf.InverseLerp(customization.m_hairMinLevel, customization.m_hairMaxLevel, x));
			}
			else
			{
				Pseudonym.LogError("Could not setup player customization for editing.");
			}
		}

		private static float InverseLerp(Vector4 a, Vector4 b, Vector4 value)
		{
			//IL_0000: Unknown result type (might be due to invalid IL or missing references)
			//IL_0001: Unknown result type (might be due to invalid IL or missing references)
			//IL_0002: Unknown result type (might be due to invalid IL or missing references)
			//IL_0007: Unknown result type (might be due to invalid IL or missing references)
			//IL_0008: Unknown result type (might be due to invalid IL or missing references)
			//IL_0009: Unknown result type (might be due to invalid IL or missing references)
			//IL_000a: Unknown result type (might be due to invalid IL or missing references)
			//IL_000f: 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_0011: Unknown result type (might be due to invalid IL or missing references)
			//IL_0017: Unknown result type (might be due to invalid IL or missing references)
			//IL_0018: Unknown result type (might be due to invalid IL or missing references)
			Vector4 val = b - a;
			Vector4 val2 = value - a;
			return Vector4.Dot(val, val2) / Vector4.Dot(val, val);
		}

		[HarmonyPrefix]
		[HarmonyPatch("OnNewCharacterCancel")]
		private static void OnNewCharacterCancel(ref FejdStartup __instance)
		{
			_editingPlayerProfile = null;
			OnEditCharacterDone(__instance);
		}

		private static void OnEditCharacterDone(FejdStartup fejdStartup)
		{
			//IL_00a8: Unknown result type (might be due to invalid IL or missing references)
			//IL_00c5: Unknown result type (might be due to invalid IL or missing references)
			//IL_00cf: Expected O, but got Unknown
			//IL_00cf: Unknown result type (might be due to invalid IL or missing references)
			//IL_00d9: Expected O, but got Unknown
			if (_editingPlayerProfile != null)
			{
				string text = ((TMP_InputField)fejdStartup.m_csNewCharacterName).text;
				Pseudonym.LogInfo("Saving existing player: " + _editingPlayerProfile.GetName() + " -> " + text);
				_editingPlayerProfile.SetName(text);
				_editingPlayerProfile.SavePlayerData(fejdStartup.m_playerInstance.GetComponent<Player>());
				_editingPlayerProfile.SavePlayerToDisk();
			}
			_editingPlayerProfile = null;
			((TMP_Text)_newCharacterPanelTopicText).text = _topicText;
			fejdStartup.m_csNewCharacterDone.onClick = _onNewCharacterDoneEvent;
			((TMP_InputField)fejdStartup.m_csNewCharacterName).text = string.Empty;
			((TMP_InputField)fejdStartup.m_csNewCharacterName).characterLimit = _characterLimit;
			((TMP_InputField)fejdStartup.m_csNewCharacterName).contentType = _contentType;
			GuiInputField csNewCharacterName = fejdStartup.m_csNewCharacterName;
			((TMP_InputField)csNewCharacterName).onValidateInput = (OnValidateInput)Delegate.Remove((Delegate?)(object)((TMP_InputField)csNewCharacterName).onValidateInput, (Delegate?)new OnValidateInput(OnEditCharacterNameValidateInput));
			fejdStartup.m_selectCharacterPanel.SetActive(true);
			fejdStartup.m_newCharacterPanel.SetActive(false);
			fejdStartup.UpdateCharacterList();
		}
	}
	public static class PluginConfig
	{
		public static ConfigEntry<bool> IsModEnabled { get; private set; }

		public static void BindConfig(ConfigFile config)
		{
			IsModEnabled = config.BindInOrder("_Global", "isModEnabled", defaultValue: true, "Globally enable or disable this mod (restart required).");
		}
	}
	public static class FejdStartupExtensions
	{
		public static bool TryGetPlayerProfile(this FejdStartup fejdStartup, out PlayerProfile profile)
		{
			if (Object.op_Implicit((Object)(object)fejdStartup))
			{
				return fejdStartup.TryGetPlayerProfile(out profile, fejdStartup.m_profileIndex);
			}
			profile = null;
			return false;
		}

		public static bool TryGetPlayerProfile(this FejdStartup fejdStartup, out PlayerProfile profile, int profileIndex)
		{
			profile = null;
			if (!Object.op_Implicit((Object)(object)fejdStartup) || fejdStartup.m_profiles == null || fejdStartup.m_profiles.Count <= 0)
			{
				return false;
			}
			if (profileIndex < 0 || profileIndex >= fejdStartup.m_profiles.Count)
			{
				return false;
			}
			profile = fejdStartup.m_profiles[profileIndex];
			return true;
		}
	}
	[HarmonyPatch(typeof(PlayerCustomizaton))]
	internal static class PlayerCustomizatonPatch
	{
		[HarmonyTranspiler]
		[HarmonyPatch("OnBeardLeft")]
		private static IEnumerable<CodeInstruction> OnBeardLeftTranspiler(IEnumerable<CodeInstruction> instructions)
		{
			//IL_0002: 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_0037: Expected O, but got Unknown
			//IL_0045: Unknown result type (might be due to invalid IL or missing references)
			//IL_004b: Expected O, but got Unknown
			return new CodeMatcher(instructions, (ILGenerator)null).MatchForward(false, (CodeMatch[])(object)new CodeMatch[2]
			{
				new CodeMatch((OpCode?)OpCodes.Callvirt, (object)AccessTools.Method(typeof(Player), "GetPlayerModel", (Type[])null, (Type[])null), (string)null),
				new CodeMatch((OpCode?)OpCodes.Ldc_I4_1, (object)null, (string)null)
			}).ThrowIfInvalid("Could not patch PlayerCustomizaton.OnBeardLeft()!").Advance(1)
				.InsertAndAdvance((CodeInstruction[])(object)new CodeInstruction[1] { Transpilers.EmitDelegate<Func<int, int>>((Func<int, int>)GetPlayerModelDelegate) })
				.InstructionEnumeration();
		}

		[HarmonyTranspiler]
		[HarmonyPatch("OnBeardRight")]
		private static IEnumerable<CodeInstruction> OnBeardRightTranspiler(IEnumerable<CodeInstruction> instructions)
		{
			//IL_0002: 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_0037: Expected O, but got Unknown
			//IL_0045: Unknown result type (might be due to invalid IL or missing references)
			//IL_004b: Expected O, but got Unknown
			return new CodeMatcher(instructions, (ILGenerator)null).MatchForward(false, (CodeMatch[])(object)new CodeMatch[2]
			{
				new CodeMatch((OpCode?)OpCodes.Callvirt, (object)AccessTools.Method(typeof(Player), "GetPlayerModel", (Type[])null, (Type[])null), (string)null),
				new CodeMatch((OpCode?)OpCodes.Ldc_I4_1, (object)null, (string)null)
			}).ThrowIfInvalid("Could not patch PlayerCustomizaton.OnBeardRight()!").Advance(1)
				.InsertAndAdvance((CodeInstruction[])(object)new CodeInstruction[1] { Transpilers.EmitDelegate<Func<int, int>>((Func<int, int>)GetPlayerModelDelegate) })
				.InstructionEnumeration();
		}

		private static int GetPlayerModelDelegate(int value)
		{
			if (!PluginConfig.IsModEnabled.Value)
			{
				return value;
			}
			return 0;
		}
	}
	[BepInPlugin("redseiko.valheim.pseudonym", "Pseudonym", "1.3.0")]
	public sealed class Pseudonym : BaseUnityPlugin
	{
		public const string PluginGuid = "redseiko.valheim.pseudonym";

		public const string PluginName = "Pseudonym";

		public const string PluginVersion = "1.3.0";

		private static ManualLogSource _logger;

		private Harmony _harmony;

		private void Awake()
		{
			_logger = ((BaseUnityPlugin)this).Logger;
			PluginConfig.BindConfig(((BaseUnityPlugin)this).Config);
			if (PluginConfig.IsModEnabled.Value)
			{
				_harmony = Harmony.CreateAndPatchAll(Assembly.GetExecutingAssembly(), "redseiko.valheim.pseudonym");
			}
		}

		private void OnDestroy()
		{
			Harmony harmony = _harmony;
			if (harmony != null)
			{
				harmony.UnpatchSelf();
			}
		}

		public static void LogInfo(object o)
		{
			_logger.LogInfo((object)$"[{DateTime.Now.ToString(DateTimeFormatInfo.InvariantInfo)}] {o}");
		}

		public static void LogError(object o)
		{
			_logger.LogError((object)$"[{DateTime.Now.ToString(DateTimeFormatInfo.InvariantInfo)}] {o}");
		}
	}
}
namespace ComfyLib
{
	public static class ConfigFileExtensions
	{
		internal sealed class ConfigurationManagerAttributes
		{
			public Action<ConfigEntryBase> CustomDrawer;

			public bool? Browsable;

			public bool? HideDefaultButton;

			public int? Order;
		}

		private static readonly Dictionary<string, int> _sectionToSettingOrder = new Dictionary<string, int>();

		private static int GetSettingOrder(string section)
		{
			if (!_sectionToSettingOrder.TryGetValue(section, out var value))
			{
				value = 0;
			}
			_sectionToSettingOrder[section] = value - 1;
			return value;
		}

		public static ConfigEntry<T> BindInOrder<T>(this ConfigFile config, string section, string key, T defaultValue, string description, AcceptableValueBase acceptableValues)
		{
			//IL_0027: Unknown result type (might be due to invalid IL or missing references)
			//IL_0031: Expected O, but got Unknown
			return config.Bind<T>(section, key, defaultValue, new ConfigDescription(description, acceptableValues, new object[1]
			{
				new ConfigurationManagerAttributes
				{
					Order = GetSettingOrder(section)
				}
			}));
		}

		public static ConfigEntry<T> BindInOrder<T>(this ConfigFile config, string section, string key, T defaultValue, string description, Action<ConfigEntryBase> customDrawer = null, bool browsable = true, bool hideDefaultButton = false, bool hideSettingName = false)
		{
			//IL_0048: Unknown result type (might be due to invalid IL or missing references)
			//IL_0052: Expected O, but got Unknown
			return config.Bind<T>(section, key, defaultValue, new ConfigDescription(description, (AcceptableValueBase)null, new object[1]
			{
				new ConfigurationManagerAttributes
				{
					Browsable = browsable,
					CustomDrawer = customDrawer,
					HideDefaultButton = hideDefaultButton,
					Order = GetSettingOrder(section)
				}
			}));
		}

		public static void OnSettingChanged<T>(this ConfigEntry<T> configEntry, Action settingChangedHandler)
		{
			configEntry.SettingChanged += delegate
			{
				settingChangedHandler();
			};
		}

		public static void OnSettingChanged<T>(this ConfigEntry<T> configEntry, Action<T> settingChangedHandler)
		{
			configEntry.SettingChanged += delegate(object _, EventArgs eventArgs)
			{
				//IL_0007: Unknown result type (might be due to invalid IL or missing references)
				settingChangedHandler((T)((SettingChangedEventArgs)eventArgs).ChangedSetting.BoxedValue);
			};
		}

		public static void OnSettingChanged<T>(this ConfigEntry<T> configEntry, Action<ConfigEntry<T>> settingChangedHandler)
		{
			configEntry.SettingChanged += delegate(object _, EventArgs eventArgs)
			{
				//IL_0007: Unknown result type (might be due to invalid IL or missing references)
				settingChangedHandler((ConfigEntry<T>)((SettingChangedEventArgs)eventArgs).ChangedSetting.BoxedValue);
			};
		}
	}
	public static class ObjectExtensions
	{
		public static T Ref<T>(this T unityObject) where T : Object
		{
			if (!Object.op_Implicit((Object)(object)unityObject))
			{
				return default(T);
			}
			return unityObject;
		}
	}
}