Please disclose if any significant portion of your mod was created using AI tools by adding the 'AI Generated' category. Failing to do so may result in the mod being removed from Thunderstore.
Decompiled source of LegacyBundleFontFix v1.0.0
plugins/LegacyBundleFontFix.dll
Decompiled 2 hours agousing 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 BepInEx.Configuration; using BepInEx.Logging; using HarmonyLib; using Microsoft.CodeAnalysis; using TMPro; using UnityEngine; using UnityEngine.SceneManagement; [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("LegacyBundleFontFix")] [assembly: AssemblyConfiguration("Release")] [assembly: AssemblyFileVersion("1.0.0.0")] [assembly: AssemblyInformationalVersion("1.0.0")] [assembly: AssemblyProduct("LegacyBundleFontFix")] [assembly: AssemblyTitle("LegacyBundleFontFix")] [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 LegacyBundleFontFix { [BepInPlugin("Factor.LegacyBundleFontFix", "LegacyBundleFontFix", "1.0.0")] [BepInDependency(/*Could not decode attribute arguments.*/)] public sealed class Plugin : BaseUnityPlugin { private sealed class LoadAssetState { public string BundleName = string.Empty; public HashSet<Object>? Snapshot; } public const string PluginGuid = "Factor.LegacyBundleFontFix"; public const string PluginName = "LegacyBundleFontFix"; public const string PluginVersion = "1.0.0"; private static ManualLogSource? _log; private static ConfigEntry<bool>? _enabled; private static ConfigEntry<bool>? _verboseLogging; private static MethodInfo? _getLoadedTextAssets; private static MethodInfo? _readBundleUnityVersion; private static MethodInfo? _isLegacyUnityVersion; private static MethodInfo? _replaceLegacyAssets; private static MethodInfo? _restoreMaterial; private static HashSet<Object>? _legacyAssetsSet; private static readonly Dictionary<AssetBundle, string> _legacyBundles = new Dictionary<AssetBundle, string>(); [ThreadStatic] private static int _loadFileDepth; [ThreadStatic] private static int _assetLoadDepth; private Harmony? _harmony; private void Awake() { //IL_0059: Unknown result type (might be due to invalid IL or missing references) //IL_0063: Expected O, but got Unknown //IL_0079: Unknown result type (might be due to invalid IL or missing references) //IL_007e: Unknown result type (might be due to invalid IL or missing references) //IL_008a: Expected O, but got Unknown //IL_00a0: Unknown result type (might be due to invalid IL or missing references) //IL_00a5: Unknown result type (might be due to invalid IL or missing references) //IL_00ad: Expected O, but got Unknown //IL_0211: Unknown result type (might be due to invalid IL or missing references) //IL_0218: Unknown result type (might be due to invalid IL or missing references) //IL_0225: Expected O, but got Unknown //IL_0225: Expected O, but got Unknown _log = ((BaseUnityPlugin)this).Logger; _enabled = ((BaseUnityPlugin)this).Config.Bind<bool>("General", "Enabled", true, "Fix TextMeshPro fonts and materials in custom levels/mods whose Unity 2022 AssetBundles bypass REPOLib's loader (e.g., FNAFLevel)."); _verboseLogging = ((BaseUnityPlugin)this).Config.Bind<bool>("General", "VerboseLogging", false, "Log each legacy bundle and TMP asset registered or replaced."); if (!ResolveRepoLibFontPatch()) { return; } _harmony = new Harmony("Factor.LegacyBundleFontFix"); HarmonyMethod val = new HarmonyMethod(AccessTools.Method(typeof(Plugin), "LoadFromFilePrefix", (Type[])null, (Type[])null)) { priority = 800 }; HarmonyMethod val2 = new HarmonyMethod(AccessTools.Method(typeof(Plugin), "LoadFromFilePostfix", (Type[])null, (Type[])null)) { priority = 0 }; Type[][] obj = new Type[3][] { new Type[1] { typeof(string) }, new Type[2] { typeof(string), typeof(uint) }, new Type[3] { typeof(string), typeof(uint), typeof(ulong) } }; int num = 0; Type[][] array = obj; foreach (Type[] array2 in array) { MethodInfo methodInfo = AccessTools.Method(typeof(AssetBundle), "LoadFromFile", array2, (Type[])null); if (methodInfo != null) { _harmony.Patch((MethodBase)methodInfo, val, val2, (HarmonyMethod)null, (HarmonyMethod)null, (HarmonyMethod)null); num++; } } MethodInfo methodInfo2 = AccessTools.Method(typeof(Plugin), "LoadAssetPrefix", (Type[])null, (Type[])null); MethodInfo methodInfo3 = AccessTools.Method(typeof(Plugin), "LoadAssetPostfix", (Type[])null, (Type[])null); int num2 = 0; string[] array3 = new string[2] { "LoadAsset_Internal", "LoadAssetWithSubAssets_Internal" }; foreach (string text in array3) { MethodInfo methodInfo4 = AccessTools.Method(typeof(AssetBundle), text, new Type[2] { typeof(string), typeof(Type) }, (Type[])null); if (methodInfo4 != null) { _harmony.Patch((MethodBase)methodInfo4, new HarmonyMethod(methodInfo2), new HarmonyMethod(methodInfo3), (HarmonyMethod)null, (HarmonyMethod)null, (HarmonyMethod)null); num2++; } } ((BaseUnityPlugin)this).Logger.LogInfo((object)$"Patched {num} LoadFromFile overload(s) and {num2} LoadAsset funnel(s)."); SceneManager.sceneLoaded += OnSceneLoaded; ((BaseUnityPlugin)this).Logger.LogInfo((object)"LegacyBundleFontFix v1.0.0 loaded."); } private void OnDestroy() { SceneManager.sceneLoaded -= OnSceneLoaded; Harmony? harmony = _harmony; if (harmony != null) { harmony.UnpatchSelf(); } } private static bool ResolveRepoLibFontPatch() { Type type = AccessTools.TypeByName("REPOLib.Patches.FontAssetMaterialPatch"); if (type == null) { ManualLogSource? log = _log; if (log != null) { log.LogError((object)"Could not find REPOLib.Patches.FontAssetMaterialPatch."); } return false; } _getLoadedTextAssets = AccessTools.Method(type, "GetLoadedTextAssets", Type.EmptyTypes, (Type[])null); _readBundleUnityVersion = AccessTools.Method(type, "ReadBundleUnityVersion", new Type[1] { typeof(string) }, (Type[])null); _isLegacyUnityVersion = AccessTools.Method(type, "IsLegacyUnityVersion", new Type[1] { typeof(string) }, (Type[])null); _replaceLegacyAssets = AccessTools.Method(type, "ReplaceLegacyAssets", new Type[1] { typeof(TMP_Text) }, (Type[])null); _restoreMaterial = AccessTools.Method(type, "RestoreMaterial", new Type[1] { typeof(TMP_FontAsset) }, (Type[])null); _legacyAssetsSet = AccessTools.Field(type, "_legacyAssets")?.GetValue(null) as HashSet<Object>; if (_getLoadedTextAssets == null || _readBundleUnityVersion == null || _isLegacyUnityVersion == null || _replaceLegacyAssets == null || _legacyAssetsSet == null) { ManualLogSource? log2 = _log; if (log2 != null) { log2.LogError((object)"One or more expected members of REPOLib.Patches.FontAssetMaterialPatch could not be resolved."); } return false; } return true; } private static void LoadFromFilePrefix(string path, out string? __state) { __state = null; _loadFileDepth++; if (_loadFileDepth > 1) { return; } ConfigEntry<bool> enabled = _enabled; if ((enabled != null && !enabled.Value) || string.IsNullOrEmpty(path) || !File.Exists(path) || _readBundleUnityVersion == null || _isLegacyUnityVersion == null) { return; } try { string text = _readBundleUnityVersion.Invoke(null, new object[1] { path }) as string; if (!string.IsNullOrEmpty(text) && (bool)_isLegacyUnityVersion.Invoke(null, new object[1] { text })) { __state = path; LogVerbose("Detected legacy Unity " + text + " bundle: '" + Path.GetFileName(path) + "'."); } } catch (Exception ex) { ManualLogSource? log = _log; if (log != null) { log.LogWarning((object)("Failed reading Unity version for '" + path + "': " + ex.Message)); } } } private static void LoadFromFilePostfix(AssetBundle? __result, string? __state) { try { if (_loadFileDepth == 1 && (Object)(object)__result != (Object)null && __state != null) { string fileName = Path.GetFileName(__state); _legacyBundles[__result] = fileName; LogVerbose("Tracked legacy AssetBundle '" + fileName + "' for implicit TMP dependency scanning."); } } finally { if (_loadFileDepth > 0) { _loadFileDepth--; } } } private static void LoadAssetPrefix(AssetBundle __instance, out LoadAssetState? __state) { __state = null; _assetLoadDepth++; if (_assetLoadDepth > 1) { return; } ConfigEntry<bool> enabled = _enabled; if ((enabled != null && !enabled.Value) || (Object)(object)__instance == (Object)null || _getLoadedTextAssets == null || !_legacyBundles.TryGetValue(__instance, out string value)) { return; } try { if (_getLoadedTextAssets.Invoke(null, null) is IEnumerable<Object> collection) { __state = new LoadAssetState { BundleName = value, Snapshot = new HashSet<Object>(collection) }; } } catch (Exception ex) { ManualLogSource? log = _log; if (log != null) { log.LogWarning((object)("GetLoadedTextAssets failed before loading from '" + value + "': " + ex.Message)); } } } private static void LoadAssetPostfix(LoadAssetState? __state) { try { if (_assetLoadDepth != 1 || __state?.Snapshot == null || _getLoadedTextAssets == null || _legacyAssetsSet == null || !(_getLoadedTextAssets.Invoke(null, null) is IEnumerable<Object> enumerable)) { return; } List<string> list = new List<string>(); HashSet<string> hashSet = new HashSet<string>(StringComparer.Ordinal); int num = 0; foreach (Object item in enumerable) { if (!(item == (Object)null) && !__state.Snapshot.Contains(item) && _legacyAssetsSet.Add(item)) { num++; if (hashSet.Add(item.name)) { list.Add(item.name); } } } if (num > 0) { ManualLogSource? log = _log; if (log != null) { log.LogInfo((object)string.Format("Registered {0} legacy TMP asset(s) from '{1}' ({2}) with REPOLib.", num, __state.BundleName, string.Join(", ", list))); } } } catch (Exception ex) { ManualLogSource? log2 = _log; if (log2 != null) { log2.LogWarning((object)("Failed registering legacy TMP assets from '" + __state?.BundleName + "': " + ex.Message)); } } finally { if (_assetLoadDepth > 0) { _assetLoadDepth--; } } } private static void OnSceneLoaded(Scene scene, LoadSceneMode mode) { ConfigEntry<bool> enabled = _enabled; if (enabled == null || enabled.Value) { RefreshSceneTextAssets("SceneLoaded:" + ((Scene)(ref scene)).name); } } private static void RefreshSceneTextAssets(string trigger) { ConfigEntry<bool> enabled = _enabled; if ((enabled != null && !enabled.Value) || _legacyAssetsSet == null || _replaceLegacyAssets == null) { return; } int num = 0; if (_restoreMaterial != null) { TMP_FontAsset[] array = Resources.FindObjectsOfTypeAll<TMP_FontAsset>(); foreach (TMP_FontAsset val in array) { if (!((Object)(object)val == (Object)null) && ((Object)(object)((TMP_Asset)val).material == (Object)null || (Object)(object)((TMP_Asset)val).material.shader == (Object)null || ((Object)((TMP_Asset)val).material.shader).name == "Hidden/InternalErrorShader")) { _restoreMaterial.Invoke(null, new object[1] { val }); num++; } } } int num2 = 0; if (_legacyAssetsSet.Count > 0) { TMP_Text[] array2 = Object.FindObjectsByType<TMP_Text>((FindObjectsInactive)1, (FindObjectsSortMode)0); foreach (TMP_Text val2 in array2) { if (!((Object)(object)val2 == (Object)null)) { bool num3 = (Object)(object)val2.font != (Object)null && _legacyAssetsSet.Contains((Object)(object)val2.font); bool flag = (Object)(object)val2.spriteAsset != (Object)null && _legacyAssetsSet.Contains((Object)(object)val2.spriteAsset); if (num3 || flag) { _replaceLegacyAssets.Invoke(null, new object[1] { val2 }); val2.ForceMeshUpdate(true, true); num2++; } } } } if (num > 0 || num2 > 0) { LogVerbose($"[{trigger}] Restored {num} font material(s); refreshed {num2} TMP_Text component(s)."); } } private static void LogVerbose(string message) { ConfigEntry<bool> verboseLogging = _verboseLogging; if (verboseLogging != null && verboseLogging.Value) { ManualLogSource? log = _log; if (log != null) { log.LogInfo((object)message); } } } } }