-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathMain.cpp
78 lines (72 loc) · 2.25 KB
/
Main.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
#include <stdio.h>
#include "stdafx.h"
#include "lua.hpp"
#include "DirectCompositeSample.h"
//#include "LuaGlobals.cpp"
HINSTANCE hInstanceSvd;
int nCmdShowSvd;
HWND conWND;
int (*LuaCFuncs) (lua_State*) = {};
namespace LuaGlobals {
static int l_wcout(lua_State* L) {
//int strlen;
const char* input = luaL_checkstring(L, 1);
//int strlen = luaL_checkint(L, 2);
size_t strlen = lua_objlen(L, 1);
WriteConsoleA(GetStdHandle(STD_OUTPUT_HANDLE), input, strlen, NULL, NULL);
return 0;
}
static int l_newwnd(lua_State* L) {
DirectCompositeSample sample(512, 512, L"Lua Overlay");
int wnderr = Win32Application::Run(&sample, hInstanceSvd, nCmdShowSvd);
return 0;
}
static int l_quit(lua_State* L) {
FreeConsole();
return 0;
}
}
bool CheckLua(lua_State* L, int r) {
if (r != 0) {
std::string errmsg = "LUA ERROR: ";
errmsg += lua_tostring(L, -1);
errmsg += "\n";
WriteConsoleA(GetStdHandle(STD_ERROR_HANDLE), (errmsg).c_str(), errmsg.length(), NULL, NULL);
return false;
}
return true;
}
_Use_decl_annotations_
int WINAPI WinMain(HINSTANCE hInstance, HINSTANCE, LPSTR, int nCmdShow)
{
hInstanceSvd = hInstance;
nCmdShowSvd = nCmdShow;
// Console
AllocConsole();
conWND = FindWindow(L"ConsoleWindowClass", NULL);
ShowWindow(conWND, nCmdShow); //0 - hide, 1 - show
// Create VM state
lua_State* L = luaL_newstate();
// run config
//WCHAR path[MAX_PATH];
//GetModuleFileName(NULL, path, MAX_PATH);
if (!L)
return 1;
luaL_openlibs(L); // Open standard libraries
lua_pushcfunction(L, LuaGlobals::l_wcout);
lua_setglobal(L, "ConsoleWrite");
lua_pushcfunction(L, LuaGlobals::l_quit);
lua_setglobal(L, "ProgramClose");
int Rcfg = luaL_dofile(L, "cfg.lua");
if (!CheckLua(L, Rcfg)) {
std::string errmsg = "\nPlease check if cfg.lua is near overlay's executable and restart application!\n";
WriteConsoleA(GetStdHandle(STD_ERROR_HANDLE), (errmsg).c_str(), errmsg.length(), NULL, NULL);
system("pause");
return 1;
}
else {
while (GetConsoleWindow());
lua_close(L); //close lua state
return 0;
}
}