Skip to content

Commit

Permalink
review fixes
Browse files Browse the repository at this point in the history
  • Loading branch information
AGulev committed Jan 30, 2024
1 parent 13ccdf5 commit 2642282
Show file tree
Hide file tree
Showing 3 changed files with 16 additions and 29 deletions.
22 changes: 8 additions & 14 deletions defold-spine/src/comp_spine_model.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -299,9 +299,9 @@ namespace dmSpine

static void ClearCompletionCallback(SpineAnimationTrack* track)
{
if (track->m_AnimationInstance && track->m_CallbackInfo)
if (track->m_CallbackInfo)
{
DestroyCallback(track->m_CallbackInfo);
dmScript::DestroyCallback(track->m_CallbackInfo);
track->m_CallbackInfo = 0x0;
}
}
Expand Down Expand Up @@ -422,7 +422,10 @@ namespace dmSpine
{
uint32_t id = track.m_CallbackId;
RunTrackCallback(track.m_CallbackInfo, dmGameSystemDDF::SpineAnimationDone::m_DDFDescriptor, (const char*)&message, &sender);
// remove after usage only if it's the same callback
// If, in a Lua callback, the user calls spine.play_anim(),
// it will destroy the current callback and create a new one (if specified).
// Therefore, we need to check whether we are going to remove the same callback
// that we are running. If not, it has already been removed.
if (id == track.m_CallbackId)
{
ClearCompletionCallback(&track);
Expand Down Expand Up @@ -984,16 +987,7 @@ namespace dmSpine
component->m_ReHash = 1;
}

static uint32_t GetAndIncreaseCallbackId(uint32_t id)
{
if (id == 0xffffffff)
{
return 0;
}
return ++id;
}

bool CompSpineModelPlayAnimation(SpineModelComponent* component, dmGameSystemDDF::SpinePlayAnimation* message, dmMessage::URL* sender, void* callback_info, lua_State* L)
bool CompSpineModelPlayAnimation(SpineModelComponent* component, dmGameSystemDDF::SpinePlayAnimation* message, dmMessage::URL* sender, dmScript::LuaCallbackInfo* callback_info, lua_State* L)
{
bool result = PlayAnimation(component, message->m_AnimationId, (dmGameObject::Playback)message->m_Playback, message->m_BlendDuration,
message->m_Offset, message->m_PlaybackRate, message->m_Track - 1);
Expand All @@ -1002,7 +996,7 @@ namespace dmSpine
SpineAnimationTrack& track = component->m_AnimationTracks[message->m_Track - 1];
track.m_Listener = *sender;
track.m_Context = L;
track.m_CallbackId = GetAndIncreaseCallbackId(track.m_CallbackId);
track.m_CallbackId++;
track.m_CallbackInfo = callback_info;
}
return result;
Expand Down
8 changes: 4 additions & 4 deletions defold-spine/src/comp_spine_model.h
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@
#include <dmsdk/gameobject/gameobject.h>
#include <dmsdk/gamesys/render_constants.h>
#include <dmsdk/render/render.h>
#include <dmsdk/gamesys/script.h>
// The engine ddf formats aren't stored in the "dmsdk" folder (yet)
#include <gamesys/gamesys_ddf.h>

Expand All @@ -45,7 +46,7 @@ namespace dmSpine
dmMessage::URL m_Listener;
lua_State* m_Context;

void* m_CallbackInfo;
dmScript::LuaCallbackInfo* m_CallbackInfo;
uint32_t m_CallbackId;
};

Expand Down Expand Up @@ -84,7 +85,7 @@ namespace dmSpine
};

// For scripting
bool CompSpineModelPlayAnimation(SpineModelComponent* component, dmGameSystemDDF::SpinePlayAnimation* message, dmMessage::URL* sender, void* callback_info, lua_State* L);
bool CompSpineModelPlayAnimation(SpineModelComponent* component, dmGameSystemDDF::SpinePlayAnimation* message, dmMessage::URL* sender, dmScript::LuaCallbackInfo* callback_info, lua_State* L);
bool CompSpineModelCancelAnimation(SpineModelComponent* component, dmGameSystemDDF::SpineCancelAnimation* message);

bool CompSpineModelSetConstant(SpineModelComponent* component, dmGameSystemDDF::SetConstant* message);
Expand All @@ -99,8 +100,7 @@ namespace dmSpine

bool CompSpineModelGetBone(SpineModelComponent* component, dmhash_t bone_name, dmhash_t* instance_id);

void DestroyCallback(void* callback_data);
void RunTrackCallback(void* callback_data, const dmDDF::Descriptor* desc, const char* data, const dmMessage::URL* sender);
void RunTrackCallback(dmScript::LuaCallbackInfo* callback_data, const dmDDF::Descriptor* desc, const char* data, const dmMessage::URL* sender);

}

Expand Down
15 changes: 4 additions & 11 deletions defold-spine/src/script_spine.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@
#include <dmsdk/dlib/message.h>
#include <dmsdk/dlib/vmath.h>
#include <dmsdk/gameobject/script.h>
#include <dmsdk/gamesys/script.h>

// The engine ddf formats aren't stored in the "dmsdk" folder (yet)
#include <gamesys/gamesys_ddf.h>
Expand All @@ -22,6 +21,7 @@
namespace dmScript
{
bool GetURL(lua_State* L, dmMessage::URL* out_url);
void PushURL(lua_State* L, const dmMessage::URL& m);
}

namespace dmSpine
Expand Down Expand Up @@ -339,15 +339,8 @@ namespace dmSpine
return 0;
}

void DestroyCallback(void* callback_data)
void RunTrackCallback(dmScript::LuaCallbackInfo* cbk, const dmDDF::Descriptor* desc, const char* data, const dmMessage::URL* sender)
{
dmScript::LuaCallbackInfo* cbk = (dmScript::LuaCallbackInfo*)callback_data;
dmScript::DestroyCallback(cbk);
}

void RunTrackCallback(void* callback_data, const dmDDF::Descriptor* desc, const char* data, const dmMessage::URL* sender)
{
dmScript::LuaCallbackInfo* cbk = (dmScript::LuaCallbackInfo*)callback_data;
if (!dmScript::IsCallbackValid(cbk))
{
dmLogError("Spine models callback is invalid.");
Expand All @@ -363,8 +356,8 @@ namespace dmSpine
}
lua_pushstring(L, desc->m_Name);
dmScript::PushDDF(L, desc, data, false);
// dmScript::PushURL(L, sender); // function unavaliable in dmsdk
int ret = dmScript::PCall(L, 3, 0);
dmScript::PushURL(L, *sender);
int ret = dmScript::PCall(L, 4, 0);
(void)ret;
dmScript::TeardownCallback(cbk);
}
Expand Down

0 comments on commit 2642282

Please sign in to comment.