~ubuntu-branches/ubuntu/trusty/aegisub/trusty

« back to all changes in this revision

Viewing changes to src/auto4_lua.cpp

  • Committer: Package Import Robot
  • Author(s): Sebastian Reichel
  • Date: 2012-03-16 22:58:00 UTC
  • Revision ID: package-import@ubuntu.com-20120316225800-yfb8h9e5n04rk46a
Tags: upstream-2.1.9
ImportĀ upstreamĀ versionĀ 2.1.9

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
// Copyright (c) 2006, 2007, Niels Martin Hansen
 
2
// All rights reserved.
 
3
//
 
4
// Redistribution and use in source and binary forms, with or without
 
5
// modification, are permitted provided that the following conditions are met:
 
6
//
 
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.
 
15
//
 
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.
 
27
//
 
28
// -----------------------------------------------------------------------------
 
29
//
 
30
// AEGISUB
 
31
//
 
32
// Website: http://aegisub.cellosoft.com
 
33
// Contact: mailto:jiifurusu@gmail.com
 
34
//
 
35
 
 
36
#include "config.h"
 
37
 
 
38
#ifdef WITH_AUTO4_LUA
 
39
 
 
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"
 
45
#include "ass_file.h"
 
46
#include "ass_override.h"
 
47
#include "standard_paths.h"
 
48
#include "text_file_reader.h"
 
49
#include "options.h"
 
50
 
 
51
#include "vfr.h"
 
52
#include "video_context.h"
 
53
 
 
54
#ifdef __WINDOWS__
 
55
#include "../../contrib/lua51/src/lualib.h"
 
56
#include "../../contrib/lua51/src/lauxlib.h"
 
57
#else
 
58
#include "lualib.h"
 
59
#include "lauxlib.h"
 
60
#endif
 
61
 
 
62
#include <wx/msgdlg.h>
 
63
#include <wx/filename.h>
 
64
#include <wx/filefn.h>
 
65
#include <wx/window.h>
 
66
#include <assert.h>
 
67
#include <algorithm>
 
68
 
 
69
namespace Automation4 {
 
70
 
 
71
        // LuaStackcheck
 
72
 
 
73
#if 0
 
74
        struct LuaStackcheck {
 
75
                lua_State *L;
 
76
                int startstack;
 
77
                void check_stack(int additional)
 
78
                {
 
79
                        int top = lua_gettop(L);
 
80
                        if (top - additional != startstack) {
 
81
                                wxLogDebug(_T("Lua stack size mismatch."));
 
82
                                dump();
 
83
                                assert(top - additional == startstack);
 
84
                        }
 
85
                }
 
86
                void dump()
 
87
                {
 
88
                        int top = lua_gettop(L);
 
89
                        wxLogDebug(_T("Dumping Lua stack..."));
 
90
                        for (int i = top; i > 0; i--) {
 
91
                                lua_pushvalue(L, 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));
 
95
                                } else {
 
96
                                        wxLogDebug(type);
 
97
                                }
 
98
                                lua_pop(L, 1);
 
99
                        }
 
100
                        wxLogDebug(_T("--- end dump"));
 
101
                }
 
102
                LuaStackcheck(lua_State *_L) : L(_L) { startstack = lua_gettop(L); }
 
103
                ~LuaStackcheck() { check_stack(0); }
 
104
        };
 
105
#else
 
106
        struct LuaStackcheck {
 
107
                void check_stack(int additional) { }
 
108
                void dump() { }
 
109
                LuaStackcheck(lua_State *L) { }
 
110
                ~LuaStackcheck() { }
 
111
        };
 
112
#endif
 
113
 
 
114
 
 
115
        // LuaScript
 
116
 
 
117
        LuaScript::LuaScript(const wxString &filename)
 
118
                : Script(filename)
 
119
                , L(0)
 
120
        {
 
121
                Create();
 
122
        }
 
123
 
 
124
        LuaScript::~LuaScript()
 
125
        {
 
126
                if (L) Destroy();
 
127
        }
 
128
 
 
129
        void LuaScript::Create()
 
