From f709f3a2a1b1bb57fb4e33662124c1eec817f74f Mon Sep 17 00:00:00 2001 From: taynpg Date: Thu, 13 Feb 2025 11:21:05 +0800 Subject: [PATCH] =?UTF-8?q?fix=EF=BC=9A=E4=BC=98=E5=8C=96=E8=87=AA?= =?UTF-8?q?=E5=8A=A8=E7=94=9F=E6=88=90=E8=84=9A=E6=9C=AC=E3=80=82?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .luarc.json | 2 +- fs/debug.bat | 2 -- fs/env.bat | 2 ++ fs/fs.lua | 2 +- fs/lib.cxx | 31 ++++++++++++++++++++----------- gendef/main.cxx | 18 +++++++++++------- 6 files changed, 35 insertions(+), 22 deletions(-) delete mode 100644 fs/debug.bat create mode 100644 fs/env.bat diff --git a/.luarc.json b/.luarc.json index 42f45f5..6934735 100644 --- a/.luarc.json +++ b/.luarc.json @@ -1,3 +1,3 @@ { - "workspace.library": ["./build/bin/Debug/def", "./build/bin/Release/def"] + "workspace.library": ["./build/defs"] } \ No newline at end of file diff --git a/fs/debug.bat b/fs/debug.bat deleted file mode 100644 index f09bab8..0000000 --- a/fs/debug.bat +++ /dev/null @@ -1,2 +0,0 @@ -@echo off -cmd /k "set PATH=%PATH%;%~dp0..\build\bin\Debug" \ No newline at end of file diff --git a/fs/env.bat b/fs/env.bat new file mode 100644 index 0000000..cf7210e --- /dev/null +++ b/fs/env.bat @@ -0,0 +1,2 @@ +@echo off +cmd /k "set PATH=%PATH%;%~dp0..\build\bin\" \ No newline at end of file diff --git a/fs/fs.lua b/fs/fs.lua index 81fbe4d..4a5b111 100644 --- a/fs/fs.lua +++ b/fs/fs.lua @@ -1,7 +1,7 @@ -- 加载模块 local fs = require("lua_fs") print(fs.file_exists("D:\\download")) -local files = fs.get_dir_files("D:\\download") +local files = fs.get_dir_files("D:\\") if files then for i, filename in ipairs(files) do print(i, filename) -- 输出索引和文件名 diff --git a/fs/lib.cxx b/fs/lib.cxx index ce963a4..40d7baa 100644 --- a/fs/lib.cxx +++ b/fs/lib.cxx @@ -8,6 +8,15 @@ static const luaL_Reg lua_fs[] = { {"file_exists", file_exists}, {"is_regular_file", is_regular_file}, {"copy_file", copy_file}, {"delete_file", delete_file}, {"get_dir_files", get_dir_files}, {NULL, NULL}}; +int luaopen_lua_fs(lua_State* L) +{ + luaL_newlib(L, lua_fs); + return 1; +} + +/// @brief 文件是否存在 +/// @param path string 文件路径 +/// @return boolean int file_exists(lua_State* L) { const char* path = luaL_checkstring(L, 1); @@ -17,8 +26,8 @@ int file_exists(lua_State* L) } /// @brief 文件是否是一个常规文件 -/// @param path 文件路径 -/// @return +/// @param path string 文件路径 +/// @return boolean int is_regular_file(lua_State* L) { const char* path = luaL_checkstring(L, 1); @@ -28,9 +37,9 @@ int is_regular_file(lua_State* L) } /// @brief 复制文件 -/// @param source 源文件(string) -/// @param destination 目标文件(string) -/// @return bool +/// @param source string 源文件 +/// @param destination string 目标文件 +/// @return boolean int copy_file(lua_State* L) { const char* source = luaL_checkstring(L, 1); @@ -40,6 +49,9 @@ int copy_file(lua_State* L) return 1; } +/// @brief 删除文件 +/// @param path 文件路径(string) +/// @return boolean int delete_file(lua_State* L) { const char* path = luaL_checkstring(L, 1); @@ -48,12 +60,9 @@ int delete_file(lua_State* L) return 1; } -int luaopen_lua_fs(lua_State* L) -{ - luaL_newlib(L, lua_fs); - return 1; -} - +/// @brief 获取一个目录下所有文件(不递归) +/// @param dir_path string 目录 +/// @return table int get_dir_files(lua_State* L) { // 获取第一个参数:目录路径 diff --git a/gendef/main.cxx b/gendef/main.cxx index 3148e00..f64dfc7 100644 --- a/gendef/main.cxx +++ b/gendef/main.cxx @@ -24,7 +24,8 @@ std::string to_full(const std::string& path) struct LuaFunction { std::string name; std::string brief; - std::vector> params; + std::vector> + params; // (name, type, description) std::string returnType; }; @@ -33,7 +34,7 @@ std::vector parse_cpp(const std::string& cpp_code) { std::vector functions; std::regex func_regex(R"(///\s*@brief\s*(.*))"); - std::regex param_regex(R"(///\s*@param\s*(\w+)\s*(.*))"); + std::regex param_regex(R"(///\s*@param\s*(\w+)\s*(\w+)\s*(.*))"); std::regex return_regex(R"(///\s*@return\s*(.*))"); std::regex name_regex(R"(\bint\s+(\w+)\s*\()"); @@ -49,7 +50,8 @@ std::vector parse_cpp(const std::string& cpp_code) current.brief = match[1].str(); inFunction = true; } else if (std::regex_search(line, match, param_regex)) { - current.params.emplace_back(match[1].str(), match[2].str()); + // 将参数的name, type和description存储到params中 + current.params.emplace_back(match[1].str(), match[2].str(), match[3].str()); } else if (std::regex_search(line, match, return_regex)) { current.returnType = match[1].str().empty() ? "boolean" : match[1].str(); } else if (std::regex_search(line, match, name_regex)) { @@ -64,7 +66,8 @@ std::vector parse_cpp(const std::string& cpp_code) } // 生成 Lua 绑定文件 -void generate_lua_file(const std::vector& functions, const std::string& filename, const std::string& mod_name) +void generate_lua_file(const std::vector& functions, const std::string& filename, + const std::string& mod_name) { std::ofstream out(filename); if (!out) { @@ -82,18 +85,19 @@ void generate_lua_file(const std::vector& functions, const std::str for (const auto& func : functions) { out << "--- " << func.brief << "\n"; for (const auto& param : func.params) { - out << "--- @param " << param.first << " " << param.second << "\n"; + out << "--- @param " << std::get<0>(param) << " " << std::get<1>(param) << " " + << std::get<2>(param) << "\n"; } out << "--- @return " << func.returnType << "\n"; out << "function M." << func.name << "("; for (size_t i = 0; i < func.params.size(); ++i) { - out << func.params[i].first; + out << std::get<0>(func.params[i]); if (i < func.params.size() - 1) out << ", "; } out << ")\n return " << mod_call_p << func.name << "("; for (size_t i = 0; i < func.params.size(); ++i) { - out << func.params[i].first; + out << std::get<0>(func.params[i]); if (i < func.params.size() - 1) out << ", "; }