예제 #1
0
        /// <summary>
        /// Disposes of the form after saving the changes made
        /// </summary>
        private void saveButton_Click(object sender, EventArgs e)
        {
            if (optionsResolutionAdd.Text == "Save")
            {
                // Show the dialog
                DialogResult result = MessageBox.Show("You did not save the radius you were editing. Are you sure you want to continue?",
                    "Radius not saved", MessageBoxButtons.YesNo, MessageBoxIcon.Warning);

                if (result == System.Windows.Forms.DialogResult.No)
                {
                    // Quit the save process
                    return;
                }
            }

            #region Save Settings

            using (IniFile iniFile = new IniFile())
            {
                // Switch: Default profile
                iniFile.IniPath = Application.StartupPath + @"\locations.ini";
                iniFile.IniPath = Application.StartupPath + iniFile.ReadValue("PROFILES", "0");

                #region General

                #region Run the LODRE when FSX starts

                if (optionsGeneral0.Checked)
                {
                    // Write the setting value
                    iniFile.WriteValue("OPTIONS.GENERAL", "runfsx", "1");

                    ///<switch>locations.ini</switch>
                    iniFile.IniPath = Application.StartupPath + @"\locations.ini";

                    using (ExeXml exeXml = new ExeXml(iniFile.ReadValue("LOCATIONS", "cfg") + @"\exe.xml"))
                    {
                        ///<switch>current profile</switch>
                        iniFile.IniPath = Application.StartupPath + @"\locations.ini";
                        iniFile.IniPath = Application.StartupPath + iniFile.ReadValue("PROFILES", "0");

                        // Check the existence of the document
                        exeXml.CheckExistence("Level of Detail Radius Editor", false, Application.ExecutablePath);
                    }
                }
                else
                {
                    // Write the setting value
                    iniFile.WriteValue("OPTIONS.GENERAL", "runfsx", "0");

                    ///<switch>locations.ini</switch>
                    iniFile.IniPath = Application.StartupPath + @"\locations.ini";

                    using (ExeXml exeXml = new ExeXml(iniFile.ReadValue("LOCATIONS", "cfg") + @"\exe.xml"))
                    {
                        ///<switch>current profile</switch>
                        iniFile.IniPath = Application.StartupPath + @"\locations.ini";
                        iniFile.IniPath = Application.StartupPath + iniFile.ReadValue("PROFILES", "0");

                        // Check the existence of the document
                        exeXml.CheckExistence("Level of Detail Radius Editor", true, Application.ExecutablePath);
                    }
                }

                #endregion

                #region Run the LODRE when you log onto this user account

                // The full path to the application shortcut
                string shortcutPath = Environment.GetFolderPath(Environment.SpecialFolder.Startup) + @"\Level of Detail Radius Editor.lnk";

                if (optionsGeneral1.Checked)
                {
                    iniFile.WriteValue("OPTIONS.GENERAL", "runwin", "1");

                    if (!System.IO.File.Exists(shortcutPath))
                    {
                        // Create a new shortcut
                        WshShellClass wshShell = new WshShellClass();
                        IWshRuntimeLibrary.IWshShortcut appShortcut;
                        appShortcut = (IWshRuntimeLibrary.IWshShortcut)wshShell.CreateShortcut(shortcutPath);
                        appShortcut.TargetPath = Application.ExecutablePath;
                        appShortcut.Description = "Level of Detail Radius Editor";
                        appShortcut.Save();
                    }
                }
                else
                {
                    iniFile.WriteValue("OPTIONS.GENERAL", "runwin", "0");

                    if (System.IO.File.Exists(shortcutPath))
                    {
                        // Delete the shortcut
                        System.IO.File.Delete(shortcutPath);
                    }
                }

                // Null the path string
                shortcutPath = null;

                #endregion

                #region Start this application minimized:

                if (optionsGeneral2.Checked)
                    iniFile.WriteValue("OPTIONS.GENERAL", "minimize", "1");
                else
                    iniFile.WriteValue("OPTIONS.GENERAL", "minimize", "0");

                #endregion

                #region When started with FSX || Always

                if (optionsGeneral30.Checked)
                    iniFile.WriteValue("OPTIONS.GENERAL", "sub", "0");
                else
                    iniFile.WriteValue("OPTIONS.GENERAL", "sub", "1");

                #endregion

                #endregion

                #region Auto Change

                #region Auto Change enabled

                if (optionsAuto0.Checked)
                {
                    iniFile.WriteValue("OPTIONS.AUTO", "auto", "1");
                }
                else
                {
                    iniFile.WriteValue("OPTIONS.AUTO", "auto", "0");
                }

                #endregion

                #region Auto change radius

                try
                {
                    string[] selectedAutoRes = optionsAuto1.SelectedItem.ToString().Split(' ');
                    iniFile.WriteValue("OPTIONS.AUTO", "radius", selectedAutoRes[0]);
                }
                catch
                {
                    MessageBox.Show("Unable to set the Auto Change radius selected. An item was either not selected or in the wrong format.",
                        "Unable To Save Auto Change Settings", MessageBoxButtons.OK, MessageBoxIcon.Error);

                    return;
                }

                #endregion

                #region Application to run after change

                iniFile.WriteValue("OPTIONS.AUTO", "application", optionsAuto2.Text.ToString());

                #endregion

                #region Exit after radius change

                if (optionsAuto3.Checked)
                {
                    iniFile.WriteValue("OPTIONS.AUTO", "close", "1");
                }
                else
                {
                    iniFile.WriteValue("OPTIONS.AUTO", "close", "0");
                }

                #endregion

                #endregion

                #region Resolutions

                string trimmedItem;
                int counter = 0;

                while (iniFile.ReadValue("RADII", counter.ToString()) != "")
                {
                    iniFile.WriteValue("RADII", counter.ToString(), "");
                    counter++;
                }

                counter = 0;

                foreach (string item in optionsRadiusR.Items)
                {
                    trimmedItem = item;
                    trimmedItem = trimmedItem.Replace(" (", ",");
                    trimmedItem = trimmedItem.TrimEnd(')');

                    iniFile.WriteValue("RADII", counter.ToString(), trimmedItem);

                    counter++;
                }

                #endregion
            }

            #endregion

            // Dispose of the form
            this.Dispose();
        }
