-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathLuaPlugin.cpp
144 lines (124 loc) · 3.1 KB
/
LuaPlugin.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
#include "LuaPlugin.h"
#include <ctime>
#include <vector>
#include "LUA\lua.hpp"
#include "natives_module.h"
#include <stdio.h>
#include <io.h>
#include <fcntl.h>
const char* FunctionToRun = "";
int get_key_pressed(lua_State *l)
{
int key = lua_tointeger(l, 1);
lua_pushboolean(l, (GetAsyncKeyState(key) & 0x8000) != 0);
return 1;
}
int wait(lua_State *l)
{
int key = lua_tointeger(l, 1);
scriptWait(key);
return 1;
}
void runNewThread(int b) {
if (FunctionToRun != "") {
}
}
int Lua_run_in_seperate_thread(lua_State *L) {
if (lua_isstring(L, 1)) {
FunctionToRun = lua_tostring(L, 1);
lua_State* newState = lua_newthread(L);
//HANDLE hThread = ::CreateThread(NULL, 0, (::LPTHREAD_START_ROUTINE)&runNewThread, (::HMODULE)Instance, 0, NULL);
}
return 0;
}
void run_function(lua_State *L, const char *func)
{
lua_getglobal(L, func);
int error = lua_pcall(L, 0, 0, 0);
if (error) {
fprintf(stderr, "%s: %s\n", func, lua_tostring(L, -1));
lua_pop(L, 1);
scriptWait(500);
}
if (error == LUA_ERRMEM) {
fprintf(stderr, "C daemon: Memory error");
scriptWait(500);
}
}
void run_file(lua_State *L, char *file)
{
int error = luaL_loadfile(L, file) || lua_pcall(L, 0, 0, 0);
if (error) {
fprintf(stderr, "%s: %s\n", file, lua_tostring(L, -1));
lua_pop(L, 1);
}
if (error == LUA_ERRMEM) {
fprintf(stderr, "C daemon: Memory error");
}
}
lua_State * generateLuaState() {
lua_State *lua_state;
lua_state = luaL_newstate();
// load Lua libraries
luaL_openlibs(lua_state);
tolua_natives_open(lua_state);
if (luaL_dofile(lua_state, "scripts/main.lua")) {
fprintf(stderr, "Main.lua: %s\n", lua_tostring(lua_state, -1));
lua_pop(lua_state, 1);
}
lua_pushcfunction(lua_state, &get_key_pressed);
lua_setglobal(lua_state, "get_key_pressed");
lua_pushcfunction(lua_state, &wait);
lua_setglobal(lua_state, "wait");
run_function(lua_state, "init");
return lua_state;
}
void initializeLua(lua_State *lua_state) {
// create new Lua state
long lastPressedReload = 0;
printf("Loaded main.lua\n");
try {
while (true) {
#ifdef SDK
if ((GetAsyncKeyState(VK_LCONTROL) & 0x8000) != 0 && (GetAsyncKeyState(0x52) & 0x8000) != 0 && (clock() - lastPressedReload) > 500) {
lastPressedReload = clock();
run_function(lua_state, "unload");
lua_state = NULL;
lua_state = generateLuaState();
}
else {
#endif
run_function(lua_state, "tick");
scriptWait(0);
#ifdef SDK
}
#endif
}
}
catch (...) {
printf("%s\n", lua_tostring(lua_state, -1));
scriptWait(500);
}
// close the Lua state
lua_close(lua_state);
}
void LuaPlugin::initialize()
{
#ifdef SDK
AllocConsole();
HANDLE handle_out = GetStdHandle(STD_OUTPUT_HANDLE);
int hCrt = _open_osfhandle((long)handle_out, _O_TEXT);
FILE* hf_out = _fdopen(hCrt, "w");
setvbuf(hf_out, NULL, _IONBF, 1);
*stdout = *hf_out;
HANDLE handle_in = GetStdHandle(STD_INPUT_HANDLE);
hCrt = _open_osfhandle((long)handle_in, _O_TEXT);
FILE* hf_in = _fdopen(hCrt, "r");
setvbuf(hf_in, NULL, _IONBF, 128);
*stdin = *hf_in;
freopen("CONOUT$", "w", stdout);
freopen("CONOUT$", "w", stderr);
#endif
lua_State *l = generateLuaState();
initializeLua(l);
}