130
        {
 
131
                Destroy();
 
132
 
 
133
                loaded = true;
 
134
 
 
135
                try {
 
136
                        // create lua environment
 
137
                        L = lua_open();
 
138
                        LuaStackcheck _stackcheck(L);
 
139
 
 
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
 
150
                        lua_pushnil(L);
 
151
                        lua_setglobal(L, "dofile");
 
152
                        lua_pushnil(L);
 
153
                        lua_setglobal(L, "loadfile");
 
154
                        lua_pushcfunction(L, LuaInclude);
 
155
                        lua_setglobal(L, "include");
 
156
 
 
157
                        // add include_path to the module load path
 
158
                        lua_getglobal(L, "package");
 
159
                        lua_pushstring(L, "path");
 
160
                        lua_pushstring(L, "path");
 
161
                        lua_gettable(L, -3);
 
162
 
 
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());
 
169
                                        lua_concat(L, 2);
 
170
                                }
 
171
                        }
 
172
 
 
173
                        lua_settable(L, -3);
 
174
 
 
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);
 
179
                        lua_pop(L, 2);
 
180
                        _stackcheck.check_stack(0);
 
181
 
 
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
 
188
                        lua_newtable(L);
 
189
                        lua_setfield(L, LUA_REGISTRYINDEX, "features");
 
190
                        _stackcheck.check_stack(0);
 
191
 
 
192
                        // make "aegisub" table
 
193
                        lua_pushstring(L, "aegisub");
 
194
                        lua_newtable(L);
 
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");
 
204
                        // VFR handling
 
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);
 
217
 
 
218
                        // load user script
 
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"));
 
223
                                throw err;
 
224
                        }
 
225
                        _stackcheck.check_stack(1);
 
226
                        // and execute it
 
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"));
 
233
                                throw err;
 
234
                        }
 
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.");
 
243
                                }
 
244
                        }
 
245
                        lua_getglobal(L, "script_name");
 
246
                        if (lua_isstring(L, -1)) {
 
247
                                name = wxString(lua_tostring(L, -1), wxConvUTF8);
 
248
                        } else {
 
249
                                name = GetPrettyFilename();
 
250
                        }
 
251
                        lua_getglobal(L, "script_description");
 
252
                        if (lua_isstring(L, -1)) {
 
253
                                description = wxString(lua_tostring(L, -1), wxConvUTF8);
 
254
                        }
 
255
                        lua_getglobal(L, "script_author");
 
256
                        if (lua_isstring(L, -1)) {
 
257
                                author = wxString(lua_tostring(L, -1), wxConvUTF8);
 
258
                        }
 
259
                        lua_getglobal(L, "script_version");
 
260
                        if (lua_isstring(L, -1)) {
 
261
                                version = wxString(lua_tostring(L, -1), wxConvUTF8);
 
262
                        }
 
263
                        lua_pop(L, 5);
 
264
                        // if we got this far, the script should be ready
 
265
                        _stackcheck.check_stack(0);
 
266
 
 
267
                }
 
268
                catch (const char *e) {
 
269
                        Destroy();
 
270
                        loaded = false;
 
271
                        name = GetPrettyFilename();
 
272
                        description = wxString(e, wxConvUTF8);
 
273
                }
 
274
                catch (const wchar_t *e) {
 
275
                        Destroy();
 
276
                        loaded = false;
 
277
                        name = GetPrettyFilename();
 
278
                        description = e;
 
279
                }
 
280
                catch (const wxString& e) {
 
281
                        Destroy();
 
282
                        loaded = false;
 
283
                        name = GetPrettyFilename();
 
284
                        description = e;
 
285
                }
 
286
                catch (Script *s) {
 
287
                        // Be sure to properly propagate any scripts throw
 
288
                        throw s;
 
289
                }
 
290
                catch (...) {
 
291
                        Destroy();
 
292
                        loaded = false;
 
293
                        name = GetPrettyFilename();
 
294
                        description = _T("Unknown error initialising Lua script");
 
295
                }
 
296
        }
 
297
 
 
298
        void LuaScript::Destroy()
 
