Decompiled source of VBFontFix v0.0.1

VBFontFix.dll

Decompiled 2 months ago
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Linq;
using System.Reflection;
using System.Runtime.CompilerServices;
using System.Runtime.Versioning;
using System.Security;
using System.Security.Permissions;
using BepInEx;
using BepInEx.Configuration;
using HarmonyLib;
using Jotunn.Utils;
using Microsoft.CodeAnalysis;
using TMPro;
using UnityEngine;

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

		public RefSafetyRulesAttribute(int P_0)
		{
			Version = P_0;
		}
	}
}
namespace VBFontFix
{
	public enum GameServerClientState
	{
		Unknown,
		Client,
		Server
	}
	public static class Helper
	{
		public static bool IsServer()
		{
			try
			{
				ZNet instance = ZNet.instance;
				if (!Object.op_Implicit((Object)(object)instance))
				{
					return false;
				}
				return instance.IsServer();
			}
			catch
			{
				return false;
			}
		}

		public static bool IsClient()
		{
			try
			{
				ZNet instance = ZNet.instance;
				if (!Object.op_Implicit((Object)(object)instance))
				{
					return false;
				}
				return !instance.IsServer();
			}
			catch
			{
				return false;
			}
		}

		public static GameServerClientState GetGameServerClientState()
		{
			try
			{
				ZNet instance = ZNet.instance;
				if (!Object.op_Implicit((Object)(object)instance))
				{
					return GameServerClientState.Unknown;
				}
				return (!instance.IsServer()) ? GameServerClientState.Client : GameServerClientState.Server;
			}
			catch
			{
				return GameServerClientState.Unknown;
			}
		}

		public static bool AreCheatsEnabled(Terminal terminal)
		{
			try
			{
				if (!Console.IsVisible())
				{
					return false;
				}
				if (!Object.op_Implicit((Object)(object)terminal))
				{
					return false;
				}
				return Traverse.Create((object)terminal).Field<bool>("m_cheat").Value;
			}
			catch (Exception ex)
			{
				Debug.LogError((object)("Ошибка проверки чит-команд: " + ex.Message));
				return false;
			}
		}
	}
	[HarmonyPatch]
	[HarmonyWrapSafe]
	public class VB_FontChange
	{
		private static TMP_FontAsset MainFont;

		private static TMP_FontAsset SecondaryFont;

		public static ConfigEntry<string> mainFontName;

		public static ConfigEntry<string> secondaryFontName;

		private static bool isInitialized;

		private static string[] knownCyrillicFonts = new string[6] { "Valheim-Norse", "Valheim-Norsebold", "Valheim-AveriaSansLibre", "Valheim-AveriaSerifLibre", "LiberationSans SDF", "LiberationSans SDF - Fallback" };

		[HarmonyPatch(typeof(FejdStartup), "Awake", new Type[] { })]
		[HarmonyPostfix]
		private static void InitializeFont()
		{
			if (!isInitialized)
			{
				List<TMP_FontAsset> allFontsInGame = GetAllFontsInGame();
				List<TMP_FontAsset> list = FilterCyrillicFonts(allFontsInGame);
				MainFont = ((IEnumerable<TMP_FontAsset>)list).FirstOrDefault((Func<TMP_FontAsset, bool>)((TMP_FontAsset x) => ((Object)x).name == mainFontName.Value));
				SecondaryFont = ((IEnumerable<TMP_FontAsset>)list).FirstOrDefault((Func<TMP_FontAsset, bool>)((TMP_FontAsset x) => ((Object)x).name == secondaryFontName.Value));
				if (!Object.op_Implicit((Object)(object)MainFont) && list.Count > 0)
				{
					MainFont = list[0];
					mainFontName.Value = ((Object)MainFont).name;
					Debug.Log((object)("Основной шрифт из конфига не найден, установлен первый поддерживающий кириллицу: " + ((Object)MainFont).name));
				}
				if (!Object.op_Implicit((Object)(object)MainFont))
				{
					Debug.LogError((object)"Не найдено ни одного шрифта, поддерживающего кириллицу!");
					return;
				}
				Debug.Log((object)("Основной шрифт: " + mainFontName.Value + ", Второстепенный: " + secondaryFontName.Value));
				isInitialized = true;
				ApplyMainFontToAllExistingObjects();
			}
		}

		private static List<TMP_FontAsset> GetAllFontsInGame()
		{
			List<TMP_FontAsset> list = new List<TMP_FontAsset>();
			list.AddRange(Resources.FindObjectsOfTypeAll<TMP_FontAsset>());
			return (from f in list
				where Object.op_Implicit((Object)(object)f)
				group f by ((Object)f).name into g
				select g.First() into f
				orderby ((Object)f).name
				select f).ToList();
		}

