Decompiled source of BeautifulNicknames v0.0.1

BeautifulNicknamesProj.dll

Decompiled 8 hours ago
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Reflection;
using System.Runtime.CompilerServices;
using System.Runtime.InteropServices;
using System.Runtime.Versioning;
using BepInEx;
using BepInEx.Logging;
using HarmonyLib;
using UnityEngine;
using UnityEngine.SceneManagement;

[assembly: CompilationRelaxations(8)]
[assembly: RuntimeCompatibility(WrapNonExceptionThrows = true)]
[assembly: Debuggable(DebuggableAttribute.DebuggingModes.IgnoreSymbolStoreSequencePoints)]
[assembly: AssemblyTitle("BeautifulNicknamesProj")]
[assembly: AssemblyDescription("")]
[assembly: AssemblyConfiguration("")]
[assembly: AssemblyCompany("")]
[assembly: AssemblyProduct("BeautifulNicknamesProj")]
[assembly: AssemblyCopyright("Copyright ©  2026")]
[assembly: AssemblyTrademark("")]
[assembly: ComVisible(false)]
[assembly: Guid("0617570e-74e6-464a-8b43-9292d122ccbe")]
[assembly: AssemblyFileVersion("1.0.0.0")]
[assembly: TargetFramework(".NETFramework,Version=v4.8", FrameworkDisplayName = ".NET Framework 4.8")]
[assembly: AssemblyVersion("1.0.0.0")]
internal static class EnemyRenamePatcher
{
	private static readonly Dictionary<string, string> NameMap = new Dictionary<string, string>(StringComparer.Ordinal)
	{
		{ "Robe", "Wifey" },
		{ "Huntsman", "Supák" },
		{ "Bowtie", "I need to poop" },
		{ "Gnome", "woo" },
		{ "Gambit", "Zsömle" },
		{ "Mentalist", "Alien" },
		{ "Hidden", "Csongi" }
	};

	private static Harmony harmony;

	private static ManualLogSource logger;

	private static bool initialized;

	private static Type enemyParentType;

	private static MemberInfo enemyNameMember;

	private static FieldInfo enemyNameLocalizedField;

	private static readonly HashSet<string> SeenEnemyNames = new HashSet<string>(StringComparer.Ordinal);

	private static bool firstSweepLogged;

	public static void Initialize(Harmony harmonyInstance, ManualLogSource logSource)
	{
		if (initialized)
		{
			return;
		}
		harmony = harmonyInstance;
		logger = logSource;
		initialized = true;
		enemyParentType = AccessTools.TypeByName("EnemyParent");
		if (enemyParentType == null)
		{
			ManualLogSource obj = logger;
			if (obj != null)
			{
				obj.LogWarning((object)"[EnemyRenamePatcher] EnemyParent type not found. Rename patching is skipped.");
			}
			return;
		}
		enemyNameMember = (MemberInfo)(((object)AccessTools.Field(enemyParentType, "enemyName")) ?? ((object)AccessTools.Property(enemyParentType, "enemyName")));
		if (enemyNameMember == null)
		{
			ManualLogSource obj2 = logger;
			if (obj2 != null)
			{
				obj2.LogWarning((object)"[EnemyRenamePatcher] EnemyParent.enemyName member not found. Rename patching is skipped.");
			}
			return;
		}
		ManualLogSource obj3 = logger;
		if (obj3 != null)
		{
			obj3.LogInfo((object)$"[EnemyRenamePatcher] Resolved EnemyParent.enemyName via {enemyNameMember.MemberType}.");
		}
		enemyNameLocalizedField = AccessTools.Field(enemyParentType, "enemyNameLocalized");
		if (enemyNameLocalizedField == null)
		{
			ManualLogSource obj4 = logger;
			if (obj4 != null)
			{
				obj4.LogWarning((object)"[EnemyRenamePatcher] EnemyParent.enemyNameLocalized field not found. Localized names may override renames.");
			}
		}
		Patch("EnemyParent", "Awake", "EnemyParent_Awake_Postfix");
		Patch("EnemyParent", "Start", "EnemyParent_Start_Postfix");
		ManualLogSource obj5 = logger;
		if (obj5 != null)
		{
			obj5.LogInfo((object)("[EnemyRenamePatcher] Initialized with " + NameMap.Count + " rename mappings."));
		}
	}