299
        {
 
300
                // Assume the script object is clean if there's no Lua state
 
301
                if (!L) return;
 
302
 
 
303
                // remove features
 
304
                for (int i = 0; i < (int)features.size(); i++) {
 
305
                        Feature *f = features[i];
 
306
                        delete f;
 
307
                }
 
308
                features.clear();
 
309
 
 
310
                // delete environment
 
311
                lua_close(L);
 
312
                L = 0;
 
313
 
 
314
                loaded = false;
 
315
        }
 
316
 
 
317
        void LuaScript::Reload()
 
318
        {
 
319
                Destroy();
 
320
                Create();
 
321
        }
 
322
 
 
323
        LuaScript* LuaScript::GetScriptObject(lua_State *L)
 
324
        {
 
325
                lua_getfield(L, LUA_REGISTRYINDEX, "aegisub");
 
326
                void *ptr = lua_touserdata(L, -1);
 
327
                lua_pop(L, 1);
 
328
                return (LuaScript*)ptr;
 
329
        }
 
330
 
 
331
        int LuaScript::LuaTextExtents(lua_State *L)
 
332
        {
 
333
                if (!lua_istable(L, 1)) {
 
334
                        lua_pushstring(L, "First argument to text_extents must be a table");
 
335
                        lua_error(L);
 
336
                }
 
337
                if (!lua_isstring(L, 2)) {
 
338
                        lua_pushstring(L, "Second argument to text_extents must be a string");
 
339
                        lua_error(L);
 
340
                }
 
341
 
 
342
                lua_pushvalue(L, 1);
 
343
                AssEntry *et = LuaAssFile::LuaToAssEntry(L);
 
344
                AssStyle *st = dynamic_cast<AssStyle*>(et);
 
345
                lua_pop(L, 1);
 
346
                if (!st) {
 
347
                        delete et; // Make sure to delete the "live" pointer
 
348
                        lua_pushstring(L, "Not a style entry");
 
349
                        lua_error(L);
 
350
                }
 
351
 
 
352
                wxString text(lua_tostring(L, 2), wxConvUTF8);
 
353
 
 
354
                double width, height, descent, extlead;
 
355
                if (!CalculateTextExtents(st, text, width, height, descent, extlead)) {
 
356
                        delete st;
 
357
                        lua_pushstring(L, "Some internal error occurred calculating text_extents");
 
358
                        lua_error(L);
 
359
                }
 
360
                delete st;
 
361
 
 
362
                lua_pushnumber(L, width);
 
363
                lua_pushnumber(L, height);
 
364
                lua_pushnumber(L, descent);
 
365
                lua_pushnumber(L, extlead);
 
366
                return 4;
 
367
        }
 
368
 
 
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)
 
373
        {
 
374
                int pretop = lua_gettop(L);
 
375
                wxString module(lua_tostring(L, -1), wxConvUTF8);
 
376
                module.Replace(L".", _T(LUA_DIRSEP));
 
377
 
 
378
                lua_getglobal(L, "package");
 
379
                lua_pushstring(L, "path");
 
380
                lua_gettable(L, -2);
 
381
                wxString package_paths(lua_tostring(L, -1), wxConvUTF8);
 
382
                lua_pop(L, 2);
 
383
 
 
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));
 
392
                                        lua_error(L);
 
393
                                        return lua_gettop(L) - pretop;
 
394
                                }
 
395
                        }
 
396
                }
 
397
                return lua_gettop(L) - pretop;
 
398
        }
 
399
 
 
400
        /// @brief DOCME
 
401
        /// @param L 
 
402
        /// @return 
 
403
        ///
 
404
        int LuaScript::LuaInclude(lua_State *L)
 
405
        {
 
406
                LuaScript *s = GetScriptObject(L);
 
407
 
 
408
                if (!lua_isstring(L, 1)) {
 
409
                        lua_pushstring(L, "Argument to include must be a string");
 
410
                        lua_error(L);
 
411
                        return 0;
 
412
                }
 
413
                wxString fnames(lua_tostring(L, 1), wxConvUTF8);
 
414
 
 
415
                wxFileName fname(fnames);
 
416
                if (fname.GetDirCount() == 0) {
 
417
                        // filename only
 
418
                        fname = s->include_path.FindAbsoluteValidPath(fnames);
 
419
                } else if (fname.IsRelative()) {
 
420
                        // relative path
 
421
                        wxFileName sfname(s->GetFilename());
 
422
                        fname.MakeAbsolute(sfname.GetPath(true));
 
423
                } else {
 
424
                        // absolute path, do nothing
 
425
                }
 
426
                if (!fname.IsOk() || !fname.FileExists()) {
 
427
                        lua_pushfstring(L, "Lua include not found: %s", fnames.mb_str(wxConvUTF8).data());
 
428
                        lua_error(L);
 
429
                }
 
430
 
 
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));
 
