Exemple #1
0
        /// <summary>
        /// Write out the current state of the director and all of its scenes.
        /// </summary>
        public void SerializeState()
        {
            // open up isolated storage
            using (IsolatedStorageFile storage = IsolatedStorageFile.GetUserStoreForApplication())
            {
                // if our screen manager directory already exists, delete the contents
                if (storage.DirectoryExists(m_sStorageDirName))
                {
                    DeleteState(storage);
                }

                // otherwise just create the directory
                else
                {
                    storage.CreateDirectory(m_sStorageDirName);
                }

                // create a file we'll use to store the list of screens in the stack

                CCLog.Log("Saving CCDirector state to file: " + Path.Combine(m_sStorageDirName, m_sSaveFileName));

                try
                {
                    using (IsolatedStorageFileStream stream = storage.OpenFile(Path.Combine(m_sStorageDirName, m_sSaveFileName), FileMode.OpenOrCreate))
                    {
                        using (StreamWriter writer = new StreamWriter(stream))
                        {
                            // write out the full name of all the types in our stack so we can
                            // recreate them if needed.
                            foreach (CCScene scene in m_pobScenesStack)
                            {
                                if (scene.IsSerializable)
                                {
                                    writer.WriteLine(scene.GetType().AssemblyQualifiedName);
                                }
                                else
                                {
                                    CCLog.Log("Scene is not serializable: " + scene.GetType().FullName);
                                }
                            }
                            // Write out our local state
                            if (m_pRunningScene != null && m_pRunningScene.IsSerializable)
                            {
                                writer.WriteLine("m_pRunningScene");
                                writer.WriteLine(m_pRunningScene.GetType().AssemblyQualifiedName);
                            }
                            // Add my own state
                            // [*]name=value
                            //
                        }
                    }

                    // now we create a new file stream for each screen so it can save its state
                    // if it needs to. we name each file "ScreenX.dat" where X is the index of
                    // the screen in the stack, to ensure the files are uniquely named
                    int    screenIndex = 0;
                    string fileName    = null;
                    foreach (CCScene scene in m_pobScenesStack)
                    {
                        if (scene.IsSerializable)
                        {
                            fileName = string.Format(Path.Combine(m_sStorageDirName, m_sSceneSaveFileName), screenIndex);

                            // open up the stream and let the screen serialize whatever state it wants
                            using (IsolatedStorageFileStream stream = storage.CreateFile(fileName))
                            {
                                scene.Serialize(stream);
                            }

                            screenIndex++;
                        }
                    }
                    // Write the current running scene
                    if (m_pRunningScene != null && m_pRunningScene.IsSerializable)
                    {
                        fileName = string.Format(Path.Combine(m_sStorageDirName, m_sSceneSaveFileName), "XX");
                        // open up the stream and let the screen serialize whatever state it wants
                        using (IsolatedStorageFileStream stream = storage.CreateFile(fileName))
                        {
                            m_pRunningScene.Serialize(stream);
                        }
                    }
                }
                catch (Exception ex)
                {
                    CCLog.Log("Failed to serialize the CCDirector state. Erasing the save files.");
                    CCLog.Log(ex.ToString());
                    DeleteState(storage);
                }
            }
        }