Пример #1
0
        //=========================================================================================
        /// <summary>
        /// Attempts to save user preferences to the preferences xml file
        /// </summary>        
        //=========================================================================================
        public static void SavePreferences()
        {
            // This might fail:

            try
            {
                // Get the current storage container:

                StorageContainer container = StorageSystem.Container;

                // Only do if we have a storage device:

                if ( container != null )
                {
                    // Try and get the preferences file:

                    FileInfo file = new FileInfo( container.Path + "\\" + PREFERENCES_FILE );

                    // Create a new xml data object:

                    XmlObjectData data = new XmlObjectData();

                    // Ok: write all attributes

                    data.Write( "SoundVolume"   , s_sound_volume  );
                    data.Write( "MusicVolume"   , s_music_volume  );
                    data.Write( "Brightness"    , s_brightness    );

                    // Open an output stream:

                    Stream output_stream = file.Open(FileMode.Create,FileAccess.Write);

                    // Now save the data:

                    try
                    {
                        // Save:

                        data.Save( output_stream );
                    }

                    // Show what happened on windows debug if something went wrong

                    #if WINDOWS_DEBUG

                        catch ( Exception e ){ DebugConsole.PrintException(e); }

                    #else

                        catch ( Exception ){}

                    #endif

                    // Close the output stream

                    output_stream.Close();
                }

            }

            // In windows debug show what happened if something went wrong:

            #if WINDOWS_DEBUG

                catch ( Exception e ){ DebugConsole.PrintException(e); }

            #else

                catch ( Exception ){}

            #endif
        }
Пример #2
0
        //#########################################################################################
        /// <summary>
        /// Saves the high scores for each level in the game.
        /// </summary>
        /// <param name="folder"> 
        /// Folder name containing the xml files holding the high scores. Each xml file corresponds to 
        /// the high scores for one level and is named the same as the level file.
        /// </param>
        //#########################################################################################
        public static void Save( string folder )
        {
            // This might fail:

            try
            {
                // Make sure the folder exists, if not then try and make it:

                DirectoryInfo dir = new DirectoryInfo( folder );

                // Make it if doesn't exist:

                if ( dir.Exists == false ) dir.Create();

                // Run through all the high scores in the list:

                Dictionary<string,HighScoreRecord>.Enumerator e = s_scores.GetEnumerator();

                // Run through the list:

                while ( e.MoveNext() )
                {
                    // Make up the file:

                    FileInfo file = new FileInfo( folder + "\\" + e.Current.Key );

                    // Ok: open a write stream

                    Stream stream = file.Open( FileMode.Create , FileAccess.Write );

                    // Write to the file:

                    try
                    {
                        // Makeup an xml data object:

                        XmlObjectData data = new XmlObjectData();

                        // Write all high scores for this level to it:

                        data.Write( "Score1" , e.Current.Value.Score1 );
                        data.Write( "Score2" , e.Current.Value.Score2 );
                        data.Write( "Score3" , e.Current.Value.Score3 );

                        data.Write( "Name1" , e.Current.Value.Name1 );
                        data.Write( "Name2" , e.Current.Value.Name2 );
                        data.Write( "Name3" , e.Current.Value.Name3 );

                        // Write the data to the stream:

                        data.Save( stream );

                        // Close the stream:

                        stream.Close();
                    }
                    catch ( Exception )
                    {
                        // Close the stream:

                        try { stream.Close(); } catch ( Exception ){}

                        // Try and delete the file, it may be corrupt:

                        try { file.Delete(); } catch ( Exception ){}
                    }
                }
            }

            // On windows debug display errors:

            #if WINDOWS_DEBUG

                catch ( Exception e )
                {
                    // Show what happened:

                    DebugConsole.PrintException(e);

                    // Clear high scores:

                    s_scores.Clear();
                }

            #else

                catch ( Exception )
                {
                    // Clear high scores:

                    s_scores.Clear();
                }

            #endif
        }