434
                        lua_error(L);
 
435
                        return 0;
 
436
                }
 
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;
 
440
        }
 
441
 
 
442
        int LuaScript::LuaFrameFromMs(lua_State *L)
 
443
        {
 
444
                int ms = (int)lua_tonumber(L, -1);
 
445
                lua_pop(L, 1);
 
446
                if (VFR_Output.IsLoaded()) {
 
447
                        lua_pushnumber(L, VFR_Output.GetFrameAtTime(ms, true));
 
448
                        return 1;
 
449
                } else {
 
450
                        lua_pushnil(L);
 
451
                        return 1;
 
452
                }
 
453
        }
 
454
 
 
455
        int LuaScript::LuaMsFromFrame(lua_State *L)
 
456
        {
 
457
                int frame = (int)lua_tonumber(L, -1);
 
458
                lua_pop(L, 1);
 
459
                if (VFR_Output.IsLoaded()) {
 
460
                        lua_pushnumber(L, VFR_Output.GetTimeAtFrame(frame, true));
 
461
                        return 1;
 
462
                } else {
 
463
                        lua_pushnil(L);
 
464
                        return 1;
 
465
                }
 
466
        }
 
467
 
 
468
        int LuaScript::LuaVideoSize(lua_State *L)
 
469
        {
 
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());
 
476
                        return 4;
 
477
                } else {
 
478
                        lua_pushnil(L);
 
479
                        return 1;
 
480
                }
 
481
        }
 
482
 
 
483
 
 
484
        // LuaThreadedCall
 
485
 
 
486
        LuaThreadedCall::LuaThreadedCall(lua_State *_L, int _nargs, int _nresults)
 
487
                : wxThread(wxTHREAD_JOINABLE)
 
488
                , L(_L)
 
489
                , nargs(_nargs)
 
490
                , nresults(_nresults)
 
491
        {
 
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
 
497
                Create();
 
498
                SetPriority(prio);
 
499
                Run();
 
500
        }
 
501
 
 
502
        wxThread::ExitCode LuaThreadedCall::Entry()
 
503
        {
 
504
                int result = lua_pcall(L, nargs, nresults, 0);
 
505
 
 
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);
 
510
 
 
511
                        if (result) {
 
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);
 
516
                                lua_pop(L, 1);
 
517
                        }
 
518
 
 
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
 
522
                        wxWakeUpIdle();
 
523
                }
 
524
                lua_pop(L, 1);
 
525
 
 
526
                lua_gc(L, LUA_GCCOLLECT, 0);
 
527
                if (result) return (wxThread::ExitCode) 1;
 
528
                else return 0;
 
529
        }
 
530
 
 
531
 
 
532
        // LuaFeature
 
533
 
 
534
        LuaFeature::LuaFeature(lua_State *_L, ScriptFeatureClass _featureclass, const wxString &_name)
 
535
                : Feature(_featureclass, _name)
 
536
                , L(_L)
 
537
        {
 
538
        }
 
539
 
 
540
        void LuaFeature::RegisterFeature()
 
541
        {
 
542
                // get the LuaScript objects
 
543
                lua_getfield(L, LUA_REGISTRYINDEX, "aegisub");
 
544
                LuaScript *s = (LuaScript*)lua_touserdata(L, -1);
 
545
                lua_pop(L, 1);
 
546
 
 
547
                // add the Feature object
 
548
                s->features.push_back(this);
 
549
 
 
550
                // get the index+1 it was pushed into
 
551
                myid = (int)s->features.size()-1;
 
552
 
 
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);
 
558
                lua_pop(L, 1);
 
559
        }
 
