转码工具页面功能

This commit is contained in:
taocong
2026-05-25 10:18:19 +08:00
commit 3db5fe7f13
107 changed files with 8591 additions and 0 deletions
@@ -0,0 +1,44 @@
cmake_minimum_required(VERSION 3.15)
project(uf2touft3_binding VERSION 1.0.0 LANGUAGES CXX)
set(CMAKE_CXX_STANDARD 17)
set(CMAKE_CXX_STANDARD_REQUIRED ON)
# 查找 Python
find_package(Python3 COMPONENTS Interpreter Development REQUIRED)
# 尝试查找 pybind11
# 方法 1: 通过 pip 安装的 pybind11
execute_process(
COMMAND ${Python3_EXECUTABLE} -m pybind11 --cmakedir
OUTPUT_VARIABLE PYBIND11_CMAKE_DIR
OUTPUT_STRIP_TRAILING_WHITESPACE
ERROR_QUIET
)
if(PYBIND11_CMAKE_DIR)
list(APPEND CMAKE_PREFIX_PATH ${PYBIND11_CMAKE_DIR})
endif()
find_package(pybind11 REQUIRED)
# 创建 Python 模块
pybind11_add_module(uf2touft3_binding uf2touft3_binding.cpp)
# Windows 特定设置
if(WIN32)
set_target_properties(uf2touft3_binding PROPERTIES
SUFFIX ".pyd"
)
endif()
# 安装目标
install(TARGETS uf2touft3_binding
LIBRARY DESTINATION .
RUNTIME DESTINATION .
)
message(STATUS "Python3 executable: ${Python3_EXECUTABLE}")
message(STATUS "Python3 include dirs: ${Python3_INCLUDE_DIRS}")
message(STATUS "Python3 libraries: ${Python3_LIBRARIES}")
message(STATUS "pybind11 found: ${pybind11_FOUND}")
@@ -0,0 +1,81 @@
#include <pybind11/pybind11.h>
#include <pybind11/embed.h>
#include <string>
#include <iostream>
namespace py = pybind11;
// 全局解释器持有对象
py::scoped_interpreter* g_python_interpreter = nullptr;
// 初始化 Python 解释器
extern "C" __declspec(dllexport) bool InitializePython()
{
try {
if (!g_python_interpreter) {
g_python_interpreter = new py::scoped_interpreter();
}
return true;
} catch (const std::exception& e) {
std::cerr << "Failed to initialize Python: " << e.what() << std::endl;
return false;
}
}
// 清理 Python 解释器
extern "C" __declspec(dllexport) void FinalizePython()
{
if (g_python_interpreter) {
delete g_python_interpreter;
g_python_interpreter = nullptr;
}
}
// 执行 doWork 函数
extern "C" __declspec(dllexport) bool RunConversion(const char* script_path)
{
try {
if (!g_python_interpreter) {
std::cerr << "Python interpreter not initialized!" << std::endl;
return false;
}
// 添加脚本路径到 sys.path
py::module sys = py::module::import("sys");
py::object path = sys.attr("path");
// 获取脚本目录
std::string script_dir = script_path;
size_t last_sep = script_dir.find_last_of("/\\");
if (last_sep != std::string::npos) {
script_dir = script_dir.substr(0, last_sep);
}
// 添加到 sys.path
path.attr("insert")(0, script_dir);
// 导入脚本模块
py::module script_module = py::module::import("main");
// 调用 doWork 函数
script_module.attr("doWork")();
return true;
} catch (const py::error_already_set& e) {
std::cerr << "Python error: " << e.what() << std::endl;
return false;
} catch (const std::exception& e) {
std::cerr << "Error: " << e.what() << std::endl;
return false;
}
}
// 简单的 C API 封装
PYBIND11_MODULE(uf2touft3, m) {
m.doc() = "UFT2 to UFT3 code conversion module";
m.def("initialize", &InitializePython, "Initialize Python interpreter");
m.def("finalize", &FinalizePython, "Finalize Python interpreter");
m.def("run_conversion", &RunConversion, "Run the conversion process",
py::arg("script_path"));
}