-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathlua_binding_manual.cpp
More file actions
301 lines (257 loc) · 8.25 KB
/
lua_binding_manual.cpp
File metadata and controls
301 lines (257 loc) · 8.25 KB
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
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
#include "scripting/lua-bindings/manual/tolua_fix.h"
#include "lua_binding_manual.h"
#include "network/CCDownloader.h"
#include "scripting/lua-bindings/manual/LuaBasicConversions.h"
#include "scripting/lua-bindings/manual/CCLuaValue.h"
#include "scripting/lua-bindings/manual/CCLuaEngine.h"
USING_NS_CC;
static int& lua_dd_Downloader_get_handler()
{
static int s_downloaderHandler = 0;
return s_downloaderHandler;
}
static void lua_dd_Downloader_constructor_setCallBacks(network::Downloader* downloader)
{
// define progress callback
downloader->onTaskProgress = [](const network::DownloadTask& task,
int64_t bytesReceived,
int64_t totalBytesReceived,
int64_t totalBytesExpected)
{
int handler = lua_dd_Downloader_get_handler();
if (0 != handler)
{
LuaStack* stack = LuaEngine::getInstance()->getLuaStack();
stack->pushString("onProgress");
stack->pushString(task.identifier.c_str());
stack->pushLong(bytesReceived);
stack->pushLong(totalBytesReceived);
stack->pushLong(totalBytesExpected);
stack->executeFunctionByHandler(handler, 5);
stack->clean();
}
};
// define success callback
downloader->onDataTaskSuccess = [](const cocos2d::network::DownloadTask& task,
std::vector<unsigned char>& data)
{
int handler = lua_dd_Downloader_get_handler();
if (0 != handler)
{
CCLOG("Success to download data");
LuaStack* stack = LuaEngine::getInstance()->getLuaStack();
stack->pushString("onDataSuccess");
stack->pushString(task.identifier.c_str());
stack->pushString(std::string(data.begin(), data.end()).c_str(), (int)data.size());
stack->executeFunctionByHandler(handler, 3);
stack->clean();
}
};
downloader->onFileTaskSuccess = [](const cocos2d::network::DownloadTask& task)
{
CCLOG("Success to download file : %s to path(%s)"
, task.requestURL.c_str()
, task.storagePath.c_str());
int handler = lua_dd_Downloader_get_handler();
if (0 != handler)
{
LuaStack* stack = LuaEngine::getInstance()->getLuaStack();
stack->pushString("onFileSuccess");
stack->pushString(task.identifier.c_str());
stack->executeFunctionByHandler(handler, 2);
stack->clean();
}
};
downloader->onTaskError = [](const cocos2d::network::DownloadTask& task,
int errorCode,
int errorCodeInternal,
const std::string& errorStr)
{
CCLOG("Failed to download : %s to path(%s) error code(%d), internal error code(%d) desc(%s)"
, task.requestURL.c_str()
, task.storagePath.c_str()
, errorCode
, errorCodeInternal
, errorStr.c_str());
int handler = lua_dd_Downloader_get_handler();
if (0 != handler)
{
LuaStack* stack = LuaEngine::getInstance()->getLuaStack();
stack->pushString("onTaskFailed");
stack->pushString(task.identifier.c_str());
stack->executeFunctionByHandler(handler, 2);
stack->clean();
}
};
}
static void lua_dd_Downloader_remove_handler(lua_State* L)
{
int handler = lua_dd_Downloader_get_handler();
if (0 != handler)
{
toluafix_remove_function_by_refid(L, handler);
}
}
static int lua_collect_Downloader (lua_State* L)
{
network::Downloader* self = (network::Downloader*) tolua_tousertype(L,1,0);
CC_SAFE_DELETE(self);
lua_dd_Downloader_remove_handler(L);
return 0;
}
static int lua_dd_Downloader_constructor(lua_State* L)
{
int argc = 0;
network::Downloader* self = nullptr;
argc = lua_gettop(L)-1;
if (argc == 0)
{
self = new (std::nothrow) network::Downloader();
tolua_pushusertype(L, (void*)self, "dd.Downloader");
tolua_register_gc(L, lua_gettop(L));
lua_dd_Downloader_constructor_setCallBacks(self);
return 1;
}
luaL_error(L, "%s has wrong number of arguments: %d, was expecting %d \n", "Downloader",argc, 0);
return 0;
}
/*
params:
@url
@task Id -- string
*/
static int lua_dd_Downloader_downloadData(lua_State* L)
{
int argc = 0;
network::Downloader* self = nullptr;
bool ok = true;
#if COCOS2D_DEBUG >= 1
tolua_Error tolua_err;
if (!tolua_isusertype(L,1,"dd.Downloader",0,&tolua_err)) goto tolua_lerror;
#endif
self = (network::Downloader*) tolua_tousertype(L,1,0);
#if COCOS2D_DEBUG >= 1
if (nullptr == self)
{
tolua_error(L,"invalid 'self' in function 'lua_dd_Downloader_downloadData'\n", nullptr);
return 0;
}
#endif
argc = lua_gettop(L) - 1;
if (argc == 2)
{
std::string arg0;
std::string arg1;
ok &= luaval_to_std_string(L, 2, &arg0, "Downloader:downloadData");
ok &= luaval_to_std_string(L, 3, &arg1, "Downloader:downloadData");
if (!ok)
{
tolua_error(L, "invalid arguments in function 'lua_dd_Downloader_downloadData'", nullptr);
return 0;
}
self->createDownloadDataTask(arg0, arg1);
return 0;
}
luaL_error(L, "%s has wrong number of arguments: %d, was expecting %d\n", "dd.Downloader.downloadData",argc, 1);
return 0;
#if COCOS2D_DEBUG >= 1
tolua_lerror:
tolua_error(L,"#ferror in function 'lua_dd_Downloader_downloadData'.",&tolua_err);
return 0;
#endif
}
/*
params:
@url
@Absolute FilePath
@task Id -- string
*/
static int lua_dd_Downloader_downloadFile(lua_State* L)
{
int argc = 0;
network::Downloader* self = nullptr;
bool ok = true;
#if COCOS2D_DEBUG >= 1
tolua_Error tolua_err;
if (!tolua_isusertype(L,1,"dd.Downloader",0,&tolua_err)) goto tolua_lerror;
#endif
self = (network::Downloader*) tolua_tousertype(L,1,0);
#if COCOS2D_DEBUG >= 1
if (nullptr == self)
{
tolua_error(L,"invalid 'self' in function 'lua_dd_Downloader_downloadFile'\n", nullptr);
return 0;
}
#endif
argc = lua_gettop(L) - 1;
if (argc == 3)
{
std::string arg0;
std::string arg1;
std::string arg2;
ok &= luaval_to_std_string(L, 2, &arg0, "Downloader:downloadFile");
ok &= luaval_to_std_string(L, 3, &arg1, "Downloader:downloadFile");
ok &= luaval_to_std_string(L, 4, &arg2, "Downloader:downloadFile");
if (!ok)
{
tolua_error(L, "invalid arguments in function 'lua_dd_Downloader_downloadFile'", nullptr);
return 0;
}
self->createDownloadFileTask(arg0, arg1, arg2);
return 0;
}
luaL_error(L, "%s has wrong number of arguments: %d, was expecting %d\n", "dd.Downloader.downloadFile",argc, 1);
return 0;
#if COCOS2D_DEBUG >= 1
tolua_lerror:
tolua_error(L,"#ferror in function 'lua_dd_Downloader_downloadFile'.",&tolua_err);
return 0;
#endif
}
static int lua_dd_Downloader_registCallBack(lua_State* L)
{
int argc = 0;
#if COCOS2D_DEBUG >= 1
tolua_Error tolua_err;
if (!tolua_isusertable(L, 1, "dd.Downloader", 0, &tolua_err)) goto tolua_lerror;
#endif
argc = lua_gettop(L) - 1;
if (argc == 1)
{
#if COCOS2D_DEBUG >= 1
if (!toluafix_isfunction(L, 2, "LUA_FUNCTION", 0, &tolua_err))
goto tolua_lerror;
#endif
lua_dd_Downloader_remove_handler(L);
int& handler = lua_dd_Downloader_get_handler();
handler = (toluafix_ref_function(L, 2, 0));
return 0;
}
luaL_error(L, "%s has wrong number of arguments: %d, was expecting %d\n", "dd.Downloader.registCallBack",argc, 1);
return 0;
#if COCOS2D_DEBUG >= 1
tolua_lerror:
tolua_error(L,"#ferror in function 'lua_dd_Downloader_registerCallBack'.",&tolua_err);
return 0;
#endif
}
static void extendDownloader(lua_State* L)
{
tolua_usertype(L, "dd.Downloader");
tolua_cclass(L, "Downloader", "dd.Downloader", "", lua_collect_Downloader);
tolua_beginmodule(L, "Downloader");
tolua_function(L, "new", lua_dd_Downloader_constructor);
tolua_function(L, "downloadFile", lua_dd_Downloader_downloadFile);
tolua_function(L, "downloadData", lua_dd_Downloader_downloadData);
tolua_function(L, "registCallBack", lua_dd_Downloader_registCallBack);
tolua_endmodule(L);
}
int register_user_manual(lua_State* L)
{
tolua_open(L);
tolua_module(L, "dd",0);
tolua_beginmodule(L,"dd");
extendDownloader(L);
tolua_endmodule(L);
return 1;
}