Beispiel #1
0
 private void Remove_file_from_list_of_managed_files()
 {
     if (SelectedFileToManage != null)
     {
         FilesToBeManaged.Remove(SelectedFileToManage);
     }
 }
Beispiel #2
0
        private void Add_file_to_list_of_managed_files()
        {
            try
            {
                Microsoft.Win32.OpenFileDialog dlg = new Microsoft.Win32.OpenFileDialog();
                dlg.InitialDirectory = SelectedGame.Save_file_location;

                Nullable <bool> result = dlg.ShowDialog();

                if (result == true)
                {
                    List <string> selected_filenames = dlg.FileNames.ToList <string>();

                    foreach (string selected_filename in selected_filenames)
                    {
                        FileInfo finfo = new FileInfo(selected_filename);

                        if (!FilesToBeManaged.Contains(selected_filename) && string.Equals(finfo.DirectoryName, SelectedGame.Save_file_location))
                        {
                            FilesToBeManaged.Add(finfo.Name);
                        }
                    }
                }
            }
            catch (Exception)
            {
                MessageBox.Show("Failed to add file names into list.", "Error", MessageBoxButton.OK, MessageBoxImage.Error);
                FilesToBeManaged.Clear();
            }
        }
Beispiel #3
0
        private void Save_changes_to_selected_game()
        {
            //Make changes to game selected from list
            try
            {
                if (File.Exists(applicationDataFilePath))
                {
                    //double stream to one file might cause unpredictable behavior. Close filestream as soon as you load data.

                    XDocument xdoc = new XDocument();//XDocument.Load(applicationDataFilePath, LoadOptions.SetBaseUri);
                    bool      prompt_save_file_migration = false;

                    using (FileStream fs = new FileStream(applicationDataFilePath /*xdoc.BaseUri*/, FileMode.Open, FileAccess.Read))
                    {
                        xdoc = XDocument.Load(fs);
                    }

                    Game     temp_game_item = tempSelectedGameData;
                    XElement query          = (from item in xdoc.Element("Game_save_file_manager").Element("Games").Elements("Game")
                                               where (string)item.Element("Name").Value == temp_game_item.Game_name //create field containing temporary game name or use Game fields' values
                                               select item).First();
                    //Same as above, but with lambda expression
                    //XElement elem1 = xdoc.Element("Game_save_file_manager").Element("Games").Elements("Game").Where(item => (string)item.Element("Name").Value == temp_game_item.Game_name).Select(item => item).First();

                    query.Element("Name").Value = SelectedGame.Game_name;

                    //update save file location element if the value was changed
                    if ((query.Element("Save_file_location").Value != SelectedGame.Save_file_location) && Directory.Exists(SelectedGame.Save_file_location))
                    {
                        query.Element("Save_file_location").Value = SelectedGame.Save_file_location;
                        prompt_save_file_migration = true;
                    }
                    else if (!Directory.Exists(SelectedGame.Save_file_location))
                    {
                        throw new DirectoryNotFoundException();
                    }


                    bool node_exists = query.Elements("Manage_selected_files_only").Any();
                    if (node_exists == false)
                    {
                        query.Add(new XElement("Manage_selected_files_only", SelectedGame.ManageSelectedFilesOnly.ToString()));
                    }
                    else
                    {
                        query.Element("Manage_selected_files_only").SetValue(SelectedGame.ManageSelectedFilesOnly.ToString());
                    }

                    //add files to list of managed files
                    if (SelectedGame.ManageSelectedFilesOnly == true && !temp_game_item.FilesToManage.Equals(FilesToBeManaged.ToList <string>()))
                    {
                        List <string> new_filenames_list = FilesToBeManaged.ToList <string>();
                        //var subquery = from item in query.Element("Files_to_manage").Elements("Files") select item;
                        if (query.Elements("Files_to_manage").Any())
                        {
                            query.Element("Files_to_manage").Elements("File").Remove();
                        }
                        else
                        {
                            query.Add(new XElement("Files_to_manage"));
                        }

                        foreach (string filename in new_filenames_list)
                        {
                            query.Element("Files_to_manage").Add(new XElement("File", filename));
                        }
                    }

                    //if save file storage method had been changed, game's element will be updated and prompt will be displayed to the user, asking if manager should move files to a new directory
                    if (!(query.Element("Store_profile_saves_in_app_location").Value == SelectedGame.Profile_specific_save_file_storage_method.ToString()))
                    {
                        query.Element("Store_profile_saves_in_app_location").Value = SelectedGame.Profile_specific_save_file_storage_method.ToString();
                        prompt_save_file_migration = true;
                    }

                    if (prompt_save_file_migration)
                    {
                        //prompt
                        MessageBoxResult boxresult1 = MessageBox.Show("Game save file storage method or game save file directory has been changed. Would you like to migrate save files to a new directory?", "Attention", MessageBoxButton.YesNo, MessageBoxImage.Question);

                        if (boxresult1 == MessageBoxResult.Yes)
                        {
                            MigrateSaveFilesToNewDirectory(temp_game_item.Game_name, temp_game_item.Profile_specific_save_file_storage_method, SelectedGame.Profile_specific_save_file_storage_method, temp_game_item.Save_file_location, SelectedGame.Save_file_location);
                        }
                    }

                    xdoc.Save(applicationDataFilePath);
                    //End edit mode
                    Deactivate_Edit_Mode();
                }
                else
                {
                    //Warning
                }
            }
            catch (DirectoryNotFoundException)
            {
                MessageBox.Show("Specified save files' directory has not been found or is incorrect.", "Error");
            }
            catch (Exception ex)
            {
                MessageBox.Show("Failed to save games' list correctly. " + ex.Message);
                //End edit mode
                Deactivate_Edit_Mode();
            }
            //Load_Games_List();
            //Load_Selected_Games_Data(SelectedGame);
        }
