From 2176954aeef668068df51ac8850c0425c8d1d44c Mon Sep 17 00:00:00 2001 From: arch1t3cht Date: Tue, 26 Jul 2022 21:26:25 +0200 Subject: [PATCH] Folding: Pass fold data through Lua This way, automation scripts won't nuke the fold data on any lines they modify. This is done as userdata for now, so scripts that don't know what they're doing don't break the folds even further. If scripts get more access to folds, it should be with a safer and simpler API. --- src/auto4_lua_assfile.cpp | 16 ++++++++++++++++ 1 file changed, 16 insertions(+) diff --git a/src/auto4_lua_assfile.cpp b/src/auto4_lua_assfile.cpp index 3aef3b769..f98e0a35d 100644 --- a/src/auto4_lua_assfile.cpp +++ b/src/auto4_lua_assfile.cpp @@ -40,6 +40,7 @@ #include "ass_karaoke.h" #include "ass_style.h" #include "compat.h" +#include "fold_controller.h" #include #include @@ -100,6 +101,16 @@ namespace { return ret; } + template + void get_userdata_field(lua_State *L, const char *name, const char *line_class, T *target) + { + lua_getfield(L, -1, name); + if (!lua_isuserdata(L, -1)) + throw bad_field("userdata", name, line_class); + *target = *static_cast(lua_touserdata(L, -1)); + lua_pop(L, 1); + } + using namespace Automation4; template int closure_wrapper(lua_State *L) @@ -181,6 +192,10 @@ namespace Automation4 { set_field(L, "text", dia->Text); + // preserve the folds + *static_cast(lua_newuserdata(L, sizeof(FoldInfo))) = dia->Fold; + lua_setfield(L, -2, "_foldinfo"); + // create extradata table lua_newtable(L); for (auto const& ed : ass->GetExtradata(dia->ExtradataIds)) { @@ -301,6 +316,7 @@ namespace Automation4 { dia->Margin[2] = get_int_field(L, "margin_t", "dialogue"); dia->Effect = get_string_field(L, "effect", "dialogue"); dia->Text = get_string_field(L, "text", "dialogue"); + get_userdata_field(L, "_foldinfo", "dialogue", &dia->Fold); std::vector new_ids;