* lua绑定库替换为sol2

This commit is contained in:
2026-04-05 16:12:20 +08:00
parent 556ad75f61
commit ee346c8f28
19 changed files with 362 additions and 503 deletions
+190 -203
View File
@@ -1,21 +1,86 @@
#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"
// 枚举类绑定
template <>
struct luabridge::Stack<dl::File::CopyMode> : luabridge::Enum<dl::File::CopyMode,
dl::File::CopyMode::NONE,
dl::File::CopyMode::OVERRIDE,
dl::File::CopyMode::SKIP>
{
};
#include "base/dl_type_def.h"
#include "render/dl_image.h"
#include "base/dl_string.h"
#include "render/dl_factory.h"
#include "system/dl_system.h"
#include "render/dl_graphics.h"
namespace sol {
using namespace dl;
// GC类声明
template <typename T>
struct unique_usertype_traits<GC<T>>
{
typedef T type;
typedef GC<T> actual_type;
static const bool value = true;
static bool is_null(const actual_type& ptr)
{
return !ptr;
}
static type* get(const actual_type& ptr)
{
return (type*)ptr.GetPointer();
}
};
// 自定义容器
/*template <>
struct is_container<Position2> : std::true_type {};*/
//template <>
//struct usertype_container<not_my_type> {
// ...
// // see below for implemetation details
//};
/*template <typename T, size_t N>
struct is_container<Array<T, N>> : std::true_type {};
template <typename T, size_t N>
struct usertype_container<Array<T, N>> {
static int size(lua_State* L)
{
Array<T, N>& v = sol::stack::get<Array<T, N>&>(L, 1);
return stack::push(L, N);
}
static int get(lua_State* L) {
Array<T, N>& v = sol::stack::get<Array<T, N>&>(L, 1);
std::ptrdiff_t idx = sol::stack::unqualified_get<std::ptrdiff_t>(L, 2);
if (idx < 0 || idx >= N)
return sol::stack::push(L, sol::lua_nil);
return sol::stack::push(L, v[idx]);
}
static int index_get(lua_State* L) {
return get(L);
}
static void begin(lua_State* L, T& ret)
{
Array<T, N>& v = sol::stack::get<Array<T, N>&>(L, 1);
ret = v[0];
return {};
}
static void end(lua_State* L, T& ret)
{
Array<T, N>& v = sol::stack::get<Array<T, N>&>(L, 1);
ret = v[N];
return {};
}
};*/
}
namespace dl
{
@@ -31,124 +96,112 @@ namespace dl
std::string lua_to_string(const Position2& pos)
{
return to_string(pos);
return dl::to_string(pos);
}
void lua_register_base(lua_State* L)
void lua_register_base(sol::state& l)
{
luabridge::getGlobalNamespace(L)
.addFunction("log", lua_log)
.addFunction("delete", lua_delete)
.addFunction("to_string",
luabridge::overload<const Position2&>(lua_to_string));
l["log"]= lua_log;
l["delete"]= lua_delete;
l["to_string"]= lua_to_string;
luabridge::getGlobalNamespace(L)
.beginNamespace("String")
.addFunction("ErasePath", String::ErasePath)
.addFunction("GetFileStem", String::GetFileStem)
.addFunction("toPosition2", String::toPosition2)
.addFunction("ReplaceExtension",
luabridge::overload<std::string_view, std::string_view>(String::ReplaceExtension))
.addFunction("EraseFilename",
luabridge::overload<std::string_view>(String::EraseFilename))
.endNamespace();
l.new_usertype<Position>("Position",
sol::call_constructor,
sol::factories([](float x, float y) {
return Position{ x, y };
}),
"x", &Position::x,
"y", &Position::y);
luabridge::getGlobalNamespace(L)
.beginNamespace("Img")
.addVariable("REGEX_FORMAT", Img::REGEX_FORMAT)
.addFunction("Resize",
luabridge::overload<const Image*, float>(Img::Resize),
luabridge::overload<const Image*, const Size&>(Img::Resize))
.addFunction("SetAlphaZero",
luabridge::overload<Image*>(Img::SetAlphaZero),
luabridge::overload<Image*, uint8_t, uint32_t>(Img::SetAlphaZero))
.addFunction("Create",
luabridge::overload<std::string_view>(Img::Create),
luabridge::overload<const Size&, const Color&>(Img::Create))
.addFunction("Save", Img::Save)
.endNamespace();
{
sol::table ll = l["String"].get_or_create<sol::table>();
ll["ErasePath"] = String::ErasePath;
ll["GetFileStem"] = String::GetFileStem;
ll["toPosition2"] = String::toPosition2;
ll["ReplaceExtension"] = sol::resolve<std::string_view, std::string_view>(
String::ReplaceExtension);
ll["EraseFilename"] = sol::resolve<std::string_view>(
String::EraseFilename);
}
{
sol::table ll = l["Img"].get_or_create<sol::table>();
ll["REGEX_FORMAT"] = Img::REGEX_FORMAT;
ll["Resize"] = sol::overload(
sol::resolve<const Image*, float>(Img::Resize),
sol::resolve<const Image*, const Size&>(Img::Resize));
ll["SetAlphaZero"] = sol::overload(
sol::resolve<Image*, uint8_t, uint32_t>(Img::SetAlphaZero),
sol::resolve<void(Image*)>(Img::SetAlphaZero));
ll["Create"] = sol::overload(
sol::resolve<const Size&, const Color&>(Img::Create),
sol::resolve<std::string_view>(Img::Create));
ll["Save"] = Img::Save;
}
luabridge::getGlobalNamespace(L)
.beginClass<Image>("Image")
.endClass();
luabridge::getGlobalNamespace(L)
.beginNamespace("ColorDef")
.addVariable("NONE", ColorDef::NONE)
.addVariable("RED", ColorDef::RED)
.endNamespace();
l.new_enum("ColorDef",
"NONE", ColorDef::NONE,
"RED", ColorDef::RED);
}
void lua_register_system(lua_State* L)
void lua_register_system(sol::state& l)
{
#ifndef DL_DISABLE_RENDER
luabridge::getGlobalNamespace(L)
.beginClass<ParamWindow>("ParamWindow")
.addConstructor<void (*)()>() // 忽略这个构造会出错……
.addProperty("title", &ParamWindow::_strTitle)
.endClass();
l.new_usertype<ParamWindow>("ParamWindow",
"title", &ParamWindow::_strTitle);
luabridge::getGlobalNamespace(L)
.beginClass<System>("System")
.addFunction("SetFuncInit", &System::lua_SetFuncInit)
.addFunction("SetFuncRun", &System::lua_SetFuncUpdate)
.addFunction("Start", (void(System::*)(const ParamWindow&))&System::Start)
.endClass();
l.new_usertype<System>("System", sol::no_constructor,
"SetFuncInit", &System::lua_SetFuncInit,
"SetFuncRun", &System::lua_SetFuncRun,
"Start", sol::resolve<void(const ParamWindow&)>(&System::Start));
auto result = luabridge::push(L, g_system);
lua_setglobal(L, "g_system");
l["g_system"] = g_system;
#endif
}
void lua_register_file(lua_State* L)
void lua_register_file(sol::state& l)
{
using namespace File::Lua;
{
sol::table ll = l["File"].get_or_create<sol::table>();
ll.new_enum("CopyMode",
"NONE", File::CopyMode::NONE,
"OVERRIDE", File::CopyMode::OVERRIDE,
"SKIP", File::CopyMode::SKIP);
}
luabridge::getGlobalNamespace(L)
.beginNamespace("g_file")
.beginNamespace("CopyMode")
.addVariable("NONE", File::CopyMode::NONE)
.addVariable("OVERRIDE", File::CopyMode::OVERRIDE)
.addVariable("SKIP",File::CopyMode::SKIP)
.endNamespace()
.endNamespace();
luabridge::getGlobalNamespace(L)
.beginNamespace("g_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("SaveString",
luabridge::overload<std::string_view, std::string_view>(File::SaveString))
.endNamespace();
{
sol::table ll = l["File"].get_or_create<sol::table>();
ll["GetPathFileAll"] = sol::overload(lua_GetPathFileAll_3, lua_GetPathFileAll_2, lua_GetPathFileAll_1);
ll["GetAbsolute"] = File::GetAbsolute;
ll["GetRelative"] = File::GetRelative;
ll["CreateFolder"] = File::CreateFolder;
ll["DeleteContent"] = File::DeleteContent;
ll["Rename"] = File::Rename;
ll["Copy"] = File::Copy;
ll["GetFilePath"] = File::GetFilePath;
ll["GetFileName"] = File::GetFileName;
ll["GetString"] = File::GetString;
ll["SaveString"] = sol::resolve<std::string_view, std::string_view>(File::SaveString);
}
}
void lua_register_gfx(lua_State* L)
void lua_register_gfx(sol::state& l)
{
#ifndef DL_DISABLE_RENDER
luabridge::getGlobalNamespace(L)
.beginClass<Graphics>("Graphics")
.addFunction("Rectangle2D", &Graphics::Rectangle2D)
.endClass();
l.new_usertype<Graphics>("Graphics", sol::no_constructor,
"Rectangle2D", &Graphics::Rectangle2D);
auto result = luabridge::push(L, g_gfx);
lua_setglobal(L, "g_gfx");
l["g_gfx"] = g_gfx;
#endif
}
void lua_register_ui(lua_State* L)
void lua_register_ui(sol::state& l)
{
#ifndef DL_DISABLE_RENDER
using namespace UI;
/*using namespace UI;
luabridge::getGlobalNamespace(L)
.beginClass<Panel>("UIPanel")
.addFunction("Add", (void(Panel::*)(UI::Text*))&Panel::Add)
@@ -168,130 +221,64 @@ namespace dl
.endClass();
auto result = luabridge::push(L, g_ui);
lua_setglobal(L, "g_ui");
lua_setglobal(L, "g_ui");*/
#endif
}
void lua_register_factory(lua_State* L)
void lua_register_factory(sol::state& l)
{
luabridge::getGlobalNamespace(L)
.beginClass<Factory>("Factory")
.addFunction("CreateText", &Factory::CreateText)
.addFunction("CreateTextDefault", &Factory::CreateTextDefault)
.addFunction("CreateSprite",
luabridge::overload<std::string_view, size_t>(&Factory::CreateSprite),
luabridge::overload<const SpriteID&>(&Factory::CreateSprite))
.addFunction("SetPath", &Factory::SetPath)
.endClass();
l.new_usertype<Factory>("Factory", sol::no_constructor,
"SetPath", &Factory::SetPath,
"CreateText", &Factory::CreateText,
"CreateTextDefault", &Factory::CreateTextDefault,
"CreateSprite", sol::overload(
sol::resolve<gcSprite(std::string_view, size_t)>(&Factory::CreateSprite),
sol::resolve<gcSprite(const SpriteID&)>(&Factory::CreateSprite))
);
auto result = luabridge::push(L, g_factory);
lua_setglobal(L, "g_factory");
l["g_factory"] = g_factory;
}
void lua_register_sprite(lua_State* L)
void lua_register_sprite(sol::state& l)
{
luabridge::getGlobalNamespace(L)
.beginClass<Sprite>("Sprite")
.addFunction("Render", &Sprite::Render)
.addFunction("SetColor",
luabridge::overload<const Color&>(&Sprite::SetColor),
luabridge::overload<const Color&, size_t>(&Sprite::SetColor))
.endClass();
luabridge::getGlobalNamespace(L)
.beginClass<gcSprite>("gcSprite")
.addFunction("GetPointer", &gcSprite::GetPointer)
.addIndexMetaMethod([](gcSprite& self, const luabridge::LuaRef& key, lua_State* L) -> luabridge::LuaRef
{
if (key.tostring() == "GetPointer")
{// 不清楚为何存在 GetPointer,还是会触发元函数
luabridge::LuaRef ref_class = luabridge::getGlobal(L, "gcSprite");
luabridge::LuaRef ref_func = ref_class[key];
return ref_func;
}
// 获取到原Sprite函数
luabridge::LuaRef ref_class = luabridge::getGlobal(L, "Sprite");
luabridge::LuaRef ref_func = ref_class[key];
luabridge::setGlobal(L, ref_func, "__dl_sprite_func");
// 调用代理函数
luabridge::LuaRef ref_proxy = luabridge::getGlobal(L, "__dl_sprite_proxy");
return ref_proxy;
})
.endClass();
// 注册sprite代理函数
g_lua->DoString(R"(
__dl_sprite_func = nil
__dl_sprite_proxy = function(obj, ...)
__dl_sprite_func(obj:GetPointer(), ...)
end
)");
l.new_usertype<Sprite>("Sprite",
"Render", &Sprite::Render,
"SetColor", sol::overload(
sol::resolve<void(const Color&)>(&Sprite::SetColor),
sol::resolve<void(const Color&, size_t)>(&Sprite::SetColor)
));
}
void lua_register_text(lua_State* L)
void lua_register_text(sol::state& l)
{
luabridge::getGlobalNamespace(L)
.beginClass<Text>("Text")
.addFunction("Render", &Text::Render)
.addFunction("SetString", &Text::SetString)
.endClass();
luabridge::getGlobalNamespace(L)
.beginClass<gcText>("gcText")
.addFunction("GetPointer", &gcText::GetPointer)
.addIndexMetaMethod([](gcText& self, const luabridge::LuaRef& key, lua_State* L) -> luabridge::LuaRef
{
if (key.tostring() == "GetPointer")
{// 不清楚为何存在 GetPointer,还是会触发元函数
luabridge::LuaRef ref_class = luabridge::getGlobal(L, "gcText");
luabridge::LuaRef ref_func = ref_class[key];
return ref_func;
}
// 获取到原Text函数
luabridge::LuaRef ref_class = luabridge::getGlobal(L, "Text");
luabridge::LuaRef ref_func = ref_class[key];
luabridge::setGlobal(L, ref_func, "__dl_text_func");
// 调用代理函数
luabridge::LuaRef ref_proxy = luabridge::getGlobal(L, "__dl_text_proxy");
return ref_proxy;
})
.endClass();
// 注册sprite代理函数
g_lua->DoString(R"(
__dl_text_func = nil
__dl_text_proxy = function(obj, ...)
__dl_text_func(obj:GetPointer(), ...)
end
)");
l.new_usertype<Text>("Text",
"Render", &Text::Render,
"SetString", &Text::SetString);
}
#if DL_PLATFORM == DL_PLATFORM_WIN32
#define WIN32_LEAN_AND_MEAN
#include <Windows.h>
void lua_register_win32(lua_State* L)
void lua_register_win32(sol::state& l)
{
luabridge::getGlobalNamespace(L)
.addFunction("system", &system);
l["system"] = system;
}
#endif
void lua_register_all(lua_State* L)
void lua_register_all(sol::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_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);
#if DL_PLATFORM == DL_PLATFORM_WIN32
lua_register_win32(L);
lua_register_win32(l);
#endif
}