Decompiled source of PEAK MENU Plus v1.0.1

BepInEx/plugins/PeakMenuChinese/PeakMenuChinese.dll

Decompiled a week ago
using System;
using System.Collections;
using System.Collections.Generic;
using System.Diagnostics;
using System.Reflection;
using System.Runtime.CompilerServices;
using System.Runtime.Versioning;
using BepInEx;
using BepInEx.Logging;
using HarmonyLib;
using Microsoft.CodeAnalysis;
using UnityEngine;

[assembly: CompilationRelaxations(8)]
[assembly: RuntimeCompatibility(WrapNonExceptionThrows = true)]
[assembly: Debuggable(DebuggableAttribute.DebuggingModes.Default | DebuggableAttribute.DebuggingModes.DisableOptimizations | DebuggableAttribute.DebuggingModes.IgnoreSymbolStoreSequencePoints | DebuggableAttribute.DebuggingModes.EnableEditAndContinue)]
[assembly: TargetFramework(".NETFramework,Version=v4.7.2", FrameworkDisplayName = ".NET Framework 4.7.2")]
[assembly: AssemblyVersion("0.0.0.0")]
[module: RefSafetyRules(11)]
namespace Microsoft.CodeAnalysis
{
	[CompilerGenerated]
	[Embedded]
	internal sealed class EmbeddedAttribute : Attribute
	{
	}
}
namespace System.Runtime.CompilerServices
{
	[CompilerGenerated]
	[Embedded]
	[AttributeUsage(AttributeTargets.Module, AllowMultiple = false, Inherited = false)]
	internal sealed class RefSafetyRulesAttribute : Attribute
	{
		public readonly int Version;

		public RefSafetyRulesAttribute(int P_0)
		{
			Version = P_0;
		}
	}
}
namespace PeakMenuChinese
{
	internal static class ChineseDisplayNameIntegration
	{
		private static bool _patched;

		public static void Apply(ManualLogSource log)
		{
			try
			{
				if (!TryApply(log))
				{
					StartRetryer(log);
					log.LogInfo((object)"[PEAK_MENU_中文物品名] PEAK_MENU_CN 尚未就绪,已启动延时重试挂载");
				}
			}
			catch (Exception ex)
			{
				log.LogWarning((object)("[PEAK_MENU_中文物品名] 挂载流程异常(不影响其它功能): " + ex.Message));
			}
		}

		internal static bool TryApply(ManualLogSource log)
		{
			//IL_0077: Unknown result type (might be due to invalid IL or missing references)
			//IL_007e: Expected O, but got Unknown
			//IL_0092: Unknown result type (might be due to invalid IL or missing references)
			//IL_0099: Expected O, but got Unknown
			if (_patched)
			{
				return true;
			}
			try
			{
				Type type = AccessTools.TypeByName("P.E.A.K_MENU.Features.ItemSpawn.ItemCatalogService");
				if (type == null)
				{
					return false;
				}
				MethodInfo methodInfo = AccessTools.Method(type, "ResolveDisplayName", new Type[1] { typeof(Item) }, (Type[])null);
				if (methodInfo == null)
				{
					log.LogWarning((object)"[PEAK_MENU_中文物品名] 未找到 ResolveDisplayName(Item),跳过");
					return false;
				}
				Harmony val = new Harmony("peakmenu.chinese.ChineseDisplayName");
				HarmonyMethod val2 = new HarmonyMethod(typeof(ResolveDisplayNamePatch).GetMethod("Prefix"));
				val.Patch((MethodBase)methodInfo, val2, (HarmonyMethod)null, (HarmonyMethod)null, (HarmonyMethod)null, (HarmonyMethod)null);
				_patched = true;
				ForceRefreshCatalog(log);
				log.LogInfo((object)"[PEAK_MENU_中文物品名] 已挂载:PEAK_MENU_CN 物品列表将优先显示中文");
				return true;
			}
			catch (Exception ex)
			{
				log.LogWarning((object)("[PEAK_MENU_中文物品名] 单次挂载尝试失败(将重试): " + ex.Message));
				return false;
			}
		}

		private static void StartRetryer(ManualLogSource log)
		{
			//IL_0007: Unknown result type (might be due to invalid IL or missing references)
			//IL_000d: Expected O, but got Unknown
			try
			{
				GameObject val = new GameObject("ChinesePatchRetryer");
				Object.DontDestroyOnLoad((Object)(object)val);
				ChinesePatchRetryer chinesePatchRetryer = val.AddComponent<ChinesePatchRetryer>();
				chinesePatchRetryer.Init(log);
			}
			catch (Exception ex)
			{
				log.LogWarning((object)("[PEAK_MENU_中文物品名] 启动重试器失败(将放弃中文补丁): " + ex.Message));
			}
		}

