Skip to content

Commit aae6ebf

Browse files
committed
Move duplicate package methods to CLuaMain.Shared
1 parent 8d74aa2 commit aae6ebf

File tree

3 files changed

+151
-276
lines changed

3 files changed

+151
-276
lines changed

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

Lines changed: 0 additions & 138 deletions
Original file line numberDiff line numberDiff line change
@@ -560,141 +560,3 @@ int CLuaMain::OnUndump(const char* p, size_t n)
560560
}
561561
return 1;
562562
}
563-
564-
///////////////////////////////////////////////////////////////
565-
//
566-
// CLuaMain::InitPackageStorage
567-
//
568-
// Create a table for storage of Lua packages - stored in the Lua VM
569-
//
570-
///////////////////////////////////////////////////////////////
571-
void CLuaMain::InitPackageStorage(lua_State* L)
572-
{
573-
lua_newtable(L); // stack: [tbl_new]
574-
lua_pushstring(L, "loaded"); // stack: [tbl_new,"loaded"]
575-
lua_newtable(L); // stack: [tbl_new,"loaded",tbl_new2]
576-
577-
// We push our default Lua libs are loaded packages
578-
std::vector<const char*> szDefaultLibs = {"math", "string", "table", "debug", "utf8"};
579-
for (auto it : szDefaultLibs)
580-
{
581-
lua_pushstring(L, it); // stack: [tbl_new,"loaded",tbl_new2,"math"]
582-
lua_getglobal(L, it); // stack: [tbl_new,"loaded",tbl_new2,"math",_G.math]
583-
lua_settable(L, -3); // stack: [tbl_new,"loaded",tbl_new2]
584-
}
585-
586-
// Keep our package.loaded table safely in registry
587-
m_iPackageLoadedRef = luaL_ref(L, LUA_REGISTRYINDEX); // stack: [tbl_new,"loaded"]
588-
lua_rawgeti(L, LUA_REGISTRYINDEX, m_iPackageLoadedRef); // stack: [tbl_new,"loaded",tbl_new2]
589-
590-
// Finally, store our original table as global package.loaded
591-
lua_settable(L, -3); // stack: [tbl_new]
592-
lua_setglobal(L, "package"); // stack: []
593-
}
594-
595-
///////////////////////////////////////////////////////////////
596-
//
597-
// CLuaMain::SetPackage
598-
//
599-
// Set the top most value as a package (No pop)
600-
//
601-
///////////////////////////////////////////////////////////////
602-
void CLuaMain::SetPackage(lua_State* L, SString& strName)
603-
{
604-
if (m_iPackageLoadedRef < 0)
605-
return;
606-
// We set varPkg, which is already on the stack, into package.loaded.moduleName = varPkg.
607-
// stack: [varPkg]
608-
int iPkg = luaL_ref(L, LUA_REGISTRYINDEX); // stack: []
609-
lua_rawgeti(L, LUA_REGISTRYINDEX, m_iPackageLoadedRef); // [tbl_loaded]
610-
611-
lua_pushstring(L, strName.c_str()); // stack: [tbl_loaded,"moduleName"]
612-
lua_rawgeti(L, LUA_REGISTRYINDEX, iPkg); // stack: [tbl_loaded,"moduleName",varPkg]
613-
lua_rawset(L, -3); // stack: [tbl_loaded]
614-
lua_pop(L, 2); // stack: []
615-
lua_rawgeti(L, LUA_REGISTRYINDEX, iPkg); // stack: [varPkg]
616-
617-
// Cleanup our used registry entry, i.e. REGISTRY[i] = nil.
618-
lua_pushnil(L); // stack: [varPkg,nil]
619-
lua_rawseti(L, LUA_REGISTRYINDEX, iPkg); // stack: [varPkg]
620-
}
621-
622-
///////////////////////////////////////////////////////////////
623-
//
624-
// CLuaMain::GetPackage
625-
//
626-
// Push the value of a package of name to the stack
627-
//
628-
///////////////////////////////////////////////////////////////
629-
void CLuaMain::GetPackage(lua_State* L, SString& strName)
630-
{
631-
if (m_iPackageLoadedRef < 0)
632-
return;
633-
634-
lua_rawgeti(L, LUA_REGISTRYINDEX, m_iPackageLoadedRef); // stack: [tbl_loaded]
635-
lua_pushstring(L, strName.c_str()); // stack: [tbl_loaded,"moduleName"]
636-
lua_rawget(L, -2); // stack: [tbl_loaded,varPkg]
637-
lua_remove(L, -2); // stack: [varPkg]
638-
}
639-
640-
///////////////////////////////////////////////////////////////
641-
//
642-
// CLuaMain::LoadLuaLib
643-
//
644-
// Load a Lua lib of a given name
645-
//
646-
///////////////////////////////////////////////////////////////
647-
bool CLuaMain::LoadLuaLib(lua_State* L, SString strName, SString& strError)
648-
{
649-
SString strPath = strName;
650-
// Name format shouldn't include slashes. Subdirs are dots.
651-
ReplaceOccurrencesInString(strPath, "\\", "");
652-
ReplaceOccurrencesInString(strPath, "/", "");
653-
ReplaceOccurrencesInString(strPath, ".", "/");
654-
655-
SString strResPath = m_pResource->GetResourceDirectoryPath(ACCESS_PUBLIC, "");
656-
657-
std::vector<char> buffer;
658-
// Try <resource>/?.lua
659-
SString strFilePath = PathJoin(strResPath, strPath + ".lua");
660-
if (FileExists(strFilePath))
661-
FileLoad(strFilePath, buffer);
662-
else
663-
{
664-
// Don't use a format string for safety, so we construct the error by hand
665-
strError += "error loading module " + strName + " from file " + strFilePath + ":\n\t The specified module could not be found";
666-
667-
// Try <resource>/?/init.lua
668-
strFilePath = PathJoin(strResPath, strPath, "init.lua");
669-
if (FileExists(strFilePath))
670-
FileLoad(strFilePath, buffer);
671-
else
672-
{
673-
strError += "\n";
674-
strError += "error loading module " + strName + " from file " + strFilePath + ":\n\t The specified module could not be found";
675-
return false;
676-
}
677-
}
678-
679-
if (buffer.size() > 0)
680-
{
681-
int luaSavedTop = lua_gettop(L);
682-
LoadScriptFromBuffer(&buffer.at(0), buffer.size(), strFilePath.c_str(), false);
683-
// Only keep the first return value
684-
if (lua_gettop(L) > luaSavedTop)
685-
lua_settop(L, luaSavedTop + 1);
686-
687-
if (lua_type(L, -1) == LUA_TNIL)
688-
{
689-
strError += "error loading module " + strName + " from file " + strFilePath + ":\n\t Module didn't return a value";
690-
return false;
691-
}
692-
693-
SetPackage(L, strName); // Store our package into package.loaded
694-
return true;
695-
}
696-
else
697-
strError += "error loading module " + strName + " from file " + strFilePath + ":\n\t Error loading script file";
698-
699-
return false;
700-
}

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