Beispiel #4
0
        //private void Load_Games_List() //this method should be availabale in the main window
        //{
        //    //games_listbox.SelectedItem = null;
        //    Games.Clear(); //games_listbox.Items.Clear();
        //    try
        //    {
        //        //Try to load file in xml extension, with list of games
        //        if (File.Exists(AppDomain.CurrentDomain.BaseDirectory + "games_list.xml"))
        //        {
        //            XDocument xdoc = XDocument.Load(AppDomain.CurrentDomain.BaseDirectory + "games_list.xml");
        //            //FileStream fs = new FileStream(xdoc.BaseUri, FileMode.Open, FileAccess.Read);

        //            //reminder: games_list.xml construction: Games_list->Games->Game->Id|Name|Save_file_location|Store_profile_saves_in_app_location|
        //            var query = from item in xdoc.Element("Game_save_file_manager").Element("Games").Elements("Game")
        //                        select item;

        //            foreach (var item in query) //adding games to list
        //            {
        //                Game gi_ob = new Game();
        //                gi_ob.Game_name = item.Element("Name").Value;/*(from temp_it in item.Descendants()
        //                                  select temp_it.Element("Name")).First().ToString();*/

        //                gi_ob.Save_file_location = item.Element("Save_file_location").Value;/*(from temp_it in item.Descendants()
        //                                           select temp_it.Element("Save_file_location")).First().ToString();*/

        //                string temp_profile_specific_file_storage_method;
        //                temp_profile_specific_file_storage_method = item.Element("Store_profile_saves_in_app_location").Value; /*(from temp_it in item.Descendants()
        //                                           select temp_it.Element("Store_profile_saves_in_app_location")).First().ToString();*/

        //                gi_ob.Profile_specific_save_file_storage_method = Convert.ToInt32(temp_profile_specific_file_storage_method);

        //                //games_listbox.Items.Add(gi_ob);
        //                Games.Add(gi_ob);
        //            }
        //        }
        //        else
        //        {
        //            FileNotFoundMethod(applicationDataFilePath);
        //        }
        //    }
        //    catch (Exception ex)
        //    {
        //        MessageBox.Show("Failed to load games' list. " + ex.Message);
        //    }
        //}

        private void LoadApplicationData(string path)
        {
            Games.Clear(); //games_listbox.Items.Clear();
            FilesToBeManaged.Clear();

            try
            {
                applicationDataFilePath = path;
                //Try to load file in xml extension, with list of games
                if (File.Exists(applicationDataFilePath))
                {
                    XDocument xdoc = XDocument.Load(applicationDataFilePath);
                    //FileStream fs = new FileStream(xdoc.BaseUri, FileMode.Open, FileAccess.Read);

                    //reminder: games_list.xml construction: Games_list->Games->Game->Id|Name|Save_file_location|Store_profile_saves_in_app_location|
                    var query = from item in xdoc.Element("Game_save_file_manager").Element("Games").Elements("Game")
                                select item;

                    foreach (var item in query) //adding games to list
                    {
                        Game gi_ob = new Game();
                        gi_ob.Game_name = item.Element("Name").Value;                        /*(from temp_it in item.Descendants()
                                                                                              * select temp_it.Element("Name")).First().ToString();*/

                        gi_ob.Save_file_location = item.Element("Save_file_location").Value; /*(from temp_it in item.Descendants()
                                                                                              * select temp_it.Element("Save_file_location")).First().ToString();*/

                        string temp_profile_specific_file_storage_method;
                        temp_profile_specific_file_storage_method = item.Element("Store_profile_saves_in_app_location").Value; /*(from temp_it in item.Descendants()
                                                                                                                                * select temp_it.Element("Store_profile_saves_in_app_location")).First().ToString();*/

                        gi_ob.Profile_specific_save_file_storage_method = Convert.ToInt32(temp_profile_specific_file_storage_method);

                        bool node_exists = item.Elements("Manage_selected_files_only").Any();
                        if (node_exists == false)
                        {
                            gi_ob.ManageSelectedFilesOnly = false;
                        }
                        else
                        {
                            gi_ob.ManageSelectedFilesOnly = Convert.ToBoolean(item.Element("Manage_selected_files_only").Value);
                        }

                        if (item.Elements("Files_to_manage").Any())//.Elements("File").Any())
                        {
                            if (item.Element("Files_to_manage").Elements("File").Any())
                            {
                                foreach (string filename in item.Element("Files_to_manage").Elements("File"))
                                {
                                    gi_ob.FilesToManage.Add(filename);
                                }
                            }
                        }
                        //games_listbox.Items.Add(gi_ob);
                        Games.Add(gi_ob);
                    }
                    Mediator.Instance.NotifyColleagues(Mediator.ViewModelMessages.GameListUpdated, games);
                }
                else
                {
                    //create new "games_list.xml" file
                    FileNotFoundMethod(applicationDataFilePath);
                }
            }
            catch (Exception ex)
            {
                MessageBox.Show("Failed to load games' list. " + ex.Message);
            }
        }