forked from hrydgard/ppsspp
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathStorageFileLoader.cpp
177 lines (155 loc) · 4.27 KB
/
StorageFileLoader.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
#include "pch.h"
#include "ppltasks.h"
#include "base/logging.h"
#include "file/file_util.h"
#include "thread/threadutil.h"
#include "StorageFileLoader.h"
#include "UWPUtil.h"
using namespace Concurrency;
using namespace Windows::Storage;
using namespace Windows::Storage::Streams;
static std::mutex initMutex;
StorageFileLoader::StorageFileLoader(Windows::Storage::StorageFile ^file) {
file_ = file;
path_ = FromPlatformString(file_->Path);
thread_.reset(new std::thread([this]() { this->threadfunc(); }));
}
StorageFileLoader::~StorageFileLoader() {
initMutex.lock();
active_ = false;
operationRequested_ = false;
cond_.notify_all();
thread_->join();
initMutex.unlock();
}
void StorageFileLoader::threadfunc() {
setCurrentThreadName("StorageFileLoader");
initMutex.lock();
auto opentask = create_task(file_->OpenReadAsync()).then([this](IRandomAccessStreamWithContentType ^stream) {
stream_ = stream;
active_ = true;
});
try {
opentask.wait();
}
catch (const std::exception& e) {
operationFailed_ = true;
// TODO: What do we do?
const char *what = e.what();
ILOG("%s", what);
}
catch (Platform::COMException ^e) {
}
auto sizetask = create_task(file_->GetBasicPropertiesAsync()).then([this](Windows::Storage::FileProperties::BasicProperties ^props) {
size_ = props->Size;
});
try {
sizetask.wait();
}
catch (const std::exception& e) {
const char *what = e.what();
ILOG("%s", what);
}
catch (Platform::COMException ^e) {
std::string what = FromPlatformString(e->ToString());
ILOG("%s", what.c_str());
}
initMutex.unlock();
std::unique_lock<std::mutex> lock(mutex_);
while (active_) {
if (!operationRequested_) {
cond_.wait(lock);
}
if (operationRequested_) {
switch (operation_.type) {
case OpType::READ_AT: {
Streams::Buffer ^buf = ref new Streams::Buffer(operation_.size);
operationFailed_ = false;
stream_->Seek(operation_.offset);
auto task = create_task(stream_->ReadAsync(buf, operation_.size, Streams::InputStreamOptions::None));
Streams::IBuffer ^output = nullptr;
try {
task.wait();
output = task.get();
} catch (const std::exception& e) {
operationFailed_ = true;
const char *what = e.what();
ILOG("%s", what);
}
operationRequested_ = false;
std::unique_lock<std::mutex> lock(mutexResponse_);
response_.buffer = output;
responseAvailable_ = true;
condResponse_.notify_one();
break;
}
default:
ELOG("Unknown operation");
operationRequested_ = false;
break;
}
}
}
}
bool StorageFileLoader::Exists() {
return file_ != nullptr;
}
bool StorageFileLoader::ExistsFast() {
return file_ != nullptr;
}
bool StorageFileLoader::IsDirectory() {
return (file_->Attributes & Windows::Storage::FileAttributes::Directory) != Windows::Storage::FileAttributes::Normal;
}
s64 StorageFileLoader::FileSize() {
EnsureOpen();
if (size_ == -1)
__debugbreak(); // crude race condition detection
return size_;
}
std::string StorageFileLoader::Path() const {
return path_;
}
std::string StorageFileLoader::Extension() {
return "." + getFileExtension(path_);
}
void StorageFileLoader::EnsureOpen() {
// UGLY!
while (!thread_)
Sleep(100);
while (size_ == -1)
Sleep(100);
}
size_t StorageFileLoader::ReadAt(s64 absolutePos, size_t bytes, size_t count, void *data, Flags flags) {
EnsureOpen();
if (operationRequested_ || responseAvailable_)
Crash();
{
std::unique_lock<std::mutex> lock(mutex_);
operation_.type = OpType::READ_AT;
operation_.offset = absolutePos;
operation_.size = (int64_t)(bytes * count);
operationRequested_ = true;
cond_.notify_one();
}
// OK, now wait for response...
{
std::unique_lock<std::mutex> responseLock(mutexResponse_);
while (!responseAvailable_) {
condResponse_.wait(responseLock);
}
if (operationFailed_) {
return 0;
}
DataReader ^rd = DataReader::FromBuffer(response_.buffer);
size_t len = response_.buffer->Length;
Platform::Array<uint8_t> ^bytearray = ref new Platform::Array<uint8_t>((unsigned int)len);
rd->ReadBytes(bytearray);
memcpy(data, bytearray->Data, len);
responseAvailable_ = false;
response_.buffer = nullptr;
return len / bytes;
}
}
FileLoader *StorageFileLoaderFactory::ConstructFileLoader(const std::string &filename) {
return file_ ? new StorageFileLoader(file_) : nullptr;
}