	public static int RenameActiveEnemies()
	{
		if (enemyParentType == null || enemyNameMember == null)
		{
			return 0;
		}
		Object[] array;
		try
		{
			array = Resources.FindObjectsOfTypeAll(enemyParentType);
		}
		catch (Exception ex)
		{
			ManualLogSource obj = logger;
			if (obj != null)
			{
				obj.LogWarning((object)("[EnemyRenamePatcher] FindObjectsOfTypeAll failed: " + ex.Message));
			}
			return 0;
		}
		if (array == null || array.Length == 0)
		{
			return 0;
		}
		int num = 0;
		Object[] array2 = array;
		foreach (Object instance in array2)
		{
			try
			{
				if (TryRenameInstance(instance))
				{
					num++;
				}
			}
			catch (Exception ex2)
			{
				ManualLogSource obj2 = logger;
				if (obj2 != null)
				{
					obj2.LogWarning((object)("[EnemyRenamePatcher] Failed to rename instance: " + ex2.Message));
				}
			}
		}
		if (!firstSweepLogged && array.Length != 0)
		{
			firstSweepLogged = true;
			ManualLogSource obj3 = logger;
			if (obj3 != null)
			{
				obj3.LogInfo((object)string.Format("[EnemyRenamePatcher] First sweep saw {0} EnemyParent objects. Names: {1}", array.Length, string.Join(", ", SeenEnemyNames)));
			}
		}
		if (num > 0)
		{
			ManualLogSource obj4 = logger;
			if (obj4 != null)
			{
				obj4.LogInfo((object)$"[EnemyRenamePatcher] Renamed {num} enemies.");
			}
		}
		return num;
	}

	private static bool TryRenameInstance(object instance)
	{
		string enemyName = GetEnemyName(instance);
		if (string.IsNullOrEmpty(enemyName))
		{
			return false;
		}
		if (SeenEnemyNames.Add(enemyName))
		{
			ManualLogSource obj = logger;
			if (obj != null)
			{
				obj.LogInfo((object)("[EnemyRenamePatcher] Observed enemyName value: '" + enemyName + "'"));
			}
		}
		string text = ResolveNewName(enemyName);
		if (text == null)
		{
			return false;
		}
		if (string.Equals(enemyName, text, StringComparison.Ordinal))
		{
			ClearLocalizedName(instance);
			return false;
		}
		SetEnemyName(instance, text);
		ClearLocalizedName(instance);
		ManualLogSource obj2 = logger;
		if (obj2 != null)
		{
			obj2.LogInfo((object)("[EnemyRenamePatcher] Renamed '" + enemyName + "' -> '" + text + "'."));
		}
		return true;
	}

	private static void ClearLocalizedName(object instance)
	{
		if (enemyNameLocalizedField == null)
		{
			return;
		}
		try
		{
			if (enemyNameLocalizedField.GetValue(instance) != null)
			{
				enemyNameLocalizedField.SetValue(instance, null);
				ManualLogSource obj = logger;
				if (obj != null)
				{
					obj.LogInfo((object)"[EnemyRenamePatcher] Cleared enemyNameLocalized override.");
				}
			}
		}
		catch (Exception ex)
		{
			ManualLogSource obj2 = logger;
			if (obj2 != null)
			{
				obj2.LogWarning((object)("[EnemyRenamePatcher] Failed to clear enemyNameLocalized: " + ex.Message));
			}
		}
	}

	private static string ResolveNewName(string currentName)
	{
		if (NameMap.TryGetValue(currentName, out var value))
		{
			return value;
		}
		foreach (KeyValuePair<string, string> item in NameMap)
		{
			if (currentName.IndexOf(item.Key, StringComparison.OrdinalIgnoreCase) >= 0)
			{
				return item.Value;
			}
		}
		return null;
	}