		private static List<TMP_FontAsset> FilterCyrillicFonts(List<TMP_FontAsset> allFonts)
		{
			List<TMP_FontAsset> list = new List<TMP_FontAsset>();
			foreach (TMP_FontAsset allFont in allFonts)
			{
				if (knownCyrillicFonts.Contains(((Object)allFont).name))
				{
					list.Add(allFont);
				}
			}
			return (from f in list
				group f by ((Object)f).name into g
				select g.First() into f
				orderby ((Object)f).name
				select f).ToList();
		}

		[HarmonyPostfix]
		[HarmonyPatch(typeof(Menu), "Start", new Type[] { })]
		private static void Menu_Patch(Menu __instance)
		{
			if (Object.op_Implicit((Object)(object)MainFont))
			{
				ApplyMainFontToAllTextsInObject(((Component)__instance).gameObject, 30, 10);
			}
		}

		[HarmonyPatch(typeof(Settings), "Awake", new Type[] { })]
		[HarmonyPostfix]
		private static void Settings_Patch(Settings __instance)
		{
			if (Object.op_Implicit((Object)(object)MainFont))
			{
				ApplyMainFontToAllTextsInObject(((Component)__instance).gameObject, 25, 10);
			}
		}

		[HarmonyPatch(typeof(Hud), "Awake", new Type[] { })]
		[HarmonyPostfix]
		private static void Hud_Patch(Hud __instance)
		{
			if (Object.op_Implicit((Object)(object)MainFont))
			{
				ApplyMainFontToAllTextsInObject(((Component)__instance).gameObject, 20, 10);
			}
		}

		[HarmonyPatch(typeof(Minimap), "Awake", new Type[] { })]
		[HarmonyPostfix]
		private static void Minimap_Patch(Minimap __instance)
		{
			if (Object.op_Implicit((Object)(object)MainFont))
			{
				ApplyMainFontToAllTextsInObject(((Component)__instance).gameObject, 20, 10);
			}
		}

		[HarmonyPatch(typeof(ConnectPanel), "Start", new Type[] { })]
		[HarmonyPostfix]
		private static void ConnectPanel_Patch(ConnectPanel __instance)
		{
			if (Object.op_Implicit((Object)(object)MainFont))
			{
				ApplyMainFontToAllTextsInObject(((Component)__instance).gameObject, 17, 10);
			}
		}

		[HarmonyPostfix]
		[HarmonyPatch(typeof(ConnectPanel), "Update", new Type[] { })]
		private static void ConnectPanel_Update_Patch(ConnectPanel __instance)
		{
			if (Object.op_Implicit((Object)(object)MainFont))
			{
				ApplyMainFontToAllTextsInObject(((Component)__instance).gameObject, 17, 10);
			}
		}

		[HarmonyPatch(typeof(Tutorial), "Awake", new Type[] { })]
		[HarmonyPostfix]
		private static void Tutorial_Patch(Tutorial __instance)
		{
			if (Object.op_Implicit((Object)(object)MainFont))
			{
				ApplyMainFontToAllTextsInObject(((Component)__instance).gameObject, 20, 10);
			}
		}

		[HarmonyPatch(typeof(TextViewer), "Awake", new Type[] { })]
		[HarmonyPostfix]
		private static void TextViewer_Patch(TextViewer __instance)
		{
			if (Object.op_Implicit((Object)(object)MainFont))
			{
				ApplyMainFontToAllTextsInObject(((Component)__instance.m_text).gameObject, 25, 25);
			}
		}

		[HarmonyPostfix]
		[HarmonyPatch(typeof(MessageHud), "Awake", new Type[] { })]
		private static void MessageHud_Patch(MessageHud __instance)
		{
			if (Object.op_Implicit((Object)(object)MainFont))
			{
				ApplyMainFontToAllTextsInObject(((Component)__instance).gameObject, 55, 35);
				ApplyMainFontToAllTextsInObject(__instance.m_unlockMsgPrefab.gameObject, 25, 15);
				ApplyMainFontToAllTextsInObject(((Component)__instance.m_messageText).gameObject, 25, 15);
			}
		}

		[HarmonyPatch(typeof(MessageHud), "Update", new Type[] { })]
		[HarmonyPostfix]
		private static void MessageHud_Update_Patch(MessageHud __instance)
		{
			if (Object.op_Implicit((Object)(object)MainFont))
			{
				ApplyMainFontToAllTextsInObject(((Component)__instance).gameObject, 55, 35);
				ApplyMainFontToAllTextsInObject(__instance.m_unlockMsgPrefab.gameObject, 25, 15);
				ApplyMainFontToAllTextsInObject(((Component)__instance.m_messageText).gameObject, 25, 15);
			}
		}

