-std::string LuaSAO::getServerInitializationData()
-{
- std::string data;
-
- // Script name
- data.append(serializeString(m_script_name));
-
- /*
- Get data from server-side script for inclusion
- */
- std::string other_data;
-
- do{
-
- const char *funcname = "on_get_server_init_data";
- lua_getglobal(L, funcname);
- if(!lua_isfunction(L,-1))
- {
- lua_pop(L,1);
- dstream<<"WARNING: LuaSAO: Function not found: "
- <<funcname<<std::endl;
- break;
- }
-
- // Parameters:
- // 1: self
- lua_pushlightuserdata(L, this);
-
- // Call (1 parameters, 1 result)
- if(lua_pcall(L, 1, 1, 0))
- {
- dstream<<"WARNING: LuaSAO: Error running function "
- <<funcname<<": "
- <<lua_tostring(L,-1)<<std::endl;
- break;
- }
-
- // Retrieve result
- if(!lua_isstring(L,-1))
- {
- dstream<<"WARNING: LuaSAO: Function "<<funcname
- <<" didn't return a string"<<std::endl;
- break;
- }
-
- size_t cs_len = 0;
- const char *cs = lua_tolstring(L, -1, &cs_len);
- lua_pop(L,1);
-
- other_data = std::string(cs, cs_len);
-
- }while(0);
-
- data += serializeLongString(other_data);
-
- return data;
-}
-
-void LuaSAO::initializeFromNothing(const std::string &script_name)
-{
- loadScripts(script_name);
-
- /*
- Call on_initialize(self, data) in the script
- */
-
- const char *funcname = "on_initialize";
- lua_getglobal(L, funcname);
- if(!lua_isfunction(L,-1))
- {
- lua_pop(L,1);
- dstream<<"WARNING: LuaSAO: Function not found: "
- <<funcname<<std::endl;
- return;
- }
-
- // Parameters:
- // 1: self
- lua_pushlightuserdata(L, this);
- // 2: data (other)
- lua_pushstring(L, "");
-
- // Call (2 parameters, 0 result)
- if(lua_pcall(L, 2, 0, 0))
- {
- dstream<<"WARNING: LuaSAO: Error running function "
- <<funcname<<": "
- <<lua_tostring(L,-1)<<std::endl;
- return;
- }
-}
-
-void LuaSAO::initializeFromSave(const std::string &data)
-{
- std::istringstream is(data, std::ios::binary);
- std::string script_name = deSerializeString(is);
- std::string other = deSerializeLongString(is);
-
- loadScripts(script_name);
-
- /*
- Call on_initialize(self, data) in the script
- */
-
- const char *funcname = "on_initialize";
- lua_getglobal(L, funcname);
- if(!lua_isfunction(L,-1))
- {
- lua_pop(L,1);
- dstream<<"WARNING: LuaSAO: Function not found: "
- <<funcname<<std::endl;
- return;
- }
-
- // Parameters:
- // 1: self
- lua_pushlightuserdata(L, this);
- // 2: data (other)
- lua_pushlstring(L, other.c_str(), other.size());
-
- // Call (2 parameters, 0 result)
- if(lua_pcall(L, 2, 0, 0))
- {
- dstream<<"WARNING: LuaSAO: Error running function "
- <<funcname<<": "
- <<lua_tostring(L,-1)<<std::endl;
- return;
- }
-}
-
-void LuaSAO::loadScripts(const std::string &script_name)
-{
- m_script_name = script_name;
-
- std::string relative_path;
- relative_path += "scripts/objects/";
- relative_path += script_name;
- std::string server_file = relative_path + "/server.lua";
- std::string server_path = porting::getDataPath(server_file.c_str());
-
- // Load the file
- int ret;
- ret = luaL_loadfile(L, server_path.c_str());
- if(ret)
- {
- const char *message = lua_tostring(L, -1);
- lua_pop(L, 1);
- dstream<<"LuaSAO::loadScript(): lua_loadfile failed: "
- <<message<<std::endl;
- assert(0);
- return;
- }
- ret = lua_pcall(L, 0, 0, 0);
- if(ret)
- {
- const char *message = lua_tostring(L, -1);
- lua_pop(L, 1);
- dstream<<"LuaSAO::loadScript(): lua_pcall failed: "
- <<message<<std::endl;
- assert(0);
- return;
- }
-}
-
-void LuaSAO::step(float dtime, Queue<ActiveObjectMessage> &messages)
-{
- const char *funcname = "on_step";
- lua_getglobal(L, funcname);
- if(!lua_isfunction(L,-1))
- {
- lua_pop(L,1);
- dstream<<"WARNING: LuaSAO::step(): Function not found: "
- <<funcname<<std::endl;
- return;
- }
-
- // Parameters:
- // 1: self
- lua_pushlightuserdata(L, this);
- // 2: dtime
- lua_pushnumber(L, dtime);
-
- // Call (2 parameters, 0 result)
- if(lua_pcall(L, 2, 0, 0))
- {
- dstream<<"WARNING: LuaSAO::step(): Error running function "
- <<funcname<<": "
- <<lua_tostring(L,-1)<<std::endl;
- return;
- }
-
- // Move messages
- while(m_message_queue.size() != 0)
- {
- messages.push_back(m_message_queue.pop_front());
- }
-}
-
-
-