Ejemplo n.º 1
0
    public async Task <LoadDataResult> LoadData()
    {
        if (m_saveProvider == null)
        {
            throw new InvalidOperationException("The save system is not initialized.");
        }

        GameSaveContainer container = m_saveProvider.CreateContainer(c_saveContainerName);

        string[] blobsToRead = new string[] { c_saveBlobName };

        // GetAsync allocates a new Dictionary to hold the retrieved data. You can also use ReadAsync
        // to provide your own preallocated Dictionary.
        GameSaveBlobGetResult result = await container.GetAsync(blobsToRead);

        int loadedData = 0;

        if (result.Status == GameSaveErrorStatus.Ok)
        {
            IBuffer loadedBuffer;
            result.Value.TryGetValue(c_saveBlobName, out loadedBuffer);

            if (loadedBuffer == null)
            {
                throw new Exception(String.Format("Didn't find expected blob \"{0}\" in the loaded data.", c_saveBlobName));
            }

            DataReader reader = DataReader.FromBuffer(loadedBuffer);
            loadedData = reader.ReadInt32();
        }

        return(new LoadDataResult(result.Status, loadedData));
    }
Ejemplo n.º 2
0
        public static async Task <string> Load(string filename)
        {
            string loadedData = null;

            const string c_saveBlobName      = "data";
            string       c_saveContainerName = filename;

            //Now you have a GameSaveProvider
            //Next you need to call CreateContainer to get a GameSaveContainer
            var gameSaveProvider = await GetGameSaveProvider();

            if (gameSaveProvider == null)
            {
                return(null);
            }

            GameSaveContainer gameSaveContainer = gameSaveProvider.CreateContainer(c_saveContainerName);

            //Parameter
            //string name (name of the GameSaveContainer Created)

            //form an array of strings containing the blob names you would like to read.
            string[] blobsToRead = new string[] { c_saveBlobName };

            // GetAsync allocates a new Dictionary to hold the retrieved data. You can also use ReadAsync
            // to provide your own preallocated Dictionary.
            GameSaveBlobGetResult result = await gameSaveContainer.GetAsync(blobsToRead);

            //Check status to make sure data was read from the container
            if (result.Status == GameSaveErrorStatus.Ok)
            {
                //prepare a buffer to receive blob
                IBuffer loadedBuffer;

                //retrieve the named blob from the GetAsync result, place it in loaded buffer.
                result.Value.TryGetValue(c_saveBlobName, out loadedBuffer);

                if (loadedBuffer == null)
                {
                    throw new Exception(String.Format("Didn't find expected blob \"{0}\" in the loaded data.", c_saveBlobName));
                }
                DataReader reader = DataReader.FromBuffer(loadedBuffer);

                uint bytesToRead = reader.ReadUInt32();
                loadedData = reader.ReadString(bytesToRead);

                Debug.WriteLine("XboxLiveConnectedStorage :: Loaded Data - " + loadedData);
                return(loadedData);
            }
            else
            {
                return(null);
            }
        }
Ejemplo n.º 3
0
    // Use this for initialization
    void Start()
    {
        // Load the game save container (or create it if we don't have one)
        GameSaveContainer gsc = GameSaveContainer.Load();

        // We automatically get an active save.  Add a medal for level 0
        //gsc.ActiveSave().AddMedal(0,0);

        // Save the container
        gsc.Save();

        Debug.Log("We " + (gsc.ActiveSave().HasMedal(0, 0)?"do":"don't") + " have a level 0 medal");
    }
Ejemplo n.º 4
0
    /// <summary>
    /// Load the TMGGameSaveContainer from disk
    /// </summary>
    public static GameSaveContainer Load()
    {
        //string path = Path.Combine(Application.persistentDataPath, "GameSaves.xml");

        #if UNITY_IPHONE || UNITY_STANDALONE_OSX
        string gsc_text = PlayerPrefs.GetString("GameSave");
        GameSaveContainer container;

        if (gsc_text == null || gsc_text == "") {
            gsc_text = PlayerPrefs.GetString("GameSave");
        }

        if (gsc_text == null || gsc_text == "") {
            container = new GameSaveContainer();
            container.AddNewGameSave("default", true);
        } else {
            container = GameSaveContainer.LoadFromText(gsc_text);
            if (container.ActiveSave() == null) container.AddNewGameSave("default", true);
        }

        return container;
        #elif !UNITY_WEBPLAYER
        if (!File.Exists(path)) {
            GameSaveContainer container = new GameSaveContainer();
            container.AddNewGameSave("default", 0, true);
            container.Save();

            return container;
        }

        var serializer = new XmlSerializer(typeof(GameSaveContainer));
        using(var stream = new FileStream(path, FileMode.Open)) {
            return serializer.Deserialize(stream) as GameSaveContainer;
        }

        #else
        string gsc_text = PlayerPrefs.GetString("GameSave");
        GameSaveContainer container;
        if (gsc_text == null || gsc_text == "") {
            container = new GameSaveContainer();
            container.AddNewGameSave("default", 0, true);
        } else {
            container = GameSaveContainer.LoadFromText(gsc_text);
            if (container.ActiveSave() == null) container.AddNewGameSave("default", 0, true);
        }

        return container;
        #endif
    }