		[HarmonyPatch(typeof(InventoryGui), "Awake", new Type[] { })]
		[HarmonyPostfix]
		private static void Inventory_Patch(InventoryGui __instance)
		{
			if (Object.op_Implicit((Object)(object)SecondaryFont))
			{
				ApplySecondaryFontToAllTextsInObject(((Component)__instance).gameObject);
				ApplyMainFontToAllTextsInObject(((Component)__instance.m_craftButton).gameObject, 20, 10);
				ApplyMainFontToAllTextsInObject(((Component)__instance.m_skillsDialog).gameObject, 20, 10);
				ApplyMainFontToAllTextsInObject(((Component)__instance.m_tabUpgrade).gameObject, 20, 10);
				ApplyMainFontToAllTextsInObject(((Component)__instance.m_tabCraft).gameObject, 20, 10);
				ApplyMainFontToAllTextsInObject(((Component)__instance.m_craftingStationName).gameObject, 40, 20);
				ApplyMainFontToAllTextsInObject(((Component)__instance.m_qualityPanel).gameObject, 25, 10);
				ApplyMainFontToAllTextsInObject(((Component)__instance.m_infoPanel).gameObject, 20, 10);
				ApplyMainFontToAllTextsInObject(((Component)__instance.m_textsDialog).gameObject, 25, 10);
				ApplyMainFontToAllTextsInObject(__instance.m_trophiesPanel.gameObject, 25, 10);
			}
		}

		[HarmonyPostfix]
		[HarmonyPatch(typeof(Terminal), "Awake", new Type[] { })]
		private static void Terminal_Patch(Terminal __instance)
		{
			if (Object.op_Implicit((Object)(object)SecondaryFont))
			{
				ApplySecondaryFontToAllTextsInObject(((Component)__instance).gameObject);
			}
		}

		public static void ApplyMainFontToAllExistingObjects()
		{
			if (!Object.op_Implicit((Object)(object)MainFont))
			{
				return;
			}
			TextMeshProUGUI[] array = Resources.FindObjectsOfTypeAll<TextMeshProUGUI>();
			TextMeshPro[] array2 = Resources.FindObjectsOfTypeAll<TextMeshPro>();
			int num = 0;
			TextMeshProUGUI[] array3 = array;
			foreach (TextMeshProUGUI val in array3)
			{
				if (Object.op_Implicit((Object)(object)val) && (Object)(object)((TMP_Text)val).font != (Object)(object)MainFont)
				{
					((TMP_Text)val).font = MainFont;
					num++;
				}
			}
			TextMeshPro[] array4 = array2;
			foreach (TextMeshPro val2 in array4)
			{
				if (Object.op_Implicit((Object)(object)val2) && (Object)(object)((TMP_Text)val2).font != (Object)(object)MainFont)
				{
					((TMP_Text)val2).font = MainFont;
					num++;
				}
			}
			Debug.Log((object)$"Применен основной шрифт к {num} текстовым элементам");
		}

		private static void ApplyMainFontToAllTextsInObject(GameObject obj, int fontSize, int fontSizeMin)
		{
			if (!Object.op_Implicit((Object)(object)MainFont) || !Object.op_Implicit((Object)(object)obj))
			{
				return;
			}
			TextMeshProUGUI[] componentsInChildren = obj.GetComponentsInChildren<TextMeshProUGUI>(true);
			TextMeshPro[] componentsInChildren2 = obj.GetComponentsInChildren<TextMeshPro>(true);
			TextMeshProUGUI[] array = componentsInChildren;
			foreach (TextMeshProUGUI val in array)
			{
				if (Object.op_Implicit((Object)(object)val) && (Object)(object)((TMP_Text)val).font != (Object)(object)MainFont)
				{
					((TMP_Text)val).font = MainFont;
					((TMP_Text)val).fontSize = fontSize;
					((TMP_Text)val).fontSizeMin = fontSizeMin;
				}
			}
			TextMeshPro[] array2 = componentsInChildren2;
			foreach (TextMeshPro val2 in array2)
			{
				if (Object.op_Implicit((Object)(object)val2) && (Object)(object)((TMP_Text)val2).font != (Object)(object)MainFont)
				{
					((TMP_Text)val2).font = MainFont;
					((TMP_Text)val2).fontSize = fontSize;
					((TMP_Text)val2).fontSizeMin = fontSizeMin;
				}
			}
		}

