forked from twig33/ynoclient
-
Notifications
You must be signed in to change notification settings - Fork 8
/
Copy pathasync_handler.h
253 lines (220 loc) · 6.86 KB
/
async_handler.h
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
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
/*
* This file is part of EasyRPG Player.
*
* EasyRPG Player is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* EasyRPG Player is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with EasyRPG Player. If not, see <http://www.gnu.org/licenses/>.
*/
#ifndef EP_ASYNC_HANDLER_H
#define EP_ASYNC_HANDLER_H
#include <functional>
#include <memory>
#include <string>
#include <vector>
#include "string_view.h"
class FileRequestAsync;
struct FileRequestResult;
/**
* AsyncHandler supports asynchronous file requests for platforms that don't
* support synchronous IO (e.g. Emscripten).
*/
namespace AsyncHandler {
/**
* Parses the specified JSON file. The file mapping read from this file
* will be used for further ajax requests.
*/
void CreateRequestMapping(const std::string& file);
/**
* Creates a request to a file.
* When the same file was already requested this will return an already
* existing request.
* The caller shouldn't choose different code path depending on new or
* existing requests.
*
* @param folder_name folder where the file is stored
* @param file_name Name of the requested file requested.
* @return The async request.
*/
FileRequestAsync* RequestFile(StringView folder_name, StringView file_name);
/**
* Creates a request to a file.
* When the same file was already requested this will return an already
* existing request.
* The caller shouldn't choose different code path depending on new or
* existing requests.
*
* @param file_name Name of the requested file requested.
* @return The async request.
*/
FileRequestAsync* RequestFile(StringView file_name);
/**
* Checks if any file with important-flag hasn't finished downloading yet.
*
* @return If any file with important-flag is pending.
*/
bool IsImportantFilePending();
/**
* Checks if any file with graphic-flag hasn't finished downloading yet.
*
* @return If any file with graphic-flag is pending.
*/
bool IsGraphicFilePending();
/**
* Checks if any file is hasn't finished downloading yet.
*
* @param important check only important files
* @param graphic check only graphic files
*
* @return If any file with params is pending.
*/
bool IsFilePending(bool important, bool graphic);
}
using FileRequestBinding = std::shared_ptr<int>;
using FileRequestBindingWeak = std::weak_ptr<int>;
/**
* Handles a single asynchronous file request.
*/
class FileRequestAsync {
public:
enum AsyncState {
State_WaitForStart,
State_DoneSuccess,
State_DoneFailure,
State_Pending
};
/**
* Don't use this API directly. Use AsyncHandler::RequestFile.
*
* @param path path to the file
* @return The async request.
*/
FileRequestAsync(std::string path, std::string directory, std::string file);
/**
* Checks if a request finished.
*
* @return True when request suceeded or failed, false otherwise.
*/
bool IsReady() const;
/**
* @return If while has important-flag set.
*/
bool IsImportantFile() const;
/**
* Sets the important flag.
* This flag must be set before Start() is invoked.
* When the important flag is set the Player update loop will block until
* the request is finished.
*
* @param important value of important flag.
*/
void SetImportantFile(bool important);
/**
* @return If while has graphic-flag set.
*/
bool IsGraphicFile() const;
/**
* Sets the graphic flag.
* When the graphic flag is set, Scene_Map will block starting any
* user requested transitions until the request is finished.
*
* @param important value of important flag.
*/
void SetGraphicFile(bool graphic);
/**
* Starts the async requests.
* When the request was already started earlier and is pending this call
* does nothing. When the request is already all binded event handlers are
* called immediately.
*/
void Start();
/**
* @return Path to the requested file.
*/
const std::string& GetPath() const;
/**
* Binds a member function as request-finished event handler.
* The event handler is only invoked once (one-shot).
* The caller must hold a reference to the returned object, otherwise the
* handler is unbound.
*
* @param func member function.
* @param that instance of object.
* @param args arguments passed to func.
* @return request binding reference.
*/
template<typename T, typename... Args>
FileRequestBinding Bind(void (T::*func)(FileRequestResult*, Args...), T* that, Args... args);
/**
* Binds a function as request-finished event handler.
* The event handler is only invoked once (one-shot).
* The caller must hold a reference to the returned object, otherwise the
* handler is unbound.
*
* @param func function.
* @return request binding reference.
*/
FileRequestBinding Bind(std::function<void(FileRequestResult*)> func);
/**
* Binds a function as request-finished event handler.
* The event handler is only invoked once (one-shot).
* The caller must hold a reference to the returned object, otherwise the
* handler is unbound.
*
* @param func function.
* @return request binding reference.
*/
FileRequestBinding Bind(void(*func)(FileRequestResult*));
// don't call these directly
void DownloadDone(bool success);
void UpdateProgress();
private:
void CallListeners(bool success);
std::vector<std::pair<FileRequestBindingWeak, std::function<void(FileRequestResult*)> > > listeners;
std::string directory;
std::string file;
std::string path;
int state = State_DoneFailure;
bool important = false;
bool graphic = false;
};
/**
* Contains the result of an async request.
* directory: Directory name
* file: Name of requested file
* success: true if requested was successful, otherwise false.
*/
struct FileRequestResult {
const std::string& directory;
const std::string& file;
bool success;
};
template<typename T, typename... Args>
FileRequestBinding FileRequestAsync::Bind(void (T::*func)(FileRequestResult*, Args...), T* that, Args... args) {
std::function<void(FileRequestResult*)> f = std::bind(std::mem_fn(func), that, std::placeholders::_1, args...);
return Bind(f);
}
inline bool FileRequestAsync::IsReady() const {
return state == State_DoneSuccess || state == State_DoneFailure;
}
inline bool FileRequestAsync::IsImportantFile() const {
return important;
}
inline void FileRequestAsync::SetImportantFile(bool important) {
this->important = important;
}
inline bool FileRequestAsync::IsGraphicFile() const {
return graphic;
}
inline const std::string& FileRequestAsync::GetPath() const {
return path;
}
#endif