#include "dl_lua_register.h" #include "dl_io.h" #include "render/dl_graphics.h" #include "system/dl_system.h" #include "script/dl_lua.h" #include "ui/dl_ui_manager.h" #include "render/dl_image.h" #include "script/dl_lua_register_container.h" #include "memory/dl_crypto.h" #include "memory/dl_memory.h" #include "tool/dl_tool_archiver.h" namespace luabridge { // GC类 using namespace dl; template struct ContainerTraits> { using Type = T; static GC construct(T* c) { return c; } static T* get(const GC& c) { return (T*)c.GetPointer(); } }; // 枚举类绑定 template <> struct Stack : luabridge::Enum { }; template <> struct Stack : luabridge::Enum { }; } // namespace luabridge namespace dl { void lua_log(std::string_view str) { log(str); } void lua_delete(Image* img) { delete img; } std::string lua_to_string(const Position2& pos) { return to_string(pos); } void lua_register_base(lua_State* L) { luabridge::getGlobalNamespace(L) .addFunction("log", lua_log) .addFunction("delete", lua_delete) .addFunction("to_string", luabridge::overload(lua_to_string)); luabridge::getGlobalNamespace(L) .beginNamespace("String") .addFunction("ErasePath", String::ErasePath) .addFunction("GetFileStem", String::GetFileStem) .addFunction("toPosition2", String::toPosition2) .addFunction("ReplaceExtension", luabridge::overload(String::ReplaceExtension)) .addFunction("EraseFilename", luabridge::overload(String::EraseFilename)) .endNamespace(); luabridge::getGlobalNamespace(L) .beginNamespace("Img") .addVariable("REGEX_FORMAT", Img::REGEX_FORMAT) //.addFunction("Resize", // luabridge::overload(Img::Resize), // luabridge::overload(Img::Resize)) .addFunction("SetAlphaZero", luabridge::overload(Img::SetAlphaZero), luabridge::overload(Img::SetAlphaZero)) /*.addFunction("Create", luabridge::overload(Img::Create), luabridge::overload(Img::Create))*/ .addFunction("Save", Img::Save) .endNamespace(); luabridge::getGlobalNamespace(L) .beginClass("Image") .endClass(); luabridge::getGlobalNamespace(L) .beginNamespace("ColorDef") .addVariable("NONE", ColorDef::NONE) .addVariable("RED", ColorDef::RED) .endNamespace(); } void lua_register_system(lua_State* L) { #ifndef DL_DISABLE_RENDER luabridge::getGlobalNamespace(L) .beginClass("ParamWindow") .addConstructor() // 忽略这个构造会出错…… .addProperty("title", &ParamWindow::_strTitle, &ParamWindow::_strTitle) .endClass(); luabridge::getGlobalNamespace(L) .beginClass("System") .addFunction("SetFuncInit", &System::lua_SetFuncInit) .addFunction("SetFuncRun", &System::lua_SetFuncRun) .addFunction("Start", (void(System::*)(const ParamWindow&))&System::Start) .endClass(); auto result = luabridge::push(L, g_system); lua_setglobal(L, "g_system"); #endif } void lua_register_file(lua_State* L) { using namespace File::Lua; luabridge::getGlobalNamespace(L) .beginNamespace("File") .beginNamespace("CopyMode") .addVariable("NONE", File::CopyMode::NONE) .addVariable("OVERRIDE", File::CopyMode::OVERRIDE) .addVariable("SKIP",File::CopyMode::SKIP) .endNamespace() .endNamespace(); luabridge::getGlobalNamespace(L) .beginNamespace("File") .addFunction("GetPathFileAll", lua_GetPathFileAll_3, lua_GetPathFileAll_2, lua_GetPathFileAll_1) .addFunction("GetAbsolute", File::GetAbsolute) .addFunction("GetRelative", File::GetRelative) .addFunction("CreateFolder", File::CreateFolder) .addFunction("DeleteContent", File::DeleteContent) .addFunction("Rename", File::Rename) .addFunction("Copy", File::Copy) .addFunction("GetFilePath", File::GetFilePath) .addFunction("GetFileName", File::GetFileName) .addFunction("GetString", File::GetString) .addFunction("IsExist", File::IsExist) .addFunction("SaveString", luabridge::overload(File::SaveString)) .endNamespace(); } void lua_register_gfx(lua_State* L) { #ifndef DL_DISABLE_RENDER luabridge::getGlobalNamespace(L) .beginClass("Graphics") .addFunction("Rectangle2D", &Graphics::Rectangle2D) .endClass(); auto result = luabridge::push(L, g_gfx); lua_setglobal(L, "g_gfx"); #endif } void lua_register_ui(lua_State* L) { #ifndef DL_DISABLE_RENDER using namespace UI; luabridge::getGlobalNamespace(L) .beginClass("UIPanel") .addFunction("Add", (void(Panel::*)(UI::Text*))&Panel::Add) .endClass(); luabridge::getGlobalNamespace(L) .beginClass("UIText") .addFunction("SetString", &UI::Text::SetString) .addFunction("SetLang", &UI::Text::SetLang) .endClass(); luabridge::getGlobalNamespace(L) .beginClass("UIManager") .addFunction("CreatePanel", (Panel * (Manager::*)())&Manager::lua_CreatePanel) .addFunction("CreateText", &Manager::CreateText) .addFunction("CreateTextDefault", &Manager::CreateTextDefault) .endClass(); auto result = luabridge::push(L, g_ui); lua_setglobal(L, "g_ui"); #endif } void lua_register_factory(lua_State* L) { luabridge::getGlobalNamespace(L) .beginClass("Factory") .addFunction("CreateText", &Factory::CreateText) .addFunction("CreateTextDefault", &Factory::CreateTextDefault) .addFunction("CreateSprite", luabridge::overload(&Factory::CreateSprite), luabridge::overload(&Factory::CreateSprite)) .addFunction("SetPath", &Factory::SetPath) .endClass(); auto result = luabridge::push(L, g_factory); lua_setglobal(L, "g_factory"); luabridge::getGlobalNamespace(L) .beginNamespace("Img") .addFunction("Create", luabridge::overload(&Img::Create), luabridge::overload(&Img::Create)) .addFunction("CreateSvg", &Img::CreateSvg) .addFunction("CreateWebp", luabridge::overload(&Img::CreateWebp), luabridge::overload(&Img::CreateWebp)) .addFunction("Save", &Img::Save) .addFunction("SetAlphaZero", luabridge::overload(&Img::SetAlphaZero), luabridge::overload(&Img::SetAlphaZero)) .addFunction("Resize", luabridge::overload(&Img::Resize), luabridge::overload(&Img::Resize)) .endNamespace(); } void lua_register_sprite(lua_State* L) { luabridge::getGlobalNamespace(L) .beginClass("Sprite") .addFunction("Render", &Sprite::Render) .addFunction("SetColor", luabridge::overload(&Sprite::SetColor), luabridge::overload(&Sprite::SetColor)) .endClass(); } void lua_register_text(lua_State* L) { luabridge::getGlobalNamespace(L) .beginClass("Text") .addFunction("Render", &Text::Render) .addFunction("SetString", &Text::SetString) .endClass(); } void lua_register_memory(lua_State* L) { luabridge::getGlobalNamespace(L) .beginNamespace("Memory") .beginNamespace("CompressMode") .addVariable("NONE", Memory::CompressMode::E_NONE) .addVariable("LZ4", Memory::CompressMode::E_LZ4) .addVariable("ZIP", Memory::CompressMode::E_ZIP) .endNamespace() .endNamespace(); using namespace dl::Crypto; luabridge::getGlobalNamespace(L) .beginNamespace("Crypto") .beginClass("Aes256Key") .addProperty("_key", &Aes256Key::_key, &Aes256Key::_key) .addProperty("_iv", &Aes256Key::_iv, &Aes256Key::_iv) .addProperty("_tag", &Aes256Key::_tag, &Aes256Key::_tag) .endClass() .addFunction("GenerateKeyAes256", &GenerateKeyAes256) .addFunction("EncryptAes256", &EncryptAes256) .addFunction("DecryptAes256", &DecryptAes256) .endNamespace(); } luabridge::LuaRef lua_json_to_lua(lua_State* L, const json& j) { switch (j.type()) { case json::value_t::null: return luabridge::LuaRef(L, luabridge::LuaNil()); case json::value_t::object: return luabridge::LuaRef(L, j); case json::value_t::array: return luabridge::LuaRef(L, j); case json::value_t::string: return luabridge::LuaRef(L, j.get()); case json::value_t::number_integer: return luabridge::LuaRef(L, j.get()); case json::value_t::number_unsigned: return luabridge::LuaRef(L, j.get()); case json::value_t::number_float: return luabridge::LuaRef(L, j.get()); case json::value_t::binary: default: break; } log_err0("未处理类型!"); return luabridge::LuaRef(L, luabridge::LuaNil()); } json lua_lua_to_json(lua_State* L, const luabridge::LuaRef& ref) { if (ref.isBool()) return ref.cast().valueOr(false); if (ref.isLightUserdata()) return ref.cast().valueOr(0); if (ref.isNumber()) return ref.cast().valueOr(0); if (ref.isString()) return ref.cast ().valueOr(""); // log_err0("未处理类型!"); return json(); } std::string lua_json_encode(json& j) { return j.dump(); } void lua_json_decode(json& j, std::string str) { j = Json::ParseString(str); } void lua_register_json(lua_State* L) { luabridge::getGlobalNamespace(L) .beginClass("json") .addConstructor() .addFunction("encode", lua_json_encode) .addFunction("decode", lua_json_decode) .addIndexMetaMethod([](const json& self, const luabridge::LuaRef& key, lua_State* L) { std::string k = key.tostring(); auto iter = self.find(k); if(iter == self.end()) return luabridge::LuaRef(L, luabridge::LuaNil()); return lua_json_to_lua(L, *iter); }) .addNewIndexMetaMethod([](json& self, const luabridge::LuaRef& key, const luabridge::LuaRef& value, lua_State* L) { std::string k = key.tostring(); self[k] = lua_lua_to_json(L, value); }) .endClass(); } void lua_register_other(lua_State* L) { luabridge::getGlobalNamespace(L) .beginNamespace("ToolArchiver") .addFunction("Compress", &Tool::Archiver::Compress) .addFunction("Decompress", luabridge::overload(&Tool::Archiver::Decompress), luabridge::overload(&Tool::Archiver::Decompress)) .endNamespace(); luabridge::getGlobalNamespace(L) .beginClass("ToolArchiverParam") .addConstructor() .addProperty("_src", &Tool::Archiver::Param::_src, &Tool::Archiver::Param::_src) .addProperty("_out", &Tool::Archiver::Param::_out, &Tool::Archiver::Param::_out) .addProperty("_compressMode", &Tool::Archiver::Param::_compressMode, &Tool::Archiver::Param::_compressMode) .addProperty("_bCrc", &Tool::Archiver::Param::_bCrc, &Tool::Archiver::Param::_bCrc) .endClass(); } #if DL_PLATFORM == DL_PLATFORM_WIN32 #define WIN32_LEAN_AND_MEAN #include void lua_register_win32(lua_State* L) { luabridge::getGlobalNamespace(L) .addFunction("system", &system); } #endif void lua_register_all(lua_State* L) { lua_register_base(L); lua_register_system(L); lua_register_file(L); lua_register_gfx(L); lua_register_ui(L); lua_register_factory(L); lua_register_sprite(L); lua_register_text(L); lua_register_memory(L); lua_register_other(L); lua_register_json(L); #if DL_PLATFORM == DL_PLATFORM_WIN32 lua_register_win32(L); #endif } }