0.9.6: set current directory to script's location on lua script load

This commit is contained in:
gocha 2010-09-20 08:14:46 +00:00
parent 5919d664ee
commit 36da18aec6
1 changed files with 25 additions and 0 deletions

View File

@ -1,3 +1,4 @@
#include "types.h"
#include "lua-engine.h"
#include "movie.h"
#include <assert.h>
@ -16,6 +17,11 @@
#include "windows.h"
#include "video.h"
#endif
#ifdef WIN32
#include <direct.h>
#else
#include <unistd.h>
#endif
using namespace std;
@ -488,6 +494,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);
@ -4578,6 +4597,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