| 2304 | } |
| 2305 | |
| 2306 | static bool LoadScript(lua_State* L, dmLuaDDF::LuaSource *source, Script* script) |
| 2307 | { |
| 2308 | for (uint32_t i = 0; i < MAX_SCRIPT_FUNCTION_COUNT; ++i) |
| 2309 | script->m_FunctionReferences[i] = LUA_NOREF; |
| 2310 | |
| 2311 | bool result = false; |
| 2312 | int top = lua_gettop(L); |
| 2313 | (void) top; |
| 2314 | |
| 2315 | int ret = dmScript::LuaLoad(L, source); |
| 2316 | if (ret == 0) |
| 2317 | { |
| 2318 | lua_rawgeti(L, LUA_REGISTRYINDEX, script->m_InstanceReference); |
| 2319 | dmScript::SetInstance(L); |
| 2320 | |
| 2321 | ret = dmScript::PCall(L, 0, 0); |
| 2322 | if (ret == 0) |
| 2323 | { |
| 2324 | for (uint32_t i = 0; i < MAX_SCRIPT_FUNCTION_COUNT; ++i) |
| 2325 | { |
| 2326 | lua_getglobal(L, SCRIPT_FUNCTION_NAMES[i]); |
| 2327 | if (lua_isnil(L, -1) == 0) |
| 2328 | { |
| 2329 | if (lua_type(L, -1) == LUA_TFUNCTION) |
| 2330 | { |
| 2331 | script->m_FunctionReferences[i] = dmScript::Ref(L, LUA_REGISTRYINDEX); |
| 2332 | } |
| 2333 | else |
| 2334 | { |
| 2335 | dmLogError("The global name '%s' in '%s' must be a function.", SCRIPT_FUNCTION_NAMES[i], source->m_Filename); |
| 2336 | lua_pop(L, 1); |
| 2337 | goto bail; |
| 2338 | } |
| 2339 | } |
| 2340 | else |
| 2341 | { |
| 2342 | script->m_FunctionReferences[i] = LUA_NOREF; |
| 2343 | lua_pop(L, 1); |
| 2344 | } |
| 2345 | } |
| 2346 | result = true; |
| 2347 | } |
| 2348 | lua_pushnil(L); |
| 2349 | dmScript::SetInstance(L); |
| 2350 | } else { |
| 2351 | dmLogError("Error running script: %s", lua_tostring(L,-1)); |
| 2352 | lua_pop(L, 1); |
| 2353 | } |
| 2354 | bail: |
| 2355 | for (uint32_t i = 0; i < MAX_SCRIPT_FUNCTION_COUNT; ++i) |
| 2356 | { |
| 2357 | lua_pushnil(L); |
| 2358 | lua_setglobal(L, SCRIPT_FUNCTION_NAMES[i]); |
| 2359 | } |
| 2360 | assert(top == lua_gettop(L)); |
| 2361 | return result; |
| 2362 | } |
| 2363 |
no test coverage detected