45 lines
1.3 KiB
C
Raw Normal View History

2015-04-27 22:25:09 +02:00
#pragma once
#include <ppl.h>
#include <ppltasks.h>
2016-07-09 11:21:54 +02:00
#include <wrl/client.h>
2015-04-27 22:25:09 +02:00
2016-07-09 11:21:54 +02:00
namespace DX {
inline void ThrowIfFailed(HRESULT hr)
2015-04-27 22:25:09 +02:00
{
2016-07-09 11:21:54 +02:00
if (FAILED(hr)) {
// Set a breakpoint on this line to catch Win32 API errors.
throw Platform::Exception::CreateException(hr);
2015-04-27 22:25:09 +02:00
}
2016-07-09 11:21:54 +02:00
}
2015-04-27 22:25:09 +02:00
2016-07-09 11:21:54 +02:00
// Function that reads from a binary file asynchronously.
inline Concurrency::task<Platform::Array<byte> ^> ReadDataAsync(
Platform::String ^ filename)
{
using namespace Windows::Storage;
using namespace Concurrency;
2015-04-27 22:25:09 +02:00
2016-07-09 11:21:54 +02:00
auto folder = Windows::ApplicationModel::Package::Current->InstalledLocation;
2015-04-27 22:25:09 +02:00
2016-07-09 11:21:54 +02:00
return create_task(folder->GetFileAsync(filename))
.then([](StorageFile ^ file) {
2015-04-27 22:25:09 +02:00
#if !PHONE
return FileIO::ReadBufferAsync(file);
#else
return file->OpenReadAsync();
}).then([](Streams::IRandomAccessStreamWithContentType^ stream)
{
unsigned int bufferSize = static_cast<unsigned int>(stream->Size);
auto fileBuffer = ref new Streams::Buffer(bufferSize);
return stream->ReadAsync(fileBuffer, bufferSize, Streams::InputStreamOptions::None);
#endif
2016-07-09 11:21:54 +02:00
})
.then([](Streams::IBuffer ^ fileBuffer) -> Platform::Array<byte> ^ {
2015-04-27 22:25:09 +02:00
auto fileData = ref new Platform::Array<byte>(fileBuffer->Length);
Streams::DataReader::FromBuffer(fileBuffer)->ReadBytes(fileData);
return fileData;
});
2016-07-09 11:21:54 +02:00
}
2015-04-27 22:25:09 +02:00
}