/// <summary> /// A simple constructor that initializes the object with the given dependencies. /// </summary> /// <param name="p_eifEnvironmentInfo">The application's envrionment info.</param> /// <param name="p_gmrInstalledGames">The registry of insalled games.</param> /// <param name="p_gmdGameMode">The game mode currently being managed.</param> /// <param name="p_mrpModRepository">The repository we are logging in to.</param> /// <param name="p_dmtMonitor">The download monitor to use to track task progress.</param> /// <param name="p_umgUpdateManager">The update manager to use to perform updates.</param> /// <param name="p_mmgModManager">The <see cref="ModManager"/> to use to manage mods.</param> /// <param name="p_pmgPluginManager">The <see cref="PluginManager"/> to use to manage plugins.</param> public MainFormVM(IEnvironmentInfo p_eifEnvironmentInfo, GameModeRegistry p_gmrInstalledGames, IGameMode p_gmdGameMode, IModRepository p_mrpModRepository, DownloadMonitor p_dmtMonitor, ActivateModsMonitor p_ammMonitor, UpdateManager p_umgUpdateManager, ModManager p_mmgModManager, IPluginManager p_pmgPluginManager) { EnvironmentInfo = p_eifEnvironmentInfo; GameMode = p_gmdGameMode; GameMode.GameLauncher.GameLaunching += new CancelEventHandler(GameLauncher_GameLaunching); ModManager = p_mmgModManager; ModRepository = p_mrpModRepository; UpdateManager = p_umgUpdateManager; ModManagerVM = new ModManagerVM(p_mmgModManager, p_eifEnvironmentInfo.Settings, p_gmdGameMode.ModeTheme); if (GameMode.UsesPlugins) { PluginManagerVM = new PluginManagerVM(p_pmgPluginManager, p_eifEnvironmentInfo.Settings, p_gmdGameMode); } DownloadMonitorVM = new DownloadMonitorVM(p_dmtMonitor, p_eifEnvironmentInfo.Settings, p_mmgModManager, p_mrpModRepository); ActivateModsMonitorVM = new ActivateModsMonitorVM(p_ammMonitor, p_eifEnvironmentInfo.Settings, p_mmgModManager); HelpInfo = new HelpInformation(p_eifEnvironmentInfo); GeneralSettingsGroup gsgGeneralSettings = new GeneralSettingsGroup(p_eifEnvironmentInfo); foreach (IModFormat mftFormat in p_mmgModManager.ModFormats) { gsgGeneralSettings.AddFileAssociation(mftFormat.Extension, mftFormat.Name); } ModOptionsSettingsGroup mosModOptions = new ModOptionsSettingsGroup(p_eifEnvironmentInfo); List <ISettingsGroupView> lstSettingGroups = new List <ISettingsGroupView>(); lstSettingGroups.Add(new GeneralSettingsPage(gsgGeneralSettings)); lstSettingGroups.Add(new ModOptionsPage(mosModOptions)); DownloadSettingsGroup dsgDownloadSettings = new DownloadSettingsGroup(p_eifEnvironmentInfo, ModRepository); lstSettingGroups.Add(new DownloadSettingsPage(dsgDownloadSettings)); if (p_gmdGameMode.SettingsGroupViews != null) { lstSettingGroups.AddRange(p_gmdGameMode.SettingsGroupViews); } SettingsFormVM = new SettingsFormVM(p_gmdGameMode, p_eifEnvironmentInfo, lstSettingGroups); UpdateCommand = new Command("Update", String.Format("Update {0}", EnvironmentInfo.Settings.ModManagerName), UpdateProgramme); LogoutCommand = new Command("Logout", "Logout", Logout); List <Command> lstChangeGameModeCommands = new List <Command>(); List <IGameModeDescriptor> lstSortedModes = new List <IGameModeDescriptor>(p_gmrInstalledGames.RegisteredGameModes); lstSortedModes.Sort((x, y) => x.Name.CompareTo(y.Name)); foreach (IGameModeDescriptor gmdInstalledGame in lstSortedModes) { string strId = gmdInstalledGame.ModeId; string strName = gmdInstalledGame.Name; string strDescription = String.Format("Change game to {0}", gmdInstalledGame.Name); Image imgCommandIcon = new Icon(gmdInstalledGame.ModeTheme.Icon, 32, 32).ToBitmap(); lstChangeGameModeCommands.Add(new Command(strId, strName, strDescription, imgCommandIcon, () => ChangeGameMode(strId), true)); } lstChangeGameModeCommands.Add(new Command("Change Default Game...", "Change Default Game", () => ChangeGameMode(CHANGE_DEFAULT_GAME_MODE))); lstChangeGameModeCommands.Add(new Command("Rescan Installed Games...", "Rescan Installed Games", () => ChangeGameMode(RESCAN_INSTALLED_GAMES))); ChangeGameModeCommands = lstChangeGameModeCommands; }
/// <summary> /// Gets a registry of installed game modes. /// </summary> /// <param name="supportedGameModes">The games modes supported by the mod manager.</param> /// <returns>A registry of installed game modes.</returns> protected GameModeRegistry GetInstalledGameModes(GameModeRegistry supportedGameModes) { if (!_environmentInfo.Settings.InstalledGamesDetected) { var gdrGameDetector = new GameDiscoverer(); var vmlGameDetection = new GameDetectionVM(_environmentInfo, gdrGameDetector, supportedGameModes); var frmGameDetector = new GameDetectionForm(vmlGameDetection); gdrGameDetector.Find(supportedGameModes.RegisteredGameModeFactories); frmGameDetector.ShowDialog(); if (gdrGameDetector.Status != TaskStatus.Complete) { return(null); } if (gdrGameDetector.DiscoveredGameModes.Count == 0) { return(null); } _environmentInfo.Settings.InstalledGames.Clear(); var j = 0; foreach (var gidGameMode in gdrGameDetector.DiscoveredGameModes) { if (gidGameMode?.GameMode != null) { var gmfGameModeFactory = supportedGameModes.GetGameMode(gidGameMode.GameMode.ModeId); _environmentInfo.Settings.InstallationPaths[gidGameMode.GameMode.ModeId] = gmfGameModeFactory.GetInstallationPath(gidGameMode.GameInstallPath); _environmentInfo.Settings.ExecutablePaths[gidGameMode.GameMode.ModeId] = gmfGameModeFactory.GetExecutablePath(gidGameMode.GameInstallPath); _environmentInfo.Settings.InstalledGames.Add(gidGameMode.GameMode.ModeId); } else { MessageBox.Show($"An error occured during the scan of the game {gdrGameDetector.DiscoveredGameModes[j].GameMode.ModeId} : {Environment.NewLine + "The object GameMode is NULL"}"); } j++; } _environmentInfo.Settings.InstalledGamesDetected = true; _environmentInfo.Settings.CacheOverhaulSetup = false; _environmentInfo.Settings.Save(); } var gmrInstalledGameModes = GameModeRegistry.LoadInstalledGameModes(supportedGameModes, _environmentInfo); return(gmrInstalledGameModes); }
/// <summary> /// Checks if the default game mode is installed, and clears the setting if that is not the case. /// </summary> /// <param name="installedGames">GameModeRegistry of installed games.</param> private void CheckIfDefaultGameModeIsInstalled(GameModeRegistry installedGames) { var found = false; foreach (var availableModes in installedGames.RegisteredGameModes) { if (availableModes.ModeId.Equals(_environmentInfo.Settings.RememberedGameMode, StringComparison.OrdinalIgnoreCase)) { found = true; } } if (!found) { Trace.TraceWarning($"Remembered game mode \"{_environmentInfo.Settings.RememberedGameMode}\" was not found in installed games list, clearing setting."); _environmentInfo.Settings.RememberGameMode = false; } }
/// <summary> /// Gets a registry of installed game modes. /// </summary> /// <param name="p_gmrSupportedGameModes">The games modes supported by the mod manager.</param> /// <returns>A registry of installed game modes.</returns> protected GameModeRegistry GetInstalledGameModes(GameModeRegistry p_gmrSupportedGameModes) { if (!m_eifEnvironmentInfo.Settings.InstalledGamesDetected) { GameDiscoverer gdrGameDetector = new GameDiscoverer(); GameDetectionVM vmlGameDetection = new GameDetectionVM(m_eifEnvironmentInfo, gdrGameDetector, p_gmrSupportedGameModes); GameDetectionForm frmGameDetector = new GameDetectionForm(vmlGameDetection); gdrGameDetector.Find(p_gmrSupportedGameModes.RegisteredGameModeFactories); frmGameDetector.ShowDialog(); if (gdrGameDetector.Status != TaskStatus.Complete) { return(null); } if (gdrGameDetector.DiscoveredGameModes.Count == 0) { return(null); } m_eifEnvironmentInfo.Settings.InstalledGames.Clear(); Int32 j = 0; foreach (GameDiscoverer.GameInstallData gidGameMode in gdrGameDetector.DiscoveredGameModes) { if ((gidGameMode != null) && (gidGameMode.GameMode != null)) { IGameModeFactory gmfGameModeFactory = p_gmrSupportedGameModes.GetGameMode(gidGameMode.GameMode.ModeId); m_eifEnvironmentInfo.Settings.InstallationPaths[gidGameMode.GameMode.ModeId] = gmfGameModeFactory.GetInstallationPath(gidGameMode.GameInstallPath); m_eifEnvironmentInfo.Settings.ExecutablePaths[gidGameMode.GameMode.ModeId] = gmfGameModeFactory.GetExecutablePath(gidGameMode.GameInstallPath); m_eifEnvironmentInfo.Settings.InstalledGames.Add(gidGameMode.GameMode.ModeId); } else { MessageBox.Show(string.Format("An error occured during the scan of the game {0} : {1}", gdrGameDetector.DiscoveredGameModes[j].GameMode.ModeId, Environment.NewLine + "The object GameMode is NULL")); } j++; } m_eifEnvironmentInfo.Settings.InstalledGamesDetected = true; m_eifEnvironmentInfo.Settings.CacheOverhaulSetup = false; m_eifEnvironmentInfo.Settings.Save(); } GameModeRegistry gmrInstalledGameModes = GameModeRegistry.LoadInstalledGameModes(p_gmrSupportedGameModes, m_eifEnvironmentInfo); return(gmrInstalledGameModes); }
/// <summary> /// Gets a registry of supported game modes. /// </summary> /// <returns>A registry of supported game modes.</returns> protected GameModeRegistry GetSupportedGameModes() { return(GameModeRegistry.DiscoverSupportedGameModes(_environmentInfo)); }
/// <summary> /// Runs the applications /// </summary> /// <remarks> /// This method makes sure the environment is sane. If so, it creates the required services /// and launches the main form. /// </remarks> /// <param name="p_strArgs">The command line arguments passed to the application.</param> /// <returns><c>true</c> if the application started as expected; /// <c>false</c> otherwise.</returns> public bool RunMainForm(string[] p_strArgs) { if (!SandboxCheck(m_eifEnvironmentInfo)) return false; SetCompressorPath(m_eifEnvironmentInfo); string strRequestedGameMode = null; string[] strArgs = p_strArgs; Uri uriModToAdd = null; if ((p_strArgs.Length > 0) && !p_strArgs[0].StartsWith("-")) { if (Uri.TryCreate(p_strArgs[0], UriKind.Absolute, out uriModToAdd) && uriModToAdd.Scheme.Equals("nxm", StringComparison.OrdinalIgnoreCase)) strRequestedGameMode = uriModToAdd.Host; } else for (Int32 i = 0; i < p_strArgs.Length; i++) { string strArg = p_strArgs[i]; if (strArg.StartsWith("-")) { switch (strArg) { case "-game": strRequestedGameMode = p_strArgs[i + 1]; Trace.Write("Game Specified On Command line: " + strRequestedGameMode + ") "); break; } } } bool booChangeDefaultGameMode = false; GameModeRegistry gmrSupportedGames = GetSupportedGameModes(); do { NexusFontSetResolver nfrResolver = SetUpFonts(); GameModeRegistry gmrInstalledGames = GetInstalledGameModes(gmrSupportedGames); if (gmrInstalledGames == null) { Trace.TraceInformation("No installed games."); MessageBox.Show(String.Format("No games were detected! {0} will now close.", m_eifEnvironmentInfo.Settings.ModManagerName), "No Games", MessageBoxButtons.OK, MessageBoxIcon.Error); return false; } GameModeSelector gmsSelector = new GameModeSelector(gmrSupportedGames, gmrInstalledGames, m_eifEnvironmentInfo); IGameModeFactory gmfGameModeFactory = gmsSelector.SelectGameMode(strRequestedGameMode, booChangeDefaultGameMode); if (gmsSelector.RescanRequested) { m_eifEnvironmentInfo.Settings.InstalledGamesDetected = false; m_eifEnvironmentInfo.Settings.Save(); booChangeDefaultGameMode = true; continue; } if (gmfGameModeFactory == null) return false; Trace.TraceInformation(String.Format("Game Mode Factory Selected: {0} ({1})", gmfGameModeFactory.GameModeDescriptor.Name, gmfGameModeFactory.GameModeDescriptor.ModeId)); Mutex mtxGameModeMutex = null; bool booOwnsMutex = false; try { for (Int32 intAttemptCount = 0; intAttemptCount < 3; intAttemptCount++) { Trace.TraceInformation("Creating Game Mode mutex (Attempt: {0})", intAttemptCount); mtxGameModeMutex = new Mutex(true, String.Format("{0}-{1}-GameModeMutex", m_eifEnvironmentInfo.Settings.ModManagerName, gmfGameModeFactory.GameModeDescriptor.ModeId), out booOwnsMutex); //If the mutex is owned, you are the first instance of the mod manager for game mode, so break out of loop. if (booOwnsMutex) break; try { //If the mutex isn't owned, attempt to talk across the messager. using (IMessager msgMessager = MessagerClient.GetMessager(m_eifEnvironmentInfo, gmfGameModeFactory.GameModeDescriptor)) { if (msgMessager != null) { //Messenger was created OK, send download request, or bring to front. if (uriModToAdd != null) { Trace.TraceInformation(String.Format("Messaging to add: {0}", uriModToAdd)); msgMessager.AddMod(uriModToAdd.ToString()); } else { Trace.TraceInformation(String.Format("Messaging to bring to front.")); msgMessager.BringToFront(); } return true; } } mtxGameModeMutex.Close(); mtxGameModeMutex = null; } catch (InvalidOperationException) { StringBuilder stbPromptMessage = new StringBuilder(); stbPromptMessage.AppendFormat("{0} was unable to start. It appears another instance of {0} is already running.", m_eifEnvironmentInfo.Settings.ModManagerName).AppendLine(); stbPromptMessage.AppendFormat("If you were trying to download multiple files, wait for {0} to start before clicking on a new file download.", m_eifEnvironmentInfo.Settings.ModManagerName).AppendLine(); MessageBox.Show(stbPromptMessage.ToString(), "Already running", MessageBoxButtons.OK, MessageBoxIcon.Information); return false; } //Messenger couldn't be created, so sleep for a few seconds to give time for opening // the running copy of the mod manager to start up/shut down Thread.Sleep(TimeSpan.FromSeconds(5.0d)); } if (!booOwnsMutex) { HeaderlessTextWriterTraceListener htlListener = (HeaderlessTextWriterTraceListener)Trace.Listeners["DefaultListener"]; htlListener.ChangeFilePath(Path.Combine(Path.GetDirectoryName(htlListener.FilePath), "Messager" + Path.GetFileName(htlListener.FilePath))); Trace.TraceInformation("THIS IS A MESSAGER TRACE LOG."); if (!htlListener.TraceIsForced) htlListener.SaveToFile(); StringBuilder stbPromptMessage = new StringBuilder(); stbPromptMessage.AppendFormat("{0} was unable to start. It appears another instance of {0} is already running.", m_eifEnvironmentInfo.Settings.ModManagerName).AppendLine(); stbPromptMessage.AppendLine("A Trace Log file was created at:"); stbPromptMessage.AppendLine(htlListener.FilePath); stbPromptMessage.AppendLine("Before reporting the issue, don't close this window and check for a fix here (you can close it afterwards):"); stbPromptMessage.AppendLine(NexusLinks.FAQs); stbPromptMessage.AppendLine("If you can't find a solution, please make a bug report and attach the TraceLog file here:"); stbPromptMessage.AppendLine(NexusLinks.Issues); MessageBox.Show(stbPromptMessage.ToString(), "Error", MessageBoxButtons.OK, MessageBoxIcon.Information); return false; } //ApplicationInitializer ainInitializer = new ApplicationInitializer(m_eifEnvironmentInfo, nfrResolver); //ApplicationInitializationForm frmAppInitilizer = new ApplicationInitializationForm(ainInitializer); //ainInitializer.Initialize(gmfGameModeFactory, SynchronizationContext.Current); //frmAppInitilizer.ShowDialog(); ApplicationInitializer ainInitializer = null; ApplicationInitializationForm frmAppInitilizer = null; while ((ainInitializer == null) || (ainInitializer.Status == TaskStatus.Retrying)) { ainInitializer = new ApplicationInitializer(m_eifEnvironmentInfo, nfrResolver); frmAppInitilizer = new ApplicationInitializationForm(ainInitializer); ainInitializer.Initialize(gmfGameModeFactory, SynchronizationContext.Current); frmAppInitilizer.ShowDialog(); } if (ainInitializer.Status != TaskStatus.Complete) { if (ainInitializer.Status == TaskStatus.Error) return false; booChangeDefaultGameMode = true; DisposeServices(ainInitializer.Services); continue; } IGameMode gmdGameMode = ainInitializer.GameMode; ServiceManager svmServices = ainInitializer.Services; MainFormVM vmlMainForm = new MainFormVM(m_eifEnvironmentInfo, gmrInstalledGames, gmdGameMode, svmServices.ModRepository, svmServices.DownloadMonitor, svmServices.ActivateModsMonitor, svmServices.UpdateManager, svmServices.ModManager, svmServices.PluginManager); MainForm frmMain = new MainForm(vmlMainForm); using (IMessager msgMessager = MessagerServer.InitializeListener(m_eifEnvironmentInfo, gmdGameMode, svmServices.ModManager, frmMain)) { if (uriModToAdd != null) { Trace.TraceInformation("Adding mod: " + uriModToAdd.ToString()); msgMessager.AddMod(uriModToAdd.ToString()); uriModToAdd = null; } Trace.TraceInformation("Running Application."); try { Application.Run(frmMain); svmServices.ModInstallLog.Backup(); strRequestedGameMode = vmlMainForm.RequestedGameMode; booChangeDefaultGameMode = vmlMainForm.DefaultGameModeChangeRequested; } finally { DisposeServices(svmServices); gmdGameMode.Dispose(); } } } finally { if (mtxGameModeMutex != null) { if (booOwnsMutex) mtxGameModeMutex.ReleaseMutex(); mtxGameModeMutex.Close(); } FileUtil.ForceDelete(m_eifEnvironmentInfo.TemporaryPath); //Clean up created font's. FontManager.Dispose(); } } while (!String.IsNullOrEmpty(strRequestedGameMode) || booChangeDefaultGameMode); return true; }
/// <summary> /// Gets a registry of supported game modes. /// </summary> /// <returns>A registry of supported game modes.</returns> protected GameModeRegistry GetSupportedGameModes() { return GameModeRegistry.DiscoverSupportedGameModes(m_eifEnvironmentInfo); }
/// <summary> /// A simple constructor that initializes the object with the given dependencies. /// </summary> /// <param name="p_eifEnvironmentInfo">The application's envrionment info.</param> /// <param name="p_gdvGameDetector">The discoverer to use to find the game installation path.</param> /// <param name="p_gmrSupportedGameModes">The list of supported game modes.</param> public GameDetectionVM(IEnvironmentInfo p_eifEnvironmentInfo, GameDiscoverer p_gdvGameDetector, GameModeRegistry p_gmrSupportedGameModes) { EnvironmentInfo = p_eifEnvironmentInfo; GameDetector = p_gdvGameDetector; SupportedGameModes = p_gmrSupportedGameModes; }
/// <summary> /// A simple constructor that initializes the object with the given dependencies. /// </summary> /// <param name="p_gmrSupportedGameModes">The games modes supported by the mod manager.</param> /// <param name="p_gmrInstalledGameModes">The game modes factories for installed games.</param> /// <param name="p_eifEnvironmentInfo">The application's envrionment info.</param> public GameModeSelector(GameModeRegistry p_gmrSupportedGameModes, GameModeRegistry p_gmrInstalledGameModes, IEnvironmentInfo p_eifEnvironmentInfo) { SupportedGameModes = p_gmrSupportedGameModes; InstalledGameModes = p_gmrInstalledGameModes; EnvironmentInfo = p_eifEnvironmentInfo; }