Ejemplo n.º 5
0
        public static async void Save(string data, string filename)
        {
            const string c_saveContainerDisplayName = "GameSave";
            const string c_saveBlobName             = "data";
            string       c_saveContainerName        = filename;

            var gameSaveProvider = await GetGameSaveProvider();

            if (gameSaveProvider == null)
            {
                return;
            }

            //Now you have a GameSaveProvider (formerly ConnectedStorageSpace)
            //Next you need to call CreateContainer to get a GameSaveContainer (formerly ConnectedStorageContainer)
            GameSaveContainer gameSaveContainer = gameSaveProvider.CreateContainer(c_saveContainerName);             // this will create a new named game save container with the name = to the input name
            //Parameter
            //string name

            // To store a value in the container, it needs to be written into a buffer, then stored with
            // a blob name in a Dictionary.
            DataWriter writer   = new DataWriter();
            uint       dataSize = writer.MeasureString(data);

            writer.WriteUInt32(dataSize);
            writer.WriteString(data);
            IBuffer dataBuffer = writer.DetachBuffer();

            var blobsToWrite = new Dictionary <string, IBuffer>();

            blobsToWrite.Add(c_saveBlobName, dataBuffer);

            GameSaveOperationResult gameSaveOperationResult = await gameSaveContainer.SubmitUpdatesAsync(blobsToWrite, null, c_saveContainerDisplayName);

            //IReadOnlyDictionary<String, IBuffer> blobsToWrite
            //IEnumerable<string> blobsToDelete
            //string displayName
            Debug.WriteLine("XboxLiveConnectedStorage :: Saved Data - " + data);
        }
Ejemplo n.º 6
0
    public async Task <GameSaveErrorStatus> SaveData(int data)
    {
        if (m_saveProvider == null)
        {
            throw new InvalidOperationException("The save system is not initialized.");
        }

        GameSaveContainer container = m_saveProvider.CreateContainer(c_saveContainerName);

        // To store a value in the container, it needs to be written into a buffer, then stored with
        // a blob name in a Dictionary.
        DataWriter writer = new DataWriter();

        writer.WriteInt32(data);
        IBuffer dataBuffer = writer.DetachBuffer();

        var updates = new Dictionary <string, IBuffer>();

        updates.Add(c_saveBlobName, dataBuffer);

        GameSaveOperationResult result = await container.SubmitUpdatesAsync(updates, null, c_saveContainerDisplayName);

        return(result.Status);
    }
Ejemplo n.º 7
0
    /// <summary>
    /// Load the TMGGameSaveContainer from disk
    /// </summary>
    public static GameSaveContainer Load()
    {
        //string path = Path.Combine(Application.persistentDataPath, "GameSaves.xml");

#if UNITY_IPHONE || UNITY_STANDALONE_OSX
        string            gsc_text = PlayerPrefs.GetString("GameSave");
        GameSaveContainer container;

        if (gsc_text == null || gsc_text == "")
        {
            gsc_text = PlayerPrefs.GetString("GameSave");
        }

        if (gsc_text == null || gsc_text == "")
        {
            container = new GameSaveContainer();
            container.AddNewGameSave("default", true);
        }
        else
        {
            container = GameSaveContainer.LoadFromText(gsc_text);
            if (container.ActiveSave() == null)
            {
                container.AddNewGameSave("default", true);
            }
        }

        return(container);
#elif !UNITY_WEBPLAYER
        if (!File.Exists(path))
        {
            GameSaveContainer container = new GameSaveContainer();
            container.AddNewGameSave("default", 0, true);
            container.Save();

            return(container);
        }

        var serializer = new XmlSerializer(typeof(GameSaveContainer));
        using (var stream = new FileStream(path, FileMode.Open)) {
            return(serializer.Deserialize(stream) as GameSaveContainer);
        }
#else
        string            gsc_text = PlayerPrefs.GetString("GameSave");
        GameSaveContainer container;
        if (gsc_text == null || gsc_text == "")
        {
            container = new GameSaveContainer();
            container.AddNewGameSave("default", 0, true);
        }
        else
        {
            container = GameSaveContainer.LoadFromText(gsc_text);
            if (container.ActiveSave() == null)
            {
                container.AddNewGameSave("default", 0, true);
            }
        }

        return(container);
#endif
    }