int CLuaFunctionDefs::GetServerConfigSetting ( lua_State* luaVM ) { // string getServerConfigSetting ( string name ) SString strName; CScriptArgReader argStream ( luaVM ); argStream.ReadString ( strName ); if ( !argStream.HasErrors () ) { SString strValue; // Try as single setting if ( g_pGame->GetConfig ()->GetSetting ( strName, strValue ) ) { lua_pushstring ( luaVM, strValue ); return 1; } // Try as multiple setting CLuaArguments result; if ( g_pGame->GetConfig ()->GetSettingTable ( strName, &result ) ) { result.PushAsTable ( luaVM ); return 1; } } else m_pScriptDebugging->LogCustom ( luaVM, argStream.GetFullErrorMessage () ); lua_pushboolean ( luaVM, false ); 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; }
int CLuaFunctionDefs::GetCTime ( lua_State* luaVM ) { // table getRealTime( [int seconds = current], bool localTime = true ) time_t timer; time ( &timer ); bool bLocalTime = true; CScriptArgReader argStream ( luaVM ); if ( argStream.NextCouldBeNumber () ) { argStream.ReadNumber ( timer ); if ( timer < 0 ) { argStream.SetCustomError ( "seconds cannot be negative" ); } } if ( argStream.NextIsBool () ) argStream.ReadBool ( bLocalTime ); tm * time; if ( bLocalTime ) time = localtime ( &timer ); else time = gmtime ( &timer ); if ( time == NULL ) argStream.SetCustomError ( "seconds is out of range" ); if ( argStream.HasErrors () ) { m_pScriptDebugging->LogCustom ( luaVM, argStream.GetFullErrorMessage () ); lua_pushboolean ( luaVM, false ); return 1; } CLuaArguments ret; ret.PushString ( "second" ); ret.PushNumber ( time->tm_sec ); ret.PushString ( "minute" ); ret.PushNumber ( time->tm_min ); ret.PushString ( "hour" ); ret.PushNumber ( time->tm_hour ); ret.PushString ( "monthday" ); ret.PushNumber ( time->tm_mday ); ret.PushString ( "month" ); ret.PushNumber ( time->tm_mon ); ret.PushString ( "year" ); ret.PushNumber ( time->tm_year ); ret.PushString ( "weekday" ); ret.PushNumber ( time->tm_wday ); ret.PushString ( "yearday" ); ret.PushNumber ( time->tm_yday ); ret.PushString ( "isdst" ); ret.PushNumber ( time->tm_isdst ); ret.PushString ( "timestamp" ); ret.PushNumber ( (double) timer ); ret.PushAsTable ( luaVM ); return 1; }