add:添加自动生成定义工具。
This commit is contained in:
parent
22298f0583
commit
bd6af3cfd8
3
.luarc.json
Normal file
3
.luarc.json
Normal file
@ -0,0 +1,3 @@
|
||||
{
|
||||
"workspace.library": ["./build/bin/Debug/def", "./build/bin/Release/def"]
|
||||
}
|
74
.vscode/settings.json
vendored
74
.vscode/settings.json
vendored
@ -32,5 +32,79 @@
|
||||
"ja": true,
|
||||
"zh-hant": true,
|
||||
"zh-hans": true
|
||||
},
|
||||
"files.associations": {
|
||||
"filesystem": "cpp",
|
||||
"algorithm": "cpp",
|
||||
"atomic": "cpp",
|
||||
"cctype": "cpp",
|
||||
"charconv": "cpp",
|
||||
"chrono": "cpp",
|
||||
"clocale": "cpp",
|
||||
"cmath": "cpp",
|
||||
"cstddef": "cpp",
|
||||
"cstdint": "cpp",
|
||||
"cstdio": "cpp",
|
||||
"cstdlib": "cpp",
|
||||
"cstring": "cpp",
|
||||
"ctime": "cpp",
|
||||
"cwchar": "cpp",
|
||||
"exception": "cpp",
|
||||
"forward_list": "cpp",
|
||||
"fstream": "cpp",
|
||||
"functional": "cpp",
|
||||
"initializer_list": "cpp",
|
||||
"iomanip": "cpp",
|
||||
"ios": "cpp",
|
||||
"iosfwd": "cpp",
|
||||
"iostream": "cpp",
|
||||
"istream": "cpp",
|
||||
"iterator": "cpp",
|
||||
"limits": "cpp",
|
||||
"list": "cpp",
|
||||
"locale": "cpp",
|
||||
"map": "cpp",
|
||||
"memory": "cpp",
|
||||
"mutex": "cpp",
|
||||
"new": "cpp",
|
||||
"optional": "cpp",
|
||||
"ostream": "cpp",
|
||||
"ratio": "cpp",
|
||||
"regex": "cpp",
|
||||
"sstream": "cpp",
|
||||
"stdexcept": "cpp",
|
||||
"streambuf": "cpp",
|
||||
"string": "cpp",
|
||||
"string_view": "cpp",
|
||||
"system_error": "cpp",
|
||||
"thread": "cpp",
|
||||
"tuple": "cpp",
|
||||
"type_traits": "cpp",
|
||||
"typeinfo": "cpp",
|
||||
"unordered_map": "cpp",
|
||||
"utility": "cpp",
|
||||
"vector": "cpp",
|
||||
"xfacet": "cpp",
|
||||
"xhash": "cpp",
|
||||
"xiosbase": "cpp",
|
||||
"xlocale": "cpp",
|
||||
"xlocbuf": "cpp",
|
||||
"xlocinfo": "cpp",
|
||||
"xlocmes": "cpp",
|
||||
"xlocmon": "cpp",
|
||||
"xlocnum": "cpp",
|
||||
"xloctime": "cpp",
|
||||
"xmemory": "cpp",
|
||||
"xmemory0": "cpp",
|
||||
"xstddef": "cpp",
|
||||
"xstring": "cpp",
|
||||
"xtr1common": "cpp",
|
||||
"xtree": "cpp",
|
||||
"xutility": "cpp",
|
||||
"bit": "cpp",
|
||||
"compare": "cpp",
|
||||
"concepts": "cpp",
|
||||
"format": "cpp",
|
||||
"stop_token": "cpp"
|
||||
}
|
||||
}
|
@ -1,6 +1,6 @@
|
||||
cmake_minimum_required(VERSION 3.16)
|
||||
|
||||
project(elua LANGUAGES CXX)
|
||||
project(exlua LANGUAGES CXX)
|
||||
set(CMAKE_CXX_STANDARD 17)
|
||||
set(CMAKE_CXX_STANDARD_REQUIRED ON)
|
||||
|
||||
@ -12,9 +12,9 @@ message(STATUS "System: ${CMAKE_SYSTEM_NAME}")
|
||||
message(STATUS "Compiler CXX ID: ${CMAKE_CXX_COMPILER_ID}")
|
||||
|
||||
# Set output directories
|
||||
set(LIBRARY_OUTPUT_PATH ${PROJECT_BINARY_DIR}/lib/${CMAKE_BUILD_TYPE})
|
||||
set(CMAKE_RUNTIME_OUTPUT_DIRECTORY ${PROJECT_BINARY_DIR}/bin/${CMAKE_BUILD_TYPE}/)
|
||||
set(LIBRARY_OUTPUT_PATH ${PROJECT_BINARY_DIR}/lib/)
|
||||
set(CMAKE_RUNTIME_OUTPUT_DIRECTORY ${PROJECT_BINARY_DIR}/bin/)
|
||||
|
||||
add_subdirectory(lua)
|
||||
add_subdirectory(fs)
|
||||
add_executable(elua main.cpp)
|
||||
add_subdirectory(gendef)
|
3
def_fen.bat
Normal file
3
def_fen.bat
Normal file
@ -0,0 +1,3 @@
|
||||
@echo off
|
||||
.\build\bin\gendef.exe .\fs\lib.cxx lua_fs .\build\defs
|
||||
pause
|
@ -1,6 +1,5 @@
|
||||
-- 加载模块
|
||||
local fs = require "lua_fs"
|
||||
|
||||
local fs = require("lua_fs")
|
||||
print(fs.file_exists("D:\\download"))
|
||||
local files = fs.get_dir_files("D:\\download")
|
||||
if files then
|
||||
|
@ -16,6 +16,9 @@ int file_exists(lua_State* L)
|
||||
return 1;
|
||||
}
|
||||
|
||||
/// @brief 文件是否是一个常规文件
|
||||
/// @param path 文件路径
|
||||
/// @return
|
||||
int is_regular_file(lua_State* L)
|
||||
{
|
||||
const char* path = luaL_checkstring(L, 1);
|
||||
@ -24,6 +27,10 @@ int is_regular_file(lua_State* L)
|
||||
return 1;
|
||||
}
|
||||
|
||||
/// @brief 复制文件
|
||||
/// @param source 源文件(string)
|
||||
/// @param destination 目标文件(string)
|
||||
/// @return bool
|
||||
int copy_file(lua_State* L)
|
||||
{
|
||||
const char* source = luaL_checkstring(L, 1);
|
||||
|
10
gendef/CMakeLists.txt
Normal file
10
gendef/CMakeLists.txt
Normal file
@ -0,0 +1,10 @@
|
||||
cmake_minimum_required(VERSION 3.16)
|
||||
|
||||
project(gendef LANGUAGES CXX)
|
||||
set(CMAKE_CXX_STANDARD 17)
|
||||
|
||||
if(MSVC)
|
||||
add_compile_options("/source-charset:utf-8")
|
||||
endif()
|
||||
|
||||
add_executable(gendef main.cxx)
|
155
gendef/main.cxx
Normal file
155
gendef/main.cxx
Normal file
@ -0,0 +1,155 @@
|
||||
#include <filesystem>
|
||||
#include <fstream>
|
||||
#include <iostream>
|
||||
#include <regex>
|
||||
#include <sstream>
|
||||
#include <string>
|
||||
#include <vector>
|
||||
|
||||
namespace fs = std::filesystem;
|
||||
|
||||
std::string to_full(const std::string& path)
|
||||
{
|
||||
fs::path base;
|
||||
if (fs::path(path).is_relative()) {
|
||||
base = fs::current_path();
|
||||
base.append(path);
|
||||
} else {
|
||||
base = fs::path(path);
|
||||
}
|
||||
fs::path ret = fs::absolute(base);
|
||||
return ret.string();
|
||||
}
|
||||
|
||||
struct LuaFunction {
|
||||
std::string name;
|
||||
std::string brief;
|
||||
std::vector<std::pair<std::string, std::string>> params;
|
||||
std::string returnType;
|
||||
};
|
||||
|
||||
// 解析 C++ 代码并提取函数信息
|
||||
std::vector<LuaFunction> parse_cpp(const std::string& cpp_code)
|
||||
{
|
||||
std::vector<LuaFunction> functions;
|
||||
std::regex func_regex(R"(///\s*@brief\s*(.*))");
|
||||
std::regex param_regex(R"(///\s*@param\s*(\w+)\s*(.*))");
|
||||
std::regex return_regex(R"(///\s*@return\s*(.*))");
|
||||
std::regex name_regex(R"(\bint\s+(\w+)\s*\()");
|
||||
|
||||
LuaFunction current;
|
||||
bool inFunction = false;
|
||||
|
||||
std::istringstream stream(cpp_code);
|
||||
std::string line;
|
||||
while (std::getline(stream, line)) {
|
||||
std::smatch match;
|
||||
|
||||
if (std::regex_search(line, match, func_regex)) {
|
||||
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());
|
||||
} 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)) {
|
||||
current.name = match[1].str();
|
||||
} else if (inFunction && line.find("}") != std::string::npos) {
|
||||
functions.push_back(current);
|
||||
current = LuaFunction();
|
||||
inFunction = false;
|
||||
}
|
||||
}
|
||||
return functions;
|
||||
}
|
||||
|
||||
// 生成 Lua 绑定文件
|
||||
void generate_lua_file(const std::vector<LuaFunction>& functions, const std::string& filename, const std::string& mod_name)
|
||||
{
|
||||
std::ofstream out(filename);
|
||||
if (!out) {
|
||||
std::cerr << "can't create file: " << filename << std::endl;
|
||||
return;
|
||||
}
|
||||
|
||||
std::string mod_call = mod_name + "_call";
|
||||
std::string mod_call_p = mod_name + "_call.";
|
||||
|
||||
out << "---@meta\n";
|
||||
out << "local " << mod_call << " = require(\"" << mod_name << "\")\n\n";
|
||||
out << "local M = {}\n\n";
|
||||
|
||||
for (const auto& func : functions) {
|
||||
out << "--- " << func.brief << "\n";
|
||||
for (const auto& param : func.params) {
|
||||
out << "--- @param " << param.first << " " << param.second << "\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;
|
||||
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;
|
||||
if (i < func.params.size() - 1)
|
||||
out << ", ";
|
||||
}
|
||||
out << ")\nend\n\n";
|
||||
}
|
||||
|
||||
out << "return M\n";
|
||||
out.close();
|
||||
std::cout << "Lua file gen success: " << filename << std::endl;
|
||||
}
|
||||
|
||||
// 主函数
|
||||
int main(int argc, char* argv[])
|
||||
{
|
||||
if (argc < 4) {
|
||||
std::cout << "arg1: cxx file.\n";
|
||||
std::cout << "arg2: module name.\n";
|
||||
std::cout << "arg3: out dir.\n";
|
||||
return -1;
|
||||
}
|
||||
|
||||
std::string arg_file(argv[1]);
|
||||
std::string arg_mod(argv[2]);
|
||||
std::string arg_out(argv[3]);
|
||||
|
||||
arg_file = to_full(arg_file);
|
||||
arg_out = to_full(arg_out);
|
||||
std::string out_file = fs::path(arg_out).append(arg_mod + ".lua").string();
|
||||
|
||||
std::cout << "file: " << arg_file << std::endl;
|
||||
std::cout << "mod: " << arg_mod << std::endl;
|
||||
std::cout << "out: " << arg_out << std::endl;
|
||||
|
||||
if (!fs::exists(arg_file)) {
|
||||
std::cerr << "file not found: " << arg_file << std::endl;
|
||||
return -2;
|
||||
}
|
||||
try {
|
||||
if (!fs::exists(arg_out)) {
|
||||
fs::create_directories(arg_out);
|
||||
}
|
||||
} catch (const std::exception& e) {
|
||||
std::cerr << "create dir auto failed: " << e.what() << '\n';
|
||||
return -3;
|
||||
}
|
||||
|
||||
std::string s;
|
||||
std::ifstream in(arg_file);
|
||||
if (!in.is_open()) {
|
||||
std::cerr << "open file failed: " << arg_file << std::endl;
|
||||
return -1;
|
||||
}
|
||||
std::istreambuf_iterator<char> iterf(in);
|
||||
std::istreambuf_iterator<char> iter;
|
||||
std::string content(iterf, iter);
|
||||
std::vector<LuaFunction> functions = parse_cpp(content);
|
||||
generate_lua_file(functions, out_file, arg_mod);
|
||||
return 0;
|
||||
}
|
Loading…
x
Reference in New Issue
Block a user