private static void CreatePreloadedContext(string entryPointArg) { Runtime.AssemblyContextRef contextRef = Runtime.AssemblyContextRef.Invalid; if (SharedRuntimeState.CurrentRuntime == EDotNetRuntime.CoreCLR) { contextRef = Runtime.AssemblyContext.Create(); } else { // Seperate method to avoid issues with .NET Core contextRef = CreatePreloadedContextAppDomain(entryPointArg); } entryPointArg += "|AssemblyContext=" + contextRef.Format(); if (!contextRef.IsInvalid) { Debug.Assert(preloadedContextRef.IsInvalid, "Trying to preload when there is already something preloaded"); try { AssemblyLoader loader = new AssemblyLoader(mainAssemblyPath, entryPointType, entryPointMethod, entryPointArg, true, contextRef); contextRef.DoCallBack(loader.Load); preloadedContextRef = contextRef; } catch (Exception e) { GameThreadHelper.Run(delegate() // For stylized message box (as we may not be in the game thread) { MessageBox("Failed to create assembly context for \"" + mainAssemblyPath + "\" " + Environment.NewLine + Environment.NewLine + e, errorMsgBoxTitle); }); preloadFailed = true; UnloadContext(contextRef); } } }
private static bool ReloadMainContext(bool threaded = true) { if (!GameThreadHelper.IsInGameThread()) { bool result = false; GameThreadHelper.Run(delegate { result = ReloadMainContext(); }); return(result); } if (!SharedRuntimeState.IsActiveRuntime) { return(false); } if (!File.Exists(mainAssemblyPath)) { SharedRuntimeState.SetHotReloadData(null); return(false); } if (!mainContextRef.IsInvalid) { UnloadMainContext(threaded); } string entryPointArgEx = entryPointArg; bool firstLoad = preloadContextWaitHandle == null; if (firstLoad) { PreloadNextContext(threaded); } else { entryPointArgEx += "|Reloading=true"; } preloadContextWaitHandle.WaitOne(Timeout.Infinite); preloadContextWaitHandle.Reset(); if (!preloadFailed) { Debug.Assert(!preloadedContextRef.IsInvalid, "Preloaded context shouldn't be invalid"); mainContextRef = preloadedContextRef; preloadedContextRef = Runtime.AssemblyContextRef.Invalid; entryPointArgEx += "|AssemblyContext=" + mainContextRef.Format(); try { AssemblyLoader loader = new AssemblyLoader(mainAssemblyPath, entryPointType, entryPointMethod, entryPointArgEx, false, mainContextRef); mainContextRef.DoCallBack(loader.Load); UpdateAssemblyWatchers(); } catch (Exception e) { MessageBox("Failed to create assembly context for \"" + mainAssemblyPath + "\" " + Environment.NewLine + Environment.NewLine + e, errorMsgBoxTitle); } } PreloadNextContext(threaded); SharedRuntimeState.SetHotReloadData(null); return(true); }