Skip to content

Commit

Permalink
Remove callback using unityads.setCallback() or unityads.setCallback(…
Browse files Browse the repository at this point in the history
…nil)

Will not run DefUnityAds callback when the instance has been deleted.
  • Loading branch information
AGulev committed May 23, 2018
1 parent 7d16d7d commit 21e26b8
Show file tree
Hide file tree
Showing 5 changed files with 419 additions and 45 deletions.
4 changes: 3 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ android:theme="@android:style/Theme.NoTitleBar.Fullscreen" />

See the [example folder](https://github.com/AGulev/DefUnityAds/tree/master/example) for understand how to use extension. Especially [ui.gui_script](https://github.com/AGulev/DefUnityAds/blob/master/example/ui.gui_script) file.

![Example screenshot](https://cdn.rawgit.com/AGulev/DefUnityAds/2f17110e/example/example_screenshot.png)
![Example screenshot](https://user-images.githubusercontent.com/2209596/40448649-cf08e002-5ede-11e8-9681-c20d17c0512a.jpg)


## LUA Api
Expand All @@ -54,6 +54,8 @@ unityads.initialize("1401815", defunityads_callback, true) -- testMode is option
[original doc](https://github.com/Unity-Technologies/unity-ads-ios/wiki/sdk_android_api_reference#unityadssetlistener)
```lua
unityads.setCallback(defunityads_callback) -- set callback
unityads.setCallback(nil) -- remove callback
unityads.setCallback() -- remove callback
```


Expand Down
59 changes: 30 additions & 29 deletions defunityads/src/DefUnityCallback.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -5,18 +5,6 @@
DefUnityAdsListener defUtoLua;
dmArray<CallbackData> m_callbacksQueue;

static void ClearQueue()
{
for(uint32_t i = 0; i != m_callbacksQueue.Size(); ++i)
{
CallbackData* data = &m_callbacksQueue[i];
if(data->value_1)
free(data->value_1);
data->value_1 = 0;
m_callbacksQueue.EraseSwap(i--);
}
}

static void RegisterCallback(lua_State* L, int index, DefUnityAdsListener* cbk)
{
if(cbk->m_Callback != LUA_NOREF)
Expand Down Expand Up @@ -49,6 +37,7 @@ static void invoke_callback(int type, char*key_1, char*value_1, char*key_2, int
{
if(cbk->m_Callback == LUA_NOREF)
{
dmLogInfo("DefUnityAds callback do not exist.");
return;
}

Expand All @@ -59,33 +48,45 @@ static void invoke_callback(int type, char*key_1, char*value_1, char*key_2, int
lua_pushvalue(L, -1);
dmScript::SetInstance(L);

lua_pushnumber(L, type);
int count_table_elements = 1;
if (key_2 != NULL) {
count_table_elements = 2;
if (!dmScript::IsInstanceValid(L)) {
UnregisterCallback(&defUtoLua);
dmLogError("Could not run DefUnityAds callback because the instance has been deleted.");
lua_pop(L, 2);
}
lua_createtable(L, 0, count_table_elements);
luaL_push_pair_str_str(L, key_1, value_1);
if (key_2 != NULL) {
luaL_push_pair_str_num(L, key_2, value_2);
}

int number_of_arguments = 3;
int ret = lua_pcall(L, number_of_arguments, 0, 0);
if(ret != 0) {
dmLogError("Error running callback: %s", lua_tostring(L, -1));
lua_pop(L, 1);
else {
lua_pushnumber(L, type);
int count_table_elements = 1;
if (key_2 != NULL) {
count_table_elements = 2;
}
lua_createtable(L, 0, count_table_elements);
luaL_push_pair_str_str(L, key_1, value_1);
if (key_2 != NULL) {
luaL_push_pair_str_num(L, key_2, value_2);
}

int number_of_arguments = 3;
int ret = lua_pcall(L, number_of_arguments, 0, 0);
if(ret != 0) {
dmLogError("Error running callback: %s", lua_tostring(L, -1));
lua_pop(L, 1);
}
}
assert(top == lua_gettop(L));
}

void finalize(){
UnregisterCallback(&defUtoLua);
ClearQueue();
}

void set_callback(lua_State* L, int pos){
RegisterCallback(L, pos, &defUtoLua);
int type = lua_type(L, pos);
if (type == LUA_TNONE || type == LUA_TNIL) {
UnregisterCallback(&defUtoLua);
}
else{
RegisterCallback(L, pos, &defUtoLua);
}
}

void add_to_queue(int type, char*key_1, char*value_1, char*key_2, int value_2){
Expand Down
Loading

0 comments on commit 21e26b8

Please sign in to comment.