		private static void ForceRefreshCatalog(ManualLogSource log)
		{
			try
			{
				Type type = AccessTools.TypeByName("P.E.A.K_MENU.Features.ItemSpawn.ItemSpawnRuntime");
				if (type == null)
				{
					return;
				}
				PropertyInfo propertyInfo = AccessTools.Property(type, "Catalog");
				if (propertyInfo == null || propertyInfo.GetGetMethod(nonPublic: true) == null)
				{
					return;
				}
				object value = propertyInfo.GetValue(null);
				if (value != null)
				{
					MethodInfo methodInfo = AccessTools.Method(value.GetType(), "ForceRefresh", (Type[])null, (Type[])null);
					if (!(methodInfo == null))
					{
						methodInfo.Invoke(value, null);
						log.LogInfo((object)"[PEAK_MENU_中文物品名] 已强制刷新物品目录");
					}
				}
			}
			catch (Exception ex)
			{
				log.LogWarning((object)("[PEAK_MENU_中文物品名] 强制刷新目录失败(可忽略): " + ex.Message));
			}
		}

		internal static void ForceRefreshOnce(ManualLogSource log)
		{
			ForceRefreshCatalog(log);
		}
	}
	internal class ChinesePatchRetryer : MonoBehaviour
	{
		private ManualLogSource _log;

		private int _attempts;

		private const int MaxAttempts = 60;

		public void Init(ManualLogSource log)
		{
			_log = log;
		}

		private IEnumerator Start()
		{
			while (_attempts < 60)
			{
				_attempts++;
				if (ChineseDisplayNameIntegration.TryApply(_log))
				{
					for (int i = 0; i < 10; i++)
					{
						ChineseDisplayNameIntegration.ForceRefreshOnce(_log);
						yield return (object)new WaitForSeconds(0.5f);
					}
					Object.Destroy((Object)(object)((Component)this).gameObject);
					yield break;
				}
				yield return (object)new WaitForSeconds(0.5f);
			}
			_log.LogWarning((object)"[PEAK_MENU_中文物品名] 多次重试仍未找到 PEAK_MENU_CN,放弃中文补丁(不影响其它功能)");
			Object.Destroy((Object)(object)((Component)this).gameObject);
		}
	}
	internal static class ResolveDisplayNamePatch
	{
		public static bool Prefix(Item item, ref string __result)
		{
			try
			{
				if ((Object)(object)item != (Object)null)
				{
					string name = ((Object)item).name;
					if (!string.IsNullOrEmpty(name) && ChineseNameMap.Map.TryGetValue(name, out var value))
					{
						__result = value;
						return false;
					}
					string name2 = item.GetName();
					if (!string.IsNullOrEmpty(name2) && !name2.StartsWith("LOC:", StringComparison.OrdinalIgnoreCase))
					{
						__result = name2;
						return false;
					}
				}
			}
			catch (Exception ex)
			{
				ManualLogSource log = Plugin.Log;
				if (log != null)
				{
					log.LogWarning((object)("[PEAK_MENU_中文物品名] 解析物品名异常: " + ex.Message));
				}
			}
			return true;
		}
	}
	internal static class ChineseNameMap
	{
		public static readonly Dictionary<string, string> Map = new Dictionary<string, string>(StringComparer.OrdinalIgnoreCase)
		{
			{ "AK", "AK 突击步枪" },
			{ "Apple Berry Weird", "怪味苹果莓" },
			{ "ClimbingChalk", "攀岩粉" },
			{ "Cure-Some", "缓和药剂" },
			{ "Darkberry", "暗莓" },
			{ "Energy Elixir", "能量精油" },
			{ "Lollipop_Evil", "邪糖果" },
			{ "Matchbook", "火柴盒" },
			{ "Painkillers", "止痛药" },
			{ "Skyberry", "天空莓" },
			{ "Wand of Wind", "风之杖" },
			{ "Warpsketball", "传送篮球" },
			{ "Wonderberry", "奇幻莓" },
			{ "WildDogMilk", "野生狗奶" }
		};
	}
	[BepInPlugin("peakmenu.chinese", "PEAK_MENU_CN Plus", "1.0.0")]
	[BepInDependency(/*Could not decode attribute arguments.*/)]
	public class Plugin : BaseUnityPlugin
	{
		internal static ManualLogSource Log;

		private void Awake()
		{
			Log = ((BaseUnityPlugin)this).Logger;
			try
			{
				ChineseDisplayNameIntegration.Apply(Log);
			}
			catch (Exception ex)
			{
				Log.LogWarning((object)("[PEAK_MENU_CN Plus] 初始化失败(不影响游戏): " + ex.Message));
			}
		}
	}
}