> ## Documentation Index
> Fetch the complete documentation index at: https://devdocs.xbox.com/llms.txt
> Use this file to discover all available pages before exploring further.

# XGameSaveReadBlobData

> XGameSaveReadBlobData

# XGameSaveReadBlobData

读取容器的 Blob 数据。

## 语法

```cpp theme={null}
HRESULT XGameSaveReadBlobData(  
         XGameSaveContainerHandle container,  
         const char** blobNames,  
         uint32_t* countOfBlobs,  
         size_t blobsSize,  
         XGameSaveBlob* blobData  
)  
```

### 参数

*container*   \_In\_\
类型：XGameSaveContainerHandle

包含 XGameSaveBlob 数据的 **XGameSaveContainer** 的句柄。

*blobNames*   \_In\_opt\_z\_count\_(*countOfBlobs)\
类型：char*\*

表示 [XGameSaveBlob](/reference/system/xgamesave/structs/xgamesaveblob) 名称的字符串数组的指针。

*countOfBlobs*   \_Inout\_\
类型：uint32\_t\*

要读取的 Blob 数量。

*blobsSize*   \_In\_\
类型：size\_t

已分配的 Blob 数据大小，可通过读取 Blob 元数据推断得出。

*blobData*   \_Out\_writes\_bytes\_(blobsSize)\
类型：[XGameSaveBlob\*](/reference/system/xgamesave/structs/xgamesaveblob)

用于包含 Blob 数据的 [XGameSaveBlob](/reference/system/xgamesave/structs/xgamesaveblob) 指针。必须分配足够的内存来存储从容器请求的所有 Blob。

### 返回值

类型：HRESULT

函数结果。

#### 常见错误

* E\_GS\_INVALID\_CONTAINER\_NAME
* E\_GS\_PROVIDED\_BUFFER\_TOO\_SMALL
* E\_GS\_BLOB\_NOT\_FOUND
* E\_GS\_CONTAINER\_NOT\_IN\_SYNC
* E\_GS\_CONTAINER\_SYNC\_FAILED
* E\_GS\_HANDLE\_EXPIRED

## 备注

<Note>在时间敏感线程上调用此函数并不安全。有关详细信息，请参阅[时间敏感线程](https://learn.microsoft.com/gaming/gdk/docs/gdk-dev/console-dev/overviews/threads/time-sensitive-threads)。</Note>

使用此函数读取游戏保存 Blob 中的数据。此函数将返回 Blob 数量和 Blob 容器中的数据。你可以使用这些信息迭代容器中的 Blob 以读取相应的信息。此函数还有一个异步版本 [XGameSaveReadBlobDataAsync](/reference/system/xgamesave/functions/xgamesavereadblobdataasync)。

```cpp theme={null}
// SYNC Read - should not be called on time sensitive thread 
//             as this will block until the operation is complete 
void Sample::_ReadContainerBlobsSync(const XGameSaveContainerInfo* container) 
{ 
    const char* blobNames[] = { 
        "WorldState", 
        "PlayerState", 
        "PlayerInventory" 
    }; 
  
    XGameSaveContainerHandle containerContext = nullptr; 
    size_t allocSize; 
    uint32_t countOfBlobs = _countof(blobNames); 
    XGameSaveBlob* blobs = nullptr; 
    HRESULT hr = XGameSaveCreateContainer(_provider, container->name, &containerContext); 
  
    if (SUCCEEDED(hr)) 
    { 
        // this method finds the size for only the blobs in the container 
        // that we are requesting to read right now 
        hr = _GetContainerBlobsDataSize(container, blobNames, _countof(blobNames), &allocSize); 
    } 
    if (SUCCEEDED(hr)) 
    { 
        blobs = reinterpret_cast<XGameSaveBlob*>(malloc(allocSize)); 
        if (blobs == nullptr) 
        { 
            hr = E_OUTOFMEMORY; 
        } 
    } 
    if (SUCCEEDED(hr)) 
    { 
        hr = XGameSaveReadBlobData(containerContext, blobNames, &countOfBlobs, allocSize, blobs); 
    } 
    if (SUCCEEDED(hr)) 
    { 
        if (countOfBlobs == _countof(blobNames)) 
        { 
            for (uint32_t i = 0; i < countOfBlobs; i++) 
            { 
                XGameSaveBlob* currentBlob = blobs + i; 
                if (strcmp(currentBlob->info.name, "WorldState") == 0) 
                { 
                    hr = _LoadSaveBlob(currentBlob, _worldState); 
                } 
                else if (strcmp(currentBlob->info.name, "PlayerState") == 0) 
                { 
                    hr = _LoadSaveBlob(currentBlob, _playerState); 
                } 
                else if (strcmp(currentBlob->info.name, "PlayerInventory") == 0) 
                { 
                    hr = _LoadSaveBlob(currentBlob, _playerInventory); 
                } 
                if (FAILED(hr)) 
                { 
                    break; 
                } 
            } 
        } 
        else 
        { 
            hr = E_UNEXPECTED; 
        } 
    } 
  
    _HandleContainerBlobErrors(hr); 
  
    if (blobs != nullptr) 
    { 
        free(blobs); 
    } 
    if (containerContext != nullptr) 
    { 
        XGameSaveCloseContainer(containerContext); 
    } 
} 
  
  
void Sample::_HandleContainerBlobErrors(HRESULT hr) 
{ 
    // set some state 
    switch (hr) 
    { 
    case E_GS_INVALID_CONTAINER_NAME: 
        // tried to access a container with an invalid name 
        break; 
    case E_GS_PROVIDED_BUFFER_TOO_SMALL: 
        // shouldn't ever happen unless our math is wrong!! 
        break; 
    case E_GS_BLOB_NOT_FOUND: 
        // we asked for a blob that isn't in the container 
        break; 
    case E_GS_CONTAINER_NOT_IN_SYNC: 
    case E_GS_CONTAINER_SYNC_FAILED: 
        // need to sync and we are offline ? 
        break; 
    case E_GS_HANDLE_EXPIRED: 
        // need to re-initialize since another device has taken 
        // ownership while we were suspended and/or busy  
        break; 
    } 
} 
```

## 要求

**标头：** XGameSave.h

**库：** xgameruntime.lib

**支持的平台：** Windows、XBOX One 系列主机和 XBOX Series 主机

## 概念性文档

* [时间敏感线程](https://learn.microsoft.com/gaming/gdk/docs/gdk-dev/console-dev/overviews/threads/time-sensitive-threads)

## 另请参阅

[XGameSave](/reference/system/xgamesave/xgamesave_members)\
[XGameSaveBlobInfo](/reference/system/xgamesave/structs/xgamesaveblobinfo)\
[XGameSaveReadBlobDataAsync](/reference/system/xgamesave/functions/xgamesavereadblobdataasync)\
[调试游戏保存](/build/core-features/common/game-save/game-saves-debugging)


## Related topics

- [XGameSaveReadBlobDataAsync](/zh-CN/reference/system/xgamesave/functions/xgamesavereadblobdataasync.md)
- [XGameSaveBlob](/zh-CN/reference/system/xgamesave/structs/xgamesaveblob.md)
- [XGameSave](/zh-CN/reference/system/xgamesave/xgamesave_members.md)
- [面向 GDK 的 Unity C# API 包装器](/zh-CN/build/gdk-and-engines/unity/unity-api-wrappers.md)
- [XGameSaveReadBlobDataResult](/zh-CN/reference/system/xgamesave/functions/xgamesavereadblobdataresult.md)
