Decompiled source of Russian Font Fix v1.0.1

plugins/RepoRussianFixes.dll

Decompiled a week 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 BepInEx;
using HarmonyLib;
using Microsoft.CodeAnalysis;
using TMPro;
using UnityEngine;
using UnityEngine.TextCore;
using UnityEngine.TextCore.LowLevel;

[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("RepoRussianFixes")]
[assembly: AssemblyConfiguration("Release")]
[assembly: AssemblyFileVersion("1.0.0.0")]
[assembly: AssemblyInformationalVersion("1.0.0+84ac573e8cf72121df859bb9967bbf492c2e9842")]
[assembly: AssemblyProduct("RepoRussianFixes")]
[assembly: AssemblyTitle("RepoRussianFixes")]
[assembly: AssemblyVersion("1.0.0.0")]
[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 RepoRussianFixes
{
	[BepInPlugin("local.repo.russianfixes", "REPO Cyrillic Fonts", "1.0.0")]
	public sealed class Plugin : BaseUnityPlugin
	{
		public const string Id = "local.repo.russianfixes";

		private static readonly Dictionary<TMP_FontAsset, TMP_FontAsset?> FontCache = new Dictionary<TMP_FontAsset, TMP_FontAsset>();

		private static Plugin instance = null;

		private string directory;

		private void Awake()
		{
			//IL_005c: Unknown result type (might be due to invalid IL or missing references)
			//IL_0063: Unknown result type (might be due to invalid IL or missing references)
			//IL_0071: Expected O, but got Unknown
			instance = this;
			directory = Path.GetDirectoryName(((BaseUnityPlugin)this).Info.Location);
			try
			{
				MethodInfo methodInfo = typeof(TMP_FontAssetUtilities).GetMethod("GetCharacterFromFontAsset") ?? throw new MissingMethodException("GetCharacterFromFontAsset");
				MethodInfo method = typeof(Plugin).GetMethod("FindCyrillicGlyph", BindingFlags.Static | BindingFlags.NonPublic);
				new Harmony("local.repo.russianfixes").Patch((MethodBase)methodInfo, new HarmonyMethod(method), (HarmonyMethod)null, (HarmonyMethod)null, (HarmonyMethod)null, (HarmonyMethod)null);
				((BaseUnityPlugin)this).Logger.LogInfo((object)"Cyrillic fonts enabled.");
			}
			catch (Exception arg)
			{
				((BaseUnityPlugin)this).Logger.LogError((object)$"Could not enable Cyrillic fonts: {arg}");
			}
		}

		internal static string? FontFileFor(string assetName)
		{
			if (assetName.StartsWith("Teko-", StringComparison.OrdinalIgnoreCase))
			{
				return "Teko-Cyrillic.ttf";
			}
			if (assetName.StartsWith("RobotoMono-Thin", StringComparison.OrdinalIgnoreCase))
			{
				return "RobotoMono-Thin.ttf";
			}
			if (assetName.StartsWith("VCR_OSD_MONO_", StringComparison.OrdinalIgnoreCase))
			{
				return "Cozette-Cyrillic.ttf";
			}
			return null;
		}

		private static TMP_FontAsset? GetFont(TMP_FontAsset source, string filename)
		{
			//IL_006c: Unknown result type (might be due to invalid IL or missing references)
			//IL_0072: Unknown result type (might be due to invalid IL or missing references)
			//IL_0077: Unknown result type (might be due to invalid IL or missing references)
			if (FontCache.TryGetValue(source, out TMP_FontAsset value))
			{
				return value;
			}
			string text = Path.Combine(instance.directory, "Fonts", filename);
			try
			{
				if (!File.Exists(text))
				{
					throw new FileNotFoundException("Font file missing", text);
				}
				value = TMP_FontAsset.CreateFontAsset(text, 0, 90, 9, (GlyphRenderMode)4165, 1024, 1024);
				if ((Object)(object)value == (Object)null)
				{
					throw new InvalidOperationException("TextMesh Pro could not open the font.");
				}
				((TMP_Asset)value).faceInfo = MatchLineMetrics(((TMP_Asset)source).faceInfo, ((TMP_Asset)value).faceInfo);
				((Object)value).name = "RepoRussianFixes_" + Path.GetFileNameWithoutExtension(filename);
				((BaseUnityPlugin)instance).Logger.LogInfo((object)("Loaded " + filename + "."));
			}
			catch (Exception ex)
			{
				((BaseUnityPlugin)instance).Logger.LogWarning((object)("Could not load " + filename + ": " + ex.Message));
				value = null;
			}
			FontCache[source] = value;
			return value;
		}

		private static FaceInfo MatchLineMetrics(FaceInfo source, FaceInfo fallback)
		{
			//IL_0050: Unknown result type (might be due to invalid IL or missing references)
			float num = ((FaceInfo)(ref source)).scale / ((FaceInfo)(ref source)).pointSize / (((FaceInfo)(ref fallback)).scale / ((FaceInfo)(ref fallback)).pointSize);
			((FaceInfo)(ref fallback)).ascentLine = ((FaceInfo)(ref source)).ascentLine * num;
			((FaceInfo)(ref fallback)).descentLine = ((FaceInfo)(ref source)).descentLine * num;
			((FaceInfo)(ref fallback)).lineHeight = ((FaceInfo)(ref source)).lineHeight * num;
			return fallback;
		}

		private static bool FindCyrillicGlyph(uint unicode, TMP_FontAsset sourceFontAsset, FontStyles fontStyle, FontWeight fontWeight, ref bool isAlternativeTypeface, ref TMP_Character __result)
		{
			//IL_0043: 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)
			if (unicode < 1024 || unicode > 1327 || (Object)(object)sourceFontAsset == (Object)null)
			{
				return true;
			}
			string text = FontFileFor(((Object)sourceFontAsset).name);
			if (text != null)
			{
				TMP_FontAsset font = GetFont(sourceFontAsset, text);
				if (font != null && !((Object)(object)font == (Object)null))
				{
					bool flag = default(bool);
					TMP_Character characterFromFontAsset = TMP_FontAssetUtilities.GetCharacterFromFontAsset(unicode, font, false, fontStyle, fontWeight, ref flag);
					if (characterFromFontAsset == null)
					{
						return true;
					}
					__result = characterFromFontAsset;
					isAlternativeTypeface = flag;
					return false;
				}
			}
			return true;
		}
	}
}