| 102 | }; |
| 103 | |
| 104 | ScriptResult RunScript(lua_State* L, HScript script, ScriptFunction script_function, HScriptInstance script_instance, const RunScriptParams& params) |
| 105 | { |
| 106 | DM_PROFILE("RunScript"); |
| 107 | |
| 108 | ScriptResult result = SCRIPT_RESULT_OK; |
| 109 | |
| 110 | if (script->m_FunctionReferences[script_function] != LUA_NOREF) |
| 111 | { |
| 112 | int top = lua_gettop(L); |
| 113 | (void) top; |
| 114 | |
| 115 | lua_rawgeti(L, LUA_REGISTRYINDEX, script_instance->m_InstanceReference); |
| 116 | dmScript::SetInstance(L); |
| 117 | |
| 118 | lua_rawgeti(L, LUA_REGISTRYINDEX, script->m_FunctionReferences[script_function]); |
| 119 | lua_rawgeti(L, LUA_REGISTRYINDEX, script_instance->m_InstanceReference); |
| 120 | |
| 121 | int arg_count = 1; |
| 122 | |
| 123 | if (script_function == SCRIPT_FUNCTION_INIT) |
| 124 | { |
| 125 | // Backwards compatibility |
| 126 | lua_rawgeti(L, LUA_REGISTRYINDEX, script_instance->m_InstanceReference); |
| 127 | ++arg_count; |
| 128 | } |
| 129 | if (script_function == SCRIPT_FUNCTION_UPDATE || script_function == SCRIPT_FUNCTION_FIXED_UPDATE || script_function == SCRIPT_FUNCTION_LATE_UPDATE) |
| 130 | { |
| 131 | lua_pushnumber(L, params.m_UpdateContext->m_DT); |
| 132 | ++arg_count; |
| 133 | } |
| 134 | |
| 135 | { |
| 136 | char buffer[128]; |
| 137 | const char* profiler_string = dmScript::GetProfilerString(L, 0, script->m_LuaModule->m_Source.m_Filename, SCRIPT_FUNCTION_NAMES[script_function], 0, buffer, sizeof(buffer)); |
| 138 | DM_PROFILE_DYN(profiler_string, 0); |
| 139 | |
| 140 | if (dmScript::PCall(L, arg_count, 0) != 0) |
| 141 | { |
| 142 | result = SCRIPT_RESULT_FAILED; |
| 143 | } |
| 144 | } |
| 145 | |
| 146 | lua_pushnil(L); |
| 147 | dmScript::SetInstance(L); |
| 148 | |
| 149 | assert(top == lua_gettop(L)); |
| 150 | } |
| 151 | |
| 152 | return result; |
| 153 | } |
| 154 | |
| 155 | CreateResult CompScriptDestroy(const ComponentDestroyParams& params) |
| 156 | { |
no test coverage detected