Skip to content

Commit

Permalink
Fix only one will be handled if there are multiple events on a single…
Browse files Browse the repository at this point in the history
… frame
  • Loading branch information
zbx1425 committed Apr 17, 2021
1 parent d07c4d0 commit 6e3dba1
Show file tree
Hide file tree
Showing 15 changed files with 351 additions and 281 deletions.
6 changes: 5 additions & 1 deletion BatsWinApi/BatsWinApi.vcxproj
Original file line number Diff line number Diff line change
Expand Up @@ -179,11 +179,15 @@
<ItemGroup>
<ClInclude Include="atsplugin.h" />
<ClInclude Include="framework.h" />
<ClInclude Include="globals.h" />
<ClInclude Include="luainterop.h" />
<ClInclude Include="pch.h" />
<ClInclude Include="plugin.h" />
<ClInclude Include="winfile.h" />
</ItemGroup>
<ItemGroup>
<ClCompile Include="globals.cpp" />
<ClCompile Include="loader.cpp" />
<ClCompile Include="luainterop.cpp" />
<ClCompile Include="plugin.cpp" />
<ClCompile Include="dllmain.cpp" />
<ClCompile Include="pch.cpp">
Expand Down
16 changes: 14 additions & 2 deletions BatsWinApi/BatsWinApi.vcxproj.filters
Original file line number Diff line number Diff line change
Expand Up @@ -30,10 +30,13 @@
<ClInclude Include="atsplugin.h">
<Filter>头文件</Filter>
</ClInclude>
<ClInclude Include="plugin.h">
<ClInclude Include="winfile.h">
<Filter>头文件</Filter>
</ClInclude>
<ClInclude Include="winfile.h">
<ClInclude Include="globals.h">
<Filter>头文件</Filter>
</ClInclude>
<ClInclude Include="luainterop.h">
<Filter>头文件</Filter>
</ClInclude>
</ItemGroup>
Expand All @@ -50,5 +53,14 @@
<ClCompile Include="winfile.cpp">
<Filter>源文件</Filter>
</ClCompile>
<ClCompile Include="globals.cpp">
<Filter>源文件</Filter>
</ClCompile>
<ClCompile Include="luainterop.cpp">
<Filter>源文件</Filter>
</ClCompile>
<ClCompile Include="loader.cpp">
<Filter>源文件</Filter>
</ClCompile>
</ItemGroup>
</Project>
7 changes: 7 additions & 0 deletions BatsWinApi/globals.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
#include "pch.h"

lua_State *L = NULL;

ATS_VEHICLESPEC vSpec;
int phPower, phBrake, phReverser;
int *bvePanel, *bveSound;
8 changes: 8 additions & 0 deletions BatsWinApi/globals.h
Original file line number Diff line number Diff line change
@@ -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;
119 changes: 119 additions & 0 deletions BatsWinApi/loader.cpp
Original file line number Diff line number Diff line change
@@ -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);
}
94 changes: 94 additions & 0 deletions BatsWinApi/luainterop.cpp
Original file line number Diff line number Diff line change
@@ -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<wchar_t> 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<wchar_t> 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);
}
4 changes: 4 additions & 0 deletions BatsWinApi/luainterop.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
#pragma once

void l_printerr();
void open_interop_functions();
15 changes: 15 additions & 0 deletions BatsWinApi/pch.h
Original file line number Diff line number Diff line change
Expand Up @@ -13,9 +13,24 @@
#include "framework.h"

#include <vector>
#include <queue>

#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
Loading

0 comments on commit 6e3dba1

Please sign in to comment.