		private static void ApplySecondaryFontToAllTextsInObject(GameObject obj)
		{
			if (!Object.op_Implicit((Object)(object)SecondaryFont) || !Object.op_Implicit((Object)(object)obj))
			{
				return;
			}
			TextMeshProUGUI[] componentsInChildren = obj.GetComponentsInChildren<TextMeshProUGUI>(true);
			TextMeshPro[] componentsInChildren2 = obj.GetComponentsInChildren<TextMeshPro>(true);
			TextMeshProUGUI[] array = componentsInChildren;
			foreach (TextMeshProUGUI val in array)
			{
				if (Object.op_Implicit((Object)(object)val) && (Object)(object)((TMP_Text)val).font != (Object)(object)SecondaryFont)
				{
					((TMP_Text)val).font = SecondaryFont;
				}
			}
			TextMeshPro[] array2 = componentsInChildren2;
			foreach (TextMeshPro val2 in array2)
			{
				if (Object.op_Implicit((Object)(object)val2) && (Object)(object)((TMP_Text)val2).font != (Object)(object)SecondaryFont)
				{
					((TMP_Text)val2).font = SecondaryFont;
				}
			}
		}

		public static void ReloadFonts()
		{
			List<TMP_FontAsset> allFontsInGame = GetAllFontsInGame();
			List<TMP_FontAsset> list = FilterCyrillicFonts(allFontsInGame);
			MainFont = ((IEnumerable<TMP_FontAsset>)list).FirstOrDefault((Func<TMP_FontAsset, bool>)((TMP_FontAsset x) => ((Object)x).name == mainFontName.Value));
			SecondaryFont = ((IEnumerable<TMP_FontAsset>)list).FirstOrDefault((Func<TMP_FontAsset, bool>)((TMP_FontAsset x) => ((Object)x).name == secondaryFontName.Value));
			if (!Object.op_Implicit((Object)(object)MainFont) && list.Count > 0)
			{
				MainFont = list[0];
				mainFontName.Value = ((Object)MainFont).name;
				Debug.Log((object)("Основной шрифт из конфига не найден, установлен первый поддерживающий кириллицу: " + ((Object)MainFont).name));
			}
			if (!Object.op_Implicit((Object)(object)MainFont))
			{
				Debug.LogError((object)"Не найдено ни одного шрифта, поддерживающего кириллицу!");
			}
			else
			{
				Debug.Log((object)("Основной шрифт: " + mainFontName.Value + ", Второстепенный: " + secondaryFontName.Value));
			}
		}

