1
// Copyright (c) 2006, 2007, Niels Martin Hansen
2
// All rights reserved.
4
// Redistribution and use in source and binary forms, with or without
5
// modification, are permitted provided that the following conditions are met:
7
// * Redistributions of source code must retain the above copyright notice,
8
// this list of conditions and the following disclaimer.
9
// * Redistributions in binary form must reproduce the above copyright notice,
10
// this list of conditions and the following disclaimer in the documentation
11
// and/or other materials provided with the distribution.
12
// * Neither the name of the Aegisub Group nor the names of its contributors
13
// may be used to endorse or promote products derived from this software
14
// without specific prior written permission.
16
// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
17
// AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
18
// IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
19
// ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
20
// LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
21
// CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
22
// SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
23
// INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
24
// CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
25
// ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
26
// POSSIBILITY OF SUCH DAMAGE.
28
// -----------------------------------------------------------------------------
32
// Website: http://aegisub.cellosoft.com
33
// Contact: mailto:jiifurusu@gmail.com
40
#include "auto4_lua.h"
41
#include "auto4_lua_factory.h"
42
#include "auto4_lua_scriptreader.h"
43
#include "ass_dialogue.h"
44
#include "ass_style.h"
46
#include "ass_override.h"
47
#include "standard_paths.h"
48
#include "text_file_reader.h"
52
#include "video_context.h"
55
#include "../../contrib/lua51/src/lualib.h"
56
#include "../../contrib/lua51/src/lauxlib.h"
62
#include <wx/msgdlg.h>
63
#include <wx/filename.h>
64
#include <wx/filefn.h>
65
#include <wx/window.h>
69
namespace Automation4 {
74
struct LuaStackcheck {
77
void check_stack(int additional)
79
int top = lua_gettop(L);
80
if (top - additional != startstack) {
81
wxLogDebug(_T("Lua stack size mismatch."));
83
assert(top - additional == startstack);
88
int top = lua_gettop(L);
89
wxLogDebug(_T("Dumping Lua stack..."));
90
for (int i = top; i > 0; i--) {
92
wxString type(lua_typename(L, lua_type(L, -1)), wxConvUTF8);
93
if (lua_isstring(L, i)) {
94
wxLogDebug(type + _T(": ") + wxString(lua_tostring(L, -1), wxConvUTF8));
100
wxLogDebug(_T("--- end dump"));
102
LuaStackcheck(lua_State *_L) : L(_L) { startstack = lua_gettop(L); }
103
~LuaStackcheck() { check_stack(0); }
106
struct LuaStackcheck {
107
void check_stack(int additional) { }
109
LuaStackcheck(lua_State *L) { }
117
LuaScript::LuaScript(const wxString &filename)
124
LuaScript::~LuaScript()
129
void LuaScript::Create()
136
// create lua environment
138
LuaStackcheck _stackcheck(L);
140
// register standard libs
141
lua_pushcfunction(L, luaopen_base); lua_call(L, 0, 0);
142
lua_pushcfunction(L, luaopen_package); lua_call(L, 0, 0);
143
lua_pushcfunction(L, luaopen_string); lua_call(L, 0, 0);
144
lua_pushcfunction(L, luaopen_table); lua_call(L, 0, 0);
145
lua_pushcfunction(L, luaopen_math); lua_call(L, 0, 0);
146
lua_pushcfunction(L, luaopen_io); lua_call(L, 0, 0);
147
lua_pushcfunction(L, luaopen_os); lua_call(L, 0, 0);
148
_stackcheck.check_stack(0);
149
// dofile and loadfile are replaced with include
151
lua_setglobal(L, "dofile");
153
lua_setglobal(L, "loadfile");
154
lua_pushcfunction(L, LuaInclude);
155
lua_setglobal(L, "include");
157
// add include_path to the module load path
158
lua_getglobal(L, "package");
159
lua_pushstring(L, "path");
160
lua_pushstring(L, "path");
163
wxStringTokenizer toker(Options.AsText(_T("Automation Include Path")), _T("|"), wxTOKEN_STRTOK);
164
while (toker.HasMoreTokens()) {
165
wxFileName path(StandardPaths::DecodePath(toker.GetNextToken()));
166
if (path.IsOk() && !path.IsRelative() && path.DirExists()) {
167
wxCharBuffer p = path.GetLongPath().utf8_str();
168
lua_pushfstring(L, ";%s?.lua;%s?/init.lua", p.data(), p.data());
175
// Replace the default lua module loader with our utf-8 compatible one
176
lua_getfield(L, -1, "loaders");
177
lua_pushcfunction(L, LuaModuleLoader);
178
lua_rawseti(L, -2, 2);
180
_stackcheck.check_stack(0);
182
// prepare stuff in the registry
183
// reference to the script object
184
lua_pushlightuserdata(L, this);
185
lua_setfield(L, LUA_REGISTRYINDEX, "aegisub");
186
// the "feature" table
187
// integer indexed, using same indexes as "features" vector in the base Script class
189
lua_setfield(L, LUA_REGISTRYINDEX, "features");
190
_stackcheck.check_stack(0);
192
// make "aegisub" table
193
lua_pushstring(L, "aegisub");
195
// aegisub.register_macro
196
lua_pushcfunction(L, LuaFeatureMacro::LuaRegister);
197
lua_setfield(L, -2, "register_macro");
198
// aegisub.register_filter
199
lua_pushcfunction(L, LuaFeatureFilter::LuaRegister);
200
lua_setfield(L, -2, "register_filter");
201
// aegisub.text_extents
202
lua_pushcfunction(L, LuaTextExtents);
203
lua_setfield(L, -2, "text_extents");
205
lua_pushcfunction(L, LuaFrameFromMs);
206
lua_setfield(L, -2, "frame_from_ms");
207
lua_pushcfunction(L, LuaMsFromFrame);
208
lua_setfield(L, -2, "ms_from_frame");
209
lua_pushcfunction(L, LuaVideoSize);
210
lua_setfield(L, -2, "video_size");
211
// aegisub.lua_automation_version
212
lua_pushinteger(L, 4);
213
lua_setfield(L, -2, "lua_automation_version");
214
// store aegisub table to globals
215
lua_settable(L, LUA_GLOBALSINDEX);
216
_stackcheck.check_stack(0);
219
LuaScriptReader script_reader(GetFilename());
220
if (lua_load(L, script_reader.reader_func, &script_reader, GetPrettyFilename().mb_str(wxConvUTF8))) {
221
wxString err(lua_tostring(L, -1), wxConvUTF8);
222
err.Prepend(_T("Error loading Lua script \"") + GetPrettyFilename() + _T("\":\n\n"));
225
_stackcheck.check_stack(1);
227
// this is where features are registered
228
// don't thread this, as there's no point in it and it seems to break on wx 2.8.3, for some reason
229
if (lua_pcall(L, 0, 0, 0)) {
230
// error occurred, assumed to be on top of Lua stack
231
wxString err(lua_tostring(L, -1), wxConvUTF8);
232
err.Prepend(_T("Error initialising Lua script \"") + GetPrettyFilename() + _T("\":\n\n"));
235
_stackcheck.check_stack(0);
236
lua_getglobal(L, "version");
237
if (lua_isnumber(L, -1)) {
238
if (lua_tointeger(L, -1) == 3) {
239
lua_pop(L, 1); // just to avoid tripping the stackcheck in debug
240
// So this is an auto3 script...
241
// Throw it as an exception, the script factory manager will catch this and use the auto3 script instead of this script object
242
throw _T("Attempted to load an Automation 3 script as an Automation 4 Lua script. Automation 3 is no longer supported.");
245
lua_getglobal(L, "script_name");
246
if (lua_isstring(L, -1)) {
247
name = wxString(lua_tostring(L, -1), wxConvUTF8);
249
name = GetPrettyFilename();
251
lua_getglobal(L, "script_description");
252
if (lua_isstring(L, -1)) {
253
description = wxString(lua_tostring(L, -1), wxConvUTF8);
255
lua_getglobal(L, "script_author");
256
if (lua_isstring(L, -1)) {
257
author = wxString(lua_tostring(L, -1), wxConvUTF8);
259
lua_getglobal(L, "script_version");
260
if (lua_isstring(L, -1)) {
261
version = wxString(lua_tostring(L, -1), wxConvUTF8);
264
// if we got this far, the script should be ready
265
_stackcheck.check_stack(0);
268
catch (const char *e) {
271
name = GetPrettyFilename();
272
description = wxString(e, wxConvUTF8);
274
catch (const wchar_t *e) {
277
name = GetPrettyFilename();
280
catch (const wxString& e) {
283
name = GetPrettyFilename();
287
// Be sure to properly propagate any scripts throw
293
name = GetPrettyFilename();
294
description = _T("Unknown error initialising Lua script");
298
void LuaScript::Destroy()
300
// Assume the script object is clean if there's no Lua state
304
for (int i = 0; i < (int)features.size(); i++) {
305
Feature *f = features[i];
310
// delete environment
317
void LuaScript::Reload()
323
LuaScript* LuaScript::GetScriptObject(lua_State *L)
325
lua_getfield(L, LUA_REGISTRYINDEX, "aegisub");
326
void *ptr = lua_touserdata(L, -1);
328
return (LuaScript*)ptr;
331
int LuaScript::LuaTextExtents(lua_State *L)
333
if (!lua_istable(L, 1)) {
334
lua_pushstring(L, "First argument to text_extents must be a table");
337
if (!lua_isstring(L, 2)) {
338
lua_pushstring(L, "Second argument to text_extents must be a string");
343
AssEntry *et = LuaAssFile::LuaToAssEntry(L);
344
AssStyle *st = dynamic_cast<AssStyle*>(et);
347
delete et; // Make sure to delete the "live" pointer
348
lua_pushstring(L, "Not a style entry");
352
wxString text(lua_tostring(L, 2), wxConvUTF8);
354
double width, height, descent, extlead;
355
if (!CalculateTextExtents(st, text, width, height, descent, extlead)) {
357
lua_pushstring(L, "Some internal error occurred calculating text_extents");
362
lua_pushnumber(L, width);
363
lua_pushnumber(L, height);
364
lua_pushnumber(L, descent);
365
lua_pushnumber(L, extlead);
369
/// @brief Module loader which uses our include rather than Lua's, for unicode file support
370
/// @param L The Lua state
371
/// @return Always 1 per loader_Lua?
372
int LuaScript::LuaModuleLoader(lua_State *L)
374
int pretop = lua_gettop(L);
375
wxString module(lua_tostring(L, -1), wxConvUTF8);
376
module.Replace(L".", _T(LUA_DIRSEP));
378
lua_getglobal(L, "package");
379
lua_pushstring(L, "path");
381
wxString package_paths(lua_tostring(L, -1), wxConvUTF8);
384
wxStringTokenizer toker(package_paths, L";", wxTOKEN_STRTOK);
385
while (toker.HasMoreTokens()) {
386
wxString filename = toker.GetNextToken();
387
filename.Replace(L"?", module);
388
if (wxFileName::FileExists(filename)) {
389
LuaScriptReader script_reader(filename);
390
if (lua_load(L, script_reader.reader_func, &script_reader, filename.utf8_str())) {
391
lua_pushfstring(L, "Error loading Lua module \"%s\":\n\n%s", filename.utf8_str().data(), lua_tostring(L, -1));
393
return lua_gettop(L) - pretop;
397
return lua_gettop(L) - pretop;
404
int LuaScript::LuaInclude(lua_State *L)
406
LuaScript *s = GetScriptObject(L);
408
if (!lua_isstring(L, 1)) {
409
lua_pushstring(L, "Argument to include must be a string");
413
wxString fnames(lua_tostring(L, 1), wxConvUTF8);
415
wxFileName fname(fnames);
416
if (fname.GetDirCount() == 0) {
418
fname = s->include_path.FindAbsoluteValidPath(fnames);
419
} else if (fname.IsRelative()) {
421
wxFileName sfname(s->GetFilename());
422
fname.MakeAbsolute(sfname.GetPath(true));
424
// absolute path, do nothing
426
if (!fname.IsOk() || !fname.FileExists()) {
427
lua_pushfstring(L, "Lua include not found: %s", fnames.mb_str(wxConvUTF8).data());
431
LuaScriptReader script_reader(fname.GetFullPath());
432
if (lua_load(L, script_reader.reader_func, &script_reader, fname.GetFullName().mb_str(wxConvUTF8))) {
433
lua_pushfstring(L, "Error loading Lua include \"%s\":\n\n%s", fname.GetFullPath().mb_str(wxConvUTF8).data(), lua_tostring(L, -1));
437
int pretop = lua_gettop(L) - 1; // don't count the function value itself
438
lua_call(L, 0, LUA_MULTRET);
439
return lua_gettop(L) - pretop;
442
int LuaScript::LuaFrameFromMs(lua_State *L)
444
int ms = (int)lua_tonumber(L, -1);
446
if (VFR_Output.IsLoaded()) {
447
lua_pushnumber(L, VFR_Output.GetFrameAtTime(ms, true));
455
int LuaScript::LuaMsFromFrame(lua_State *L)
457
int frame = (int)lua_tonumber(L, -1);
459
if (VFR_Output.IsLoaded()) {
460
lua_pushnumber(L, VFR_Output.GetTimeAtFrame(frame, true));
468
int LuaScript::LuaVideoSize(lua_State *L)
470
VideoContext *ctx = VideoContext::Get();
471
if (ctx->IsLoaded()) {
472
lua_pushnumber(L, ctx->GetWidth());
473
lua_pushnumber(L, ctx->GetHeight());
474
lua_pushnumber(L, ctx->GetAspectRatioValue());
475
lua_pushnumber(L, ctx->GetAspectRatioType());
486
LuaThreadedCall::LuaThreadedCall(lua_State *_L, int _nargs, int _nresults)
487
: wxThread(wxTHREAD_JOINABLE)
490
, nresults(_nresults)
492
int prio = Options.AsInt(_T("Automation Thread Priority"));
493
if (prio == 0) prio = 50; // normal
494
else if (prio == 1) prio = 30; // below normal
495
else if (prio == 2) prio = 10; // lowest
496
else prio = 50; // fallback normal
502
wxThread::ExitCode LuaThreadedCall::Entry()
504
int result = lua_pcall(L, nargs, nresults, 0);
506
// see if there's a progress sink window to close
507
lua_getfield(L, LUA_REGISTRYINDEX, "progress_sink");
508
if (lua_isuserdata(L, -1)) {
509
LuaProgressSink *ps = LuaProgressSink::GetObjPointer(L, -1);
512
// if the call failed, log the error here
513
wxString errmsg(lua_tostring(L, -2), wxConvUTF8);
514
ps->AddDebugOutput(_T("\n\nLua reported a runtime error:\n"));
515
ps->AddDebugOutput(errmsg);
519
// don't bother protecting this with a mutex, it should be safe enough like this
520
ps->script_finished = true;
521
// tell wx to run its idle-events now, just to make the progress window notice earlier that we're done
526
lua_gc(L, LUA_GCCOLLECT, 0);
527
if (result) return (wxThread::ExitCode) 1;
534
LuaFeature::LuaFeature(lua_State *_L, ScriptFeatureClass _featureclass, const wxString &_name)
535
: Feature(_featureclass, _name)
540
void LuaFeature::RegisterFeature()
542
// get the LuaScript objects
543
lua_getfield(L, LUA_REGISTRYINDEX, "aegisub");
544
LuaScript *s = (LuaScript*)lua_touserdata(L, -1);
547
// add the Feature object
548
s->features.push_back(this);
550
// get the index+1 it was pushed into
551
myid = (int)s->features.size()-1;
553
// create table with the functions
554
// get features table
555
lua_getfield(L, LUA_REGISTRYINDEX, "features");
556
lua_pushvalue(L, -2);
557
lua_rawseti(L, -2, myid);
561
void LuaFeature::GetFeatureFunction(int functionid)
564
lua_getfield(L, LUA_REGISTRYINDEX, "features");
565
// get this feature's function pointers
566
lua_rawgeti(L, -1, myid);
567
// get pointer for validation function
568
lua_rawgeti(L, -1, functionid);
573
void LuaFeature::CreateIntegerArray(const std::vector<int> &ints)
575
// create an array-style table with an integer vector in it
576
// leave the new table on top of the stack
578
for (size_t i = 0; i != ints.size(); ++i) {
579
// We use zero-based indexing but Lua wants one-based, so add one
580
lua_pushinteger(L, ints[i] + 1);
581
lua_rawseti(L, -2, (int)i+1);
585
void LuaFeature::ThrowError()
587
wxString err(lua_tostring(L, -1), wxConvUTF8);
595
int LuaFeatureMacro::LuaRegister(lua_State *L)
597
wxString _name(lua_tostring(L, 1), wxConvUTF8);
598
wxString _description(lua_tostring(L, 2), wxConvUTF8);
600
LuaFeatureMacro *macro = new LuaFeatureMacro(_name, _description, L);
606
LuaFeatureMacro::LuaFeatureMacro(const wxString &_name, const wxString &_description, lua_State *_L)
607
: Feature(SCRIPTFEATURE_MACRO, _name)
608
, FeatureMacro(_name, _description)
609
, LuaFeature(_L, SCRIPTFEATURE_MACRO, _name)
611
// new table for containing the functions for this feature
613
// store processing function
614
if (!lua_isfunction(L, 3)) {
615
lua_pushstring(L, "The macro processing function must be a function");
619
lua_rawseti(L, -2, 1);
620
// and validation function
622
no_validate = !lua_isfunction(L, -1);
623
lua_rawseti(L, -2, 2);
624
// make the feature known
626
// and remove the feature function table again
630
bool LuaFeatureMacro::Validate(AssFile *subs, const std::vector<int> &selected, int active)
635
GetFeatureFunction(2); // 2 = validation function
637
// prepare function call
638
LuaAssFile *subsobj = new LuaAssFile(L, subs, false, false);
640
CreateIntegerArray(selected); // selected items
641
lua_pushinteger(L, -1); // active line
644
LuaThreadedCall call(L, 3, 1);
645
wxThread::ExitCode code = call.Wait();
648
bool result = !!lua_toboolean(L, -1);
656
void LuaFeatureMacro::Process(AssFile *subs, std::vector<int> &selected, int active, wxWindow * const progress_parent)
658
GetFeatureFunction(1); // 1 = processing function
660
// prepare function call
661
LuaAssFile *subsobj = new LuaAssFile(L, subs, true, true);
663
CreateIntegerArray(selected); // selected items
664
lua_pushinteger(L, -1); // active line
666
LuaProgressSink *ps = new LuaProgressSink(L, progress_parent);
667
ps->SetTitle(GetName());
670
// 3 args: subtitles, selected lines, active line
671
// 1 result: new selected lines
672
LuaThreadedCall call(L, 3, 1);
675
wxThread::ExitCode code = call.Wait();
676
(void) code; // ignore
677
//if (code) ThrowError();
679
// top of stack will be selected lines array, if any was returned
680
if (lua_istable(L, -1)) {
682
selected.reserve(lua_objlen(L, -1));
684
while (lua_next(L, -2)) {
685
if (lua_isnumber(L, -1)) {
686
// Lua uses one-based indexing but we want zero-based, so subtract one
687
selected.push_back(lua_tointeger(L, -1) - 1);
691
std::sort(selected.begin(), selected.end());
693
// either way, there will be something on the stack
702
LuaFeatureFilter::LuaFeatureFilter(const wxString &_name, const wxString &_description, int merit, lua_State *_L)
703
: Feature(SCRIPTFEATURE_FILTER, _name)
704
, FeatureFilter(_name, _description, merit)
705
, LuaFeature(_L, SCRIPTFEATURE_FILTER, _name)
707
// Works the same as in LuaFeatureMacro
709
if (!lua_isfunction(L, 4)) {
710
lua_pushstring(L, "The filter processing function must be a function");
714
lua_rawseti(L, -2, 1);
716
has_config = lua_isfunction(L, -1);
717
lua_rawseti(L, -2, 2);
722
void LuaFeatureFilter::Init()
724
// Don't think there's anything to do here... (empty in auto3)
727
int LuaFeatureFilter::LuaRegister(lua_State *L)
729
wxString _name(lua_tostring(L, 1), wxConvUTF8);
730
wxString _description(lua_tostring(L, 2), wxConvUTF8);
731
int _merit = lua_tointeger(L, 3);
733
LuaFeatureFilter *filter = new LuaFeatureFilter(_name, _description, _merit, L);
739
void LuaFeatureFilter::ProcessSubs(AssFile *subs, wxWindow *export_dialog)
741
LuaStackcheck stackcheck(L);
743
GetFeatureFunction(1); // 1 = processing function
744
assert(lua_isfunction(L, -1));
745
stackcheck.check_stack(1);
747
// prepare function call
748
// subtitles (undo doesn't make sense in exported subs, in fact it'll totally break the undo system)
749
LuaAssFile *subsobj = new LuaAssFile(L, subs, true/*allow modifications*/, false/*disallow undo*/);
750
assert(lua_isuserdata(L, -1));
751
stackcheck.check_stack(2);
753
if (has_config && config_dialog) {
754
int results_produced = config_dialog->LuaReadBack(L);
755
assert(results_produced == 1);
756
(void) results_produced; // avoid warning on release builds
757
// TODO, write back stored options here
759
// no config so put an empty table instead
762
assert(lua_istable(L, -1));
763
stackcheck.check_stack(3);
765
LuaProgressSink *ps = new LuaProgressSink(L, export_dialog, false);
766
ps->SetTitle(GetName());
767
stackcheck.check_stack(3);
770
LuaThreadedCall call(L, 2, 0);
773
wxThread::ExitCode code = call.Wait();
775
//if (code) ThrowError();
777
stackcheck.check_stack(0);
779
// Just ensure that subsobj survives until here
785
ScriptConfigDialog* LuaFeatureFilter::GenerateConfigDialog(wxWindow *parent)
790
GetFeatureFunction(2); // 2 = config dialog function
792
// prepare function call
793
// subtitles (don't allow any modifications during dialog creation, ideally the subs aren't even accessed)
794
LuaAssFile *subsobj = new LuaAssFile(L, AssFile::top, false/*allow modifications*/, false/*disallow undo*/);
797
lua_newtable(L); // TODO, nothing for now
799
LuaProgressSink *ps = new LuaProgressSink(L, 0, false);
800
ps->SetTitle(GetName());
803
LuaThreadedCall call(L, 2, 1);
806
wxThread::ExitCode code = call.Wait();
812
// The config dialog table should now be on stack as LuaConfigDialog constructor expects
813
return config_dialog = new LuaConfigDialog(L, false);
815
return config_dialog = 0;
822
LuaProgressSink::LuaProgressSink(lua_State *_L, wxWindow *parent, bool allow_config_dialog)
823
: ProgressSink(parent)
826
LuaProgressSink **ud = (LuaProgressSink**)lua_newuserdata(L, sizeof(LuaProgressSink*));
829
// register progress reporting stuff
830
lua_getglobal(L, "aegisub");
833
lua_pushvalue(L, -3);
834
lua_pushcclosure(L, LuaSetProgress, 1);
835
lua_setfield(L, -2, "set");
837
lua_pushvalue(L, -3);
838
lua_pushcclosure(L, LuaSetTask, 1);
839
lua_setfield(L, -2, "task");
841
lua_pushvalue(L, -3);
842
lua_pushcclosure(L, LuaSetTitle, 1);
843
lua_setfield(L, -2, "title");
845
lua_pushvalue(L, -3);
846
lua_pushcclosure(L, LuaGetCancelled, 1);
847
lua_setfield(L, -2, "is_cancelled");
849
lua_setfield(L, -2, "progress");
852
lua_pushvalue(L, -3);
853
lua_pushcclosure(L, LuaDebugOut, 1);
854
lua_setfield(L, -2, "out");
855
lua_setfield(L, -2, "debug");
856
lua_pushvalue(L, -2);
857
lua_pushcclosure(L, LuaDebugOut, 1);
858
lua_setfield(L, -2, "log");
860
if (allow_config_dialog) {
862
lua_pushvalue(L, -3);
863
lua_pushcclosure(L, LuaDisplayDialog, 1);
864
lua_setfield(L, -2, "display");
865
lua_setfield(L, -2, "dialog");
868
// reference so other objects can also find the progress sink
869
lua_pushvalue(L, -2);
870
lua_setfield(L, LUA_REGISTRYINDEX, "progress_sink");
875
LuaProgressSink::~LuaProgressSink()
877
// remove progress reporting stuff
878
lua_getglobal(L, "aegisub");
880
lua_setfield(L, -2, "progress");
882
lua_setfield(L, -2, "debug");
885
lua_setfield(L, LUA_REGISTRYINDEX, "progress_sink");
888
LuaProgressSink* LuaProgressSink::GetObjPointer(lua_State *L, int idx)
890
assert(lua_type(L, idx) == LUA_TUSERDATA);
891
void *ud = lua_touserdata(L, idx);
892
return *((LuaProgressSink**)ud);
895
int LuaProgressSink::LuaSetProgress(lua_State *L)
897
LuaProgressSink *ps = GetObjPointer(L, lua_upvalueindex(1));
898
float progress = lua_tonumber(L, 1);
899
ps->SetProgress(progress);
903
int LuaProgressSink::LuaSetTask(lua_State *L)
905
LuaProgressSink *ps = GetObjPointer(L, lua_upvalueindex(1));
906
wxString task(lua_tostring(L, 1), wxConvUTF8);
911
int LuaProgressSink::LuaSetTitle(lua_State *L)
913
LuaProgressSink *ps = GetObjPointer(L, lua_upvalueindex(1));
914
wxString title(lua_tostring(L, 1), wxConvUTF8);
919
int LuaProgressSink::LuaGetCancelled(lua_State *L)
921
LuaProgressSink *ps = GetObjPointer(L, lua_upvalueindex(1));
922
lua_pushboolean(L, ps->cancelled);
926
int LuaProgressSink::LuaDebugOut(lua_State *L)
928
LuaProgressSink *ps = GetObjPointer(L, lua_upvalueindex(1));
931
if (lua_isnumber(L, 1)) {
932
int level = lua_tointeger(L, 1);
933
if (level > ps->trace_level)
935
// remove trace level
939
// Only do format-string handling if there's more than one argument left
940
// (If there's more than one argument left, assume first is a format string and rest are format arguments)
941
if (lua_gettop(L) > 1) {
943
lua_getglobal(L, "string");
944
lua_getfield(L, -1, "format");
945
// Here stack contains format string, format arguments, 'string' table, format function
946
// remove 'string' table
948
// put the format function into place
950
// call format function
951
lua_call(L, lua_gettop(L)-1, 1);
954
// Top of stack is now a string to output
955
wxString msg(lua_tostring(L, 1), wxConvUTF8);
956
ps->AddDebugOutput(msg);
960
int LuaProgressSink::LuaDisplayDialog(lua_State *L)
962
LuaProgressSink *ps = GetObjPointer(L, lua_upvalueindex(1));
964
// Check that two arguments were actually given
965
// If only one, add another empty table for buttons
966
if (lua_gettop(L) == 1) {
969
// If more than two, remove the excess
970
if (lua_gettop(L) > 2) {
974
// Send the "show dialog" event
975
// See comments in auto4_base.h for more info on this synchronisation
976
ShowConfigDialogEvent evt;
978
LuaConfigDialog dlg(L, true); // magically creates the config dialog structure etc
979
evt.config_dialog = &dlg;
981
wxSemaphore sema(0, 1);
982
evt.sync_sema = &sema;
984
ps->AddPendingEvent(evt);
988
// more magic: puts two values on stack: button pushed and table with control results
989
return dlg.LuaReadBack(L);
993
LuaScriptFactory::LuaScriptFactory() {}
994
LuaScriptFactory::~LuaScriptFactory() {}
996
void LuaScriptFactory::RegisterFactory ()
998
engine_name = _T("Lua");
999
filename_pattern = _T("*.lua");
1003
Script* LuaScriptFactory::Produce(const wxString &filename) const
1005
// Just check if file extension is .lua
1006
// Reject anything else
1007
if (filename.Right(4).Lower() == _T(".lua")) {
1008
return new LuaScript(filename);
1016
#endif // WITH_AUTO4_LUA