From c7ba237933310866a7f7945e5a2d92cb777cd503 Mon Sep 17 00:00:00 2001 From: MihailRis Date: Wed, 22 Jul 2026 21:55:53 +0300 Subject: [PATCH] add xml library --- src/coders/xml.cpp | 8 +++ src/coders/xml.hpp | 6 ++ src/logic/scripting/lua/libs/api_lua.hpp | 1 + src/logic/scripting/lua/libs/libxml.cpp | 90 ++++++++++++++++++++++++ src/logic/scripting/lua/lua_engine.cpp | 1 + 5 files changed, 106 insertions(+) create mode 100644 src/logic/scripting/lua/libs/libxml.cpp diff --git a/src/coders/xml.cpp b/src/coders/xml.cpp index 93e1c8031..b40ee7700 100644 --- a/src/coders/xml.cpp +++ b/src/coders/xml.cpp @@ -528,3 +528,11 @@ std::string xml::stringify( return ss.str(); } + +std::string xml::stringify( + const Node& element, bool nice, const std::string& indentStr +) { + std::stringstream ss; + stringifyElement(ss, element, nice, indentStr, 0); + return ss.str(); +} diff --git a/src/coders/xml.hpp b/src/coders/xml.hpp index 4954caa17..1a8a94ca5 100644 --- a/src/coders/xml.hpp +++ b/src/coders/xml.hpp @@ -119,6 +119,12 @@ namespace xml { const std::string& indentStr = " " ); + std::string stringify( + const Node& element, + bool nice = true, + const std::string& indentStr = " " + ); + /// @brief Read XML Document from string /// @param filename file name will be shown in error messages /// @param source xml source code string diff --git a/src/logic/scripting/lua/libs/api_lua.hpp b/src/logic/scripting/lua/libs/api_lua.hpp index e304d6dc1..6d135c100 100644 --- a/src/logic/scripting/lua/libs/api_lua.hpp +++ b/src/logic/scripting/lua/libs/api_lua.hpp @@ -53,6 +53,7 @@ extern const luaL_Reg vec3lib[]; // vecn.cpp extern const luaL_Reg vec4lib[]; // vecn.cpp extern const luaL_Reg weatherlib[]; // gfx.weather extern const luaL_Reg worldlib[]; +extern const luaL_Reg xmllib[]; extern const luaL_Reg yamllib[]; // Components diff --git a/src/logic/scripting/lua/libs/libxml.cpp b/src/logic/scripting/lua/libs/libxml.cpp new file mode 100644 index 000000000..e2121465d --- /dev/null +++ b/src/logic/scripting/lua/libs/libxml.cpp @@ -0,0 +1,90 @@ +#include "coders/xml.hpp" +#include "api_lua.hpp" + +static int push_xml(lua::State* L, const xml::xmlelement& elem) { + if (elem.isText()) { + return lua::pushlstring(L, elem.getInnerText()); + } + + const auto& attrs = elem.getAttributes(); + const auto& elems = elem.getElements(); + const auto& tag = elem.getTag(); + + lua::createtable(L, elem.size(), attrs.size() + 1); + lua::pushlstring(L, tag); + lua::setfield(L, "@"); + + for (int i = 0; i < elems.size(); i++) { + push_xml(L, *elems[i]); + lua::rawseti(L, i + 1); + } + + for (const auto& [key, value] : attrs) { + lua::pushlstring(L, value.getText()); + lua::setfield(L, key); + } + return 1; +} + +static std::unique_ptr toxml(lua::State* L) { + lua::getfield(L, "@"); + auto tag = lua::require_lstring(L, -1); + lua::pop(L); + + auto elem = std::make_unique(std::string(tag)); + int length = lua::objlen(L, -1); + + for (int i = 0; i < length; i++) { + lua::rawgeti(L, i + 1); + elem->add(toxml(L)); + lua::pop(L); + } + + lua::pushnil(L); + while (lua::next(L, -2)) { + lua::pushvalue(L, -2); + do { + if (!lua::isstring(L, -1)) { + break; + } + auto key = lua::require_lstring(L, -1); + if (key == "@") { + break; + } + auto value = lua::require_lstring(L, -2); + elem->set(std::string(key), std::string(value)); + } while (false); + + lua::pop(L, 2); + } + return elem; +} + +static int l_tostring(lua::State* L) { + bool nice = lua::isboolean(L, 2) ? lua::toboolean(L, 2) : true; + lua::pushvalue(L, 1); + auto xml = toxml(L); + lua::pop(L); + return lua::pushstring(L, xml::stringify(*xml, nice)); +} + +static int l_parse(lua::State* L) { + auto string = lua::require_string(L, 1); + auto document = xml::parse("[string]", string); + return push_xml(L, *document->getRoot()); +} + + +static int l_parse_vcd(lua::State* L) { + auto string = lua::require_string(L, 1); + auto rootTag = lua::tostring(L, 2); + auto document = xml::parse_vcm("[string]", string, rootTag ? rootTag : "root"); + return push_xml(L, *document->getRoot()); +} + +const luaL_Reg xmllib[] = { + {"tostring", lua::wrap}, + {"parse", lua::wrap}, + {"parse_vcd", lua::wrap}, + {nullptr, nullptr} +}; diff --git a/src/logic/scripting/lua/lua_engine.cpp b/src/logic/scripting/lua/lua_engine.cpp index afb2f42eb..276dbaa74 100644 --- a/src/logic/scripting/lua/lua_engine.cpp +++ b/src/logic/scripting/lua/lua_engine.cpp @@ -66,6 +66,7 @@ static void create_libs(State* L, StateType stateType) { openlib(L, "vec2", vec2lib); openlib(L, "vec3", vec3lib); openlib(L, "vec4", vec4lib); + openlib(L, "xml", xmllib); openlib(L, "yaml", yamllib); openlib(L, "__vc_app", applib);