Decompiled source of BlackBox cn v1.0.7

BlackBoxChinesePatch.dll

Decompiled 12 hours ago
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.IO;
using System.Reflection;
using System.Runtime.CompilerServices;
using System.Runtime.Versioning;
using System.Security;
using System.Security.Permissions;
using BepInEx;
using BepInEx.Logging;
using HarmonyLib;
using Microsoft.CodeAnalysis;
using UnityEngine;

[assembly: CompilationRelaxations(8)]
[assembly: RuntimeCompatibility(WrapNonExceptionThrows = true)]
[assembly: Debuggable(DebuggableAttribute.DebuggingModes.IgnoreSymbolStoreSequencePoints)]
[assembly: TargetFramework(".NETStandard,Version=v2.1", FrameworkDisplayName = ".NET Standard 2.1")]
[assembly: AssemblyCompany("TooRed")]
[assembly: AssemblyConfiguration("Release")]
[assembly: AssemblyFileVersion("1.0.0.0")]
[assembly: AssemblyInformationalVersion("1.0.0+7937bbe02fb7860bbd9966b7d5c308e2aa40975a")]
[assembly: AssemblyProduct("BlackBoxChinesePatch")]
[assembly: AssemblyTitle("BlackBoxChinesePatch")]
[assembly: SecurityPermission(SecurityAction.RequestMinimum, SkipVerification = true)]
[assembly: AssemblyVersion("1.0.0.0")]
[module: UnverifiableCode]
[module: RefSafetyRules(11)]
namespace Microsoft.CodeAnalysis
{
	[CompilerGenerated]
	[Embedded]
	internal sealed class EmbeddedAttribute : Attribute
	{
	}
}
namespace System.Runtime.CompilerServices
{
	[CompilerGenerated]
	[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]
	[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;
		}
	}
	[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 BlackBoxChinesePatch
{
	[BepInPlugin("TooRed.BlackBoxChinesePatch", "BlackBoxChinesePatch", "1.0.6")]
	[BepInDependency(/*Could not decode attribute arguments.*/)]
	[BepInDependency(/*Could not decode attribute arguments.*/)]
	public class BlackBoxChinesePatch : BaseUnityPlugin
	{
		private readonly Dictionary<string, string> DefaultTextMap = new Dictionary<string, string>(StringComparer.OrdinalIgnoreCase)
		{
			["DAMAGE DONE TO ITEMS"] = "搞破坏排行榜(疯狂指责)",
			["DESTROYED"] = "被打碎",
			["TOTAL"] = "总计",
			["LEFT"] = "剩余",
			["UNKNOWN"] = "未知",
			["EXTRACTED"] = "已提取",
			["ITEM(S)"] = " 件",
			["LOST"] = "损失",
			["IN VALUE WITH"] = "金钱,其中",
			["DISCOVERED"] = "已发现贵重品",
			["OUT OF"] = "件,总计"
		};

		private static Dictionary<string, string>? TextMap;

		internal static BlackBoxChinesePatch Instance { get; private set; }

		internal static ManualLogSource Logger => Instance.Log;

		private ManualLogSource Log => ((BaseUnityPlugin)this).Logger;

		internal Harmony? Harmony { get; set; }

		public string ConfigPath => Paths.ConfigPath;

		private void Awake()
		{
			Instance = this;
			((Component)this).gameObject.transform.parent = null;
			((Object)((Component)this).gameObject).hideFlags = (HideFlags)61;
			if (InitializeHarmonyPatch())
			{
				InitializeTextMap();
			}
			Logger.LogInfo((object)$"{((BaseUnityPlugin)this).Info.Metadata.GUID} v{((BaseUnityPlugin)this).Info.Metadata.Version} has loaded!");
		}

		private void InitializeTextMap()
		{
			TextMap = new Dictionary<string, string>(DefaultTextMap, StringComparer.OrdinalIgnoreCase);
			string path = Path.Combine(ConfigPath, "Translation", "TooRed.txt");
			if (!File.Exists(path))
			{
				return;
			}
			try
			{
				int num = 0;
				string[] array = File.ReadAllLines(path);
				foreach (string text in array)
				{
					if (string.IsNullOrWhiteSpace(text) || text.TrimStart().StartsWith("#"))
					{
						continue;
					}
					int num2 = text.IndexOf('=');
					if (num2 > 0 && num2 < text.Length - 1)
					{
						string text2 = text.Substring(0, num2).Trim();
						string value = text.Substring(num2 + 1).Trim();
						if (!string.IsNullOrEmpty(text2) && !TextMap.ContainsKey(text2))
						{
							TextMap.Add(text2, value);
							num++;
						}
					}
				}
				Logger.LogInfo((object)$"[BlackBox中文补丁] 成功读取外部汉化文件,新增 {num} 条,字典总条数:{TextMap.Count}");
			}
			catch
			{
				Logger.LogInfo((object)"[BlackBox中文补丁] 读取外部汉化文件失败,仅使用默认字典");
			}
		}

		private bool InitializeHarmonyPatch()
		{
			//IL_0046: Unknown result type (might be due to invalid IL or missing references)
			//IL_0061: Unknown result type (might be due to invalid IL or missing references)
			//IL_0067: Expected O, but got Unknown
			try
			{
				MethodInfo methodInfo = AccessTools.Method(Type.GetType("Blackbox.MonitorTest, MonitorTest"), "setText", new Type[2]
				{
					typeof(string),
					typeof(bool)
				}, (Type[])null);
				Harmony val = new Harmony(((BaseUnityPlugin)this).Info.Metadata.GUID);
				HarmonyMethod val2 = new HarmonyMethod(typeof(BlackBoxChinesePatch).GetMethod("SetText_Prefix", BindingFlags.Static | BindingFlags.NonPublic));
				val.Patch((MethodBase)methodInfo, val2, (HarmonyMethod)null, (HarmonyMethod)null, (HarmonyMethod)null, (HarmonyMethod)null);
				Logger.LogInfo((object)"[BlackBox中文补丁] 汉化补丁加载成功!");
				return true;
			}
			catch (Exception ex)
			{
				Logger.LogError((object)("[BlackBox中文补丁] 初始化异常:" + ex.Message + "\n" + ex.StackTrace));
				return false;
			}
		}

		private static bool SetText_Prefix(ref string text)
		{
			if (string.IsNullOrEmpty(text) || TextMap == null)
			{
				return true;
			}
			foreach (KeyValuePair<string, string> item in TextMap)
			{
				text = text.Replace(item.Key, item.Value);
			}
			return true;
		}
	}
}