Pontus Magnusson
Pontus Magnusson

Reputation: 651

Isolated storage file handling

Im working on a game in XNA.
I am currently writing towards both windows and xbox360.

Is this the correct approach for the following problem?

I want to save a file to the isolated storage on the xbox, that already exists in my content. I have a settings.xml file containing all the settings. It is in my content with properties to not be compiled(marked as Content), and copy if newer, so it can be modified, but still be maintained inside Content.

I have a save and a load function that acts differently for windows & xbox, and currently loading GameSettings object, but will be able to save and load XML files as well.
Load:

public static GameSettings Load(string settingsFilename)
{
    GameSettings settings; // Creates a new instance of GameSettings
#if WINDOWS
    using (Stream stream = File.OpenRead(settingsFilename))// Open and Read-stream from the filename entered
    {
        XmlSerializer serializer = new XmlSerializer(typeof(GameSettings)); // Serializer of type GameSettings
        settings = (GameSettings)serializer.Deserialize(stream); //Deserialize the xml file stream and fill the data into a GameSettings object
    }
#elif XBOX
    using (IsolatedStorageFile isf = IsolatedStorageFile.GetUserStoreForApplication())
    {
        using (IsolatedStorageFileStream stream = new IsolatedStorageFileStream(settingsFilename, FileMode.Open, FileAccess.Read, isf))
        {
            XmlSerializer serializer = new XmlSerializer(typeof(GameSettings));
            settings = (GameSettings)serializer.Deserialize(stream);
        }
    }
#endif
    return settings;
}

Save:

public static void Save(string settingsFileName, GameSettings settings)
{
#if WINDOWS
    using (Stream stream = File.OpenWrite(settingsFileName))// Open and Write-stream from the filname entered
    {
        XmlSerializer serializer = new XmlSerializer(typeof(GameSettings)); // Serializer of type GameSettings
        serializer.Serialize(stream, settings); // serialize the GameSettings data into the stream
    }
#elif XBOX
    using (IsolatedStorageFile isf = IsolatedStorageFile.GetUserStoreForApplication())
    {
        using (IsolatedStorageFileStream stream = new IsolatedStorageFileStream(settingsFileName, FileMode.OpenOrCreate, isf))
        {
            XmlSerializer serializer = new XmlSerializer(typeof(GameSettings));
            serializer.Serialize(stream, settings);
        }
    }
#endif
}

Are there other ways that are less complex or plain smarter with handling default files like settings, that should only be created the first time the game is launched, and then it should be able to be modified during runtime.

Upvotes: 0

Views: 411

Answers (1)

Pontus Magnusson
Pontus Magnusson

Reputation: 651

I save the file from content into isolated storage using the following method: The code is fairly straight forward so I won't comment it.

public static void InitializeFiles()
{
    using (IsolatedStorageFile isf = IsolatedStorageFile.GetUserStoreForApplication())
    {
        if (!isf.FileExists("settings.xml"))
        {
            isf.CreateFile("settings.xml").Dispose();
            XDocument xml;
            using (Stream stream = File.Open("Content/settings.xml", FileMode.Open, FileAccess.Read))
            {
                xml = XDocument.Load(stream);
            }
            using (IsolatedStorageFileStream stream = new IsolatedStorageFileStream("settings.xml", FileMode.Create, isf))
            {
                xml.Save(stream);
            }
        }
    }
}

But I am still unsure if this is a good approach cause it feels clumsy. So I will not mark this answered.

Upvotes: 1

Related Questions