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 ); } } }
void CServerBrowser::SetServerPassword ( std::string strHost, std::string strPassword ) { CXMLNode* pConfig = CCore::GetSingletonPtr ()->GetConfig (); CXMLNode* pServerPasswords = pConfig->FindSubNode ( CONFIG_NODE_SERVER_SAVED ); if ( !pServerPasswords ) { pServerPasswords = pConfig ->CreateSubNode ( CONFIG_NODE_SERVER_SAVED ); } //Check if the server password already exists for ( unsigned int i = 0 ; i < pServerPasswords->GetSubNodeCount() ; i++ ) { CXMLAttributes* pAttributes = &(pServerPasswords->GetSubNode(i)->GetAttributes()); if ( pAttributes->Find( "host" ) ) { if ( CXMLAttribute* pHost = pAttributes->Find ( "host" ) ) { std::string strXMLHost = pHost->GetValue(); if ( strXMLHost == strHost ) { CXMLAttribute* pPassword = pAttributes->Create( "password" ); pPassword->SetValue(strPassword.c_str()); return; } } } } // Otherwise create the node from scratch CXMLNode* pNode = pServerPasswords->CreateSubNode( "server" ); CXMLAttribute* pHostAttribute = pNode->GetAttributes().Create ( "host" ); pHostAttribute->SetValue(strHost.c_str()); CXMLAttribute* pPasswordAttribute = pNode->GetAttributes().Create ( "password" ); pPasswordAttribute->SetValue(strPassword.c_str()); }
std::string CServerBrowser::GetServerPassword ( std::string strHost ) { CXMLNode* pConfig = CCore::GetSingletonPtr ()->GetConfig (); CXMLNode* pServerPasswords = pConfig->FindSubNode ( CONFIG_NODE_SERVER_SAVED ); if ( !pServerPasswords ) { pServerPasswords = pConfig ->CreateSubNode ( CONFIG_NODE_SERVER_SAVED ); } //Check if the server password already exists for ( unsigned int i = 0 ; i < pServerPasswords->GetSubNodeCount() ; i++ ) { CXMLAttributes* pAttributes = &(pServerPasswords->GetSubNode(i)->GetAttributes()); if ( pAttributes->Find( "host" ) ) { if ( CXMLAttribute* pHost = pAttributes->Find ( "host" ) ) { std::string strXMLHost = pHost->GetValue(); if ( strXMLHost == strHost ) { CXMLAttribute* pPassword = pAttributes->Create( "password" ); std::string strPassword = pPassword->GetValue(); return strPassword; } } } } return ""; }
// ----------------------------------------------------------------------------- // CXMLAttributes::NewL // Two-phased constructor. // ----------------------------------------------------------------------------- // CXMLAttributes* CXMLAttributes::NewL() { CXMLAttributes* self = new( ELeave ) CXMLAttributes; CleanupStack::PushL( self ); self->ConstructL(); CleanupStack::Pop(); return self; }
// Creates a new setting and adds it to the destination node CXMLNode *CSettings::CreateSetting(CXMLNode *pDst, const char *szName, const char *szContent) { // Create the node CXMLNode * pNode = pDst->CreateSubNode("setting"); CXMLAttributes *pAttributes = &(pNode->GetAttributes()); // Add the attributes with the corresponding values pAttributes->Create("name")->SetValue(szName); pAttributes->Create("value")->SetValue(szContent); return pNode; }
// Set ( resource requesting the query, setting name, content ) bool CSettings::Set(const char *szLocalResource, const char *szSetting, const char *szContent) { CXMLNode * pNode; CResource * pResource; CXMLAttributes *pAttributes; char szBuffer[MAX_SETTINGS_LENGTH] = {0}; char szQueryResource[MAX_RESOURCE_LENGTH] = {0}; SettingStatus eStatus; bool bDeleteNode, bExists; SString strOldValue; // Check for empty strings if (strlen(szSetting) < 1) return false; // Get the actual resource name from the specified setting, and get the resource class if (!GetResourceName(szSetting, szQueryResource, MAX_RESOURCE_LENGTH - 1)) { // No name was specified, so use the local resource pResource = m_pResourceManager->GetResource(szLocalResource); } else pResource = m_pResourceManager->GetResource(szQueryResource); // If we have a valid resource if (pResource) { CXMLNode *pSource = pResource->GetSettingsNode(); // Check whether the setting exists in the settings registry pNode = Get(m_pNodeGlobalSettings, NULL, "", szLocalResource, szSetting, bDeleteNode, eStatus); bExists = true; // Default value // Try to get the value for the appropriate setting from the resource's meta XML file if (eStatus == NotFound && pSource) { pNode = Get(pSource, NULL, pResource->GetName().c_str(), szLocalResource, szSetting, bDeleteNode, eStatus); bExists = false; // There's no node in the settings registry, so we create one } // See if we have access if (eStatus != NoAccess) { // See if we have a prefix bool bPrefix = HasPrefix(szSetting[0]); // If no resource name was specified, use the local resource name if (!HasResourceName(szSetting)) { // If we have a prefix, move it from szSetting and put it at the beginning if (bPrefix) snprintf(szBuffer, MAX_SETTINGS_LENGTH - 1, "%c%s.%s", szSetting[0], szLocalResource, szSetting + 1); else snprintf(szBuffer, MAX_SETTINGS_LENGTH - 1, "%s.%s", szLocalResource, szSetting); } else { // If we have a prefix, move it from szSetting and put it at the beginning if (bPrefix) snprintf(szBuffer, MAX_SETTINGS_LENGTH - 1, "%c%s", szSetting[0], szSetting + 1); else strncpy(szBuffer, szSetting, MAX_SETTINGS_LENGTH - 1); } if (!bExists || !pNode) { // No existing settings registry entry, so create a new setting CreateSetting(m_pNodeGlobalSettings, szBuffer, szContent); } else { // Existing settings registry entry // Get the attributes pAttributes = &(pNode->GetAttributes()); // Abort if this value isnt public (but protected or private), and if the local resource // (doing the query) doesn't equal the setting's resource name if (GetAccessType(pAttributes->Find("name")->GetValue()[0]) != CSettings::Public && stricmp(pResource->GetName().c_str(), szLocalResource) != 0) return false; // Get the node's current value strOldValue = pAttributes->Find("value")->GetValue(); // Set the node's value pAttributes->Find("value")->SetValue(szContent); // If a prefix was given, set the node's name (to override any access operators) if (bPrefix) pAttributes->Find("name")->SetValue(szBuffer); } // Trigger onSettingChange CLuaArguments Arguments; Arguments.PushString(szSetting); if (strOldValue.length() > 0) Arguments.PushString(strOldValue.c_str()); else Arguments.PushNil(); Arguments.PushString(szContent); g_pGame->GetMapManager()->GetRootElement()->CallEvent("onSettingChange", Arguments); // Save the XML file if (m_pFile->Write()) return true; CLogger::ErrorPrintf("Error saving '%s'\n", FILENAME_SETTINGS); } } return false; }
bool CAccountManager::LoadXML ( CXMLNode* pParent ) { CLogger::LogPrint ( "Converting Accounts.xml into internal.db\n" ); //##Keep for backwards compatability with accounts.xml## #define ACCOUNT_VALUE_LENGTH 128 std::string strBuffer, strName, strPassword, strIP, strDataKey, strDataValue; if ( pParent ) { CXMLNode* pAccountNode = NULL; unsigned int uiAccountNodesCount = pParent->GetSubNodeCount (); for ( unsigned int i = 0 ; i < uiAccountNodesCount ; i++ ) { pAccountNode = pParent->GetSubNode ( i ); if ( pAccountNode == NULL ) continue; strBuffer = pAccountNode->GetTagName (); if ( strBuffer.compare ( "account" ) == 0 ) { CXMLAttribute* pAttribute = pAccountNode->GetAttributes ().Find ( "name" ); if ( pAttribute ) { strName = pAttribute->GetValue (); pAttribute = pAccountNode->GetAttributes ().Find ( "password" ); if ( pAttribute ) { strPassword = pAttribute->GetValue (); if ( !strName.empty () && !strPassword.empty () ) { pAttribute = pAccountNode->GetAttributes ().Find ( "ip" ); if ( pAttribute ) { strIP = pAttribute->GetValue (); CAccount* pAccount = NULL; pAttribute = pAccountNode->GetAttributes ().Find ( "serial" ); if ( pAttribute ) { //Insert the entry into the accounts database m_pDatabaseManager->Execf ( m_hDbConnection, "INSERT INTO accounts (name, ip, serial, password) VALUES(?,?,?,?)", SQLITE_TEXT, strName.c_str(), SQLITE_TEXT, strIP.c_str(), SQLITE_TEXT, pAttribute->GetValue ().c_str(), SQLITE_TEXT, strPassword.c_str() ); pAccount = new CAccount ( this, true, strName, strPassword, strIP, m_iAccounts++, pAttribute->GetValue () ); } else { //Insert the entry into the accounts database m_pDatabaseManager->Execf ( m_hDbConnection, "INSERT INTO accounts (name, ip, password) VALUES(?,?,?)", SQLITE_TEXT, strName.c_str(), SQLITE_TEXT, strIP.c_str(), SQLITE_TEXT, strPassword.c_str() ); pAccount = new CAccount ( this, true, strName, strPassword, strIP, m_iAccounts++ ); } // Grab the data on this account CXMLNode* pDataNode = NULL; int iType = LUA_TNIL; unsigned int uiDataNodesCount = pAccountNode->GetSubNodeCount (); for ( unsigned int j = 0 ; j < uiDataNodesCount ; j++ ) { pDataNode = pAccountNode->GetSubNode ( j ); if ( pDataNode == NULL ) continue; strBuffer = pDataNode->GetTagName (); if ( strBuffer == "nil_data" ) iType = LUA_TNIL; else if ( strBuffer == "boolean_data" ) iType = LUA_TBOOLEAN; else if ( strBuffer == "string_data" ) iType = LUA_TSTRING; else if ( strBuffer == "number_data" ) iType = LUA_TNUMBER; CXMLAttributes* pAttributes = &(pDataNode->GetAttributes ()); CXMLAttribute* pAttribute = NULL; unsigned int uiDataValuesCount = pAttributes->Count (); for ( unsigned int a = 0 ; a < uiDataValuesCount ; a++ ) { pAttribute = pAttributes->Get ( a ); strDataKey = pAttribute->GetName (); strDataValue = pAttribute->GetValue (); char szKey[128]; STRNCPY( szKey, strDataKey.c_str(), 128 ); SetAccountData( pAccount, szKey, strDataValue, iType ); } } } else { CAccount* pAccount = NULL; pAttribute = pAccountNode->GetAttributes ().Find ( "serial" ); if ( pAttribute ) { //Insert the entry into the accounts database m_pDatabaseManager->Execf ( m_hDbConnection, "INSERT INTO accounts (name, password, serial) VALUES(?,?,?)", SQLITE_TEXT, strName.c_str(), SQLITE_TEXT, strPassword.c_str(), SQLITE_TEXT, pAttribute->GetValue().c_str() ); pAccount = new CAccount ( this, true, strName, strPassword, "", m_iAccounts++, pAttribute->GetValue () ); } else { //Insert the entry into the accounts database m_pDatabaseManager->Execf ( m_hDbConnection, "INSERT INTO accounts (name, password) VALUES(?,?)", SQLITE_TEXT, strName.c_str(), SQLITE_TEXT, strPassword.c_str() ); pAccount = new CAccount ( this, true, strName, strPassword, "", m_iAccounts++, "" ); } } } else { if ( strName == CONSOLE_ACCOUNT_NAME ) { //Add Console to the SQL Database (You don't need to create an account since the server takes care of that m_pDatabaseManager->Execf ( m_hDbConnection, "INSERT INTO accounts (name, password) VALUES(?,?)", SQLITE_TEXT, "Console", SQLITE_TEXT, "" ); ++m_iAccounts; } } } } } else { //Load the settings from XML LoadSetting ( pAccountNode ); } } //Save the settings to SQL SaveSettings(); CLogger::LogPrint ( "Conversion Complete.\n" ); m_bChangedSinceSaved = false; return true; } return false; }
/////////////////////////////////////////////////////////////// // // CJoystickManager::LoadFromXML // // Load axes mapping for the current joypad. // /////////////////////////////////////////////////////////////// bool CJoystickManager::LoadFromXML ( void ) { m_SettingsRevision++; // Try load CXMLNode* pMainNode = GetConfigNode ( false ); if ( !pMainNode ) return false; int iErrors = 0; { // Find the 'info' node CXMLNode* pNode = pMainNode->FindSubNode ( "info" ); // If it was found if ( pNode ) { CXMLAttributes* pAttributes = &pNode->GetAttributes (); CXMLAttribute* pA = NULL; if ( pA = pAttributes->Find( "deadzone" ) ) m_DevInfo.iDeadZone = Clamp ( 0, atoi ( pA->GetValue ().c_str () ), 49 ); else iErrors++; if ( pA = pAttributes->Find( "saturation" ) ) m_DevInfo.iSaturation = Clamp ( 51, atoi ( pA->GetValue ().c_str () ), 100 ); else iErrors++; } else iErrors++; } // Iterate the binds reading them from the XML tree for ( int i = 0 ; i < NUMELMS(m_currentMapping) ; i++ ) { SMappingLine& line = m_currentMapping[i]; // Find the 'axis' node CXMLNode* pNode = pMainNode->FindSubNode( "axis", i ); // If it was found if ( pNode ) { CXMLAttributes* pAttributes = &pNode->GetAttributes (); CXMLAttribute* pA = NULL; if ( pA = pAttributes->Find( "source_index" ) ) line.SourceAxisIndex = (eJoy)Clamp < int > ( 0, atoi ( pA->GetValue ().c_str () ), eJoyMax ); else iErrors++; if ( pA = pAttributes->Find( "source_dir" ) ) line.SourceAxisDir = (eDir)Clamp < int > ( 0, atoi ( pA->GetValue ().c_str () ), eDirMax ); else iErrors++; if ( pA = pAttributes->Find( "output_index" ) ) line.OutputAxisIndex = (eStick)Clamp < int > ( 0, atoi ( pA->GetValue ().c_str () ), eStickMax ); else iErrors++; if ( pA = pAttributes->Find( "output_dir" ) ) line.OutputAxisDir = (eDir)Clamp < int > ( 0, atoi ( pA->GetValue ().c_str () ), eDirMax ); else iErrors++; if ( pA = pAttributes->Find( "enabled" ) ) line.bEnabled = atoi ( pA->GetValue ().c_str () ) ? true : false; else iErrors++; if ( pA = pAttributes->Find( "max_value" ) ) line.MaxValue = atoi ( pA->GetValue ().c_str () ); else iErrors++; } else iErrors++; } if ( iErrors ) if ( CCore::GetSingleton ().GetConsole () ) CCore::GetSingleton ().GetConsole ()->Printf( "Warning: %d errors reading joypad configuration.", iErrors ); return true; }
/////////////////////////////////////////////////////////////// // // CJoystickManager::GetConfigNode // // Get the main node for load/saving data for the current joypad. // /////////////////////////////////////////////////////////////// CXMLNode* CJoystickManager::GetConfigNode ( bool bCreateIfRequired ) { // Get the root node CXMLNode *pRoot = CCore::GetSingleton ().GetConfig (); if ( !pRoot ) return NULL; // Get the top joypad config node CXMLNode* pSectionNode = pRoot->FindSubNode ( CONFIG_NODE_JOYPAD ); if ( !pSectionNode ) { if ( !bCreateIfRequired ) return NULL; // Non-existant, create a new node pSectionNode = pRoot->CreateSubNode ( CONFIG_NODE_JOYPAD ); } // Get the node for this joystick's GUID CXMLNode* pItemNode = NULL; // Find existing node for( int i=0; true; i++ ) { CXMLNode* pNode = pSectionNode->FindSubNode ( "product", i ); if ( !pNode ) break; CXMLAttributes* pAttributes = &pNode->GetAttributes (); if ( CXMLAttribute* pA = pAttributes->Find ( "guid" ) ) { string value = pA->GetValue (); if ( value == m_DevInfo.strGuid ) { pItemNode = pNode; break; } } } if ( !pItemNode ) { if ( !bCreateIfRequired ) return NULL; // Non-existant, create a new node pItemNode = pSectionNode->CreateSubNode ( "product" ); if ( pItemNode ) { CXMLAttributes* pAttributes = &pItemNode->GetAttributes (); CXMLAttribute* pA = NULL; pA = pAttributes->Create ( "guid" ); pA->SetValue ( m_DevInfo.strGuid.c_str () ); } } return pItemNode; }
/////////////////////////////////////////////////////////////// // // CJoystickManager::SaveToXML // // Save axes mapping for the current joypad. // /////////////////////////////////////////////////////////////// bool CJoystickManager::SaveToXML ( void ) { if ( !IsJoypadValid () ) return false; m_SettingsRevision++; CXMLNode* pMainNode = GetConfigNode ( true ); // Add the current settings if ( pMainNode ) { // Clear our current bind nodes pMainNode->DeleteAllSubNodes (); { // Create a new 'info' node CXMLNode* pNode = pMainNode->CreateSubNode ( "info" ); // If it was created if ( pNode ) { CXMLAttributes* pAttributes = &pNode->GetAttributes (); CXMLAttribute* pA = NULL; pA = pAttributes->Create ( "deadzone" ); pA->SetValue ( m_DevInfo.iDeadZone ); pA = pAttributes->Create ( "saturation" ); pA->SetValue ( m_DevInfo.iSaturation ); pA = pAttributes->Create ( "product_name" ); pA->SetValue ( m_DevInfo.strProductName.c_str () ); } } // Iterate the binds adding them to the XML tree for ( int i = 0 ; i < NUMELMS(m_currentMapping) ; i++ ) { const SMappingLine& line = m_currentMapping[i]; // Create the new 'axis' node CXMLNode* pNode = pMainNode->CreateSubNode ( "axis" ); // If it was created if ( pNode ) { CXMLAttributes* pAttributes = &pNode->GetAttributes (); CXMLAttribute* pA = NULL; pA = pAttributes->Create ( "source_index" ); pA->SetValue ( line.SourceAxisIndex ); pA = pAttributes->Create ( "source_dir" ); pA->SetValue ( line.SourceAxisDir ); pA = pAttributes->Create ( "output_index" ); pA->SetValue ( line.OutputAxisIndex ); pA = pAttributes->Create ( "output_dir" ); pA->SetValue ( line.OutputAxisDir ); pA = pAttributes->Create ( "enabled" ); pA->SetValue ( line.bEnabled ); pA = pAttributes->Create ( "max_value" ); pA->SetValue ( line.MaxValue ); } } return true; } return false; }