Skip to content

Commit b8ec9e2

Browse files
Added more useful error messages to require
1 parent a1d0cd7 commit b8ec9e2

File tree

4 files changed

+45
-10
lines changed

4 files changed

+45
-10
lines changed

Server/mods/deathmatch/logic/lua/CLuaMain.cpp

Lines changed: 36 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -757,7 +757,7 @@ void CLuaMain::SetPackage(lua_State* L, SString &strName )
757757
// Push the value of a package of name to the stack
758758
//
759759
///////////////////////////////////////////////////////////////
760-
void CLuaMain::GetPackage(lua_State* L, SString &strName)
760+
void CLuaMain::GetPackage(lua_State* L, SString &strName )
761761
{
762762
if (m_iPackageLoadedRef < 0)
763763
return;
@@ -775,7 +775,7 @@ void CLuaMain::GetPackage(lua_State* L, SString &strName)
775775
// Load a Lua lib of a given name
776776
//
777777
///////////////////////////////////////////////////////////////
778-
bool CLuaMain::LoadLuaLib(lua_State *L, SString strName)
778+
bool CLuaMain::LoadLuaLib(lua_State *L, SString strName, SString &strError)
779779
{
780780
SString strPath = strName;
781781
// Name format shouldn't include slashes. Subdirs are dots.
@@ -792,10 +792,21 @@ bool CLuaMain::LoadLuaLib(lua_State *L, SString strName)
792792
FileLoad(strFilePath, buffer);
793793
else
794794
{
795+
// Don't use a format string for safety, so we construct the error by hand
796+
strError += "error loading module " + strName + " from file " + strFilePath +
797+
":\n\t The specified module could not be found";
798+
795799
// Try <resource>/?/init.lua
796800
strFilePath = PathJoin(strResPath, strPath, "init.lua");
797801
if (FileExists(strFilePath))
798802
FileLoad(strFilePath, buffer);
803+
else
804+
{
805+
strError += "\n";
806+
strError += "error loading module " + strName + " from file " + strFilePath +
807+
":\n\t The specified module could not be found";
808+
return false;
809+
}
799810
}
800811

801812
if (buffer.size() > 0)
@@ -806,9 +817,19 @@ bool CLuaMain::LoadLuaLib(lua_State *L, SString strName)
806817
if (lua_gettop(L) > luaSavedTop)
807818
lua_settop(L, luaSavedTop+1);
808819

820+
if (lua_type(L, -1) == LUA_TNIL)
821+
{
822+
strError += "error loading module " + strName + " from file " + strFilePath +
823+
":\n\t Module didn't return a value";
824+
return false;
825+
}
826+
809827
SetPackage(L, strName); // Store our package into package.loaded
810828
return true;
811829
}
830+
else
831+
strError += "error loading module " + strName + " from file " + strFilePath +
832+
":\n\t Error loading script file";
812833

813834
return false;
814835
}
@@ -820,7 +841,7 @@ bool CLuaMain::LoadLuaLib(lua_State *L, SString strName)
820841
// Load a C lib of a given name
821842
//
822843
///////////////////////////////////////////////////////////////
823-
bool CLuaMain::LoadClib(lua_State* L, SString strName)
844+
bool CLuaMain::LoadClib(lua_State* L, SString strName, SString &strError )
824845
{
825846
SString strPath = strName;
826847
// Name format shouldn't include slashes. Subdirs are dots.
@@ -836,9 +857,19 @@ bool CLuaMain::LoadClib(lua_State* L, SString strName)
836857
strPath += ".so";
837858
#endif
838859

839-
luaL_loader_C(L, strName.c_str(), strPath.c_str());
840-
if (lua_type(L, -1) == LUA_TNIL)
860+
if (!FileExists(strPath))
861+
{
862+
strError += "error loading module " + strName + " from file " + strPath +
863+
":\n\t The specified module could not be found";
841864
return false;
865+
}
866+
867+
if (!luaL_loader_C(L, strName.c_str(), strPath.c_str()) || lua_type(L, -1) == LUA_TNIL)
868+
{
869+
strError += "error loading module " + strName + " from file " + strPath +
870+
":\n\t Failed to load module";
871+
return false;
872+
}
842873

843874
SetPackage(L, strName);
844875
return true;

Server/mods/deathmatch/logic/lua/CLuaMain.h

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -123,8 +123,8 @@ class CLuaMain //: public CClient
123123
void InitPackageStorage ( lua_State* L ); // Create a psuedo package.loaded table
124124
void GetPackage ( lua_State *L, SString &strName ); // Push the package value to the top of the stack
125125
void SetPackage ( lua_State *L, SString &strName ); // Set the package to the value at the top of the stack
126-
bool LoadLuaLib ( lua_State *L, SString strName ); // Load a lua library of a given name
127-
bool LoadClib ( lua_State *L, SString strName ); // Load a C Lib of a given name
126+
bool LoadLuaLib ( lua_State *L, SString strName, SString &strError ); // Load a lua library of a given name
127+
bool LoadClib ( lua_State *L, SString strName, SString &strError ); // Load a C Lib of a given name
128128

129129
private:
130130
void InitSecurity ( void );

Shared/mods/deathmatch/logic/luadefs/CLuaUtilDefs.cpp

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -575,6 +575,7 @@ int CLuaUtilDefs::Require(lua_State* luaVM)
575575

576576
if (!argStream.HasErrors())
577577
{
578+
SString strError = "";
578579
// Check if package exists already, if so load it
579580
// stack: ["moduleName"]
580581
pLuaMain->GetPackage(luaVM, strMod); // stack: ["moduleName",pkgModule/nil]
@@ -583,12 +584,15 @@ int CLuaUtilDefs::Require(lua_State* luaVM)
583584
lua_pop(luaVM,1); // stack: ["moduleName"]
584585

585586
// Check whether the appropriate pure lua module exists
586-
if (pLuaMain->LoadLuaLib(luaVM, strMod)) // stack: ["moduleName",pkgLuaMod/nil]
587+
if (pLuaMain->LoadLuaLib(luaVM, strMod,strError)) // stack: ["moduleName",pkgLuaMod/nil]
587588
return 1;
589+
strError += "\n";
588590

589591
// Check if a C library exists
590-
if (pLuaMain->LoadClib(luaVM, strMod)) // stack: ["moduleName",fncModule/nil]
592+
if (pLuaMain->LoadClib(luaVM, strMod,strError)) // stack: ["moduleName",fncModule/nil]
591593
return 1;
594+
m_pScriptDebugging->LogCustom(luaVM, strError);
595+
592596
lua_pop(luaVM,2); // stack: []
593597
}
594598
else

vendor/lua/src/loadlib.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -673,6 +673,6 @@ LUALIB_API int luaopen_package (lua_State *L) {
673673
LUA_API int luaL_loader_C(lua_State *L, const char* name, const char* filename) {
674674
const char* funcname = mkfuncname(L, name);
675675
if (ll_loadfunc(L, filename, funcname) != 0)
676-
loaderror(L, filename);
676+
return 0;
677677
return 1; /* library loaded successfully */
678678
}

0 commit comments

Comments
 (0)