	private static void Patch(string typeName, string methodName, string postfixName = null)
	{
		//IL_008e: Unknown result type (might be due to invalid IL or missing references)
		//IL_0094: Expected O, but got Unknown
		if (harmony == null)
		{
			return;
		}
		Type type = AccessTools.TypeByName(typeName);
		if (type == null)
		{
			ManualLogSource obj = logger;
			if (obj != null)
			{
				obj.LogWarning((object)("[EnemyRenamePatcher] Type not found: " + typeName));
			}
			return;
		}
		MethodBase methodBase = AccessTools.Method(type, methodName, (Type[])null, (Type[])null);
		if (methodBase == null)
		{
			ManualLogSource obj2 = logger;
			if (obj2 != null)
			{
				obj2.LogWarning((object)("[EnemyRenamePatcher] Method not found: " + typeName + "." + methodName));
			}
			return;
		}
		HarmonyMethod val = null;
		if (!string.IsNullOrEmpty(postfixName))
		{
			MethodInfo methodInfo = AccessTools.Method(typeof(EnemyRenamePatcher), postfixName, (Type[])null, (Type[])null);
			if (methodInfo != null)
			{
				val = new HarmonyMethod(methodInfo);
			}
		}
		harmony.Patch(methodBase, (HarmonyMethod)null, val, (HarmonyMethod)null, (HarmonyMethod)null, (HarmonyMethod)null);
		ManualLogSource obj3 = logger;
		if (obj3 != null)
		{
			obj3.LogInfo((object)("[EnemyRenamePatcher] Patched " + typeName + "." + methodName));
		}
	}

	private static void EnemyParent_Awake_Postfix(object __instance)
	{
		TryRename(__instance);
	}

	private static void EnemyParent_Start_Postfix(object __instance)
	{
		TryRename(__instance);
	}

	private static void TryRename(object instance)
	{
		if (instance == null)
		{
			return;
		}
		try
		{
			TryRenameInstance(instance);
		}
		catch (Exception ex)
		{
			ManualLogSource obj = logger;
			if (obj != null)
			{
				obj.LogWarning((object)("[EnemyRenamePatcher] Rename on spawn failed: " + ex.Message));
			}
		}
	}

	private static string GetEnemyName(object instance)
	{
		if (enemyNameMember is FieldInfo fieldInfo)
		{
			return fieldInfo.GetValue(instance) as string;
		}
		if (enemyNameMember is PropertyInfo propertyInfo)
		{
			return propertyInfo.GetValue(instance) as string;
		}
		return null;
	}

	private static void SetEnemyName(object instance, string value)
	{
		if (enemyNameMember is FieldInfo fieldInfo)
		{
			fieldInfo.SetValue(instance, value);
		}
		else if (enemyNameMember is PropertyInfo propertyInfo)
		{
			propertyInfo.SetValue(instance, value);
		}
	}
}
public class StarterModHandler : MonoBehaviour
{
	private float nextEnemyRenameCheckAt;

	private const float EnemyRenameIntervalSeconds = 2f;

	private void Update()
	{
		if (Time.time >= nextEnemyRenameCheckAt)
		{
			nextEnemyRenameCheckAt = Time.time + 2f;
			int num = EnemyRenamePatcher.RenameActiveEnemies();
			if (num > 0)
			{
				Debug.Log((object)$"[StarterModHandler] Auto-renamed {num} enemies.");
			}
		}
	}
}
namespace BasicPluginProj;

[BepInPlugin("com.yourname.beautifulnicknames", "BeautifulNicknames", "0.0.1")]
public class BasicPlugin : BaseUnityPlugin
{
	private void Awake()
	{
		//IL_0015: Unknown result type (might be due to invalid IL or missing references)
		//IL_001a: Unknown result type (might be due to invalid IL or missing references)
		//IL_0026: Expected O, but got Unknown
		((BaseUnityPlugin)this).Logger.LogInfo((object)"[BeautifulNicknames] Plugin Loaded and initialized.");
		Harmony val = new Harmony("com.yourname.beautifulnicknames");
		EnemyRenamePatcher.Initialize(val, ((BaseUnityPlugin)this).Logger);
		val.PatchAll();
		((BaseUnityPlugin)this).Logger.LogInfo((object)"[BeautifulNicknames] === PATCHER INITIALIZED ===");
		SceneManager.sceneLoaded += OnSceneLoaded;
	}

	private void OnSceneLoaded(Scene scene, LoadSceneMode mode)
	{
		//IL_0033: Unknown result type (might be due to invalid IL or missing references)
		//IL_0038: Unknown result type (might be due to invalid IL or missing references)
		//IL_0044: Expected O, but got Unknown
		((BaseUnityPlugin)this).Logger.LogInfo((object)("[BeautifulNicknames] Scene loaded: " + ((Scene)(ref scene)).name));
		if ((Object)(object)GameObject.Find("StarterModHandler") == (Object)null)
		{
			GameObject val = new GameObject("StarterModHandler");
			val.AddComponent<StarterModHandler>();
			Object.DontDestroyOnLoad((Object)val);
		}
	}
}