diff --git a/desmume/src/lua-engine.cpp b/desmume/src/lua-engine.cpp index 7cf0b753e..f894d70c0 100644 --- a/desmume/src/lua-engine.cpp +++ b/desmume/src/lua-engine.cpp @@ -1,3 +1,4 @@ +#include "types.h" #include "lua-engine.h" #include "movie.h" #include @@ -16,6 +17,11 @@ #include "windows.h" #include "video.h" #endif +#ifdef WIN32 +#include +#else +#include +#endif using namespace std; @@ -497,6 +503,19 @@ static const char* FilenameFromPath(const char* path) return rv; } +void TrimFilenameFromPath(char* path) +{ + char* slash1 = strrchr(path, '\\'); + char* slash2 = strrchr(path, '/'); + char* slash = slash1; + if (slash == NULL || slash2 > slash) { + slash = slash2; + } + if (slash != NULL) { + *(slash + 1) = '\0'; + } +} + static void toCStringConverter(lua_State* L, int i, char*& ptr, int& remaining); @@ -4587,6 +4606,12 @@ void RunLuaScriptFile(int uid, const char* filenameCStr) info.nextFilename = filenameCStr; + // TODO: store script's current directory into LuaContextInfo + static char dirnameCStr[MAX_PATH]; + strcpy(dirnameCStr, filenameCStr); + TrimFilenameFromPath(dirnameCStr); + chdir(dirnameCStr); + if(info.running) { // it's a little complicated, but... the call to luaL_dofile below