コード例 #1
0
        private static void Main(string[] args)
        {
            if (!Settings.Default.settingsUpgraded)
            {
                Settings.Default.Upgrade();
                Settings.Default.settingsUpgraded = true;
                Settings.Default.Save();
            }

            AppDomain.CurrentDomain.UnhandledException += CurrentDomain_UnhandledException;
            Application.ThreadException += Application_ThreadException;
            if (Array.IndexOf(args, "-mono") != -1)
            {
                MonoMode = true;
            }
            Directory.SetCurrentDirectory(ExecutableDirectory);
            //Style setup
            Application.EnableVisualStyles();
            Application.SetCompatibleTextRenderingDefault(false);

            if (args.Length > 0 && (args[0] == "-?" || args[0] == "/?" || args[0] == "-help"))
            {
                WriteHelp();
                return;
            }

            var sgmSelectedGame = Settings.Default.rememberedGameMode;
            var booChooseGame   = true;

            if ((args.Length > 0) && args[0].StartsWith("-"))
            {
                switch (args[0])
                {
                case "-game":
                    try
                    {
                        sgmSelectedGame = (SupportedGameModes)Enum.Parse(typeof(SupportedGameModes), args[1], true);
                        booChooseGame   = false;
                    }
                    catch {}
                    break;
                }
            }

            var booChangeGameMode = false;

            do
            {
                if (booChangeGameMode || (booChooseGame && !Settings.Default.rememberGameMode))
                {
                    var gmsSelector = new GameModeSelector();
                    gmsSelector.ShowDialog();
                    sgmSelectedGame = gmsSelector.SelectedGameMode;
                }
                switch (sgmSelectedGame)
                {
                case SupportedGameModes.Fallout3:
                    GameMode = new Fallout3GameMode();
                    break;

                case SupportedGameModes.FalloutNV:
                    GameMode = new FalloutNewVegasGameMode();
                    break;

                default:
                    MessageBox.Show("Unrecognized game selection.", "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
                    return;
                }

                Mutex  mutex;
                bool   booNewMutex;
                string autoLoad = null;

                if (!booChangeGameMode && (args.Length > 0))
                {
                    var booArgsHandled = true;
                    if (!args[0].StartsWith("-") && File.Exists(args[0]))
                    {
                        switch (Path.GetExtension(args[0]).ToLowerInvariant())
                        {
                        case ".rar":
                        case ".7z":
                        case ".zip":
                        case ".fomod":
                            mutex = new Mutex(true, "fommMainMutex", out booNewMutex);
                            mutex.Close();
                            if (!booNewMutex)
                            {
                                Messaging.TransmitMessage(args[0]);
                                return;
                            }
                            autoLoad = args[0];
                            break;

                        default:
                            booArgsHandled = false;
                            break;
                        }
                    }
                    else
                    {
                        switch (args[0])
                        {
                        case "-u":
                            var strGuid = args[1];
                            var strPath = Environment.GetFolderPath(Environment.SpecialFolder.System);
                            var psiInfo = new ProcessStartInfo(strPath + @"\msiexec.exe", "/x " + strGuid);
                            Process.Start(psiInfo);
                            return;

                        default:
                            booArgsHandled = false;
                            break;
                        }
                    }
                    if (!booArgsHandled && GameMode.HandleStandaloneArguments(args))
                    {
                        return;
                    }
                }

                mutex = new Mutex(true, "fommMainMutex", out booNewMutex);
                if (!booNewMutex)
                {
                    MessageBox.Show(ProgrammeAcronym + " is already running", "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
                    mutex.Close();
                    return;
                }

                try
                {
                    string strErrorMessage;
                    if (!GameMode.SetWorkingDirectory(out strErrorMessage))
                    {
                        MessageBox.Show(null, strErrorMessage, "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
                        booChangeGameMode = false;
                        if (Settings.Default.rememberGameMode)
                        {
                            booChangeGameMode = true;
                            Settings.Default.rememberGameMode = false;
                            Settings.Default.Save();
                        }
                        continue;
                    }

                    GameMode.PreInit();

                    //Check that we're in fallout's directory and that we have write access
                    var cancellaunch = true;
                    if (!Settings.Default.NoUACCheck || Array.IndexOf(args, "-no-uac-check") == -1)
                    {
                        try
                        {
                            File.Delete("limited");
                            var strVirtualStore =
                                Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.LocalApplicationData), "VirtualStore\\");
                            strVirtualStore = Path.Combine(strVirtualStore, Directory.GetCurrentDirectory().Remove(0, 3));
                            strVirtualStore = Path.Combine(strVirtualStore, "limited");
                            if (File.Exists(strVirtualStore))
                            {
                                File.Delete(strVirtualStore);
                            }
                            var fs = File.Create("limited");
                            fs.Close();
                            if (File.Exists(strVirtualStore))
                            {
                                MessageBox.Show(
                                    "UAC is preventing Fallout mod manager from obtaining write access to fallout's installation directory.\n" +
                                    "Either right click fomm.exe and check the 'run as administrator' checkbox on the comptibility tab, or disable UAC",
                                    "Error");
                                File.Delete("limited");
                            }
                            else
                            {
                                File.Delete("limited");
                                cancellaunch = false;
                            }
                        }
                        catch
                        {
                            MessageBox.Show(
                                "Unable to get write permissions for:" + Environment.NewLine + GameMode.PluginsPath +
                                Environment.NewLine + "Please read" + Environment.NewLine +
                                Path.Combine(ProgrammeInfoDirectory, "Readme - fomm.txt") + Environment.NewLine +
                                "for the solution.", "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
                        }
                    }
                    else
                    {
                        cancellaunch = false;
                    }

                    if (cancellaunch)
                    {
                        return;
                    }

                    if (!Directory.Exists(tmpPath))
                    {
                        Directory.CreateDirectory(tmpPath);
                    }

                    var str7zPath = "";

                    if (Environment.Is64BitProcess)
                    {
                        str7zPath = Path.Combine(ProgrammeInfoDirectory, "7z-64bit.dll");
                    }
                    else
                    {
                        str7zPath = Path.Combine(ProgrammeInfoDirectory, "7z-32bit.dll");
                    }
                    SevenZipBase.SetLibraryPath(str7zPath);

                    if (!GameMode.Init())
                    {
                        return;
                    }
                    PermissionsManager.Init();

                    //check to see if we need to upgrade the install log format
                    if (InstallLog.Current.GetInstallLogVersion() != InstallLog.CURRENT_VERSION)
                    {
                        var iluUgrader = new InstallLogUpgrader();
                        try
                        {
                            MessageBox.Show(
                                "FOMM needs to upgrade some of its files. This could take a few minutes, depending on how many mods are installed.",
                                "Upgrade Required");
                            if (!iluUgrader.UpgradeInstallLog())
                            {
                                return;
                            }
                        }
                        catch (Exception e)
                        {
                            HandleException(e, "An error occurred while upgrading your log file.", "Upgrade Error");
                            return;
                        }
                    }

                    InstallLog.Reload();

                    //let's uninstall any fomods that have been deleted since we last ran
                    var lstMods = InstallLog.Current.GetVersionedModList();
                    foreach (var fifMod in lstMods)
                    {
                        var strFomodPath = Path.Combine(GameMode.ModDirectory, fifMod.BaseName + ".fomod");
                        if (!File.Exists(strFomodPath))
                        {
                            var strMessage = "'" + fifMod.BaseName + ".fomod' was deleted without being deactivated. " +
                                             Environment.NewLine +
                                             "If you don't uninstall the FOMod, FOMM will close and you will " +
                                             "have to put the FOMod back in the mods folder." + Environment.NewLine +
                                             "Would you like to uninstall the missing FOMod?";
                            if (MessageBox.Show(strMessage, "Missing FOMod", MessageBoxButtons.YesNo, MessageBoxIcon.Warning) ==
                                DialogResult.No)
                            {
                                return;
                            }
                            var mduUninstaller = new ModUninstaller(fifMod.BaseName);
                            mduUninstaller.Uninstall(true);
                        }
                    }

                    try
                    {
                        //check to see if any fomod versions have changed, and whether to upgrade them
                        var upsScanner = new UpgradeScanner();
                        upsScanner.Scan();
                    }
                    catch (Exception e)
                    {
                        HandleException(e, "An error occurred while scanning your fomods for new versions.", "Scan Error");
                        return;
                    }

                    if (booChangeGameMode || !GameMode.HandleInAppArguments(args))
                    {
                        try
                        {
                            var frmMain = new MainForm(autoLoad);
                            frmMain.Text += " (" + MVersion + ") - " + GameMode.GameName;

                            Application.Run(frmMain);
                            booChangeGameMode = frmMain.ChangeGameMode;
                        }
                        catch (Exception e)
                        {
                            HandleException(e, "Something bad seems to have happened.", "Error");
                        }
                    }

                    //backup the install log
                    if (File.Exists(InstallLog.Current.InstallLogPath))
                    {
                        var      strLogPath       = InstallLog.Current.InstallLogPath + ".bak";
                        var      fifInstallLog    = new FileInfo(InstallLog.Current.InstallLogPath);
                        FileInfo fifInstallLogBak = null;
                        if (File.Exists(strLogPath))
                        {
                            fifInstallLogBak = new FileInfo(strLogPath);
                        }

                        if ((fifInstallLogBak == null) || (fifInstallLogBak.LastWriteTimeUtc != fifInstallLog.LastWriteTimeUtc))
                        {
                            for (var i = 4; i > 0; i--)
                            {
                                if (File.Exists(strLogPath + i))
                                {
                                    File.Copy(strLogPath + i, strLogPath + (i + 1), true);
                                }
                            }
                            if (File.Exists(strLogPath))
                            {
                                File.Copy(strLogPath, strLogPath + "1", true);
                            }
                            File.Copy(InstallLog.Current.InstallLogPath, InstallLog.Current.InstallLogPath + ".bak", true);
                        }
                    }

                    FileUtil.ForceDelete(tmpPath);
                }
                finally
                {
                    if (mutex != null)
                    {
                        mutex.Close();
                    }
                }
            }while (booChangeGameMode);
        }