@@ -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