[lua] Add Uuid type (fix #3809)

This commit is contained in:
David Capello 2023-04-12 13:51:37 -03:00
parent 7104a1a449
commit 636cce6f0d
4 changed files with 102 additions and 1 deletions

View File

@ -211,6 +211,7 @@ if(ENABLE_SCRIPTING)
script/tilesets_class.cpp
script/timer_class.cpp
script/tool_class.cpp
script/uuid_class.cpp
script/values.cpp
script/version_class.cpp
shell.cpp

View File

@ -10,6 +10,6 @@
// Increment this value if the scripting API is modified between two
// released Aseprite versions.
#define API_VERSION 22
#define API_VERSION 23
#endif

View File

@ -194,6 +194,7 @@ void register_tileset_class(lua_State* L);
void register_tilesets_class(lua_State* L);
void register_timer_class(lua_State* L);
void register_tool_class(lua_State* L);
void register_uuid_class(lua_State* L);
void register_version_class(lua_State* L);
void register_websocket_class(lua_State* L);
@ -494,6 +495,7 @@ Engine::Engine()
register_tilesets_class(L);
register_timer_class(L);
register_tool_class(L);
register_uuid_class(L);
register_version_class(L);
#if ENABLE_WEBSOCKET
register_websocket_class(L);

View File

@ -0,0 +1,98 @@
// Aseprite
// Copyright (C) 2023 Igara Studio S.A.
//
// This program is distributed under the terms of
// the End-User License Agreement for Aseprite.
#ifdef HAVE_CONFIG_H
#include "config.h"
#endif
#include "app/script/engine.h"
#include "app/script/luacpp.h"
#include "base/convert_to.h"
#include "base/uuid.h"
namespace app {
namespace script {
using Uuid = base::Uuid;
namespace {
Uuid Uuid_new(lua_State* L, int index)
{
// Copy other uuid
if (auto uuid = may_get_obj<Uuid>(L, index)) {
return *uuid;
}
else if (const char* s = lua_tostring(L, index)) {
return base::convert_to<Uuid>(std::string(s));
}
else
return Uuid::Generate();
}
int Uuid_new(lua_State* L)
{
push_obj(L, Uuid_new(L, 1));
return 1;
}
int Uuid_gc(lua_State* L)
{
get_obj<Uuid>(L, 1)->~Uuid();
return 0;
}
int Uuid_eq(lua_State* L)
{
const auto a = get_obj<Uuid>(L, 1);
const auto b = get_obj<Uuid>(L, 2);
lua_pushboolean(L, *a == *b);
return 1;
}
int Uuid_tostring(lua_State* L)
{
const auto uuid = get_obj<Uuid>(L, 1);
lua_pushstring(L, base::convert_to<std::string>(*uuid).c_str());
return 1;
}
int Uuid_index(lua_State* L)
{
const auto uuid = get_obj<Uuid>(L, 1);
const int i = lua_tointeger(L, 2);
if (i >= 1 && i <= 16)
lua_pushinteger(L, (*uuid)[i-1]);
else
lua_pushnil(L);
return 1;
}
const luaL_Reg Uuid_methods[] = {
{ "__gc", Uuid_gc },
{ "__eq", Uuid_eq },
{ "__tostring", Uuid_tostring },
{ "__index", Uuid_index },
{ nullptr, nullptr }
};
} // anonymous namespace
DEF_MTNAME(Uuid);
void register_uuid_class(lua_State* L)
{
REG_CLASS(L, Uuid);
REG_CLASS_NEW(L, Uuid);
}
base::Uuid convert_args_into_uuid(lua_State* L, int index)
{
return Uuid_new(L, index);
}
} // namespace script
} // namespace app