This commit is contained in:
2026-01-01 20:18:50 +01:00
parent c992bca55f
commit 91c4ebd6c7
5 changed files with 240 additions and 129 deletions

219
plugin.c
View File

@@ -47,6 +47,10 @@ static lua_State* L = NULL;
static HWND hEditControl = NULL;
static TDB* g_DBPointer = NULL;
static HFONT g_hCurrentFont = NULL;
#define FONT_NAME "Segoe UI"
#define IDC_MAIN_BUTTON 101
static const unsigned char EBU[127] = {
0xE1, 'a', 0xE9, 'e', 0xED, 'i', 0xF3, 'o', 0xFA, 'u', 'N', 0xC7, 0xAA, 0xDF, 'I', 0x00,
@@ -75,6 +79,8 @@ unsigned char CharConv(unsigned char Ch) {
return Ch;
}
void InitLua();
void lua_event(int event);
LRESULT CALLBACK WndProc(HWND hwnd, UINT msg, WPARAM wParam, LPARAM lParam)
{
switch (msg)
@@ -82,6 +88,15 @@ LRESULT CALLBACK WndProc(HWND hwnd, UINT msg, WPARAM wParam, LPARAM lParam)
case WM_CLOSE:
ShowWindow(hwnd, SW_HIDE);
return 0;
case WM_COMMAND:
if (HIWORD(wParam) == BN_CLICKED)
{
int controlId = LOWORD(wParam);
if (controlId == IDC_MAIN_BUTTON) InitLua();
else if (controlId > IDC_MAIN_BUTTON && controlId <= IDC_MAIN_BUTTON + 5) lua_event(controlId - IDC_MAIN_BUTTON);
}
break;
case WM_DESTROY:
hWnd = NULL;
return 0;
@@ -115,12 +130,62 @@ void CreatePluginWindow(HWND hOwner) {
WS_EX_CLIENTEDGE, "EDIT", "",
WS_CHILD | WS_VISIBLE | WS_VSCROLL |
ES_LEFT | ES_MULTILINE | ES_AUTOVSCROLL | ES_READONLY,
10, 10, 480-30, 320-50,
10, 10, 480-25, 320-75,
hWnd, NULL, hInst, NULL
);
HFONT hFont = CreateFont(18, 0, 0, 0, FW_NORMAL, FALSE, FALSE, FALSE, DEFAULT_CHARSET, OUT_DEFAULT_PRECIS, CLIP_DEFAULT_PRECIS, DEFAULT_QUALITY, DEFAULT_PITCH | FF_DONTCARE, "Segoe UI");
HWND hButton = CreateWindowEx(
0, "BUTTON", "Reload",
WS_TABSTOP | WS_VISIBLE | WS_CHILD | BS_PUSHBUTTON,
10, 258,
70, 30, hWnd,
(HMENU)IDC_MAIN_BUTTON, hInst, NULL
);
HWND hButton_event1 = CreateWindowEx(
0, "BUTTON", "Event 1",
WS_TABSTOP | WS_VISIBLE | WS_CHILD | BS_PUSHBUTTON,
10+(75*1), 258,
70, 30, hWnd,
(HMENU)(IDC_MAIN_BUTTON + 1), hInst, NULL
);
HWND hButton_event2 = CreateWindowEx(
0, "BUTTON", "Event 2",
WS_TABSTOP | WS_VISIBLE | WS_CHILD | BS_PUSHBUTTON,
10+(75*2), 258,
70, 30, hWnd,
(HMENU)(IDC_MAIN_BUTTON + 2), hInst, NULL
);
HWND hButton_event3 = CreateWindowEx(
0, "BUTTON", "Event 3",
WS_TABSTOP | WS_VISIBLE | WS_CHILD | BS_PUSHBUTTON,
10+(75*3), 258,
70, 30, hWnd,
(HMENU)(IDC_MAIN_BUTTON + 3), hInst, NULL
);
HWND hButton_event4 = CreateWindowEx(
0, "BUTTON", "Event 4",
WS_TABSTOP | WS_VISIBLE | WS_CHILD | BS_PUSHBUTTON,
10+(75*4), 258,
70, 30, hWnd,
(HMENU)(IDC_MAIN_BUTTON + 4), hInst, NULL
);
HWND hButton_event5 = CreateWindowEx(
0, "BUTTON", "Event 5",
WS_TABSTOP | WS_VISIBLE | WS_CHILD | BS_PUSHBUTTON,
10+(75*5), 258,
70, 30, hWnd,
(HMENU)(IDC_MAIN_BUTTON + 5), hInst, NULL
);
HFONT hFont = CreateFont(18, 0, 0, 0, FW_NORMAL, FALSE, FALSE, FALSE, DEFAULT_CHARSET, OUT_DEFAULT_PRECIS, CLIP_DEFAULT_PRECIS, DEFAULT_QUALITY, DEFAULT_PITCH | FF_DONTCARE, FONT_NAME);
SendMessage(hEditControl, WM_SETFONT, (WPARAM)hFont, TRUE);
SendMessage(hButton, WM_SETFONT, (WPARAM)hFont, TRUE);
SendMessage(hButton_event1, WM_SETFONT, (WPARAM)hFont, TRUE);
SendMessage(hButton_event2, WM_SETFONT, (WPARAM)hFont, TRUE);
SendMessage(hButton_event3, WM_SETFONT, (WPARAM)hFont, TRUE);
SendMessage(hButton_event4, WM_SETFONT, (WPARAM)hFont, TRUE);
SendMessage(hButton_event5, WM_SETFONT, (WPARAM)hFont, TRUE);
HICON hIconBig = (HICON)SendMessage(hOwner, WM_GETICON, ICON_BIG, 0);
HICON hIconSmall = (HICON)SendMessage(hOwner, WM_GETICON, ICON_SMALL, 0);
@@ -160,13 +225,29 @@ int lua_set_console(lua_State* localL) {
}
int lua_set_console_mode(lua_State* localL) {
if (!lua_isboolean(localL, 1)) return luaL_error(localL, "boolean expected, got %s", luaL_typename(localL, 1));
if (!lua_isboolean(localL, 1)) return luaL_error(localL, "boolean expected, got %s", luaL_typename(localL, 1));
int mode = lua_toboolean(localL, 1);
SetText("");
console_mode = mode;
return 0;
}
int lua_set_font_size(lua_State* localL) {
int size = luaL_checkinteger(localL, 1);
HFONT hNewFont = CreateFont(size, 0, 0, 0, FW_NORMAL, FALSE, FALSE, FALSE, DEFAULT_CHARSET, OUT_DEFAULT_PRECIS, CLIP_DEFAULT_PRECIS, DEFAULT_QUALITY, DEFAULT_PITCH | FF_DONTCARE, FONT_NAME);
if (hNewFont && hEditControl) {
SendMessage(hEditControl, WM_SETFONT, (WPARAM)hNewFont, TRUE);
if (g_hCurrentFont != NULL) DeleteObject(g_hCurrentFont);
g_hCurrentFont = hNewFont;
}
return 0;
}
int lua_MessageBox(lua_State* localL) {
const char* data = luaL_checkstring(localL, 1);
const char* title = luaL_checkstring(localL, 2);
@@ -395,7 +476,7 @@ void lua_call_command(const char* Cmd, const char* Param) {
lua_pushstring(L, Param);
if (lua_pcall(L, 2, 0, 0) != LUA_OK) {
char msg_buffer[255];
sprintf(msg_buffer, "Lua error: %s at '%s'\n", lua_tostring(L, -1), "command");
snprintf(msg_buffer, sizeof(msg_buffer), "Lua error: %s at '%s'\n", lua_tostring(L, -1), "command");
AppendText(msg_buffer);
lua_pop(L, 1);
}
@@ -414,7 +495,21 @@ void lua_call_group() {
lua_pushinteger(L, Group.Blk4);
if (lua_pcall(L, 6, 0, 0) != LUA_OK) {
char msg_buffer[255];
sprintf(msg_buffer, "Lua error: %s at '%s'\r\n", lua_tostring(L, -1), "command");
snprintf(msg_buffer, sizeof(msg_buffer), "Lua error: %s at '%s'\r\n", lua_tostring(L, -1), "group");
AppendText(msg_buffer);
lua_pop(L, 1);
}
} else lua_pop(L, 1);
}
void lua_event(int event) {
lua_getglobal(L, "event");
if (lua_isfunction(L, -1)) {
lua_pushinteger(L, event);
if (lua_pcall(L, 1, 0, 0) != LUA_OK) {
char msg_buffer[255];
snprintf(msg_buffer, sizeof(msg_buffer), "Lua error: %s at '%s'\r\n", lua_tostring(L, -1), "event");
AppendText(msg_buffer);
lua_pop(L, 1);
}
@@ -466,12 +561,12 @@ __declspec(dllexport) void __stdcall Command(const char* Cmd, const char* Param)
} else if (_stricmp(Cmd, "LUASCRIPT") == 0) { // custom
char msg_buffer[255];
if (luaL_loadfile(L, Param) != LUA_OK) {
sprintf(msg_buffer, "Lua error loading file: %s\r\n", lua_tostring(L, -1));
snprintf(msg_buffer, sizeof(msg_buffer), "Lua error loading file: %s\r\n", lua_tostring(L, -1));
AppendText(msg_buffer);
lua_pop(L, 1);
} else {
if (lua_pcall(L, 0, 0, 0) != LUA_OK) {
sprintf(msg_buffer, "Init error: %s\r\n", lua_tostring(L, -1));
snprintf(msg_buffer, sizeof(msg_buffer), "Init error: %s\r\n", lua_tostring(L, -1));
AppendText(msg_buffer);
lua_pop(L, 1);
}
@@ -481,60 +576,60 @@ __declspec(dllexport) void __stdcall Command(const char* Cmd, const char* Param)
__declspec(dllexport) const char* __stdcall PluginName(void) { return "Lua Host"; }
void RegisterDBFunctions(lua_State* localL) {
lua_newtable(localL);
lua_pushcfunction(localL, lua_ReadValue);
lua_setfield(localL, -2, "read_value");
lua_pushcfunction(localL, lua_ReadRecord);
lua_setfield(localL, -2, "read_record");
lua_pushcfunction(localL, lua_AddValue);
lua_setfield(localL, -2, "add_value");
lua_pushcfunction(localL, lua_ResetValues);
lua_setfield(localL, -2, "reset_values");
lua_pushcfunction(localL, lua_CountRecords);
lua_setfield(localL, -2, "count_records");
lua_pushcfunction(localL, lua_CharConv);
lua_setfield(localL, -2, "char_conv");
lua_pushcfunction(localL, lua_SaveString);
lua_setfield(localL, -2, "save_string");
lua_pushcfunction(localL, lua_SaveInteger);
lua_setfield(localL, -2, "save_integer");
lua_pushcfunction(localL, lua_SaveBoolean);
lua_setfield(localL, -2, "save_boolean");
lua_pushcfunction(localL, lua_LoadString);
lua_setfield(localL, -2, "load_string");
lua_pushcfunction(localL, lua_LoadInteger);
lua_setfield(localL, -2, "load_integer");
lua_pushcfunction(localL, lua_LoadBoolean);
lua_setfield(localL, -2, "load_boolean");
lua_setglobal(localL, "db");
}
__declspec(dllexport) int __stdcall Initialize(HANDLE hHandle, TDB* DBPointer) {
CreatePluginWindow(hHandle);
AppendText(LUA_COPYRIGHT);
AppendText("\r\n");
void InitLua() {
if(L != NULL) {
lua_close(L);
L = NULL;
}
L = luaL_newstate();
luaL_openlibs(L);
lua_register(L, "set_font_size", lua_set_font_size);
lua_register(L, "message_box", lua_MessageBox);
lua_register(L, "log", lua_log);
lua_register(L, "set_console", lua_set_console);
lua_register(L, "set_console_mode", lua_set_console_mode);
g_DBPointer = DBPointer;
RegisterDBFunctions(L);
lua_newtable(L);
lua_pushcfunction(L, lua_ReadValue);
lua_setfield(L, -2, "read_value");
lua_pushcfunction(L, lua_ReadRecord);
lua_setfield(L, -2, "read_record");
lua_pushcfunction(L, lua_AddValue);
lua_setfield(L, -2, "add_value");
lua_pushcfunction(L, lua_ResetValues);
lua_setfield(L, -2, "reset_values");
lua_pushcfunction(L, lua_CountRecords);
lua_setfield(L, -2, "count_records");
lua_pushcfunction(L, lua_CharConv);
lua_setfield(L, -2, "char_conv");
lua_pushcfunction(L, lua_SaveString);
lua_setfield(L, -2, "save_string");
lua_pushcfunction(L, lua_SaveInteger);
lua_setfield(L, -2, "save_integer");
lua_pushcfunction(L, lua_SaveBoolean);
lua_setfield(L, -2, "save_boolean");
lua_pushcfunction(L, lua_LoadString);
lua_setfield(L, -2, "load_string");
lua_pushcfunction(L, lua_LoadInteger);
lua_setfield(L, -2, "load_integer");
lua_pushcfunction(L, lua_LoadBoolean);
lua_setfield(L, -2, "load_boolean");
lua_setglobal(L, "db");
console_mode = 0;
char path[MAX_PATH];
char fullPath[MAX_PATH];
@@ -543,21 +638,29 @@ __declspec(dllexport) int __stdcall Initialize(HANDLE hHandle, TDB* DBPointer) {
else {
MessageBoxA(NULL, "Could not get the local app data path", "Error", MB_ICONERROR | MB_OK | MB_TOPMOST);
AppendText("Could not get the local app data path\r\n");
return (int)hWnd;
return;
}
char msg_buffer[255];
if (luaL_loadfile(L, fullPath) != LUA_OK) {
sprintf(msg_buffer, "Lua error loading file: %s\r\n", lua_tostring(L, -1));
snprintf(msg_buffer, sizeof(msg_buffer), "Lua error loading file: %s\r\n", lua_tostring(L, -1));
AppendText(msg_buffer);
lua_pop(L, 1);
} else {
if (lua_pcall(L, 0, 0, 0) != LUA_OK) {
sprintf(msg_buffer, "Init error: %s\r\n", lua_tostring(L, -1));
snprintf(msg_buffer, sizeof(msg_buffer), "Lua error: %s\r\n", lua_tostring(L, -1));
AppendText(msg_buffer);
lua_pop(L, 1);
}
}
}
__declspec(dllexport) int __stdcall Initialize(HANDLE hHandle, TDB* DBPointer) {
CreatePluginWindow(hHandle);
AppendText(LUA_COPYRIGHT);
AppendText("\r\n");
g_DBPointer = DBPointer;
InitLua();
return (int)hWnd;
}