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
|
|
|
|
2009-01-04 07:31:48 +01:00
|
|
|
#include "config.h"
|
|
|
|
|
2013-01-04 16:01:50 +01:00
|
|
|
#include "auto4_lua.h"
|
2010-06-09 01:21:39 +02:00
|
|
|
|
2012-11-26 19:46:03 +01:00
|
|
|
#include "auto4_lua_utils.h"
|
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"
|
2009-06-25 00:22:45 +02:00
|
|
|
#include "utils.h"
|
2008-03-05 05:10:20 +01:00
|
|
|
|
2013-01-04 16:01:50 +01:00
|
|
|
#include <libaegisub/exception.h>
|
2013-06-10 15:58:13 +02:00
|
|
|
#include <libaegisub/util.h>
|
2013-01-04 16:01:50 +01:00
|
|
|
|
|
|
|
#include <algorithm>
|
|
|
|
#include <boost/algorithm/string/case_conv.hpp>
|
|
|
|
#include <boost/range/adaptor/indirected.hpp>
|
|
|
|
#include <boost/range/algorithm_ext.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 {
|
2011-09-28 21:48:28 +02:00
|
|
|
DEFINE_SIMPLE_EXCEPTION_NOINNER(BadField, Automation4::MacroRunError, "automation/macro/bad_field")
|
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)
|
|
|
|
{
|
|
|
|
return (LuaAssFile::GetObjPointer(L, lua_upvalueindex(1))->*closure)(L);
|
|
|
|
}
|
|
|
|
|
|
|
|
template<void (LuaAssFile::*closure)(lua_State *)>
|
|
|
|
int closure_wrapper_v(lua_State *L)
|
|
|
|
{
|
|
|
|
(LuaAssFile::GetObjPointer(L, lua_upvalueindex(1))->*closure)(L);
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2011-09-28 21:47:12 +02:00
|
|
|
int modification_mask(AssEntry *e)
|
|
|
|
{
|
2012-11-25 01:08:29 +01:00
|
|
|
switch (e->Group())
|
2011-09-28 21:47:12 +02:00
|
|
|
{
|
|
|
|
case ENTRY_DIALOGUE: return AssFile::COMMIT_DIAG_ADDREM;
|
2012-11-25 01:08:29 +01:00
|
|
|
case ENTRY_STYLE: return AssFile::COMMIT_STYLES;
|
|
|
|
case ENTRY_FONT: return AssFile::COMMIT_ATTACHMENT;
|
|
|
|
case ENTRY_GRAPHIC: return AssFile::COMMIT_ATTACHMENT;
|
|
|
|
default: return AssFile::COMMIT_SCRIPTINFO;
|
2011-09-28 21:47:03 +02:00
|
|
|
}
|
2011-09-28 21:47:12 +02:00
|
|
|
}
|
|
|
|
}
|
2011-09-28 21:47:03 +02:00
|
|
|
|
|
|
|
namespace Automation4 {
|
|
|
|
void LuaAssFile::CheckAllowModify()
|
|
|
|
{
|
|
|
|
if (!can_modify)
|
|
|
|
luaL_error(L, "Attempt to modify subtitles in read-only feature context.");
|
|
|
|
}
|
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())
|
|
|
|
luaL_error(L, "Requested out-of-range line from subtitle file: %d", idx);
|
|
|
|
}
|
|
|
|
|
2006-12-28 22:18:35 +01:00
|
|
|
void LuaAssFile::AssEntryToLua(lua_State *L, AssEntry *e)
|
|
|
|
{
|
|
|
|
lua_newtable(L);
|
|
|
|
|
2012-11-25 01:08:29 +01:00
|
|
|
set_field(L, "section", e->GroupHeader());
|
2013-01-04 16:01:50 +01:00
|
|
|
set_field(L, "raw", e->GetEntryData());
|
2006-12-28 22:18:35 +01:00
|
|
|
|
2012-12-08 03:51:09 +01:00
|
|
|
if (AssInfo *info = dynamic_cast<AssInfo*>(e)) {
|
|
|
|
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");
|
|
|
|
}
|
|
|
|
else if (AssDialogue *dia = dynamic_cast<AssDialogue*>(e)) {
|
|
|
|
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
|
|
|
|
2011-09-28 21:47:03 +02:00
|
|
|
set_field(L, "class", "dialogue");
|
|
|
|
}
|
|
|
|
else if (AssStyle *sty = dynamic_cast<AssStyle*>(e)) {
|
|
|
|
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 {
|
2012-11-25 01:08:29 +01:00
|
|
|
// Attachments
|
2011-09-28 21:47:03 +02:00
|
|
|
set_field(L, "class", "unknown");
|
2006-12-28 22:18:35 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-06-10 15:58:13 +02:00
|
|
|
std::unique_ptr<AssEntry> LuaAssFile::LuaToAssEntry(lua_State *L)
|
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))
|
|
|
|
luaL_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))
|
|
|
|
luaL_error(L, "Table lacks 'class' field, can't convert to AssEntry");
|
|
|
|
|
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;
|
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
|
|
|
|
2011-09-28 21:47:03 +02:00
|
|
|
try {
|
2013-01-04 16:01:50 +01:00
|
|
|
if (lclass == "info")
|
2013-06-10 15:58:13 +02:00
|
|
|
result = agi::util::make_unique<AssInfo>(get_string_field(L, "key", "info"), get_string_field(L, "value", "info"));
|
2011-09-28 21:47:03 +02:00
|
|
|
else if (lclass == "style") {
|
|
|
|
AssStyle *sty = new AssStyle;
|
2013-06-10 15:58:13 +02:00
|
|
|
result.reset(sty);
|
2012-12-30 01:32:36 +01:00
|
|
|
sty->name = get_string_field(L, "name", "style");
|
|
|
|
sty->font = get_string_field(L, "fontname", "style");
|
2011-09-28 21:47:03 +02:00
|
|
|
sty->fontsize = get_double_field(L, "fontsize", "style");
|
2012-10-26 16:09:14 +02:00
|
|
|
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");
|
2011-09-28 21:47:03 +02:00
|
|
|
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") {
|
|
|
|
AssDialogue *dia = new AssDialogue;
|
2013-06-10 15:58:13 +02:00
|
|
|
result.reset(dia);
|
2011-09-28 21:47:03 +02:00
|
|
|
|
|
|
|
dia->Comment = get_bool_field(L, "comment", "dialogue");
|
|
|
|
dia->Layer = get_int_field(L, "layer", "dialogue");
|
2011-12-22 22:28:51 +01:00
|
|
|
dia->Start = get_int_field(L, "start_time", "dialogue");
|
|
|
|
dia->End = get_int_field(L, "end_time", "dialogue");
|
2013-01-04 16:01:50 +01:00
|
|
|
dia->Style = get_string_field(L, "style", "dialogue");
|
|
|
|
dia->Actor = get_string_field(L, "actor", "dialogue");
|
2011-09-28 21:47:03 +02:00
|
|
|
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");
|
2013-01-04 16:01:50 +01:00
|
|
|
dia->Effect = get_string_field(L, "effect", "dialogue");
|
|
|
|
dia->Text = get_string_field(L, "text", "dialogue");
|
2011-09-28 21:47:03 +02:00
|
|
|
}
|
2011-12-26 23:20:49 +01:00
|
|
|
else {
|
2013-01-04 16:01:50 +01:00
|
|
|
luaL_error(L, "Found line with unknown class: %s", lclass.c_str());
|
2011-12-26 23:20:49 +01:00
|
|
|
return 0;
|
|
|
|
}
|
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
|
|
|
|
2011-09-28 21:47:03 +02:00
|
|
|
return result;
|
|
|
|
}
|
|
|
|
catch (agi::Exception const& e) {
|
|
|
|
luaL_error(L, e.GetMessage().c_str());
|
2013-06-10 15:58:13 +02:00
|
|
|
return nullptr;
|
2011-09-28 21:47:03 +02:00
|
|
|
}
|
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);
|
|
|
|
AssEntryToLua(L, lines[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)
|
|
|
|
lua_pushcclosure(L, closure_wrapper_v<&LuaAssFile::ObjectDelete>, 1);
|
|
|
|
else if (strcmp(idx, "deleterange") == 0)
|
|
|
|
lua_pushcclosure(L, closure_wrapper_v<&LuaAssFile::ObjectDeleteRange>, 1);
|
|
|
|
else if (strcmp(idx, "insert") == 0)
|
|
|
|
lua_pushcclosure(L, closure_wrapper_v<&LuaAssFile::ObjectInsert>, 1);
|
|
|
|
else if (strcmp(idx, "append") == 0)
|
|
|
|
lua_pushcclosure(L, closure_wrapper_v<&LuaAssFile::ObjectAppend>, 1);
|
|
|
|
else {
|
|
|
|
// idiot
|
|
|
|
lua_pop(L, 1);
|
|
|
|
return luaL_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!
|
|
|
|
return luaL_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;
|
|
|
|
}
|
|
|
|
|
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
|
|
|
|
2011-09-28 21:47:03 +02:00
|
|
|
int n = luaL_checkint(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
|
2013-06-10 15:58:13 +02:00
|
|
|
auto e = LuaToAssEntry(L);
|
|
|
|
modification_type |= modification_mask(e.get());
|
2012-10-12 23:06:41 +02:00
|
|
|
CheckBounds(n);
|
|
|
|
lines_to_delete.push_back(lines[n - 1]);
|
2013-06-10 15:58:13 +02:00
|
|
|
lines[n - 1] = e.release();
|
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);
|
|
|
|
std::vector<int> ids;
|
|
|
|
ids.reserve(itemcount);
|
|
|
|
|
|
|
|
while (itemcount > 0) {
|
2011-09-28 21:47:03 +02:00
|
|
|
int n = luaL_checkint(L, itemcount);
|
2011-09-28 21:50:14 +02:00
|
|
|
luaL_argcheck(L, n > 0 && n <= (int)lines.size(), itemcount, "Out of range line index");
|
2012-10-12 23:06:41 +02:00
|
|
|
ids.push_back(n - 1);
|
2006-12-28 22:18:35 +01:00
|
|
|
--itemcount;
|
|
|
|
}
|
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]);
|
|
|
|
lines_to_delete.push_back(lines[i]);
|
|
|
|
++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
|
|
|
|
2012-10-12 23:06:41 +02:00
|
|
|
size_t a = std::max<size_t>(luaL_checkinteger(L, 1), 1) - 1;
|
|
|
|
size_t b = std::min<size_t>(luaL_checkinteger(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
|
|
|
|
2012-10-12 23:06:41 +02:00
|
|
|
for (; b < lines.size(); ++a, ++b) {
|
|
|
|
modification_type |= modification_mask(lines[a]);
|
|
|
|
lines_to_delete.push_back(lines[a]);
|
|
|
|
lines[a] = lines[b];
|
2006-12-28 22:18:35 +01:00
|
|
|
}
|
2012-10-12 23:06:41 +02:00
|
|
|
|
|
|
|
lines.erase(lines.begin() + a, lines.end());
|
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);
|
2013-06-10 15:58:13 +02:00
|
|
|
auto e = LuaToAssEntry(L);
|
|
|
|
modification_type |= modification_mask(e.get());
|
2011-12-22 22:21:29 +01:00
|
|
|
|
|
|
|
// Find the appropriate place to put it
|
2012-12-23 00:35:13 +01:00
|
|
|
auto it = lines.end();
|
2011-12-22 22:21:29 +01:00
|
|
|
if (!lines.empty()) {
|
|
|
|
do {
|
|
|
|
--it;
|
|
|
|
}
|
2012-11-25 01:08:29 +01:00
|
|
|
while (it != lines.begin() && (*it)->Group() != e->Group());
|
2010-06-24 22:48:49 +02:00
|
|
|
}
|
2011-12-22 22:21:29 +01:00
|
|
|
|
2012-11-25 01:08:29 +01:00
|
|
|
if (it == lines.end() || (*it)->Group() != e->Group()) {
|
2011-12-22 22:21:29 +01:00
|
|
|
// The new entry belongs to a group that doesn't exist yet, so
|
|
|
|
// create it at the end of the file
|
2013-06-10 15:58:13 +02:00
|
|
|
lines.push_back(e.release());
|
2010-06-24 22:48:49 +02:00
|
|
|
}
|
2011-12-22 22:21:29 +01:00
|
|
|
else {
|
|
|
|
// Append the entry to the end of the existing group
|
2013-06-10 15:58:13 +02:00
|
|
|
lines.insert(++it, e.release());
|
2011-12-22 22:21:29 +01:00
|
|
|
}
|
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
|
|
|
|
2011-09-28 21:47:03 +02:00
|
|
|
int before = luaL_checkinteger(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
|
2011-09-28 21:50:14 +02:00
|
|
|
luaL_argcheck(L, before > 0 && before <= (int)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
|
|
|
|
2011-10-23 19:00:14 +02:00
|
|
|
if (before == (int)lines.size() + 1) {
|
|
|
|
lua_remove(L, 1);
|
|
|
|
ObjectAppend(L);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2011-09-28 21:47:03 +02:00
|
|
|
int n = lua_gettop(L);
|
2012-11-28 16:50:52 +01:00
|
|
|
std::vector<AssEntry *> new_entries(n - 1, nullptr);
|
2006-12-28 22:18:35 +01:00
|
|
|
for (int i = 2; i <= n; i++) {
|
|
|
|
lua_pushvalue(L, i);
|
2013-06-10 15:58:13 +02:00
|
|
|
auto e = LuaToAssEntry(L);
|
|
|
|
modification_type |= modification_mask(e.get());
|
|
|
|
new_entries[i - 2] = e.release();
|
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)
|
|
|
|
{
|
|
|
|
int i = luaL_checkint(L, 2);
|
|
|
|
if (i >= (int)lines.size()) {
|
|
|
|
lua_pushnil(L);
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
push_value(L, i + 1);
|
|
|
|
AssEntryToLua(L, lines[i]);
|
|
|
|
return 2;
|
|
|
|
}
|
|
|
|
|
2006-12-28 22:18:35 +01:00
|
|
|
int LuaAssFile::LuaParseKaraokeData(lua_State *L)
|
|
|
|
{
|
2013-06-10 15:58:13 +02:00
|
|
|
auto e = LuaToAssEntry(L);
|
2011-09-28 21:46:23 +02:00
|
|
|
AssDialogue *dia = dynamic_cast<AssDialogue*>(e.get());
|
2011-09-28 21:47:03 +02:00
|
|
|
luaL_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
|
|
|
|
lua_newtable(L);
|
|
|
|
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) {
|
2011-09-28 21:47:03 +02:00
|
|
|
lua_newtable(L);
|
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;
|
|
|
|
}
|
|
|
|
|
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)
|
|
|
|
luaL_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;
|
2013-01-04 16:01:50 +01:00
|
|
|
back.mesage = wxString::FromUTF8(luaL_checkstring(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
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
LuaAssFile *LuaAssFile::GetObjPointer(lua_State *L, int idx)
|
|
|
|
{
|
|
|
|
assert(lua_type(L, idx) == LUA_TUSERDATA);
|
|
|
|
void *ud = lua_touserdata(L, idx);
|
|
|
|
return *((LuaAssFile**)ud);
|
|
|
|
}
|
|
|
|
|
2011-09-28 21:47:03 +02:00
|
|
|
void LuaAssFile::ProcessingComplete(wxString const& undo_description)
|
2006-12-28 22:18:35 +01:00
|
|
|
{
|
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) {
|
2012-10-12 23:06:41 +02:00
|
|
|
ass->Line.clear();
|
2012-11-04 04:53:03 +01:00
|
|
|
boost::push_back(ass->Line, pc.lines | boost::adaptors::indirected);
|
|
|
|
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
|
2012-10-12 23:06:41 +02:00
|
|
|
if (modification_type) {
|
|
|
|
ass->Line.clear();
|
2012-10-12 19:16:39 +02:00
|
|
|
boost::push_back(ass->Line, lines | boost::adaptors::indirected);
|
2012-10-12 23:06:41 +02:00
|
|
|
}
|
2011-09-28 21:50:14 +02:00
|
|
|
if (modification_type && can_set_undo && !undo_description.empty())
|
|
|
|
ass->Commit(undo_description, modification_type);
|
|
|
|
|
|
|
|
delete_clear(lines_to_delete);
|
|
|
|
|
|
|
|
references--;
|
|
|
|
if (!references) delete this;
|
|
|
|
}
|
|
|
|
|
|
|
|
void LuaAssFile::Cancel()
|
|
|
|
{
|
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)
|
2011-09-28 21:47:12 +02:00
|
|
|
, modification_type(0)
|
2011-09-28 21:47:03 +02:00
|
|
|
, references(2)
|
2006-12-28 22:18:35 +01:00
|
|
|
{
|
2012-11-04 04:53:03 +01:00
|
|
|
for (auto& line : ass->Line)
|
|
|
|
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
|
|
|
|
lua_newtable(L);
|
2011-09-28 21:47:03 +02:00
|
|
|
set_field(L, "__index", closure_wrapper<&LuaAssFile::ObjectIndexRead>);
|
|
|
|
set_field(L, "__newindex", closure_wrapper_v<&LuaAssFile::ObjectIndexWrite>);
|
|
|
|
set_field(L, "__len", closure_wrapper<&LuaAssFile::ObjectGetLen>);
|
|
|
|
set_field(L, "__gc", closure_wrapper_v<&LuaAssFile::ObjectGarbageCollect>);
|
2013-05-02 05:51:17 +02:00
|
|
|
set_field(L, "__ipairs", closure_wrapper<&LuaAssFile::ObjectIPairs>);
|
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");
|
|
|
|
|
2011-09-28 21:47:03 +02:00
|
|
|
set_field(L, "parse_karaoke_data", closure_wrapper<&LuaAssFile::LuaParseKaraokeData>);
|
|
|
|
set_field(L, "set_undo_point", closure_wrapper_v<&LuaAssFile::LuaSetUndoPoint>);
|
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
|
|
|
}
|