-
Notifications
You must be signed in to change notification settings - Fork 0
/
winNativeHelper.c
247 lines (215 loc) · 5.14 KB
/
winNativeHelper.c
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
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
//Native-code helper for Lua 5.3
//by Microeinstein
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <windows.h>
#include "lua.h"
#include "lauxlib.h"
#include "lualib.h"
#if defined(_MSC_VER)
//Microsoft
#define EXPORT __declspec(dllexport)
#define IMPORT __declspec(dllimport)
#elif defined(__GNUC__)
//GCC
#define EXPORT __attribute__((visibility("default")))
#define IMPORT
#else
#define EXPORT
#define IMPORT
#pragma warning Unknown dynamic link import/export semantics.
#endif
#define length(x) (sizeof(x) / sizeof((x)[0]))
typedef unsigned long long ulong;
typedef struct {
double s0, s1, s2, c;
int first;
} aleaData;
aleaData state;
int zfrs(int n, int s) {
if (s < 1)
return n;
n = n & 0x7FFFFFFF;
n = n >> s;
return n;
}
ulong getTick() {
SYSTEMTIME st;
FILETIME ft;
GetSystemTime(&st);
SystemTimeToFileTime(&st, &ft);
ulong res = (ulong)ft.dwHighDateTime;
res = res << (sizeof(DWORD) * 8);
res = res | ft.dwLowDateTime;
return res;
}
int clearAll() {
HANDLE hOut = GetStdHandle(STD_OUTPUT_HANDLE);
CONSOLE_SCREEN_BUFFER_INFO csbi;
DWORD count;
DWORD cellCount;
COORD homeCoords = {0, 0};
if (hOut == INVALID_HANDLE_VALUE)
return 1;
if (!GetConsoleScreenBufferInfo(hOut, &csbi))
return 2;
cellCount = csbi.dwSize.X * csbi.dwSize.Y;
if (!FillConsoleOutputCharacter(hOut, (TCHAR)' ', cellCount, homeCoords, &count))
return 3;
if (!FillConsoleOutputAttribute(hOut, csbi.wAttributes, cellCount, homeCoords, &count))
return 4;
SetConsoleCursorPosition(hOut, homeCoords);
return 0;
}
int setCursor(int x, int y) {
HANDLE hOut = GetStdHandle(STD_OUTPUT_HANDLE);
if (hOut == INVALID_HANDLE_VALUE)
return 1;
COORD pos = {x, y};
SetConsoleCursorPosition(hOut, pos);
return 0;
}
int moveCursor(int x, int y) {
HANDLE hOut = GetStdHandle(STD_OUTPUT_HANDLE);
CONSOLE_SCREEN_BUFFER_INFO csbi;
if (hOut == INVALID_HANDLE_VALUE)
return 1;
if (!GetConsoleScreenBufferInfo(hOut, &csbi))
return 2;
COORD pos = csbi.dwCursorPosition;
pos.X += x;
pos.Y += y;
SetConsoleCursorPosition(hOut, pos);
return 0;
}
//Porting of the script obtained here: https://ss64.com/nt/syntax-random.html
double mash(const char *data, double **n) {
if (*n == NULL) {
*n = malloc(sizeof(double));
**n = 0xefc8249d;
}
double nn = **n;
for (int i = 0; data[i] != '\0'; i++) {
nn += (int)data[i];
double h = 0.02519603282416938 * nn;
nn = (int)h;
h -= nn;
h *= nn;
nn = (int)h;
h -= nn;
nn += h * 0x100000000L;
}
**n = nn;
return ((int)nn) * 2.3283064365386963e-10;
}
aleaData alea(const char *args[]) {
int len = length(args);
double s0, s1, s2;
double *n = NULL;
s0 = mash(" ", &n);
s1 = mash(" ", &n);
s2 = mash(" ", &n);
//printf("%lf\n%lf\n%lf\n\n", s0, s1, s2);
for (int i = 0; i < len; i++) {
s0 -= mash(args[i], &n);
if (s0 < 0)
s0 += 1;
s1 -= mash(args[i], &n);
if (s1 < 0)
s1 += 1;
s2 -= mash(args[i], &n);
if (s2 < 0)
s2 += 1;
}
free(n);
//printf("%lf\n%lf\n%lf\n\n", s0, s1, s2);
aleaData data = {s0, s1, s2, 1, 1};
return data;
}
double random(aleaData *data) {
double
s0 = data->s0,
s1 = data->s1,
s2 = data->s2,
c = data->c,
t;
int chk;
do {
t = 2091639 * s0 + c * 2.3283064365386963e-10;
s0 = s1;
s1 = s2;
c = (int)t;
s2 = t - c;
chk = s2 < 1e-7;
} while (data->first && chk);
data->s0 = s0;
data->s1 = s1;
data->s2 = s2;
data->c = c;
data->first = 0;
return s2;
}
int l_moveCursor(lua_State *L) {
int abs = (int)lua_toboolean(L, 1);
int x = (int)luaL_checknumber(L, 2);
int y = (int)luaL_checknumber(L, 3);
int ret;
if (abs) {
ret = setCursor(x, y);
} else {
ret = moveCursor(x, y);
}
if (ret != 0) {
lua_pushliteral(L, "winTermHelper: Internal error (move cursor)");
lua_error(L);
}
return 0;
}
int l_clearAll(lua_State *L) {
int ret = clearAll();
if (ret != 0) {
lua_pushliteral(L, "winTermHelper: Internal error (clear)");
lua_error(L);
}
return 0;
}
int l_timeTick(lua_State *L) {
ulong num = getTick();
lua_pushinteger(L, num);
return 1;
}
int l_random(lua_State *L) {
double num = random(&state);
lua_pushnumber(L, num);
return 1;
}
int EXPORT libinit(lua_State* L) {
ulong num = getTick();
char now[21];
sprintf(now, "%llu", num);
char to[15];
memcpy(to, now, 14);
to[15] = '\0';
const char *args[1] = { to };
state = alea(args);
//Ind(ic|ex)es starts at 1... damn lua
//1 (first element pushed), 2, 3...
lua_newtable(L);
lua_pushstring(L, "moveCursor");
lua_pushcfunction(L, l_moveCursor);
lua_settable(L, 1);
lua_pushstring(L, "clearAll");
lua_pushcfunction(L, l_clearAll);
lua_settable(L, 1);
lua_pushstring(L, "timeTick");
lua_pushcfunction(L, l_timeTick);
lua_settable(L, 1);
lua_pushstring(L, "random");
lua_pushcfunction(L, l_random);
lua_settable(L, 1);
lua_setglobal(L, "nativeHelper");
//lua_register(L, "wth_moveCursor", l_moveCursor);
//lua_register(L, "wth_clearAll", l_clearAll);
return 0;
}