Ejemplo 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");
                            if (module.SaveDataAsync)
                            {
                                module.WriteSaveData(SaveData.Instance.FileSlot, module.SerializeSaveData(SaveData.Instance.FileSlot));
                            }
                            else
                            {
#pragma warning disable CS0618 // Synchronous save / load IO is obsolete but some mods still override / use it.
                                if (CoreModule.Settings.SaveDataFlush ?? false)
                                {
                                    module.ForceSaveDataFlush++;
                                }
                                module.SaveSaveData(SaveData.Instance.FileSlot);
#pragma warning restore CS0618
                            }

                            if (SaveData.Instance.CurrentSession?.InArea ?? false)
                            {
                                Logger.Log("core", $"Saving session slot {SaveData.Instance.FileSlot} for {module.Metadata} before reloading");
                                if (module.SaveDataAsync)
                                {
                                    module.WriteSession(SaveData.Instance.FileSlot, module.SerializeSession(SaveData.Instance.FileSlot));
                                }
                                else
                                {
#pragma warning disable CS0618 // Synchronous save / load IO is obsolete but some mods still override / use it.
                                    if (CoreModule.Settings.SaveDataFlush ?? false)
                                    {
                                        module.ForceSaveDataFlush++;
                                    }
                                    module.SaveSession(SaveData.Instance.FileSlot);
#pragma warning restore CS0618
                                }
                            }
                        }

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