Minor refactor

This commit is contained in:
y5nw 2024-05-17 20:24:14 +02:00
parent d7343b5896
commit 21a7ee52ce
7 changed files with 41 additions and 95 deletions

View File

@ -423,3 +423,21 @@ void* AsyncWorkerThread::run()
return 0;
}
u32 ScriptApiAsync::queueAsync(std::string &&serialized_func,
PackedValue *param, const std::string &mod_origin)
{
return asyncEngine.queueAsyncJob(std::move(serialized_func),
param, mod_origin);
}
u32 ScriptApiAsync::replaceAsync(const u32 &id, std::string &&serialized_func,
PackedValue *param, const std::string &mod_origin)
{
return asyncEngine.replaceAsyncJob(id, std::move(serialized_func),
param, mod_origin);
}
void ScriptApiAsync::stepAsync()
{
asyncEngine.step(getStack());
}

View File

@ -232,3 +232,21 @@ private:
// Counter semaphore for job dispatching
Semaphore jobQueueCounter;
};
class ScriptApiAsync:
virtual public ScriptApiBase
{
public:
ScriptApiAsync(Server *server): asyncEngine(server) {}
virtual void initAsync() = 0;
void stepAsync();
u32 queueAsync(std::string &&serialized_func,
PackedValue *param, const std::string &mod_origin);
u32 replaceAsync(const u32 &id, std::string &&serialized_func,
PackedValue *param, const std::string &mod_origin);
protected:
AsyncEngine asyncEngine;
};

View File

@ -1,5 +1,6 @@
set(common_SCRIPT_LUA_API_SRCS
${CMAKE_CURRENT_SOURCE_DIR}/l_areastore.cpp
${CMAKE_CURRENT_SOURCE_DIR}/l_async.cpp
${CMAKE_CURRENT_SOURCE_DIR}/l_auth.cpp
${CMAKE_CURRENT_SOURCE_DIR}/l_base.cpp
${CMAKE_CURRENT_SOURCE_DIR}/l_craft.cpp

View File

@ -644,61 +644,6 @@ int ModApiServer::l_notify_authentication_modified(lua_State *L)
return 0;
}
static std::string get_serialized_function(lua_State *L, int index)
{
luaL_checktype(L, index, LUA_TFUNCTION);
call_string_dump(L, index);
size_t func_length;
const char *serialized_func_raw = lua_tolstring(L, -1, &func_length);
return std::string(serialized_func_raw, func_length);
}
// do_async_callback(func, params, mod_origin)
int ModApiServer::l_do_async_callback(lua_State *L)
{
NO_MAP_LOCK_REQUIRED;
ServerScripting *script = getScriptApi<ServerScripting>(L);
luaL_checktype(L, 2, LUA_TTABLE);
luaL_checktype(L, 3, LUA_TSTRING);
auto serialized_func = get_serialized_function(L, 1);
PackedValue *param = script_pack(L, 2);
std::string mod_origin = readParam<std::string>(L, 3);
u32 jobId = script->queueAsync(
std::move(serialized_func),
param, mod_origin);
lua_settop(L, 0);
lua_pushinteger(L, jobId);
return 1;
}
// replace_async_callback(id, func, params, mod_origin)
int ModApiServer::l_replace_async_callback(lua_State *L)
{
NO_MAP_LOCK_REQUIRED;
ServerScripting *script = getScriptApi<ServerScripting>(L);
luaL_checktype(L, 1, LUA_TNUMBER);
luaL_checktype(L, 3, LUA_TTABLE);
luaL_checktype(L, 4, LUA_TSTRING);
u32 id = lua_tointeger(L, 1);
auto serialized_func = get_serialized_function(L, 2);
PackedValue *param = script_pack(L, 3);
std::string mod_origin = readParam<std::string>(L, 4);
u32 jobId = script->replaceAsync(id,
std::move(serialized_func),
param, mod_origin);
lua_settop(L, 0);
lua_pushinteger(L, jobId);
return 1;
}
// register_async_dofile(path)
int ModApiServer::l_register_async_dofile(lua_State *L)
{
@ -794,8 +739,6 @@ void ModApiServer::Initialize(lua_State *L, int top)
API_FCT(unban_player_or_ip);
API_FCT(notify_authentication_modified);
API_FCT(do_async_callback);
API_FCT(replace_async_callback);
API_FCT(register_async_dofile);
API_FCT(serialize_roundtrip);

View File

@ -115,12 +115,6 @@ private:
// notify_authentication_modified(name)
static int l_notify_authentication_modified(lua_State *L);
// do_async_callback(func, params, mod_origin)
static int l_do_async_callback(lua_State *L);
// replace_async_callback(id, func, params, mod_origin)
static int l_replace_async_callback(lua_State *L);
// register_async_dofile(path)
static int l_register_async_dofile(lua_State *L);

View File

@ -24,6 +24,7 @@ with this program; if not, write to the Free Software Foundation, Inc.,
#include "filesys.h"
#include "cpp_api/s_internal.h"
#include "lua_api/l_areastore.h"
#include "lua_api/l_async.h"
#include "lua_api/l_auth.h"
#include "lua_api/l_base.h"
#include "lua_api/l_craft.h"
@ -53,7 +54,7 @@ extern "C" {
ServerScripting::ServerScripting(Server* server):
ScriptApiBase(ScriptingType::Server),
asyncEngine(server)
ScriptApiAsync(server)
{
setGameDef(server);
@ -128,25 +129,6 @@ void ServerScripting::initAsync()
asyncEngine.initialize(0);
}
void ServerScripting::stepAsync()
{
asyncEngine.step(getStack());
}
u32 ServerScripting::queueAsync(std::string &&serialized_func,
PackedValue *param, const std::string &mod_origin)
{
return asyncEngine.queueAsyncJob(std::move(serialized_func),
param, mod_origin);
}
u32 ServerScripting::replaceAsync(const u32 &id, std::string &&serialized_func,
PackedValue *param, const std::string &mod_origin)
{
return asyncEngine.replaceAsyncJob(id, std::move(serialized_func),
param, mod_origin);
}
void ServerScripting::InitializeModApi(lua_State *L, int top)
{
// Register reference classes (userdata)
@ -170,6 +152,7 @@ void ServerScripting::InitializeModApi(lua_State *L, int top)
ModChannelRef::Register(L);
// Initialize mod api modules
ModApiAsync::Initialize(L, top);
ModApiAuth::Initialize(L, top);
ModApiCraft::Initialize(L, top);
ModApiEnv::Initialize(L, top);

View File

@ -37,6 +37,7 @@ struct PackedValue;
class ServerScripting:
virtual public ScriptApiBase,
public ScriptApiAsync,
public ScriptApiDetached,
public ScriptApiEntity,
public ScriptApiEnv,
@ -58,20 +59,8 @@ public:
// Initialize async engine, call this AFTER loading all mods
void initAsync();
// Global step handler to collect async results
void stepAsync();
// Pass job to async threads
u32 queueAsync(std::string &&serialized_func,
PackedValue *param, const std::string &mod_origin);
u32 replaceAsync(const u32 &id, std::string &&serialized_func,
PackedValue *param, const std::string &mod_origin);
private:
void InitializeModApi(lua_State *L, int top);
static void InitializeAsync(lua_State *L, int top);
AsyncEngine asyncEngine;
};