int CLuaFunctionDefs::GetResourceDynamicElementRoot ( lua_State* luaVM ) { CResource* pResource = NULL; CScriptArgReader argStream ( luaVM ); argStream.ReadUserData ( pResource ); if ( !argStream.HasErrors () ) { if ( pResource ) { CClientEntity* pEntity = pResource->GetResourceDynamicEntity(); if ( pEntity ) { lua_pushelement ( luaVM, pEntity ); return 1; } else m_pScriptDebugging->LogError ( luaVM, "getResourceDynamicElementRoot: Resource %s Is Not Currently Running", pResource->GetName() ); } else m_pScriptDebugging->LogBadPointer ( luaVM, "resource", 1 ); } else m_pScriptDebugging->LogCustom ( luaVM, argStream.GetFullErrorMessage() ); lua_pushboolean ( luaVM, false ); return 1; }
int CLuaFunctionDefs::GetResourceDynamicElementRoot ( lua_State* luaVM ) { if ( lua_type ( luaVM, 1 ) == LUA_TLIGHTUSERDATA ) { CResource* pResource = lua_toresource ( luaVM, 1 ); if ( pResource ) { CClientEntity* pEntity = pResource->GetResourceDynamicEntity(); if ( pEntity ) { lua_pushelement ( luaVM, pEntity ); return 1; } else m_pScriptDebugging->LogError ( luaVM, "getResourceDynamicElementRoot: Resource %s Is Not Currently Running", pResource->GetName() ); } else m_pScriptDebugging->LogBadPointer ( luaVM, "getResourceDynamicElementRoot", "resource", 1 ); } else m_pScriptDebugging->LogBadType ( luaVM, "getResourceDynamicElementRoot" ); lua_pushboolean ( luaVM, false ); return 1; }
int CLuaFunctionDefs::CreateBrowser ( lua_State* luaVM ) { // texture createBrowser ( int width, int height, bool isLocal [, bool transparent = false] ) CVector2D vecSize; bool bIsLocal; bool bTransparent; CScriptArgReader argStream ( luaVM ); argStream.ReadVector2D ( vecSize ); argStream.ReadBool ( bIsLocal ); argStream.ReadBool ( bTransparent, false ); if ( !argStream.HasErrors () ) { if ( !bIsLocal && !g_pCore->GetWebCore()->GetRemotePagesEnabled () ) { lua_pushboolean ( luaVM, false ); return 1; } CLuaMain* pLuaMain = m_pLuaManager->GetVirtualMachine ( luaVM ); if ( pLuaMain ) { CResource* pParentResource = pLuaMain->GetResource (); CClientWebBrowser* pBrowserTexture = g_pClientGame->GetManager ()->GetRenderElementManager ()->CreateWebBrowser ( (int)vecSize.fX, (int)vecSize.fY, bIsLocal, bTransparent ); if ( pBrowserTexture ) { // Make it a child of the resource's file root ** CHECK Should parent be pFileResource, and element added to pParentResource's ElementGroup? ** pBrowserTexture->SetParent ( pParentResource->GetResourceDynamicEntity () ); // Set our owner resource pBrowserTexture->SetResource ( pParentResource ); } lua_pushelement ( luaVM, pBrowserTexture ); return 1; } } else m_pScriptDebugging->LogCustom ( luaVM, argStream.GetFullErrorMessage () ); lua_pushboolean ( luaVM, false ); return 1; }
// // Material/string // void MixedReadMaterialString(CScriptArgReader& argStream, CClientMaterial*& pMaterialElement) { pMaterialElement = NULL; if (!argStream.NextIsString()) argStream.ReadUserData(pMaterialElement); else { SString strFilePath; argStream.ReadString(strFilePath); // If no element, auto find/create one CLuaMain* pLuaMain = g_pClientGame->GetLuaManager()->GetVirtualMachine(argStream.m_luaVM); CResource* pParentResource = pLuaMain ? pLuaMain->GetResource() : NULL; if (pParentResource) { CResource* pFileResource = pParentResource; SString strPath, strMetaPath; if (CResourceManager::ParseResourcePathInput(strFilePath, pFileResource, &strPath, &strMetaPath)) { SString strUniqueName = SString("%s*%s*%s", pParentResource->GetName(), pFileResource->GetName(), strMetaPath.c_str()).Replace("\\", "/"); pMaterialElement = g_pClientGame->GetManager()->GetRenderElementManager()->FindAutoTexture(strPath, strUniqueName); if (pMaterialElement) { // Check if brand new if (!pMaterialElement->GetParent()) // Make it a child of the resource's file root ** CHECK Should parent be pFileResource, and element added to pParentResource's // ElementGroup? ** pMaterialElement->SetParent(pParentResource->GetResourceDynamicEntity()); } else argStream.SetCustomError(strFilePath, "Error loading image"); } else argStream.SetCustomError(strFilePath, "Bad file path"); } } }
bool CMapEventManager::Call ( const char* szName, const CLuaArguments& Arguments, class CClientEntity* pSource, class CClientEntity* pThis ) { // Check if no events if ( !m_bHasEvents ) return false; // Check if no events with a name match EventsIterPair itPair = m_EventsMap.equal_range ( szName ); if ( itPair.first == itPair.second ) return false; TIMEUS startTimeCall = GetTimeUs (); SString strStatus; // Check for multi-threading slipups assert ( IsMainThread () ); // Call all the events with matching names bool bCalled = false; bool bIsAlreadyIterating = m_bIteratingList; m_bIteratingList = true; // Copy the results into a array in case m_EventsMap is modified during the call std::vector< CMapEvent* > matchingEvents; for ( EventsIter iter = itPair.first ; iter != itPair.second ; ++iter ) matchingEvents.push_back(iter->second); for ( std::vector< CMapEvent* >::iterator iter = matchingEvents.begin() ; iter != matchingEvents.end() ; ++iter ) { CMapEvent* pMapEvent = *iter; // If it's not being destroyed if ( !pMapEvent->IsBeingDestroyed () ) { // Compare the names dassert ( strcmp ( pMapEvent->GetName (), szName ) == 0 ); { // Call if propagated? if ( pSource == pThis || pMapEvent->IsPropagated () ) { // Grab the current VM lua_State* pState = pMapEvent->GetVM ()->GetVM (); LUA_CHECKSTACK ( pState, 1 ); // Ensure some room #if MTA_DEBUG int luaStackPointer = lua_gettop ( pState ); #endif TIMEUS startTime = GetTimeUs(); // Aspect ratio adjustment bodges if ( pMapEvent->ShouldAllowAspectRatioAdjustment() ) { g_bAllowAspectRatioAdjustment = true; if ( pMapEvent->ShouldForceAspectRatioAdjustment() ) g_pCore->GetGraphics()->SetAspectRatioAdjustmentEnabled( true ); } // Record event for the crash dump writer static bool bEnabled = ( g_pCore->GetDiagnosticDebug () == EDiagnosticDebug::LUA_TRACE_0000 ); if ( bEnabled ) g_pCore->LogEvent ( 0, "Lua Event", pMapEvent->GetVM ()->GetScriptName (), szName ); // Store the current values of the globals lua_getglobal ( pState, "source" ); CLuaArgument OldSource ( pState, -1 ); lua_pop( pState, 1 ); lua_getglobal ( pState, "this" ); CLuaArgument OldThis ( pState, -1 ); lua_pop( pState, 1 ); lua_getglobal ( pState, "sourceResource" ); CLuaArgument OldResource ( pState, -1 ); lua_pop( pState, 1 ); lua_getglobal ( pState, "sourceResourceRoot" ); CLuaArgument OldResourceRoot ( pState, -1 ); lua_pop( pState, 1 ); lua_getglobal ( pState, "eventName" ); CLuaArgument OldEventName ( pState, -1 ); lua_pop( pState, 1 ); // Set the "source", "this", "sourceResource" and the "sourceResourceRoot" globals on that VM lua_pushelement ( pState, pSource ); lua_setglobal ( pState, "source" ); lua_pushelement ( pState, pThis ); lua_setglobal ( pState, "this" ); CLuaMain* pLuaMain = g_pClientGame->GetScriptDebugging()->GetTopLuaMain(); CResource* pSourceResource = pLuaMain ? pLuaMain->GetResource() : NULL; if ( pSourceResource ) { lua_pushresource ( pState, pSourceResource ); lua_setglobal ( pState, "sourceResource" ); lua_pushelement ( pState, pSourceResource->GetResourceDynamicEntity() ); lua_setglobal ( pState, "sourceResourceRoot" ); } else { lua_pushnil ( pState ); lua_setglobal ( pState, "sourceResource" ); lua_pushnil ( pState ); lua_setglobal ( pState, "sourceResourceRoot" ); } lua_pushstring ( pState, szName ); lua_setglobal ( pState, "eventName" ); // Call it pMapEvent->Call ( Arguments ); bCalled = true; // Reset the globals on that VM OldSource.Push ( pState ); lua_setglobal ( pState, "source" ); OldThis.Push ( pState ); lua_setglobal ( pState, "this" ); OldResource.Push ( pState ); lua_setglobal ( pState, "sourceResource" ); OldResourceRoot.Push ( pState ); lua_setglobal ( pState, "sourceResourceRoot" ); OldEventName.Push ( pState ); lua_setglobal ( pState, "eventName" ); #if MTA_DEBUG assert ( lua_gettop ( pState ) == luaStackPointer ); #endif // Aspect ratio adjustment bodges if ( pMapEvent->ShouldAllowAspectRatioAdjustment() ) { g_pCore->GetGraphics()->SetAspectRatioAdjustmentEnabled( false ); g_bAllowAspectRatioAdjustment = false; } TIMEUS deltaTimeUs = GetTimeUs() - startTime; if ( deltaTimeUs > 3000 ) if ( IS_TIMING_CHECKPOINTS() ) strStatus += SString ( " (%s %d ms)", pMapEvent->GetVM ()->GetScriptName (), deltaTimeUs / 1000 ); CClientPerfStatLuaTiming::GetSingleton ()->UpdateLuaTiming ( pMapEvent->GetVM (), szName, deltaTimeUs ); } } } } // Clean out the trash if we're no longer calling events. if ( !bIsAlreadyIterating ) { TakeOutTheTrash (); // We're no longer iterating the list m_bIteratingList = false; } if ( IS_TIMING_CHECKPOINTS() ) { TIMEUS deltaTimeUs = GetTimeUs() - startTimeCall; if ( deltaTimeUs > 5000 ) TIMING_DETAIL( SString ( "CMapEventManager::Call ( %s, ... ) took %d ms ( %s )", szName, deltaTimeUs / 1000, *strStatus ) ); } // Return whether we called atleast one func or not return bCalled; }
int CLuaFileDefs::fileCreate ( lua_State* luaVM ) { // file fileCreate ( string filePath ) SString filePath; CScriptArgReader argStream ( luaVM ); argStream.ReadString ( filePath ); if ( !argStream.HasErrors () ) { // Grab our lua VM CLuaMain* pLuaMain = m_pLuaManager->GetVirtualMachine ( luaVM ); if ( !g_pNet->ValidateBinaryFileName ( filePath ) ) { argStream.SetCustomError ( SString ( "Filename not allowed %s", *filePath ), "File error" ); } else if ( pLuaMain ) { SString strAbsPath; SString strMetaPath; CResource* pThisResource = pLuaMain->GetResource (); CResource* pResource = pThisResource; if ( CResourceManager::ParseResourcePathInput ( filePath, pResource, strAbsPath, strMetaPath ) ) { // Inform file verifier g_pClientGame->GetResourceManager()->FileModifedByScript( strAbsPath ); // Make sure the destination folder exist so we can create the file MakeSureDirExists ( strAbsPath.c_str () ); // Create the file to create eAccessType accessType = filePath[0] == '@' ? eAccessType::ACCESS_PRIVATE : eAccessType::ACCESS_PUBLIC; CScriptFile* pFile = new CScriptFile( pThisResource->GetScriptID( ), strMetaPath.c_str(), DEFAULT_MAX_FILESIZE, accessType); assert ( pFile ); // Try to load it if ( pFile->Load ( pResource, CScriptFile::MODE_CREATE ) ) { // Make it a child of the resource's file root pFile->SetParent ( pResource->GetResourceDynamicEntity () ); // Add it to the scrpt resource element group CElementGroup* pGroup = pThisResource->GetElementGroup (); if ( pGroup ) { pGroup->Add ( pFile ); } // Success. Return the file. lua_pushelement ( luaVM, pFile ); return 1; } else { // Delete the file again delete pFile; // Output error argStream.SetCustomError ( SString ( "Unable to create %s", *filePath ), "File error" ); } } } } if ( argStream.HasErrors () ) m_pScriptDebugging->LogCustom ( luaVM, argStream.GetFullErrorMessage () ); // Failed lua_pushboolean ( luaVM, false ); return 1; }
int CLuaFileDefs::fileOpen ( lua_State* luaVM ) { // file fileOpen ( string filePath [, bool readOnly = false ] ) SString filePath; bool readOnly; CScriptArgReader argStream ( luaVM ); argStream.ReadString ( filePath ); argStream.ReadBool ( readOnly, false ); if ( !argStream.HasErrors () ) { // Grab our lua VM CLuaMain* pLuaMain = m_pLuaManager->GetVirtualMachine ( luaVM ); if ( pLuaMain ) { SString strAbsPath; SString strMetaPath; CResource* pThisResource = pLuaMain->GetResource (); CResource* pResource = pThisResource; if ( CResourceManager::ParseResourcePathInput ( filePath, pResource, strAbsPath, strMetaPath ) ) { // Inform file verifier if ( !readOnly ) g_pClientGame->GetResourceManager()->FileModifedByScript( strAbsPath ); // Create the file to create eAccessType accessType = filePath[0] == '@' ? eAccessType::ACCESS_PRIVATE : eAccessType::ACCESS_PUBLIC; CScriptFile* pFile = new CScriptFile( pThisResource->GetScriptID( ), strMetaPath.c_str( ), DEFAULT_MAX_FILESIZE, accessType ); assert ( pFile ); // Try to load it if ( pFile->Load ( pResource, readOnly ? CScriptFile::MODE_READ : CScriptFile::MODE_READWRITE ) ) { // Make it a child of the resource's file root pFile->SetParent ( pResource->GetResourceDynamicEntity () ); // Grab its owner resource CResource* pParentResource = pLuaMain->GetResource (); if ( pParentResource ) { // Add it to the scrpt resource element group CElementGroup* pGroup = pParentResource->GetElementGroup (); if ( pGroup ) { pGroup->Add ( pFile ); } } // Success. Return the file. lua_pushelement ( luaVM, pFile ); return 1; } else { // Delete the file again delete pFile; // Output error argStream.SetCustomError( SString( "unable to load file '%s'", *filePath ) ); } } } } if ( argStream.HasErrors () ) m_pScriptDebugging->LogCustom ( luaVM, argStream.GetFullErrorMessage() ); // Failed lua_pushboolean ( luaVM, false ); return 1; }
int CLuaFileDefs::fileOpen ( lua_State* luaVM ) { // file fileOpen ( string filePath [, bool readOnly = false ] ) SString strInputPath; bool bReadOnly; CScriptArgReader argStream ( luaVM ); argStream.ReadString ( strInputPath ); argStream.ReadBool ( bReadOnly, false ); if ( argStream.NextIsUserData () ) m_pScriptDebugging->LogCustom ( luaVM, "fileOpen may be using an outdated syntax. Please check and update." ); if ( !argStream.HasErrors () ) { // Grab our lua VM CLuaMain* pLuaMain = m_pLuaManager->GetVirtualMachine ( luaVM ); if ( pLuaMain ) { SString strAbsPath; SString strMetaPath; CResource* pThisResource = pLuaMain->GetResource (); CResource* pResource = pThisResource; if ( CResourceManager::ParseResourcePathInput ( strInputPath, pResource, &strAbsPath, &strMetaPath ) ) { CheckCanModifyOtherResource( argStream, pThisResource, pResource ); CheckCanAccessOtherResourceFile( argStream, pThisResource, pResource, strAbsPath, &bReadOnly ); if ( !argStream.HasErrors() ) { #ifndef MTA_CLIENT // IF SERVER // Create the file to create CScriptFile* pFile = new CScriptFile ( pThisResource->GetScriptID (), strMetaPath, DEFAULT_MAX_FILESIZE ); #else eAccessType accessType = strInputPath[0] == '@' ? eAccessType::ACCESS_PRIVATE : eAccessType::ACCESS_PUBLIC; CScriptFile* pFile = new CScriptFile ( pThisResource->GetScriptID (), strMetaPath, DEFAULT_MAX_FILESIZE, accessType ); #endif // Try to load it if ( pFile->Load ( pResource, bReadOnly ? CScriptFile::MODE_READ : CScriptFile::MODE_READWRITE ) ) { #ifdef MTA_CLIENT // Make it a child of the resource's file root pFile->SetParent ( pResource->GetResourceDynamicEntity () ); #endif // Grab its owner resource CResource* pParentResource = pLuaMain->GetResource (); if ( pParentResource ) { // Add it to the scrpt resource element group CElementGroup* pGroup = pParentResource->GetElementGroup (); if ( pGroup ) { pGroup->Add ( pFile ); } } // Success. Return the file. lua_pushelement ( luaVM, pFile ); return 1; } else { // Delete the file again delete pFile; // Output error argStream.SetCustomError ( SString ( "unable to load file '%s'", *strInputPath ) ); } } } } } if ( argStream.HasErrors () ) m_pScriptDebugging->LogCustom ( luaVM, argStream.GetFullErrorMessage () ); // Failed lua_pushboolean ( luaVM, false ); return 1; }