Add login.initialize function

This commit is contained in:
2024-02-27 17:32:08 -05:00
parent 2b426eefeb
commit 32dae0df5a
5 changed files with 40 additions and 79 deletions

View File

@@ -246,18 +246,26 @@ World::Redirects World::fetch_redirects() {
return result;
}
int64_t World::create_login_actor() {
int64_t World::create_login_actor(bool initialize) {
assert(stack_is_clear());
int64_t id = id_global_pool_.get_one();
LuaVar database, classtab, mt;
LuaExtStack LS(state(), database, classtab, mt);
Tangible *tan = tangible_make(LS, database, id);
LS.makeclass(classtab, "login");
LS.getmetatable(mt, database);
LS.rawset(mt, "__index", classtab);
tan->configure_id_pool_for_actor();
tan->print_buffer_.clear();
return tan->id();
{
LuaVar database, classtab, mt, func;
LuaExtStack LS(state(), database, classtab, mt, func);
Tangible *tan = tangible_make(LS, database, id);
LS.makeclass(classtab, "login");
LS.getmetatable(mt, database);
LS.rawset(mt, "__index", classtab);
tan->configure_id_pool_for_actor();
tan->print_buffer_.clear();
if (initialize) {
LS.rawget(func, classtab, "initialize");
spawn(LS, id, id, func, true, 0, false);
}
}
run_scheduled_threads();
return id;
}
eng::string World::probe_lua(int64_t actor_id, const eng::string &lua) {
@@ -794,17 +802,6 @@ void push_simple_dynamic(lua_State *L, StreamBuffer *sb) {
void World::invoke_engio(int64_t actor_id, int64_t place_id, std::string_view datapack) {
assert(stack_is_clear());
// Get the actor and place. Make sure both exist.
Tangible *tactor = tangible_get(actor_id);
Tangible *tplace = tangible_get(place_id);
if ((tactor == nullptr) || (tplace == nullptr)) {
return;
}
// Get an ID for the thread. We always use the player
// pool in this case.
int64_t tid = tactor->id_player_pool_.get_one();
// Use a streambuffer to parse the datapack.
StreamBuffer datasb(datapack);
@@ -820,37 +817,19 @@ void World::invoke_engio(int64_t actor_id, int64_t place_id, std::string_view da
}
{
// Set up for Lua manipulation.
lua_State *L = state();
LuaVar actor, place, func, mt, tangibles, playercb, thread, threads, thinfo, message;
LuaExtStack LS(L, actor, place, func, mt, tangibles, playercb, thread, threads, thinfo, message);
LuaVar engio, func;
LuaExtStack LS(L, engio, func);
// Get the actor and place.
LS.rawget(tangibles, LuaRegistry, "tangibles");
LS.rawget(actor, tangibles, actor_id);
LS.rawget(place, tangibles, place_id);
if (!LS.istable(actor) || !LS.istable(place)) {
return;
}
// Get the closure (playerinvoke.funcname).
eng::string err = LS.getclass(playercb, "engio");
if ((!err.empty()) || (!LS.istable(playercb))) {
return;
}
LS.rawget(func, playercb, funcname);
if (!LS.isfunction(func)) {
// Get the closure (engio.funcname).
eng::string err = LS.getclass(engio, "engio");
if ((!err.empty()) || (!LS.istable(engio))) {
return;
}
LS.rawget(func, engio, funcname);
// Create a new thread, push func, actor, place.
int nargs = 3;
lua_State *CO = LS.newthread(thread);
lua_pushvalue(L, func.index());
lua_pushvalue(L, actor.index());
lua_pushvalue(L, place.index());
// Push any additional args from the datapack.
// Spawn a thread, pushing extra arguments from the datapack.
int nargs = 0;
try {
while (!datasb.empty()) {
push_simple_dynamic(L, &datasb);
@@ -859,33 +838,8 @@ void World::invoke_engio(int64_t actor_id, int64_t place_id, std::string_view da
} catch (const StreamException &exc) {
return;
}
// Transfer all arguments to the new thread.
lua_xmove(L, CO, nargs);
// Create the thread info table.
LS.newtable(thinfo);
LS.rawset(thinfo, "thread", thread);
LS.rawset(thinfo, "actorid", actor_id);
LS.rawset(thinfo, "isnew", true);
LS.rawset(thinfo, "useppool", true);
LS.rawset(thinfo, "print", false);
// Store the thread into place's thread table.
LS.getmetatable(mt, place);
if (!LS.istable(mt)) {
return;
}
LS.rawget(threads, mt, "threads");
if (!LS.istable(threads)) {
return;
}
LS.rawset(threads, tid, thinfo);
spawn(LS, actor_id, place_id, func, true, nargs, false);
}
// Push the thread's ID into the runnable thread queue,
// then run the thread queue.
schedule(0, tid, place_id);
run_scheduled_threads();
assert(stack_is_clear());
}