560
 
 
561
        void LuaFeature::GetFeatureFunction(int functionid)
 
562
        {
 
563
                // get feature table
 
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);
 
569
                lua_remove(L, -2);
 
570
                lua_remove(L, -2);
 
571
        }
 
572
 
 
573
        void LuaFeature::CreateIntegerArray(const std::vector<int> &ints)
 
574
        {
 
575
                // create an array-style table with an integer vector in it
 
576
                // leave the new table on top of the stack
 
577
                lua_newtable(L);
 
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);
 
582
                }
 
583
        }
 
584
 
 
585
        void LuaFeature::ThrowError()
 
586
        {
 
587
                wxString err(lua_tostring(L, -1), wxConvUTF8);
 
588
                lua_pop(L, 1);
 
589
                wxLogError(err);
 
590
        }
 
591
 
 
592
 
 
593
        // LuaFeatureMacro
 
594
 
 
595
        int LuaFeatureMacro::LuaRegister(lua_State *L)
 
596
        {
 
597
                wxString _name(lua_tostring(L, 1), wxConvUTF8);
 
598
                wxString _description(lua_tostring(L, 2), wxConvUTF8);
 
599
 
 
600
                LuaFeatureMacro *macro = new LuaFeatureMacro(_name, _description, L);
 
601
                (void)macro;
 
602
 
 
603
                return 0;
 
604
        }
 
605
 
 
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)
 
610
        {
 
611
                // new table for containing the functions for this feature
 
612
                lua_newtable(L);
 
613
                // store processing function
 
614
                if (!lua_isfunction(L, 3)) {
 
615
                        lua_pushstring(L, "The macro processing function must be a function");
 
616
                        lua_error(L);
 
617
                }
 
618
                lua_pushvalue(L, 3);
 
619
                lua_rawseti(L, -2, 1);
 
620
                // and validation function
 
621
                lua_pushvalue(L, 4);
 
622
                no_validate = !lua_isfunction(L, -1);
 
623
                lua_rawseti(L, -2, 2);
 
624
                // make the feature known
 
625
                RegisterFeature();
 
626
                // and remove the feature function table again
 
627
                lua_pop(L, 1);
 
628
        }
 
629
 
 
630
        bool LuaFeatureMacro::Validate(AssFile *subs, const std::vector<int> &selected, int active)
 
631
        {
 
632
                if (no_validate)
 
633
                        return true;
 
634
 
 
635
                GetFeatureFunction(2);  // 2 = validation function
 
636
 
 
637
                // prepare function call
 
638
                LuaAssFile *subsobj = new LuaAssFile(L, subs, false, false);
 
639
                (void) subsobj;
 
640
                CreateIntegerArray(selected); // selected items
 
641
                lua_pushinteger(L, -1); // active line
 
642
 
 
643
                // do call
 
644
                LuaThreadedCall call(L, 3, 1);
 
645
                wxThread::ExitCode code = call.Wait();
 
646
                (void) code;
 
647
                // get result
 
648
                bool result = !!lua_toboolean(L, -1);
 
649
 
 
650
                // clean up stack
 
651
                lua_pop(L, 1);
 
652
 
 
653
                return result;
 
654
        }
 
655
 
 
656
        void LuaFeatureMacro::Process(AssFile *subs, std::vector<int> &selected, int active, wxWindow * const progress_parent)
 
657
        {
 
658
                GetFeatureFunction(1); // 1 = processing function
 
659
 
 
660
                // prepare function call
 
661
                LuaAssFile *subsobj = new LuaAssFile(L, subs, true, true);
 
662
                (void) subsobj;
 
663
                CreateIntegerArray(selected); // selected items
 
664
                lua_pushinteger(L, -1); // active line
 
665
 
 
666
                LuaProgressSink *ps = new LuaProgressSink(L, progress_parent);
 
667
                ps->SetTitle(GetName());
 
668
 
 
669
                // do call
 
670
                // 3 args: subtitles, selected lines, active line
 
671
                // 1 result: new selected lines
 
672
                LuaThreadedCall call(L, 3, 1);
 
673
 
 
674
                ps->ShowModal();
 
675
                wxThread::ExitCode code = call.Wait();
 
676
                (void) code; // ignore
 
677
                //if (code) ThrowError();
 
678
 
 
679
                // top of stack will be selected lines array, if any was returned
 
680
                if (lua_istable(L, -1)) {
 
681
                        selected.clear();
 
682
                        selected.reserve(lua_objlen(L, -1));
 
683
                        lua_pushnil(L);
 
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);
 
688
                                }
 
