summaryrefslogtreecommitdiff
path: root/plugins/MirLua
diff options
context:
space:
mode:
authoraunsane <aunsane@gmail.com>2018-07-13 23:57:25 +0300
committeraunsane <aunsane@gmail.com>2018-07-15 15:03:47 +0300
commitce719a1725988bded5566a5d7f596b1ba42cfa91 (patch)
treeee5a96fa51444ed509edb111a0ed49eabda834f2 /plugins/MirLua
parent40198c29b0f5e7f14f73a3e0a62541ac918612a8 (diff)
MirLua: add Evaluate option page to test scripts
Diffstat (limited to 'plugins/MirLua')
-rw-r--r--plugins/MirLua/res/resource.rc66
-rw-r--r--plugins/MirLua/src/Modules/m_options.cpp6
-rw-r--r--plugins/MirLua/src/environment.cpp47
-rw-r--r--plugins/MirLua/src/environment.h12
-rw-r--r--plugins/MirLua/src/lua.cpp30
-rw-r--r--plugins/MirLua/src/lua.h17
-rw-r--r--plugins/MirLua/src/options.cpp136
-rw-r--r--plugins/MirLua/src/options.h34
-rw-r--r--plugins/MirLua/src/plugin.cpp123
-rw-r--r--plugins/MirLua/src/plugin.h17
-rw-r--r--plugins/MirLua/src/resource.h9
-rw-r--r--plugins/MirLua/src/script.cpp15
-rw-r--r--plugins/MirLua/src/script_loader.cpp14
-rw-r--r--plugins/MirLua/src/script_loader.h6
-rw-r--r--plugins/MirLua/src/stdafx.h3
-rw-r--r--plugins/MirLua/src/version.h2
16 files changed, 342 insertions, 195 deletions
diff --git a/plugins/MirLua/res/resource.rc b/plugins/MirLua/res/resource.rc
index 86e499df0c..3c6008c4ff 100644
--- a/plugins/MirLua/res/resource.rc
+++ b/plugins/MirLua/res/resource.rc
@@ -80,17 +80,28 @@ IDI_COMPILE ICON "compile.ico"
// Dialog
//
-IDD_OPTIONS DIALOGEX 0, 0, 310, 250
+IDD_OPTIONSMAIN DIALOGEX 0, 0, 310, 234
STYLE DS_SETFONT | DS_FIXEDSYS | WS_CHILD
EXSTYLE WS_EX_CONTROLPARENT
FONT 8, "MS Shell Dlg", 0, 0, 0x1
BEGIN
- CONTROL "",IDC_SCRIPTS,"SysListView32",LVS_REPORT | LVS_SINGLESEL | LVS_ALIGNLEFT | LVS_NOCOLUMNHEADER | WS_BORDER | WS_TABSTOP,8,57,294,169
- PUSHBUTTON "Reload all",IDC_RELOAD,226,228,76,14
- GROUPBOX "Scripts",IDC_STATIC,0,46,309,203
- CONTROL "Popup on script error",IDC_POPUPONERROR,"Button",BS_AUTOCHECKBOX | WS_TABSTOP,8,10,294,10
- GROUPBOX "General",IDC_STATIC,1,0,308,44
- CONTROL "Popup on obsolete methods",IDC_POPUPONOBSOLETE,"Button",BS_AUTOCHECKBOX | WS_TABSTOP,8,21,294,10
+ CONTROL "",IDC_SCRIPTS,"SysListView32",LVS_REPORT | LVS_SINGLESEL | LVS_ALIGNLEFT | LVS_NOCOLUMNHEADER | WS_BORDER | WS_TABSTOP,14,66,282,137
+ PUSHBUTTON "Reload all",IDC_RELOAD,220,207,76,14
+ GROUPBOX "Scripts",IDC_STATIC,7,54,296,173
+ CONTROL "Popup on script error",IDC_POPUPONERROR,"Button",BS_AUTOCHECKBOX | WS_TABSTOP,14,17,282,10
+ GROUPBOX "General",IDC_STATIC,7,7,296,44
+ CONTROL "Popup on obsolete methods",IDC_POPUPONOBSOLETE,"Button",BS_AUTOCHECKBOX | WS_TABSTOP,14,28,282,10
+END
+
+IDD_OPTIONSEVALUATE DIALOGEX 0, 0, 310, 234
+STYLE DS_SETFONT | DS_FIXEDSYS | WS_CHILD
+EXSTYLE WS_EX_CONTROLPARENT
+FONT 8, "MS Shell Dlg", 0, 0, 0x1
+BEGIN
+ EDITTEXT IDC_SCRIPTTEXT,7,7,296,148,ES_MULTILINE | ES_AUTOVSCROLL | ES_AUTOHSCROLL | ES_WANTRETURN | WS_VSCROLL | WS_HSCROLL
+ CONTROL "Auto evaluation",IDC_AUTOEVAL,"Button",BS_AUTOCHECKBOX | WS_TABSTOP,7,160,167,10
+ PUSHBUTTON "Evaluate",IDC_EVALUATE,253,157,50,14
+ EDITTEXT IDC_SCRIPTRESULT,7,173,296,54,ES_MULTILINE | ES_AUTOVSCROLL | ES_AUTOHSCROLL | ES_READONLY | ES_WANTRETURN | WS_VSCROLL | WS_HSCROLL
END
IDD_SCRIPTOPTIONSPAGE DIALOGEX 0, 0, 310, 250
@@ -109,15 +120,36 @@ END
#ifdef APSTUDIO_INVOKED
GUIDELINES DESIGNINFO
BEGIN
- IDD_OPTIONS, DIALOG
+ IDD_OPTIONSMAIN, DIALOG
+ BEGIN
+ LEFTMARGIN, 7
+ RIGHTMARGIN, 303
+ VERTGUIDE, 14
+ VERTGUIDE, 296
+ TOPMARGIN, 7
+ BOTTOMMARGIN, 227
+ HORZGUIDE, 54
+ HORZGUIDE, 66
+ HORZGUIDE, 203
+ HORZGUIDE, 221
+ END
+
+ IDD_OPTIONSEVALUATE, DIALOG
BEGIN
- VERTGUIDE, 8
- VERTGUIDE, 302
- HORZGUIDE, 242
+ LEFTMARGIN, 7
+ RIGHTMARGIN, 303
+ TOPMARGIN, 7
+ BOTTOMMARGIN, 227
+ HORZGUIDE, 155
+ HORZGUIDE, 173
END
IDD_SCRIPTOPTIONSPAGE, DIALOG
BEGIN
+ LEFTMARGIN, 7
+ RIGHTMARGIN, 302
+ TOPMARGIN, 7
+ BOTTOMMARGIN, 169
END
END
#endif // APSTUDIO_INVOKED
@@ -128,7 +160,17 @@ END
// AFX_DIALOG_LAYOUT
//
-IDD_OPTIONS AFX_DIALOG_LAYOUT
+IDD_OPTIONSMAIN AFX_DIALOG_LAYOUT
+BEGIN
+ 0
+END
+
+IDD_OPTIONSEVALUATE AFX_DIALOG_LAYOUT
+BEGIN
+ 0
+END
+
+IDD_SCRIPTOPTIONSPAGE AFX_DIALOG_LAYOUT
BEGIN
0
END
diff --git a/plugins/MirLua/src/Modules/m_options.cpp b/plugins/MirLua/src/Modules/m_options.cpp
index bcb9c799d0..74a8e2be72 100644
--- a/plugins/MirLua/src/Modules/m_options.cpp
+++ b/plugins/MirLua/src/Modules/m_options.cpp
@@ -9,8 +9,10 @@ private:
public:
CMLuaScriptOptionPage(lua_State *_L, int onInitDialogRef, int onApplyRef)
- : CDlgBase(g_plugin, IDD_SCRIPTOPTIONSPAGE), L(_L),
- m_onInitDialogRef(onInitDialogRef), m_onApplyRef(onApplyRef)
+ : CDlgBase(g_plugin, IDD_SCRIPTOPTIONSPAGE),
+ L(_L),
+ m_onInitDialogRef(onInitDialogRef),
+ m_onApplyRef(onApplyRef)
{
}
diff --git a/plugins/MirLua/src/environment.cpp b/plugins/MirLua/src/environment.cpp
index 88e7bff62b..1816483a17 100644
--- a/plugins/MirLua/src/environment.cpp
+++ b/plugins/MirLua/src/environment.cpp
@@ -4,13 +4,12 @@
extern PLUGININFOEX pluginInfoEx;
-CMLuaEnvironment::CMLuaEnvironment(lua_State *_l) :
- CMPluginBase(nullptr, pluginInfoEx),
- L(_l)
+CMLuaEnvironment::CMLuaEnvironment(lua_State *L) :
+ CMPluginBase(nullptr, pluginInfoEx), L(L)
{
}
-CMLuaEnvironment::~CMLuaEnvironment()
+int CMLuaEnvironment::Unload()
{
KillModuleIcons(this);
KillModuleSounds(this);
@@ -25,6 +24,8 @@ CMLuaEnvironment::~CMLuaEnvironment()
for (auto &it : m_serviceRefs)
luaL_unref(L, LUA_REGISTRYINDEX, it.second);
+
+ return 0;
}
CMLuaEnvironment* CMLuaEnvironment::GetEnvironment(lua_State *L)
@@ -121,6 +122,9 @@ void CMLuaEnvironment::DestroyServiceFunction(HANDLE hService)
void CMLuaEnvironment::CreateEnvironmentTable()
{
+ if (!lua_isfunction(L, -1))
+ return;
+
lua_createtable(L, 1, 1);
lua_pushlightuserdata(L, this);
lua_rawseti(L, -2, NULL);
@@ -132,14 +136,41 @@ void CMLuaEnvironment::CreateEnvironmentTable()
lua_getglobal(L, "_G");
lua_setfield(L, -2, "__index");
lua_setmetatable(L, -2);
+
+ lua_setupvalue(L, -2, 1);
}
-int CMLuaEnvironment::Load()
+/***********************************************/
+
+wchar_t* CMLuaEnvironment::Error()
{
- luaL_checktype(L, -1, LUA_TFUNCTION);
+ const char *message = lua_tostring(L, -1);
+ wchar_t *error = mir_utf8decodeW(message);
+ lua_pop(L, 1);
+ return error;
+}
+wchar_t* CMLuaEnvironment::Call()
+{
CreateEnvironmentTable();
- lua_setupvalue(L, -2, 1);
+ luaM_pcall(L, 0, 1);
- return lua_pcall(L, 0, 1, 0) == LUA_OK;
+ wchar_t *result = mir_utf8decodeW(lua_tostring(L, -1));
+ lua_pop(L, 1);
+
+ return result;
+}
+
+wchar_t* CMLuaEnvironment::Eval(const wchar_t *script)
+{
+ if (luaL_loadstring(L, T2Utf(script)))
+ return Error();
+ return Call();
+}
+
+wchar_t* CMLuaEnvironment::Exec(const wchar_t *path)
+{
+ if (luaL_loadfile(L, T2Utf(path)))
+ return Error();
+ return Call();
}
diff --git a/plugins/MirLua/src/environment.h b/plugins/MirLua/src/environment.h
index 7c2300b6d9..ab6cf8c8d2 100644
--- a/plugins/MirLua/src/environment.h
+++ b/plugins/MirLua/src/environment.h
@@ -3,16 +3,22 @@
class CMLuaEnvironment : public CMPluginBase
{
private:
+ CMPluginBase *m_plugin;
+
std::map<HANDLE, int> m_hookRefs;
std::map<HANDLE, int> m_serviceRefs;
+protected:
void CreateEnvironmentTable();
+ wchar_t* Error();
+
public:
lua_State *L;
CMLuaEnvironment(lua_State *L);
- virtual ~CMLuaEnvironment();
+
+ int Unload() override;
static CMLuaEnvironment* GetEnvironment(lua_State *L);
static HPLUGIN GetEnvironmentId(lua_State *L);
@@ -23,5 +29,7 @@ public:
HANDLE CreateServiceFunction(const char *name, int ref);
void DestroyServiceFunction(HANDLE hService);
- int Load() override;
+ wchar_t* Call();
+ wchar_t* Eval(const wchar_t *script);
+ wchar_t* Exec(const wchar_t *path);
};
diff --git a/plugins/MirLua/src/lua.cpp b/plugins/MirLua/src/lua.cpp
deleted file mode 100644
index 7f96a2300f..0000000000
--- a/plugins/MirLua/src/lua.cpp
+++ /dev/null
@@ -1,30 +0,0 @@
-#include "stdafx.h"
-
-CMLua::CMLua() : L(nullptr)
-{
-}
-
-CMLua::~CMLua()
-{
- Log("Unloading lua engine");
-
- KillObjectEventHooks(L);
- KillObjectServices(L);
-
- lua_close(L);
-}
-
-lua_State* CMLua::GetState()
-{
- return L;
-}
-
-void CMLua::Load()
-{
- Log("Loading lua engine");
- L = luaL_newstate();
- Log("Loading standard modules");
- luaL_openlibs(L);
-
- lua_atpanic(L, luaM_atpanic);
-}
diff --git a/plugins/MirLua/src/lua.h b/plugins/MirLua/src/lua.h
deleted file mode 100644
index 5d64c65ff6..0000000000
--- a/plugins/MirLua/src/lua.h
+++ /dev/null
@@ -1,17 +0,0 @@
-#pragma once
-
-class CMLua
-{
- friend class CMPlugin;
-
-private:
- lua_State *L;
-
-public:
- CMLua();
- ~CMLua();
-
- lua_State* GetState();
-
- void Load();
-};
diff --git a/plugins/MirLua/src/options.cpp b/plugins/MirLua/src/options.cpp
index fe3ac6abba..bfdacc6e6a 100644
--- a/plugins/MirLua/src/options.cpp
+++ b/plugins/MirLua/src/options.cpp
@@ -1,18 +1,19 @@
#include "stdafx.h"
-CMLuaOptions::CMLuaOptions()
- : CDlgBase(g_plugin, IDD_OPTIONS),
+CMLuaOptionsMain::CMLuaOptionsMain(CMPlugin &plugin)
+ : CDlgBase(plugin, IDD_OPTIONSMAIN),
+ m_plugin(plugin),
isScriptListInit(false),
m_popupOnError(this, IDC_POPUPONERROR),
m_popupOnObsolete(this, IDC_POPUPONOBSOLETE),
- m_scripts(this, IDC_SCRIPTS),
+ m_scriptsList(this, IDC_SCRIPTS),
m_reload(this, IDC_RELOAD)
{
CreateLink(m_popupOnError, "PopupOnError", DBVT_BYTE, 1);
CreateLink(m_popupOnObsolete, "PopupOnObsolete", DBVT_BYTE, 1);
- m_scripts.OnClick = Callback(this, &CMLuaOptions::OnScriptListClick);
- m_reload.OnClick = Callback(this, &CMLuaOptions::OnReload);
+ m_scriptsList.OnClick = Callback(this, &CMLuaOptionsMain::OnScriptListClick);
+ m_reload.OnClick = Callback(this, &CMLuaOptionsMain::OnReload);
}
static int ScriptStatusToIcon(ScriptStatus status)
@@ -29,26 +30,26 @@ static int ScriptStatusToIcon(ScriptStatus status)
return 0;
}
-void CMLuaOptions::LoadScripts()
+void CMLuaOptionsMain::LoadScripts()
{
- for (auto &script : g_plugin.Scripts.rev_iter()) {
+ for (auto &script : m_plugin.m_scripts.rev_iter()) {
int iIcon = ScriptStatusToIcon(script->GetStatus());
- int iItem = m_scripts.AddItem(script->GetName(), iIcon, (LPARAM)script);
- m_scripts.SetCheckState(iItem, script->IsEnabled());
- m_scripts.SetItem(iItem, 1, TranslateT("Open"), 2);
- m_scripts.SetItem(iItem, 2, TranslateT("Reload"), 3);
+ int iItem = m_scriptsList.AddItem(script->GetName(), iIcon, (LPARAM)script);
+ m_scriptsList.SetCheckState(iItem, script->IsEnabled());
+ m_scriptsList.SetItem(iItem, 1, TranslateT("Open"), 2);
+ m_scriptsList.SetItem(iItem, 2, TranslateT("Reload"), 3);
if (!script->IsBinary())
- m_scripts.SetItem(iItem, 3, TranslateT("Compile"), 4);
+ m_scriptsList.SetItem(iItem, 3, TranslateT("Compile"), 4);
}
}
-bool CMLuaOptions::OnInitDialog()
+bool CMLuaOptionsMain::OnInitDialog()
{
CDlgBase::OnInitDialog();
- m_scripts.SetExtendedListViewStyle(LVS_EX_SUBITEMIMAGES | LVS_EX_FULLROWSELECT | LVS_EX_CHECKBOXES | LVS_EX_INFOTIP);
+ m_scriptsList.SetExtendedListViewStyle(LVS_EX_SUBITEMIMAGES | LVS_EX_FULLROWSELECT | LVS_EX_CHECKBOXES | LVS_EX_INFOTIP);
- HIMAGELIST hImageList = m_scripts.CreateImageList(LVSIL_SMALL);
+ HIMAGELIST hImageList = m_scriptsList.CreateImageList(LVSIL_SMALL);
ImageList_AddIcon(hImageList, GetIcon(IDI_LOADED));
ImageList_AddIcon(hImageList, GetIcon(IDI_FAILED));
ImageList_AddIcon(hImageList, GetIcon(IDI_OPEN));
@@ -64,10 +65,10 @@ bool CMLuaOptions::OnInitDialog()
wchar_t header[MAX_PATH + 100];
mir_snwprintf(header, L"%s (%s)", TranslateT("Common scripts"), relativeScriptDir);
- m_scripts.AddColumn(0, L"Script", 380);
- m_scripts.AddColumn(1, nullptr, 32 - GetSystemMetrics(SM_CXVSCROLL));
- m_scripts.AddColumn(2, nullptr, 32 - GetSystemMetrics(SM_CXVSCROLL));
- m_scripts.AddColumn(3, nullptr, 32 - GetSystemMetrics(SM_CXVSCROLL));
+ m_scriptsList.AddColumn(0, L"Script", 370);
+ m_scriptsList.AddColumn(1, nullptr, 32 - GetSystemMetrics(SM_CXVSCROLL));
+ m_scriptsList.AddColumn(2, nullptr, 32 - GetSystemMetrics(SM_CXVSCROLL));
+ m_scriptsList.AddColumn(3, nullptr, 32 - GetSystemMetrics(SM_CXVSCROLL));
LoadScripts();
@@ -75,12 +76,12 @@ bool CMLuaOptions::OnInitDialog()
return true;
}
-bool CMLuaOptions::OnApply()
+bool CMLuaOptionsMain::OnApply()
{
- int count = m_scripts.GetItemCount();
+ int count = m_scriptsList.GetItemCount();
for (int iItem = 0; iItem < count; iItem++) {
- CMLuaScript *script = (CMLuaScript*)m_scripts.GetItemData(iItem);
- if (!m_scripts.GetCheckState(iItem))
+ CMLuaScript *script = (CMLuaScript*)m_scriptsList.GetItemData(iItem);
+ if (!m_scriptsList.GetCheckState(iItem))
script->Disable();
else
script->Enable();
@@ -88,13 +89,13 @@ bool CMLuaOptions::OnApply()
return true;
}
-INT_PTR CMLuaOptions::DlgProc(UINT msg, WPARAM wParam, LPARAM lParam)
+INT_PTR CMLuaOptionsMain::DlgProc(UINT msg, WPARAM wParam, LPARAM lParam)
{
switch (msg) {
case WM_NOTIFY:
{
LPNMHDR lpnmHdr = (LPNMHDR)lParam;
- if (lpnmHdr->idFrom == (UINT_PTR)m_scripts.GetCtrlId() && lpnmHdr->code == LVN_ITEMCHANGED) {
+ if (lpnmHdr->idFrom == (UINT_PTR)m_scriptsList.GetCtrlId() && lpnmHdr->code == LVN_ITEMCHANGED) {
LPNMLISTVIEW pnmv = (LPNMLISTVIEW)lParam;
if (pnmv->uChanged & LVIF_STATE && pnmv->uNewState & LVIS_STATEIMAGEMASK) {
if (isScriptListInit)
@@ -107,7 +108,7 @@ INT_PTR CMLuaOptions::DlgProc(UINT msg, WPARAM wParam, LPARAM lParam)
return CDlgBase::DlgProc(msg, wParam, lParam);
}
-void CMLuaOptions::OnScriptListClick(CCtrlListView::TEventInfo *evt)
+void CMLuaOptionsMain::OnScriptListClick(CCtrlListView::TEventInfo *evt)
{
LVITEM lvi = {};
lvi.iItem = evt->nmlvia->iItem;
@@ -117,7 +118,7 @@ void CMLuaOptions::OnScriptListClick(CCtrlListView::TEventInfo *evt)
lvi.pszText = (LPTSTR)mir_calloc(MAX_PATH * sizeof(wchar_t));
lvi.cchTextMax = MAX_PATH;
lvi.mask = LVIF_GROUPID | LVIF_TEXT | LVIF_PARAM;
- m_scripts.GetItem(&lvi);
+ m_scriptsList.GetItem(&lvi);
lvi.iSubItem = evt->nmlvia->iSubItem;
CMLuaScript *script = (CMLuaScript*)lvi.lParam;
@@ -132,8 +133,8 @@ void CMLuaOptions::OnScriptListClick(CCtrlListView::TEventInfo *evt)
lvi.mask = LVIF_IMAGE;
lvi.iSubItem = 0;
lvi.iImage = ScriptStatusToIcon(script->GetStatus());
- m_scripts.SetItem(&lvi);
- m_scripts.Update(lvi.iItem);
+ m_scriptsList.SetItem(&lvi);
+ m_scriptsList.Update(lvi.iItem);
break;
case 3:
@@ -143,33 +144,80 @@ void CMLuaOptions::OnScriptListClick(CCtrlListView::TEventInfo *evt)
lvi.mask = LVIF_IMAGE;
lvi.iSubItem = 0;
lvi.iImage = ScriptStatusToIcon(script->GetStatus());
- m_scripts.SetItem(&lvi);
- m_scripts.Update(lvi.iItem);
+ m_scriptsList.SetItem(&lvi);
+ m_scriptsList.Update(lvi.iItem);
break;
}
mir_free(lvi.pszText);
}
-void CMLuaOptions::OnReload(CCtrlBase*)
+void CMLuaOptionsMain::OnReload(CCtrlBase*)
{
isScriptListInit = false;
- m_scripts.DeleteAllItems();
- g_plugin.Reload();
+ m_scriptsList.DeleteAllItems();
+ m_plugin.ReloadLuaScripts();
LoadScripts();
isScriptListInit = true;
}
/***********************************************/
-int OnOptionsInit(WPARAM wParam, LPARAM)
+CMLuaEvaluateOptions::CMLuaEvaluateOptions(CMPlugin &plugin)
+ : CDlgBase(plugin, IDD_OPTIONSEVALUATE),
+ m_script(this, IDC_SCRIPTTEXT),
+ m_result(this, IDC_SCRIPTRESULT),
+ m_autoEval(this, IDC_AUTOEVAL),
+ m_evaluate(this, IDC_EVALUATE)
{
- OPTIONSDIALOGPAGE odp = {};
- odp.flags = ODPF_BOLDGROUPS | ODPF_UNICODE | ODPF_DONTTRANSLATE;
- odp.szGroup.w = LPGENW("Services");
- odp.szTitle.w = L"Lua";
- odp.szTab.w = LPGENW("Scripts");
- odp.pDialog = new CMLuaOptions();
- g_plugin.addOptions(wParam, &odp);
- return 0;
-} \ No newline at end of file
+ this->L = lua_newthread(plugin.L);
+ threadRef = luaL_ref(L, LUA_REGISTRYINDEX);
+
+ CreateLink(m_autoEval, "AutoEval", DBVT_BYTE, 0);
+
+ m_autoEval.OnChange = Callback(this, &CMLuaEvaluateOptions::OnAutoEvalChange);
+ m_evaluate.OnClick = Callback(this, &CMLuaEvaluateOptions::OnEvaluate);
+}
+
+CMLuaEvaluateOptions::~CMLuaEvaluateOptions()
+{
+ luaL_unref(L, LUA_REGISTRYINDEX, threadRef);
+ threadRef = 0;
+}
+
+bool CMLuaEvaluateOptions::OnInitDialog()
+{
+ m_script.SetText(m_pPlugin.getMStringW("Script"));
+ if (m_autoEval.IsChecked()) {
+ m_script.OnChange = Callback(this, &CMLuaEvaluateOptions::OnEvaluate);
+ m_evaluate.Click();
+ }
+ return true;
+}
+
+bool CMLuaEvaluateOptions::OnApply()
+{
+ m_pPlugin.setWString("Script", m_script.GetText());
+ return true;
+}
+
+void CMLuaEvaluateOptions::OnAutoEvalChange(CCtrlBase*)
+{
+ if (m_autoEval.IsChecked()) {
+ m_script.OnChange = Callback(this, &CMLuaEvaluateOptions::OnEvaluate);
+ m_evaluate.Click();
+ }
+ else
+ m_script.OnChange = CCallback<CCtrlBase>();
+}
+
+void CMLuaEvaluateOptions::OnEvaluate(CCtrlBase*)
+{
+ ptrW script(m_script.GetText());
+
+ CMLuaEnvironment env(L);
+ m_result.SetText(env.Eval(script));
+ env.Unload();
+}
+
+/***********************************************/
diff --git a/plugins/MirLua/src/options.h b/plugins/MirLua/src/options.h
index 9b540bc392..879f06b55a 100644
--- a/plugins/MirLua/src/options.h
+++ b/plugins/MirLua/src/options.h
@@ -1,14 +1,16 @@
#pragma once
-class CMLuaOptions : public CDlgBase
+class CMLuaOptionsMain : public CDlgBase
{
private:
+ CMPlugin &m_plugin;
+
bool isScriptListInit;
CCtrlCheck m_popupOnError;
CCtrlCheck m_popupOnObsolete;
- CCtrlListView m_scripts;
+ CCtrlListView m_scriptsList;
CCtrlButton m_reload;
void LoadScripts();
@@ -23,5 +25,31 @@ protected:
INT_PTR DlgProc(UINT msg, WPARAM wParam, LPARAM lParam) override;
public:
- CMLuaOptions();
+ CMLuaOptionsMain(CMPlugin &plugin);
+};
+
+/***********************************************/
+
+class CMLuaEvaluateOptions : public CDlgBase
+{
+private:
+ lua_State *L = nullptr;
+ int threadRef = 0;
+
+ CCtrlEdit m_script;
+ CCtrlEdit m_result;
+
+ CCtrlCheck m_autoEval;
+ CCtrlButton m_evaluate;
+
+protected:
+ bool OnInitDialog() override;
+ bool OnApply() override;
+
+ void OnAutoEvalChange(CCtrlBase*);
+ void OnEvaluate(CCtrlBase*);
+
+public:
+ CMLuaEvaluateOptions(CMPlugin &plugin);
+ ~CMLuaEvaluateOptions();
};
diff --git a/plugins/MirLua/src/plugin.cpp b/plugins/MirLua/src/plugin.cpp
index d8f1864c94..a5451d173b 100644
--- a/plugins/MirLua/src/plugin.cpp
+++ b/plugins/MirLua/src/plugin.cpp
@@ -24,8 +24,8 @@ static int ScriptsCompare(const CMLuaScript* p1, const CMLuaScript* p2)
CMPlugin::CMPlugin()
: PLUGIN(MODULENAME, pluginInfoEx),
- lua(nullptr),
- Scripts(1, ScriptsCompare)
+ L(nullptr),
+ m_scripts(1, ScriptsCompare)
{
MUUID muidLast = MIID_LAST;
g_hMLuaLangpack = GetPluginLangId(muidLast, 0);
@@ -37,47 +37,85 @@ CMPlugin::CMPlugin()
CreatePluginService(MS_LUA_EVAL, &CMPlugin::Eval);
}
+void CMPlugin::LoadLuaScripts()
+{
+ CMLuaScriptLoader::Load(L, m_scripts);
+}
+
+void CMPlugin::UnloadLuaScripts()
+{
+ for (auto &script : m_scripts.rev_iter()) {
+ script->Unload();
+ delete m_scripts.removeItem(&script);
+ }
+}
+
void CMPlugin::LoadLua()
{
- lua = new CMLua();
- lua->Load();
- CMLuaFunctionLoader::Load(lua->L);
- CMLuaModuleLoader::Load(lua->L);
- CMLuaScriptLoader::Load(lua->L);
+ Log("Loading lua engine");
+ L = luaL_newstate();
+ Log("Loading standard modules");
+ luaL_openlibs(L);
+
+ lua_atpanic(L, luaM_atpanic);
+
+ CMLuaFunctionLoader::Load(L);
+ CMLuaModuleLoader::Load(L);
+ LoadLuaScripts();
}
void CMPlugin::UnloadLua()
{
- Scripts.destroy();
+ UnloadLuaScripts();
+
+ if (L != nullptr) {
+ KillObjectEventHooks(L);
+ KillObjectServices(L);
+
+ Log("Unloading lua engine");
+
+ lua_close(L);
+ }
KillModuleIcons(this);
KillModuleSounds(this);
KillModuleMenus(this);
KillModuleHotkeys(this);
-
- KillObjectEventHooks(lua->L);
- KillObjectServices(lua->L);
-
- if (lua != nullptr) {
- delete lua;
- lua = nullptr;
- }
}
-void CMPlugin::Reload()
+void CMPlugin::ReloadLuaScripts()
{
- Unload();
- Load();
+ UnloadLuaScripts();
+ LoadLuaScripts();
}
/***********************************************/
-static int OnModulesLoaded(WPARAM, LPARAM)
+int CMPlugin::OnOptionsInit(WPARAM wParam, LPARAM)
+{
+ OPTIONSDIALOGPAGE odp = {};
+ odp.flags = ODPF_BOLDGROUPS | ODPF_UNICODE | ODPF_DONTTRANSLATE;
+ odp.szGroup.w = LPGENW("Services");
+ odp.szTitle.w = L"Lua";
+
+ odp.szTab.w = LPGENW("Scripts");
+ odp.pDialog = new CMLuaOptionsMain(*this);
+ g_plugin.addOptions(wParam, &odp);
+
+ odp.szTab.w = LPGENW("Evaluate");
+ odp.pDialog = new CMLuaEvaluateOptions(*this);
+ g_plugin.addOptions(wParam, &odp);
+
+ return 0;
+}
+
+int CMPlugin::OnModulesLoaded(WPARAM, LPARAM)
{
g_hCLibsFolder = FoldersRegisterCustomPathT(MODULENAME, "CLibsFolder", MIRLUA_PATHT, TranslateT("C libs folder"));
g_hScriptsFolder = FoldersRegisterCustomPathT(MODULENAME, "ScriptsFolder", MIRLUA_PATHT, TranslateT("Scripts folder"));
- HookEvent(ME_OPT_INITIALISE, OnOptionsInit);
+ HookPluginEvent(ME_OPT_INITIALISE, &CMPlugin::OnOptionsInit);
+
return 0;
}
@@ -87,7 +125,7 @@ int CMPlugin::Load()
LoadNetlib();
LoadLua();
- HookEvent(ME_SYSTEM_MODULESLOADED, OnModulesLoaded);
+ HookPluginEvent(ME_SYSTEM_MODULESLOADED, &CMPlugin::OnModulesLoaded);
return 0;
}
@@ -127,17 +165,14 @@ INT_PTR CMPlugin::Call(WPARAM wParam, LPARAM lParam)
const wchar_t *module = (const wchar_t*)wParam;
const wchar_t *function = (const wchar_t*)lParam;
- lua_pushstring(lua->L, ptrA(mir_utf8encodeW(module)));
- lua_pushstring(lua->L, ptrA(mir_utf8encodeW(function)));
+ lua_pushstring(L, T2Utf(module));
+ lua_pushstring(L, T2Utf(function));
- lua_newtable(lua->L);
- lua_pushcclosure(lua->L, mlua_call, 1);
+ lua_pushcfunction(L, mlua_call);
- CMLuaEnvironment env(lua->L);
- env.Load();
-
- wchar_t *result = mir_utf8decodeW(lua_tostring(lua->L, -1));
- lua_pop(lua->L, 1);
+ CMLuaEnvironment env(L);
+ wchar_t *result = env.Call();
+ env.Unload();
return (INT_PTR)result;
}
@@ -146,16 +181,9 @@ INT_PTR CMPlugin::Eval(WPARAM, LPARAM lParam)
{
const wchar_t *script = (const wchar_t*)lParam;
- if (luaL_loadstring(lua->L, ptrA(mir_utf8encodeW(script)))) {
- ReportError(lua->L);
- return NULL;
- }
-
- CMLuaEnvironment env(lua->L);
- env.Load();
-
- wchar_t *result = mir_utf8decodeW(lua_tostring(lua->L, -1));
- lua_pop(lua->L, 1);
+ CMLuaEnvironment env(L);
+ wchar_t *result = env.Eval(script);
+ env.Unload();
return (INT_PTR)result;
}
@@ -164,16 +192,9 @@ INT_PTR CMPlugin::Exec(WPARAM, LPARAM lParam)
{
const wchar_t *path = (const wchar_t*)lParam;
- if (luaL_loadfile(lua->L, ptrA(mir_utf8encodeW(path)))) {
- ReportError(lua->L);
- return NULL;
- }
-
- CMLuaEnvironment env(lua->L);
- env.Load();
-
- wchar_t *result = mir_utf8decodeW(lua_tostring(lua->L, -1));
- lua_pop(lua->L, 1);
+ CMLuaEnvironment env(L);
+ wchar_t *result = env.Exec(path);
+ env.Unload();
return (INT_PTR)result;
}
diff --git a/plugins/MirLua/src/plugin.h b/plugins/MirLua/src/plugin.h
index ac8b89142e..dc07965144 100644
--- a/plugins/MirLua/src/plugin.h
+++ b/plugins/MirLua/src/plugin.h
@@ -2,25 +2,30 @@
struct CMPlugin : public PLUGIN<CMPlugin>
{
- friend class CMLuaOptions;
+ friend class CMLuaOptionsMain;
+ friend class CMLuaEvaluateOptions;
private:
- CMLua *lua;
+ lua_State *L;
void LoadLua();
void UnloadLua();
+ OBJLIST<CMLuaScript> m_scripts;
+ void LoadLuaScripts();
+ void UnloadLuaScripts();
+ void ReloadLuaScripts();
+
+ int __cdecl OnOptionsInit(WPARAM wParam, LPARAM);
+ int __cdecl OnModulesLoaded(WPARAM, LPARAM);
+
INT_PTR __cdecl Eval(WPARAM, LPARAM);
INT_PTR __cdecl Call(WPARAM, LPARAM);
INT_PTR __cdecl Exec(WPARAM, LPARAM);
public:
- OBJLIST<CMLuaScript> Scripts;
-
CMPlugin();
- void Reload();
-
int Load() override;
int Unload() override;
};
diff --git a/plugins/MirLua/src/resource.h b/plugins/MirLua/src/resource.h
index 6085769a72..2e612e42ca 100644
--- a/plugins/MirLua/src/resource.h
+++ b/plugins/MirLua/src/resource.h
@@ -5,12 +5,17 @@
#define IDI_ICON 100
#define IDI_SCRIPT 100
#define IDI_OPEN 105
-#define IDD_OPTIONS 106
+#define IDD_OPTIONSMAIN 106
#define IDD_SCRIPTOPTIONSPAGE 107
#define IDI_RELOAD 108
#define IDI_FAILED 109
#define IDI_LOADED 110
#define IDI_COMPILE 111
+#define IDD_OPTIONSEVALUATE 113
+#define IDC_SCRIPTTEXT 1000
+#define IDC_EVALUATE 1001
+#define IDC_AUTOEVAL 1002
+#define IDC_SCRIPTRESULT 1003
#define IDC_SCRIPTS 1011
#define IDC_RELOAD 1012
#define IDC_POPUPONERROR 1013
@@ -20,7 +25,7 @@
//
#ifdef APSTUDIO_INVOKED
#ifndef APSTUDIO_READONLY_SYMBOLS
-#define _APS_NEXT_RESOURCE_VALUE 112
+#define _APS_NEXT_RESOURCE_VALUE 115
#define _APS_NEXT_COMMAND_VALUE 40001
#define _APS_NEXT_CONTROL_VALUE 1015
#define _APS_NEXT_SYMED_VALUE 101
diff --git a/plugins/MirLua/src/script.cpp b/plugins/MirLua/src/script.cpp
index dc379c907c..2337a6737d 100644
--- a/plugins/MirLua/src/script.cpp
+++ b/plugins/MirLua/src/script.cpp
@@ -3,8 +3,10 @@
#define MT_SCRIPT "SCRIPT"
CMLuaScript::CMLuaScript(lua_State *L, const wchar_t *path)
- : CMLuaEnvironment(L), isBinary(false),
- status(ScriptStatus::None), unloadRef(LUA_NOREF)
+ : CMLuaEnvironment(L),
+ isBinary(false),
+ status(ScriptStatus::None),
+ unloadRef(LUA_NOREF)
{
mir_wstrcpy(filePath, path);
@@ -32,7 +34,7 @@ CMLuaScript::CMLuaScript(const CMLuaScript &script)
CMLuaScript::~CMLuaScript()
{
- Unload();
+ //Unload();
mir_free((void*)m_szModuleName);
mir_free((void*)scriptName);
}
@@ -81,7 +83,9 @@ int CMLuaScript::Load()
return false;
}
- if (!CMLuaEnvironment::Load()) {
+ CMLuaEnvironment::CreateEnvironmentTable();
+
+ if (lua_pcall(L, 0, 1, 0) != LUA_OK) {
ReportError(L);
return false;
}
@@ -138,7 +142,8 @@ int CMLuaScript::Unload()
lua_pushnil(L);
lua_setfield(L, -2, m_szModuleName);
lua_pop(L, 1);
- return 0;
+
+ return CMLuaEnvironment::Unload();
}
bool CMLuaScript::Reload()
diff --git a/plugins/MirLua/src/script_loader.cpp b/plugins/MirLua/src/script_loader.cpp
index 4782d3ea85..1b70d0f039 100644
--- a/plugins/MirLua/src/script_loader.cpp
+++ b/plugins/MirLua/src/script_loader.cpp
@@ -3,8 +3,8 @@
HANDLE g_hCLibsFolder = nullptr;
HANDLE g_hScriptsFolder = nullptr;
-CMLuaScriptLoader::CMLuaScriptLoader(lua_State *L)
- : L(L)
+CMLuaScriptLoader::CMLuaScriptLoader(lua_State *L, OBJLIST<CMLuaScript> &scripts)
+ : L(L), m_scripts(scripts)
{
}
@@ -37,20 +37,20 @@ void CMLuaScriptLoader::LoadScript(const wchar_t *scriptDir, const wchar_t *file
CMLuaScript *script = new CMLuaScript(L, path);
- const CMLuaScript *found = g_plugin.Scripts.find(script);
+ const CMLuaScript *found = m_scripts.find(script);
if (found != nullptr) {
Log(L"%s:PASS", script->GetFilePath());
delete script;
return;
}
+ m_scripts.insert(script);
+
if (!script->IsEnabled()) {
Log(L"%s:PASS", path);
return;
}
- g_plugin.Scripts.insert(script);
-
if (script->Load())
Log(L"%s:OK", path);
}
@@ -92,7 +92,7 @@ void CMLuaScriptLoader::LoadScripts()
}
}
-void CMLuaScriptLoader::Load(lua_State *L)
+void CMLuaScriptLoader::Load(lua_State *L, OBJLIST<CMLuaScript> &scripts)
{
- CMLuaScriptLoader(L).LoadScripts();
+ CMLuaScriptLoader(L, scripts).LoadScripts();
} \ No newline at end of file
diff --git a/plugins/MirLua/src/script_loader.h b/plugins/MirLua/src/script_loader.h
index 1b7b87eab5..d57bf83fd7 100644
--- a/plugins/MirLua/src/script_loader.h
+++ b/plugins/MirLua/src/script_loader.h
@@ -5,7 +5,9 @@ class CMLuaScriptLoader
private:
lua_State *L;
- CMLuaScriptLoader(lua_State *L);
+ OBJLIST<CMLuaScript> &m_scripts;
+
+ CMLuaScriptLoader(lua_State *L, OBJLIST<CMLuaScript> &scripts);
void SetPaths();
@@ -13,5 +15,5 @@ private:
void LoadScripts();
public:
- static void Load(lua_State *L);
+ static void Load(lua_State *L, OBJLIST<CMLuaScript> &scripts);
};
diff --git a/plugins/MirLua/src/stdafx.h b/plugins/MirLua/src/stdafx.h
index 2aabb9c8a5..c24b840447 100644
--- a/plugins/MirLua/src/stdafx.h
+++ b/plugins/MirLua/src/stdafx.h
@@ -37,7 +37,6 @@
class CMLuaScript;
-#include "lua.h"
#include "plugin.h"
#include "modules.h"
#include "environment.h"
@@ -72,8 +71,6 @@ void LoadIcons();
HICON GetIcon(int iconId);
HANDLE GetIconHandle(int iconId);
-int OnOptionsInit(WPARAM wParam, LPARAM);
-
/* utils */
void Log(const char *format, ...);
diff --git a/plugins/MirLua/src/version.h b/plugins/MirLua/src/version.h
index dea60e0b6b..64b5931bfb 100644
--- a/plugins/MirLua/src/version.h
+++ b/plugins/MirLua/src/version.h
@@ -1,7 +1,7 @@
#define __MAJOR_VERSION 0
#define __MINOR_VERSION 11
#define __RELEASE_NUM 9
-#define __BUILD_NUM 0
+#define __BUILD_NUM 1
#include <stdver.h>