Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 3 additions & 1 deletion .github/workflows/main.yml
Original file line number Diff line number Diff line change
Expand Up @@ -41,8 +41,10 @@ jobs:
if [ "${BOB_LOCAL_SHA1}" != "${BOB_SHA1}" ]; then wget --progress=dot:mega -O build/bundle/bob.jar "https://d.defold.com/archive/${BOB_SHA1}/bob/bob.jar"; fi
java -jar build/bundle/bob.jar --version

TITLE=$(awk -F "=" '/^title/ {gsub(/[ \r\n\t]/, "", $2); print $2}' game.project)

java -jar build/bundle/bob.jar --email [email protected] --auth 12345 --texture-compression true --bundle-output build/bundle/js-web --platform js-web --architectures wasm-web --archive --variant release resolve build bundle
(cd build/bundle/js-web/* && zip -r ../../public/demo_no-sw_no-native-cache.zip .)
(cd build/bundle/js-web/${TITLE} && zip -r ../../public/demo_no-sw_no-native-cache.zip .)

- name: Upload Result
uses: actions/upload-artifact@v4
Expand Down
2 changes: 1 addition & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -11,4 +11,4 @@ builtins
/.vscode
/editor-script-lua-format
/doc

/.editor_settings
5 changes: 3 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -292,8 +292,9 @@ And it's also a good idea to upload a demo build of YaGames to your game's draft
| - `safeStorage.key(n)` | `yagames.storage_key(n)`<br>Returns the name of the nth key in the storage or `nil`. *Note: the n index is zero-based.* |
| - `safeStorage.length` | `yagames.storage_length()`<br>Returns the number of data items stored in the storage. |
| **Events** [(docs)](https://yandex.ru/dev/games/doc/en/sdk/sdk-events) | |
| `ysdk.onEvent(eventName, listener)` | `yagames.event_on(event_name, listener)` |
| `ysdk.dispatchEvent(eventName)` | `yagames.event_dispatch(event_name)` |
| `ysdk.on(eventName, listener)` | `yagames.event_on(event_name, listener)`<br>`event_name` is a string: `game_api_pause`, `game_api_resume`, `HISTORY_BACK` etc. |
| `ysdk.off(eventName, listener)` | `yagames.event_off(event_name, listener)`<br>`event_name` is a string: `game_api_pause`, `game_api_resume`, `HISTORY_BACK` etc. |
| `ysdk.dispatchEvent(eventName)` | `yagames.event_dispatch(event_name)`<br>`event_name` is a string: `EXIT` etc. |
| **Remote Config** [(docs)](https://yandex.ru/dev/games/doc/en/sdk/sdk-config) | |
| `ysdk.getFlags(options)` | `yagames.flags_get(options, callback)`<br>Options is optional. The callback result is a table like `{ flagName = "value" }` |
| **Sitelock** [(docs)](#sitelock) | |
Expand Down
17 changes: 13 additions & 4 deletions build_example.sh
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
#!/bin/bash

# Requirements: awk, curl, java, jq, perl, wget, zip
# Usage: ./build_example.sh [PORT], where PORT is optional.

# Immediately exit if any command has a non-zero exit status:
set -e
Expand All @@ -9,14 +10,22 @@ set -e
sed -i 's/service_worker_url = .*//' game.project
sed -i 's/manifest_url = .*//' game.project

mkdir -p build
mkdir -p build/bundle

BOB_SHA1=$(curl -s 'https://d.defold.com/stable/info.json' | jq -r .sha1)
BOB_SHA1=${BOB_SHA1:-$(curl -s 'https://d.defold.com/stable/info.json' | jq -r .sha1)}
BOB_LOCAL_SHA1=$((java -jar build/bob.jar --version | cut -d' ' -f6) || true)
if [ "${BOB_LOCAL_SHA1}" != "${BOB_SHA1}" ]; then wget --progress=dot:mega -O build/bob.jar "https://d.defold.com/archive/${BOB_SHA1}/bob/bob.jar"; fi
TITLE=$(awk -F "=" '/^title/ {gsub(/[ \r\n\t]/, "", $2); print $2}' game.project)
SETTINGS="--build-server https://build.defold.com --variant debug --email [email protected] --auth 12345 --texture-compression true"
PLATFORM=js-web
java -jar build/bob.jar ${SETTINGS} --bundle-output build/bundle/${PLATFORM} --platform ${PLATFORM} --architectures wasm-web --archive resolve build bundle
ARCHITECTURES=wasm-web
java -jar build/bob.jar ${SETTINGS} --bundle-output build/bundle/${PLATFORM} --platform ${PLATFORM} --architectures ${ARCHITECTURES} --archive resolve build bundle
perl -pi -e "s/cachePrefix \+ \"-v1\"/cachePrefix + \"-v$(date +%s)\"/g" "build/bundle/${PLATFORM}/${TITLE}/sw.js"
(cd "build/bundle/${PLATFORM}/${TITLE}" && rm -f "../../bundle_${PLATFORM}.zip" && zip -r "../../bundle_${PLATFORM}.zip" .)

if [ $# -eq 1 ]; then
PORT=$1
echo "Starting http-server on port ${PORT}..."
npx @yandex-games/sdk-dev-proxy -p "build/bundle/${PLATFORM}/${TITLE}" --port ${PORT}
else
(cd "build/bundle/${PLATFORM}/${TITLE}" && rm -f "../../bundle_${PLATFORM}.zip" && zip -r "../../bundle_${PLATFORM}.zip" .)
fi
20 changes: 18 additions & 2 deletions example/ysdkdebug/playground.gui_script
Original file line number Diff line number Diff line change
Expand Up @@ -32,10 +32,26 @@ local function log_update(self)
end

local function event_test(self)
yagames.event_on("HISTORY_BACK", function (self)
print("yagames.event_on(\"HISTORY_BACK\")")
-- Example of adding an event listener for "game_api_pause" event:
yagames.event_on("game_api_pause", function (self)
print("yagames.event_on(\"game_api_pause\")")
end)

-- Example of adding an event listener for "game_api_resume" event:
local cb = function (self)
print("yagames.event_on(\"game_api_resume\")")
end
yagames.event_on("game_api_resume", cb)

-- Example of removing an event listener:
-- yagames.event_off("game_api_resume", cb)

-- Example of adding an event listener for "HISTORY_BACK" event (used on the TV platform only):
-- yagames.event_on("HISTORY_BACK", function (self)
-- print("yagames.event_on(\"HISTORY_BACK\")")
-- end)

-- Example of dispatching an event:
-- yagames.event_dispatch("EXIT")
end

Expand Down
4 changes: 2 additions & 2 deletions game.project
Original file line number Diff line number Diff line change
Expand Up @@ -11,10 +11,10 @@ high_dpi = 1

[project]
title = yagames
version = 0.16.0
version = 0.17.0
developer = Indiesoft LLC
bundle_resources = example/bundle/
dependencies#0 = https://github.com/subsoap/defos/archive/v2.8.1.zip
dependencies#0 = https://github.com/subsoap/defos/archive/v2.8.3.zip
dependencies#1 = https://github.com/Insality/druid/archive/0.6.0.zip
dependencies#2 = https://github.com/britzl/ludobits/archive/7.1.0.zip

Expand Down
58 changes: 52 additions & 6 deletions yagames/helpers/mock.lua
Original file line number Diff line number Diff line change
Expand Up @@ -9,13 +9,13 @@ local NO_ERR = nil
--
--

function M.send(cb_id, message_id, message)
timer.delay(0.001, false, function(self)
function M.send(cb_id, arg1, arg2)
timer.delay(0, false, function(self)
local count = #M.listeners
for i = count, 1, -1 do
local listener = M.listeners[i]
if listener.only_id == cb_id then
listener.func(self, cb_id, message_id, message)
listener.func(self, cb_id, arg1, arg2)
end
end
end)
Expand All @@ -25,17 +25,42 @@ function M.add_listener(cb_id, listener)
table.insert(M.listeners, {only_id = cb_id, func = listener})
end

function M.remove_listener(listener)
function M.remove_listener(listener_fn)
local count = #M.listeners
for i = count, 1, -1 do
local listener = M.listeners[i]
if listener.func == listener then
if listener.func == listener_fn then
table.remove(M.listeners, i)
return listener.only_id
end
end
return nil
end

local function dispatch_event(event_name, arg1, arg2)
local count = #M.listeners
for i = count, 1, -1 do
local listener = M.listeners[i]
if listener.event_name == event_name then
M.send(listener.only_id, arg1, arg2)
break
end
end
end

local function sequence_calls(...)
local args = {...}
local handle
handle = timer.delay(0, true, function(self)
if #args > 0 then
local func = table.remove(args, 1)
func(self)
else
timer.cancel(handle)
end
end)
end

--
-- Yandex Games SDK
--
Expand Down Expand Up @@ -142,7 +167,15 @@ function M.show_fullscreen_adv(cb_id)
end

function M.show_rewarded_video(cb_id)
M.send(cb_id, "close")
sequence_calls(function(self)
dispatch_event("game_api_pause", NO_ERR)
end, function(self)
-- Uncomment this to receive "rewarded" event.
-- M.send(cb_id, "rewarded")
end, function(self)
M.send(cb_id, "close")
dispatch_event("game_api_resume", NO_ERR)
end)
end

function M.adv_get_banner_adv_status(cb_id)
Expand Down Expand Up @@ -599,9 +632,22 @@ function M.storage_length()
end

function M.event_dispatch(event_name)
-- No need to do anything here.
end

function M.event_on(event_name, cb_id)
-- Add event name to the listener to be able to find it later.
local count = #M.listeners
for i = count, 1, -1 do
local listener = M.listeners[i]
if listener.only_id == cb_id then
listener.event_name = event_name
end
end
end

function M.event_off(event_name, cb_id)
-- No need to do anything here.
end

function M.get_flags(cb_id, options)
Expand Down
34 changes: 25 additions & 9 deletions yagames/lib/web/lib_yagames.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,8 @@ var LibYaGamesPrivate = {
_callback_number: null,
_callback_bool: null,

_listeners: {},

toErrStr: function (err) {
return err + "";
},
Expand Down Expand Up @@ -93,6 +95,7 @@ var LibYaGamesPrivate = {
self._callback_empty = callback_empty;
self._callback_number = callback_number;
self._callback_bool = callback_bool;
self._listeners = {};

while (typeof YaGamesPrivate_MsgQueue !== "undefined" && YaGamesPrivate_MsgQueue.length) {
var m = YaGamesPrivate_MsgQueue.shift();
Expand All @@ -108,6 +111,7 @@ var LibYaGamesPrivate = {
self._callback_empty = null;
self._callback_number = null;
self._callback_bool = null;
self._listeners = {};
},

YaGamesPrivate_IsAvailableMethod: function (cb_id, cname) {
Expand Down Expand Up @@ -965,18 +969,20 @@ var LibYaGamesPrivate = {
},

YaGamesPrivate_Event_Dispatch: function (cevent_name) {
var self = YaGamesPrivate;
var event_name = UTF8ToString(cevent_name);
const self = YaGamesPrivate;
const event_name = UTF8ToString(cevent_name);
self._ysdk.dispatchEvent(self._ysdk.EVENTS[event_name]);
},

YaGamesPrivate_Event_On: function (cevent_name, cb_id) {
var self = YaGamesPrivate;
var event_name = UTF8ToString(cevent_name);
const self = YaGamesPrivate;
const event_name = UTF8ToString(cevent_name);
try {
self._ysdk.onEvent(self._ysdk.EVENTS[event_name], () => {
const cb = () => {
self.send(cb_id, null);
});
};
self._listeners[cb_id] = cb;
self._ysdk.on(event_name, cb);

// Uncomment to test the behaviour:
// setInterval(function() { self.send(cb_id, null); }, 3000);
Expand All @@ -985,10 +991,20 @@ var LibYaGamesPrivate = {
}
},

YaGamesPrivate_Event_Off: function (cevent_name, cb_id) {
const self = YaGamesPrivate;
const event_name = UTF8ToString(cevent_name);
if (!self._listeners[cb_id]) {
return;
}
self._ysdk.off(event_name, self._listeners[cb_id]);
delete self._listeners[cb_id];
},

YaGamesPrivate_GetFlags: function (cb_id, coptions) {
var self = YaGamesPrivate;
const self = YaGamesPrivate;
try {
var options = coptions === 0 ? {} : self.parseJson(UTF8ToString(coptions));
const options = coptions === 0 ? {} : self.parseJson(UTF8ToString(coptions));
self._ysdk
.getFlags(options)
.then((flags) => {
Expand All @@ -998,7 +1014,7 @@ var LibYaGamesPrivate = {
self.send(cb_id, self.toErrStr(err));
});
} catch (err) {

self.delaySend(cb_id, self.toErrStr(err));
}
}
};
Expand Down
28 changes: 24 additions & 4 deletions yagames/src/main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -80,6 +80,7 @@ extern "C"
const int YaGamesPrivate_Storage_Length();
void YaGamesPrivate_Event_Dispatch(const char* cevent_name);
void YaGamesPrivate_Event_On(const char* cevent_name, const int cb_id);
void YaGamesPrivate_Event_Off(const char* cevent_name, const int cb_id);
void YaGamesPrivate_GetFlags(const int cb_id, const char* coptions);
}

Expand All @@ -97,7 +98,7 @@ struct YaGamesPrivateListener
int m_OnlyId;
};

static void UnregisterCallback(lua_State* L, YaGamesPrivateListener* cbk);
static int UnregisterCallback(lua_State* L, YaGamesPrivateListener* cbk);
static int GetEqualIndexOfListener(lua_State* L, YaGamesPrivateListener* cbk);

static dmArray<YaGamesPrivateListener> m_Listeners;
Expand Down Expand Up @@ -276,6 +277,7 @@ static void SendBoolMessage(const int cb_id, const char* message_id, int message
}
}

// WARNING: Sets m_OnlyId of `cbk` if the callback is found.
static int GetEqualIndexOfListener(lua_State* L, YaGamesPrivateListener* cbk)
{
lua_rawgeti(L, LUA_REGISTRYINDEX, cbk->m_Callback);
Expand All @@ -293,6 +295,7 @@ static int GetEqualIndexOfListener(lua_State* L, YaGamesPrivateListener* cbk)
if (lua_equal(L, second, second + 1))
{
lua_pop(L, 3);
cbk->m_OnlyId = cb->m_OnlyId;
return i;
}
lua_pop(L, 2);
Expand All @@ -306,7 +309,7 @@ static int GetEqualIndexOfListener(lua_State* L, YaGamesPrivateListener* cbk)
return -1;
}

static void UnregisterCallback(lua_State* L, YaGamesPrivateListener* cbk)
static int UnregisterCallback(lua_State* L, YaGamesPrivateListener* cbk)
{
int index = GetEqualIndexOfListener(L, cbk);
if (index >= 0)
Expand All @@ -322,11 +325,13 @@ static void UnregisterCallback(lua_State* L, YaGamesPrivateListener* cbk)
{
YaGamesPrivate_RemoveCallbacks();
}
return cbk->m_OnlyId;
}
else
{
dmLogError("Can't remove a callback that didn't not register.");
}
return -1;
}

static int AddListener(lua_State* L)
Expand Down Expand Up @@ -382,8 +387,16 @@ static int RemoveListener(lua_State* L)
dmScript::GetInstance(L);
cbk.m_Self = dmScript::Ref(L, LUA_REGISTRYINDEX);

UnregisterCallback(L, &cbk);
return 0;
int only_id = UnregisterCallback(L, &cbk);
if (only_id != -1)
{
lua_pushinteger(L, only_id);
}
else
{
lua_pushnil(L);
}
return 1;
}

//
Expand Down Expand Up @@ -837,6 +850,12 @@ static int Event_On(lua_State* L)
return 0;
}

static int Event_Off(lua_State* L)
{
YaGamesPrivate_Event_Off(luaL_checkstring(L, 1), luaL_checkint(L, 2));
return 0;
}

//
// Flags
//
Expand Down Expand Up @@ -931,6 +950,7 @@ static const luaL_reg Module_methods[] = {
// - Events
{ "event_dispatch", Event_Dispatch },
{ "event_on", Event_On },
{ "event_off", Event_Off },
// - Config
{ "get_flags", GetFlags },
{ 0, 0 }
Expand Down
Loading