9
9
10
10
namespace RTE {
11
11
12
- const std::unordered_set<std::string> LuaMan::c_FileAccessModes = { " r" , " r+" , " w" , " w+" , " a" , " a+" };
12
+ const std::unordered_set<std::string> LuaMan::c_FileAccessModes = { " r" , " r+" , " w" , " w+" , " a" , " a+" , " rt " , " wt " };
13
13
14
14
// ///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
15
15
@@ -227,8 +227,9 @@ namespace RTE {
227
227
// Override "math.random" in the lua state to use RTETools MT19937 implementation. Preserve return types of original to not break all the things.
228
228
" math.random = function(lower, upper) if lower ~= nil and upper ~= nil then return LuaMan:SelectRand(lower, upper); elseif lower ~= nil then return LuaMan:SelectRand(1, lower); else return LuaMan:PosRand(); end end"
229
229
" \n "
230
- // Override "dofile" to be able to account for Data/ or Mods/ directory.
230
+ // Override "dofile"/"loadfile" to be able to account for Data/ or Mods/ directory.
231
231
" OriginalDoFile = dofile; dofile = function(filePath) filePath = PresetMan:GetFullModulePath(filePath); if filePath ~= '' then return OriginalDoFile(filePath); end end;"
232
+ " OriginalLoadFile = loadfile; loadfile = function(filePath) filePath = PresetMan:GetFullModulePath(filePath); if filePath ~= '' then return OriginalLoadFile(filePath); end end;"
232
233
// Internal helper functions to add callbacks for async pathing requests
233
234
" _AsyncPathCallbacks = {};"
234
235
" _AddAsyncPathCallback = function(id, callback) _AsyncPathCallbacks[id] = callback; end\n "
@@ -636,7 +637,7 @@ namespace RTE {
636
637
637
638
// ///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
638
639
639
- int LuaStateWrapper::RunScriptFile (const std::string &filePath, bool consoleErrors) {
640
+ int LuaStateWrapper::RunScriptFile (const std::string &filePath, bool consoleErrors, bool doInSandboxedEnvironment ) {
640
641
const std::string fullScriptPath = g_PresetMan.GetFullModulePath (filePath);
641
642
if (fullScriptPath.empty ()) {
642
643
m_LastError = " Can't run a script file with an empty filepath!" ;
@@ -674,20 +675,22 @@ namespace RTE {
674
675
}
675
676
676
677
if (error == 0 ) {
677
- // create a new environment table
678
- lua_getglobal (m_State, filePath.c_str ());
679
- if (lua_isnil (m_State, -1 )) {
680
- lua_pop (m_State, 1 );
681
- lua_newtable (m_State);
682
- lua_newtable (m_State);
683
- lua_getglobal (m_State, " _G" );
684
- lua_setfield (m_State, -2 , " __index" );
685
- lua_setmetatable (m_State, -2 );
686
- lua_setglobal (m_State, filePath.c_str ());
678
+ if (doInSandboxedEnvironment) {
679
+ // create a new environment table
687
680
lua_getglobal (m_State, filePath.c_str ());
688
- }
681
+ if (lua_isnil (m_State, -1 )) {
682
+ lua_pop (m_State, 1 );
683
+ lua_newtable (m_State);
684
+ lua_newtable (m_State);
685
+ lua_getglobal (m_State, " _G" );
686
+ lua_setfield (m_State, -2 , " __index" );
687
+ lua_setmetatable (m_State, -2 );
688
+ lua_setglobal (m_State, filePath.c_str ());
689
+ lua_getglobal (m_State, filePath.c_str ());
690
+ }
689
691
690
- lua_setfenv (m_State, -2 );
692
+ lua_setfenv (m_State, -2 );
693
+ }
691
694
692
695
// execute script file with pcall. Pcall will call the file and line error handler if there's an error by pointing 2 up the stack to it.
693
696
if (lua_pcall (m_State, 0 , LUA_MULTRET, -2 )) {
@@ -710,7 +713,36 @@ namespace RTE {
710
713
711
714
// ///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
712
715
713
- int LuaStateWrapper::RunScriptFileAndRetrieveFunctions (const std::string &filePath, const std::string &prefix, const std::vector<std::string> &functionNamesToLookFor, std::unordered_map<std::string, LuabindObjectWrapper *> &outFunctionNamesAndObjects, bool forceReload) {
716
+ bool LuaStateWrapper::RetrieveFunctions (const std::string& funcObjectName, const std::vector<std::string>& functionNamesToLookFor, std::unordered_map<std::string, LuabindObjectWrapper*>& outFunctionNamesAndObjects) {
717
+ std::lock_guard<std::recursive_mutex> lock (m_Mutex);
718
+ s_currentLuaState = this ;
719
+
720
+ luabind::object funcHoldingObject = luabind::globals (m_State)[funcObjectName.c_str ()];
721
+ if (luabind::type (funcHoldingObject) == LUA_TNIL) {
722
+ return false ;
723
+ }
724
+
725
+ auto & newScript = m_ScriptCache[funcObjectName.c_str ()];
726
+ newScript.functionNamesAndObjects .clear ();
727
+ for (const std::string& functionName : functionNamesToLookFor) {
728
+ luabind::object functionObject = funcHoldingObject[functionName];
729
+ if (luabind::type (functionObject) == LUA_TFUNCTION) {
730
+ luabind::object* functionObjectCopyForStoring = new luabind::object (functionObject);
731
+ newScript.functionNamesAndObjects .try_emplace (functionName, new LuabindObjectWrapper (functionObjectCopyForStoring, funcObjectName));
732
+ }
733
+ }
734
+
735
+ for (auto & pair : newScript.functionNamesAndObjects ) {
736
+ luabind::object* functionObjectCopyForStoring = new luabind::object (*pair.second ->GetLuabindObject ());
737
+ outFunctionNamesAndObjects.try_emplace (pair.first , new LuabindObjectWrapper (functionObjectCopyForStoring, funcObjectName));
738
+ }
739
+
740
+ return true ;
741
+ }
742
+
743
+ // ///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
744
+
745
+ int LuaStateWrapper::RunScriptFileAndRetrieveFunctions (const std::string &filePath, const std::vector<std::string> &functionNamesToLookFor, std::unordered_map<std::string, LuabindObjectWrapper *> &outFunctionNamesAndObjects, bool forceReload) {
714
746
static bool disableCaching = false ;
715
747
forceReload = forceReload || disableCaching;
716
748
@@ -733,32 +765,10 @@ namespace RTE {
733
765
return error;
734
766
}
735
767
736
- luabind::object prefixObject;
737
- if (prefix == " " ) {
738
- prefixObject = luabind::globals (m_State)[filePath.c_str ()];
739
- } else {
740
- prefixObject = luabind::globals (m_State)[filePath.c_str ()][prefix];
741
- }
742
-
743
- if (luabind::type (prefixObject) == LUA_TNIL) {
768
+ if (!RetrieveFunctions (filePath, functionNamesToLookFor, outFunctionNamesAndObjects)) {
744
769
return -1 ;
745
770
}
746
771
747
- auto &newScript = m_ScriptCache[filePath];
748
- newScript.functionNamesAndObjects .clear ();
749
- for (const std::string& functionName : functionNamesToLookFor) {
750
- luabind::object functionObject = prefixObject[functionName];
751
- if (luabind::type (functionObject) == LUA_TFUNCTION) {
752
- luabind::object* functionObjectCopyForStoring = new luabind::object (functionObject);
753
- newScript.functionNamesAndObjects .try_emplace (functionName, new LuabindObjectWrapper (functionObjectCopyForStoring, filePath));
754
- }
755
- }
756
-
757
- for (auto & pair : newScript.functionNamesAndObjects ) {
758
- luabind::object* functionObjectCopyForStoring = new luabind::object (*pair.second ->GetLuabindObject ());
759
- outFunctionNamesAndObjects.try_emplace (pair.first , new LuabindObjectWrapper (functionObjectCopyForStoring, filePath));
760
- }
761
-
762
772
return 0 ;
763
773
}
764
774
0 commit comments