Skip to content

Commit 4f299a1

Browse files
CrosRoad95patrikjuvonen
authored andcommitted
Improved warnings for lua-package (#683)
* Improved warnings Previous looks like shit * fix conflicts * fix conflicts * Fix indentation * Tweak location text * fix path * fixed `nil` return in require * Fix code formatting
1 parent 3ed2191 commit 4f299a1

File tree

7 files changed

+71
-21
lines changed

7 files changed

+71
-21
lines changed

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

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -78,10 +78,10 @@ class CLuaMain //: public CClient
7878

7979
bool IsOOPEnabled() { return m_bEnableOOP; }
8080

81-
void InitPackageStorage(lua_State* L); // Create a psuedo package.loaded table
82-
void GetPackage(lua_State* L, SString& strName); // Push the package value to the top of the stack
83-
void SetPackage(lua_State* L, SString& strName); // Set the package to the value at the top of the stack
84-
bool LoadLuaLib(lua_State* L, SString strName, SString& strError); // Load a lua library of a given name
81+
void InitPackageStorage(lua_State* L); // Create a psuedo package.loaded table
82+
void GetPackage(lua_State* L, SString& strName); // Push the package value to the top of the stack
83+
void SetPackage(lua_State* L, SString& strName); // Set the package to the value at the top of the stack
84+
bool LoadLuaLib(lua_State* L, SString strName, SString& strError, bool& bEmpty); // Load a lua library of a given name
8585

8686
private:
8787
void InitSecurity();

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

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -686,7 +686,7 @@ bool CLuaMain::LoadClib(lua_State* L, SString strName, SString& strError)
686686

687687
if (!FileExists(strPath))
688688
{
689-
strError += "error loading module " + strName + " from file " + strPath + ":\n\t The specified module could not be found";
689+
strError += "#3: " + ConformPath(strPath, "modules/");
690690
return false;
691691
}
692692

@@ -696,14 +696,14 @@ bool CLuaMain::LoadClib(lua_State* L, SString strName, SString& strError)
696696
!g_pGame->GetACLManager()->CanObjectUseRight(m_pResource->GetName().c_str(), CAccessControlListGroupObject::OBJECT_TYPE_RESOURCE, strName.c_str(),
697697
CAccessControlListRight::RIGHT_TYPE_MODULE, false))
698698
{
699-
strError += "error loading module " + strName + " from file " + strPath + ":\n\t ACL access denied. Grant \"module." + strName +
700-
"\" right to resource " + m_pResource->GetName();
699+
strError = "could not load module '" + strName + "' from file " + ConformPath(strPath, "modules/") + ":\n\t ACL access denied. Grant \"module." +
700+
strName + "\" right to resource " + m_pResource->GetName();
701701
return false;
702702
}
703703

704704
if (!luaL_loader_C(L, strName.c_str(), strPath.c_str()) || lua_type(L, -1) == LUA_TNIL)
705705
{
706-
strError += "error loading module " + strName + " from file " + strPath + ":\n\t Failed to load module";
706+
strError = "failed to load module '" + strName + "' from file " + ConformPath(strPath, "modules/");
707707
return false;
708708
}
709709

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

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -108,11 +108,11 @@ class CLuaMain //: public CClient
108108
static int LuaLoadBuffer(lua_State* L, const char* buff, size_t sz, const char* name);
109109
static int OnUndump(const char* p, size_t n);
110110

111-
void InitPackageStorage(lua_State* L); // Create a psuedo package.loaded table
112-
void GetPackage(lua_State* L, SString& strName); // Push the package value to the top of the stack
113-
void SetPackage(lua_State* L, SString& strName); // Set the package to the value at the top of the stack
114-
bool LoadLuaLib(lua_State* L, SString strName, SString& strError); // Load a lua library of a given name
115-
bool LoadClib(lua_State* L, SString strName, SString& strError); // Load a C Lib of a given name
111+
void InitPackageStorage(lua_State* L); // Create a psuedo package.loaded table
112+
void GetPackage(lua_State* L, SString& strName); // Push the package value to the top of the stack
113+
void SetPackage(lua_State* L, SString& strName); // Set the package to the value at the top of the stack
114+
bool LoadLuaLib(lua_State* L, SString strName, SString& strError, bool& bEmpty); // Load a lua library of a given name
115+
bool LoadClib(lua_State* L, SString strName, SString& strError); // Load a C Lib of a given name
116116

117117
private:
118118
void InitSecurity();

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