689
                                lua_pop(L, 1);
 
690
                        }
 
691
                        std::sort(selected.begin(), selected.end());
 
692
                }
 
693
                // either way, there will be something on the stack
 
694
                lua_pop(L, 1);
 
695
 
 
696
                delete ps;
 
697
        }
 
698
 
 
699
 
 
700
        // LuaFeatureFilter
 
701
 
 
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)
 
706
        {
 
707
                // Works the same as in LuaFeatureMacro
 
708
                lua_newtable(L);
 
709
                if (!lua_isfunction(L, 4)) {
 
710
                        lua_pushstring(L, "The filter processing function must be a function");
 
711
                        lua_error(L);
 
712
                }
 
713
                lua_pushvalue(L, 4);
 
714
                lua_rawseti(L, -2, 1);
 
715
                lua_pushvalue(L, 5);
 
716
                has_config = lua_isfunction(L, -1);
 
717
                lua_rawseti(L, -2, 2);
 
718
                RegisterFeature();
 
719
                lua_pop(L, 1);
 
720
        }
 
721
 
 
722
        void LuaFeatureFilter::Init()
 
723
        {
 
724
                // Don't think there's anything to do here... (empty in auto3)
 
725
        }
 
726
 
 
727
        int LuaFeatureFilter::LuaRegister(lua_State *L)
 
728
        {
 
729
                wxString _name(lua_tostring(L, 1), wxConvUTF8);
 
730
                wxString _description(lua_tostring(L, 2), wxConvUTF8);
 
731
                int _merit = lua_tointeger(L, 3);
 
732
 
 
733
                LuaFeatureFilter *filter = new LuaFeatureFilter(_name, _description, _merit, L);
 
734
                (void) filter;
 
735
 
 
736
                return 0;
 
737
        }
 
738
 
 
739
        void LuaFeatureFilter::ProcessSubs(AssFile *subs, wxWindow *export_dialog)
 
740
        {
 
741
                LuaStackcheck stackcheck(L);
 
742
 
 
743
                GetFeatureFunction(1); // 1 = processing function
 
744
                assert(lua_isfunction(L, -1));
 
745
                stackcheck.check_stack(1);
 
746
 
 
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);
 
752
                // config
 
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
 
758
                } else {
 
759
                        // no config so put an empty table instead
 
760
                        lua_newtable(L);
 
761
                }
 
762
                assert(lua_istable(L, -1));
 
763
                stackcheck.check_stack(3);
 
764
 
 
765
                LuaProgressSink *ps = new LuaProgressSink(L, export_dialog, false);
 
766
                ps->SetTitle(GetName());
 
767
                stackcheck.check_stack(3);
 
768
 
 
769
                // do call
 
770
                LuaThreadedCall call(L, 2, 0);
 
771
 
 
772
                ps->ShowModal();
 
773
                wxThread::ExitCode code = call.Wait();
 
774
                (void) code;
 
775
                //if (code) ThrowError();
 
776
 
 
777
                stackcheck.check_stack(0);
 
778
 
 
779
                // Just ensure that subsobj survives until here
 
780
                (void) subsobj;
 
781
 
 
782
                delete ps;
 
783
        }
 
784
 
 
785
        ScriptConfigDialog* LuaFeatureFilter::GenerateConfigDialog(wxWindow *parent)
 
786
        {
 
787
                if (!has_config)
 
788
                        return 0;
 
789
 
 
790
                GetFeatureFunction(2); // 2 = config dialog function
 
791
 
 
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*/);
 
795
                (void) subsobj;
 
796
                // stored options
 
797
                lua_newtable(L); // TODO, nothing for now
 
798
 
 
799
                LuaProgressSink *ps = new LuaProgressSink(L, 0, false);
 
800
                ps->SetTitle(GetName());
 
801
 
 
802
                // do call
 
