Skip to content

Commit 0e48c3b

Browse files
committed
Update to support Defold 1.4.0 new JSON API
1 parent 56bca60 commit 0e48c3b

File tree

4 files changed

+13
-33
lines changed

4 files changed

+13
-33
lines changed

README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ You can check [here](https://radar.yandex.ru/yandex) the size of Yandex.Games au
1212

1313
You can use it in your own project by adding this project as a [Defold library dependency](http://www.defold.com/manuals/libraries/). Open your `game.project` file and in the dependencies field add **a link to the ZIP file of a [specific release](https://github.com/indiesoftby/defold-yagames/tags).**
1414

15-
**Note:** Use [version 0.2.0](https://github.com/indiesoftby/defold-yagames/archive/0.2.0.zip) for the old Defold 1.2.177 or lower.
15+
**Note:** Use [version 0.8.1](https://github.com/indiesoftby/defold-yagames/releases/tag/0.8.1) for Defold 1.3.7 and older.
1616

1717
## Getting Started
1818

game.project

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ vsync = 0
1212

1313
[project]
1414
title = yagames
15-
version = 0.8.0
15+
version = 0.9.0
1616
developer = Indiesoft LLC
1717
bundle_resources = example/bundle/
1818
dependencies#0 = https://github.com/subsoap/defos/archive/v2.5.0.zip

yagames/lib/web/lib_yagames.js

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -44,14 +44,16 @@ var LibYaGamesPrivate = {
4444
{{{ makeDynCall("viif", "YaGamesPrivate._callback_number") }}}(cb_id, cmsg_id, message);
4545
break;
4646
case "string":
47-
var msg = allocate(intArrayFromString(message), "i8", ALLOC_NORMAL);
48-
{{{ makeDynCall("viii", "YaGamesPrivate._callback_string") }}}(cb_id, cmsg_id, msg);
47+
var msg_arr = intArrayFromString(message, false);
48+
var msg = allocate(msg_arr, "i8", ALLOC_NORMAL);
49+
{{{ makeDynCall("viii", "YaGamesPrivate._callback_string") }}}(cb_id, cmsg_id, msg, msg_arr.length);
4950
Module._free(msg);
5051
break;
5152
case "object":
5253
var msg = JSON.stringify(message);
53-
msg = allocate(intArrayFromString(msg), "i8", ALLOC_NORMAL);
54-
{{{ makeDynCall("viii", "YaGamesPrivate._callback_object") }}}(cb_id, cmsg_id, msg);
54+
var msg_arr = intArrayFromString(msg, false);
55+
msg = allocate(msg_arr, "i8", ALLOC_NORMAL);
56+
{{{ makeDynCall("viii", "YaGamesPrivate._callback_object") }}}(cb_id, cmsg_id, msg, msg_arr.length);
5557
Module._free(msg);
5658
break;
5759
case "boolean":

yagames/src/main.cpp

Lines changed: 5 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66

77
#if defined(DM_PLATFORM_HTML5)
88

9-
typedef void (*ObjectMessage)(const int cb_id, const char* message_id, const char* message);
9+
typedef void (*ObjectMessage)(const int cb_id, const char* message_id, const char* message, const int length);
1010
typedef void (*NoMessage)(const int cb_id, const char* message_id);
1111
typedef void (*NumberMessage)(const int cb_id, const char* message_id, float message);
1212
typedef void (*BooleanMessage)(const int cb_id, const char* message_id, int message);
@@ -131,7 +131,7 @@ static bool CheckCallbackAndInstance(YaGamesPrivateListener* cbk)
131131
return true;
132132
}
133133

134-
static void SendObjectMessage(const int cb_id, const char* message_id, const char* message)
134+
static void SendObjectMessage(const int cb_id, const char* message_id, const char* message, const int length)
135135
{
136136
for (int i = m_Listeners.Size() - 1; i >= 0; --i)
137137
{
@@ -150,37 +150,15 @@ static void SendObjectMessage(const int cb_id, const char* message_id, const cha
150150
{
151151
lua_pushnil(L);
152152
}
153+
dmScript::JsonToLua(L, message, length); // throws lua error if it fails
153154

154-
dmJson::Document doc;
155-
dmJson::Result r = dmJson::Parse(message, &doc);
156-
if (r == dmJson::RESULT_OK && doc.m_NodeCount > 0)
157-
{
158-
char error_str_out[128];
159-
if (dmScript::JsonToLua(L, &doc, 0, error_str_out, sizeof(error_str_out)) < 0)
160-
{
161-
dmLogError("Failed converting object JSON to Lua; %s", error_str_out);
162-
is_fail = true;
163-
}
164-
}
165-
else
166-
{
167-
dmLogError("Failed to parse JS object(%d): (%s)", r, message);
168-
is_fail = true;
169-
}
170-
dmJson::Free(&doc);
171-
if (is_fail)
172-
{
173-
lua_pop(L, 3);
174-
assert(top == lua_gettop(L));
175-
return;
176-
}
177155
dmScript::PCall(L, 4, 0);
178156
}
179157
assert(top == lua_gettop(L));
180158
}
181159
}
182160

183-
static void SendStringMessage(const int cb_id, const char* message_id, const char* message)
161+
static void SendStringMessage(const int cb_id, const char* message_id, const char* message, const int length)
184162
{
185163
for (int i = m_Listeners.Size() - 1; i >= 0; --i)
186164
{
@@ -202,7 +180,7 @@ static void SendStringMessage(const int cb_id, const char* message_id, const cha
202180
{
203181
lua_pushnil(L);
204182
}
205-
lua_pushstring(L, message);
183+
lua_pushlstring(L, message, length);
206184

207185
dmScript::PCall(L, 4, 0);
208186
}

0 commit comments

Comments
 (0)