| 312 | } |
| 313 | |
| 314 | int Service::LoadFile(lua_State *pState) { |
| 315 | // 1 - name |
| 316 | |
| 317 | SCRIPT_BEGIN(pState) |
| 318 | |
| 319 | const char* name(lua_tostring(pState, 1)); |
| 320 | if (!name) { |
| 321 | SCRIPT_ERROR("loadFile must take a string argument") |
| 322 | return 0; |
| 323 | } |
| 324 | if (FileSystem::IsFolder(name)) { |
| 325 | SCRIPT_ERROR("loadFile can't load a folder") |
| 326 | return 0; |
| 327 | } |
| 328 | |
| 329 | if (!FileSystem::IsAbsolute(name)) { |
| 330 | |
| 331 | Service* pService((Service*)lua_touserdata(pState,lua_upvalueindex(1))); |
| 332 | string path; |
| 333 | |
| 334 | while (pService) { |
| 335 | Exception ex; |
| 336 | String::Format(path,pService->_rootPath,pService->path,'/',name); |
| 337 | |
| 338 | if (FileSystem::Exists(path)) { |
| 339 | if (luaL_loadfile(pState, path.c_str()) == 0) { |
| 340 | lua_rawgeti(pState, LUA_REGISTRYINDEX, pService->reference()); |
| 341 | lua_setfenv(pState, -2); |
| 342 | return 1; |
| 343 | } |
| 344 | SCRIPT_ERROR(Script::LastError(pState)) |
| 345 | return 0; |
| 346 | } |
| 347 | |
| 348 | pService = pService->_pParent; |
| 349 | } |
| 350 | |
| 351 | } |
| 352 | |
| 353 | // try pure relative or absolute |
| 354 | if (luaL_loadfile(pState, name) == 0) |
| 355 | return 1; |
| 356 | |
| 357 | SCRIPT_ERROR(Script::LastError(pState)) |
| 358 | |
| 359 | SCRIPT_END |
| 360 | return 0; |
| 361 | } |
| 362 | |
| 363 | int Service::ExecuteFile(lua_State *pState) { |
| 364 | // 1 - name |