From 8bfefd356cf5efc1987bbc8c17bd44011278f6c6 Mon Sep 17 00:00:00 2001 From: Thomas Goyne Date: Mon, 9 Jan 2012 20:31:19 +0000 Subject: [PATCH] Return nil from lua functions which require a project context if they're called during script initialization. Closes #1380. Originally committed to SVN as r6255. --- aegisub/src/auto4_lua.cpp | 16 ++++++++++++---- 1 file changed, 12 insertions(+), 4 deletions(-) diff --git a/aegisub/src/auto4_lua.cpp b/aegisub/src/auto4_lua.cpp index eecdbd7cd..e3d0800b2 100644 --- a/aegisub/src/auto4_lua.cpp +++ b/aegisub/src/auto4_lua.cpp @@ -125,7 +125,10 @@ namespace { const agi::Context *get_context(lua_State *L) { lua_getfield(L, LUA_REGISTRYINDEX, "project_context"); - assert(lua_islightuserdata(L, -1)); + if (!lua_islightuserdata(L, -1)) { + lua_pop(L, 1); + return 0; + } const agi::Context * c = static_cast(lua_touserdata(L, -1)); lua_pop(L, 1); return c; @@ -458,7 +461,7 @@ namespace Automation4 { const agi::Context *c = get_context(L); int ms = lua_tointeger(L, -1); lua_pop(L, 1); - if (c->videoController->TimecodesLoaded()) + if (c && c->videoController->TimecodesLoaded()) lua_pushnumber(L, c->videoController->FrameAtTime(ms, agi::vfr::START)); else lua_pushnil(L); @@ -471,7 +474,7 @@ namespace Automation4 { const agi::Context *c = get_context(L); int frame = lua_tointeger(L, -1); lua_pop(L, 1); - if (c->videoController->TimecodesLoaded()) + if (c && c->videoController->TimecodesLoaded()) lua_pushnumber(L, c->videoController->TimeAtFrame(frame, agi::vfr::START)); else lua_pushnil(L); @@ -481,7 +484,7 @@ namespace Automation4 { int LuaScript::LuaVideoSize(lua_State *L) { const agi::Context *c = get_context(L); - if (c->videoController->IsLoaded()) { + if (c && c->videoController->IsLoaded()) { lua_pushnumber(L, c->videoController->GetWidth()); lua_pushnumber(L, c->videoController->GetHeight()); lua_pushnumber(L, c->videoController->GetAspectRatioValue()); @@ -497,6 +500,11 @@ namespace Automation4 { int LuaScript::LuaGetKeyframes(lua_State *L) { const agi::Context *c = get_context(L); + if (!c) { + lua_pushnil(L); + return 1; + } + std::vector const& kf = c->videoController->GetKeyFrames(); lua_newtable(L);