И ещё, при создании Lua-машин нужно добавлять путь до папки с расширением в
package.path и в
package.сpath, чтобы расширение могло загружать модули (как .dll, так и .lua) из своей папки. В path надо добавлять путь вида
%ExtensionPath%\?.lua, а в cpath вида
%ExtensionPath%\?.dll. Пути добавляются через точку с запятой.
Пример кода добавления пути в path (
взято тут):
int setLuaPath( lua_State* L, const char* path )
{
lua_getglobal( L, "package" );
lua_getfield( L, -1, "path" ); // get field "path" from table at top of stack (-1)
std::string cur_path = lua_tostring( L, -1 ); // grab path string from top of stack
cur_path.append( ";" ); // do your path magic here
cur_path.append( path );
lua_pop( L, 1 ); // get rid of the string on the stack we just pushed on line 5
lua_pushstring( L, cur_path.c_str() ); // push the new one
lua_setfield( L, -2, "path" ); // set the field "path" in table at -2 with value at top of stack
lua_pop( L, 1 ); // get rid of package table from top of stack
return 0; // all done!
}
Аналогично путь добавляется и в cpath.
Сейчас же в скрипте приходится делать лишние проверки и перед загрузкой модулей инициализировать эти пути руками:
local script_path = string.match(hc.script_name, [[.*\]])
local lua_path = script_path .. '?.lua'
if not string.find(package.path, lua_path) then
package.path = package.path .. ';' .. lua_path
end
local c_path = script_path .. '?.dll'
if not string.find(package.cpath, c_path) then
package.cpath = package.cpath .. ';' .. c_path
end