个人资料

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

C# 使用 XboxLive 游戏数据云存储

:

XboxLive 游戏数据云存储不同于连接的存储在于可以允许上传复杂的游戏内容到服务器

比如玩家图片等.连接的存储适合单个简单的数据,云存储适合复杂模型和游戏资料.


using Microsoft.Xbox.Services.TitleStorage;
using MyPlatformGame.Model;
using Newtonsoft.Json;
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.IO;
using System.Linq;
using System.Runtime.InteropServices.WindowsRuntime;
using System.Text;
using System.Threading.Tasks;
using Windows.Storage.Streams;

namespace MyPlatformGame.helper
{
    public partial class XboxLiveHelper
    {

        public const string SettingPath = "Setting.json";
        //public const string PlayerAssetPath = "Asset.json";

        public static async Task Upload<T>(T Tvalue,string path)
        {


            if (context == null) return;


            TitleStorageService service = context.TitleStorageService;
            //
            TitleStorageBlobMetadata meta =new TitleStorageBlobMetadata(
                context.AppConfig.ServiceConfigurationId,
                TitleStorageType.Universal,
                path,
                TitleStorageBlobType.Json, 
                xboxUser.XboxUserId);// displayname, etag  

            string json=JsonConvert.SerializeObject(Tvalue);
           
            byte[] byteList = Encoding.UTF8.GetBytes(json);
            IBuffer blob_buffer= byteList.AsBuffer();

            await service.UploadBlobAsync(
                meta, 
                blob_buffer,
                TitleStorageETagMatchCondition.NotUsed,
                1024);

        }

        public static async Task<T> Download<T>(string path="")
        {

            if (context == null) return default(T);

            TitleStorageService service = context.TitleStorageService;
            var metaResult=await service.GetBlobMetadataAsync(
            context.AppConfig.ServiceConfigurationId,
            TitleStorageType.Universal,
            path,
            xboxUser.XboxUserId,
            0,
            1);
            TitleStorageBlobMetadata meta = metaResult.Items[0];
            byte[] bytes = new byte[meta.Length];
            IBuffer blob_buffer = bytes.AsBuffer();
            var downloadResult=await service.DownloadBlobAsync(meta, blob_buffer, TitleStorageETagMatchCondition.NotUsed,"");

            var result=JsonConvert.DeserializeObject<T>(Encoding.UTF8.GetString(downloadResult.BlobBuffer.ToArray()));

            return result;

        }


        public static async Task Delete(string path = "")
        {

            if (context == null) return;

            TitleStorageService service = context.TitleStorageService;
            var metaResult = await service.GetBlobMetadataAsync(
            context.AppConfig.ServiceConfigurationId,
            TitleStorageType.Universal,
            path,
            xboxUser.XboxUserId,
            0,
            1);
            TitleStorageBlobMetadata meta = metaResult.Items[0];

            Debug.WriteLine($"BlobPath:{meta.BlobPath}DisplayName:{meta.DisplayName}");


            await service.DeleteBlobAsync(meta,false);


        }

    }
}



songshizhao
最初发表2022/5/1 23:52:37 最近更新2022/5/2 0:05:41 795
为此篇作品打分
10