Skip to content

Commit

Permalink
Update to support Defold 1.4.0 new JSON API
Browse files Browse the repository at this point in the history
  • Loading branch information
aglitchman committed Nov 7, 2022
1 parent 56bca60 commit 0e48c3b
Show file tree
Hide file tree
Showing 4 changed files with 13 additions and 33 deletions.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ You can check [here](https://radar.yandex.ru/yandex) the size of Yandex.Games au

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).**

**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.
**Note:** Use [version 0.8.1](https://github.com/indiesoftby/defold-yagames/releases/tag/0.8.1) for Defold 1.3.7 and older.

## Getting Started

Expand Down
2 changes: 1 addition & 1 deletion game.project
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ vsync = 0

[project]
title = yagames
version = 0.8.0
version = 0.9.0
developer = Indiesoft LLC
bundle_resources = example/bundle/
dependencies#0 = https://github.com/subsoap/defos/archive/v2.5.0.zip
Expand Down
10 changes: 6 additions & 4 deletions yagames/lib/web/lib_yagames.js
Original file line number Diff line number Diff line change
Expand Up @@ -44,14 +44,16 @@ var LibYaGamesPrivate = {
{{{ makeDynCall("viif", "YaGamesPrivate._callback_number") }}}(cb_id, cmsg_id, message);
break;
case "string":
var msg = allocate(intArrayFromString(message), "i8", ALLOC_NORMAL);
{{{ makeDynCall("viii", "YaGamesPrivate._callback_string") }}}(cb_id, cmsg_id, msg);
var msg_arr = intArrayFromString(message, false);
var msg = allocate(msg_arr, "i8", ALLOC_NORMAL);
{{{ makeDynCall("viii", "YaGamesPrivate._callback_string") }}}(cb_id, cmsg_id, msg, msg_arr.length);
Module._free(msg);
break;
case "object":
var msg = JSON.stringify(message);
msg = allocate(intArrayFromString(msg), "i8", ALLOC_NORMAL);
{{{ makeDynCall("viii", "YaGamesPrivate._callback_object") }}}(cb_id, cmsg_id, msg);
var msg_arr = intArrayFromString(msg, false);
msg = allocate(msg_arr, "i8", ALLOC_NORMAL);
{{{ makeDynCall("viii", "YaGamesPrivate._callback_object") }}}(cb_id, cmsg_id, msg, msg_arr.length);
Module._free(msg);
break;
case "boolean":
Expand Down
32 changes: 5 additions & 27 deletions yagames/src/main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@

#if defined(DM_PLATFORM_HTML5)

typedef void (*ObjectMessage)(const int cb_id, const char* message_id, const char* message);
typedef void (*ObjectMessage)(const int cb_id, const char* message_id, const char* message, const int length);
typedef void (*NoMessage)(const int cb_id, const char* message_id);
typedef void (*NumberMessage)(const int cb_id, const char* message_id, float message);
typedef void (*BooleanMessage)(const int cb_id, const char* message_id, int message);
Expand Down Expand Up @@ -131,7 +131,7 @@ static bool CheckCallbackAndInstance(YaGamesPrivateListener* cbk)
return true;
}

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

dmJson::Document doc;
dmJson::Result r = dmJson::Parse(message, &doc);
if (r == dmJson::RESULT_OK && doc.m_NodeCount > 0)
{
char error_str_out[128];
if (dmScript::JsonToLua(L, &doc, 0, error_str_out, sizeof(error_str_out)) < 0)
{
dmLogError("Failed converting object JSON to Lua; %s", error_str_out);
is_fail = true;
}
}
else
{
dmLogError("Failed to parse JS object(%d): (%s)", r, message);
is_fail = true;
}
dmJson::Free(&doc);
if (is_fail)
{
lua_pop(L, 3);
assert(top == lua_gettop(L));
return;
}
dmScript::PCall(L, 4, 0);
}
assert(top == lua_gettop(L));
}
}

static void SendStringMessage(const int cb_id, const char* message_id, const char* message)
static void SendStringMessage(const int cb_id, const char* message_id, const char* message, const int length)
{
for (int i = m_Listeners.Size() - 1; i >= 0; --i)
{
Expand All @@ -202,7 +180,7 @@ static void SendStringMessage(const int cb_id, const char* message_id, const cha
{
lua_pushnil(L);
}
lua_pushstring(L, message);
lua_pushlstring(L, message, length);

dmScript::PCall(L, 4, 0);
}
Expand Down

0 comments on commit 0e48c3b

Please sign in to comment.