void CRemoteCall::ProgressCallback(double sizeJustDownloaded, double totalDownloaded, char * data, size_t dataLength, void * obj, bool complete, int error) { //printf("Progress: %s\n", data); if ( complete ) { CRemoteCall * call = (CRemoteCall*)obj; if ( g_pGame->GetRemoteCalls()->CallExists(call) ) { //printf("RECIEVED: %s\n", data); CLuaArguments arguments; arguments.ReadFromJSONString ( data ); arguments.Call ( call->m_VM, call->m_iFunction); g_pGame->GetRemoteCalls()->Remove(call); // delete ourselves } } else if ( error ) { CRemoteCall * call = (CRemoteCall*)obj; if ( g_pGame->GetRemoteCalls()->CallExists(call) ) { CLuaArguments arguments; arguments.PushString("ERROR"); arguments.PushNumber(error); arguments.Call ( call->m_VM, call->m_iFunction); g_pGame->GetRemoteCalls()->Remove(call); // delete ourselves } } }
void CElement::ReadCustomData ( CLuaMain* pLuaMain, CEvents* pEvents ) { assert ( pLuaMain ); assert ( pEvents ); // Got an XML node? if ( m_pXMLNode ) { // Iterate the attributes of our XML node CXMLAttributes* pAttributes = &(m_pXMLNode->GetAttributes ()); unsigned int uiAttributeCount = pAttributes->Count (); for ( unsigned int uiIndex = 0; uiIndex < uiAttributeCount; uiIndex++ ) { // Grab the node (we can assume it exists here) CXMLAttribute* pAttribute = pAttributes->Get ( uiIndex ); // Make a lua argument from it and set the content CLuaArguments args; if ( !args.ReadFromJSONString ( pAttribute->GetValue ().c_str() ) ) args.PushString ( pAttribute->GetValue ().c_str () ); // Don't trigger onElementDataChanged event SetCustomData ( pAttribute->GetName ().c_str (), *args[0], pLuaMain, g_pGame->GetConfig ()->GetSyncMapElementData (), NULL, false ); } } }
int CLuaFunctionDefs::fromJSON ( lua_State* luaVM ) { // Got a string argument? SString strJsonString = ""; CScriptArgReader argStream ( luaVM ); argStream.ReadString ( strJsonString ); if ( !argStream.HasErrors ( ) ) { // Read it into lua arguments CLuaArguments Converted; if ( Converted.ReadFromJSONString ( strJsonString ) ) { // Return it as data Converted.PushArguments ( luaVM ); return Converted.Count (); } } else m_pScriptDebugging->LogCustom ( luaVM, argStream.GetFullErrorMessage() ); // Failed lua_pushnil ( luaVM ); return 1; }
void CRemoteCall::DownloadFinishedCallback(const SHttpDownloadResult& result) { CRemoteCall* pCall = (CRemoteCall*)result.pObj; if (!g_pClientGame->GetRemoteCalls()->CallExists(pCall)) return; CLuaArguments arguments; if (pCall->IsLegacy()) { if (result.bSuccess) { if (pCall->IsFetch()) { arguments.PushString(std::string(result.pData, result.dataSize)); arguments.PushNumber(0); } else arguments.ReadFromJSONString(result.pData); } else { arguments.PushString("ERROR"); arguments.PushNumber(result.iErrorCode); } } else { // Append response body arguments.PushString(std::string(result.pData, result.dataSize)); // Append info table CLuaArguments info; info.PushString("success"); info.PushBoolean(result.iErrorCode >= 200 && result.iErrorCode <= 299); info.PushString("statusCode"); info.PushNumber(result.iErrorCode); // Headers as a subtable CLuaArguments headers; for (auto iter : result.headers ) { headers.PushString(iter.first); headers.PushString(iter.second); } info.PushString("headers"); info.PushTable(&headers); arguments.PushTable(&info); } // Append stored arguments if (pCall->IsFetch()) for (uint i = 0; i < pCall->GetFetchArguments().Count(); i++ ) arguments.PushArgument(*(pCall->GetFetchArguments()[i])); arguments.Call(pCall->m_VM, pCall->m_iFunction); g_pClientGame->GetRemoteCalls()->Remove(pCall); }
bool CRemoteCall::ProgressCallback(double sizeJustDownloaded, double totalDownloaded, char * data, size_t dataLength, void * obj, bool complete, int error) { //printf("Progress: %s\n", data); if ( complete ) { CRemoteCall * call = (CRemoteCall*)obj; if ( g_pGame->GetRemoteCalls()->CallExists(call) ) { //printf("RECIEVED: %s\n", data); CLuaArguments arguments; if ( call->IsFetch () ) { arguments.PushString ( std::string ( data, dataLength ) ); arguments.PushNumber ( 0 ); for ( uint i = 0 ; i < call->GetFetchArguments ().Count () ; i++ ) arguments.PushArgument ( *( call->GetFetchArguments ()[i] ) ); } else arguments.ReadFromJSONString ( data ); arguments.Call ( call->m_VM, call->m_iFunction); g_pGame->GetRemoteCalls()->Remove(call); // delete ourselves return true; } } else if ( error ) { CRemoteCall * call = (CRemoteCall*)obj; if ( g_pGame->GetRemoteCalls()->CallExists(call) ) { CLuaArguments arguments; arguments.PushString("ERROR"); arguments.PushNumber(error); if ( call->IsFetch () ) for ( uint i = 0 ; i < call->GetFetchArguments ().Count () ; i++ ) arguments.PushArgument ( *( call->GetFetchArguments ()[i] ) ); arguments.Call ( call->m_VM, call->m_iFunction); g_pGame->GetRemoteCalls()->Remove(call); // delete ourselves return true; } } return false; // Possible problem }
int CLuaFunctionDefs::fromJSON ( lua_State* luaVM ) { // Got a string argument? if ( lua_type ( luaVM, 1 ) == LUA_TSTRING ) { // Grab the JSON string const char* szJSONString = lua_tostring ( luaVM, 1 ); // Read it into lua arguments CLuaArguments Converted; if ( Converted.ReadFromJSONString ( szJSONString ) ) { // Return it as data Converted.PushArguments ( luaVM ); return Converted.Count (); } } // Failed lua_pushnil ( luaVM ); return 1; }
int CLuaFunctionDefs::Get ( lua_State* luaVM ) { CResource* pResource = m_pLuaManager->GetVirtualMachine ( luaVM )->GetResource (); SString strSetting; CLuaArguments Args; CScriptArgReader argStream ( luaVM ); argStream.ReadString ( strSetting ); if ( !argStream.HasErrors () ) { unsigned int uiIndex = 0; bool bDeleteNode; // Extract attribute name if setting to be gotten has three parts i.e. resname.settingname.attributename SString strAttribute = "value"; vector < SString > Result; strSetting.Split ( ".", Result ); if ( Result.size () == 3 && Result[2].length () ) { strAttribute = Result[2]; } // Get the setting CXMLNode *pSubNode, *pNode = g_pGame->GetSettings ()->Get ( pResource->GetName ().c_str (), strSetting.c_str (), bDeleteNode ); // Only proceed if we have a valid node if ( pNode ) { // Argument count unsigned int uiArgCount = 1; // See if we need to return a table with single or multiple entries if ( pNode->GetSubNodeCount () == 0 ) { // See if required attribute exists CXMLAttribute *pAttribute = pNode->GetAttributes ().Find ( strAttribute.c_str () ); if ( !pAttribute ) { if ( bDeleteNode ) delete pNode; lua_pushboolean ( luaVM, false ); return 1; } // We only have a single entry for a specific setting, so output a string const std::string& strDataValue = pAttribute->GetValue (); if ( !Args.ReadFromJSONString ( strDataValue.c_str () ) ) { // No valid JSON? Parse as plain text Args.PushString ( strDataValue ); } Args.PushArguments ( luaVM ); uiArgCount = Args.Count (); /* Don't output a table because although it is more consistent with the multiple values output below, ** due to lua's implementation of associative arrays (assuming we use the "setting-name", "value" key-value pairs) ** it would require the scripter to walk through an array that only has a single entry which is a Bad Thing, performance wise. ** PUSH_SETTING ( pNode ); Args.PushAsTable ( luaVM ); **/ } else { // We need to return multiply entries, so push all subnodes while ( ( pSubNode = pNode->FindSubNode ( "setting", uiIndex++ ) ) ) { CXMLAttributes& attributes = pSubNode->GetAttributes (); Args.PushString ( attributes.Find ( "name" )->GetValue () ); const std::string& strDataValue = attributes.Find ( "value" )->GetValue (); if ( !Args.ReadFromJSONString ( strDataValue.c_str () ) ) { Args.PushString ( strDataValue ); } } // Push a table and return Args.PushAsTable ( luaVM ); } // Check if we have to delete the node if ( bDeleteNode ) delete pNode; return uiArgCount; } } else m_pScriptDebugging->LogCustom ( luaVM, argStream.GetFullErrorMessage () ); lua_pushboolean ( luaVM, false ); return 1; }