예제 #1
0
int CLuaHTTPDefs::httpSetResponseCookie ( lua_State* luaVM )
{
    if ( lua_type ( luaVM, 1 ) == LUA_TSTRING && lua_type ( luaVM, 2 ) == LUA_TSTRING )
    {
        CLuaMain* pLuaMain = m_pLuaManager->GetVirtualMachine ( luaVM );
        if ( pLuaMain )
        {
            CResourceFile * file = pLuaMain->GetResourceFile();
            if ( file && file->GetType() == CResourceHTMLItem::RESOURCE_FILE_TYPE_HTML )
            {
                CResourceHTMLItem * html = (CResourceHTMLItem *)file;
                char* szHeaderName = (char *) lua_tostring ( luaVM, 1 );
				char* szHeaderValue = (char *) lua_tostring ( luaVM, 2 );
				html->SetResponseCookie ( szHeaderName, szHeaderValue );
                lua_pushboolean ( luaVM, true );
                return 1;
            }
            else
                m_pScriptDebugging->LogError ( luaVM, "httpSetResponseCookie: Can only be used in HTML scripts" );
        } 
        else
            m_pScriptDebugging->LogError ( luaVM, "httpSetResponseCookie" );
    }   
    else
        m_pScriptDebugging->LogBadType ( luaVM, "httpSetResponseCookie" );
    lua_pushboolean ( luaVM, false );
    return 1;
}
예제 #2
0
int CLuaHTTPDefs::httpWrite ( lua_State* luaVM )
{
    if ( lua_type ( luaVM, 1 ) == LUA_TSTRING )
    {
        CLuaMain* pLuaMain = m_pLuaManager->GetVirtualMachine ( luaVM );
        if ( pLuaMain )
        {
            CResourceFile * file = pLuaMain->GetResourceFile();
            if ( file && file->GetType() == CResourceHTMLItem::RESOURCE_FILE_TYPE_HTML )
            {
                CResourceHTMLItem * html = (CResourceHTMLItem *)file;
                unsigned long length = NULL;
                if ( lua_type ( luaVM, 2 ) == LUA_TNUMBER )
                    length = static_cast < unsigned long > ( lua_tonumber ( luaVM, 2 ) );
                else
                    length = lua_objlen ( luaVM, 1 );
                const char* szBuffer = lua_tolstring ( luaVM, 1, NULL );
                html->AppendToPageBuffer ( szBuffer, length );
                lua_pushboolean ( luaVM, true );
                return 1;
            }
            else
                m_pScriptDebugging->LogError ( luaVM, "httpWrite: Can only be used in HTML scripts" );
        } 
        else
            m_pScriptDebugging->LogError ( luaVM, "httpWrite" );
    }   
    else
        m_pScriptDebugging->LogBadType ( luaVM, "httpWrite" );
    lua_pushboolean ( luaVM, false );
    return 1;
}
예제 #3
0
int CLuaHTTPDefs::httpRequestLogin ( lua_State* luaVM )
{
    CLuaMain* pLuaMain = m_pLuaManager->GetVirtualMachine ( luaVM );
    if ( pLuaMain )
    {
        CResourceFile * file = pLuaMain->GetResourceFile();
        if ( file && file->GetType() == CResourceHTMLItem::RESOURCE_FILE_TYPE_HTML )
        {
            CResourceHTMLItem * html = (CResourceHTMLItem *)file;

			char szName[255];
			sprintf ( szName, "Basic realm=\"%s\"", m_pMainConfig->GetServerName ().c_str () );
			html->SetResponseHeader("WWW-Authenticate", szName);
			html->SetResponseCode ( 401 );
            lua_pushboolean ( luaVM, true );
            return 1;
        }
        else
            m_pScriptDebugging->LogError ( luaVM, "httpRequestLogin: Can only be used in HTML scripts" );
    } 
    else
        m_pScriptDebugging->LogError ( luaVM, "httpRequestLogin" );   
    lua_pushboolean ( luaVM, false );
    return 1;
}
예제 #4
0
int CLuaHTTPDefs::httpSetResponseCode ( lua_State* luaVM )
{
    if ( lua_type ( luaVM, 1 ) == LUA_TNUMBER  )
    {
        CLuaMain* pLuaMain = m_pLuaManager->GetVirtualMachine ( luaVM );
        if ( pLuaMain )
        {
            CResourceFile * file = pLuaMain->GetResourceFile();
            if ( file && file->GetType() == CResourceHTMLItem::RESOURCE_FILE_TYPE_HTML )
            {
                CResourceHTMLItem * html = (CResourceHTMLItem *)file;
                unsigned int responseCode = static_cast < unsigned int > ( lua_tonumber ( luaVM, 1 ) );
				html->SetResponseCode ( responseCode );
                lua_pushboolean ( luaVM, true );
                return 1;
            }
            else
                m_pScriptDebugging->LogError ( luaVM, "httpSetResponseCode: Can only be used in HTML scripts" );
        } 
        else
            m_pScriptDebugging->LogError ( luaVM, "httpSetResponseCode" );
    }   
    else
        m_pScriptDebugging->LogBadType ( luaVM, "httpSetResponseCode" );
    lua_pushboolean ( luaVM, false );
    return 1;
}
예제 #5
0
int CLuaHTTPDefs::httpClear ( lua_State* luaVM )
{
    CLuaMain* pLuaMain = m_pLuaManager->GetVirtualMachine ( luaVM );
    if ( pLuaMain )
    {
        CResourceFile * file = pLuaMain->GetResourceFile();
        if ( file && file->GetType() == CResourceHTMLItem::RESOURCE_FILE_TYPE_HTML )
        {
            CResourceHTMLItem * html = (CResourceHTMLItem *)file;
			html->ClearPageBuffer();
            lua_pushboolean ( luaVM, true );
            return 1;
        }
        else
            m_pScriptDebugging->LogError ( luaVM, "httpClear: Can only be used in HTML scripts" );
    } 
    else
        m_pScriptDebugging->LogError ( luaVM, "httpClear" );   
    lua_pushboolean ( luaVM, false );
    return 1;
}
예제 #6
0
void CLuaMain::SaveXML ( CXMLNode * pRootNode )
{
    list<CXMLFile *>::iterator iter;
    for ( iter = m_XMLFiles.begin(); iter != m_XMLFiles.end(); ++iter )
    {
        CXMLFile * file = (*iter);
        if ( file )
        {
            if ( file->GetRootNode() == pRootNode )
            {
                file->Write();
                break;
            }
        }
    }
    if ( m_pResource )
    {
        list < CResourceFile* > ::iterator iter = m_pResource->IterBegin ();
        for ( ; iter != m_pResource->IterEnd () ; ++iter )
        {
            CResourceFile* pResourceFile = *iter;
            if ( pResourceFile->GetType () == CResourceFile::RESOURCE_FILE_TYPE_CONFIG )
            {
                CResourceConfigItem* pConfigItem = static_cast < CResourceConfigItem* > ( pResourceFile );
                if ( pConfigItem->GetRoot () == pRootNode )
                {
                    CXMLFile* pFile = pConfigItem->GetFile ();
                    if ( pFile )
                    {
                        pFile->Write ();
                    }
                    break;
                }
            }
        }
    }
}
예제 #7
0
void CScriptDebugging::LogString ( const char* szPrePend, lua_State * luaVM, const char* szMessage, unsigned int uiMinimumDebugLevel, unsigned char ucRed, unsigned char ucGreen, unsigned char ucBlue )
{
    SString strText;
    lua_Debug debugInfo;

    // Initialize values for onDebugMessage
    SString strMsg  = szMessage;
    SString strFile = "";
    int     iLine   = -1;

    // Get a VM from somewhere
    if ( !luaVM && !m_LuaMainStack.empty () )
        luaVM = m_LuaMainStack.back ()->GetVM ();

    for ( int level = 1; level < 3; level++ )
    {
	    if ( luaVM && lua_getstack ( luaVM, level, &debugInfo ) )
	    {
		    lua_getinfo ( luaVM, "nlS", &debugInfo );

		     // Make sure this function isn't defined in a string (eg: from runcode)
            if ( debugInfo.source[0] == '@' )
            {
                // Get and store the location of the debug message
                strFile = debugInfo.source + 1;
                iLine   = debugInfo.currentline;

                // Populate a message to print/send (unless "info" type)
                if ( uiMinimumDebugLevel < 3 )
                    strText = SString ( "%s%s:%d: %s", szPrePend, strFile.c_str (), debugInfo.currentline, szMessage );
                // if the file isn't empty, stop trying any other levels
                break;
            }
            else
            {
                strFile = debugInfo.short_src;

                if ( uiMinimumDebugLevel < 3 )
                    strText = SString ( "%s%s %s", szPrePend, szMessage, strFile.c_str () );
                if ( strFile != "[string \"?\"]" ) // if the file isn't empty, stop trying any other levels
                    break;
            }
        }
        else
        {
            strText = SString ( "%s%s%s", szPrePend, m_strLineAndFile.c_str(), szMessage );
            // no point in trying other levels
            break;
        }
    }

    // Create a different message if type is "INFO"
    if ( uiMinimumDebugLevel > 2 )
        strText = SString ( "%s%s", szPrePend, szMessage );

    // Check whether onDebugMessage is currently being triggered
    if ( !m_bTriggeringOnDebugMessage )
    {
        // Make sure the state of onDebugMessage being triggered can be retrieved later
        m_bTriggeringOnDebugMessage = true;

        // Prepare onDebugMessage
        CLuaArguments Arguments;
        Arguments.PushString ( strMsg.c_str ( ) );
        Arguments.PushNumber ( uiMinimumDebugLevel );

        // Push the file name (if any)
        if ( strFile.length ( ) > 0 )
            Arguments.PushString ( strFile.c_str ( ) );
        else
            Arguments.PushNil ( );

        // Push the line (if any)
        if ( iLine > -1 )
            Arguments.PushNumber ( iLine );
        else
            Arguments.PushNil ( );
        
        // Call onDebugMessage
        g_pGame->GetMapManager ( )->GetRootElement ( )->CallEvent ( "onDebugMessage", Arguments );

        // Reset trigger state, so onDebugMessage can be called again at a later moment
        m_bTriggeringOnDebugMessage = false;
    }

    // Log it to the file if enough level
    if ( m_uiLogFileLevel >= uiMinimumDebugLevel )
    {
        PrintLog ( strText );
    }

    // Log to console
    CLogger::LogPrintf( "%s\n", strText.c_str () );

    // Not sure what this is for, seems pretty useless
    if ( m_uiHtmlLogLevel >= uiMinimumDebugLevel )
    {
        if ( luaVM )
        {
            CLuaMain* pLuaMain = g_pGame->GetLuaManager()->GetVirtualMachine ( luaVM );
            if ( pLuaMain )
            {
                CResourceFile * file = pLuaMain->GetResourceFile();
                if ( file && file->GetType() == CResourceHTMLItem::RESOURCE_FILE_TYPE_HTML )
                {
                    CResourceHTMLItem * html = (CResourceHTMLItem *)file;
                    html->AppendToPageBuffer ( strText );
                    html->AppendToPageBuffer ( "<br/>" );
                }
            }
        }
    }

    // Tell the players
    Broadcast ( CDebugEchoPacket ( strText, uiMinimumDebugLevel, ucRed, ucGreen, ucBlue ), uiMinimumDebugLevel );
}
예제 #8
0
///////////////////////////////////////////////////////////////
//
// CResourceChecker::CheckResourceForIssues
//
// Check each file and do the appropriate action
//
///////////////////////////////////////////////////////////////
void CResourceChecker::CheckResourceForIssues( CResource* pResource, const string& strResourceZip )
{
    m_strMinClientReqFromMetaXml = pResource->GetMinClientReqFromMetaXml();
    m_strMinServerReqFromMetaXml = pResource->GetMinServerReqFromMetaXml();
    m_strReqClientVersion = "";
    m_strReqServerVersion = "";
    m_strReqClientReason = "";
    m_strReqServerReason = "";

    m_ulDeprecatedWarningCount = 0;
    m_upgradedFullPathList.clear ();

    // Check each file in the resource
    list < CResourceFile* > ::iterator iterf = pResource->IterBegin ();
    for ( ; iterf != pResource->IterEnd (); iterf++ )
    {
        CResourceFile* pResourceFile = *iterf;
        // Skip this one if validate=false in the meta.xml
        if ( stricmp( pResourceFile->GetMetaFileAttribute ( "validate" ).c_str (), "false" ) )
        {
            string strPath;
            if ( pResource->GetFilePath ( pResourceFile->GetName(), strPath ) )
            {
                CResourceFile::eResourceType type = pResourceFile->GetType ();

                bool bScript;
                bool bClient;
                if ( type == CResourceFile::RESOURCE_FILE_TYPE_SCRIPT )
                {
                    bScript = true;
                    bClient = false;
                }
                else
                if ( type == CResourceFile::RESOURCE_FILE_TYPE_CLIENT_SCRIPT )
                {
                    bScript = true;
                    bClient = true;
                }
                else
                if ( type == CResourceFile::RESOURCE_FILE_TYPE_CLIENT_FILE )
                {
                    bScript = false;
                    bClient = true;
                }
                else
                    continue;

                CheckFileForIssues ( strPath, pResourceFile->GetName(), pResource->GetName (), bScript, bClient, false );
            }
        }
    }

    // Also check meta.xml
    {
        SString strFilename = "meta.xml";
        string strPath;
        if ( pResource->GetFilePath ( strFilename, strPath ) )
        {
            CheckFileForIssues ( strPath, strFilename, pResource->GetName (), false, false, true );
        }
    }

    // Output deprecated warning if required
    if ( m_ulDeprecatedWarningCount )
    {
        CLogger::LogPrintf ( "Some files in '%s' use deprecated functions.\n", pResource->GetName ().c_str () );
        CLogger::LogPrintf ( "Use the 'upgrade' command to perform a basic upgrade of resources.\n" );
    }

    // Handle upgrading a zip file if required
    if ( m_upgradedFullPathList.size () )
    {
        if ( pResource->IsResourceZip () )
        {
            string strOrigZip = strResourceZip;
            string strTempZip = strResourceZip.substr ( 0, strResourceZip.length () - 4 ) + "_tmp.zip";

            string strCacheDir = pResource->GetResourceCacheDirectoryPath ();

            vector < string > pathInArchiveList;

            for ( unsigned long i = 0 ; i < m_upgradedFullPathList.size () ; i++ )
            {
                string strFullPath = m_upgradedFullPathList[i];
                string strPathInArchive = strFullPath.substr ( strCacheDir.length () );
                pathInArchiveList.push_back ( strPathInArchive );
            }

            File::Delete( strTempZip.c_str () );

            if ( !ReplaceFilesInZIP( strOrigZip, strTempZip, pathInArchiveList, m_upgradedFullPathList ) )
            {
                CLogger::LogPrintf ( "Failed to upgrade (ReplaceFilesInZIP) '%s'\n", strOrigZip.c_str () );
            }
            else
            {
                if ( !RenameBackupFile( strOrigZip, ".old" ) )
                {
                    CLogger::LogPrintf ( "Failed to upgrade (RenameBackupFile) '%s'\n", strOrigZip.c_str () );
                }
                else
                {
                    if ( File::Rename( strTempZip.c_str (), strOrigZip.c_str () ) )
                    {
                        CLogger::LogPrintf ( "Failed to upgrade (rename) '%s'\n", strOrigZip.c_str () );
                    }
                }
            }

            File::Delete( strTempZip.c_str () );
        }
    }

    // Check LC_COLLATE is correct
    if ( strcoll( "a", "B" ) < 0 )
    {
        CLogger::LogPrintf ( "ERROR: LC_COLLATE is not set correctly\n" );
    }
}
예제 #9
0
void CScriptDebugging::LogString ( const char* szPrePend, const SLuaDebugInfo& luaDebugInfo, const char* szMessage, unsigned int uiMinimumDebugLevel, unsigned char ucRed, unsigned char ucGreen, unsigned char ucBlue )
{
    SString strText = ComposeErrorMessage( szPrePend, luaDebugInfo, szMessage );

    // Create a different message if type is "INFO"
    if ( uiMinimumDebugLevel > 2 )
        strText = SString ( "%s%s", szPrePend, szMessage );

    // Check whether onDebugMessage is currently being triggered
    if ( !m_bTriggeringOnDebugMessage )
    {
        // Make sure the state of onDebugMessage being triggered can be retrieved later
        m_bTriggeringOnDebugMessage = true;

        // Prepare onDebugMessage
        CLuaArguments Arguments;
        Arguments.PushString ( szMessage );
        Arguments.PushNumber ( uiMinimumDebugLevel );

        // Push the file name (if any)
        if ( !luaDebugInfo.strFile.empty() )
            Arguments.PushString ( luaDebugInfo.strFile );
        else
            Arguments.PushNil ( );

        // Push the line (if any)
        if ( luaDebugInfo.iLine != INVALID_LINE_NUMBER )
            Arguments.PushNumber ( luaDebugInfo.iLine );
        else
            Arguments.PushNil ( );
        
        // Call onDebugMessage
        g_pGame->GetMapManager ( )->GetRootElement ( )->CallEvent ( "onDebugMessage", Arguments );

        // Reset trigger state, so onDebugMessage can be called again at a later moment
        m_bTriggeringOnDebugMessage = false;
    }

    // Log it to the file if enough level
    if ( m_uiLogFileLevel >= uiMinimumDebugLevel )
    {
        PrintLog ( strText );
    }

    // Log to console
    CLogger::LogPrintf( "%s\n", strText.c_str () );

#if 0
    // Not sure what this is for, seems pretty useless
    if ( m_uiHtmlLogLevel >= uiMinimumDebugLevel )
    {
        if ( luaVM )
        {
            CLuaMain* pLuaMain = g_pGame->GetLuaManager()->GetVirtualMachine ( luaVM );
            if ( pLuaMain )
            {
                CResourceFile * file = pLuaMain->GetResourceFile();
                if ( file && file->GetType() == CResourceHTMLItem::RESOURCE_FILE_TYPE_HTML )
                {
                    CResourceHTMLItem * html = (CResourceHTMLItem *)file;
                    html->AppendToPageBuffer ( strText );
                    html->AppendToPageBuffer ( "<br/>" );
                }
            }
        }
    }
#endif

    // Tell the players
    Broadcast ( CDebugEchoPacket ( strText, uiMinimumDebugLevel, ucRed, ucGreen, ucBlue ), uiMinimumDebugLevel );
}