Finale PDK Framework 0.77
Power Up Your Finale Music Software
Loading...
Searching...
No Matches
fflua_luascriptitem.h
1/*
2* File: fflua_luascriptitem.h
3*
4* Created by Robert Patterson 2022-07-30
5*
6* ******* IMPORTANT!!! ******
7* Although this file is available in the PDK Framework folder, it should NOT be used in
8* the PDK Framework umbrella. This file is placed in the framework folder purely ONLY to
9* provide online documentation for the FCLuaScriptItem and FCLuaScriptItems classes!
10*
11* (The file will ONLY make sense in the context of the specific
12* LuaBridge connection implementation.)
13*
14* The following class implementations are for the special FCLuaScriptItem and FCLuaScriptItems classes
15* in RGP Lua. Since a limitation of LuaBridge is that the include file can only be included at one place,
16* this work-around is used.
17*/
18
19#ifndef jwlua_fflua_luascriptitem_h
20#define jwlua_fflua_luascriptitem_h
21
22#include <string>
23#include <functional>
24#include <unordered_map>
25
26#include "ff_base.h"
27
70{
71private:
72 std::string _filepath;
73 std::string _filename;
74 std::optional<std::string> _scripttext;
75 std::string _menutext;
76 std::string _undotext;
77 std::string _description;
78 std::string _prefix;
79 std::unordered_map<std::string, std::string> _fields;
80 bool _debug;
81 bool _loadasstring;
82 int _executableindex;
83 bool _versionCompatible;
84 bool _autoReportErrors;
85 bool _trusted;
86 __FCUserWindow* _controllingwindow;
87 EVERSION _minFinaleYear;
88 EVERSION _maxFinaleYear;
89 EVERSION _minFinaleVersion;
90 EVERSION _maxFinaleVersion;
91 EVERSION _minLuaVersion;
92 EVERSION _maxLuaVersion;
93
94 // The cleanup function is called in the destructor function.
95 // This is needed because the Lua connector (RGP Lua) is tightly coupled to this
96 // class and needs to be notified when it is destroyed.
97 std::function<void (FCLuaScriptItem *)> _cleanupFunction;
98 // The isexecuting function is called to determine if there is a script executing
99 std::function<bool (const FCLuaScriptItem *)> _isExecuting;
100 void* _userData; // opaque to FCLuaScriptItem
101
102 luabridge::LuaRef _printCallBack;
103 luabridge::LuaRef _handleExecutionStarted;
104 luabridge::LuaRef _handleExecutionStopped;
105 luabridge::LuaRef _handleModalWindowOpened;
106 luabridge::LuaRef _handleModalWindowClosed;
107
108 void _init()
109 {
110 _debug = false;
111 _loadasstring = false;
112 _versionCompatible = false;
113 _autoReportErrors = true;
114 _trusted = false;
115 _minFinaleYear = 0;
116 _maxFinaleYear = UINT32_MAX;
117 _minFinaleVersion = 0;
118 _maxFinaleVersion = UINT32_MAX;
119 _minLuaVersion = 0;
120 _maxLuaVersion = UINT32_MAX;
121 _userData = nullptr;
122 _controllingwindow = nullptr;
123 }
124
125 template<typename... Args>
126 void ExecuteCallbackFunction(luabridge::LuaRef& functionLuaRef, Args... args)
127 {
128 if (functionLuaRef.isFunction())
129 functionLuaRef(this, args...);
130 }
131
132public:
133#ifndef DOXYGEN_SHOULD_IGNORE_THIS
142 FCLuaScriptItem(std::function<void (FCLuaScriptItem *)> cleanupFunc, std::function<bool (const FCLuaScriptItem *)> isExecutingFunc, const char *filePath, const char *fileName, const char *scriptText, lua_State* L)
143 : _cleanupFunction(cleanupFunc), _isExecuting(isExecutingFunc), _executableindex(-1),
144 _printCallBack(L),
145 _handleExecutionStarted(L),
146 _handleExecutionStopped(L),
147 _handleModalWindowOpened(L),
148 _handleModalWindowClosed(L)
149 {
150 _init();
151 if (filePath)
152 _filepath = filePath;
153 if (fileName)
154 _filename = fileName;
155 assert(!_filepath.size() || _filename.size());
156 if (scriptText)
157 _scripttext = scriptText;
158 }
159
166 FCLuaScriptItem(std::function<bool (const FCLuaScriptItem *)> isExecuting, int index, const char *fileName, lua_State* L)
167 : _cleanupFunction(nullptr), _isExecuting(isExecuting), _executableindex(index),
168 _printCallBack(L),
169 _handleExecutionStarted(L),
170 _handleExecutionStopped(L),
171 _handleModalWindowOpened(L),
172 _handleModalWindowClosed(L)
173 {
174 _init();
175 if (fileName)
176 _filename = fileName;
177 }
178
181 {
182 // Nop-out the callbacks, because if they throw an error when we are trying to be destroyed,
183 // there may not be anything to catch the exception. Consider the following scenario:
184 //
185 // Both handlers run the same code that contains a runtime error.
186 // The supervising script executes the script item, which error aborts in the start handler.
187 // When the supervisor tries to call lua_close on itself, all its open script items are destroyed.
188 // Including this one that (if we didn't nop these out) would call the stopped
189 // handler and trigger another exception, crashing the system.
190 //
191 // If there is ever a need to report execution stopped from a dying FCLuaScriptItem, we'll
192 // revisit this. As of now, there is not.
193 _handleExecutionStarted = luabridge::LuaRef(_handleExecutionStarted.state());
194 _handleExecutionStopped = luabridge::LuaRef(_handleExecutionStopped.state());
195 if (_cleanupFunction) _cleanupFunction(this);
196 }
197#endif // DOXYGEN_SHOULD_IGNORE_THIS
198
199 const char* ClassName() const override { return "FCLuaScriptItem"; }
200
227
232 bool IsExecuting() const { return _isExecuting(this); }
233
240 void StopExecuting() { if (_cleanupFunction) _cleanupFunction(this); }
241
242 //
243 // Getters
244 //
245
253 const char * GetFilePath() const { return _filepath.c_str(); }
254
263 const char * GetFileName() const { return _filename.c_str(); }
264
286 const char * GetOptionalScriptText() const
287 {
288 if (_scripttext.has_value())
289 return _scripttext->c_str();
290 return nullptr;
291 }
292
297 const char * GetMenuItemText() const { return _menutext.c_str(); }
298
303 const char * GetUndoText() const { return _undotext.c_str(); }
304
309 const char * GetDescription() const { return _description.c_str(); }
310
315 const char * GetPrefix() const { return _prefix.c_str(); }
316
321 bool GetDebug() const { return _debug; }
322
332 bool GetLoadAsString() const { return _loadasstring; }
333
341 int GetExecutableIndex() const { return _executableindex; }
342
352 bool GetVersionCompatible() const { return _versionCompatible; }
353
361 EVERSION GetRequiredVersion(int versiontype) const
362 {
363 switch(versiontype)
364 {
365 case REQVERSIONTYPE_MINIMUM_FINALE: return _minFinaleVersion;
366 case REQVERSIONTYPE_MAXIMUM_FINALE: return _maxFinaleVersion;
367 case REQVERSIONTYPE_MINIMUM_FINALE_MAJOR: return _minFinaleYear;
368 case REQVERSIONTYPE_MAXIMUM_FINALE_MAJOR: return _maxFinaleYear;
369 case REQVERSIONTYPE_MINIMUM_LUAX1000: return _minLuaVersion;
370 case REQVERSIONTYPE_MAXIMUM_LUAX1000: return _maxLuaVersion;
371 }
372 throw std::runtime_error("Invalid value passed to FCLuaScriptItem::GetRequiredVersion");
373 }
374
379 bool GetAutomaticallyReportErrors() const { return _autoReportErrors; }
380
385 bool GetTrusted() const { return _trusted; }
386
400 __FCUserWindow* GetControllingWindow() const { return _controllingwindow; }
401
411 FCString * CreateFinalePluginValue(const char *fieldname)
412 {
413 auto it = _fields.find(fieldname);
414 if (it == _fields.end())
415 return nullptr;
416 return new FCString(it->second.c_str());
417 }
418
419#ifndef DOXYGEN_SHOULD_IGNORE_THIS
420 luabridge::RefCountedPtr<FCString> CreateFinalePluginValue_GC(const char *fieldname)
421 { return makeLuaSharedPtr(CreateFinalePluginValue(fieldname)); }
422#endif // DOXYGEN_SHOULD_IGNORE_THIS
423
424 //
425 // Setters
426 //
427
437 void SetOptionalScriptText(const char* value)
438 {
439 if (value)
440 _scripttext = value;
441 else
442 _scripttext = std::nullopt;
443 }
444
449 void SetMenuItemText(const char * value) { _menutext = value; }
450
455 void SetUndoText(const char * value) { _undotext = value; }
456
461 void SetDescription(const char * value) { _description = value; }
462
467 void SetPrefix(const char * value) { _prefix = value; }
468
473 void SetDebug(bool state) { _debug = state; }
474
482 void SetLoadAsString(bool state) { _loadasstring = state; }
483
488 void SetAutomaticallyReportErrors(bool state) { _autoReportErrors = state; }
489
496 void SetTrusted(bool state) { _trusted = state; }
497
509 void SetControllingWindow(__FCUserWindow* value) { _controllingwindow = value; }
510
525 bool RegisterPrintFunction(luabridge::LuaRef lua_callback_function)
526 {
527 if (!lua_callback_function.isFunction()) return false;
528 _printCallBack = lua_callback_function;
529 return true;
530 }
531
542 bool RegisterOnExecutionWillStart(luabridge::LuaRef lua_callback_function)
543 {
544 if (!lua_callback_function.isFunction()) return false;
545 _handleExecutionStarted = lua_callback_function;
546 return true;
547 }
548
559 bool RegisterOnExecutionDidStop(luabridge::LuaRef lua_callback_function)
560 {
561 if (!lua_callback_function.isFunction()) return false;
562 _handleExecutionStopped = lua_callback_function;
563 return true;
564 }
565
579 bool RegisterOnModalWindowWillOpen(luabridge::LuaRef lua_callback_function)
580 {
581 if (!lua_callback_function.isFunction()) return false;
582 _handleModalWindowOpened = lua_callback_function;
583 return true;
584 }
585
596 bool RegisterOnModalWindowDidClose(luabridge::LuaRef lua_callback_function)
597 {
598 if (!lua_callback_function.isFunction()) return false;
599 _handleModalWindowClosed = lua_callback_function;
600 return true;
601 }
602
608 {
609 ExecuteCallbackFunction(_handleExecutionStarted);
610 }
611
624 void OnExecutionDidStop(bool success, const char* msg, int msgtype, int linenumber, const char* source)
625 {
626 ExecuteCallbackFunction(_handleExecutionStopped, success, msg, msgtype, linenumber, source);
627 }
628
634 {
635 ExecuteCallbackFunction(_handleModalWindowOpened);
636 }
637
647 {
648 ExecuteCallbackFunction(_handleModalWindowClosed);
649 }
650
651#ifndef DOXYGEN_SHOULD_IGNORE_THIS
653 const luabridge::LuaRef& GetPrintFunction() const { return _printCallBack; }
654
656 void SetVersionCompatible(const bool state) { _versionCompatible = state; }
657
659 void SetFinalePluginFields(const std::unordered_map<std::string, std::string>& fields)
660 { _fields = fields; }
661
663 void SetRequiredVersion(LUASCRIPT_REQVERSION_TYPES versiontype, EVERSION requiredversion)
664 {
665 switch(versiontype)
666 {
668 _minFinaleVersion = requiredversion;
669 break;
670
672 _maxFinaleVersion = requiredversion;
673 break;
674
676 _minFinaleYear = requiredversion;
677 break;
678
680 _maxFinaleYear = requiredversion;
681 break;
682
684 _minLuaVersion = requiredversion;
685 break;
686
688 _maxLuaVersion = requiredversion;
689 break;
690
691 default:
692 throw std::runtime_error("Invalid value passed to FCLuaScriptItem::SetRequiredVersion");
693 }
694 }
695
697 void* GetUserData() const { return _userData; }
698
700 void SetUserData(void* ptr) { _userData = ptr; }
701#endif // DOXYGEN_SHOULD_IGNORE_THIS
702};
703
713{
714public:
715 const char* ClassName() const override { return "FCLuaScriptItems"; }
716
723
728 void Add(FCLuaScriptItem *pNewItem) { __FCCollection::Add(pNewItem); }
729
734 void InsertItemAt(FCLuaScriptItem *pNewItem, int index) { __FCCollection::InsertItemAt(pNewItem, index); }
735
741};
742
743
744#endif // jwlua_fflua_luascriptitem_h
Base class for the Finale Framework classes.
Definition ff_base.h:71
void SetUserData(void *pData)
Sets the user data attached to the instance of an object.
Definition ff_base.h:503
void * GetUserData() const
Gets the user data attached to the instance of an object.
Definition ff_base.h:517
Base class for all collection classes. A collection is a storage that can store multiple objects of s...
Definition ff_basecollection.h:26
void Add(__FCBase *pNewItem)
Adds an element to the end of the collection.
Definition finaleframework.cpp:13726
__FCBase * GetItemAt(int index) const
Returns the object at the index position. Index is 0-based.
Definition finaleframework.cpp:13767
void InsertItemAt(__FCBase *pNewItem, int index)
Inserts an item into the collection.
Definition finaleframework.cpp:13733
Base class for all other dialog/window classes.
Definition ff_dialogs.h:563
Class that represents a single Lua script item. This class is not part of the C++ PDK Framework.
Definition fflua_luascriptitem.h:70
bool GetAutomaticallyReportErrors() const
Gets if errors should automatically be reported by the Lua connector. The default value is true.
Definition fflua_luascriptitem.h:379
bool GetTrusted() const
Gets if the script should run in trusted mode. Default is false.
Definition fflua_luascriptitem.h:385
void StopExecuting()
Stops execution and terminates the Lua state.
Definition fflua_luascriptitem.h:240
void SetUndoText(const char *value)
Sets the undo text for the script.
Definition fflua_luascriptitem.h:455
void SetOptionalScriptText(const char *value)
Sets the optional script text.
Definition fflua_luascriptitem.h:437
void SetDebug(bool state)
Sets if the script should be run in debug mode.
Definition fflua_luascriptitem.h:473
const char * GetMenuItemText() const
Returns the menu item text for the script.
Definition fflua_luascriptitem.h:297
bool RegisterPrintFunction(luabridge::LuaRef lua_callback_function)
Registers a Lua function that replaces called script's print function.
Definition fflua_luascriptitem.h:525
LUASCRIPT_REQVERSION_TYPES
Settings used to get required version information for a script.
Definition fflua_luascriptitem.h:206
@ REQVERSIONTYPE_MAXIMUM_LUAX1000
Definition fflua_luascriptitem.h:224
@ REQVERSIONTYPE_MINIMUM_LUAX1000
Definition fflua_luascriptitem.h:221
@ REQVERSIONTYPE_MAXIMUM_FINALE
Definition fflua_luascriptitem.h:212
@ REQVERSIONTYPE_MINIMUM_FINALE_MAJOR
Definition fflua_luascriptitem.h:215
@ REQVERSIONTYPE_MINIMUM_FINALE
Definition fflua_luascriptitem.h:209
@ REQVERSIONTYPE_MAXIMUM_FINALE_MAJOR
Definition fflua_luascriptitem.h:218
bool RegisterOnExecutionWillStart(luabridge::LuaRef lua_callback_function)
Registers a Lua function that gets called when the script item starts executing. For the function to ...
Definition fflua_luascriptitem.h:542
void OnModalWindowWillOpen()
Function that is called when the script execution will open a modal dialog.
Definition fflua_luascriptitem.h:633
bool RegisterOnModalWindowWillOpen(luabridge::LuaRef lua_callback_function)
Registers a Lua function that gets called when the script item opens a modal dialog....
Definition fflua_luascriptitem.h:579
void SetDescription(const char *value)
Sets the description for the script.
Definition fflua_luascriptitem.h:461
void SetPrefix(const char *value)
Sets the prefix for the script.
Definition fflua_luascriptitem.h:467
void SetMenuItemText(const char *value)
Sets the menu item text for the script.
Definition fflua_luascriptitem.h:449
bool RegisterOnExecutionDidStop(luabridge::LuaRef lua_callback_function)
Registers a Lua function that gets called when the script item stops executing. For the function to b...
Definition fflua_luascriptitem.h:559
bool GetDebug() const
Return true if the script should be run in debug mode.
Definition fflua_luascriptitem.h:321
void OnExecutionWillStart()
Function that is called when script execution starts.
Definition fflua_luascriptitem.h:607
const char * GetPrefix() const
Returns the prefix for the script.
Definition fflua_luascriptitem.h:315
bool IsExecuting() const
Returns true if the script associated with this script item is currently executing.
Definition fflua_luascriptitem.h:232
__FCUserWindow * GetControllingWindow() const
Gets the controlling window for this script item.
Definition fflua_luascriptitem.h:400
void SetTrusted(bool state)
Sets if the script should run in trusted mode. If you set this to true, the script that calls finenv....
Definition fflua_luascriptitem.h:496
const char * GetFilePath() const
Returns the fully-qualified file path for the script or empty string if none.
Definition fflua_luascriptitem.h:253
const char * GetUndoText() const
Returns the undo text for the script.
Definition fflua_luascriptitem.h:303
bool GetVersionCompatible() const
If false, this script is not compatible with either the running Finale or the running Lua plugin vers...
Definition fflua_luascriptitem.h:352
const char * ClassName() const override
Returns the name of the class, for diagnostic purposes. This method MUST be overwritten in each child...
Definition fflua_luascriptitem.h:199
FCString * CreateFinalePluginValue(const char *fieldname)
Return the requested finaleplugin value.
Definition fflua_luascriptitem.h:411
const char * GetOptionalScriptText() const
Returns the optional script text.
Definition fflua_luascriptitem.h:286
int GetExecutableIndex() const
Returns the executable index for this script.
Definition fflua_luascriptitem.h:341
void OnExecutionDidStop(bool success, const char *msg, int msgtype, int linenumber, const char *source)
Function that is called when script execution stops and its Lua state is closed.
Definition fflua_luascriptitem.h:624
void SetControllingWindow(__FCUserWindow *value)
Sets the controlling window for this script item.
Definition fflua_luascriptitem.h:509
bool GetLoadAsString() const
Return true if the script should be loaded as a string.
Definition fflua_luascriptitem.h:332
void SetAutomaticallyReportErrors(bool state)
Sets if errors should automatically be reported by the Lua connector. The default value is true.
Definition fflua_luascriptitem.h:488
void SetLoadAsString(bool state)
Sets if the script should be loaded as a string.
Definition fflua_luascriptitem.h:482
bool RegisterOnModalWindowDidClose(luabridge::LuaRef lua_callback_function)
Registers a Lua function that gets called when the script item closes a modal dialog....
Definition fflua_luascriptitem.h:596
EVERSION GetRequiredVersion(int versiontype) const
Returns a required version number from one of the LUASCRIPT_REQVERSION_TYPES constants.
Definition fflua_luascriptitem.h:361
const char * GetFileName() const
Returns the file name of the script or empty string if none.
Definition fflua_luascriptitem.h:263
void OnModalWindowDidClose()
Function that is called when the script execution has closed a modal dialog.
Definition fflua_luascriptitem.h:646
const char * GetDescription() const
Returns the description for the script.
Definition fflua_luascriptitem.h:309
A collection of FCLuaScriptItem members.
Definition fflua_luascriptitem.h:713
FCLuaScriptItem * GetItemAt(int index) const
Overridden GetItemAt() method that returns an FCLuaScriptItem object.
Definition fflua_luascriptitem.h:740
FCLuaScriptItems()
The constructor.
Definition fflua_luascriptitem.h:721
const char * ClassName() const override
Returns the name of the class, for diagnostic purposes. This method MUST be overwritten in each child...
Definition fflua_luascriptitem.h:715
void Add(FCLuaScriptItem *pNewItem)
Overridden Add() method that takes an FCLuaScriptItem object.
Definition fflua_luascriptitem.h:728
void InsertItemAt(FCLuaScriptItem *pNewItem, int index)
Overridden InsertItemAt() method that takes an FCLuaScriptItem object.
Definition fflua_luascriptitem.h:734
Class that provides storage for text. This is to achieve platform-transparent text handling,...
Definition ff_base.h:1877