Decompiled source of ThaiLanguage v1.0.1

RoR2ThaiMod.dll

Decompiled 17 hours ago
using System;
using System.Collections.Generic;
using System.IO;
using System.Reflection;
using System.Runtime.CompilerServices;
using BepInEx;
using RoR2;
using TMPro;
using UnityEngine;
using UnityEngine.SceneManagement;
using UnityEngine.TextCore.LowLevel;

[assembly: CompilationRelaxations(8)]
[assembly: RuntimeCompatibility(WrapNonExceptionThrows = true)]
[assembly: AssemblyVersion("0.0.0.0")]
namespace RoR2ThaiMod;

[BepInPlugin("com.ror2thai.thailanguage", "Thai Language", "1.0.0")]
public class ThaiPlugin : BaseUnityPlugin
{
	public const string GUID = "com.ror2thai.thailanguage";

	public const string NAME = "Thai Language";

	public const string VERSION = "1.0.0";

	private const string BundleFile = "ror2thaifont.bundle";

	private const string FontAssetPath = "assets/ror2thai/notosansthai-thaimod.ttf";

	private string languageRoot;

	private TMP_FontAsset thaiFont;

	private void Awake()
	{
		string directoryName = Path.GetDirectoryName(((BaseUnityPlugin)this).Info.Location);
		languageRoot = Path.Combine(directoryName, "Language");
		if (!Directory.Exists(Path.Combine(languageRoot, "th")))
		{
			((BaseUnityPlugin)this).Logger.LogError((object)("Language/th not found next to the plugin: " + languageRoot));
		}
		Language.collectLanguageRootFolders += AddLanguageRoot;
		LoadFont(Path.Combine(directoryName, "ror2thaifont.bundle"));
		SceneManager.activeSceneChanged += OnSceneChanged;
	}

	private void AddLanguageRoot(List<string> roots)
	{
		if (!roots.Contains(languageRoot))
		{
			roots.Add(languageRoot);
		}
	}

	private void LoadFont(string bundlePath)
	{
		try
		{
			AssetBundle val = AssetBundle.LoadFromFile(bundlePath);
			if ((Object)(object)val == (Object)null)
			{
				((BaseUnityPlugin)this).Logger.LogError((object)("Could not load " + bundlePath));
				return;
			}
			Font val2 = val.LoadAsset<Font>("assets/ror2thai/notosansthai-thaimod.ttf");
			if ((Object)(object)val2 == (Object)null)
			{
				((BaseUnityPlugin)this).Logger.LogError((object)("Font missing in " + bundlePath));
				return;
			}
			thaiFont = TMP_FontAsset.CreateFontAsset(val2, 90, 9, (GlyphRenderMode)4165, 1024, 1024, (AtlasPopulationMode)1, true);
			if ((Object)(object)thaiFont == (Object)null)
			{
				((BaseUnityPlugin)this).Logger.LogError((object)"CreateFontAsset failed");
				return;
			}
			((Object)thaiFont).name = "NotoSansThai-ThaiMod SDF";
			((Object)thaiFont).hideFlags = (HideFlags)32;
			EnsureFallback();
			((BaseUnityPlugin)this).Logger.LogInfo((object)"Thai font ready");
		}
		catch (Exception ex)
		{
			((BaseUnityPlugin)this).Logger.LogError((object)("Thai font setup failed: " + ex));
		}
	}

	private void OnSceneChanged(Scene from, Scene to)
	{
		EnsureFallback();
	}

	private void EnsureFallback()
	{
		if (!((Object)(object)thaiFont == (Object)null))
		{
			List<TMP_FontAsset> fallbackFontAssets = TMP_Settings.fallbackFontAssets;
			if (fallbackFontAssets != null && !fallbackFontAssets.Contains(thaiFont))
			{
				fallbackFontAssets.Add(thaiFont);
				((BaseUnityPlugin)this).Logger.LogInfo((object)("Added Thai font to TMP fallback list (" + fallbackFontAssets.Count + " entries)"));
			}
			TMP_FontAsset defaultFontAsset = TMP_Settings.defaultFontAsset;
			Material material = ((TMP_Asset)thaiFont).material;
			if ((Object)(object)material != (Object)null && ((Object)(object)material.shader == (Object)null || !material.shader.isSupported) && (Object)(object)defaultFontAsset != (Object)null && (Object)(object)((TMP_Asset)defaultFontAsset).material != (Object)null)
			{
				material.shader = ((TMP_Asset)defaultFontAsset).material.shader;
			}
		}
	}
}