2006-12-28 22:18:35 +01:00
|
|
|
// Copyright (c) 2006, Niels Martin Hansen
|
|
|
|
// All rights reserved.
|
|
|
|
//
|
|
|
|
// Redistribution and use in source and binary forms, with or without
|
|
|
|
// modification, are permitted provided that the following conditions are met:
|
|
|
|
//
|
|
|
|
// * Redistributions of source code must retain the above copyright notice,
|
|
|
|
// this list of conditions and the following disclaimer.
|
|
|
|
// * Redistributions in binary form must reproduce the above copyright notice,
|
|
|
|
// this list of conditions and the following disclaimer in the documentation
|
|
|
|
// and/or other materials provided with the distribution.
|
|
|
|
// * Neither the name of the Aegisub Group nor the names of its contributors
|
|
|
|
// may be used to endorse or promote products derived from this software
|
|
|
|
// without specific prior written permission.
|
|
|
|
//
|
|
|
|
// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
|
|
|
|
// AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
|
|
|
|
// IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
|
|
|
|
// ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
|
|
|
|
// LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
|
|
|
|
// CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
|
|
|
|
// SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
|
|
|
|
// INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
|
|
|
|
// CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
|
|
|
|
// ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
|
|
|
|
// POSSIBILITY OF SUCH DAMAGE.
|
|
|
|
//
|
2009-07-29 07:43:02 +02:00
|
|
|
// Aegisub Project http://www.aegisub.org/
|
|
|
|
|
|
|
|
/// @file auto4_lua_assfile.cpp
|
|
|
|
/// @brief Lua 5.1-based scripting engine (interface to subtitle files)
|
|
|
|
/// @ingroup scripting
|
|
|
|
///
|
2006-12-28 22:18:35 +01:00
|
|
|
|
2013-01-04 16:01:50 +01:00
|
|
|
#include "auto4_lua.h"
|
2010-06-09 01:21:39 +02:00
|
|
|
|
2006-12-28 22:18:35 +01:00
|
|
|
#include "ass_dialogue.h"
|
2012-12-08 03:51:09 +01:00
|
|
|
#include "ass_info.h"
|
2006-12-28 22:18:35 +01:00
|
|
|
#include "ass_file.h"
|
2011-09-28 21:46:23 +02:00
|
|
|
#include "ass_karaoke.h"
|
2009-09-10 15:06:40 +02:00
|
|
|
#include "ass_style.h"
|
2014-05-23 00:40:16 +02:00
|
|
|
#include "compat.h"
|
2008-03-05 05:10:20 +01:00
|
|
|
|
2013-01-04 16:01:50 +01:00
|
|
|
#include <libaegisub/exception.h>
|
2014-04-26 00:40:43 +02:00
|
|
|
#include <libaegisub/log.h>
|
2014-03-27 03:24:37 +01:00
|
|
|
#include <libaegisub/lua/utils.h>
|
2014-04-26 00:40:43 +02:00
|
|
|
#include <libaegisub/make_unique.h>
|
2013-01-04 16:01:50 +01:00
|
|
|
|
|
|
|
#include <algorithm>
|
|
|
|
#include <boost/algorithm/string/case_conv.hpp>
|
|
|
|
#include <cassert>
|
2013-06-10 15:58:13 +02:00
|
|
|
#include <memory>
|
2013-01-04 16:01:50 +01:00
|
|
|
|
2011-09-28 21:47:03 +02:00
|
|
|
namespace {
|
2014-03-27 03:24:37 +01:00
|
|
|
using namespace agi::lua;
|
|
|
|
|
2014-05-29 14:57:27 +02:00
|
|
|
DEFINE_EXCEPTION(BadField, Automation4::MacroRunError);
|
2011-09-28 21:47:03 +02:00
|
|
|
BadField bad_field(const char *expected_type, const char *name, const char *line_clasee)
|
|
|
|
{
|
2011-12-22 22:21:38 +01:00
|
|
|
return BadField(std::string("Invalid or missing field '") + name + "' in '" + line_clasee + "' class subtitle line (expected " + expected_type + ")");
|
2011-09-28 21:47:03 +02:00
|
|
|
}
|
|
|
|
|
2012-10-26 16:09:14 +02:00
|
|
|
std::string get_string_field(lua_State *L, const char *name, const char *line_class)
|
|
|
|
{
|
|
|
|
lua_getfield(L, -1, name);
|
|
|
|
if (!lua_isstring(L, -1))
|
|
|
|
throw bad_field("string", name, line_class);
|
|
|
|
std::string ret(lua_tostring(L, -1));
|
|
|
|
lua_pop(L, 1);
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
2011-09-28 21:47:03 +02:00
|
|
|
double get_double_field(lua_State *L, const char *name, const char *line_class)
|
|
|
|
{
|
|
|
|
lua_getfield(L, -1, name);
|
|
|
|
if (!lua_isnumber(L, -1))
|
|
|
|
throw bad_field("number", name, line_class);
|
|
|
|
double ret = lua_tonumber(L, -1);
|
|
|
|
lua_pop(L, 1);
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
|
|
|
int get_int_field(lua_State *L, const char *name, const char *line_class)
|
|
|
|
{
|
|
|
|
lua_getfield(L, -1, name);
|
|
|
|
if (!lua_isnumber(L, -1))
|
|
|
|
throw bad_field("number", name, line_class);
|
|
|
|
int ret = lua_tointeger(L, -1);
|
|
|
|
lua_pop(L, 1);
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool get_bool_field(lua_State *L, const char *name, const char *line_class)
|
|
|
|
{
|
|
|
|
lua_getfield(L, -1, name);
|
|
|
|
if (!lua_isboolean(L, -1))
|
|
|
|
throw bad_field("boolean", name, line_class);
|
|
|
|
bool ret = !!lua_toboolean(L, -1);
|
|
|
|
lua_pop(L, 1);
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
|
|
|
using namespace Automation4;
|
|
|
|
template<int (LuaAssFile::*closure)(lua_State *)>
|
|
|
|
int closure_wrapper(lua_State *L)
|
|
|
|
{
|
2014-03-20 21:58:58 +01:00
|
|
|
return (LuaAssFile::GetObjPointer(L, lua_upvalueindex(1), false)->*closure)(L);
|
2011-09-28 21:47:03 +02:00
|
|
|
}
|
|
|
|
|
2014-03-20 21:58:58 +01:00
|
|
|
template<void (LuaAssFile::*closure)(lua_State *), bool allow_expired>
|
2011-09-28 21:47:03 +02:00
|
|
|
int closure_wrapper_v(lua_State *L)
|
|
|
|
{
|
2014-03-20 21:58:58 +01:00
|
|
|
(LuaAssFile::GetObjPointer(L, lua_upvalueindex(1), allow_expired)->*closure)(L);
|
2011-09-28 21:47:03 +02:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2011-09-28 21:47:12 +02:00
|
|
|
int modification_mask(AssEntry *e)
|
|
|
|
{
|
2014-03-09 04:14:22 +01:00
|
|
|
if (!e) return AssFile::COMMIT_SCRIPTINFO;
|
|
|
|
switch (e->Group()) {
|
2013-06-13 00:54:19 +02:00
|
|
|
case AssEntryGroup::DIALOGUE: return AssFile::COMMIT_DIAG_ADDREM;
|
|
|
|
case AssEntryGroup::STYLE: return AssFile::COMMIT_STYLES;
|
2014-03-07 18:02:24 +01:00
|
|
|
default: return AssFile::COMMIT_SCRIPTINFO;
|
2011-09-28 21:47:03 +02:00
|
|
|
}
|
2011-09-28 21:47:12 +02:00
|
|
|
}
|
2014-12-26 17:51:14 +01:00
|
|
|
|
|
|
|
template<typename T, typename U>
|
2015-01-14 00:20:55 +01:00
|
|
|
const T *check_cast_constptr(const U *value) {
|
|
|
|
return typeid(const T) == typeid(*value) ? static_cast<const T *>(value) : nullptr;
|
2014-12-26 17:51:14 +01:00
|
|
|
}
|
2011-09-28 21:47:12 +02:00
|
|
|
}
|
2011-09-28 21:47:03 +02:00
|
|
|
|
|
|
|
namespace Automation4 {
|
2013-10-27 15:15:39 +01:00
|
|
|
LuaAssFile::~LuaAssFile() { }
|
|
|
|
|
2011-09-28 21:47:03 +02:00
|
|
|
void LuaAssFile::CheckAllowModify()
|
|
|
|
{
|
|
|
|
if (!can_modify)
|
2014-04-27 16:20:11 +02:00
|
|
|
error(L, "Attempt to modify subtitles in read-only feature context.");
|
2011-09-28 21:47:03 +02:00
|
|
|
}
|
Note: This was done using a script! it's far from perfect but 95% of the work has been done already formatting-wise.
Document all functions, class, struct, union, enum, macro, variable, typedefs. This isn't the actual document in itself but empty documentation using any old documentation if it was there.
This was done using exuberant ctags to get tag info, then a TCL script to parse/remove old comments and convert them into Doxygen-style.
Some notes:
* Anything labeled 'DOCME' needs to be documented, @param and @return have been left blank as it would be annoying to delete the 'DOCME' from every one of those.
* Some multiline comments may have been munged into single line comments
* Leave the /// comments above global variables with a space, if they're harder to read then we'll be less likey to use them.
* Enum comments can go after the enumeration itself '[value] /// comment'
* include/aegisub/*.h haven't been converted yet, this will be done in a later commit
* Some documentation blocks are in the wrong place, in the .h when it should be in the .cpp, or vice versa.
See http://devel.aegisub.org/wiki/Doxygen for some details on Doxygen and a 'style guide'.
Originally committed to SVN as r3312.
2009-07-30 00:59:22 +02:00
|
|
|
|
2012-10-12 23:06:41 +02:00
|
|
|
void LuaAssFile::CheckBounds(int idx)
|
|
|
|
{
|
|
|
|
if (idx <= 0 || idx > (int)lines.size())
|
2014-04-27 16:20:11 +02:00
|
|
|
error(L, "Requested out-of-range line from subtitle file: %d", idx);
|
2012-10-12 23:06:41 +02:00
|
|
|
}
|
|
|
|
|
2014-03-09 04:14:22 +01:00
|
|
|
void LuaAssFile::AssEntryToLua(lua_State *L, size_t idx)
|
2006-12-28 22:18:35 +01:00
|
|
|
{
|
|
|
|
lua_newtable(L);
|
|
|
|
|
2014-03-09 04:14:22 +01:00
|
|
|
const AssEntry *e = lines[idx];
|
|
|
|
if (!e)
|
|
|
|
e = &ass->Info[idx];
|
|
|
|
|
2012-11-25 01:08:29 +01:00
|
|
|
set_field(L, "section", e->GroupHeader());
|
2006-12-28 22:18:35 +01:00
|
|
|
|
2015-01-14 00:20:55 +01:00
|
|
|
if (auto info = check_cast_constptr<AssInfo>(e)) {
|
2014-03-08 02:02:32 +01:00
|
|
|
set_field(L, "raw", info->GetEntryData());
|
2012-12-08 03:51:09 +01:00
|
|
|
set_field(L, "key", info->Key());
|
|
|
|
set_field(L, "value", info->Value());
|
2011-09-28 21:47:03 +02:00
|
|
|
set_field(L, "class", "info");
|
|
|
|
}
|
2015-01-14 00:20:55 +01:00
|
|
|
else if (auto dia = check_cast_constptr<AssDialogue>(e)) {
|
2014-03-08 02:02:32 +01:00
|
|
|
set_field(L, "raw", dia->GetEntryData());
|
2011-09-28 21:47:03 +02:00
|
|
|
set_field(L, "comment", dia->Comment);
|
2006-12-28 22:18:35 +01:00
|
|
|
|
2011-09-28 21:47:03 +02:00
|
|
|
set_field(L, "layer", dia->Layer);
|
2006-12-28 22:18:35 +01:00
|
|
|
|
2011-12-22 22:28:51 +01:00
|
|
|
set_field(L, "start_time", dia->Start);
|
|
|
|
set_field(L, "end_time", dia->End);
|
2006-12-28 22:18:35 +01:00
|
|
|
|
2011-09-28 21:47:03 +02:00
|
|
|
set_field(L, "style", dia->Style);
|
|
|
|
set_field(L, "actor", dia->Actor);
|
|
|
|
set_field(L, "effect", dia->Effect);
|
2006-12-28 22:18:35 +01:00
|
|
|
|
2011-09-28 21:47:03 +02:00
|
|
|
set_field(L, "margin_l", dia->Margin[0]);
|
|
|
|
set_field(L, "margin_r", dia->Margin[1]);
|
|
|
|
set_field(L, "margin_t", dia->Margin[2]);
|
2012-10-10 16:04:44 +02:00
|
|
|
set_field(L, "margin_b", dia->Margin[2]);
|
2006-12-28 22:18:35 +01:00
|
|
|
|
2011-09-28 21:47:03 +02:00
|
|
|
set_field(L, "text", dia->Text);
|
2006-12-28 22:18:35 +01:00
|
|
|
|
2014-04-23 21:50:26 +02:00
|
|
|
// create extradata table
|
|
|
|
lua_newtable(L);
|
|
|
|
for (auto const& ed : ass->GetExtradata(dia->ExtradataIds)) {
|
2014-09-06 18:16:44 +02:00
|
|
|
push_value(L, ed.key);
|
|
|
|
push_value(L, ed.value);
|
2014-04-23 21:50:26 +02:00
|
|
|
lua_settable(L, -3);
|
|
|
|
}
|
|
|
|
lua_setfield(L, -2, "extra");
|
|
|
|
|
2011-09-28 21:47:03 +02:00
|
|
|
set_field(L, "class", "dialogue");
|
|
|
|
}
|
2015-01-14 00:20:55 +01:00
|
|
|
else if (auto sty = check_cast_constptr<AssStyle>(e)) {
|
2014-03-08 02:02:32 +01:00
|
|
|
set_field(L, "raw", sty->GetEntryData());
|
2011-09-28 21:47:03 +02:00
|
|
|
set_field(L, "name", sty->name);
|
2006-12-28 22:18:35 +01:00
|
|
|
|
2011-09-28 21:47:03 +02:00
|
|
|
set_field(L, "fontname", sty->font);
|
|
|
|
set_field(L, "fontsize", sty->fontsize);
|
2006-12-28 22:18:35 +01:00
|
|
|
|
2012-10-26 16:09:14 +02:00
|
|
|
set_field(L, "color1", sty->primary.GetAssStyleFormatted() + "&");
|
|
|
|
set_field(L, "color2", sty->secondary.GetAssStyleFormatted() + "&");
|
|
|
|
set_field(L, "color3", sty->outline.GetAssStyleFormatted() + "&");
|
|
|
|
set_field(L, "color4", sty->shadow.GetAssStyleFormatted() + "&");
|
2011-09-28 21:47:03 +02:00
|
|
|
|
|
|
|
set_field(L, "bold", sty->bold);
|
|
|
|
set_field(L, "italic", sty->italic);
|
|
|
|
set_field(L, "underline", sty->underline);
|
|
|
|
set_field(L, "strikeout", sty->strikeout);
|
2006-12-28 22:18:35 +01:00
|
|
|
|
2011-09-28 21:47:03 +02:00
|
|
|
set_field(L, "scale_x", sty->scalex);
|
|
|
|
set_field(L, "scale_y", sty->scaley);
|
|
|
|
|
|
|
|
set_field(L, "spacing", sty->spacing);
|
|
|
|
|
|
|
|
set_field(L, "angle", sty->angle);
|
|
|
|
|
|
|
|
set_field(L, "borderstyle", sty->borderstyle);
|
|
|
|
set_field(L, "outline", sty->outline_w);
|
|
|
|
set_field(L, "shadow", sty->shadow_w);
|
|
|
|
|
|
|
|
set_field(L, "align", sty->alignment);
|
|
|
|
|
|
|
|
set_field(L, "margin_l", sty->Margin[0]);
|
|
|
|
set_field(L, "margin_r", sty->Margin[1]);
|
|
|
|
set_field(L, "margin_t", sty->Margin[2]);
|
2012-10-10 16:04:44 +02:00
|
|
|
set_field(L, "margin_b", sty->Margin[2]);
|
2011-09-28 21:47:03 +02:00
|
|
|
|
|
|
|
set_field(L, "encoding", sty->encoding);
|
|
|
|
|
|
|
|
set_field(L, "relative_to", 2);// From STS.h: "0: window, 1: video, 2: undefined (~window)"
|
|
|
|
|
|
|
|
set_field(L, "class", "style");
|
|
|
|
}
|
|
|
|
else {
|
2014-03-07 18:02:24 +01:00
|
|
|
assert(false);
|
2006-12-28 22:18:35 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-04-23 21:50:26 +02:00
|
|
|
std::unique_ptr<AssEntry> LuaAssFile::LuaToAssEntry(lua_State *L, AssFile *ass)
|
2006-12-28 22:18:35 +01:00
|
|
|
{
|
|
|
|
// assume an assentry table is on the top of the stack
|
|
|
|
// convert it to a real AssEntry object, and pop the table from the stack
|
|
|
|
|
2011-09-28 21:47:03 +02:00
|
|
|
if (!lua_istable(L, -1))
|
2014-04-27 16:20:11 +02:00
|
|
|
error(L, "Can't convert a non-table value to AssEntry");
|
2006-12-28 22:18:35 +01:00
|
|
|
|
|
|
|
lua_getfield(L, -1, "class");
|
2011-09-28 21:47:03 +02:00
|
|
|
if (!lua_isstring(L, -1))
|
2014-04-27 16:20:11 +02:00
|
|
|
error(L, "Table lacks 'class' field, can't convert to AssEntry");
|
2011-09-28 21:47:03 +02:00
|
|
|
|
2013-01-04 16:01:50 +01:00
|
|
|
std::string lclass(lua_tostring(L, -1));
|
|
|
|
boost::to_lower(lclass);
|
2006-12-28 22:18:35 +01:00
|
|
|
lua_pop(L, 1);
|
|
|
|
|
2013-06-10 15:58:13 +02:00
|
|
|
std::unique_ptr<AssEntry> result;
|
2014-04-27 16:20:11 +02:00
|
|
|
if (lclass == "info")
|
|
|
|
result = agi::make_unique<AssInfo>(get_string_field(L, "key", "info"), get_string_field(L, "value", "info"));
|
|
|
|
else if (lclass == "style") {
|
|
|
|
auto sty = new AssStyle;
|
|
|
|
result.reset(sty);
|
|
|
|
sty->name = get_string_field(L, "name", "style");
|
|
|
|
sty->font = get_string_field(L, "fontname", "style");
|
|
|
|
sty->fontsize = get_double_field(L, "fontsize", "style");
|
|
|
|
sty->primary = get_string_field(L, "color1", "style");
|
|
|
|
sty->secondary = get_string_field(L, "color2", "style");
|
|
|
|
sty->outline = get_string_field(L, "color3", "style");
|
|
|
|
sty->shadow = get_string_field(L, "color4", "style");
|
|
|
|
sty->bold = get_bool_field(L, "bold", "style");
|
|
|
|
sty->italic = get_bool_field(L, "italic", "style");
|
|
|
|
sty->underline = get_bool_field(L, "underline", "style");
|
|
|
|
sty->strikeout = get_bool_field(L, "strikeout", "style");
|
|
|
|
sty->scalex = get_double_field(L, "scale_x", "style");
|
|
|
|
sty->scaley = get_double_field(L, "scale_y", "style");
|
|
|
|
sty->spacing = get_double_field(L, "spacing", "style");
|
|
|
|
sty->angle = get_double_field(L, "angle", "style");
|
|
|
|
sty->borderstyle = get_int_field(L, "borderstyle", "style");
|
|
|
|
sty->outline_w = get_double_field(L, "outline", "style");
|
|
|
|
sty->shadow_w = get_double_field(L, "shadow", "style");
|
|
|
|
sty->alignment = get_int_field(L, "align", "style");
|
|
|
|
sty->Margin[0] = get_int_field(L, "margin_l", "style");
|
|
|
|
sty->Margin[1] = get_int_field(L, "margin_r", "style");
|
|
|
|
sty->Margin[2] = get_int_field(L, "margin_t", "style");
|
|
|
|
sty->encoding = get_int_field(L, "encoding", "style");
|
|
|
|
sty->UpdateData();
|
|
|
|
}
|
|
|
|
else if (lclass == "dialogue") {
|
|
|
|
assert(ass != 0); // since we need AssFile::AddExtradata
|
|
|
|
auto dia = new AssDialogue;
|
|
|
|
result.reset(dia);
|
|
|
|
|
|
|
|
dia->Comment = get_bool_field(L, "comment", "dialogue");
|
|
|
|
dia->Layer = get_int_field(L, "layer", "dialogue");
|
|
|
|
dia->Start = get_int_field(L, "start_time", "dialogue");
|
|
|
|
dia->End = get_int_field(L, "end_time", "dialogue");
|
|
|
|
dia->Style = get_string_field(L, "style", "dialogue");
|
|
|
|
dia->Actor = get_string_field(L, "actor", "dialogue");
|
|
|
|
dia->Margin[0] = get_int_field(L, "margin_l", "dialogue");
|
|
|
|
dia->Margin[1] = get_int_field(L, "margin_r", "dialogue");
|
|
|
|
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");
|
|
|
|
|
|
|
|
std::vector<uint32_t> new_ids;
|
2014-06-10 02:20:31 +02:00
|
|
|
|
|
|
|
lua_getfield(L, -1, "extra");
|
|
|
|
auto type = lua_type(L, -1);
|
2015-01-20 19:18:29 +01:00
|
|
|
if (type == LUA_TTABLE) {
|
|
|
|
lua_for_each(L, [&] {
|
|
|
|
if (lua_type(L, -2) != LUA_TSTRING) return;
|
|
|
|
new_ids.push_back(ass->AddExtradata(
|
|
|
|
get_string_or_default(L, -2),
|
|
|
|
get_string_or_default(L, -1)));
|
|
|
|
});
|
|
|
|
std::sort(begin(new_ids), end(new_ids));
|
|
|
|
dia->ExtradataIds = std::move(new_ids);
|
|
|
|
}
|
|
|
|
else if (type != LUA_TNIL) {
|
2014-06-10 02:20:31 +02:00
|
|
|
error(L, "dialogue extradata must be a table");
|
2015-01-20 19:18:29 +01:00
|
|
|
}
|
2011-09-28 21:47:03 +02:00
|
|
|
}
|
2014-04-27 16:20:11 +02:00
|
|
|
else {
|
|
|
|
error(L, "Found line with unknown class: %s", lclass.c_str());
|
2013-06-10 15:58:13 +02:00
|
|
|
return nullptr;
|
2011-09-28 21:47:03 +02:00
|
|
|
}
|
2014-04-27 16:20:11 +02:00
|
|
|
|
|
|
|
return result;
|
2006-12-28 22:18:35 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
int LuaAssFile::ObjectIndexRead(lua_State *L)
|
|
|
|
{
|
|
|
|
switch (lua_type(L, 2)) {
|
|
|
|
case LUA_TNUMBER:
|
2012-10-12 23:06:41 +02:00
|
|
|
{
|
2011-09-28 21:47:03 +02:00
|
|
|
// read an indexed AssEntry
|
2012-10-12 23:06:41 +02:00
|
|
|
int idx = lua_tointeger(L, 2);
|
|
|
|
CheckBounds(idx);
|
2014-03-09 04:14:22 +01:00
|
|
|
AssEntryToLua(L, idx - 1);
|
2011-09-28 21:47:03 +02:00
|
|
|
return 1;
|
2012-10-12 23:06:41 +02:00
|
|
|
}
|
2011-09-28 21:47:03 +02:00
|
|
|
|
|
|
|
case LUA_TSTRING:
|
|
|
|
{
|
|
|
|
// either return n or a function doing further stuff
|
|
|
|
const char *idx = lua_tostring(L, 2);
|
|
|
|
|
|
|
|
if (strcmp(idx, "n") == 0) {
|
|
|
|
// get number of items
|
2011-09-28 21:50:14 +02:00
|
|
|
lua_pushnumber(L, lines.size());
|
2006-12-28 22:18:35 +01:00
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
2011-09-28 21:47:03 +02:00
|
|
|
lua_pushvalue(L, 1);
|
|
|
|
if (strcmp(idx, "delete") == 0)
|
2014-03-20 21:58:58 +01:00
|
|
|
lua_pushcclosure(L, closure_wrapper_v<&LuaAssFile::ObjectDelete, false>, 1);
|
2011-09-28 21:47:03 +02:00
|
|
|
else if (strcmp(idx, "deleterange") == 0)
|
2014-03-20 21:58:58 +01:00
|
|
|
lua_pushcclosure(L, closure_wrapper_v<&LuaAssFile::ObjectDeleteRange, false>, 1);
|
2011-09-28 21:47:03 +02:00
|
|
|
else if (strcmp(idx, "insert") == 0)
|
2014-03-20 21:58:58 +01:00
|
|
|
lua_pushcclosure(L, closure_wrapper_v<&LuaAssFile::ObjectInsert, false>, 1);
|
2011-09-28 21:47:03 +02:00
|
|
|
else if (strcmp(idx, "append") == 0)
|
2014-03-20 21:58:58 +01:00
|
|
|
lua_pushcclosure(L, closure_wrapper_v<&LuaAssFile::ObjectAppend, false>, 1);
|
2014-12-24 22:56:23 +01:00
|
|
|
else if (strcmp(idx, "script_resolution") == 0)
|
|
|
|
lua_pushcclosure(L, closure_wrapper<&LuaAssFile::LuaGetScriptResolution>, 1);
|
2011-09-28 21:47:03 +02:00
|
|
|
else {
|
|
|
|
// idiot
|
|
|
|
lua_pop(L, 1);
|
2014-04-27 16:20:11 +02:00
|
|
|
return error(L, "Invalid indexing in Subtitle File object: '%s'", idx);
|
2006-12-28 22:18:35 +01:00
|
|
|
}
|
|
|
|
|
2011-09-28 21:47:03 +02:00
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
2006-12-28 22:18:35 +01:00
|
|
|
default:
|
2011-09-28 21:47:03 +02:00
|
|
|
// crap, user is stupid!
|
2014-04-27 16:20:11 +02:00
|
|
|
return error(L, "Attempt to index a Subtitle File object with value of type '%s'.", lua_typename(L, lua_type(L, 2)));
|
2006-12-28 22:18:35 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
assert(false);
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2014-03-09 04:14:22 +01:00
|
|
|
void LuaAssFile::InitScriptInfoIfNeeded()
|
|
|
|
{
|
|
|
|
if (script_info_copied) return;
|
|
|
|
size_t i = 0;
|
|
|
|
for (auto const& info : ass->Info) {
|
|
|
|
// Just in case an insane user inserted non-info lines into the
|
|
|
|
// script info section...
|
|
|
|
while (lines[i]) ++i;
|
2014-04-23 22:53:24 +02:00
|
|
|
lines_to_delete.emplace_back(agi::make_unique<AssInfo>(info));
|
2014-03-09 04:14:22 +01:00
|
|
|
lines[i++] = lines_to_delete.back().get();
|
|
|
|
}
|
|
|
|
script_info_copied = true;
|
|
|
|
}
|
|
|
|
|
|
|
|
void LuaAssFile::QueueLineForDeletion(size_t idx)
|
|
|
|
{
|
|
|
|
if (!lines[idx] || lines[idx]->Group() == AssEntryGroup::INFO)
|
|
|
|
InitScriptInfoIfNeeded();
|
|
|
|
else
|
|
|
|
lines_to_delete.emplace_back(lines[idx]);
|
|
|
|
}
|
|
|
|
|
|
|
|
void LuaAssFile::AssignLine(size_t idx, std::unique_ptr<AssEntry> e)
|
|
|
|
{
|
|
|
|
auto group = e->Group();
|
|
|
|
if (group == AssEntryGroup::INFO)
|
|
|
|
InitScriptInfoIfNeeded();
|
|
|
|
lines[idx] = e.get();
|
|
|
|
if (group == AssEntryGroup::INFO)
|
|
|
|
lines_to_delete.emplace_back(std::move(e));
|
|
|
|
else
|
|
|
|
e.release();
|
|
|
|
}
|
|
|
|
|
|
|
|
void LuaAssFile::InsertLine(std::vector<AssEntry *> &vec, size_t idx, std::unique_ptr<AssEntry> e)
|
|
|
|
{
|
|
|
|
auto group = e->Group();
|
|
|
|
if (group == AssEntryGroup::INFO)
|
|
|
|
InitScriptInfoIfNeeded();
|
|
|
|
vec.insert(vec.begin() + idx, e.get());
|
|
|
|
if (group == AssEntryGroup::INFO)
|
|
|
|
lines_to_delete.emplace_back(std::move(e));
|
|
|
|
else
|
|
|
|
e.release();
|
|
|
|
}
|
|
|
|
|
2011-09-28 21:47:03 +02:00
|
|
|
void LuaAssFile::ObjectIndexWrite(lua_State *L)
|
2006-12-28 22:18:35 +01:00
|
|
|
{
|
|
|
|
// instead of implementing everything twice, just call the other modification-functions from here
|
|
|
|
// after modifying the stack to match their expectations
|
|
|
|
|
2011-09-28 21:47:03 +02:00
|
|
|
CheckAllowModify();
|
2006-12-28 22:18:35 +01:00
|
|
|
|
2014-04-27 16:20:11 +02:00
|
|
|
int n = check_int(L, 2);
|
2006-12-28 22:18:35 +01:00
|
|
|
if (n < 0) {
|
|
|
|
// insert line so new index is n
|
2011-09-28 21:47:03 +02:00
|
|
|
lua_remove(L, 1);
|
2006-12-28 22:18:35 +01:00
|
|
|
lua_pushinteger(L, -n);
|
2011-09-28 21:47:03 +02:00
|
|
|
lua_replace(L, 1);
|
|
|
|
ObjectInsert(L);
|
|
|
|
}
|
|
|
|
else if (n == 0) {
|
2006-12-28 22:18:35 +01:00
|
|
|
// append line to list
|
2011-09-28 21:47:03 +02:00
|
|
|
lua_remove(L, 1);
|
2011-12-22 22:21:47 +01:00
|
|
|
lua_remove(L, 1);
|
2011-09-28 21:47:03 +02:00
|
|
|
ObjectAppend(L);
|
|
|
|
}
|
|
|
|
else {
|
2006-12-28 22:18:35 +01:00
|
|
|
// replace line at index n or delete
|
|
|
|
if (!lua_isnil(L, 3)) {
|
|
|
|
// insert
|
2014-03-09 04:14:22 +01:00
|
|
|
CheckBounds(n);
|
|
|
|
|
2014-04-23 21:50:26 +02:00
|
|
|
auto e = LuaToAssEntry(L, ass);
|
2013-06-10 15:58:13 +02:00
|
|
|
modification_type |= modification_mask(e.get());
|
2014-03-09 04:14:22 +01:00
|
|
|
QueueLineForDeletion(n - 1);
|
|
|
|
AssignLine(n - 1, std::move(e));
|
2011-09-28 21:47:03 +02:00
|
|
|
}
|
|
|
|
else {
|
2006-12-28 22:18:35 +01:00
|
|
|
// delete
|
2011-09-28 21:47:03 +02:00
|
|
|
lua_remove(L, 1);
|
2011-12-22 22:21:47 +01:00
|
|
|
lua_remove(L, 1);
|
2011-09-28 21:47:03 +02:00
|
|
|
ObjectDelete(L);
|
2006-12-28 22:18:35 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
int LuaAssFile::ObjectGetLen(lua_State *L)
|
|
|
|
{
|
2011-09-28 21:50:14 +02:00
|
|
|
lua_pushnumber(L, lines.size());
|
2006-12-28 22:18:35 +01:00
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
2011-09-28 21:47:03 +02:00
|
|
|
void LuaAssFile::ObjectDelete(lua_State *L)
|
2006-12-28 22:18:35 +01:00
|
|
|
{
|
2011-09-28 21:47:03 +02:00
|
|
|
CheckAllowModify();
|
2011-09-28 21:46:23 +02:00
|
|
|
|
2006-12-28 22:18:35 +01:00
|
|
|
// get number of items to delete
|
|
|
|
int itemcount = lua_gettop(L);
|
2014-04-16 05:47:59 +02:00
|
|
|
if (itemcount == 0) return;
|
2006-12-28 22:18:35 +01:00
|
|
|
|
2014-04-27 16:20:11 +02:00
|
|
|
std::vector<size_t> ids;
|
2014-04-16 05:47:59 +02:00
|
|
|
if (itemcount == 1 && lua_istable(L, 1)) {
|
|
|
|
lua_pushvalue(L, 1);
|
|
|
|
lua_for_each(L, [&] {
|
2014-04-27 16:20:11 +02:00
|
|
|
size_t n = check_uint(L, -1);
|
|
|
|
argcheck(L, n > 0 && n <= lines.size(), 1, "Out of range line index");
|
2014-04-16 05:47:59 +02:00
|
|
|
ids.push_back(n - 1);
|
|
|
|
});
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
ids.reserve(itemcount);
|
|
|
|
while (itemcount > 0) {
|
2014-04-27 16:20:11 +02:00
|
|
|
size_t n = check_uint(L, -1);
|
|
|
|
argcheck(L, n > 0 && n <= lines.size(), itemcount, "Out of range line index");
|
2014-04-16 05:47:59 +02:00
|
|
|
ids.push_back(n - 1);
|
|
|
|
--itemcount;
|
|
|
|
}
|
2006-12-28 22:18:35 +01:00
|
|
|
}
|
2011-09-28 21:47:03 +02:00
|
|
|
|
|
|
|
sort(ids.begin(), ids.end());
|
2006-12-28 22:18:35 +01:00
|
|
|
|
2012-10-12 23:06:41 +02:00
|
|
|
size_t id_idx = 0, out = 0;
|
|
|
|
for (size_t i = 0; i < lines.size(); ++i) {
|
|
|
|
if (id_idx < ids.size() && ids[id_idx] == i) {
|
|
|
|
modification_type |= modification_mask(lines[i]);
|
2014-03-09 04:14:22 +01:00
|
|
|
QueueLineForDeletion(i);
|
2012-10-12 23:06:41 +02:00
|
|
|
++id_idx;
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
lines[out++] = lines[i];
|
2011-09-28 21:47:03 +02:00
|
|
|
}
|
2006-12-28 22:18:35 +01:00
|
|
|
}
|
2012-10-12 23:06:41 +02:00
|
|
|
|
|
|
|
lines.erase(lines.begin() + out, lines.end());
|
2006-12-28 22:18:35 +01:00
|
|
|
}
|
|
|
|
|
2011-09-28 21:47:03 +02:00
|
|
|
void LuaAssFile::ObjectDeleteRange(lua_State *L)
|
2006-12-28 22:18:35 +01:00
|
|
|
{
|
2011-09-28 21:47:03 +02:00
|
|
|
CheckAllowModify();
|
2006-12-28 22:18:35 +01:00
|
|
|
|
2014-04-27 16:20:11 +02:00
|
|
|
size_t a = std::max<size_t>(check_uint(L, 1), 1) - 1;
|
|
|
|
size_t b = std::min<size_t>(check_uint(L, 2), lines.size());
|
2006-12-28 22:18:35 +01:00
|
|
|
|
2012-10-12 23:06:41 +02:00
|
|
|
if (a >= b) return;
|
2006-12-28 22:18:35 +01:00
|
|
|
|
2014-02-02 05:57:00 +01:00
|
|
|
for (size_t i = a; i < b; ++i) {
|
|
|
|
modification_type |= modification_mask(lines[i]);
|
2014-03-09 04:14:22 +01:00
|
|
|
QueueLineForDeletion(i);
|
2006-12-28 22:18:35 +01:00
|
|
|
}
|
2012-10-12 23:06:41 +02:00
|
|
|
|
2014-02-02 05:57:00 +01:00
|
|
|
lines.erase(lines.begin() + a, lines.begin() + b);
|
2006-12-28 22:18:35 +01:00
|
|
|
}
|
|
|
|
|
2011-09-28 21:47:03 +02:00
|
|
|
void LuaAssFile::ObjectAppend(lua_State *L)
|
2006-12-28 22:18:35 +01:00
|
|
|
{
|
2011-09-28 21:47:03 +02:00
|
|
|
CheckAllowModify();
|
2011-09-28 21:46:23 +02:00
|
|
|
|
2006-12-28 22:18:35 +01:00
|
|
|
int n = lua_gettop(L);
|
|
|
|
|
|
|
|
for (int i = 1; i <= n; i++) {
|
|
|
|
lua_pushvalue(L, i);
|
2014-04-23 21:50:26 +02:00
|
|
|
auto e = LuaToAssEntry(L, ass);
|
2013-06-10 15:58:13 +02:00
|
|
|
modification_type |= modification_mask(e.get());
|
2011-12-22 22:21:29 +01:00
|
|
|
|
2014-03-09 04:14:22 +01:00
|
|
|
if (lines.empty()) {
|
|
|
|
InsertLine(lines, 0, std::move(e));
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
2011-12-22 22:21:29 +01:00
|
|
|
// Find the appropriate place to put it
|
2014-03-09 04:14:22 +01:00
|
|
|
auto group = e->Group();
|
|
|
|
for (size_t i = lines.size(); i > 0; --i) {
|
|
|
|
auto cur_group = lines[i - 1] ? lines[i - 1]->Group() : AssEntryGroup::INFO;
|
|
|
|
if (cur_group == group) {
|
|
|
|
InsertLine(lines, i, std::move(e));
|
|
|
|
break;
|
2011-12-22 22:21:29 +01:00
|
|
|
}
|
2010-06-24 22:48:49 +02:00
|
|
|
}
|
2011-12-22 22:21:29 +01:00
|
|
|
|
2014-03-09 04:14:22 +01:00
|
|
|
// No lines of this type exist already, so just append it to the end
|
|
|
|
if (e) InsertLine(lines, lines.size(), std::move(e));
|
2006-12-28 22:18:35 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2011-09-28 21:47:03 +02:00
|
|
|
void LuaAssFile::ObjectInsert(lua_State *L)
|
2006-12-28 22:18:35 +01:00
|
|
|
{
|
2011-09-28 21:47:03 +02:00
|
|
|
CheckAllowModify();
|
2006-12-28 22:18:35 +01:00
|
|
|
|
2014-04-27 16:20:11 +02:00
|
|
|
size_t before = check_uint(L, 1);
|
2006-12-28 22:18:35 +01:00
|
|
|
|
2011-09-28 21:47:03 +02:00
|
|
|
// + 1 to allow appending at the end of the file
|
2014-04-27 16:20:11 +02:00
|
|
|
argcheck(L, before > 0 && before <= lines.size() + 1, 1,
|
2011-09-28 21:47:03 +02:00
|
|
|
"Out of range line index");
|
2006-12-28 22:18:35 +01:00
|
|
|
|
2014-04-27 16:20:11 +02:00
|
|
|
if (before == lines.size() + 1) {
|
2011-10-23 19:00:14 +02:00
|
|
|
lua_remove(L, 1);
|
|
|
|
ObjectAppend(L);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2011-09-28 21:47:03 +02:00
|
|
|
int n = lua_gettop(L);
|
2014-03-15 16:29:53 +01:00
|
|
|
std::vector<AssEntry *> new_entries;
|
|
|
|
new_entries.reserve(n - 1);
|
2006-12-28 22:18:35 +01:00
|
|
|
for (int i = 2; i <= n; i++) {
|
|
|
|
lua_pushvalue(L, i);
|
2014-04-23 21:50:26 +02:00
|
|
|
auto e = LuaToAssEntry(L, ass);
|
2013-06-10 15:58:13 +02:00
|
|
|
modification_type |= modification_mask(e.get());
|
2014-03-09 04:14:22 +01:00
|
|
|
InsertLine(new_entries, i - 2, std::move(e));
|
2006-12-28 22:18:35 +01:00
|
|
|
lua_pop(L, 1);
|
|
|
|
}
|
2012-10-12 23:06:41 +02:00
|
|
|
lines.insert(lines.begin() + before - 1, new_entries.begin(), new_entries.end());
|
2006-12-28 22:18:35 +01:00
|
|
|
}
|
|
|
|
|
2011-09-28 21:47:03 +02:00
|
|
|
void LuaAssFile::ObjectGarbageCollect(lua_State *L)
|
2006-12-28 22:18:35 +01:00
|
|
|
{
|
2011-09-28 21:47:03 +02:00
|
|
|
references--;
|
|
|
|
if (!references) delete this;
|
2010-06-09 01:21:39 +02:00
|
|
|
LOG_D("automation/lua") << "Garbage collected LuaAssFile";
|
2006-12-28 22:18:35 +01:00
|
|
|
}
|
|
|
|
|
2013-05-02 05:51:17 +02:00
|
|
|
int LuaAssFile::ObjectIPairs(lua_State *L)
|
|
|
|
{
|
|
|
|
lua_pushvalue(L, lua_upvalueindex(1)); // push 'this' as userdata
|
|
|
|
lua_pushcclosure(L, closure_wrapper<&LuaAssFile::IterNext>, 1);
|
|
|
|
lua_pushnil(L);
|
|
|
|
push_value(L, 0);
|
|
|
|
return 3;
|
|
|
|
}
|
|
|
|
|
|
|
|
int LuaAssFile::IterNext(lua_State *L)
|
|
|
|
{
|
2014-04-27 16:20:11 +02:00
|
|
|
size_t i = check_uint(L, 2);
|
|
|
|
if (i >= lines.size()) {
|
2013-05-02 05:51:17 +02:00
|
|
|
lua_pushnil(L);
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
push_value(L, i + 1);
|
2014-03-09 04:14:22 +01:00
|
|
|
AssEntryToLua(L, i);
|
2013-05-02 05:51:17 +02:00
|
|
|
return 2;
|
|
|
|
}
|
|
|
|
|
2006-12-28 22:18:35 +01:00
|
|
|
int LuaAssFile::LuaParseKaraokeData(lua_State *L)
|
|
|
|
{
|
2014-04-23 21:50:26 +02:00
|
|
|
auto e = LuaToAssEntry(L, ass);
|
2015-01-14 00:20:55 +01:00
|
|
|
auto dia = check_cast_constptr<AssDialogue>(e.get());
|
2014-04-27 16:20:11 +02:00
|
|
|
argcheck(L, !!dia, 1, "Subtitle line must be a dialogue line");
|
2006-12-28 22:18:35 +01:00
|
|
|
|
2011-09-30 22:42:09 +02:00
|
|
|
int idx = 0;
|
2012-01-26 22:19:25 +01:00
|
|
|
|
|
|
|
// 2.1.x stored everything before the first syllable at index zero
|
|
|
|
// There's no longer any such thing with the new parser, but scripts
|
|
|
|
// may rely on kara[0] existing so add an empty syllable
|
2014-04-27 15:41:28 +02:00
|
|
|
lua_createtable(L, 0, 6);
|
2012-01-26 22:19:25 +01:00
|
|
|
set_field(L, "duration", 0);
|
2012-01-31 01:44:07 +01:00
|
|
|
set_field(L, "start_time", 0);
|
|
|
|
set_field(L, "end_time", 0);
|
2012-01-26 22:19:25 +01:00
|
|
|
set_field(L, "tag", "");
|
|
|
|
set_field(L, "text", "");
|
|
|
|
set_field(L, "text_stripped", "");
|
|
|
|
lua_rawseti(L, -2, idx++);
|
|
|
|
|
2012-02-16 06:21:00 +01:00
|
|
|
AssKaraoke kara(dia, false, false);
|
2012-12-23 00:35:13 +01:00
|
|
|
for (auto const& syl : kara) {
|
2014-04-27 15:41:28 +02:00
|
|
|
lua_createtable(L, 0, 6);
|
2012-12-23 00:35:13 +01:00
|
|
|
set_field(L, "duration", syl.duration);
|
|
|
|
set_field(L, "start_time", syl.start_time - dia->Start);
|
|
|
|
set_field(L, "end_time", syl.start_time + syl.duration - dia->Start);
|
|
|
|
set_field(L, "tag", syl.tag_type);
|
|
|
|
set_field(L, "text", syl.GetText(false));
|
|
|
|
set_field(L, "text_stripped", syl.text);
|
2011-09-30 22:42:09 +02:00
|
|
|
lua_rawseti(L, -2, idx++);
|
2011-09-28 21:47:03 +02:00
|
|
|
}
|
2006-12-28 22:18:35 +01:00
|
|
|
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
2014-12-24 22:56:23 +01:00
|
|
|
int LuaAssFile::LuaGetScriptResolution(lua_State *L)
|
|
|
|
{
|
|
|
|
int w, h;
|
|
|
|
ass->GetResolution(w, h);
|
|
|
|
push_value(L, w);
|
|
|
|
push_value(L, h);
|
|
|
|
return 2;
|
|
|
|
}
|
|
|
|
|
2011-09-28 21:47:03 +02:00
|
|
|
void LuaAssFile::LuaSetUndoPoint(lua_State *L)
|
2006-12-28 22:18:35 +01:00
|
|
|
{
|
2011-09-28 21:47:03 +02:00
|
|
|
if (!can_set_undo)
|
2014-04-27 16:20:11 +02:00
|
|
|
error(L, "Attempt to set an undo point in a context where it makes no sense to do so.");
|
2006-12-28 22:18:35 +01:00
|
|
|
|
2011-09-28 21:47:12 +02:00
|
|
|
if (modification_type) {
|
2012-11-28 16:35:26 +01:00
|
|
|
pending_commits.emplace_back();
|
2011-09-28 21:50:14 +02:00
|
|
|
PendingCommit& back = pending_commits.back();
|
|
|
|
|
|
|
|
back.modification_type = modification_type;
|
2014-04-27 16:20:11 +02:00
|
|
|
back.mesage = to_wx(check_string(L, 1));
|
2011-09-28 21:50:14 +02:00
|
|
|
back.lines = lines;
|
2011-09-28 21:47:12 +02:00
|
|
|
modification_type = 0;
|
2006-12-28 22:18:35 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-03-20 21:58:58 +01:00
|
|
|
LuaAssFile *LuaAssFile::GetObjPointer(lua_State *L, int idx, bool allow_expired)
|
2006-12-28 22:18:35 +01:00
|
|
|
{
|
|
|
|
assert(lua_type(L, idx) == LUA_TUSERDATA);
|
2014-03-20 21:58:58 +01:00
|
|
|
auto ud = lua_touserdata(L, idx);
|
|
|
|
auto laf = *static_cast<LuaAssFile **>(ud);
|
|
|
|
if (!allow_expired && laf->references < 2)
|
2014-04-27 16:20:11 +02:00
|
|
|
error(L, "Subtitles object is no longer valid");
|
2014-03-20 21:58:58 +01:00
|
|
|
return laf;
|
2006-12-28 22:18:35 +01:00
|
|
|
}
|
|
|
|
|
2014-03-07 18:02:24 +01:00
|
|
|
std::vector<AssEntry *> LuaAssFile::ProcessingComplete(wxString const& undo_description)
|
2006-12-28 22:18:35 +01:00
|
|
|
{
|
2014-03-07 18:02:24 +01:00
|
|
|
auto apply_lines = [&](std::vector<AssEntry *> const& lines) {
|
2014-03-09 04:14:22 +01:00
|
|
|
if (script_info_copied)
|
|
|
|
ass->Info.clear();
|
2014-03-07 18:02:24 +01:00
|
|
|
ass->Styles.clear();
|
|
|
|
ass->Events.clear();
|
|
|
|
|
|
|
|
for (auto line : lines) {
|
2014-03-09 04:14:22 +01:00
|
|
|
if (!line) continue;
|
2014-03-07 18:02:24 +01:00
|
|
|
switch (line->Group()) {
|
|
|
|
case AssEntryGroup::INFO: ass->Info.push_back(*static_cast<AssInfo *>(line)); break;
|
|
|
|
case AssEntryGroup::STYLE: ass->Styles.push_back(*static_cast<AssStyle *>(line)); break;
|
|
|
|
case AssEntryGroup::DIALOGUE: ass->Events.push_back(*static_cast<AssDialogue *>(line)); break;
|
|
|
|
default: break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
};
|
2011-09-28 21:50:14 +02:00
|
|
|
// Apply any pending commits
|
2012-11-04 04:53:03 +01:00
|
|
|
for (auto const& pc : pending_commits) {
|
2014-03-07 18:02:24 +01:00
|
|
|
apply_lines(pc.lines);
|
2012-11-04 04:53:03 +01:00
|
|
|
ass->Commit(pc.mesage, pc.modification_type);
|
2011-09-28 21:47:03 +02:00
|
|
|
}
|
2011-09-28 21:50:14 +02:00
|
|
|
|
|
|
|
// Commit any changes after the last undo point was set
|
2014-03-07 18:02:24 +01:00
|
|
|
if (modification_type)
|
|
|
|
apply_lines(lines);
|
2011-09-28 21:50:14 +02:00
|
|
|
if (modification_type && can_set_undo && !undo_description.empty())
|
|
|
|
ass->Commit(undo_description, modification_type);
|
|
|
|
|
2013-10-27 15:15:39 +01:00
|
|
|
lines_to_delete.clear();
|
2011-09-28 21:50:14 +02:00
|
|
|
|
2014-03-07 18:02:24 +01:00
|
|
|
auto ret = std::move(lines);
|
2011-09-28 21:50:14 +02:00
|
|
|
references--;
|
|
|
|
if (!references) delete this;
|
2014-03-07 18:02:24 +01:00
|
|
|
return ret;
|
2011-09-28 21:50:14 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
void LuaAssFile::Cancel()
|
|
|
|
{
|
2013-10-27 15:15:39 +01:00
|
|
|
for (auto& line : lines_to_delete) line.release();
|
2011-09-28 21:47:03 +02:00
|
|
|
references--;
|
|
|
|
if (!references) delete this;
|
2006-12-28 22:18:35 +01:00
|
|
|
}
|
|
|
|
|
2011-09-29 02:10:22 +02:00
|
|
|
LuaAssFile::LuaAssFile(lua_State *L, AssFile *ass, bool can_modify, bool can_set_undo)
|
2011-09-28 21:47:03 +02:00
|
|
|
: ass(ass)
|
|
|
|
, L(L)
|
|
|
|
, can_modify(can_modify)
|
|
|
|
, can_set_undo(can_set_undo)
|
2006-12-28 22:18:35 +01:00
|
|
|
{
|
2014-03-07 18:02:24 +01:00
|
|
|
for (auto& line : ass->Info)
|
2014-03-09 04:14:22 +01:00
|
|
|
lines.push_back(nullptr);
|
2014-03-07 18:02:24 +01:00
|
|
|
for (auto& line : ass->Styles)
|
|
|
|
lines.push_back(&line);
|
|
|
|
for (auto& line : ass->Events)
|
2012-11-04 04:53:03 +01:00
|
|
|
lines.push_back(&line);
|
2012-10-12 19:16:39 +02:00
|
|
|
|
2006-12-28 22:18:35 +01:00
|
|
|
// prepare userdata object
|
2012-10-15 06:37:14 +02:00
|
|
|
*static_cast<LuaAssFile**>(lua_newuserdata(L, sizeof(LuaAssFile*))) = this;
|
2006-12-28 22:18:35 +01:00
|
|
|
|
|
|
|
// make the metatable
|
2014-04-27 15:41:28 +02:00
|
|
|
lua_createtable(L, 0, 5);
|
2014-04-27 16:20:11 +02:00
|
|
|
set_field<closure_wrapper<&LuaAssFile::ObjectIndexRead>>(L, "__index");
|
|
|
|
set_field<closure_wrapper_v<&LuaAssFile::ObjectIndexWrite, false>>(L, "__newindex");
|
|
|
|
set_field<closure_wrapper<&LuaAssFile::ObjectGetLen>>(L, "__len");
|
|
|
|
set_field<closure_wrapper_v<&LuaAssFile::ObjectGarbageCollect, true>>(L, "__gc");
|
|
|
|
set_field<closure_wrapper<&LuaAssFile::ObjectIPairs>>(L, "__ipairs");
|
2006-12-28 22:18:35 +01:00
|
|
|
lua_setmetatable(L, -2);
|
|
|
|
|
|
|
|
// register misc functions
|
|
|
|
// assume the "aegisub" global table exists
|
|
|
|
lua_getglobal(L, "aegisub");
|
|
|
|
|
2014-04-27 16:20:11 +02:00
|
|
|
set_field<closure_wrapper<&LuaAssFile::LuaParseKaraokeData>>(L, "parse_karaoke_data");
|
|
|
|
set_field<closure_wrapper_v<&LuaAssFile::LuaSetUndoPoint, false>>(L, "set_undo_point");
|
2008-03-05 00:17:07 +01:00
|
|
|
|
2011-09-28 21:47:03 +02:00
|
|
|
lua_pop(L, 1); // pop "aegisub" table
|
2009-07-29 07:43:02 +02:00
|
|
|
|
2011-09-28 21:47:03 +02:00
|
|
|
// Leaves userdata object on stack
|
|
|
|
}
|
2011-12-28 22:27:06 +01:00
|
|
|
}
|