个人资料

跳过导航链接首页 > 博客列表 > 博客正文

C# 使用 XboxLive 连接的存储

分类:

XboxLive 使用连接的存储 可以实现游戏数据在不同设备上的加载切换

可以存储一些简单的游戏数据(float) 又或者存储字符串string用于json序列化/反序列化还原自定义游戏数据对象

public static partial class GameParameter
    {

        const string c_saveContainerDisplayName = "GameSave";
        const string c_saveContainerName = "GameSaveContainer";


        public const string json = "blob_json_string";

        public const string 关卡 = "blob_level";
        public const string 宝石数 = "blob_gem";
        public const string 移速 = "blob_move_speed";
        public const string 跳跃力 = "blob_jump_strength";
        public const string 攻速 = "blob_attack_speed";
        public const string 攻击力 = "blob_attack_strength";
        public const string 生命值 = "blob_max_life";
        private const string ServiceConfigurationId = "00000000-0000-0000-0000-0000********";

        //public static XboxLiveContext context
        //{
        //    get {

        //        Debug.WriteLine(XboxLiveHelper.context.AppConfig.ServiceConfigurationId);
        //        return XboxLiveHelper.context; 
        //    }

        //}


        public static async void WriteData(string blobName, string data)
        {

            var users = await Windows.System.User.FindAllAsync();


            GameSaveProvider gameSaveProvider;
            GameSaveProviderGetResult gameSaveTask = await GameSaveProvider.GetForUserAsync(users[0],
            ServiceConfigurationId);

            if (gameSaveTask.Status == GameSaveErrorStatus.Ok)
            {
                gameSaveProvider = gameSaveTask.Value;
            }
            else
            {
                return;
                //throw new Exception("Game Save Provider Initialization failed");
            }
            //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();
            writer.WriteString(data); //some number you want to save, in this case 23.
            IBuffer dataBuffer = writer.DetachBuffer();
            var blobsToWrite = new Dictionary<string, IBuffer>();
            blobsToWrite.Add(blobName, dataBuffer);
            GameSaveOperationResult gameSaveOperationResult = await gameSaveContainer.SubmitUpdatesAsync(blobsToWrite,
            null, c_saveContainerDisplayName);


        }


        public static async void WriteData(string blobName, float data)
        {

            var users = await Windows.System.User.FindAllAsync();

            GameSaveProvider gameSaveProvider;
            GameSaveProviderGetResult gameSaveTask = await GameSaveProvider.GetForUserAsync(users[0],
            ServiceConfigurationId);

            if (gameSaveTask.Status == GameSaveErrorStatus.Ok)
            {
                gameSaveProvider = gameSaveTask.Value;
            }
            else
            {
                Debug.WriteLine("Game Save Provider Initialization failed");
                return;
            }

            GameSaveContainer gameSaveContainer = gameSaveProvider.CreateContainer(c_saveContainerName);

            DataWriter writer = new DataWriter();
            writer.WriteSingle(data);
            IBuffer dataBuffer = writer.DetachBuffer();
            var blobsToWrite = new Dictionary<string, IBuffer>();
            blobsToWrite.Add(blobName, dataBuffer);
            GameSaveOperationResult gameSaveOperationResult = await gameSaveContainer.SubmitUpdatesAsync(blobsToWrite,
            null, c_saveContainerDisplayName);


        }


        public static async Task<string> ReadData(string blobName)
        {
            //int intData=0;
            var users = await Windows.System.User.FindAllAsync();
            GameSaveProvider gameSaveProvider;
            GameSaveProviderGetResult gameSaveTask = await GameSaveProvider.GetForUserAsync(users[0],
            ServiceConfigurationId);
            //Parameters
            //Windows.System.User user
            //string SCID
            if (gameSaveTask.Status == GameSaveErrorStatus.Ok)
            {
                gameSaveProvider = gameSaveTask.Value;
            }
            else
            {
                return "";
                //throw new Exception("Game Save Provider Initialization failed");;
            }
            //Now you have a GameSaveProvider
            //Next you need to call CreateContainer to get a GameSaveContainer
            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[] { blobName };
            // 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);
            string loadedData = "";
            //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(blobName, out loadedBuffer);
                if (loadedBuffer == null)
                {
                    Debug.WriteLine(string.Format("Didn't find expected blob \"{0}\" in the loaded data.", blobName));

                }
                loadedData = Encoding.UTF8.GetString(loadedBuffer.ToArray());
            }
            return loadedData;
        }


        public static async Task<float> ReadDataNumber(string blobName)
        {
            //int intData=0;
            var users = await Windows.System.User.FindAllAsync();
            GameSaveProvider gameSaveProvider;
            GameSaveProviderGetResult gameSaveTask = await GameSaveProvider.GetForUserAsync(users[0],
            ServiceConfigurationId);
            //Parameters
            //Windows.System.User user
            //string SCID
            if (gameSaveTask.Status == GameSaveErrorStatus.Ok)
            {
                gameSaveProvider = gameSaveTask.Value;
            }
            else
            {
                return -1f;
                //throw new Exception("Game Save Provider Initialization failed");;
            }
            //Now you have a GameSaveProvider
            //Next you need to call CreateContainer to get a GameSaveContainer
            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[] { blobName };
            // 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);
            float loadedData = 0;
            //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(blobName, out loadedBuffer);

                if (loadedBuffer == null)
                {
                    Debug.WriteLine(String.Format("Didn't find expected blob \"{0}\" in the loaded data.", blobName));
                    return -1f;
                }
                DataReader reader = DataReader.FromBuffer(loadedBuffer);
                loadedData = reader.ReadSingle();
            }
            return loadedData;
        }



        public static async void DeleteData(string blobName)
        {

            var users = await Windows.System.User.FindAllAsync();


            GameSaveProvider gameSaveProvider;
            GameSaveProviderGetResult gameSaveTask = await GameSaveProvider.GetForUserAsync(users[0],
            ServiceConfigurationId);
            //Parameters
            //Windows.System.User user
            //string SCID
            if (gameSaveTask.Status == GameSaveErrorStatus.Ok)
            {
                gameSaveProvider = gameSaveTask.Value;
            }
            else
            {
                return;
                //throw new Exception("Game Save Provider Initialization failed");
            }
            //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

            string[] blobsToDelete = new string[] { blobName };
            //Call SubmitUpdatesAsync with the dictionary of the names of blobs to delete as the second parameter.
            GameSaveOperationResult gameSaveOperationResult = await gameSaveContainer.SubmitUpdatesAsync(null,
            blobsToDelete, c_saveContainerDisplayName);
            //Parameters
            //IReadOnlyDictionary<String, IBuffer> blobsToWrite
            //IEnumerable<string> blobsToDelete
            //string displayName
            //Check status of the operation to ensure successful delete.
            if (gameSaveOperationResult.Status == GameSaveErrorStatus.Ok)
            {
                //Successful Deletion Logic
            }
            else
            {
                //handle failed deletion logic.
            }
            //DELETE A SAVE CONTAINER
            GameSaveOperationResult deleteContainerResult = await
            gameSaveProvider.DeleteContainerAsync(c_saveContainerName);
            //Parameter
            //string name (name of container to be deleted)
            //Check status of the operation to ensure successful delete.
            if (deleteContainerResult.Status == GameSaveErrorStatus.Ok)
            {
                //Successful Deletion Logic
            }
            else
            {
                //handle failed deletion logic.
            }

        }



    }


end

songshizhao
最初发表2022/5/1 23:47:39 最近更新2022/5/2 0:04:46 104
为此篇作品打分
10
   评论