289 lines
7.4 KiB
C++
289 lines
7.4 KiB
C++
#include "dl_lua_register.h"
|
|
|
|
|
|
#include "base/dl_type_def.h"
|
|
#include "render/dl_image.h"
|
|
#include "base/dl_string.h"
|
|
#include "system/dl_system.h"
|
|
#include "render/dl_graphics.h"
|
|
|
|
#include "tool/dl_tool_archiver.h"
|
|
|
|
|
|
|
|
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 dl::to_string(pos);
|
|
}
|
|
|
|
std::string lua_to_string_hex_16(const std::array<std::byte, 16>& arr, bool add_tag)
|
|
{
|
|
return dl::to_string_hex(arr, add_tag);
|
|
}
|
|
|
|
|
|
void lua_register_base(sol::state& l)
|
|
{
|
|
l["log"]= lua_log;
|
|
l["delete"]= lua_delete;
|
|
l["to_string"] = lua_to_string;
|
|
//l["to_string_hex"] = lua_to_string_hex_16;
|
|
/*l["to_string_hex"] = sol::overload(
|
|
sol::resolve<BufferView, bool>(dl::to_string_hex),
|
|
sol::resolve<const std::array<std::byte, 16>&, bool>(lua_to_string_hex_16));*/
|
|
|
|
|
|
l.new_usertype<Position>("Position",
|
|
sol::call_constructor,
|
|
sol::factories([](float x, float y) {
|
|
return Position{ x, y };
|
|
}),
|
|
"x", &Position::x,
|
|
"y", &Position::y);
|
|
|
|
{
|
|
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;
|
|
}
|
|
|
|
l.new_enum("ColorDef",
|
|
"NONE", ColorDef::NONE,
|
|
"RED", ColorDef::RED);
|
|
}
|
|
|
|
void lua_register_system(sol::state& l)
|
|
{
|
|
#ifndef DL_DISABLE_RENDER
|
|
l.new_usertype<ParamWindow>("ParamWindow",
|
|
"title", &ParamWindow::_strTitle);
|
|
|
|
l.new_usertype<System>("System", sol::no_constructor,
|
|
"SetFuncInit", &System::lua_SetFuncInit,
|
|
"SetFuncRun", &System::lua_SetFuncRun,
|
|
"Start", sol::resolve<void(const ParamWindow&)>(&System::Start));
|
|
|
|
l["g_system"] = g_system;
|
|
#endif
|
|
}
|
|
|
|
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);
|
|
}
|
|
|
|
{
|
|
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["GetBuffer"] = sol::resolve<std::string_view>(File::GetBuffer);
|
|
ll["GetString"] = File::GetString;
|
|
ll["SaveString"] = sol::resolve<std::string_view, std::string_view>(File::SaveString);
|
|
ll["SaveBuffer"] = File::SaveBuffer;
|
|
ll["IsExist"] = File::IsExist;
|
|
}
|
|
}
|
|
|
|
void lua_register_gfx(sol::state& l)
|
|
{
|
|
#ifndef DL_DISABLE_RENDER
|
|
l.new_usertype<Graphics>("Graphics", sol::no_constructor,
|
|
"Rectangle2D", &Graphics::Rectangle2D);
|
|
|
|
l["g_gfx"] = g_gfx;
|
|
#endif
|
|
}
|
|
|
|
void lua_register_ui(sol::state& l)
|
|
{
|
|
#ifndef DL_DISABLE_RENDER
|
|
/*using namespace UI;
|
|
luabridge::getGlobalNamespace(L)
|
|
.beginClass<Panel>("UIPanel")
|
|
.addFunction("Add", (void(Panel::*)(UI::Text*))&Panel::Add)
|
|
.endClass();
|
|
|
|
luabridge::getGlobalNamespace(L)
|
|
.beginClass<UI::Text>("UIText")
|
|
.addFunction("SetString", &UI::Text::SetString)
|
|
.addFunction("SetLang", &UI::Text::SetLang)
|
|
.endClass();
|
|
|
|
luabridge::getGlobalNamespace(L)
|
|
.beginClass<Manager>("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
|
|
}
|
|
|
|
|
|
BufferView lua_to_view(const Buffer& buf)
|
|
{
|
|
return CreateBufferView(buf);
|
|
}
|
|
|
|
void lua_register_memory(sol::state& l)
|
|
{
|
|
/*l.new_usertype<BufferView>("BufferView",
|
|
sol::constructors<BufferView(), BufferView(const Buffer& buf)>());*/
|
|
|
|
l["toView"] = lua_to_view;
|
|
l["CreateBufferHex"] = CreateBufferHex;
|
|
l["CreateArrayHex12"] = CreateArrayHex<12>;
|
|
l["CreateArrayHex14"] = CreateArrayHex<14>;
|
|
l["CreateArrayHex16"] = CreateArrayHex<16>;
|
|
l["CreateArrayHex32"] = CreateArrayHex<32>;
|
|
|
|
{
|
|
sol::table ll = l["Memory"].get_or_create<sol::table>();
|
|
ll.new_enum("CompressMode",
|
|
"NONE", Memory::CompressMode::E_NONE,
|
|
"ZIP", Memory::CompressMode::E_ZIP,
|
|
"LZ4", Memory::CompressMode::E_LZ4);
|
|
}
|
|
{
|
|
sol::table ll = l["Crypto"].get_or_create<sol::table>();
|
|
ll["HashCrc32"] = Crypto::HashCrc32;
|
|
ll["HashMd5"] = Crypto::HashMd5;
|
|
ll["HashSha256"] = Crypto::HashSha256;
|
|
ll["HashSha512"] = Crypto::HashSha512;
|
|
ll["HashBlake2b"] = Crypto::HashBlake2b;
|
|
|
|
|
|
ll.new_usertype<Crypto::Aes256Key>("Aes256Key",
|
|
"_key", &Crypto::Aes256Key::_key,
|
|
"_iv", &Crypto::Aes256Key::_iv,
|
|
"_tag", &Crypto::Aes256Key::_tag);
|
|
|
|
ll["EncryptAes256"] = Crypto::EncryptAes256;
|
|
ll["DecryptAes256"] = Crypto::DecryptAes256;
|
|
ll["GenerateKeyAes256"] = Crypto::GenerateKeyAes256;
|
|
}
|
|
}
|
|
|
|
void lua_register_tool(sol::state& l)
|
|
{
|
|
l.new_usertype<Tool::Archiver::Param>("ToolArchiverParam",
|
|
"_src", &Tool::Archiver::Param::_src,
|
|
"_out", &Tool::Archiver::Param::_out,
|
|
"_compressMode", &Tool::Archiver::Param::_compressMode,
|
|
"_bCrc", &Tool::Archiver::Param::_bCrc);
|
|
|
|
{
|
|
sol::table ll = l["ToolArchiver"].get_or_create<sol::table>();
|
|
ll["Compress"] = Tool::Archiver::Compress;
|
|
ll["Decompress"] = sol::resolve<std::string_view, std::string_view>(
|
|
Tool::Archiver::Decompress);
|
|
}
|
|
}
|
|
|
|
#if DL_PLATFORM == DL_PLATFORM_WIN32
|
|
#define WIN32_LEAN_AND_MEAN
|
|
#include <Windows.h>
|
|
void lua_register_win32(sol::state& l)
|
|
{
|
|
l["system"] = system;
|
|
}
|
|
|
|
#endif
|
|
|
|
|
|
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_sound(l);
|
|
lua_register_memory(l);
|
|
lua_register_tool(l);
|
|
lua_register_json(l);
|
|
|
|
#if DL_PLATFORM == DL_PLATFORM_WIN32
|
|
lua_register_win32(l);
|
|
#endif
|
|
|
|
// C++侧不好实现的函数
|
|
g_lua->DoString(R"(
|
|
to_string_hex = function(value, is_upper, add_tag)
|
|
if value == nil then
|
|
return ""
|
|
end
|
|
|
|
local str = add_tag and "0x" or ""
|
|
if type(value) ~= "userdata" then
|
|
local s = string.format("%x", value)
|
|
if is_upper then
|
|
s = string.upper(s)
|
|
end
|
|
return str .. s
|
|
end
|
|
for i = 1, #value do
|
|
local c = value[i]
|
|
local s = string.format("%02x", c)
|
|
if is_upper then
|
|
s = string.upper(s)
|
|
end
|
|
str = str .. s
|
|
end
|
|
return str
|
|
end
|
|
)");
|
|
}
|
|
|
|
} |