2014-08-28 17:53:24 +04:00
|
|
|
#pragma once
|
|
|
|
|
|
|
|
#include <ppl.h>
|
|
|
|
#include <ppltasks.h>
|
2016-04-29 17:53:13 +03:00
|
|
|
#include <wrl/client.h>
|
2014-08-28 17:53:24 +04:00
|
|
|
|
2016-05-16 17:34:04 +03:00
|
|
|
namespace DX {
|
|
|
|
inline void ThrowIfFailed(HRESULT hr)
|
2014-08-28 17:53:24 +04:00
|
|
|
{
|
2016-05-16 17:34:04 +03:00
|
|
|
if (FAILED(hr)) {
|
|
|
|
// Set a breakpoint on this line to catch Win32 API errors.
|
|
|
|
throw Platform::Exception::CreateException(hr);
|
2014-08-28 17:53:24 +04:00
|
|
|
}
|
2016-05-16 17:34:04 +03:00
|
|
|
}
|
2014-08-28 17:53:24 +04:00
|
|
|
|
2016-05-16 17:34:04 +03: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;
|
2014-08-28 17:53:24 +04:00
|
|
|
|
2016-05-16 17:34:04 +03:00
|
|
|
auto folder = Windows::ApplicationModel::Package::Current->InstalledLocation;
|
2014-08-28 17:53:24 +04:00
|
|
|
|
2016-05-16 17:34:04 +03:00
|
|
|
return create_task(folder->GetFileAsync(filename))
|
|
|
|
.then([](StorageFile ^ file) {
|
2014-08-28 17:53:24 +04: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-05-16 17:34:04 +03:00
|
|
|
})
|
|
|
|
.then([](Streams::IBuffer ^ fileBuffer) -> Platform::Array<byte> ^ {
|
2014-08-28 17:53:24 +04:00
|
|
|
auto fileData = ref new Platform::Array<byte>(fileBuffer->Length);
|
|
|
|
Streams::DataReader::FromBuffer(fileBuffer)->ReadBytes(fileData);
|
|
|
|
return fileData;
|
|
|
|
});
|
2016-05-16 17:34:04 +03:00
|
|
|
}
|
2014-08-28 17:53:24 +04:00
|
|
|
}
|