From 09b2b9d9bc260e5c8e8f8a8da9d2b985154e2f5f Mon Sep 17 00:00:00 2001 From: seokjin1013 Date: Wed, 27 Sep 2023 04:27:12 +0900 Subject: [PATCH] add lcf2xml python build --- .gitignore | 1 + CMakeLists.txt | 13 +++++- python/pylcf2xml.cpp | 95 ++++++++++++++++++++++++++++++++++++++++++++ python/pylcf2xml.h | 23 +++++++++++ 4 files changed, 131 insertions(+), 1 deletion(-) create mode 100644 python/pylcf2xml.cpp create mode 100644 python/pylcf2xml.h diff --git a/.gitignore b/.gitignore index a639c80d..d80427e4 100644 --- a/.gitignore +++ b/.gitignore @@ -56,6 +56,7 @@ build/ # Python generated files *.pyc __pycache__/ +python/venv # other generated files Makefile diff --git a/CMakeLists.txt b/CMakeLists.txt index de8dec18..a1e408b2 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -12,7 +12,8 @@ option(LIBLCF_WITH_ICU "ICU encoding detection (when OFF fallback to iconv, not option(LIBLCF_WITH_XML "XML reading support (expat, default: ON)" ON) option(LIBLCF_UPDATE_MIMEDB "Whether to run update-mime-database after install (default: ON)" ON) option(LIBLCF_ENABLE_TOOLS "Whether to build the tools (default: ON)" ON) -option(LIBLCF_ENABLE_TESTS "Whetner to build the unit tests (default: ON)" ON) +option(LIBLCF_ENABLE_TESTS "Whether to build the unit tests (default: ON)" ON) +option(LIBLCF_ENABLE_PYTHON "Whether to build the python library (default: ON)" ON) option(LIBLCF_ENABLE_BENCHMARKS "Whether to build the benchmarks (default: OFF)" OFF) set(CMAKE_DEBUG_POSTFIX "d" CACHE STRING "Override CMAKE_DEBUG_POSTFIX.") @@ -498,6 +499,16 @@ if(LIBLCF_ENABLE_TOOLS) endforeach() endif() +# python +if (LIBLCF_ENABLE_PYTHON) + execute_process(COMMAND python -m venv ${CMAKE_CURRENT_BINARY_DIR}/python/venv) + execute_process(COMMAND ${CMAKE_CURRENT_BINARY_DIR}/python/venv/bin/python -m pip install pybind11) + FILE(GLOB pybind11_DIR ${CMAKE_CURRENT_BINARY_DIR}/python/venv/lib/python*/site-packages/pybind11/share/cmake/pybind11) + find_package(pybind11 CONFIG REQUIRED) + pybind11_add_module(pylcf2xml python/pylcf2xml.cpp) + target_link_libraries(pylcf2xml PUBLIC lcf) +endif() + # Doxygen documentation find_package(Doxygen) if(DOXYGEN_FOUND) diff --git a/python/pylcf2xml.cpp b/python/pylcf2xml.cpp new file mode 100644 index 00000000..0a0d0eee --- /dev/null +++ b/python/pylcf2xml.cpp @@ -0,0 +1,95 @@ +#include "pylcf2xml.h" +#include + +lcf::EngineVersion GetEngineVersion(std::string engine) { + if (engine == "2k3") { + return lcf::EngineVersion::e2k3; + } + return lcf::EngineVersion::e2k; +} + +std::string lmu2xml(const std::string& in, std::string engine, std::string encoding) { + std::istringstream istream(in, std::ios::binary); + auto file = lcf::LMU_Reader::Load(istream, encoding); + std::ostringstream ostream(std::ios::binary); + lcf::LMU_Reader::SaveXml(ostream, *file, GetEngineVersion(engine)); + return ostream.str(); +} + +std::string lsd2xml(const std::string& in, std::string engine, std::string encoding) { + std::istringstream istream(in, std::ios::binary); + auto file = lcf::LSD_Reader::Load(istream, encoding); + std::ostringstream ostream(std::ios::binary); + lcf::LSD_Reader::SaveXml(ostream, *file, GetEngineVersion(engine)); + return ostream.str(); +} + +std::string ldb2xml(const std::string& in, std::string encoding) { + std::istringstream istream(in, std::ios::binary); + auto file = lcf::LDB_Reader::Load(istream, encoding); + std::ostringstream ostream(std::ios::binary); + lcf::LDB_Reader::SaveXml(ostream, *file); + return ostream.str(); +} + +std::string lmt2xml(const std::string& in, std::string engine, std::string encoding) { + std::istringstream istream(in, std::ios::binary); + auto file = lcf::LMT_Reader::Load(istream, encoding); + std::ostringstream ostream(std::ios::binary); + lcf::LMT_Reader::SaveXml(ostream, *file, GetEngineVersion(engine)); + return ostream.str(); +} + +std::string xml2lmu(const std::string& in, std::string engine, std::string encoding) { + std::istringstream istream(in, std::ios::binary); + auto file = lcf::LMU_Reader::LoadXml(istream); + std::ostringstream ostream(std::ios::binary); + lcf::LMU_Reader::Save(ostream, *file, GetEngineVersion(engine), encoding); + return ostream.str(); +} + +std::string xml2lsd(const std::string& in, std::string engine, std::string encoding) { + std::istringstream istream(in, std::ios::binary); + auto file = lcf::LSD_Reader::LoadXml(istream); + std::ostringstream ostream(std::ios::binary); + lcf::LSD_Reader::Save(ostream, *file, GetEngineVersion(engine), encoding); + return ostream.str(); +} + +std::string xml2ldb(const std::string& in, std::string encoding) { + std::istringstream istream(in, std::ios::binary); + auto file = lcf::LDB_Reader::LoadXml(istream); + std::ostringstream ostream(std::ios::binary); + lcf::LDB_Reader::Save(ostream, *file, encoding); + return ostream.str(); +} + +std::string xml2lmt(const std::string& in, std::string engine, std::string encoding) { + std::istringstream istream(in, std::ios::binary); + auto file = lcf::LMT_Reader::LoadXml(istream); + std::ostringstream ostream(std::ios::binary); + lcf::LMT_Reader::Save(ostream, *file, GetEngineVersion(engine), encoding); + return ostream.str(); +} + +PYBIND11_MODULE(pylcf2xml, m) { + m.doc() = + "LCF2XML is a small tool to convert RPG Maker 2000 and 2003 data format (LMU, LMT, LDB, LSD, ...) to XML and " + "vice versa."; + m.def("lmu2xml", &lmu2xml, "Convert .lmu file into xml", pybind11::arg("in"), pybind11::kw_only(), + pybind11::arg("engine") = "2k3", pybind11::arg("encoding")); + m.def("lsd2xml", &lsd2xml, "Convert .lsd file into xml", pybind11::arg("in"), pybind11::kw_only(), + pybind11::arg("engine") = "2k3", pybind11::arg("encoding")); + m.def("ldb2xml", &ldb2xml, "Convert .ldb file into xml", pybind11::arg("in"), pybind11::kw_only(), + pybind11::arg("encoding")); + m.def("lmt2xml", &lmt2xml, "Convert .lmt file into xml", pybind11::arg("in"), pybind11::kw_only(), + pybind11::arg("engine") = "2k3", pybind11::arg("encoding")); + m.def("xml2lmu", &xml2lmu, "Convert xml file into .lmu", pybind11::arg("in"), pybind11::kw_only(), + pybind11::arg("engine") = "2k3", pybind11::arg("encoding")); + m.def("xml2lsd", &xml2lsd, "Convert xml file into .lsd", pybind11::arg("in"), pybind11::kw_only(), + pybind11::arg("engine") = "2k3", pybind11::arg("encoding")); + m.def("xml2ldb", &xml2ldb, "Convert xml file into .ldb", pybind11::arg("in"), pybind11::kw_only(), + pybind11::arg("encoding")); + m.def("xml2lmt", &xml2lmt, "Convert xml file into .lmt", pybind11::arg("in"), pybind11::kw_only(), + pybind11::arg("engine") = "2k3", pybind11::arg("encoding")); +} \ No newline at end of file diff --git a/python/pylcf2xml.h b/python/pylcf2xml.h new file mode 100644 index 00000000..68f2e00c --- /dev/null +++ b/python/pylcf2xml.h @@ -0,0 +1,23 @@ +#include +#include +#include +#include +#include +#include +#include + +std::string lmu2xml(const std::string& in, std::string engine, std::string encoding); + +std::string lsd2xml(const std::string& in, std::string engine, std::string encoding); + +std::string ldb2xml(const std::string& in, std::string encoding); + +std::string lmt2xml(const std::string& in, std::string engine, std::string encoding); + +std::string xml2lmu(const std::string& in, std::string engine, std::string encoding); + +std::string xml2lsd(const std::string& in, std::string engine, std::string encoding); + +std::string xml2ldb(const std::string& in, std::string encoding); + +std::string xml2lmt(const std::string& in, std::string engine, std::string encoding); \ No newline at end of file