803
                LuaThreadedCall call(L, 2, 1);
 
804
 
 
805
                ps->ShowModal();
 
806
                wxThread::ExitCode code = call.Wait();
 
807
 
 
808
                delete ps;
 
809
 
 
810
                
 
811
                if (!code) {
 
812
                        // The config dialog table should now be on stack as LuaConfigDialog constructor expects
 
813
                        return config_dialog = new LuaConfigDialog(L, false);
 
814
                } else {
 
815
                        return config_dialog = 0;
 
816
                }
 
817
        }
 
818
 
 
819
 
 
820
        // LuaProgressSink
 
821
 
 
822
        LuaProgressSink::LuaProgressSink(lua_State *_L, wxWindow *parent, bool allow_config_dialog)
 
823
                : ProgressSink(parent)
 
824
                , L(_L)
 
825
        {
 
826
                LuaProgressSink **ud = (LuaProgressSink**)lua_newuserdata(L, sizeof(LuaProgressSink*));
 
827
                *ud = this;
 
828
 
 
829
                // register progress reporting stuff
 
830
                lua_getglobal(L, "aegisub");
 
831
                lua_newtable(L);
 
832
 
 
833
                lua_pushvalue(L, -3);
 
834
                lua_pushcclosure(L, LuaSetProgress, 1);
 
835
                lua_setfield(L, -2, "set");
 
836
 
 
837
                lua_pushvalue(L, -3);
 
838
                lua_pushcclosure(L, LuaSetTask, 1);
 
839
                lua_setfield(L, -2, "task");
 
840
 
 
841
                lua_pushvalue(L, -3);
 
842
                lua_pushcclosure(L, LuaSetTitle, 1);
 
843
                lua_setfield(L, -2, "title");
 
844
 
 
845
                lua_pushvalue(L, -3);
 
846
                lua_pushcclosure(L, LuaGetCancelled, 1);
 
847
                lua_setfield(L, -2, "is_cancelled");
 
848
 
 
849
                lua_setfield(L, -2, "progress");
 
850
 
 
851
                lua_newtable(L);
 
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");
 
859
 
 
860
                if (allow_config_dialog) {
 
861
                        lua_newtable(L);
 
862
                        lua_pushvalue(L, -3);
 
863
                        lua_pushcclosure(L, LuaDisplayDialog, 1);
 
864
                        lua_setfield(L, -2, "display");
 
865
                        lua_setfield(L, -2, "dialog");
 
866
                }
 
867
 
 
868
                // reference so other objects can also find the progress sink
 
869
                lua_pushvalue(L, -2);
 
870
                lua_setfield(L, LUA_REGISTRYINDEX, "progress_sink");
 
871
 
 
872
                lua_pop(L, 2);
 
873
        }
 
874
 
 
875
        LuaProgressSink::~LuaProgressSink()
 
876
        {
 
877
                // remove progress reporting stuff
 
878
                lua_getglobal(L, "aegisub");
 
879
                lua_pushnil(L);
 
880
                lua_setfield(L, -2, "progress");
 
881
                lua_pushnil(L);
 
882
                lua_setfield(L, -2, "debug");
 
883
                lua_pop(L, 1);
 
884
                lua_pushnil(L);
 
885
                lua_setfield(L, LUA_REGISTRYINDEX, "progress_sink");
 
886
        }
 
887
 
 
888
        LuaProgressSink* LuaProgressSink::GetObjPointer(lua_State *L, int idx)
 
889
        {
 
890
                assert(lua_type(L, idx) == LUA_TUSERDATA);
 
891
                void *ud = lua_touserdata(L, idx);
 
892
                return *((LuaProgressSink**)ud);
 
893
        }
 
894
 
 
895
        int LuaProgressSink::LuaSetProgress(lua_State *L)
 
896
        {
 
897
                LuaProgressSink *ps = GetObjPointer(L, lua_upvalueindex(1));
 
898
                float progress = lua_tonumber(L, 1);
 
899
                ps->SetProgress(progress);
 
900
                return 0;
 
901
        }
 
902
 
 
903
        int LuaProgressSink::LuaSetTask(lua_State *L)
 
