From edd83d8a4d5cf820c6ee4a25d811beb6facd6f4f Mon Sep 17 00:00:00 2001 From: Sam Hocevar Date: Fri, 31 May 2013 18:19:09 +0000 Subject: [PATCH] lua: implement dofile(). --- src/world.cpp | 58 +++++++++++++++++++++++++++++++++++---------------- 1 file changed, 40 insertions(+), 18 deletions(-) diff --git a/src/world.cpp b/src/world.cpp index 6364c43b..f9bf4c32 100644 --- a/src/world.cpp +++ b/src/world.cpp @@ -37,6 +37,38 @@ class WorldData return 0; } + static int LuaDoFile(lua_State *L) + { + if (lua_isnoneornil(L, 1)) + return LUA_ERRFILE; + + char const *filename = lua_tostring(L, 1); + int status = LUA_ERRFILE; + + Array pathlist = System::GetPathList(filename); + File f; + for (int i = 0; i < pathlist.Count(); ++i) + { + f.Open(pathlist[i], FileAccess::Read); + if (f.IsValid()) + { + String s = f.ReadString(); + f.Close(); + + Log::Debug("loading Lua file %s\n", pathlist[i].C()); + status = luaL_dostring(L, s.C()); + break; + } + } + + if (status == LUA_ERRFILE) + Log::Error("could not find Lua file %s\n", filename); + + lua_pop(L, 1); + + return status; + } + lua_State *m_lua_state; }; @@ -49,9 +81,14 @@ World g_world; World::World() { + /* Initialise the Lua engine */ g_world_data.m_lua_state = luaL_newstate(); lua_atpanic(g_world_data.m_lua_state, WorldData::LuaPanic); luaL_openlibs(g_world_data.m_lua_state); + + /* Override dofile() */ + lua_pushcfunction(g_world_data.m_lua_state, WorldData::LuaDoFile); + lua_setglobal(g_world_data.m_lua_state, "dofile"); } World::~World() @@ -61,24 +98,9 @@ World::~World() bool World::ExecLua(String const &lua) { - Array pathlist = System::GetPathList(lua); - File f; - for (int i = 0; i < pathlist.Count(); ++i) - { - f.Open(pathlist[i], FileAccess::Read); - if (f.IsValid()) - { - String s = f.ReadString(); - f.Close(); - - luaL_dostring(g_world_data.m_lua_state, s.C()); - Log::Debug("loaded Lua file %s\n", pathlist[i].C()); - return true; - } - } - - Log::Error("could not find Lua file %s\n", lua.C()); - return false; + lua_pushstring(g_world_data.m_lua_state, lua.C()); + int status = WorldData::LuaDoFile(g_world_data.m_lua_state); + return status == 0; } double World::GetLuaNumber(String const &var)