From b25c41ab094ebaa903c3e6b3978a440fbf4c4824 Mon Sep 17 00:00:00 2001 From: Ryan Lucia Date: Thu, 8 Oct 2026 18:03:00 -0700 Subject: [PATCH] Expose the Aegisub version to Lua as aegisub.version Scripts have so far only been able to check lua_automation_version, which hasn't changed in years, so there's no way for them to check for changes that can't be detected by looking for a function, such as new optional arguments. aegisub.version is a table with: - string: the full version string, for display and logging - major, minor, patch: the version declared in meson.build, i.e. that of the latest release this was built from or of the release being prepared, so that a development build made after 3.5.0 reports 3.5.0 - build: the build number, which increases with every commit to master, or nil if unknown (when built from a shallow clone) Co-Authored-By: Claude Opus 5.5 --- automation/v4-docs/misc.txt | 33 +++++++++++++++++++++++++++++++++ meson.build | 7 +++++++ src/auto4_lua.cpp | 19 ++++++++++++++++++- src/version.cpp | 4 ++++ src/version.h | 5 +++++ 5 files changed, 67 insertions(+), 1 deletion(-) diff --git a/automation/v4-docs/misc.txt b/automation/v4-docs/misc.txt index 62758dc2be..a3cce2590d 100644 --- a/automation/v4-docs/misc.txt +++ b/automation/v4-docs/misc.txt @@ -31,6 +31,39 @@ Returns: 4 values, all numbers. --- +Getting the Aegisub version + +aegisub.version (table) + +A table describing the version of Aegisub the script is running in, with +these fields: + +@string (string) + The full version string, e.g. "3.5.0" for a release or + "9900-master-4e440a614" for a development build. Use it for display and + logging only; its format may change. + +@major, @minor, @patch (numbers) + The version numbers of the latest release this version of Aegisub was built + from (or of the release being prepared). A development build made after + 3.5.0 reports 3.5.0, so it is safe to use these to check that the features + of a given release are available. + +@build (number or nil) + The build number, which increases with every commit to Aegisub's main + branch. It can be used to check for features which were added after the + latest release, but is only meaningful for official builds: other branches + and forks may number their builds differently. nil if unknown. + +Wherever possible, check for the specific feature you need instead, e.g. +"if aegisub.some_function then". The version is mainly useful for changes that +can't be detected that way, such as new optional arguments. + +aegisub.version was added after 3.5.0; scripts that need to run in older +versions should check that it exists first. + +--- + Getting the audio waveform selection position and duration function aegisub.get_audio_selection() diff --git a/meson.build b/meson.build index 23ff8968aa..58ead12681 100644 --- a/meson.build +++ b/meson.build @@ -96,6 +96,13 @@ if get_option('debug') and host_machine.system() != 'windows' endif conf = configuration_data() +# The version declared above is that of the latest release this was built +# from, or of the release being prepared +aegisub_version = meson.project_version().split('.') +assert(aegisub_version.length() == 3, 'The project version must be of the form X.Y.Z') +conf.set('AEGISUB_VERSION_MAJOR', aegisub_version[0].to_int()) +conf.set('AEGISUB_VERSION_MINOR', aegisub_version[1].to_int()) +conf.set('AEGISUB_VERSION_PATCH', aegisub_version[2].to_int()) conf.set_quoted('P_DATA', dataroot) conf.set_quoted('P_LOCALE', localedir) if get_option('credit') != '' diff --git a/src/auto4_lua.cpp b/src/auto4_lua.cpp index c2680b7b98..e8a4f45da6 100644 --- a/src/auto4_lua.cpp +++ b/src/auto4_lua.cpp @@ -53,6 +53,7 @@ #include "subs_controller.h" #include "video_controller.h" #include "utils.h" +#include "version.h" #include #include @@ -115,6 +116,20 @@ namespace { return 1; } + /// Push the table for aegisub.version + void push_version(lua_State *L) + { + lua_createtable(L, 0, 5); + set_field(L, "string", GetVersionNumber()); + auto numbers = GetVersionNumbers(); + set_field(L, "major", numbers[0]); + set_field(L, "minor", numbers[1]); + set_field(L, "patch", numbers[2]); + // Unknown when built from a shallow clone + if (int build = GetSVNRevision()) + set_field(L, "build", build); + } + int get_translation(lua_State *L) { wxString str(check_wxstring(L, 1)); @@ -504,7 +519,7 @@ namespace { // make "aegisub" table lua_pushstring(L, "aegisub"); - lua_createtable(L, 0, 13); + lua_createtable(L, 0, 14); set_field(L, "register_macro"); set_field(L, "register_filter"); @@ -516,6 +531,8 @@ namespace { set_field(L, "decode_path"); set_field(L, "cancel"); set_field(L, "lua_automation_version", 4); + push_version(L); + lua_setfield(L, -2, "version"); set_field(L, "__init_clipboard"); set_field(L, "__raise_warning"); set_field(L, "file_name"); diff --git a/src/version.cpp b/src/version.cpp index 3af933770e..1955824bea 100644 --- a/src/version.cpp +++ b/src/version.cpp @@ -74,6 +74,10 @@ bool GetIsOfficialRelease() { #endif } +std::array GetVersionNumbers() { + return {AEGISUB_VERSION_MAJOR, AEGISUB_VERSION_MINOR, AEGISUB_VERSION_PATCH}; +} + const char *GetVersionNumber() { return BUILD_GIT_VERSION_STRING; } diff --git a/src/version.h b/src/version.h index 4253db2123..0b2581f39e 100644 --- a/src/version.h +++ b/src/version.h @@ -34,6 +34,8 @@ #pragma once +#include + /// Version string appended in title bar of main window (quick identification of all elements of a build) const char *GetAegisubLongVersionString(); /// Version string used in About box, looks nicer @@ -44,6 +46,9 @@ const char *GetAegisubBuildTime(); const char *GetAegisubBuildCredit(); /// Is release? bool GetIsOfficialRelease(); +/// The version numbers declared in meson.build: those of the latest release +/// this was built from, or of the release being prepared +std::array GetVersionNumbers(); /// Version number const char *GetVersionNumber(); /// Get SVN revision