| 2016 | }; |
| 2017 | |
| 2018 | Result RunScript(HScene scene, ScriptFunction script_function, int custom_ref, void* args) |
| 2019 | { |
| 2020 | DM_PROFILE(__FUNCTION__); |
| 2021 | |
| 2022 | if (scene->m_Script == 0x0) |
| 2023 | return RESULT_OK; |
| 2024 | |
| 2025 | lua_State* L = scene->m_Context->m_LuaState; |
| 2026 | int top = lua_gettop(L); |
| 2027 | (void)top; |
| 2028 | |
| 2029 | int lua_ref = scene->m_Script->m_FunctionReferences[script_function]; |
| 2030 | if (custom_ref != LUA_NOREF) { |
| 2031 | lua_ref = custom_ref; |
| 2032 | } |
| 2033 | |
| 2034 | if (lua_ref != LUA_NOREF) |
| 2035 | { |
| 2036 | lua_rawgeti(L, LUA_REGISTRYINDEX, scene->m_InstanceReference); |
| 2037 | dmScript::SetInstance(L); |
| 2038 | |
| 2039 | if (custom_ref != LUA_NOREF) { |
| 2040 | dmScript::ResolveInInstance(L, custom_ref); |
| 2041 | if (!lua_isfunction(L, -1)) |
| 2042 | { |
| 2043 | // If the script instance is dead we just ignore the callback |
| 2044 | lua_pop(L, 1); |
| 2045 | lua_pushnil(L); |
| 2046 | dmScript::SetInstance(L); |
| 2047 | dmLogWarning("Failed to call message response callback function, has it been deleted?"); |
| 2048 | return RESULT_OK; |
| 2049 | } |
| 2050 | dmScript::UnrefInInstance(L, custom_ref); |
| 2051 | } |
| 2052 | else |
| 2053 | { |
| 2054 | lua_rawgeti(L, LUA_REGISTRYINDEX, lua_ref); |
| 2055 | } |
| 2056 | |
| 2057 | assert(lua_isfunction(L, -1)); |
| 2058 | |
| 2059 | lua_rawgeti(L, LUA_REGISTRYINDEX, scene->m_InstanceReference); |
| 2060 | |
| 2061 | uint32_t arg_count = 1; |
| 2062 | uint32_t ret_count = 0; |
| 2063 | |
| 2064 | const char* message_name = 0; |
| 2065 | switch (script_function) |
| 2066 | { |
| 2067 | case SCRIPT_FUNCTION_UPDATE: |
| 2068 | { |
| 2069 | float* dt = (float*)args; |
| 2070 | lua_pushnumber(L, (lua_Number) *dt); |
| 2071 | arg_count += 1; |
| 2072 | } |
| 2073 | break; |
| 2074 | case SCRIPT_FUNCTION_ONMESSAGE: |
| 2075 | { |
no test coverage detected