예제 #2
0
파일: Main.cs 프로젝트: StevenFrost/Editors
        /// <summary>
        /// Form Constructor
        /// </summary>
        public Main()
        {
            // Initialize the form
            InitializeComponent();

            // Assign the icon
            Icon = global::LODRadiusEditor.Properties.Resources.icon;

            #region Check for existing instanstance

            using (Initialization.InitMain initialize = new Initialization.InitMain())
            {
                // Check for an existing application instance
                if (!initialize.IsSingleInstance(Text))
                {
                    // Show a message informing the user that there's already an instance running
                    MessageBox.Show("Another instance of this application is already running.",
                        "Multiple application instances", MessageBoxButtons.OK, MessageBoxIcon.Exclamation);

                    // Require an exit after initialization
                    requireExit = true;
                }
            }

            #endregion

            #region Check if FSX is running

            using (Initialization.InitMain initialize = new Initialization.InitMain())
            {
                if (initialize.IsProcessOpen("fsx"))
                {
                    // Flight Simulator is running
                    fsxIsRunning = true;
                }
            }

            #endregion

            #region Integrity Checks

            #region Check File Existence and Hash

            using (Integrity.Hash hash = new Integrity.Hash())
            {
                if (File.Exists(Application.StartupPath + @"\Resources\core.resource"))
                {
                    if (hash.Match(Application.StartupPath + @"\Resources\core.resource", "ED8A4A25A3F6366F484F32B06DE084D59FE2B4D6"))
                    {
                        // Get the file lines one by one
                        string[] lines = File.ReadAllLines(Application.StartupPath + @"\Resources\core.resource");

                        for (int i = 0; i < lines.Length; i++)
                        {
                            // Split the line
                            string[] splitLine = lines[i].Split(';');

                            if (File.Exists(Application.StartupPath + splitLine[0]))
                            {
                                if (!hash.Match(Application.StartupPath + splitLine[0], splitLine[1]))
                                {
                                    // Show an error
                                    MessageBox.Show("The file " + splitLine[0] + " has been modified or is corrupt. Please reinstall the Level of Detail Radius Editor.",
                                        "Integrity Check", MessageBoxButtons.OK, MessageBoxIcon.Error);

                                    // Require an exit
                                    requireExit = true;
                                }
                            }
                            else
                            {
                                // Show an error
                                MessageBox.Show("The file " + splitLine[0] + " could not be found. Please reinstall the Level of Detail Radius Editor.",
                                    "Integrity Check", MessageBoxButtons.OK, MessageBoxIcon.Error);

                                // Require an exit
                                requireExit = true;
                            }
                        }
                    }
                    else
                    {
                        // Show an error
                        MessageBox.Show("The file core.resource has been modified or is corrupt. Please reinstall the Level of Detail Radius Editor.",
                            "Integrity Check", MessageBoxButtons.OK, MessageBoxIcon.Error);

                        // Require an exit
                        requireExit = true;
                    }
                }
                else
                {
                    // Show an error
                    MessageBox.Show("The file core.resource could not be found. Please reinstall the Level of Detail Radius Editor.",
                        "Integrity Check", MessageBoxButtons.OK, MessageBoxIcon.Error);

                    // Require an exit
                    requireExit = true;
                }
            }

            #endregion

            #region Check INI Files

            using (IniFile iniFile = new IniFile())
            {
                /// <switch>current profile</switch>
                iniFile.IniPath = Application.StartupPath + @"\locations.ini";

                if (!File.Exists(Application.StartupPath + @"\locations.ini"))
                {
                    // Copy the locations resource
                    File.Copy(Application.StartupPath + @"\Resources\locations.resource",
                        Application.StartupPath + @"\locations.ini", false);
                }

                if (!File.Exists(Application.StartupPath + iniFile.ReadValue("PROFILES", "0")))
                {
                    try
                    {
                        if (!Directory.Exists(Application.StartupPath + @"\Profiles\"))
                        {
                            Directory.CreateDirectory(Application.StartupPath + @"\Profiles\");
                        }

                        if (!File.Exists(Application.StartupPath + @"\Profiles\default.ini"))
                        {
                            // Copy the resource
                            File.Copy(Application.StartupPath + @"\Resources\profile.resource",
                                Application.StartupPath + @"\Profiles\default.ini", false);
                        }

                        // Write the new path value
                        iniFile.WriteValue("PROFILES", "0", @"\Profiles\default.ini");

                        // Success message
                        MessageBox.Show("The default profile specified in 'locations.ini' could not be found. A new one has been automatically generated.",
                            "Integrity Check", MessageBoxButtons.OK, MessageBoxIcon.Error);
                    }
                    catch
                    {
                        // Error message
                        MessageBox.Show("The default profile specified in 'locations.ini' could not be found and a new profile could not be generated. Please reinstall the Level of Detail Radius Editor.",
                            "Integrity Check", MessageBoxButtons.OK, MessageBoxIcon.Error);
                    }
                }
            }

            #endregion

            #region Check INI Variables

            using (IniFile iniFile = new IniFile())
            {
                using (Integrity.Existence existence = new Integrity.Existence())
                {
                    /// <switch>current profile</switch>
                    iniFile.IniPath = Application.StartupPath + @"\locations.ini";

                    if (!Directory.Exists(iniFile.ReadValue("LOCATIONS", "exe")) &&
                        !Directory.Exists(iniFile.ReadValue("LOCATIONS", "cfg")) &&
                        !Directory.Exists(iniFile.ReadValue("LOCATIONS", "self")))
                    {
                        // Require a locations reset
                        iniFile.WriteValue("GENERAL", "reset", "1");
                    }
                }
            }

            #endregion

            #endregion

            #region Check For Reset Switch

            using (IniFile iniFile = new IniFile())
            {
                /// <switch>locations.ini</switch>
                iniFile.IniPath = Application.StartupPath + @"\locations.ini";

                if (iniFile.ReadValue("GENERAL", "reset") == "1")
                {
                    using (Locations.FSX fsxLocations = new Locations.FSX())
                    {
                        // Reset the location variables
                        iniFile.WriteValue("LOCATIONS", "exe", fsxLocations.GetExe());
                        iniFile.WriteValue("LOCATIONS", "cfg", fsxLocations.GetCfg());
                        iniFile.WriteValue("LOCATIONS", "self", Application.StartupPath);
                    }

                    try
                    {
                        if (File.Exists(iniFile.ReadValue("LOCATIONS", "cfg") + @"\exe.xml"))
                        {
                            string dateTime = DateTime.Now.ToString();
                            dateTime = dateTime.Replace("/", "");
                            dateTime = dateTime.Replace(" ", "_");
                            dateTime = dateTime.Replace(":", "");

                            if (!Directory.Exists(Application.StartupPath + @"\Backups\"))
                            {
                                Directory.CreateDirectory(Application.StartupPath + @"\Backups\");
                            }

                            // Make a copy of exe.xml
                            File.Copy(iniFile.ReadValue("LOCATIONS", "cfg") + @"\exe.xml", Application.StartupPath + @"\Backups\exe_xml_backup_" + dateTime + ".bak");
                        }
                    }
                    catch
                    {
                        // Show an error
                        MessageBox.Show("Unable to make a copy of exe.xml. The file has not been backed up.",
                            "Unable to backup exe.xml", MessageBoxButtons.OK, MessageBoxIcon.Error);
                    }

                    // Set the reset value back to 0
                    iniFile.WriteValue("GENERAL", "reset", "0");
                }
            }

            #endregion

            #region Set Main Radius

            using (Initialization.Radii radii = new Initialization.Radii())
            {
                // Set the radius list items
                radii.FillField(homeRadius, null);
            }

            #endregion

            #region Switch Main Radius To Current

            using (Initialization.Radii radii = new Initialization.Radii())
            {
                // Select the current radius in fsx.cfg
                radii.SelectCurrentRadius(homeRadius, null);

                // Set the current value
                string[] selectedItem = homeRadius.SelectedItem.ToString().Split(' ');
                currentValue = selectedItem[0];
                selectedItem = null;
            }

            #endregion

            #region Set Location Fields

            using (IniFile iniFile = new IniFile())
            {
                /// <switch>locations.ini</switch>
                iniFile.IniPath = Application.StartupPath + @"\locations.ini";

                // Set the location fields
                homeExePath.Text = iniFile.ReadValue("LOCATIONS", "exe");
                homeCfgPath.Text = iniFile.ReadValue("LOCATIONS", "cfg");
            }

            #endregion

            #region Check for exe.xml existence and entry

            using (IniFile iniFile = new IniFile())
            {
                ///<switch>locations.ini</switch>
                iniFile.IniPath = Application.StartupPath + @"\locations.ini";

                using (ExeXml exeXml = new ExeXml(iniFile.ReadValue("LOCATIONS", "cfg") + @"\exe.xml"))
                {
                    ///<switch>current profile</switch>
                    iniFile.IniPath = Application.StartupPath + iniFile.ReadValue("PROFILES", "0");

                    if (iniFile.ReadValue("OPTIONS.GENERAL", "runfsx") == "1")
                    {
                        // Check the existence of the document
                        exeXml.CheckExistence("Level of Detail Radius Editor", false, Application.ExecutablePath);
                    }
                    else
                    {
                        // Check the existence of the document
                        exeXml.CheckExistence("Level of Detail Radius Editor", true, Application.ExecutablePath);
                    }
                }
            }

            #endregion

            #region Minimize the Form

            using (IniFile iniFile = new IniFile())
            {
                /// <switch>current profile</switch>
                iniFile.IniPath = Application.StartupPath + @"\locations.ini";
                iniFile.IniPath = Application.StartupPath + iniFile.ReadValue("PROFILES", "0");

                if (iniFile.ReadValue("OPTIONS.GENERAL", "minimize") == "1")
                {
                    if (iniFile.ReadValue("OPTIONS.GENERAL", "sub") == "0")
                    {
                        if (fsxIsRunning)
                            this.WindowState = FormWindowState.Minimized;
                    }
                    else
                        this.WindowState = FormWindowState.Minimized;
                }
            }

            #endregion

            #region Auto Change

            using (IniFile iniFile = new IniFile())
            {
                /// <switch>current profile</switch>
                iniFile.IniPath = Application.StartupPath + @"\locations.ini";
                iniFile.IniPath = Application.StartupPath + iniFile.ReadValue("PROFILES", "0");

                if (iniFile.ReadValue("OPTIONS.AUTO", "auto") == "1")
                {
                    // Get the auto radius
                    string autoResolution = iniFile.ReadValue("OPTIONS.AUTO", "radius");

                    if (autoResolution != "")
                    {
                        /// <switch>fsx.cfg</switch>
                        iniFile.IniPath = Application.StartupPath + @"\locations.ini";
                        iniFile.IniPath = iniFile.ReadValue("LOCATIONS", "cfg") + @"\fsx.cfg";

                        using (Format format = new Format())
                        {
                            // Set the Level of Detail Radius value in fsx.cfg
                            iniFile.WriteValue("TERRAIN", "LOD_RADIUS", format.Radius(autoResolution));
                        }

                        /// <switch>current profile</switch>
                        iniFile.IniPath = Application.StartupPath + @"\locations.ini";
                        iniFile.IniPath = Application.StartupPath + iniFile.ReadValue("PROFILES", "0");
                    }
                    else
                    {
                        // Show an error informing the user that the LOD Radius value has not been set
                        MessageBox.Show("Unable to set a null radius. The Level of Detail Radius value has not been changed in fsx.cfg.",
                            "Null radius", MessageBoxButtons.OK, MessageBoxIcon.Error);
                    }

                    if (iniFile.ReadValue("OPTIONS.AUTO", "application") != "")
                    {
                        try
                        {
                            // Try to start the specified process
                            System.Diagnostics.Process.Start(iniFile.ReadValue("OPTIONS.AUTO", "application"));
                        }
                        catch
                        {
                            // Show an error informing the user that their application could not be started
                            MessageBox.Show("Unable to run the post auto change application you specified.",
                                "External process error", MessageBoxButtons.OK, MessageBoxIcon.Error);
                        }
                    }

                    if (iniFile.ReadValue("OPTIONS.AUTO", "close") == "1")
                    {
                        // Set the seconds for the timer
                        autoSeconds = Int32.Parse(iniFile.ReadValue("MISCELLANEOUS", "autotime"));

                        // Require an exit
                        autoCounter = 0;
                        autoChangeExit = true;
                    }
                    else
                    {
                        // Make the auto counter negative
                        autoCounter = -1;
                    }
                }
            }

            #endregion

            #region Update switch check

            using (IniFile iniFile = new IniFile())
            {
                /// <switch>current profile</switch>
                iniFile.IniPath = Application.StartupPath + @"\locations.ini";
                iniFile.IniPath = Application.StartupPath + iniFile.ReadValue("PROFILES", "0");
            }

            #endregion
        }