2011-09-28 21:45:25 +02:00
|
|
|
// Copyright (c) 2006, 2007, 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.
|
|
|
|
//
|
|
|
|
// Aegisub Project http://www.aegisub.org/
|
|
|
|
|
|
|
|
/// @file auto4_lua_progresssink.cpp
|
|
|
|
/// @brief Lua 5.1-based scripting engine
|
|
|
|
/// @ingroup scripting
|
|
|
|
///
|
|
|
|
|
|
|
|
#include "auto4_lua.h"
|
|
|
|
|
2014-05-23 00:40:16 +02:00
|
|
|
#include "compat.h"
|
|
|
|
|
2014-03-27 03:24:37 +01:00
|
|
|
#include <libaegisub/lua/utils.h>
|
2012-02-23 20:28:19 +01:00
|
|
|
|
2012-11-26 19:46:03 +01:00
|
|
|
#include <wx/filedlg.h>
|
2011-09-28 21:45:25 +02:00
|
|
|
|
2014-04-27 16:20:11 +02:00
|
|
|
using namespace agi::lua;
|
|
|
|
|
2011-09-28 21:47:40 +02:00
|
|
|
namespace {
|
2014-04-27 16:20:11 +02:00
|
|
|
template<lua_CFunction fn>
|
|
|
|
void set_field_to_closure(lua_State *L, const char *name, int ps_idx = -3)
|
2011-09-28 21:47:40 +02:00
|
|
|
{
|
|
|
|
lua_pushvalue(L, ps_idx);
|
2014-04-27 16:20:11 +02:00
|
|
|
lua_pushcclosure(L, exception_wrapper<fn>, 1);
|
2011-09-28 21:47:40 +02:00
|
|
|
lua_setfield(L, -2, name);
|
|
|
|
}
|
|
|
|
|
|
|
|
void set_field_to_nil(lua_State *L, int idx, const char *name)
|
|
|
|
{
|
|
|
|
lua_pushnil(L);
|
|
|
|
lua_setfield(L, idx, name);
|
|
|
|
}
|
2014-03-27 03:24:37 +01:00
|
|
|
|
|
|
|
wxString check_wxstring(lua_State *L, int idx)
|
|
|
|
{
|
2014-04-27 16:20:11 +02:00
|
|
|
return to_wx(check_string(L, idx));
|
2014-03-27 03:24:37 +01:00
|
|
|
}
|
2011-09-28 21:45:25 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
namespace Automation4 {
|
2011-09-28 21:47:40 +02:00
|
|
|
LuaProgressSink::LuaProgressSink(lua_State *L, ProgressSink *ps, bool allow_config_dialog)
|
2014-04-27 16:20:11 +02:00
|
|
|
: L(L)
|
2011-09-28 21:45:25 +02:00
|
|
|
{
|
2014-04-27 16:20:11 +02:00
|
|
|
auto ud = (ProgressSink**)lua_newuserdata(L, sizeof(ProgressSink*));
|
2011-09-28 21:47:40 +02:00
|
|
|
*ud = ps;
|
2011-09-28 21:45:25 +02:00
|
|
|
|
|
|
|
// register progress reporting stuff
|
|
|
|
lua_getglobal(L, "aegisub");
|
|
|
|
|
2011-09-28 21:47:40 +02:00
|
|
|
// Create aegisub.progress table
|
2014-04-27 15:41:28 +02:00
|
|
|
lua_createtable(L, 0, 5);
|
2014-04-27 16:20:11 +02:00
|
|
|
set_field_to_closure<LuaSetProgress>(L, "set");
|
|
|
|
set_field_to_closure<LuaSetTask>(L, "task");
|
|
|
|
set_field_to_closure<LuaSetTitle>(L, "title");
|
|
|
|
set_field_to_closure<LuaGetCancelled>(L, "is_cancelled");
|
2011-09-28 21:45:25 +02:00
|
|
|
lua_setfield(L, -2, "progress");
|
|
|
|
|
2011-09-28 21:47:40 +02:00
|
|
|
// Create aegisub.debug table
|
2014-04-27 15:41:28 +02:00
|
|
|
lua_createtable(L, 0, 4);
|
2014-04-27 16:20:11 +02:00
|
|
|
set_field_to_closure<LuaDebugOut>(L, "out");
|
2011-09-28 21:45:25 +02:00
|
|
|
lua_setfield(L, -2, "debug");
|
2011-09-28 21:47:40 +02:00
|
|
|
|
|
|
|
// Set aegisub.log
|
2014-04-27 16:20:11 +02:00
|
|
|
set_field_to_closure<LuaDebugOut>(L, "log", -2);
|
2011-09-28 21:45:25 +02:00
|
|
|
|
|
|
|
if (allow_config_dialog) {
|
2014-04-27 15:41:28 +02:00
|
|
|
lua_createtable(L, 0, 3);
|
2014-04-27 16:20:11 +02:00
|
|
|
set_field_to_closure<LuaDisplayDialog>(L, "display");
|
|
|
|
set_field_to_closure<LuaDisplayOpenDialog>(L, "open");
|
|
|
|
set_field_to_closure<LuaDisplaySaveDialog>(L, "save");
|
2011-09-28 21:45:25 +02:00
|
|
|
lua_setfield(L, -2, "dialog");
|
|
|
|
}
|
|
|
|
|
|
|
|
// reference so other objects can also find the progress sink
|
|
|
|
lua_pushvalue(L, -2);
|
|
|
|
lua_setfield(L, LUA_REGISTRYINDEX, "progress_sink");
|
|
|
|
|
|
|
|
lua_pop(L, 2);
|
|
|
|
}
|
|
|
|
|
|
|
|
LuaProgressSink::~LuaProgressSink()
|
|
|
|
{
|
|
|
|
// remove progress reporting stuff
|
|
|
|
lua_getglobal(L, "aegisub");
|
2011-09-28 21:47:40 +02:00
|
|
|
set_field_to_nil(L, -2, "progress");
|
|
|
|
set_field_to_nil(L, -2, "debug");
|
2011-09-28 21:45:25 +02:00
|
|
|
lua_pop(L, 1);
|
2011-09-28 21:47:40 +02:00
|
|
|
|
|
|
|
set_field_to_nil(L, LUA_REGISTRYINDEX, "progress_sink");
|
2011-09-28 21:45:25 +02:00
|
|
|
}
|
|
|
|
|
2011-09-28 21:47:40 +02:00
|
|
|
ProgressSink* LuaProgressSink::GetObjPointer(lua_State *L, int idx)
|
2011-09-28 21:45:25 +02:00
|
|
|
{
|
|
|
|
assert(lua_type(L, idx) == LUA_TUSERDATA);
|
2011-09-28 21:47:40 +02:00
|
|
|
return *((ProgressSink**)lua_touserdata(L, idx));
|
2011-09-28 21:45:25 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
int LuaProgressSink::LuaSetProgress(lua_State *L)
|
|
|
|
{
|
2011-09-28 21:47:40 +02:00
|
|
|
GetObjPointer(L, lua_upvalueindex(1))->SetProgress(lua_tonumber(L, 1), 100);
|
2011-09-28 21:45:25 +02:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
int LuaProgressSink::LuaSetTask(lua_State *L)
|
|
|
|
{
|
2014-04-27 16:20:11 +02:00
|
|
|
GetObjPointer(L, lua_upvalueindex(1))->SetMessage(check_string(L, 1));
|
2011-09-28 21:45:25 +02:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
int LuaProgressSink::LuaSetTitle(lua_State *L)
|
|
|
|
{
|
2014-04-27 16:20:11 +02:00
|
|
|
GetObjPointer(L, lua_upvalueindex(1))->SetTitle(check_string(L, 1));
|
2011-09-28 21:45:25 +02:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
int LuaProgressSink::LuaGetCancelled(lua_State *L)
|
|
|
|
{
|
2011-09-28 21:47:40 +02:00
|
|
|
lua_pushboolean(L, GetObjPointer(L, lua_upvalueindex(1))->IsCancelled());
|
2011-09-28 21:45:25 +02:00
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
int LuaProgressSink::LuaDebugOut(lua_State *L)
|
|
|
|
{
|
2011-09-28 21:47:40 +02:00
|
|
|
ProgressSink *ps = GetObjPointer(L, lua_upvalueindex(1));
|
2011-09-28 21:45:25 +02:00
|
|
|
|
|
|
|
// Check trace level
|
2013-11-28 02:14:19 +01:00
|
|
|
if (lua_type(L, 1) == LUA_TNUMBER) {
|
2011-09-28 21:47:40 +02:00
|
|
|
if (lua_tointeger(L, 1) > ps->GetTraceLevel())
|
2011-09-28 21:45:25 +02:00
|
|
|
return 0;
|
|
|
|
// remove trace level
|
|
|
|
lua_remove(L, 1);
|
|
|
|
}
|
|
|
|
|
|
|
|
// Only do format-string handling if there's more than one argument left
|
|
|
|
// (If there's more than one argument left, assume first is a format string and rest are format arguments)
|
|
|
|
if (lua_gettop(L) > 1) {
|
|
|
|
// Format the string
|
|
|
|
lua_getglobal(L, "string");
|
|
|
|
lua_getfield(L, -1, "format");
|
|
|
|
// Here stack contains format string, format arguments, 'string' table, format function
|
|
|
|
// remove 'string' table
|
|
|
|
lua_remove(L, -2);
|
|
|
|
// put the format function into place
|
|
|
|
lua_insert(L, 1);
|
|
|
|
// call format function
|
2012-02-08 00:17:06 +01:00
|
|
|
if (lua_pcall(L, lua_gettop(L) - 1, 1, 0)) {
|
|
|
|
// format failed so top of the stack now has an error message
|
|
|
|
// which we want to add position information to
|
|
|
|
luaL_where(L, 1);
|
|
|
|
lua_insert(L, 1);
|
|
|
|
lua_concat(L, 2);
|
2014-04-28 22:36:56 +02:00
|
|
|
lua_error(L);
|
2012-02-08 00:17:06 +01:00
|
|
|
}
|
2011-09-28 21:45:25 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
// Top of stack is now a string to output
|
2014-04-27 16:20:11 +02:00
|
|
|
ps->Log(check_string(L, 1));
|
2011-09-28 21:45:25 +02:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
int LuaProgressSink::LuaDisplayDialog(lua_State *L)
|
|
|
|
{
|
2011-09-28 21:47:40 +02:00
|
|
|
ProgressSink *ps = GetObjPointer(L, lua_upvalueindex(1));
|
2011-09-28 21:45:25 +02:00
|
|
|
|
2011-09-28 21:49:27 +02:00
|
|
|
LuaDialog dlg(L, true); // magically creates the config dialog structure etc
|
|
|
|
ps->ShowDialog(&dlg);
|
2011-09-28 21:45:25 +02:00
|
|
|
|
|
|
|
// more magic: puts two values on stack: button pushed and table with control results
|
|
|
|
return dlg.LuaReadBack(L);
|
|
|
|
}
|
2012-02-22 05:17:16 +01:00
|
|
|
|
|
|
|
int LuaProgressSink::LuaDisplayOpenDialog(lua_State *L)
|
|
|
|
{
|
|
|
|
ProgressSink *ps = GetObjPointer(L, lua_upvalueindex(1));
|
|
|
|
wxString message(check_wxstring(L, 1));
|
|
|
|
wxString dir(check_wxstring(L, 2));
|
|
|
|
wxString file(check_wxstring(L, 3));
|
|
|
|
wxString wildcard(check_wxstring(L, 4));
|
|
|
|
bool multiple = !!lua_toboolean(L, 5);
|
|
|
|
bool must_exist = lua_toboolean(L, 6) || lua_isnil(L, 6);
|
|
|
|
|
|
|
|
int flags = wxFD_OPEN;
|
|
|
|
if (multiple)
|
|
|
|
flags |= wxFD_MULTIPLE;
|
|
|
|
if (must_exist)
|
|
|
|
flags |= wxFD_FILE_MUST_EXIST;
|
|
|
|
|
2013-11-21 18:13:36 +01:00
|
|
|
wxFileDialog diag(nullptr, message, dir, file, wildcard, flags);
|
2012-02-22 05:17:16 +01:00
|
|
|
if (ps->ShowDialog(&diag) == wxID_CANCEL) {
|
|
|
|
lua_pushnil(L);
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (multiple) {
|
|
|
|
wxArrayString files;
|
2013-07-22 01:38:32 +02:00
|
|
|
diag.GetPaths(files);
|
2012-02-22 05:17:16 +01:00
|
|
|
|
2014-04-27 15:41:28 +02:00
|
|
|
lua_createtable(L, files.size(), 0);
|
2012-02-22 05:17:16 +01:00
|
|
|
for (size_t i = 0; i < files.size(); ++i) {
|
|
|
|
lua_pushstring(L, files[i].utf8_str());
|
|
|
|
lua_rawseti(L, -2, i + 1);
|
|
|
|
}
|
|
|
|
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
2013-07-22 01:38:32 +02:00
|
|
|
lua_pushstring(L, diag.GetPath().utf8_str());
|
2012-02-22 05:17:16 +01:00
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
int LuaProgressSink::LuaDisplaySaveDialog(lua_State *L)
|
|
|
|
{
|
|
|
|
ProgressSink *ps = GetObjPointer(L, lua_upvalueindex(1));
|
|
|
|
wxString message(check_wxstring(L, 1));
|
|
|
|
wxString dir(check_wxstring(L, 2));
|
|
|
|
wxString file(check_wxstring(L, 3));
|
|
|
|
wxString wildcard(check_wxstring(L, 4));
|
|
|
|
bool prompt_overwrite = !lua_toboolean(L, 5);
|
|
|
|
|
|
|
|
int flags = wxFD_SAVE;
|
|
|
|
if (prompt_overwrite)
|
|
|
|
flags |= wxFD_OVERWRITE_PROMPT;
|
|
|
|
|
|
|
|
wxFileDialog diag(ps->GetParentWindow(), message, dir, file, wildcard, flags);
|
|
|
|
if (ps->ShowDialog(&diag) == wxID_CANCEL) {
|
|
|
|
lua_pushnil(L);
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
2014-01-01 20:46:27 +01:00
|
|
|
lua_pushstring(L, diag.GetPath().utf8_str());
|
2012-02-22 05:17:16 +01:00
|
|
|
return 1;
|
|
|
|
}
|
2011-09-28 21:45:25 +02:00
|
|
|
}
|