Esempio n. 1
0
            internal static void ReloadModAssembly(object source, FileSystemEventArgs e, bool retrying = false)
            {
                if (!File.Exists(e.FullPath))
                {
                    return;
                }

                Logger.Log(LogLevel.Info, "loader", $"Reloading mod assembly: {e.FullPath}");
                QueuedTaskHelper.Do("ReloadModAssembly:" + e.FullPath, () => {
                    EverestModule module = _Modules.FirstOrDefault(m => m.Metadata.DLL == e.FullPath);
                    if (module == null)
                    {
                        return;
                    }

                    AssetReloadHelper.Do($"{Dialog.Clean("ASSETRELOADHELPER_RELOADINGMODASSEMBLY")} {Path.GetFileName(e.FullPath)}", () => {
                        Assembly asm = null;
                        using (FileStream stream = File.OpenRead(e.FullPath))
                            asm = Relinker.GetRelinkedAssembly(module.Metadata, Path.GetFileNameWithoutExtension(e.FullPath), stream);

                        if (asm == null)
                        {
                            if (!retrying)
                            {
                                // Retry.
                                QueuedTaskHelper.Do("ReloadModAssembly:" + e.FullPath, () => {
                                    ReloadModAssembly(source, e, true);
                                });
                            }
                            return;
                        }

                        ((FileSystemWatcher)source).Dispose();

                        // be sure to save this module's save data and session before reloading it, so that they are not lost.
                        if (SaveData.Instance != null)
                        {
                            Logger.Log("core", $"Saving save data slot {SaveData.Instance.FileSlot} for {module.Metadata} before reloading");
                            module.SaveSaveData(SaveData.Instance.FileSlot);

                            if (SaveData.Instance.CurrentSession?.InArea ?? false)
                            {
                                Logger.Log("core", $"Saving session slot {SaveData.Instance.FileSlot} for {module.Metadata} before reloading");
                                module.SaveSession(SaveData.Instance.FileSlot);
                            }
                        }

                        Unregister(module);
                        LoadModAssembly(module.Metadata, asm);
                    });
                    AssetReloadHelper.ReloadLevel();
                });
            }