diff --git a/src/server/game/CMakeLists.txt b/src/server/game/CMakeLists.txt index 7e082555c2..cb899b022f 100644 --- a/src/server/game/CMakeLists.txt +++ b/src/server/game/CMakeLists.txt @@ -85,3 +85,14 @@ endif() if(USE_COREPCH) add_cxx_pch(game ${PRIVATE_PCH_HEADER}) endif() + +# Vcore C7: server-side Lua runtime for clientless Player controllers. +find_package(Lua 5.4 REQUIRED) +target_include_directories(game PRIVATE ${LUA_INCLUDE_DIR}) +target_link_libraries(game PRIVATE ${LUA_LIBRARIES}) + +install( + DIRECTORY "${CMAKE_SOURCE_DIR}/src/server/scripts/Custom/clientless/" + DESTINATION "bin/scripts/clientless" + FILES_MATCHING PATTERN "*.lua" +) diff --git a/src/server/game/World/ClientlessLuaEngine.cpp b/src/server/game/World/ClientlessLuaEngine.cpp new file mode 100644 index 0000000000..161d7179f8 --- /dev/null +++ b/src/server/game/World/ClientlessLuaEngine.cpp @@ -0,0 +1,198 @@ +#include "ClientlessLuaEngine.h" +#include "ClientlessPlayerMgr.h" +#include "Log.h" +#include "Player.h" +#include +#include +#include +#include +#include +#include +#include +#include + +namespace +{ +uint32 BoundGuid(lua_State* L) { return uint32(lua_tointeger(L, lua_upvalueindex(1))); } + +std::unordered_map ReadMap(lua_State* L, int index) +{ + std::unordered_map out; + if (!lua_istable(L, index)) return out; + index = lua_absindex(L, index); + lua_pushnil(L); + while (lua_next(L, index) != 0) + { + if (lua_type(L, -2) == LUA_TSTRING) + { + std::string key = lua_tostring(L, -2), value; + switch (lua_type(L, -1)) + { + case LUA_TSTRING: value = lua_tostring(L, -1); break; + case LUA_TNUMBER: + value = lua_isinteger(L, -1) ? std::to_string(lua_tointeger(L, -1)) : std::to_string(lua_tonumber(L, -1)); + break; + case LUA_TBOOLEAN: value = lua_toboolean(L, -1) ? "true" : "false"; break; + default: break; + } + if (!value.empty()) out.emplace(std::move(key), std::move(value)); + } + lua_pop(L, 1); + } + return out; +} + +int LuaIntend(lua_State* L) +{ + ClientlessIntent intent; + intent.Name = luaL_checkstring(L, 1); + if (lua_gettop(L) >= 2) intent.Parameters = ReadMap(L, 2); + lua_pushboolean(L, sClientlessPlayerMgr->SetIntent(BoundGuid(L), std::move(intent))); + return 1; +} + +int LuaClearIntent(lua_State* L) +{ + lua_pushboolean(L, sClientlessPlayerMgr->ClearIntent(BoundGuid(L))); + return 1; +} + +int LuaQueueAction(lua_State* L) +{ + ClientlessAction action; + action.Name = luaL_checkstring(L, 1); + if (lua_gettop(L) >= 2) action.Parameters = ReadMap(L, 2); + lua_pushboolean(L, sClientlessPlayerMgr->QueueAction(BoundGuid(L), std::move(action))); + return 1; +} + +int LuaCharacterGuid(lua_State* L) +{ + lua_pushinteger(L, BoundGuid(L)); + return 1; +} + +int LuaLog(lua_State* L) +{ + TC_LOG_INFO("server.worldserver", "ClientlessLua[%u]: %s", BoundGuid(L), luaL_checkstring(L, 1)); + return 0; +} + +void BoundFn(lua_State* L, int env, char const* name, lua_CFunction fn, uint32 guid) +{ + env = lua_absindex(L, env); + lua_pushinteger(L, guid); + lua_pushcclosure(L, fn, 1); + lua_setfield(L, env, name); +} + +class ClientlessAdventureLuaController final : public ClientlessPlayerController +{ +public: + void OnPlayerOnline(Player* player) override { sClientlessLuaEngine->ExecuteAdventureScript(player); } +}; +} + +ClientlessLuaEngine* ClientlessLuaEngine::instance() +{ + static ClientlessLuaEngine instance; + return &instance; +} + +ClientlessLuaEngine::ClientlessLuaEngine() +{ + _scriptRoot = std::getenv("VCORE_CLIENTLESS_SCRIPT_DIR") ? std::getenv("VCORE_CLIENTLESS_SCRIPT_DIR") : "scripts/clientless"; + _state = luaL_newstate(); + if (!_state) + { + TC_LOG_ERROR("server.worldserver", "ClientlessLua: failed to create Lua state."); + return; + } + luaL_openlibs(_state); + for (char const* g : { "io", "os", "package", "debug", "dofile", "loadfile", "require" }) + { + lua_pushnil(_state); + lua_setglobal(_state, g); + } + TC_LOG_INFO("server.worldserver", "ClientlessLua initialized, script root '%s'.", _scriptRoot.c_str()); +} + +ClientlessLuaEngine::~ClientlessLuaEngine() +{ + if (_state) lua_close(_state); +} + +void ClientlessLuaEngine::PushSandboxEnvironment(Player* player) +{ + lua_newtable(_state); + int env = lua_gettop(_state); + lua_newtable(_state); + lua_pushglobaltable(_state); + lua_setfield(_state, -2, "__index"); + lua_setmetatable(_state, env); + + uint32 guid = player->GetGUID().GetCounter(); + BoundFn(_state, env, "intend", LuaIntend, guid); + BoundFn(_state, env, "clear_intent", LuaClearIntent, guid); + BoundFn(_state, env, "queue_action", LuaQueueAction, guid); + BoundFn(_state, env, "character_guid", LuaCharacterGuid, guid); + BoundFn(_state, env, "log", LuaLog, guid); + lua_pushstring(_state, player->GetName().c_str()); + lua_setfield(_state, env, "character_name"); +} + +bool ClientlessLuaEngine::ExecuteScript(Player* player, std::string const& path, std::string const& kind) +{ + if (!_state || !player) return false; + int base = lua_gettop(_state); + if (luaL_loadfile(_state, path.c_str()) != LUA_OK) + { + TC_LOG_ERROR("server.worldserver", "ClientlessLua[%u]: load failed for %s '%s': %s", + player->GetGUID().GetCounter(), kind.c_str(), path.c_str(), lua_tostring(_state, -1)); + lua_settop(_state, base); + return false; + } + PushSandboxEnvironment(player); + if (!lua_setupvalue(_state, base + 1, 1)) + { + TC_LOG_ERROR("server.worldserver", "ClientlessLua[%u]: '%s' has no _ENV upvalue.", player->GetGUID().GetCounter(), path.c_str()); + lua_settop(_state, base); + return false; + } + if (lua_pcall(_state, 0, 0, 0) != LUA_OK) + { + TC_LOG_ERROR("server.worldserver", "ClientlessLua[%u]: runtime error in '%s': %s", + player->GetGUID().GetCounter(), path.c_str(), lua_tostring(_state, -1)); + lua_settop(_state, base); + return false; + } + TC_LOG_INFO("server.worldserver", "ClientlessLua[%u]: executed %s script '%s'.", + player->GetGUID().GetCounter(), kind.c_str(), path.c_str()); + lua_settop(_state, base); + return true; +} + +bool ClientlessLuaEngine::ExecuteAdventureScript(Player* player) +{ + if (!player) return false; + namespace fs = std::filesystem; + std::string name = player->GetName(); + std::transform(name.begin(), name.end(), name.begin(), [](unsigned char c) { return char(std::tolower(c)); }); + fs::path root(_scriptRoot); + fs::path custom = root / "adventure" / "overrides" / (name + ".lua"); + if (fs::exists(custom)) return ExecuteScript(player, custom.string(), "adventure override"); + return ExecuteScript(player, (root / "adventure" / "generic.lua").string(), "generic adventure"); +} + +bool ClientlessLuaEngine::ExecuteTestScript(Player* player, std::string const& scriptName) +{ + if (!player || scriptName.empty() || scriptName.find("..") != std::string::npos || scriptName.find('/') != std::string::npos || scriptName.find('\\') != std::string::npos) + return false; + std::filesystem::path path = std::filesystem::path(_scriptRoot) / "tests" / (scriptName + ".lua"); + return ExecuteScript(player, path.string(), "test"); +} + +std::unique_ptr CreateClientlessAdventureLuaController() +{ + return std::make_unique(); +} diff --git a/src/server/game/World/ClientlessLuaEngine.h b/src/server/game/World/ClientlessLuaEngine.h new file mode 100644 index 0000000000..af2ce4c18f --- /dev/null +++ b/src/server/game/World/ClientlessLuaEngine.h @@ -0,0 +1,34 @@ +/* Vcore C7: server-side Lua for clientless Players. */ +#ifndef VCORE_CLIENTLESS_LUA_ENGINE_H +#define VCORE_CLIENTLESS_LUA_ENGINE_H + +#include "Define.h" +#include +#include + +class ClientlessPlayerController; +class Player; +struct lua_State; + +class TC_GAME_API ClientlessLuaEngine +{ +public: + static ClientlessLuaEngine* instance(); + ~ClientlessLuaEngine(); + + bool ExecuteAdventureScript(Player* player); + bool ExecuteTestScript(Player* player, std::string const& scriptName); + std::string const& GetScriptRoot() const { return _scriptRoot; } + +private: + ClientlessLuaEngine(); + bool ExecuteScript(Player* player, std::string const& path, std::string const& kind); + void PushSandboxEnvironment(Player* player); + + lua_State* _state = nullptr; + std::string _scriptRoot; +}; + +TC_GAME_API std::unique_ptr CreateClientlessAdventureLuaController(); +#define sClientlessLuaEngine ClientlessLuaEngine::instance() +#endif diff --git a/src/server/game/World/ClientlessPlayerMgr.cpp b/src/server/game/World/ClientlessPlayerMgr.cpp index 770d7c575e..23d23ce1d8 100644 --- a/src/server/game/World/ClientlessPlayerMgr.cpp +++ b/src/server/game/World/ClientlessPlayerMgr.cpp @@ -131,6 +131,12 @@ uint32 ClientlessPlayerMgr::GetQueuedActionCount(uint32 characterGuid) const return itr != _records.end() ? uint32(itr->second.Actions.size()) : 0u; } +std::deque const* ClientlessPlayerMgr::GetQueuedActions(uint32 characterGuid) const +{ + auto itr = _records.find(characterGuid); + return itr != _records.end() ? &itr->second.Actions : nullptr; +} + void ClientlessPlayerMgr::Update(uint32 diff) { for (auto itr = _records.begin(); itr != _records.end(); ) diff --git a/src/server/game/World/ClientlessPlayerMgr.h b/src/server/game/World/ClientlessPlayerMgr.h index 4153cf723a..1fc96afb38 100644 --- a/src/server/game/World/ClientlessPlayerMgr.h +++ b/src/server/game/World/ClientlessPlayerMgr.h @@ -56,6 +56,7 @@ public: bool QueueAction(uint32 characterGuid, ClientlessAction action); bool PopNextAction(uint32 characterGuid, ClientlessAction& action); uint32 GetQueuedActionCount(uint32 characterGuid) const; + std::deque const* GetQueuedActions(uint32 characterGuid) const; void Update(uint32 diff); diff --git a/src/server/scripts/Custom/clientless/adventure/generic.lua b/src/server/scripts/Custom/clientless/adventure/generic.lua new file mode 100644 index 0000000000..b92f7a7f58 --- /dev/null +++ b/src/server/scripts/Custom/clientless/adventure/generic.lua @@ -0,0 +1,3 @@ +-- Generic AdventureBot routine: semantic intent only. +log("generic adventure routine loaded for " .. character_name) +intend("idle", { routine = "generic", purpose = "await_planner" }) diff --git a/src/server/scripts/Custom/clientless/adventure/overrides/arthoran.lua b/src/server/scripts/Custom/clientless/adventure/overrides/arthoran.lua new file mode 100644 index 0000000000..ee64208deb --- /dev/null +++ b/src/server/scripts/Custom/clientless/adventure/overrides/arthoran.lua @@ -0,0 +1,3 @@ +-- Arthoran completely overrides generic.lua. +log("Arthoran adventure override loaded") +intend("socialize", { city = "Ironforge", venue = "tavern", activity = "drink", purpose = "leisure" }) diff --git a/src/server/scripts/Custom/clientless/tests/smoke.lua b/src/server/scripts/Custom/clientless/tests/smoke.lua new file mode 100644 index 0000000000..978018757d --- /dev/null +++ b/src/server/scripts/Custom/clientless/tests/smoke.lua @@ -0,0 +1,4 @@ +-- Minimal C7 BugLab/TestBot API smoke script. +log("test smoke script loaded for " .. character_name) +intend("test", { suite = "smoke", purpose = "verify_lua_bridge" }) +queue_action("marker", { name = "c7_smoke" }) diff --git a/src/server/scripts/Custom/viano_bot_commands.cpp b/src/server/scripts/Custom/viano_bot_commands.cpp index 57919e0697..bf9b04a07a 100644 --- a/src/server/scripts/Custom/viano_bot_commands.cpp +++ b/src/server/scripts/Custom/viano_bot_commands.cpp @@ -12,6 +12,7 @@ #include "ScriptMgr.h" #include "Chat.h" #include "ClientlessPlayerMgr.h" +#include "ClientlessLuaEngine.h" #include "DatabaseEnv.h" #include "Creature.h" #include "Group.h" @@ -633,7 +634,7 @@ namespace VianoBotCommands uint32 accountId = fields[0].GetUInt32(); std::string characterName = fields[1].GetString(); - if (!sClientlessPlayerMgr->StartPlayer(accountId, characterGuid)) + if (!sClientlessPlayerMgr->StartPlayer(accountId, characterGuid, CreateClientlessAdventureLuaController())) { handler->PSendSysMessage( "Clientless login failed for '%s' (GUID %u, account %u). Check worldserver log for the rejection reason.", @@ -647,6 +648,63 @@ namespace VianoBotCommands return true; } + static bool HandleClientlessInfo(ChatHandler* handler, char const* args) + { + uint32 characterGuid = 0; + if (!ParseCharacterGuid(handler, args, characterGuid)) + { + handler->SendSysMessage("Usage: .clientless info "); + return false; + } + + if (!sClientlessPlayerMgr->IsManaged(characterGuid)) + { + handler->PSendSysMessage("Character GUID %u is not managed by ClientlessPlayerMgr.", characterGuid); + return true; + } + + uint32 accountId = sClientlessPlayerMgr->GetAccountId(characterGuid); + ClientlessPlayerState state = sWorld->GetClientlessPlayerState(characterGuid); + + char const* stateName = "Unknown"; + switch (state) + { + case ClientlessPlayerState::Offline: stateName = "Offline"; break; + case ClientlessPlayerState::Loading: stateName = "Loading"; break; + case ClientlessPlayerState::Online: stateName = "Online"; break; + case ClientlessPlayerState::Removing: stateName = "Removing"; break; + } + + handler->PSendSysMessage("Clientless GUID %u: account=%u state=%s", characterGuid, accountId, stateName); + + if (ClientlessIntent const* intent = sClientlessPlayerMgr->GetIntent(characterGuid)) + { + handler->PSendSysMessage("Intent: %s", intent->Name.c_str()); + if (intent->Parameters.empty()) + handler->SendSysMessage(" parameters: "); + else + for (auto const& parameter : intent->Parameters) + handler->PSendSysMessage(" %s = %s", parameter.first.c_str(), parameter.second.c_str()); + } + else + handler->SendSysMessage("Intent: "); + + std::deque const* actions = sClientlessPlayerMgr->GetQueuedActions(characterGuid); + handler->PSendSysMessage("Action queue: %u", actions ? uint32(actions->size()) : 0u); + + if (actions) + { + uint32 index = 0; + for (ClientlessAction const& action : *actions) + { + handler->PSendSysMessage(" [%u] %s", index++, action.Name.c_str()); + for (auto const& parameter : action.Parameters) + handler->PSendSysMessage(" %s = %s", parameter.first.c_str(), parameter.second.c_str()); + } + } + + return true; + } static bool HandleClientlessLogout(ChatHandler* handler, char const* args) { uint32 characterGuid = 0; @@ -704,6 +762,7 @@ namespace VianoBotCommands { { "login", rbac::RBAC_PERM_COMMAND_GM, false, &HandleClientlessLogin, "" }, { "logout", rbac::RBAC_PERM_COMMAND_GM, false, &HandleClientlessLogout, "" }, + { "info", rbac::RBAC_PERM_COMMAND_GM, false, &HandleClientlessInfo, "" }, }; static std::vector commandTable =