namespace CringeLauncher.Patches; #if false [HarmonyPatch] public static class ModScriptCompilerPatch { private static readonly ILogger Log = LogManager.GetCurrentClassLogger(); private static AssemblyLoadContext _modContext = new(null, true); private static readonly HashSet LoadedModAssemblyNames = new(); private static readonly ConditionalWeakTable LoadContexts = new(); private static readonly FieldInfo InstanceField = AccessTools.Field(typeof(MyProgrammableBlock), "m_instance"); private static readonly FieldInfo AssemblyField = AccessTools.Field(typeof(MyProgrammableBlock), "m_assembly"); private static readonly FieldInfo CompilerErrorsField = AccessTools.Field(typeof(MyProgrammableBlock), "m_compilerErrors"); static ModScriptCompilerPatch() { MySession.OnUnloaded += OnUnloaded; ModWhitelistAnalyzer = AccessTools.FieldRefAccess( MyScriptCompiler.Static, "m_modApiWhitelistDiagnosticAnalyzer"); ScriptWhitelistAnalyzer = AccessTools.FieldRefAccess( MyScriptCompiler.Static, "m_ingameWhitelistDiagnosticAnalyzer"); MetadataReferences = AccessTools.FieldRefAccess>( MyScriptCompiler.Static, "m_metadataReferences"); InjectMod = AccessTools.MethodDelegate>( AccessTools.Method(typeof(MyScriptCompiler), "InjectMod"), MyScriptCompiler.Static); InjectInstructionCounter = AccessTools.MethodDelegate>( AccessTools.Method(typeof(MyScriptCompiler), "InjectInstructionCounter"), MyScriptCompiler.Static); EmitDiagnostics = AccessTools.MethodDelegate, bool, Task>>( AccessTools.Method(typeof(MyScriptCompiler), "EmitDiagnostics"), MyScriptCompiler.Static); MakeAssemblyName = AccessTools.MethodDelegate>(AccessTools.Method(typeof(MyScriptCompiler), "MakeAssemblyName")); CreateInstanceMethod = AccessTools.Method(typeof(MyProgrammableBlock), "CreateInstance"); SetDetailedInfoMethod = AccessTools.Method(typeof(MyProgrammableBlock), "SetDetailedInfo"); } private static void OnUnloaded() { LoadedModAssemblyNames.Clear(); if (!_modContext.Assemblies.Any()) return; _modContext.Unload(); _modContext = new(null, true); } [HarmonyPatch(typeof(MyProgrammableBlock), "Compile")] [HarmonyPrefix] private static bool CompilePrefix(MyProgrammableBlock __instance, string program, string storage, bool instantiate, ref MyProgrammableBlock.ScriptTerminationReason ___m_terminationReason, MyIngameScriptComponent ___ScriptComponent) { if (!MySession.Static.EnableIngameScripts || __instance.CubeGrid is {IsPreview: true} or {CreatePhysics: false}) return false; ___m_terminationReason = MyProgrammableBlock.ScriptTerminationReason.None; CompileAsync(__instance, program, storage, instantiate, ___ScriptComponent); return false; } [HarmonyPatch(typeof(MyGuiScreenEditor), "CheckCodeButtonClicked")] [HarmonyPrefix] private static bool GuiCompilePrefix(List ___m_compilerErrors, MyGuiScreenEditor __instance) { ___m_compilerErrors.Clear(); var progress = new MyGuiScreenProgress(MyTexts.Get(MySpaceTexts.ProgrammableBlock_Editor_CheckingCode)); MyScreenManager.AddScreen(progress); if (__instance.Description.Text.Length > 0) CompileAsync(__instance, ___m_compilerErrors, __instance.Description.Text.ToString(), progress).Wait(); return false; } [HarmonyPatch(typeof(MyScriptCompiler), nameof(MyScriptCompiler.Compile))] [HarmonyPrefix] private static bool Prefix(ref Task __result, MyApiTarget target, string assemblyName, IEnumerable