		public static void RefreshAllUIElements()
		{
			ReloadFonts();
			Menu val = Object.FindObjectOfType<Menu>();
			if (Object.op_Implicit((Object)(object)val))
			{
				ApplyMainFontToAllTextsInObject(((Component)val).gameObject, 30, 10);
			}
			Settings val2 = Object.FindObjectOfType<Settings>();
			if (Object.op_Implicit((Object)(object)val2))
			{
				ApplyMainFontToAllTextsInObject(((Component)val2).gameObject, 25, 10);
			}
			Hud val3 = Object.FindObjectOfType<Hud>();
			if (Object.op_Implicit((Object)(object)val3))
			{
				ApplyMainFontToAllTextsInObject(((Component)val3).gameObject, 20, 10);
			}
			InventoryGui val4 = Object.FindObjectOfType<InventoryGui>();
			if (Object.op_Implicit((Object)(object)val4))
			{
				ApplySecondaryFontToAllTextsInObject(((Component)val4).gameObject);
			}
			if (Object.op_Implicit((Object)(object)val4))
			{
				ApplyMainFontToAllTextsInObject(((Component)val4.m_craftButton).gameObject, 20, 10);
			}
			if (Object.op_Implicit((Object)(object)val4))
			{
				ApplyMainFontToAllTextsInObject(((Component)val4.m_skillsDialog).gameObject, 20, 10);
			}
			if (Object.op_Implicit((Object)(object)val4))
			{
				ApplyMainFontToAllTextsInObject(((Component)val4.m_tabUpgrade).gameObject, 20, 10);
			}
			if (Object.op_Implicit((Object)(object)val4))
			{
				ApplyMainFontToAllTextsInObject(((Component)val4.m_tabCraft).gameObject, 20, 10);
			}
			if (Object.op_Implicit((Object)(object)val4))
			{
				ApplyMainFontToAllTextsInObject(((Component)val4.m_craftingStationName).gameObject, 40, 20);
			}
			if (Object.op_Implicit((Object)(object)val4))
			{
				ApplyMainFontToAllTextsInObject(((Component)val4.m_qualityPanel).gameObject, 25, 10);
			}
			if (Object.op_Implicit((Object)(object)val4))
			{
				ApplyMainFontToAllTextsInObject(((Component)val4.m_infoPanel).gameObject, 20, 10);
			}
			if (Object.op_Implicit((Object)(object)val4))
			{
				ApplyMainFontToAllTextsInObject(((Component)val4.m_textsDialog).gameObject, 25, 10);
			}
			if (Object.op_Implicit((Object)(object)val4))
			{
				ApplyMainFontToAllTextsInObject(val4.m_trophiesPanel.gameObject, 25, 10);
			}
			Minimap val5 = Object.FindObjectOfType<Minimap>();
			if (Object.op_Implicit((Object)(object)val5))
			{
				ApplyMainFontToAllTextsInObject(((Component)val5).gameObject, 20, 10);
			}
			Terminal val6 = Object.FindObjectOfType<Terminal>();
			if (Object.op_Implicit((Object)(object)val6))
			{
				ApplySecondaryFontToAllTextsInObject(((Component)val6).gameObject);
			}
			ConnectPanel val7 = Object.FindObjectOfType<ConnectPanel>();
			if (Object.op_Implicit((Object)(object)val7))
			{
				ApplyMainFontToAllTextsInObject(((Component)val7).gameObject, 17, 10);
			}
			Tutorial val8 = Object.FindObjectOfType<Tutorial>();
			if (Object.op_Implicit((Object)(object)val8))
			{
				ApplyMainFontToAllTextsInObject(((Component)val8).gameObject, 20, 10);
			}
			TextViewer val9 = Object.FindObjectOfType<TextViewer>();
			if (Object.op_Implicit((Object)(object)val9))
			{
				ApplyMainFontToAllTextsInObject(((Component)val9.m_text).gameObject, 25, 25);
			}
			MessageHud val10 = Object.FindObjectOfType<MessageHud>();
			if (Object.op_Implicit((Object)(object)val10))
			{
				ApplyMainFontToAllTextsInObject(((Component)val10).gameObject, 55, 35);
			}
			if (Object.op_Implicit((Object)(object)val10))
			{
				ApplyMainFontToAllTextsInObject(val10.m_unlockMsgPrefab.gameObject, 25, 15);
			}
			if (Object.op_Implicit((Object)(object)val10))
			{
				ApplyMainFontToAllTextsInObject(((Component)val10.m_messageText).gameObject, 25, 15);
			}
		}
	}
	[BepInPlugin("VitByr.VBFontFix", "VBFontFix", "0.0.1")]
	internal class VBFontFix : BaseUnityPlugin
	{
		private const string ModName = "VBFontFix";

		private const string ModVersion = "0.0.1";

		private const string ModGUID = "VitByr.VBFontFix";

		internal static VBFontFix self;

		private void Awake()
		{
			self = this;
			VB_FontChange.mainFontName = ((BaseUnityPlugin)this).Config.Bind<string>("01 - FontFix", "mainFontName", "Valheim-Norse", "Основной шрифт. Допустимые шрифты: Valheim-Norse, Valheim-Norsebold, Valheim-AveriaSansLibre, Valheim-AveriaSerifLibre, LiberationSans SDF, LiberationSans SDF - Fallback. Требуется перезапуск.");
			VB_FontChange.secondaryFontName = ((BaseUnityPlugin)this).Config.Bind<string>("01 - FontFix", "secondaryFontName", "Valheim-AveriaSerifLibre", "Остальной шрифт. Допустимые шрифты: Valheim-Norse, Valheim-Norsebold, Valheim-AveriaSansLibre, Valheim-AveriaSerifLibre, LiberationSans SDF, LiberationSans SDF - Fallback. Требуется перезапуск.");
			CreateConfigWatcher();
			Harmony.CreateAndPatchAll(Assembly.GetExecutingAssembly(), "VitByr.VBFontFix");
		}

		private void OnDestroy()
		{
			((BaseUnityPlugin)this).Config.Save();
			((BaseUnityPlugin)this).Logger.LogInfo((object)"DESTROY");
		}

		private void CreateConfigWatcher()
		{
			//IL_000d: Unknown result type (might be due to invalid IL or missing references)
			//IL_0013: Expected O, but got Unknown
			ConfigFileWatcher val = new ConfigFileWatcher(((BaseUnityPlugin)this).Config, 1000L);
			val.OnConfigFileReloaded += delegate
			{
				VB_FontChange.RefreshAllUIElements();
			};
		}
	}
}