Lines changed: 10 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -91,7 +91,7 @@ void CLuaMain::GetPackage(lua_State* L, SString& strName)
9191
// Load a Lua lib of a given name
9292
//
9393
///////////////////////////////////////////////////////////////
94-
bool CLuaMain::LoadLuaLib(lua_State* L, SString strName, SString& strError)
94+
bool CLuaMain::LoadLuaLib(lua_State* L, SString strName, SString& strError, bool& bEmpty)
9595
{
9696
SString strPath = strName;
9797
// Name format shouldn't include slashes. Subdirs are dots.
@@ -106,23 +106,23 @@ bool CLuaMain::LoadLuaLib(lua_State* L, SString strName, SString& strError)
106106
#endif
107107

108108
std::vector<char> buffer;
109+
strError = "error loading module '" + strName + "' from locations:\n\t";
109110
// Try <resource>/?.lua
110111
SString strFilePath = PathJoin(strResPath, strPath + ".lua");
111112
if (FileExists(strFilePath))
112113
FileLoad(strFilePath, buffer);
113114
else
114115
{
115116
// Don't use a format string for safety, so we construct the error by hand
116-
strError += "error loading module " + strName + " from file " + strFilePath + ":\n\t The specified module could not be found";
117+
strError += "#1: " + ConformPath(strFilePath, "resources/") + "\n\t";
117118

118119
// Try <resource>/?/init.lua
119120
strFilePath = PathJoin(strResPath, strPath, "init.lua");
120121
if (FileExists(strFilePath))
121122
FileLoad(strFilePath, buffer);
122123
else
123124
{
124-
strError += "\n";
125-
strError += "error loading module " + strName + " from file " + strFilePath + ":\n\t The specified module could not be found";
125+
strError += "#2: " + ConformPath(strFilePath, "resources/") + "\n\t";
126126
return false;
127127
}
128128
}
@@ -137,15 +137,19 @@ bool CLuaMain::LoadLuaLib(lua_State* L, SString strName, SString& strError)
137137

138138
if (lua_type(L, -1) == LUA_TNIL)
139139
{
140-
strError += "error loading module " + strName + " from file " + strFilePath + ":\n\t Module didn't return a value";
140+
strError = "module '" + strName + "' in location " + ConformPath(strFilePath, "resources/") + " didn't return a value.";
141+
bEmpty = true;
141142
return false;
142143
}
143144

144145
SetPackage(L, strName); // Store our package into package.loaded
145146
return true;
146147
}
147148
else
148-
strError += "error loading module " + strName + " from file " + strFilePath + ":\n\t Error loading script file";
149+
{
150+
strError = "could not load module '" + strName + "' from file " + ConformPath(strFilePath, "resources/") + ": File is empty.";
151+
bEmpty = true;
152+
}
149153

150154
return false;
151155
}

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

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -732,6 +732,7 @@ int CLuaUtilDefs::Require(lua_State* luaVM)
732732
if (!argStream.HasErrors())
733733
{
734734
SString strError = "";
735+
bool bEmpty = false;
735736
// Check if package exists already, if so load it
736737
// stack: ["moduleName"]
737738
pLuaMain->GetPackage(luaVM, strMod); // stack: ["moduleName",pkgModule/nil]
@@ -740,10 +741,16 @@ int CLuaUtilDefs::Require(lua_State* luaVM)
740741
lua_pop(luaVM, 1); // stack: ["moduleName"]
741742

742743
// Check whether the appropriate pure lua module exists
743-
if (pLuaMain->LoadLuaLib(luaVM, strMod, strError)) // stack: ["moduleName",pkgLuaMod/nil]
744+
if (pLuaMain->LoadLuaLib(luaVM, strMod, strError, bEmpty)) // stack: ["moduleName",pkgLuaMod/nil]
744745
return 1;
746+
747+
if (bEmpty)
748+
{
749+
m_pScriptDebugging->LogCustom(luaVM, strError);
750+
lua_pushboolean(luaVM, false);
751+
return 1;
752+
}
745753
#ifndef MTA_CLIENT
746-
strError += "\n";
747754
// Check if a C library exists
748755
if (pLuaMain->LoadClib(luaVM, strMod, strError)) // stack: ["moduleName",fncModule/nil]
749756
return 1;

Shared/sdk/SharedUtil.Misc.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -209,6 +209,8 @@ namespace SharedUtil
209209
//
210210
SString ConformResourcePath(const char* szRes, bool bConvertToUnixPathSep = false);
211211

212+
SString ConformPath(const char* szRes, SString szPath, bool bConvertToUnixPathSep = false);
213+
212214
//
213215
// string stuff
214216
//

Shared/sdk/SharedUtil.Misc.hpp

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1448,6 +1448,43 @@ SString SharedUtil::ConformResourcePath(const char* szRes, bool bConvertToUnixPa
14481448
return strText;
14491449
}
14501450

1451+
SString SharedUtil::ConformPath(const char* szRes, SString szPath, bool bConvertToUnixPathSep)
1452+
{
1453+
SString strText = szRes ? szRes : "";
1454+
char cPathSep;
1455+
1456+
// Handle which path sep char
1457+
#ifdef WIN32
1458+
if (!bConvertToUnixPathSep)
1459+
{
1460+
cPathSep = '\\';
1461+
szPath = szPath.Replace("/", "\\");
1462+
strText = strText.Replace("/", "\\");
1463+
}
1464+
else
1465+
#endif
1466+
{
1467+
cPathSep = '/';
1468+
szPath = szPath.Replace("\\", "/");
1469+
strText = strText.Replace("\\", "/");
1470+
}
1471+
1472+
// Remove up to first occurrence
1473+
int iPos = strText.find(szPath);
1474+
if (iPos >= 0)
1475+
return SString("%s%s", szPath.c_str(), strText.substr(iPos + szPath.length()).c_str());
1476+
1477+
if (strText.substr(0, 3) == "...")
1478+
{
1479+
// Remove up to first '/'
1480+
int iPos = strText.find(cPathSep);
1481+
if (iPos >= 0)
1482+
return SString("%s%s", szPath.c_str(), strText.substr(iPos + 1).c_str());
1483+
}
1484+
1485+
return strText;
1486+
}
1487+
14511488
namespace SharedUtil
14521489
{
14531490
CArgMap::CArgMap(const SString& strArgSep, const SString& strPartsSep, const SString& strExtraDisallowedChars)

0 commit comments

Comments
 (0)