Lines changed: 0 additions & 138 deletions
Original file line numberDiff line numberDiff line change
@@ -661,144 +661,6 @@ int CLuaMain::OnUndump(const char* p, size_t n)
661661
return 1;
662662
}
663663

664-
///////////////////////////////////////////////////////////////
665-
//
666-
// CLuaMain::InitPackageStorage
667-
//
668-
// Create a table for storage of Lua packages - stored in the Lua VM
669-
//
670-
///////////////////////////////////////////////////////////////
671-
void CLuaMain::InitPackageStorage(lua_State* L)
672-
{
673-
lua_newtable(L); // stack: [tbl_new]
674-
lua_pushstring(L, "loaded"); // stack: [tbl_new,"loaded"]
675-
lua_newtable(L); // stack: [tbl_new,"loaded",tbl_new2]
676-
677-
// We push our default Lua libs are loaded packages
678-
std::vector<const char*> szDefaultLibs = {"math", "string", "table", "debug", "utf8"};
679-
for (auto it : szDefaultLibs)
680-
{
681-
lua_pushstring(L, it); // stack: [tbl_new,"loaded",tbl_new2,"math"]
682-
lua_getglobal(L, it); // stack: [tbl_new,"loaded",tbl_new2,"math",_G.math]
683-
lua_settable(L, -3); // stack: [tbl_new,"loaded",tbl_new2]
684-
}
685-
686-
// Keep our package.loaded table safely in registry
687-
m_iPackageLoadedRef = luaL_ref(L, LUA_REGISTRYINDEX); // stack: [tbl_new,"loaded"]
688-
lua_rawgeti(L, LUA_REGISTRYINDEX, m_iPackageLoadedRef); // stack: [tbl_new,"loaded",tbl_new2]
689-
690-
// Finally, store our original table as global package.loaded
691-
lua_settable(L, -3); // stack: [tbl_new]
692-
lua_setglobal(L, "package"); // stack: []
693-
}
694-
695-
///////////////////////////////////////////////////////////////
696-
//
697-
// CLuaMain::SetPackage
698-
//
699-
// Set the top most value as a package (No pop)
700-
//
701-
///////////////////////////////////////////////////////////////
702-
void CLuaMain::SetPackage(lua_State* L, SString& strName)
703-
{
704-
if (m_iPackageLoadedRef < 0)
705-
return;
706-
// We set varPkg, which is already on the stack, into package.loaded.moduleName = varPkg.
707-
// stack: [varPkg]
708-
int iPkg = luaL_ref(L, LUA_REGISTRYINDEX); // stack: []
709-
lua_rawgeti(L, LUA_REGISTRYINDEX, m_iPackageLoadedRef); // [tbl_loaded]
710-
711-
lua_pushstring(L, strName.c_str()); // stack: [tbl_loaded,"moduleName"]
712-
lua_rawgeti(L, LUA_REGISTRYINDEX, iPkg); // stack: [tbl_loaded,"moduleName",varPkg]
713-
lua_rawset(L, -3); // stack: [tbl_loaded]
714-
lua_pop(L, 2); // stack: []
715-
lua_rawgeti(L, LUA_REGISTRYINDEX, iPkg); // stack: [varPkg]
716-
717-
// Cleanup our used registry entry, i.e. REGISTRY[i] = nil.
718-
lua_pushnil(L); // stack: [varPkg,nil]
719-
lua_rawseti(L, LUA_REGISTRYINDEX, iPkg); // stack: [varPkg]
720-
}
721-
722-
///////////////////////////////////////////////////////////////
723-
//
724-
// CLuaMain::GetPackage
725-
//
726-
// Push the value of a package of name to the stack
727-
//
728-
///////////////////////////////////////////////////////////////
729-
void CLuaMain::GetPackage(lua_State* L, SString& strName)
730-
{
731-
if (m_iPackageLoadedRef < 0)
732-
return;
733-
734-
lua_rawgeti(L, LUA_REGISTRYINDEX, m_iPackageLoadedRef); // stack: [tbl_loaded]
735-
lua_pushstring(L, strName.c_str()); // stack: [tbl_loaded,"moduleName"]
736-
lua_rawget(L, -2); // stack: [tbl_loaded,varPkg]
737-
lua_remove(L, -2); // stack: [varPkg]
738-
}
739-
740-
///////////////////////////////////////////////////////////////
741-
//
742-
// CLuaMain::LoadLuaLib
743-
//
744-
// Load a Lua lib of a given name
745-
//
746-
///////////////////////////////////////////////////////////////
747-
bool CLuaMain::LoadLuaLib(lua_State* L, SString strName, SString& strError)
748-
{
749-
SString strPath = strName;
750-
// Name format shouldn't include slashes. Subdirs are dots.
751-
ReplaceOccurrencesInString(strPath, "\\", "");
752-
ReplaceOccurrencesInString(strPath, "/", "");
753-
ReplaceOccurrencesInString(strPath, ".", "/");
754-
755-
SString strResPath = m_pResource->IsResourceZip() ? m_pResource->GetResourceCacheDirectoryPath() : m_pResource->GetResourceDirectoryPath();
756-
757-
std::vector<char> buffer;
758-
// Try <resource>/?.lua
759-
SString strFilePath = PathJoin(strResPath, strPath + ".lua");
760-
if (FileExists(strFilePath))
761-
FileLoad(strFilePath, buffer);
762-
else
763-
{
764-
// Don't use a format string for safety, so we construct the error by hand
765-
strError += "error loading module " + strName + " from file " + strFilePath + ":\n\t The specified module could not be found";
766-
767-
// Try <resource>/?/init.lua
768-
strFilePath = PathJoin(strResPath, strPath, "init.lua");
769-
if (FileExists(strFilePath))
770-
FileLoad(strFilePath, buffer);
771-
else
772-
{
773-
strError += "\n";
774-
strError += "error loading module " + strName + " from file " + strFilePath + ":\n\t The specified module could not be found";
775-
return false;
776-
}
777-
}
778-
779-
if (buffer.size() > 0)
780-
{
781-
int luaSavedTop = lua_gettop(L);
782-
LoadScriptFromBuffer(&buffer.at(0), buffer.size(), strFilePath.c_str(), false);
783-
// Only keep the first return value
784-
if (lua_gettop(L) > luaSavedTop)
785-
lua_settop(L, luaSavedTop + 1);
786-
787-
if (lua_type(L, -1) == LUA_TNIL)
788-
{
789-
strError += "error loading module " + strName + " from file " + strFilePath + ":\n\t Module didn't return a value";
790-
return false;
791-
}
792-
793-
SetPackage(L, strName); // Store our package into package.loaded
794-
return true;
795-
}
796-
else
797-
strError += "error loading module " + strName + " from file " + strFilePath + ":\n\t Error loading script file";
798-
799-
return false;
800-
}
801-
802664
///////////////////////////////////////////////////////////////
803665
//
804666
// CLuaMain::LoadClib

0 commit comments

Comments
 (0)