904
        {
 
905
                LuaProgressSink *ps = GetObjPointer(L, lua_upvalueindex(1));
 
906
                wxString task(lua_tostring(L, 1), wxConvUTF8);
 
907
                ps->SetTask(task);
 
908
                return 0;
 
909
        }
 
910
 
 
911
        int LuaProgressSink::LuaSetTitle(lua_State *L)
 
912
        {
 
913
                LuaProgressSink *ps = GetObjPointer(L, lua_upvalueindex(1));
 
914
                wxString title(lua_tostring(L, 1), wxConvUTF8);
 
915
                ps->SetTitle(title);
 
916
                return 0;
 
917
        }
 
918
 
 
919
        int LuaProgressSink::LuaGetCancelled(lua_State *L)
 
920
        {
 
921
                LuaProgressSink *ps = GetObjPointer(L, lua_upvalueindex(1));
 
922
                lua_pushboolean(L, ps->cancelled);
 
923
                return 1;
 
924
        }
 
925
 
 
926
        int LuaProgressSink::LuaDebugOut(lua_State *L)
 
927
        {
 
928
                LuaProgressSink *ps = GetObjPointer(L, lua_upvalueindex(1));
 
929
 
 
930
                // Check trace level
 
931
                if (lua_isnumber(L, 1)) {
 
932
                        int level = lua_tointeger(L, 1);
 
933
                        if (level > ps->trace_level)
 
934
                                return 0;
 
935
                        // remove trace level
 
936
                        lua_remove(L, 1);
 
937
                }
 
938
 
 
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) {
 
942
                        // Format the string
 
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
 
947
                        lua_remove(L, -2);
 
948
                        // put the format function into place
 
949
                        lua_insert(L, 1);
 
950
                        // call format function
 
951
                        lua_call(L, lua_gettop(L)-1, 1);
 
952
                }
 
953
 
 
954
                // Top of stack is now a string to output
 
955
                wxString msg(lua_tostring(L, 1), wxConvUTF8);
 
956
                ps->AddDebugOutput(msg);
 
957
                return 0;
 
958
        }
 
959
 
 
960
        int LuaProgressSink::LuaDisplayDialog(lua_State *L)
 
961
        {
 
962
                LuaProgressSink *ps = GetObjPointer(L, lua_upvalueindex(1));
 
963
 
 
964
                // Check that two arguments were actually given
 
965
                // If only one, add another empty table for buttons
 
966
                if (lua_gettop(L) == 1) {
 
967
                        lua_newtable(L);
 
968
                }
 
969
                // If more than two, remove the excess
 
970
                if (lua_gettop(L) > 2) {
 
971
                        lua_settop(L, 2);
 
972
                }
 
973
 
 
974
                // Send the "show dialog" event
 
975
                // See comments in auto4_base.h for more info on this synchronisation
 
976
                ShowConfigDialogEvent evt;
 
977
 
 
978
                LuaConfigDialog dlg(L, true); // magically creates the config dialog structure etc
 
979
                evt.config_dialog = &dlg;
 
980
 
 
981
                wxSemaphore sema(0, 1);
 
982
                evt.sync_sema = &sema;
 
983
 
 
984
                ps->AddPendingEvent(evt);
 
985
 
 
986
                sema.Wait();
 
987
 
 
988
                // more magic: puts two values on stack: button pushed and table with control results
 
989
                return dlg.LuaReadBack(L);
 
990
        }
 
991
 
 
992
        // Factory methods
 
993
        LuaScriptFactory::LuaScriptFactory() {}
 
994
        LuaScriptFactory::~LuaScriptFactory() {}
 
995
 
 
996
        void LuaScriptFactory::RegisterFactory ()
 
997
        {
 
998
                engine_name = _T("Lua");
 
999
                filename_pattern = _T("*.lua");
 
1000
                Register(this);
 
1001
        }
 
1002
 
 
1003
        Script* LuaScriptFactory::Produce(const wxString &filename) const
 
1004
        {
 
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);
 
1009
                } else {
 
1010
                        return 0;
 
1011
                }
 
1012
        }
 
1013
 
 
1014
};
 
1015
 
 
1016
#endif // WITH_AUTO4_LUA