本文整理汇总了C++中session_ptr::printf方法的典型用法代码示例。如果您正苦于以下问题:C++ session_ptr::printf方法的具体用法?C++ session_ptr::printf怎么用?C++ session_ptr::printf使用的例子?那么, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类session_ptr
的用法示例。
在下文中一共展示了session_ptr::printf方法的1个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C++代码示例。
示例1:
void run_script( session_ptr session, std::string fileName, mission_id inMissionID )
{
user_session_ptr loginInfo = session->find_sessiondata<user_session>(USER_SESSION_DATA_ID);
if( !loginInfo || (loginInfo->my_user_flags() & USER_FLAG_SERVER_OWNER) == 0 )
{
session->printf( "/!not_permitted\r\n" );
return;
}
if( fileName.size() == 0 || fileName.find("..") != std::string::npos )
{
session->printf( "/!no_such_file\r\n" );
return;
}
std::string filePath( sSettingsFolderPath );
filePath.append( "/scripts/" );
filePath.append( fileName );
filePath.append( ".lua" );
// ===== LUA CODE START: =====
lua_State *L = luaL_newstate(); // Create a context.
luaL_openlibs(L); // Load Lua standard library.
// Load the file:
int s = luaL_loadfile( L, filePath.c_str() );
if( s == 0 )
{
// Run it, with 0 params, accepting an arbitrary number of return values.
// Last 0 is error handler Lua function's stack index, or 0 to ignore.
s = lua_pcall(L, 0, LUA_MULTRET, 0); // Create all the functions and objects.
}
// Create a C-backed Lua object:
lua_newtable( L ); // Create a new object & push it on the stack.
lua_newtable( L ); // Create a new 'mission' object & push it on the stack.
lua_pushlightuserdata( L, &session ); // session object in case we need it.
lua_pushinteger( L, inMissionID ); // mission ID.
lua_pushcclosure( L, mission_add_objective, 2 );// Create the method.
lua_setfield( L, -2, "add_objective" ); // Attach it to the object with a name.
lua_pushlightuserdata( L, &session ); // session object in case we need it.
lua_pushcclosure( L, mission_load, 1 );// Create the method.
lua_setfield( L, -2, "load" ); // Attach it to the object with a name.
lua_setglobal( L, "mission" ); // Register the object as name "mission".
if( s == 0 )
{
lua_getglobal(L,"main"); // Push the function we want to call.
s = lua_pcall(L, 0, LUA_MULTRET, 0);
}
// Was an error? Get error message off the stack and send it back:
if( s != 0 )
{
session->printf( "/!script_error %s\r\n", lua_tostring(L, -1) );
lua_pop(L, 1); // remove error message
}
else
session->printf( "/ran_script\r\n" ); // Send back indication of success.
lua_close(L); // Dispose of the script context.
// ===== LUA CODE END. =====
}