From d85905f4f55e0b4454827c71cee63dea2dfefb8c Mon Sep 17 00:00:00 2001 From: xx Date: Sun, 16 Jun 2024 15:07:19 +0800 Subject: [PATCH] =?UTF-8?q?=E4=BC=98=E5=8C=96?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- CMakeLists.txt | 2 +- src/core/modulemanager.cpp | 54 +++++++++++++---- src/module/codec.cpp | 54 +++++++++++++++++ src/module/codec.h | 37 ++++++++++++ src/module/ini.cpp | 119 +++++++++++++++++++++++++++++++++++++ src/module/ini.h | 30 ++++++++++ src/module/sys.cpp | 31 ++++++++++ src/module/sys.h | 17 ++++++ 8 files changed, 330 insertions(+), 14 deletions(-) create mode 100644 src/module/codec.cpp create mode 100644 src/module/codec.h create mode 100644 src/module/ini.cpp create mode 100644 src/module/ini.h create mode 100644 src/module/sys.cpp create mode 100644 src/module/sys.h diff --git a/CMakeLists.txt b/CMakeLists.txt index ee0c2f4..16f6a6b 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -14,7 +14,7 @@ set(CMAKE_CXX_STANDARD 20) set(CMAKE_CXX_STANDARD_REQUIRED TRUE) set(FASTWEBCORE ${PROJECT_NAME}core) # 安装复制 -set(CMAKE_INSTALL_ALWAYS_COPY TRUE) +set(CMAKE_INSTALL_ALWAYS_COPY TRUE) # 设置根目录 set(ROOT_DIR ${CMAKE_CURRENT_SOURCE_DIR}) diff --git a/src/core/modulemanager.cpp b/src/core/modulemanager.cpp index 439a67d..748ae95 100644 --- a/src/core/modulemanager.cpp +++ b/src/core/modulemanager.cpp @@ -37,6 +37,9 @@ If you have any questions, please contact us: 1585346868@qq.com Or visit our web #include "module/mutex.h" #include "module/timer.h" #include "module/process.h" +#include "module/ini.h" +#include "module/codec.h" +#include "module/sys.h" fastweb::module_manager::module_manager(fastweb::app* app):Interface(app) { } @@ -70,13 +73,13 @@ void fastweb::module_manager::start() for (size_t i = 0; i < ms.size(); i++) { module_info mi; - std::string mod_filepath = app()->config->scripts.module_dir + "/" + ms[i]; + std::string mod_filepath =ms[i]; #ifdef _WIN32 SetDllDirectoryA(ylib::file::parent_dir(mod_filepath).c_str()); mi.dll = LoadLibrary(mod_filepath.c_str()); if (mi.dll == nullptr) { - LOG_ERROR("module loading failed, filename: " + mod_filepath + LOG_DEBUG("module loading failed, filename: " + mod_filepath +","+ codec::to_utf8(get_last_error_desc()) ); continue; @@ -192,6 +195,9 @@ void fastweb::module_manager::load_core(sol::state* lua) module::auto_lock::regist(lua); module::timer::regist(lua); module::process::regist(lua); + module::ini::regist(lua); + module::codec::regist(lua); + module::sys::regist(lua); app()->global->regist(lua); @@ -220,20 +226,42 @@ void fastweb::module_manager::load_lualib(sol::state* lua) std::vector fastweb::module_manager::modules() { std::vector results; - auto luas = ylib::file::traverse(app()->config->scripts.module_dir, "(.*\\.dll)"); - for_iter(iter, luas) - { - if (iter->second == IS_DIRECTORY) - continue; - std::string path = strutils::replace(iter->first, '\\', '/'); - if (path.find("/") != -1) + + auto insert_dll_path = [&](const std::string& dir) { + auto luas = ylib::file::traverse(dir, "(.*\\.dll)"); + for_iter(iter, luas) { - if (strutils::split(path, '/').size() != 2) - continue; //多级不支持 - if (ylib::file::parent_dir(path) != ylib::file::filename(path, false)) + if (iter->second == IS_DIRECTORY) continue; + std::string path = strutils::replace(iter->first, '\\', '/'); + if (path.find("/") != -1) + { + if (strutils::split(path, '/').size() != 2) + continue; //多级不支持 + if (ylib::file::parent_dir(path) != ylib::file::filename(path, false)) + continue; + } + results.push_back(dir+"/"+path); } - results.push_back(path); + }; + + std::string search_path = app()->config->scripts.module_dir + "/.install"; + + insert_dll_path(app()->config->scripts.module_dir); + + std::map dirs; + ylib::file::list(search_path, dirs); + for_iter(iter, dirs) + { + if (iter->second == false) + continue; + if (iter->first == "." || iter->first == "..") + continue; + + insert_dll_path(search_path+"/"+iter->first); } + +// auto luas = ylib::file::traverse(app()->config->scripts.module_dir, "(.*\\.dll)"); + return results; } \ No newline at end of file diff --git a/src/module/codec.cpp b/src/module/codec.cpp new file mode 100644 index 0000000..f37dcb8 --- /dev/null +++ b/src/module/codec.cpp @@ -0,0 +1,54 @@ +/*Software License + +Copyright(C) 2024[liuyingjie] +License Terms +Usage Rights + +Any individual or entity is free to use, copy, and distribute the binary form of this software without modification to the source code, without the need to disclose the source code. +If the source code is modified, the modifications must be open - sourced under the same license.This means that the modifications must be disclosed and accompanied by a copy of this license. +Future Versions Updates +From this version onwards, all future releases will be governed by the terms of the latest version of the license.This license will automatically be nullified and replaced by the new version. +Users must comply with the terms of the new license issued in future releases. +Liability and Disclaimer +This software is provided “as is”, without any express or implied warranties, including but not limited to the warranties of merchantability, fitness for a particular purpose, and non - infringement.In no event shall the author or copyright holder be liable for any claims, damages, or other liabilities, whether in an action of contract, tort, or otherwise, arising from, out of, or in connection with the software or the use or other dealings in the software. +Contact Information +If you have any questions, please contact us: 1585346868@qq.com Or visit our website fwlua.com. +*/ + +#include "codec.h" +#include "util/codec.h" +std::string module::codec::url_de(const std::string& value) +{ + return ylib::codec::url::de(value); +} + +std::string module::codec::url_en(const std::string& value) +{ + return ylib::codec::url::en(value); +} + +std::string module::codec::to_utf8(const std::string& value) +{ + return ylib::codec::to_utf8(value); +} + +std::string module::codec::to_gbk(const std::string& value) +{ + return ylib::codec::to_gbk(value); +} + +std::string module::codec::md5(const std::string& value) +{ + return ylib::codec::md5(value); +} + +void module::codec::regist(sol::state* lua) +{ + lua->new_usertype("codec", + "url_de", &module::codec::url_de, + "url_en", &module::codec::url_en, + "to_utf8", &module::codec::to_utf8, + "to_gbk", &module::codec::to_gbk, + "md5", &module::codec::md5 + ); +} diff --git a/src/module/codec.h b/src/module/codec.h new file mode 100644 index 0000000..079f74b --- /dev/null +++ b/src/module/codec.h @@ -0,0 +1,37 @@ +#pragma once +#include "sol/sol.hpp" +#include "basemodule.h" + +namespace module +{ + /// + /// 编解码 + /// + class codec{ + public: + /// + /// URL解码 + /// + static std::string url_de(const std::string& value); + /// + /// URL编码 + /// + static std::string url_en(const std::string& value); + /// + /// GBK转UTF8 + /// + /// + static std::string to_utf8(const std::string& value); + static std::string to_gbk(const std::string& value); + /// + /// MD5校验 + /// + /// + /// + static std::string md5(const std::string& value); + + static void regist(sol::state* lua); + }; + +} + diff --git a/src/module/ini.cpp b/src/module/ini.cpp new file mode 100644 index 0000000..422bee1 --- /dev/null +++ b/src/module/ini.cpp @@ -0,0 +1,119 @@ +/*Software License + +Copyright(C) 2024[liuyingjie] +License Terms +Usage Rights + +Any individual or entity is free to use, copy, and distribute the binary form of this software without modification to the source code, without the need to disclose the source code. +If the source code is modified, the modifications must be open - sourced under the same license.This means that the modifications must be disclosed and accompanied by a copy of this license. +Future Versions Updates +From this version onwards, all future releases will be governed by the terms of the latest version of the license.This license will automatically be nullified and replaced by the new version. +Users must comply with the terms of the new license issued in future releases. +Liability and Disclaimer +This software is provided “as is”, without any express or implied warranties, including but not limited to the warranties of merchantability, fitness for a particular purpose, and non - infringement.In no event shall the author or copyright holder be liable for any claims, damages, or other liabilities, whether in an action of contract, tort, or otherwise, arising from, out of, or in connection with the software or the use or other dealings in the software. +Contact Information +If you have any questions, please contact us: 1585346868@qq.com Or visit our website fwlua.com. +*/ + +#include "ini.h" + +module::ini::ini() +{ +} + +module::ini::~ini() +{ +} + +bool module::ini::open(const std::string& filepath) +{ + return m_ini.open(filepath); +} + +void module::ini::close() +{ + m_ini.close(); +} + +std::string module::ini::read(const std::string& node, const std::string& key, const std::string& default_value) const +{ + return m_ini.read(node,key,default_value); +} + +bool module::ini::write(const std::string& node, const std::string& key, const std::string& value) +{ + return m_ini.write(node,key,value); +} + +bool module::ini::del(const std::string& node, const std::string& key) +{ + return m_ini.del(node, key); +} + +sol::table module::ini::nodes(sol::this_state s) +{ + sol::state_view lua(s); + sol::table result_table = lua.create_table(); + int row_num = 1; + auto names = m_ini.names(); + for_iter(iter, names) + result_table[row_num++] = *iter; + return result_table; +} + +sol::table module::ini::keys(const std::string& node, sol::this_state s) +{ + sol::state_view lua(s); + sol::table result_table = lua.create_table(); + int row_num = 1; + auto names = m_ini.keys(node); + for_iter(iter, names) + result_table[row_num++] = *iter; + return result_table; +} +bool module::ini::exist_key(const std::string& node, const std::string& key) +{ + return m_ini.exist_key(node, key); +} + +bool module::ini::exist_node(const std::string& node) +{ + return m_ini.exist_name(node); +} + +sol::table module::ini::table(sol::this_state s) +{ + sol::state_view lua(s); + sol::table result_table = lua.create_table(); + + + auto nodes = m_ini.names(); + for_iter(iter, nodes) + { + sol::table node_table = lua.create_table(); + auto keys = m_ini.keys(*iter); + for (size_t i = 0; i < keys.size(); i++) + { + node_table[keys[i]] = m_ini.read(*iter,keys[i]); + } + result_table[*iter] = node_table; + } + return result_table; +} + +void module::ini::regist(sol::state* lua) +{ + lua->new_usertype("ini", + "new", sol::constructors(), + "close", &module::ini::close, + "del", &module::ini::del, + "exist_key", &module::ini::exist_key, + "exist_node", &module::ini::exist_node, + "keys", &module::ini::keys, + "nodes", &module::ini::nodes, + "open", &module::ini::open, + "read", &module::ini::read, + "table", &module::ini::table, + "write", &module::ini::write + ); +} diff --git a/src/module/ini.h b/src/module/ini.h new file mode 100644 index 0000000..0c6fcc0 --- /dev/null +++ b/src/module/ini.h @@ -0,0 +1,30 @@ +#pragma once +#include "sol/sol.hpp" +#include "util/ini.h" +namespace module +{ + /// + /// INI配置 + /// + class ini { + public: + ini(); + ~ini(); + bool open(const std::string& filepath); + void close(); + std::string read(const std::string& node, const std::string& key, const std::string& default_value) const; + bool write(const std::string& node, const std::string& key, const std::string& value); + bool del(const std::string& node, const std::string& key); + sol::table nodes(sol::this_state s); + sol::table keys(const std::string& node, sol::this_state s); + bool exist_key(const std::string& node, const std::string& key); + bool exist_node(const std::string& node); + sol::table table(sol::this_state s); + + static void regist(sol::state* lua); + private: + ylib::ini m_ini; + }; + +} + diff --git a/src/module/sys.cpp b/src/module/sys.cpp new file mode 100644 index 0000000..52f25c7 --- /dev/null +++ b/src/module/sys.cpp @@ -0,0 +1,31 @@ +/*Software License + +Copyright(C) 2024[liuyingjie] +License Terms +Usage Rights + +Any individual or entity is free to use, copy, and distribute the binary form of this software without modification to the source code, without the need to disclose the source code. +If the source code is modified, the modifications must be open - sourced under the same license.This means that the modifications must be disclosed and accompanied by a copy of this license. +Future Versions Updates +From this version onwards, all future releases will be governed by the terms of the latest version of the license.This license will automatically be nullified and replaced by the new version. +Users must comply with the terms of the new license issued in future releases. +Liability and Disclaimer +This software is provided “as is”, without any express or implied warranties, including but not limited to the warranties of merchantability, fitness for a particular purpose, and non - infringement.In no event shall the author or copyright holder be liable for any claims, damages, or other liabilities, whether in an action of contract, tort, or otherwise, arising from, out of, or in connection with the software or the use or other dealings in the software. +Contact Information +If you have any questions, please contact us: 1585346868@qq.com Or visit our website fwlua.com. +*/ + +#include "sys.h" +#include "util/system.h" +void module::sys::sleep_msec(uint32 msec) +{ + system::sleep_msec(msec); +} + +void module::sys::regist(sol::state* lua) +{ + lua->new_usertype("sys", + "sleep_msec", &module::sys::sleep_msec + + ); +} diff --git a/src/module/sys.h b/src/module/sys.h new file mode 100644 index 0000000..18d590c --- /dev/null +++ b/src/module/sys.h @@ -0,0 +1,17 @@ +#pragma once +#include "sol/sol.hpp" +#include "core/define.h" +namespace module +{ + /// + /// 系统 + /// + class sys { + public: + static void sleep_msec(uint32 msec); + + static void regist(sol::state* lua); + }; + +} +