lua_State* CLuaModuleManager::GetResourceFromName ( const char* szResourceName ) { CResource* pResource = g_pGame->GetResourceManager()->GetResource ( szResourceName ); if ( pResource ) { CLuaMain* pLuaMain = pResource->GetVirtualMachine (); if ( pLuaMain ) { return pLuaMain->GetVM (); } } return NULL; }
int CLuaFunctionDefs::GetCommandHandlers ( lua_State* luaVM ) { // table getCommandHandlers ( [ resource sourceResource ] ); CResource* pResource; bool bSpecificResource = false; CScriptArgReader argStream ( luaVM ); if ( !argStream.NextIsNone() ) { argStream.ReadUserData ( pResource ); bSpecificResource = true; } if ( !argStream.HasErrors() ) { if ( bSpecificResource ) { // Grab resource virtual machine CLuaMain* pLuaMain = pResource->GetVirtualMachine (); if ( pLuaMain ) { m_pRegisteredCommands->GetCommands( luaVM, pLuaMain ); return 1; } } else { m_pRegisteredCommands->GetCommands( luaVM ); return 1; } } else m_pScriptDebugging->LogCustom ( luaVM, argStream.GetFullErrorMessage () ); lua_pushboolean ( luaVM, false ); return 1; }
bool CMapManager::HandleNode ( CResource& Loader, CXMLNode& Node, CElement* pParent, vector < CElement* >* pAdded, bool bIsDuringStart, CElement** pCreated ) { // Grab the name std::string strBuffer = Node.GetTagName (); // Handle it based on the tag name CElement* pNode = NULL; if ( strBuffer.compare ( "vehicle" ) == 0 ) { pNode = m_pVehicleManager->CreateFromXML ( pParent, Node, Loader.GetVirtualMachine (), m_pEvents ); } else if ( strBuffer.compare ( "object" ) == 0 ) { bool bIsLowLod = false; pNode = m_pObjectManager->CreateFromXML ( pParent, Node, Loader.GetVirtualMachine (), m_pEvents, bIsLowLod ); } else if ( strBuffer.compare ( "blip" ) == 0 ) { CBlip* pBlip = m_pBlipManager->CreateFromXML ( pParent, Node, Loader.GetVirtualMachine (), m_pEvents ); pNode = pBlip; if ( pBlip ) { pBlip->SetIsSynced ( bIsDuringStart ); } } else if ( strBuffer.compare ( "pickup" ) == 0 ) { pNode = m_pPickupManager->CreateFromXML ( pParent, Node, Loader.GetVirtualMachine (), m_pEvents ); } else if ( strBuffer.compare ( "marker" ) == 0 ) { CMarker* pMarker = m_pMarkerManager->CreateFromXML ( pParent, Node, Loader.GetVirtualMachine (), m_pEvents ); pNode = pMarker; if ( pMarker ) { pMarker->SetIsSynced ( bIsDuringStart ); } } else if ( strBuffer.compare ( "radararea" ) == 0 ) { CRadarArea* pRadarArea = m_pRadarAreaManager->CreateFromXML ( pParent, Node, Loader.GetVirtualMachine (), m_pEvents ); pNode = pRadarArea; if ( pRadarArea ) { pRadarArea->SetIsSynced ( bIsDuringStart ); } } else if ( strBuffer.compare ( "team" ) == 0 ) { pNode = m_pTeamManager->CreateFromXML ( pParent, Node, Loader.GetVirtualMachine (), m_pEvents ); } else if ( strBuffer.compare ( "ped" ) == 0 ) { pNode = m_pPedManager->CreateFromXML ( pParent, Node, Loader.GetVirtualMachine (), m_pEvents ); } else if ( strBuffer.empty () ) { // Comment, return true return true; } else { pNode = m_pGroups->CreateFromXML ( pParent, Node, Loader.GetVirtualMachine (), m_pEvents ); } // Set the node we created in the pointer we were given if ( pCreated ) { *pCreated = pNode; } // Got a node created? if ( pNode ) { // Set its typename to the name of the tag pNode->SetTypeName ( strBuffer ); // Add it to our list over elements added if ( pAdded ) { pAdded->push_back ( pNode ); } // Add this element to the resource's element group so it's deleted with it CElementGroup* pElementGroup = Loader.GetElementGroup (); if ( pElementGroup ) pElementGroup->Add ( pNode ); // Load the elements below it return LoadSubNodes ( Loader, Node, pNode, pAdded, bIsDuringStart ); } return false; }
int CLuaDefs::CanUseFunction ( lua_CFunction f, lua_State* luaVM ) { // Quick cull of unknown pointer range if ( CLuaCFunctions::IsNotFunction( f ) ) return true; // Get associated resource CResource* pResource = m_pResourceManager->GetResourceFromLuaState( luaVM ); if ( !pResource ) return true; // Update execution time check pResource->GetVirtualMachine()->CheckExecutionTime(); // Check function right cache in resource bool bAllowed; if ( pResource->CheckFunctionRightCache( f, &bAllowed ) ) { // If in cache, and not allowed, do warning here if ( !bAllowed ) m_pScriptDebugging->LogBadAccess ( luaVM ); } else { // If not in cache, do full check bAllowed = true; // Grab the function name we're calling. If it's one of our functions, see if we can use it. CLuaCFunction* pFunction = CLuaCFunctions::GetFunction ( f ); dassert( pFunction ); if ( pFunction ) { // If it's not one of lua's functions, see if we allow it bAllowed = CLuaDefs::CanUseFunction ( pFunction->GetName ().c_str (), luaVM/*, pResource*/, pFunction->IsRestricted () ); } // Update cache in resource pResource->UpdateFunctionRightCache( f, bAllowed ); } g_pGame->GetDebugHookManager()->OnPreFunction( f, luaVM, bAllowed ); // If not allowed, do no more if ( !bAllowed ) return false; // Check if function timing is active if ( g_pStats->bFunctionTimingActive || g_pGame->GetDebugHookManager()->HasPostFunctionHooks() ) { // Check if hook needs applying if ( !ms_bRegisterdPostCallHook ) { OutputDebugLine ( "[Lua] Registering PostCallHook" ); ms_bRegisterdPostCallHook = true; lua_registerPostCallHook ( CLuaDefs::DidUseFunction ); } // Start to time the function ms_TimingFunctionStack.push_back ( STimingFunction( luaVM, f, GetTimeUs(), g_uiNetSentByteCounter ) ); } return true; }
int CLuaDefs::CanUseFunction ( lua_CFunction f, lua_State* luaVM ) { // Quick cull of unknown pointer range if ( CLuaCFunctions::IsNotFunction( f ) ) return true; // these are for OOP and establish the function to call, at which point the function is called normally so we get this called like so: // Call 1: f = CLuaClassDefs::NewIndex // Call 2: f = setElementHealth // ignore new index as it isn't registered as an LuaCFunction and just throws an assert in debug/causes issues. if ( f == (lua_CFunction)&CLuaClassDefs::NewIndex || f == (lua_CFunction)&CLuaClassDefs::StaticNewIndex || f == (lua_CFunction)&CLuaClassDefs::Index || f == (lua_CFunction)&CLuaClassDefs::Call || f == (lua_CFunction)&CLuaClassDefs::ToString || f == (lua_CFunction)&CLuaClassDefs::ReadOnly || f == (lua_CFunction)&CLuaClassDefs::WriteOnly ) { return true; } // Get associated resource CResource* pResource = m_pResourceManager->GetResourceFromLuaState( luaVM ); if ( !pResource ) return true; // Update execution time check pResource->GetVirtualMachine()->CheckExecutionTime(); // Check function right cache in resource bool bAllowed; // Check cached ACL rights if ( pResource->CheckFunctionRightCache( f, &bAllowed ) ) { // If in cache, and not allowed, do warning here if ( !bAllowed ) m_pScriptDebugging->LogBadAccess ( luaVM ); } // Not cached yet else { // If not in cache, do full check bAllowed = true; // Grab the function name we're calling. If it's one of our functions, see if we can use it. CLuaCFunction* pFunction = CLuaCFunctions::GetFunction ( f ); // works for anything registered for Lua VM // e.g. setElementHealth, setElementFrozen if ( pFunction ) { // If it's not one of lua's functions, see if we allow it bAllowed = CLuaDefs::CanUseFunction ( pFunction->GetName ().c_str (), luaVM, pFunction->IsRestricted () ); } // works for custom ACL functions e.g. // Element.position and other custom oop definitions not registered by string. else { // get the 2nd upvalue (ACL Name) const char* szName = lua_tostring ( luaVM, lua_upvalueindex ( 2 ) ); // if it has no name do nothing if ( szName != NULL && strcmp ( szName , "" ) != 0 ) { // get the function by name CLuaCFunction* pFunction = CLuaCFunctions::GetFunction ( szName ); if ( pFunction ) { // check the resource cache for the Lua name we looked up bAllowed = CLuaDefs::CanUseFunction ( szName, luaVM, pFunction->IsRestricted () ); } } } // Update cache in resource pResource->UpdateFunctionRightCache( f, bAllowed ); } g_pGame->GetDebugHookManager()->OnPreFunction( f, luaVM, bAllowed ); // If not allowed, do no more if ( !bAllowed ) return false; // Check if function timing is active if ( g_pStats->bFunctionTimingActive || g_pGame->GetDebugHookManager()->HasPostFunctionHooks() ) { // Check if hook needs applying if ( !ms_bRegisterdPostCallHook ) { OutputDebugLine ( "[Lua] Registering PostCallHook" ); ms_bRegisterdPostCallHook = true; lua_registerPostCallHook ( CLuaDefs::DidUseFunction ); } // Start to time the function ms_TimingFunctionStack.push_back ( STimingFunction( luaVM, f, GetTimeUs(), g_uiNetSentByteCounter ) ); } return true; }