From 6e3dba14ff3fc9976cae069281fe93a68210843f Mon Sep 17 00:00:00 2001 From: zbx1425 Date: Sat, 17 Apr 2021 12:44:17 +0800 Subject: [PATCH] Fix only one will be handled if there are multiple events on a single frame --- BatsWinApi/BatsWinApi.vcxproj | 6 +- BatsWinApi/BatsWinApi.vcxproj.filters | 16 +- BatsWinApi/globals.cpp | 7 + BatsWinApi/globals.h | 8 + BatsWinApi/loader.cpp | 119 +++++++ BatsWinApi/luainterop.cpp | 94 +++++ BatsWinApi/luainterop.h | 4 + BatsWinApi/pch.h | 15 + BatsWinApi/plugin.cpp | 320 ++++-------------- BatsWinApi/plugin.h | 23 -- .../UserInterface/FormAbout.Designer.cs | 3 +- BlocklyAtsGui/UserInterface/FormAbout.cs | 5 + .../UserInterface/FormBugReport.Designer.cs | 2 +- BlocklyAtsGui/UserInterface/FormBugReport.cs | 2 +- BlocklyAtsGui/Workspace/CompilerFunction.cs | 8 +- 15 files changed, 351 insertions(+), 281 deletions(-) create mode 100644 BatsWinApi/globals.cpp create mode 100644 BatsWinApi/globals.h create mode 100644 BatsWinApi/loader.cpp create mode 100644 BatsWinApi/luainterop.cpp create mode 100644 BatsWinApi/luainterop.h delete mode 100644 BatsWinApi/plugin.h diff --git a/BatsWinApi/BatsWinApi.vcxproj b/BatsWinApi/BatsWinApi.vcxproj index a8f0744..89a55ed 100644 --- a/BatsWinApi/BatsWinApi.vcxproj +++ b/BatsWinApi/BatsWinApi.vcxproj @@ -179,11 +179,15 @@ + + - + + + diff --git a/BatsWinApi/BatsWinApi.vcxproj.filters b/BatsWinApi/BatsWinApi.vcxproj.filters index 5cc87d3..0a75b27 100644 --- a/BatsWinApi/BatsWinApi.vcxproj.filters +++ b/BatsWinApi/BatsWinApi.vcxproj.filters @@ -30,10 +30,13 @@ 头文件 - + 头文件 - + + 头文件 + + 头文件 @@ -50,5 +53,14 @@ 源文件 + + 源文件 + + + 源文件 + + + 源文件 + \ No newline at end of file diff --git a/BatsWinApi/globals.cpp b/BatsWinApi/globals.cpp new file mode 100644 index 0000000..0b6f131 --- /dev/null +++ b/BatsWinApi/globals.cpp @@ -0,0 +1,7 @@ +#include "pch.h" + +lua_State *L = NULL; + +ATS_VEHICLESPEC vSpec; +int phPower, phBrake, phReverser; +int *bvePanel, *bveSound; \ No newline at end of file diff --git a/BatsWinApi/globals.h b/BatsWinApi/globals.h new file mode 100644 index 0000000..b60b0b6 --- /dev/null +++ b/BatsWinApi/globals.h @@ -0,0 +1,8 @@ +#pragma once +#include "pch.h" + +extern lua_State *L; + +extern ATS_VEHICLESPEC vSpec; +extern int phPower, phBrake, phReverser; +extern int *bvePanel, *bveSound; \ No newline at end of file diff --git a/BatsWinApi/loader.cpp b/BatsWinApi/loader.cpp new file mode 100644 index 0000000..ea8b390 --- /dev/null +++ b/BatsWinApi/loader.cpp @@ -0,0 +1,119 @@ +#include "pch.h" +#include "globals.h" +#include "winfile.h" +#include "luainterop.h" + +wchar_t dllPath[2048]; +char buffer[4096]; +DWORD read; char* luaCode; + +ATS_API void WINAPI Load() { + HMODULE hm = NULL; + + if (GetModuleHandleEx(GET_MODULE_HANDLE_EX_FLAG_FROM_ADDRESS | GET_MODULE_HANDLE_EX_FLAG_UNCHANGED_REFCOUNT, + (LPCWSTR)&Dispose, &hm) == 0) { + MessageBox(NULL, L"GetModuleHandleEx failed, ATS plugin cannot load", L"BlocklyAts Error", MB_ICONERROR); + return; + } + if (GetModuleFileName(hm, dllPath, sizeof(dllPath)) == 0) { + MessageBox(NULL, L"GetModuleFileName failed, ATS plugin cannot load", L"BlocklyAts Error", MB_ICONERROR); + return; + } + + HANDLE hFile = CreateFile(dllPath, GENERIC_READ, FILE_SHARE_READ, NULL, OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL, NULL); + if (INVALID_HANDLE_VALUE == hFile) { + MessageBox(NULL, L"CreateFile failed, ATS plugin cannot load", L"BlocklyAts Error", MB_ICONERROR); + return; + } + + ReadFile(hFile, buffer, sizeof(buffer), &read, NULL); + // The previous solution of calculating PE section size doesn't seem to work on 64-bit DLL + // I should've tried to find out why, but I cannot find how to solve this problem + DWORD exesize = *(int*)(buffer + 0x6C); // the word "DOS " in DOS stub will be replaced to describe the size + DWORD filesize = GetFileSize(hFile, NULL); + if (filesize - exesize <= 0) { + CloseHandle(hFile); + MessageBox(NULL, L"No extra code, ATS plugin cannot load", L"BlocklyAts Error", MB_ICONERROR); + return; + } + + SetFilePointer(hFile, exesize, NULL, FILE_BEGIN); + luaCode = new char[filesize - exesize + 1]; + ReadFile(hFile, luaCode, filesize - exesize, &read, NULL); + CloseHandle(hFile); + + // I knew I should've used bytecode, but then + // it'll be more difficult to compile on Mono, because you'll need to ship luac with different arch + // and somehow I could not get it to work, and I could not find out why + // So there is a quite easy xor obfuscation, to prevent the code from being stolen too easily + char confusion[] = { 0x11, 0x45, 0x14, 0x19, 0x19, 0x81, 0x14, 0x25 }; + for (DWORD i = 0; i < filesize - exesize; i++) luaCode[i] ^= confusion[i % 8]; + + L = luaL_newstate(); + luaL_openlibs(L); + luaL_requiref(L, "winfile", luaopen_winfile, 1); + open_interop_functions(); + + *(wcsrchr(dllPath, '\\') + 1) = 0; + const char * sBuf = lua_tostring(L, -1); + DWORD dBufSize = WideCharToMultiByte(CP_UTF8, 0, dllPath, -1, NULL, 0, NULL, FALSE); + char* dllPathMB = new char[dBufSize]; + WideCharToMultiByte(CP_UTF8, 0, dllPath, -1, dllPathMB, dBufSize, NULL, FALSE); + l_setglobalS("_vdlldir", dllPathMB); + + int error = luaL_loadbuffer(L, luaCode, filesize - exesize, "bats") || lua_pcall(L, 0, LUA_MULTRET, 0); + + if (error) { + l_printerr(); + } + else { + lua_getglobal(L, "_eload"); + if (lua_pcall(L, 0, 0, 0) != 0) l_printerr(); + } +} + +ATS_API int WINAPI GetPluginVersion() { + return ATS_VERSION; +} + +ATS_API void WINAPI Dispose() { + if (L == NULL) return; + lua_getglobal(L, "_edispose"); + if (lua_pcall(L, 0, 0, 0) != 0) l_printerr(); + lua_close(L); +} + +ATS_API void WINAPI SetVehicleSpec(ATS_VEHICLESPEC vehicleSpec) { + vSpec = vehicleSpec; + if (L == NULL) return; + l_setglobalI("_bVsAtsNotch", vehicleSpec.AtsNotch); + l_setglobalI("_bVsB67Notch", vehicleSpec.B67Notch); + l_setglobalI("_bVsBrakeNotches", vehicleSpec.BrakeNotches); + l_setglobalI("_bVsCars", vehicleSpec.Cars); + l_setglobalI("_bVsPowerNotches", vehicleSpec.PowerNotches); +} + +ATS_API void WINAPI Initialize(int initIndex) { + if (L == NULL) return; + lua_getglobal(L, "_einitialize"); + lua_pushinteger(L, initIndex); + if (lua_pcall(L, 1, 0, 0) != 0) l_printerr(); +} + +ATS_API void WINAPI SetPower(int notch) { + phPower = notch; + if (L == NULL) return; + l_setglobalI("_bHPower", notch); +} + +ATS_API void WINAPI SetBrake(int notch) { + phBrake = notch; + if (L == NULL) return; + l_setglobalI("_bHBrake", notch); +} + +ATS_API void WINAPI SetReverser(int pos) { + phReverser = pos; + if (L == NULL) return; + l_setglobalI("_bHReverser", pos); +} diff --git a/BatsWinApi/luainterop.cpp b/BatsWinApi/luainterop.cpp new file mode 100644 index 0000000..57f0174 --- /dev/null +++ b/BatsWinApi/luainterop.cpp @@ -0,0 +1,94 @@ +#include "pch.h" +#include "luainterop.h" +#include "globals.h" + +void l_printerr() { + const char * sBuf = lua_tostring(L, -1); + DWORD dBufSize = MultiByteToWideChar(CP_UTF8, 0, sBuf, -1, NULL, 0); + std::vector dBuf(dBufSize); + int msgBoxResult; + if (MultiByteToWideChar(CP_UTF8, 0, sBuf, -1, dBuf.data(), dBufSize) > 0) { + msgBoxResult = MessageBoxW(NULL, dBuf.data(), L"BlocklyAts Lua Script Error", MB_RETRYCANCEL | MB_ICONERROR); + } + else { + msgBoxResult = MessageBoxA(NULL, sBuf, "BlocklyAts Lua Script Error", MB_RETRYCANCEL | MB_ICONERROR); + } + if (msgBoxResult == IDCANCEL) { + lua_close(L); + L = NULL; + } +} + +static int l_panel_getset(lua_State *L) { + int id = luaL_checkinteger(L, 1); + if (id < 0 || id > 255) return luaL_error(L, "_fpanel_getset expects id 0~255"); + if (lua_gettop(L) == 1) { + lua_pushinteger(L, bvePanel[id]); + return 1; + } + else if (lua_gettop(L) == 2) { + int val = luaL_checkinteger(L, 2); + bvePanel[id] = val; + return 0; + } + else { + return luaL_error(L, "_fpanel_getset expects no more than 2 arguments"); + } +} + +static int l_sound_getset(lua_State *L) { + int id = luaL_checkinteger(L, 1); + if (id < 0 || id > 255) return luaL_error(L, "_fsound_getset expects id 0~255"); + if (lua_gettop(L) == 1) { + lua_pushinteger(L, bveSound[id]); + return 1; + } + else if (lua_gettop(L) == 2) { + int val = luaL_checkinteger(L, 2); + if (val > 2) { + bveSound[id] = 2; + } + else if (val < -10000) { + bveSound[id] = -10000; + } + else { + bveSound[id] = val; + } + return 0; + } + else if (lua_gettop(L) == 3) { + float vol = luaL_checknumber(L, 3); + if (vol >= 100) { + bveSound[id] = 0; + } + else if (vol <= 0) { + bveSound[id] = -10000; + } + else { + bveSound[id] = (int)(vol * 100) - 10000; + } + return 0; + } + else { + return luaL_error(L, "_fsound_getset expects no more than 3 arguments"); + } +} + +static int l_msgbox(lua_State *L) { + const char * sBuf = luaL_checkstring(L, 1); + DWORD dBufSize = MultiByteToWideChar(CP_UTF8, 0, sBuf, -1, NULL, 0); + std::vector dBuf(dBufSize); + if (MultiByteToWideChar(CP_UTF8, 0, sBuf, -1, dBuf.data(), dBufSize) > 0) { + MessageBoxW(NULL, dBuf.data(), L"BlocklyAts Message", 0); + } + else { + MessageBoxA(NULL, sBuf, "BlocklyAts Message", 0); + } + return 0; +} + +void open_interop_functions() { + l_setglobalF("_fpanel", l_panel_getset); + l_setglobalF("_fsound", l_sound_getset); + l_setglobalF("_fmsgbox", l_msgbox); +} \ No newline at end of file diff --git a/BatsWinApi/luainterop.h b/BatsWinApi/luainterop.h new file mode 100644 index 0000000..4397d06 --- /dev/null +++ b/BatsWinApi/luainterop.h @@ -0,0 +1,4 @@ +#pragma once + +void l_printerr(); +void open_interop_functions(); \ No newline at end of file diff --git a/BatsWinApi/pch.h b/BatsWinApi/pch.h index 0463345..6ef1b07 100644 --- a/BatsWinApi/pch.h +++ b/BatsWinApi/pch.h @@ -13,9 +13,24 @@ #include "framework.h" #include +#include #include "lua54/include/lua.hpp" #include "atsplugin.h" +#ifdef _WIN64 +#pragma comment (lib, "lua54/lua54_x64_static.lib") +#else +#pragma comment (lib, "lua54/lua54_x86_static.lib") +#endif + +#define l_setglobalN(name, val) lua_pushnumber(L, val);lua_setglobal(L, name); + +#define l_setglobalI(name, val) lua_pushinteger(L, val);lua_setglobal(L, name); + +#define l_setglobalF(name, val) lua_pushcfunction(L, val);lua_setglobal(L, name); + +#define l_setglobalS(name, val) lua_pushstring(L, val);lua_setglobal(L, name); + #endif //PCH_H diff --git a/BatsWinApi/plugin.cpp b/BatsWinApi/plugin.cpp index 83fb0eb..e5b394b 100644 --- a/BatsWinApi/plugin.cpp +++ b/BatsWinApi/plugin.cpp @@ -1,192 +1,68 @@ #include "pch.h" -#include "framework.h" -#include "plugin.h" -#include "winfile.h" - -lua_State *L = NULL; - -ATS_VEHICLESPEC vSpec; -int phPower, phBrake, phReverser; -int *bvePanel, *bveSound; - -void l_printerr() { - const char * sBuf = lua_tostring(L, -1); - DWORD dBufSize = MultiByteToWideChar(CP_UTF8, 0, sBuf, -1, NULL, 0); - std::vector dBuf(dBufSize); - int msgBoxResult; - if (MultiByteToWideChar(CP_UTF8, 0, sBuf, -1, dBuf.data(), dBufSize) > 0) { - msgBoxResult = MessageBoxW(NULL, dBuf.data(), L"BlocklyAts Lua Script Error", MB_RETRYCANCEL | MB_ICONERROR); - } else { - msgBoxResult = MessageBoxA(NULL, sBuf, "BlocklyAts Lua Script Error", MB_RETRYCANCEL | MB_ICONERROR); - } - if (msgBoxResult == IDCANCEL) { - lua_close(L); - L = NULL; - } -} - -static int l_panel_getset(lua_State *L) { - int id = luaL_checkinteger(L, 1); - if (id < 0 || id > 255) return luaL_error(L, "_fpanel_getset expects id 0~255"); - if (lua_gettop(L) == 1) { - lua_pushinteger(L, bvePanel[id]); - return 1; - } else if (lua_gettop(L) == 2) { - int val = luaL_checkinteger(L, 2); - bvePanel[id] = val; - return 0; - } else { - return luaL_error(L, "_fpanel_getset expects no more than 2 arguments"); +#include "globals.h" +#include "luainterop.h" + +bool elapseDataReady = false; +struct lafc { + short type; + float f1; + int i1, i2, i3; + static lafc doorChange(bool b1) { return lafc{ 0, 0.0f, b1, 0, 0 }; } + static lafc setSignal(int i1) { return lafc{ 1, 0.0f, i1, 0, 0 }; } + static lafc setBeaconData(float f1, int i1, int i2, int i3) { return lafc{ 2, f1, i1, i2, i3 }; } + static lafc hornBlow(int i1) { return lafc{ 3, 0.0f, i1, 0, 0 }; } + static lafc keyDown(int i1) { return lafc{ 4, 0.0f, i1, 0, 0 }; } + static lafc keyUp(int i1) { return lafc{ 5, 0.0f, i1, 0, 0 }; } + void invoke(); + void schedule(); +}; +std::queue lateCallQueue; + +void lafc::invoke() { + if (L == NULL) return; + int paramCount = 1; + switch (type) { + case 0: + lua_getglobal(L, "_edoorchange"); + lua_pushboolean(L, (bool)i1); + break; + case 1: + lua_getglobal(L, "_esetsignal"); + lua_pushinteger(L, i1); + break; + case 2: + lua_getglobal(L, "_esetbeacondata"); + lua_pushnumber(L, f1); + lua_pushinteger(L, i1); + lua_pushinteger(L, i2); + lua_pushinteger(L, i3); + paramCount = 4; + break; + case 3: + lua_getglobal(L, "_ehornblow"); + lua_pushinteger(L, i1); + break; + case 4: + lua_getglobal(L, "_ekeydown"); + lua_pushinteger(L, i1); + break; + case 5: + lua_getglobal(L, "_ekeyup"); + lua_pushinteger(L, i1); + break; } -} -static int l_sound_getset(lua_State *L) { - int id = luaL_checkinteger(L, 1); - if (id < 0 || id > 255) return luaL_error(L, "_fsound_getset expects id 0~255"); - if (lua_gettop(L) == 1) { - lua_pushinteger(L, bveSound[id]); - return 1; - } else if (lua_gettop(L) == 2) { - int val = luaL_checkinteger(L, 2); - if (val > 2) { - bveSound[id] = 2; - } else if (val < -10000) { - bveSound[id] = -10000; - } else { - bveSound[id] = val; - } - return 0; - } else if (lua_gettop(L) == 3) { - float vol = luaL_checknumber(L, 3); - if (vol >= 100) { - bveSound[id] = 0; - } else if (vol <= 0) { - bveSound[id] = -10000; - } else { - bveSound[id] = (int)(vol * 100) - 10000; - } - return 0; - } else { - return luaL_error(L, "_fsound_getset expects no more than 3 arguments"); - } + if (lua_pcall(L, paramCount, 0, 0) != 0) l_printerr(); } -static int l_msgbox(lua_State *L) { - const char * sBuf = luaL_checkstring(L, 1); - DWORD dBufSize = MultiByteToWideChar(CP_UTF8, 0, sBuf, -1, NULL, 0); - std::vector dBuf(dBufSize); - if (MultiByteToWideChar(CP_UTF8, 0, sBuf, -1, dBuf.data(), dBufSize) > 0) { - MessageBoxW(NULL, dBuf.data(), L"BlocklyAts Message", 0); - } else { - MessageBoxA(NULL, sBuf, "BlocklyAts Message", 0); - } - return 0; -} - - -wchar_t dllPath[2048]; -char buffer[4096]; -DWORD read; char* luaCode; - -ATS_API void WINAPI Load() { - HMODULE hm = NULL; - - if (GetModuleHandleEx(GET_MODULE_HANDLE_EX_FLAG_FROM_ADDRESS | GET_MODULE_HANDLE_EX_FLAG_UNCHANGED_REFCOUNT, - (LPCWSTR)&Dispose, &hm) == 0) { - MessageBox(NULL, L"GetModuleHandleEx failed, ATS plugin cannot load", L"BlocklyAts Error", MB_ICONERROR); - return; - } - if (GetModuleFileName(hm, dllPath, sizeof(dllPath)) == 0) { - MessageBox(NULL, L"GetModuleFileName failed, ATS plugin cannot load", L"BlocklyAts Error", MB_ICONERROR); - return; - } - - HANDLE hFile = CreateFile(dllPath, GENERIC_READ, FILE_SHARE_READ, NULL, OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL, NULL); - if (INVALID_HANDLE_VALUE == hFile) { - MessageBox(NULL, L"CreateFile failed, ATS plugin cannot load", L"BlocklyAts Error", MB_ICONERROR); - return; - } - - ReadFile(hFile, buffer, sizeof(buffer), &read, NULL); - // The previous solution of calculating PE section size doesn't seem to work on 64-bit DLL - // I should've tried to find out why, but I cannot find how to solve this problem - DWORD exesize = *(int*)(buffer + 0x6C); // the word "DOS " in DOS stub will be replaced to describe the size - DWORD filesize = GetFileSize(hFile, NULL); - if (filesize - exesize <= 0) { - CloseHandle(hFile); - MessageBox(NULL, L"No extra code, ATS plugin cannot load", L"BlocklyAts Error", MB_ICONERROR); - return; - } - - SetFilePointer(hFile, exesize, NULL, FILE_BEGIN); - luaCode = new char[filesize - exesize + 1]; - ReadFile(hFile, luaCode, filesize - exesize, &read, NULL); - CloseHandle(hFile); - - // I knew I should've used bytecode, but then - // it'll be more difficult to compile on Mono, because you'll need to ship luac with different arch - // and somehow I could not get it to work, and I could not find out why - // So there is a quite easy xor obfuscation, to prevent the code from being stolen too easily - char confusion[] = { 0x11, 0x45, 0x14, 0x19, 0x19, 0x81, 0x14, 0x25 }; - for (DWORD i = 0; i < filesize - exesize; i++) luaCode[i] ^= confusion[i%8]; - - L = luaL_newstate(); - luaL_openlibs(L); - luaL_requiref(L, "winfile", luaopen_winfile, 1); - - l_setglobalF("_fpanel", l_panel_getset); - l_setglobalF("_fsound", l_sound_getset); - l_setglobalF("_fmsgbox", l_msgbox); - - *(wcsrchr(dllPath, '\\') + 1) = 0; - const char * sBuf = lua_tostring(L, -1); - DWORD dBufSize = WideCharToMultiByte(CP_UTF8, 0, dllPath, -1, NULL, 0, NULL, FALSE); - char* dllPathMB = new char[dBufSize]; - WideCharToMultiByte(CP_UTF8, 0, dllPath, -1, dllPathMB, dBufSize, NULL, FALSE); - l_setglobalS("_vdlldir", dllPathMB); - - int error = luaL_loadbuffer(L, luaCode, filesize - exesize, "bats") || lua_pcall(L, 0, LUA_MULTRET, 0); - - if (error) { - l_printerr(); +void lafc::schedule() { + if (elapseDataReady) { + invoke(); } else { - lua_getglobal(L, "_eload"); - if (lua_pcall(L, 0, 0, 0) != 0) l_printerr(); + lateCallQueue.push(lafc{ type, f1, i1, i2, i3 }); } } -ATS_API void WINAPI Dispose() { - if (L == NULL) return; - lua_getglobal(L, "_edispose"); - if (lua_pcall(L, 0, 0, 0) != 0) l_printerr(); - lua_close(L); -} - -ATS_API int WINAPI GetPluginVersion() { - return ATS_VERSION; -} - -ATS_API void WINAPI SetVehicleSpec(ATS_VEHICLESPEC vehicleSpec) { - vSpec = vehicleSpec; - if (L == NULL) return; - l_setglobalI("_bVsAtsNotch", vehicleSpec.AtsNotch); - l_setglobalI("_bVsB67Notch", vehicleSpec.B67Notch); - l_setglobalI("_bVsBrakeNotches", vehicleSpec.BrakeNotches); - l_setglobalI("_bVsCars", vehicleSpec.Cars); - l_setglobalI("_bVsPowerNotches", vehicleSpec.PowerNotches); -} - -ATS_API void WINAPI Initialize(int initIndex) { - if (L == NULL) return; - lua_getglobal(L, "_einitialize"); - lua_pushinteger(L, initIndex); - if (lua_pcall(L, 1, 0, 0) != 0) l_printerr(); -} - -bool firstElapse = true; -bool schdSetSignal = false, schdSetBeacon = false, schdDoorChange = false; -int sdatSignal; bool sdatDoor; ATS_BEACONDATA sdatBeacon; - ATS_API ATS_HANDLES WINAPI Elapse(ATS_VEHICLESTATE vehicleState, int *panel, int *sound) { ATS_HANDLES result = { phBrake, phPower, phReverser, 2 }; if (L == NULL) return result; @@ -201,35 +77,16 @@ ATS_API ATS_HANDLES WINAPI Elapse(ATS_VEHICLESTATE vehicleState, int *panel, int l_setglobalN("_bEdBpPressure", vehicleState.BpPressure); l_setglobalN("_bEdSapPressure", vehicleState.SapPressure); l_setglobalN("_bEdCurrent", vehicleState.Current); - if (firstElapse) { + if (!elapseDataReady) { // Let the internal LastSound inside BVE to be -10000 // So that new sounds can start from the first plugin Elapse for (int i = 0; i < 256; i++) bveSound[i] = -10000; - firstElapse = false; + elapseDataReady = true; return result; } - if (schdDoorChange) { - schdDoorChange = false; - lua_getglobal(L, "_edoorchange"); - lua_pushboolean(L, sdatDoor); - if (lua_pcall(L, 1, 0, 0) != 0) l_printerr(); - if (L == NULL) return result; - } - if (schdSetSignal) { - schdSetSignal = false; - lua_getglobal(L, "_esetsignal"); - lua_pushinteger(L, sdatSignal); - if (lua_pcall(L, 1, 0, 0) != 0) l_printerr(); - if (L == NULL) return result; - } - if (schdSetBeacon) { - schdSetBeacon = false; - lua_getglobal(L, "_esetbeacondata"); - lua_pushnumber(L, sdatBeacon.Distance); - lua_pushinteger(L, sdatBeacon.Optional); - lua_pushinteger(L, sdatBeacon.Signal); - lua_pushinteger(L, sdatBeacon.Type); - if (lua_pcall(L, 4, 0, 0) != 0) l_printerr(); + while (!lateCallQueue.empty()) { + lateCallQueue.front().invoke(); + lateCallQueue.pop(); if (L == NULL) return result; } lua_getglobal(L, "_eelapse"); @@ -239,9 +96,10 @@ ATS_API ATS_HANDLES WINAPI Elapse(ATS_VEHICLESTATE vehicleState, int *panel, int lua_pushinteger(L, 2); if (lua_pcall(L, 4, 4, 0) != 0) { l_printerr(); + if (L == NULL) return result; // Apply emergency brake // Lt's hope it'll get the plugin out of the error state by chance - if (L != NULL) return { vSpec.BrakeNotches + 1, 0, 1, 2 }; else return result; + return { vSpec.BrakeNotches + 1, 0, 1, 2 }; } else { result.Power = lua_tointeger(L, -4); result.Brake = lua_tointeger(L, -3); @@ -251,66 +109,30 @@ ATS_API ATS_HANDLES WINAPI Elapse(ATS_VEHICLESTATE vehicleState, int *panel, int return result; } } - -ATS_API void WINAPI SetPower(int notch) { - phPower = notch; - if (L == NULL) return; - l_setglobalI("_bHPower", notch); -} - -ATS_API void WINAPI SetBrake(int notch) { - phBrake = notch; - if (L == NULL) return; - l_setglobalI("_bHBrake", notch); -} - -ATS_API void WINAPI SetReverser(int pos) { - phReverser = pos; - if (L == NULL) return; - l_setglobalI("_bHReverser", pos); -} - ATS_API void WINAPI KeyDown(int atsKeyCode) { - if (L == NULL) return; - lua_getglobal(L, "_ekeydown"); - lua_pushinteger(L, atsKeyCode); - if (lua_pcall(L, 1, 0, 0) != 0) l_printerr(); + lafc::keyDown(atsKeyCode).schedule(); } ATS_API void WINAPI KeyUp(int atsKeyCode) { - if (L == NULL) return; - lua_getglobal(L, "_ekeyup"); - lua_pushinteger(L, atsKeyCode); - if (lua_pcall(L, 1, 0, 0) != 0) l_printerr(); + lafc::keyUp(atsKeyCode).schedule(); } ATS_API void WINAPI HornBlow(int atsHornBlowIndex) { - if (L == NULL) return; - lua_getglobal(L, "_ehornblow"); - lua_pushinteger(L, atsHornBlowIndex); - if (lua_pcall(L, 1, 0, 0) != 0) l_printerr(); + lafc::hornBlow(atsHornBlowIndex).schedule(); } ATS_API void WINAPI DoorOpen() { - if (L == NULL) return; - schdDoorChange = true; - sdatDoor = true; + lafc::doorChange(true).schedule(); } ATS_API void WINAPI DoorClose() { - if (L == NULL) return; - schdDoorChange = true; - sdatDoor = false; + lafc::doorChange(false).schedule(); } ATS_API void WINAPI SetSignal(int signal) { - if (L == NULL) return; - schdSetSignal = true; - sdatSignal = signal; + lafc::setSignal(signal).schedule(); } -ATS_API void WINAPI SetBeaconData(ATS_BEACONDATA beaconData) { - if (L == NULL) return; - schdSetBeacon = true; - sdatBeacon = beaconData; +ATS_API void WINAPI SetBeaconData(ATS_BEACONDATA data) { + lafc::setBeaconData(data.Distance, data.Optional, data.Signal, data.Type).schedule(); } \ No newline at end of file diff --git a/BatsWinApi/plugin.h b/BatsWinApi/plugin.h deleted file mode 100644 index e60e326..0000000 --- a/BatsWinApi/plugin.h +++ /dev/null @@ -1,23 +0,0 @@ -#pragma once - -#ifdef BATS_LUA_DYNAMIC - #ifdef _WIN64 - #pragma comment (lib, "lua54/lua54_x64.lib") - #else - #pragma comment (lib, "lua54/lua54_x86.lib") - #endif -#else - #ifdef _WIN64 - #pragma comment (lib, "lua54/lua54_x64_static.lib") - #else - #pragma comment (lib, "lua54/lua54_x86_static.lib") - #endif -#endif - -#define l_setglobalN(name, val) lua_pushnumber(L, val);lua_setglobal(L, name); - -#define l_setglobalI(name, val) lua_pushinteger(L, val);lua_setglobal(L, name); - -#define l_setglobalF(name, val) lua_pushcfunction(L, val);lua_setglobal(L, name); - -#define l_setglobalS(name, val) lua_pushstring(L, val);lua_setglobal(L, name); \ No newline at end of file diff --git a/BlocklyAtsGui/UserInterface/FormAbout.Designer.cs b/BlocklyAtsGui/UserInterface/FormAbout.Designer.cs index b3bd053..eef662e 100644 --- a/BlocklyAtsGui/UserInterface/FormAbout.Designer.cs +++ b/BlocklyAtsGui/UserInterface/FormAbout.Designer.cs @@ -143,7 +143,7 @@ private void InitializeComponent() { this.linkLabel1.Size = new System.Drawing.Size(357, 19); this.linkLabel1.TabIndex = 25; this.linkLabel1.TabStop = true; - this.linkLabel1.Text = "zbx1425@outlook.com; www.zbx1425.cn"; + this.linkLabel1.Text = "feedback@zbx1425.cn; www.zbx1425.cn"; this.linkLabel1.TextAlign = System.Drawing.ContentAlignment.MiddleLeft; this.linkLabel1.UseCompatibleTextRendering = true; this.linkLabel1.LinkClicked += new System.Windows.Forms.LinkLabelLinkClickedEventHandler(this.linkLabel1_LinkClicked); @@ -194,6 +194,7 @@ private void InitializeComponent() { this.ShowInTaskbar = false; this.StartPosition = System.Windows.Forms.FormStartPosition.CenterParent; this.Text = "About BlocklyAts"; + this.Load += new System.EventHandler(this.FormAbout_Load); this.tableLayoutPanel.ResumeLayout(false); this.tableLayoutPanel.PerformLayout(); this.flpLogos.ResumeLayout(false); diff --git a/BlocklyAtsGui/UserInterface/FormAbout.cs b/BlocklyAtsGui/UserInterface/FormAbout.cs index 162b6e7..2fdfdf3 100644 --- a/BlocklyAtsGui/UserInterface/FormAbout.cs +++ b/BlocklyAtsGui/UserInterface/FormAbout.cs @@ -83,5 +83,10 @@ public string AssemblyCompany { private void linkLabel1_LinkClicked(object sender, LinkLabelLinkClickedEventArgs e) { PlatformFunction.CallBrowser("https://www.zbx1425.cn"); } + + private void FormAbout_Load(object sender, EventArgs e) { + // heheh + pbLogoAuthor.Visible = new Random().Next(0, 3) == 0; + } } } diff --git a/BlocklyAtsGui/UserInterface/FormBugReport.Designer.cs b/BlocklyAtsGui/UserInterface/FormBugReport.Designer.cs index 29e28cf..ea039d9 100644 --- a/BlocklyAtsGui/UserInterface/FormBugReport.Designer.cs +++ b/BlocklyAtsGui/UserInterface/FormBugReport.Designer.cs @@ -85,7 +85,7 @@ private void InitializeComponent() { this.llbEmail.Size = new System.Drawing.Size(270, 24); this.llbEmail.TabIndex = 7; this.llbEmail.TabStop = true; - this.llbEmail.Text = "zbx1425@outlook.com (As of 2021)"; + this.llbEmail.Text = "feedback@zbx1425.cn (As of 2021)"; this.llbEmail.UseCompatibleTextRendering = true; this.llbEmail.LinkClicked += new System.Windows.Forms.LinkLabelLinkClickedEventHandler(this.llbEmail_LinkClicked); // diff --git a/BlocklyAtsGui/UserInterface/FormBugReport.cs b/BlocklyAtsGui/UserInterface/FormBugReport.cs index a240678..e5a5624 100644 --- a/BlocklyAtsGui/UserInterface/FormBugReport.cs +++ b/BlocklyAtsGui/UserInterface/FormBugReport.cs @@ -27,7 +27,7 @@ private void llbTwitter_LinkClicked(object sender, LinkLabelLinkClickedEventArgs } private void llbEmail_LinkClicked(object sender, LinkLabelLinkClickedEventArgs e) { - PlatformFunction.CallBrowser("mailto:zbx1425@outlook.com?subject=BlocklyAts%20Bug%20Report"); + PlatformFunction.CallBrowser("mailto:feedback@zbx1425.cn?subject=BlocklyAts%20Bug%20Report"); } private void FormBugReport_Load(object sender, EventArgs e) { diff --git a/BlocklyAtsGui/Workspace/CompilerFunction.cs b/BlocklyAtsGui/Workspace/CompilerFunction.cs index f03d776..a2011b3 100644 --- a/BlocklyAtsGui/Workspace/CompilerFunction.cs +++ b/BlocklyAtsGui/Workspace/CompilerFunction.cs @@ -70,10 +70,12 @@ public static async Task CompileLua(string script, string outputPath, string arc var boilerplateStream = new FileStream(boilerplateFile, FileMode.Open, FileAccess.Read); var outStream = new FileStream(outputPath, FileMode.Create, FileAccess.Write); - // Write PE length to DOS stub - boilerplateStream.CopySectionTo(outStream, 0x6C); + // Write Identifier and PE length to DOS stub + byte[] identifier = Encoding.UTF8.GetBytes("BATSLUA1"); + boilerplateStream.CopySectionTo(outStream, 0x6C - identifier.Length); + outStream.Write(identifier, 0, identifier.Length); outStream.Write(BitConverter.GetBytes(boilerplateStream.Length), 0, 4); - boilerplateStream.Seek(4, SeekOrigin.Current); + boilerplateStream.Seek(4 + identifier.Length, SeekOrigin.Current); await boilerplateStream.CopyToAsync(outStream); byte[] confusion = { 0x11, 0x45, 0x14, 0x19, 0x19, 0x81, 0x14, 0x25 };