Example #1
0
        public XMLGamesConfig(Stream Stream = null)
        {
            XElement Config;

            if (Stream == null)
            {
                Stream = new FileStream(ConfigFile, FileMode.Open);
            }
            using (Stream)
                Config = XElement.Load(Stream);
            Games = (from GameXML in Config.Descendants(AGameNode)
                     select new ConfigGameModel()
            {
                GUID = GameXML.Attribute(IDAttribute)?.Value,
                Name = GameXML.Attribute(NameAttribute)?.Value,
                Path = GameXML.Attribute(PathAttribute)?.Value,
                ImagePath = GameXML.Attribute(ImagePathAttribute)?.Value,
                PlayTime = ulongParseOrNull(GameXML.Attribute(PlayTimeAttribute)?.Value),
                CostToPlay = IntParseOrNull(GameXML.Attribute(CostToPlayAttribute)?.Value),
                StartOptions = GameXML.Attribute(StartOptionsAttribute)?.Value,
                IOptionalAddtionalExeStarts = (from ExeXML in GameXML.Descendants(AddtionalExeStartNode)
                                               select new ConfigOptionalAddtionalExeStartsModel()
                {
                    Path = ExeXML.Attribute(PathAttribute)?.Value,
                    Delay = IntParseOrNull(ExeXML.Attribute(DelayAttribute)?.Value)
                }).Cast <IOptionalAddtionalExeStartsModel>().ToList()
            }).Cast <IGameModel>().ToList();

            //a trick to quickly convert to json when making the json config class test subject. Not really relavat to the code however.
            //string Result = JsonConvert.SerializeObject(Games);

            if (Games.Any(x => x.GUID == null || x.Path == null || x.PlayTime == null || x.IOptionalAddtionalExeStarts.Any(j => j.Path == null || j.Delay == null)))
            {
                throw new Exception(NotSetError);
            }
        }
Example #2
0
        /// <summary>
        /// Form Constructor
        /// </summary>
        public Main()
        {
            InitializeComponent();
            Icon = global::TextureMaxLoadEditor.Properties.Resources.icon;

            #region Check for existing instanstance

            using (InitMain initialize = new InitMain())
            {
                if (!initialize.IsSingleInstance(Text))
                {
                    MessageBox.Show("Another instance of this application is already running.",
                        "Multiple application instances", MessageBoxButtons.OK, MessageBoxIcon.Exclamation);
                    requireExit = true;
                }
            }

            #endregion

            #region Check if FSX is running

            using (InitMain initialize = new InitMain())
            {
                if (initialize.IsProcessOpen("fsx"))
                {
                    fsxIsRunning = true;
                }
            }

            #endregion

            #region Integrity Checks

            #region Check File Existence and Hash

            using (Hash hash = new Hash())
            {
                if (File.Exists(Application.StartupPath + @"\Resources\core.resource"))
                {
                    if (hash.Match(Application.StartupPath + @"\Resources\core.resource", "DDAE1DD174117AC9ABADCBA70323590F77F67513"))
                    {
                        // 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 Texture Max Load 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 Texture Max Load 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 Texture Max Load 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 Texture Max Load 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 Texture Max Load Editor.",
                            "Integrity Check", MessageBoxButtons.OK, MessageBoxIcon.Error);
                    }
                }
            }

            #endregion

            #region Check INI Variables

            using (IniFile iniFile = new IniFile())
            {
                using (Existence existence = new 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 Resolutions

            using (Resolutions resolutions = new Resolutions())
            {
                // Set the resolution list items
                resolutions.FillField(homeResolution, 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 Switch Main Resolution To Current

            using (Resolutions resolutions = new Resolutions())
            {
                // Select the current resolution in fsx.cfg
                resolutions.SelectCurrentResolution(homeResolution, null);

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

            #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("Texture Max Load Editor", false, Application.ExecutablePath);
                    }
                    else
                    {
                        // Check the existence of the document
                        exeXml.CheckExistence("Texture Max Load 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 resolution
                    string autoResolution = iniFile.ReadValue("OPTIONS.AUTO", "resolution");

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

                        // Set the Texture Max Load Value in fsx.cfg
                        iniFile.WriteValue("GRAPHICS", "TEXTURE_MAX_LOAD", 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 TML value has not been set
                        MessageBox.Show("Unable to set a null resolution. The Texture Max Load value has not been changed in fsx.cfg.",
                            "Null resolution", 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
        }
Example #3
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 resolution you were editing. Are you sure you want to continue?",
                    "Resolution 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 Texture Max Load Editor 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("Texture Max Load 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("Texture Max Load Editor", true, Application.ExecutablePath);
                    }
                }

                #endregion

                #region Run the Texture Max Load Editor when you log onto this user account

                // The full path to the application shortcut
                string shortcutPath = Environment.GetFolderPath(Environment.SpecialFolder.Startup) + @"\Texture Max Load Editor.lnk";

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

                    if (!System.IO.File.Exists(shortcutPath))
                    {
                        WshShellClass wshShell = new WshShellClass();
                        IWshRuntimeLibrary.IWshShortcut appShortcut;
                        appShortcut = (IWshRuntimeLibrary.IWshShortcut)wshShell.CreateShortcut(shortcutPath);
                        appShortcut.TargetPath = Application.ExecutablePath;
                        appShortcut.Description = "Texture Max Load Editor";
                        appShortcut.Save();
                    }
                }
                else
                {
                    iniFile.WriteValue("OPTIONS.GENERAL", "runwin", "0");

                    if (System.IO.File.Exists(shortcutPath))
                    {
                        System.IO.File.Delete(shortcutPath);
                    }
                }
                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 resolution

                try
                {
                    string[] selectedAutoRes = optionsAuto1.SelectedItem.ToString().Split(' ');
                    iniFile.WriteValue("OPTIONS.AUTO", "resolution", selectedAutoRes[0]);
                }
                catch
                {
                    MessageBox.Show("Unable to set the Auto Change resolution 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 resolution 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("RESOLUTIONS", counter.ToString()) != "")
                {
                    iniFile.WriteValue("RESOLUTIONS", counter.ToString(), "");
                    counter++;
                }

                counter = 0;

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

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

                    counter++;
                }

                #endregion

                #region Updates

                if (optionsUpdates1.Checked)
                {
                    iniFile.WriteValue("OPTIONS.UPDATE", "autocheck", "1");
                }
                else
                {
                    iniFile.WriteValue("OPTIONS.UPDATE", "autocheck", "0");
                }

                #endregion
            }

            #endregion

            // Dispose of the form
            this.Dispose();
        }