diff --git a/doc/en/scripting.md b/doc/en/scripting.md index 471bdb15..f205a237 100644 --- a/doc/en/scripting.md +++ b/doc/en/scripting.md @@ -11,7 +11,7 @@ Subsections: - [Libraries](#) - [app](scripting/builtins/libapp.md) - [base64](scripting/builtins/libbase64.md) - - [bjson, json, toml](scripting/filesystem.md) + - [bjson, json, toml, yaml](scripting/filesystem.md) - [block](scripting/builtins/libblock.md) - [byteutil](scripting/builtins/libbyteutil.md) - [cameras](scripting/builtins/libcameras.md) diff --git a/doc/en/scripting/filesystem.md b/doc/en/scripting/filesystem.md index b37c799f..8341e966 100644 --- a/doc/en/scripting/filesystem.md +++ b/doc/en/scripting/filesystem.md @@ -36,6 +36,22 @@ toml.parse(code: str) -> table Parses a TOML string into a table. +## *yaml* library + +The library contains functions for serializing and deserializing tables: + +```python +yaml.tostring(object: table) -> str +``` + +Serializes an object into a YAML string. + +```python +yaml.parse(code: str) -> table +``` + +Parses a YAML string into a table. + ## *bjson* library The library contains functions for working with the binary data exchange format [vcbjson](../../specs/binary_json_spec.md). diff --git a/doc/ru/scripting.md b/doc/ru/scripting.md index 942241b6..79e53450 100644 --- a/doc/ru/scripting.md +++ b/doc/ru/scripting.md @@ -11,7 +11,7 @@ - [Библиотеки](#) - [app](scripting/builtins/libapp.md) - [base64](scripting/builtins/libbase64.md) - - [bjson, json, toml](scripting/filesystem.md) + - [bjson, json, toml, yaml](scripting/filesystem.md) - [block](scripting/builtins/libblock.md) - [byteutil](scripting/builtins/libbyteutil.md) - [cameras](scripting/builtins/libcameras.md) diff --git a/doc/ru/scripting/filesystem.md b/doc/ru/scripting/filesystem.md index faaaf9b7..10d82c60 100644 --- a/doc/ru/scripting/filesystem.md +++ b/doc/ru/scripting/filesystem.md @@ -36,6 +36,22 @@ toml.parse(code: str) -> table Парсит TOML строку в таблицу. +## Библиотека yaml + +Библиотека содержит функции для сериализации и десериализации таблиц: + +```python +yaml.tostring(object: table) -> str +``` + +Сериализует объект в YAML строку. + +```python +yaml.parse(code: str) -> table +``` + +Парсит YAML строку в таблицу. + ## Библиотека bjson Библиотека содержит функции для работы с двоичным форматом обмена данными [vcbjson](../../specs/binary_json_spec.md). diff --git a/src/logic/scripting/lua/libs/api_lua.hpp b/src/logic/scripting/lua/libs/api_lua.hpp index 818b9c30..39d09241 100644 --- a/src/logic/scripting/lua/libs/api_lua.hpp +++ b/src/logic/scripting/lua/libs/api_lua.hpp @@ -14,6 +14,7 @@ /// int l_function_name(lua::State* L); // Libraries +extern const luaL_Reg applib[]; extern const luaL_Reg audiolib[]; extern const luaL_Reg base64lib[]; extern const luaL_Reg bjsonlib[]; @@ -38,7 +39,6 @@ extern const luaL_Reg packlib[]; extern const luaL_Reg particleslib[]; // gfx.particles extern const luaL_Reg playerlib[]; extern const luaL_Reg quatlib[]; -extern const luaL_Reg applib[]; extern const luaL_Reg text3dlib[]; // gfx.text3d extern const luaL_Reg timelib[]; extern const luaL_Reg tomllib[]; @@ -48,6 +48,7 @@ extern const luaL_Reg vec3lib[]; // vecn.cpp extern const luaL_Reg vec4lib[]; // vecn.cpp extern const luaL_Reg weatherlib[]; extern const luaL_Reg worldlib[]; +extern const luaL_Reg yamllib[]; // Components extern const luaL_Reg skeletonlib[]; diff --git a/src/logic/scripting/lua/libs/libjson.cpp b/src/logic/scripting/lua/libs/libjson.cpp index 503173bd..ed929d3d 100644 --- a/src/logic/scripting/lua/libs/libjson.cpp +++ b/src/logic/scripting/lua/libs/libjson.cpp @@ -19,4 +19,5 @@ static int l_json_parse(lua::State* L) { const luaL_Reg jsonlib[] = { {"tostring", lua::wrap}, {"parse", lua::wrap}, - {NULL, NULL}}; + {NULL, NULL} +}; diff --git a/src/logic/scripting/lua/libs/libyaml.cpp b/src/logic/scripting/lua/libs/libyaml.cpp new file mode 100644 index 00000000..3b625ddb --- /dev/null +++ b/src/logic/scripting/lua/libs/libyaml.cpp @@ -0,0 +1,20 @@ +#include "coders/yaml.hpp" +#include "api_lua.hpp" + +static int l_stringify(lua::State* L) { + auto value = lua::tovalue(L, 1); + auto string = yaml::stringify(value); + return lua::pushstring(L, string); +} + +static int l_parse(lua::State* L) { + auto string = lua::require_string(L, 1); + auto element = yaml::parse("[string]", string); + return lua::pushvalue(L, element); +} + +const luaL_Reg yamllib[] = { + {"tostring", lua::wrap}, + {"parse", lua::wrap}, + {NULL, NULL} +}; diff --git a/src/logic/scripting/lua/lua_engine.cpp b/src/logic/scripting/lua/lua_engine.cpp index ffeaf360..1ae35d39 100644 --- a/src/logic/scripting/lua/lua_engine.cpp +++ b/src/logic/scripting/lua/lua_engine.cpp @@ -56,6 +56,7 @@ static void create_libs(State* L, StateType stateType) { openlib(L, "vec2", vec2lib); openlib(L, "vec3", vec3lib); openlib(L, "vec4", vec4lib); + openlib(L, "yaml", yamllib); if (stateType == StateType::SCRIPT) { openlib(L, "app", applib);