diff --git a/laf b/laf index 310350422..c556ff980 160000 --- a/laf +++ b/laf @@ -1 +1 @@ -Subproject commit 310350422e483c4b401d7eb9f1d7a647825149aa +Subproject commit c556ff98009d06041e2ab99eeb9464233f137725 diff --git a/src/app/CMakeLists.txt b/src/app/CMakeLists.txt index e6d7b71f6..0db978e88 100644 --- a/src/app/CMakeLists.txt +++ b/src/app/CMakeLists.txt @@ -161,6 +161,7 @@ if(ENABLE_SCRIPTING) commands/cmd_run_script.cpp script/app_command_object.cpp script/app_fs_object.cpp + script/app_os_object.cpp script/app_object.cpp script/app_theme_object.cpp script/brush_class.cpp diff --git a/src/app/script/api_version.h b/src/app/script/api_version.h index 2f049018a..a9684b278 100644 --- a/src/app/script/api_version.h +++ b/src/app/script/api_version.h @@ -10,6 +10,6 @@ // Increment this value if the scripting API is modified between two // released Aseprite versions. -#define API_VERSION 27 +#define API_VERSION 28 #endif diff --git a/src/app/script/app_os_object.cpp b/src/app/script/app_os_object.cpp new file mode 100644 index 000000000..33776c50e --- /dev/null +++ b/src/app/script/app_os_object.cpp @@ -0,0 +1,121 @@ +// Aseprite +// Copyright (C) 2024 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/config.h" +#include "base/platform.h" +#include "updater/user_agent.h" + +namespace app { +namespace script { + +namespace { + +struct AppOS { }; + +int AppOS_get_name(lua_State* L) +{ +#if LAF_WINDOWS + lua_pushstring(L, "Windows"); +#elif LAF_MACOS + lua_pushstring(L, "macOS"); +#elif LAF_LINUX + lua_pushstring(L, "Linux"); +#else + lua_pushnil(L); +#endif + return 1; +} + +int AppOS_get_version(lua_State* L) +{ + base::Platform p = base::get_platform(); + push_version(L, p.osVer); + return 1; +} + +int AppOS_get_fullName(lua_State* L) +{ + lua_pushstring(L, updater::getFullOSString().c_str()); + return 1; +} + +int AppOS_get_windows(lua_State* L) +{ + lua_pushboolean(L, base::Platform::os == base::Platform::OS::Windows); + return 1; +} + +int AppOS_get_macos(lua_State* L) +{ + lua_pushboolean(L, base::Platform::os == base::Platform::OS::macOS); + return 1; +} + +int AppOS_get_linux(lua_State* L) +{ + lua_pushboolean(L, base::Platform::os == base::Platform::OS::Linux); + return 1; +} + +int AppOS_get_x64(lua_State* L) +{ + lua_pushboolean(L, base::Platform::arch == base::Platform::Arch::x64); + return 1; +} + +int AppOS_get_x86(lua_State* L) +{ + lua_pushboolean(L, base::Platform::arch == base::Platform::Arch::x86); + return 1; +} + +int AppOS_get_arm64(lua_State* L) +{ + lua_pushboolean(L, base::Platform::arch == base::Platform::Arch::arm64); + return 1; +} + +const Property AppOS_properties[] = { + { "name", AppOS_get_name, nullptr }, + { "version", AppOS_get_version, nullptr }, + { "fullName", AppOS_get_fullName, nullptr }, + { "windows", AppOS_get_windows, nullptr }, + { "macos", AppOS_get_macos, nullptr }, + { "linux", AppOS_get_linux, nullptr }, + { "x64", AppOS_get_x64, nullptr }, + { "x86", AppOS_get_x86, nullptr }, + { "arm64", AppOS_get_arm64, nullptr }, + { nullptr, nullptr, nullptr } +}; + +const luaL_Reg AppOS_methods[] = { + { nullptr, nullptr } +}; + +} // anonymous namespace + +DEF_MTNAME(AppOS); + +void register_app_os_object(lua_State* L) +{ + REG_CLASS(L, AppOS); + REG_CLASS_PROPERTIES(L, AppOS); + + lua_getglobal(L, "app"); + lua_pushstring(L, "os"); + push_new(L); + lua_rawset(L, -3); + lua_pop(L, 1); +} + +} // namespace script +} // namespace app diff --git a/src/app/script/engine.cpp b/src/app/script/engine.cpp index abb002918..bf2814c73 100644 --- a/src/app/script/engine.cpp +++ b/src/app/script/engine.cpp @@ -165,6 +165,7 @@ int os_clock(lua_State* L) void register_app_object(lua_State* L); void register_app_pixel_color_object(lua_State* L); void register_app_fs_object(lua_State* L); +void register_app_os_object(lua_State* L); void register_app_command_object(lua_State* L); void register_app_preferences_object(lua_State* L); void register_json_object(lua_State* L); @@ -259,6 +260,7 @@ Engine::Engine() register_app_object(L); register_app_pixel_color_object(L); register_app_fs_object(L); + register_app_os_object(L); register_app_command_object(L); register_app_preferences_object(L); register_json_object(L); diff --git a/src/updater/user_agent.cpp b/src/updater/user_agent.cpp index 06046d493..60c191c85 100644 --- a/src/updater/user_agent.cpp +++ b/src/updater/user_agent.cpp @@ -19,45 +19,42 @@ namespace updater { -std::string getUserAgent() +std::string getFullOSString() { base::Platform p = base::get_platform(); - std::stringstream userAgent; - - // App name and version - userAgent << get_app_name() << "/" << get_app_version() << " ("; + std::stringstream os; #if LAF_WINDOWS // ---------------------------------------------------------------------- // Windows - userAgent << "Windows"; + os << "Windows"; switch (p.windowsType) { case base::Platform::WindowsType::Server: - userAgent << " Server"; + os << " Server"; break; case base::Platform::WindowsType::NT: - userAgent << " NT"; + os << " NT"; break; } - userAgent << " " << p.osVer.str(); + os << " " << p.osVer.str(); if (p.servicePack.major() > 0) - userAgent << " SP" << p.servicePack.major(); + os << " SP" << p.servicePack.major(); if (p.isWow64) - userAgent << "; WOW64"; + os << "; WOW64"; if (p.wineVer) - userAgent << "; Wine " << p.wineVer; + os << "; Wine " << p.wineVer; #elif LAF_MACOS - userAgent << "macOS " - << p.osVer.major() << "." - << p.osVer.minor() << "." - << p.osVer.patch(); + os << "macOS " + << p.osVer.major() << "." + << p.osVer.minor() << "." + << p.osVer.patch(); #else @@ -65,14 +62,23 @@ std::string getUserAgent() // Unix like if (!p.distroName.empty()) { - userAgent << p.distroName; + os << p.distroName; if (!p.distroVer.empty()) - userAgent << " " << p.distroVer; + os << " " << p.distroVer; } #endif - userAgent << ")"; + return os.str(); +} + +std::string getUserAgent() +{ + std::stringstream userAgent; + + // App name and version + userAgent << get_app_name() << "/" << get_app_version() + << " (" << getFullOSString() << ")"; return userAgent.str(); } diff --git a/src/updater/user_agent.h b/src/updater/user_agent.h index 0ba55b1bb..7d3156131 100644 --- a/src/updater/user_agent.h +++ b/src/updater/user_agent.h @@ -1,4 +1,5 @@ // Aseprite +// Copyright (C) 2024 Igara Studio S.A. // Copyright (C) 2001-2015 David Capello // // This program is distributed under the terms of @@ -12,6 +13,7 @@ namespace updater { + std::string getFullOSString(); std::string getUserAgent(); } // namespace updater diff --git a/tests/scripts/app_fs.lua b/tests/scripts/app_fs.lua index e3e38949c..d67e0c894 100644 --- a/tests/scripts/app_fs.lua +++ b/tests/scripts/app_fs.lua @@ -8,21 +8,29 @@ local sep = fs.pathSeparator assert('' == fs.filePath('first.png')) assert('path' == fs.filePath('path/second.png')) -assert('C:\\path' == fs.filePath('C:\\path\\third.png')) +if app.os.windows then + assert('C:\\path' == fs.filePath('C:\\path\\third.png')) +end assert('first.png' == fs.fileName('first.png')) assert('second.png' == fs.fileName('path/second.png')) -assert('third.png' == fs.fileName('C:\\path\\third.png')) +if app.os.windows then + assert('third.png' == fs.fileName('C:\\path\\third.png')) +end assert('png' == fs.fileExtension('path/file.png')) assert('first' == fs.fileTitle('first.png')) assert('second' == fs.fileTitle('path/second.png')) -assert('third' == fs.fileTitle('C:\\path\\third.png')) +if app.os.windows then + assert('third' == fs.fileTitle('C:\\path\\third.png')) +end assert('first' == fs.filePathAndTitle('first.png')) assert('path/second' == fs.filePathAndTitle('path/second.png')) -assert('C:\\path\\third' == fs.filePathAndTitle('C:\\path\\third.png')) +if app.os.windows then + assert('C:\\path\\third' == fs.filePathAndTitle('C:\\path\\third.png')) +end assert('hi/bye' == fs.joinPath('hi/', 'bye')) assert('hi/bye' .. sep .. 'smth.png' == fs.joinPath('hi/', 'bye', 'smth.png'))