using System; using System.Collections.Generic; using System.Diagnostics; using System.IO; using System.Reflection; using System.Resources; using System.Runtime.CompilerServices; using System.Runtime.Versioning; using System.Security; using System.Security.Permissions; using HarmonyLib; using LanguageReplacerBBS; using MelonLoader; using Microsoft.CodeAnalysis; using UnityEngine; [assembly: CompilationRelaxations(8)] [assembly: RuntimeCompatibility(WrapNonExceptionThrows = true)] [assembly: Debuggable(DebuggableAttribute.DebuggingModes.Default | DebuggableAttribute.DebuggingModes.DisableOptimizations | DebuggableAttribute.DebuggingModes.IgnoreSymbolStoreSequencePoints | DebuggableAttribute.DebuggingModes.EnableEditAndContinue)] [assembly: MelonInfo(typeof(Core), "LanguageReplacerBBS", "1.0.1", "Caleb Orchard and Marcel Mazur", null)] [assembly: MelonGame("DefaultCompany", "BabySteps")] [assembly: TargetFramework(".NETCoreApp,Version=v6.0", FrameworkDisplayName = ".NET 6.0")] [assembly: AssemblyCompany("LanguageReplacerBBS")] [assembly: AssemblyConfiguration("Debug")] [assembly: AssemblyFileVersion("1.0.0.0")] [assembly: AssemblyInformationalVersion("1.0.0")] [assembly: AssemblyProduct("LanguageReplacerBBS")] [assembly: AssemblyTitle("LanguageReplacerBBS")] [assembly: NeutralResourcesLanguage("en-US")] [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.Module, AllowMultiple = false, Inherited = false)] internal sealed class RefSafetyRulesAttribute : Attribute { public readonly int Version; public RefSafetyRulesAttribute(int P_0) { Version = P_0; } } } namespace LanguageReplacerBBS { public class Core : MelonMod { [HarmonyPatch(/*Could not decode attribute arguments.*/)] private static class TextAssetPatches { private static void Postfix(TextAsset __instance, ref string __result) { Core instance = Instance; if (instance != null && !((Object)(object)__instance == (Object)null) && !string.IsNullOrEmpty(((Object)__instance).name) && instance.TryGetTextAssetOverride(((Object)__instance).name, out var replacementText)) { __result = replacementText; } } } internal static Core Instance; private readonly Dictionary _embeddedResourceNamesByName = new Dictionary(StringComparer.OrdinalIgnoreCase); private readonly Dictionary _replacementTextByAssetName = new Dictionary(StringComparer.OrdinalIgnoreCase); private bool _hasLoadedTranslationIndex; public override void OnInitializeMelon() { Instance = this; EnsureTranslationIndex(); ((MelonBase)this).HarmonyInstance.PatchAll(typeof(TextAssetPatches)); } private void EnsureTranslationIndex() { if (_hasLoadedTranslationIndex) { return; } _hasLoadedTranslationIndex = true; _embeddedResourceNamesByName.Clear(); Assembly assembly = typeof(Core).Assembly; string[] manifestResourceNames = assembly.GetManifestResourceNames(); foreach (string text in manifestResourceNames) { if (text.StartsWith("Translations/", StringComparison.OrdinalIgnoreCase)) { string fileNameWithoutExtension = Path.GetFileNameWithoutExtension(text); if (!string.IsNullOrWhiteSpace(fileNameWithoutExtension) && !_embeddedResourceNamesByName.ContainsKey(fileNameWithoutExtension)) { _embeddedResourceNamesByName[fileNameWithoutExtension] = text; } } } } internal bool TryGetTextAssetOverride(string assetName, out string replacementText) { replacementText = null; if (string.IsNullOrWhiteSpace(assetName)) { return false; } if (_replacementTextByAssetName.TryGetValue(assetName, out replacementText)) { return true; } if (TryReadEmbeddedTranslation(assetName, out replacementText)) { _replacementTextByAssetName[assetName] = replacementText; return true; } return false; } private bool TryReadEmbeddedTranslation(string translationAssetName, out string text) { text = null; if (string.IsNullOrWhiteSpace(translationAssetName)) { return false; } if (!_embeddedResourceNamesByName.TryGetValue(translationAssetName, out var value)) { return false; } using Stream stream = typeof(Core).Assembly.GetManifestResourceStream(value); if (stream == null) { return false; } using StreamReader streamReader = new StreamReader(stream); text = streamReader.ReadToEnd(); return true; } } }