-
-
Notifications
You must be signed in to change notification settings - Fork 413
/
CLuaUtilDefs.cpp
713 lines (605 loc) · 19.8 KB
/
CLuaUtilDefs.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
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
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
/*****************************************************************************
*
* PROJECT: Multi Theft Auto
* LICENSE: See LICENSE in the top level directory
* FILE: Shared/mods/logic/luadefs/CLuaUtilDefs.cpp
*
* Multi Theft Auto is available from http://www.multitheftauto.com/
*
*****************************************************************************/
#include "StdInc.h"
void CLuaUtilDefs::LoadFunctions()
{
constexpr static const std::pair<const char*, lua_CFunction> functions[]{
// Util functions to make scripting easier for the end user
// Some of these are based on standard mIRC script funcs as a lot of people will be used to them
{"deref", Dereference},
{"ref", Reference},
{"getTickCount", GetTickCount_},
{"getRealTime", GetCTime},
{"split", Split},
{"isOOPEnabled", IsOOPEnabled},
{"getUserdataType", GetUserdataType},
{"print", luaB_print},
{"getColorFromString", GetColorFromString},
// Utility vector math functions
{"getDistanceBetweenPoints2D", GetDistanceBetweenPoints2D},
{"getDistanceBetweenPoints3D", GetDistanceBetweenPoints3D},
{"getEasingValue", GetEasingValue},
{"interpolateBetween", InterpolateBetween},
// JSON funcs
{"toJSON", toJSON},
{"fromJSON", fromJSON},
// PCRE functions
{"pregFind", PregFind},
{"pregReplace", PregReplace},
{"pregMatch", PregMatch},
// Debug functions
{"debugSleep", DebugSleep},
// Utility functions
{"gettok", GetTok},
{"tocolor", tocolor},
};
// Add functions
for (const auto& [name, func] : functions)
CLuaCFunctions::AddFunction(name, func);
}
int CLuaUtilDefs::DisabledFunction(lua_State* luaVM)
{
m_pScriptDebugging->LogError(luaVM, "Unsafe function was called.");
lua_pushboolean(luaVM, false);
return 1;
}
int CLuaUtilDefs::Dereference(lua_State* luaVM)
{
int iPointer = 0;
CScriptArgReader argStream(luaVM);
argStream.ReadNumber(iPointer);
if (!argStream.HasErrors())
{
lua_getref(luaVM, iPointer);
return 1;
}
else
m_pScriptDebugging->LogCustom(luaVM, argStream.GetFullErrorMessage());
lua_pushboolean(luaVM, false);
return 1;
}
int CLuaUtilDefs::Reference(lua_State* luaVM)
{
CScriptArgReader argStream(luaVM);
if (!argStream.NextIsNil() && !argStream.NextIsNone())
{
int iPointer = lua_ref(luaVM, 1);
lua_pushnumber(luaVM, iPointer);
return 1;
}
lua_pushboolean(luaVM, false);
return 1;
}
int CLuaUtilDefs::GetTickCount_(lua_State* luaVM)
{
double dTime = static_cast<double>(GetTickCount64_());
lua_pushnumber(luaVM, dTime);
return 1;
}
int CLuaUtilDefs::GetCTime(lua_State* luaVM)
{
// table getRealTime( [int seconds = current], bool localTime = true )
time_t timer;
time(&timer);
bool bLocalTime = true;
CScriptArgReader argStream(luaVM);
if (argStream.NextCouldBeNumber())
{
argStream.ReadNumber(timer);
if (timer < 0)
{
argStream.SetCustomError("seconds cannot be negative");
}
}
if (argStream.NextIsBool())
argStream.ReadBool(bLocalTime);
tm* time;
if (bLocalTime)
time = localtime(&timer);
else
time = gmtime(&timer);
if (time == NULL)
argStream.SetCustomError("seconds is out of range");
if (argStream.HasErrors())
{
m_pScriptDebugging->LogCustom(luaVM, argStream.GetFullErrorMessage());
lua_pushboolean(luaVM, false);
return 1;
}
CLuaArguments ret;
ret.PushString("second");
ret.PushNumber(time->tm_sec);
ret.PushString("minute");
ret.PushNumber(time->tm_min);
ret.PushString("hour");
ret.PushNumber(time->tm_hour);
ret.PushString("monthday");
ret.PushNumber(time->tm_mday);
ret.PushString("month");
ret.PushNumber(time->tm_mon);
ret.PushString("year");
ret.PushNumber(time->tm_year);
ret.PushString("weekday");
ret.PushNumber(time->tm_wday);
ret.PushString("yearday");
ret.PushNumber(time->tm_yday);
ret.PushString("isdst");
ret.PushNumber(time->tm_isdst);
ret.PushString("timestamp");
ret.PushNumber((double)timer);
ret.PushAsTable(luaVM);
return 1;
}
int CLuaUtilDefs::Split(lua_State* luaVM)
{
SString strInput = "";
unsigned int uiDelimiter = 0;
SString strDelimiter;
CScriptArgReader argStream(luaVM);
argStream.ReadString(strInput);
if (argStream.NextIsNumber())
{
argStream.ReadNumber(uiDelimiter);
wchar_t wUNICODE[2] = {static_cast<wchar_t>(uiDelimiter), '\0'};
strDelimiter = UTF16ToMbUTF8(wUNICODE);
}
else // It's already a string
argStream.ReadString(strDelimiter);
if (!argStream.HasErrors())
{
// Copy the string
char* strText = new char[strInput.length() + 1];
strcpy(strText, strInput);
unsigned int uiCount = 0;
char* szToken = strtok(strText, strDelimiter);
// Create a new table
lua_newtable(luaVM);
// strtok until we're out of tokens
while (szToken)
{
// Add the token to the table
lua_pushnumber(luaVM, ++uiCount);
lua_pushstring(luaVM, szToken);
lua_settable(luaVM, -3);
szToken = strtok(NULL, strDelimiter);
}
// Delete the text
delete[] strText;
}
else
m_pScriptDebugging->LogCustom(luaVM, argStream.GetFullErrorMessage());
return 1;
}
int CLuaUtilDefs::IsOOPEnabled(lua_State* luaVM)
{
CLuaMain* pLuaMain = m_pLuaManager->GetVirtualMachine(luaVM);
if (pLuaMain)
lua_pushboolean(luaVM, pLuaMain->IsOOPEnabled());
else
lua_pushnil(luaVM);
return 1;
}
int CLuaUtilDefs::GetUserdataType(lua_State* luaVM)
{
CScriptArgReader argStream(luaVM);
int iArgument = lua_type(luaVM, 1);
if (argStream.NextIsUserData())
{
SString strType;
if (iArgument == LUA_TLIGHTUSERDATA)
strType = GetUserDataClassName(lua_touserdata(luaVM, 1), luaVM, false);
else if (iArgument == LUA_TUSERDATA)
strType = GetUserDataClassName(*((void**)lua_touserdata(luaVM, 1)), luaVM, false);
strType = strType.empty() ? "userdata" : strType;
lua_pushstring(luaVM, strType.c_str());
return 1;
}
else
{
argStream.SetCustomError(SString("Bad Argument: Expected userdata, got " + EnumToString((eLuaType)iArgument)));
m_pScriptDebugging->LogCustom(luaVM, argStream.GetFullErrorMessage());
}
lua_pushboolean(luaVM, false);
return 1;
}
/* Modified from Lua's own print */
int CLuaUtilDefs::luaB_print(lua_State* L)
{
int n = lua_gettop(L); /* number of arguments */
int i;
SString output = "";
lua_getglobal(L, "tostring");
for (i = 1; i <= n; i++)
{
const char* s;
lua_pushvalue(L, -1); /* function to be called */
lua_pushvalue(L, i); /* value to print */
lua_call(L, 1, 1);
s = lua_tostring(L, -1); /* get result */
if (s == NULL)
return luaL_error(L, LUA_QL("tostring") " must return a string to " LUA_QL("print"));
if (i > 1)
output += " ";
output += s;
lua_pop(L, 1); /* pop result */
}
m_pScriptDebugging->LogInformation(L, "%s", output.c_str());
return 0;
}
int CLuaUtilDefs::GetColorFromString(lua_State* luaVM)
{
// int int int int getColorFromString ( string theColor )
SString strColor;
CScriptArgReader argStream(luaVM);
argStream.ReadString(strColor);
if (!argStream.HasErrors())
{
unsigned char ucColorRed, ucColorGreen, ucColorBlue, ucColorAlpha;
if (XMLColorToInt(strColor, ucColorRed, ucColorGreen, ucColorBlue, ucColorAlpha))
{
lua_pushnumber(luaVM, ucColorRed);
lua_pushnumber(luaVM, ucColorGreen);
lua_pushnumber(luaVM, ucColorBlue);
lua_pushnumber(luaVM, ucColorAlpha);
return 4;
}
}
else
m_pScriptDebugging->LogCustom(luaVM, argStream.GetFullErrorMessage());
lua_pushboolean(luaVM, false);
return 1;
}
int CLuaUtilDefs::GetDistanceBetweenPoints2D(lua_State* luaVM)
{
// float getDistanceBetweenPoints2D ( float x1, float y1, float x2, float y2 )
CVector2D vecA, vecB;
CScriptArgReader argStream(luaVM);
argStream.ReadVector2D(vecA);
argStream.ReadVector2D(vecB);
if (!argStream.HasErrors())
{
// Return the distance
lua_pushnumber(luaVM, DistanceBetweenPoints2D(vecA, vecB));
return 1;
}
else
m_pScriptDebugging->LogCustom(luaVM, argStream.GetFullErrorMessage());
lua_pushboolean(luaVM, false);
return 1;
}
int CLuaUtilDefs::GetDistanceBetweenPoints3D(lua_State* luaVM)
{
// float getDistanceBetweenPoints3D ( float x1, float y1, float z1, float x2, float y2, float z2 )
CVector vecA, vecB;
CScriptArgReader argStream(luaVM);
argStream.ReadVector3D(vecA);
argStream.ReadVector3D(vecB);
if (!argStream.HasErrors())
{
// Return the distance
lua_pushnumber(luaVM, DistanceBetweenPoints3D(vecA, vecB));
return 1;
}
else
m_pScriptDebugging->LogCustom(luaVM, argStream.GetFullErrorMessage());
lua_pushboolean(luaVM, false);
return 1;
}
int CLuaUtilDefs::GetEasingValue(lua_State* luaVM)
{
// float getEasingValue( float fProgress, string strEasingType [, float fEasingPeriod, float fEasingAmplitude, float fEasingOvershoot] )
float fProgress;
CEasingCurve::eType easingType;
float fEasingPeriod;
float fEasingAmplitude;
float fEasingOvershoot;
CScriptArgReader argStream(luaVM);
argStream.ReadNumber(fProgress);
argStream.ReadEnumString(easingType);
argStream.ReadNumber(fEasingPeriod, 0.3f);
argStream.ReadNumber(fEasingAmplitude, 1.0f);
argStream.ReadNumber(fEasingOvershoot, 1.70158f);
if (argStream.HasErrors())
{
m_pScriptDebugging->LogCustom(luaVM, argStream.GetFullErrorMessage());
lua_pushboolean(luaVM, false);
return 1;
}
CEasingCurve easingCurve(easingType);
easingCurve.SetParams(fEasingPeriod, fEasingAmplitude, fEasingOvershoot);
lua_pushnumber(luaVM, easingCurve.ValueForProgress(fProgress));
return 1;
}
int CLuaUtilDefs::InterpolateBetween(lua_State* luaVM)
{
// float float float interpolateBetween ( float x1, float y1, float z1,
// float x2, float y2, float z2,
// float fProgress, string strEasingType,
// [ float fEasingPeriod, float fEasingAmplitude, float fEasingOvershoot ] )
CVector vecPointA;
CVector vecPointB;
float fProgress;
CEasingCurve::eType easingType;
float fEasingPeriod;
float fEasingAmplitude;
float fEasingOvershoot;
CScriptArgReader argStream(luaVM);
argStream.ReadVector3D(vecPointA);
argStream.ReadVector3D(vecPointB);
argStream.ReadNumber(fProgress);
argStream.ReadEnumString(easingType);
argStream.ReadNumber(fEasingPeriod, 0.3f);
argStream.ReadNumber(fEasingAmplitude, 1.0f);
argStream.ReadNumber(fEasingOvershoot, 1.70158f);
if (argStream.HasErrors())
{
m_pScriptDebugging->LogCustom(luaVM, argStream.GetFullErrorMessage());
lua_pushboolean(luaVM, false);
return 1;
}
CVector vecResult = TInterpolation<CVector>::Interpolate(vecPointA, vecPointB, fProgress, easingType, fEasingPeriod, fEasingAmplitude, fEasingOvershoot);
lua_pushnumber(luaVM, vecResult.fX);
lua_pushnumber(luaVM, vecResult.fY);
lua_pushnumber(luaVM, vecResult.fZ);
return 3;
}
int CLuaUtilDefs::toJSON(lua_State* luaVM)
{
// Got a string argument?
CScriptArgReader argStream(luaVM);
if (!argStream.NextIsNil())
{
int jsonFlags = 0;
// Read the argument
CLuaArguments JSON;
JSON.ReadArgument(luaVM, 1);
argStream.Skip(1);
bool bCompact;
argStream.ReadBool(bCompact, false);
jsonFlags |= bCompact ? JSON_C_TO_STRING_PLAIN : JSON_C_TO_STRING_SPACED;
eJSONPrettyType jsonPrettyType;
argStream.ReadEnumString(jsonPrettyType, JSONPRETTY_NONE);
if (jsonPrettyType != JSONPRETTY_NONE)
jsonFlags |= jsonPrettyType;
if (!argStream.HasErrors())
{
// Convert it to a JSON string
std::string strJSON;
if (JSON.WriteToJSONString(strJSON, false, jsonFlags))
{
// Return the JSON string
lua_pushstring(luaVM, strJSON.c_str());
return 1;
}
}
else
m_pScriptDebugging->LogCustom(luaVM, argStream.GetFullErrorMessage());
}
else
m_pScriptDebugging->LogBadType(luaVM);
// Failed
lua_pushnil(luaVM);
return 1;
}
int CLuaUtilDefs::fromJSON(lua_State* luaVM)
{
// Got a string argument?
SString strJson = "";
CScriptArgReader argStream(luaVM);
argStream.ReadString(strJson);
if (!argStream.HasErrors())
{
// Read it into lua arguments
CLuaArguments Converted;
if (Converted.ReadFromJSONString(strJson))
{
// Return it as data
Converted.PushArguments(luaVM);
return Converted.Count();
}
}
else
m_pScriptDebugging->LogCustom(luaVM, argStream.GetFullErrorMessage());
// Failed
lua_pushnil(luaVM);
return 1;
}
int CLuaUtilDefs::PregFind(lua_State* luaVM)
{
// bool pregFind ( string base, string pattern, uint/string flags = 0 )
SString strBase, strPattern;
pcrecpp::RE_Options pOptions;
CScriptArgReader argStream(luaVM);
argStream.ReadString(strBase);
argStream.ReadString(strPattern);
ReadPregFlags(argStream, pOptions);
if (!argStream.HasErrors())
{
pcrecpp::RE pPattern(strPattern, pOptions);
if (pPattern.PartialMatch(strBase))
{
lua_pushboolean(luaVM, true);
return 1;
}
}
else
m_pScriptDebugging->LogCustom(luaVM, argStream.GetFullErrorMessage());
lua_pushboolean(luaVM, false);
return 1;
}
int CLuaUtilDefs::PregReplace(lua_State* luaVM)
{
// string pregReplace ( string base, string pattern, string replace, uint/string flags = 0 )
SString strBase, strPattern, strReplace;
pcrecpp::RE_Options pOptions;
CScriptArgReader argStream(luaVM);
argStream.ReadString(strBase);
argStream.ReadString(strPattern);
argStream.ReadString(strReplace);
ReadPregFlags(argStream, pOptions);
if (!argStream.HasErrors())
{
pcrecpp::RE pPattern(strPattern, pOptions);
string strNew = strBase;
if (pPattern.GlobalReplace(strReplace, &strNew))
{
lua_pushstring(luaVM, strNew.c_str());
return 1;
}
}
else
m_pScriptDebugging->LogCustom(luaVM, argStream.GetFullErrorMessage());
lua_pushboolean(luaVM, false);
return 1;
}
int CLuaUtilDefs::PregMatch(lua_State* luaVM)
{
// table pregMatch ( string base, string pattern, uint/string flags = 0, int maxResults = 100000 )
SString strBase, strPattern;
pcrecpp::RE_Options pOptions;
int iMaxResults;
CScriptArgReader argStream(luaVM);
argStream.ReadString(strBase);
argStream.ReadString(strPattern);
ReadPregFlags(argStream, pOptions);
argStream.ReadNumber(iMaxResults, 100000);
if (!argStream.HasErrors())
{
lua_newtable(luaVM);
pcrecpp::RE pPattern(strPattern, pOptions);
pcrecpp::StringPiece strInput(strBase);
string strGet;
int i = 1;
while (pPattern.FindAndConsume(&strInput, &strGet) && i <= iMaxResults)
{
lua_pushnumber(luaVM, i);
lua_pushstring(luaVM, strGet.c_str());
lua_settable(luaVM, -3);
i++;
}
return 1;
}
else
m_pScriptDebugging->LogCustom(luaVM, argStream.GetFullErrorMessage());
lua_pushboolean(luaVM, false);
return 1;
}
int CLuaUtilDefs::DebugSleep(lua_State* luaVM)
{
std::size_t milliseconds;
CScriptArgReader argStream(luaVM);
argStream.ReadNumber(milliseconds);
if (!argStream.HasErrors())
{
#ifdef MTA_CLIENT
if (!g_pClientGame->GetDevelopmentMode())
#else
if (!g_pGame->GetDevelopmentMode())
#endif
{
m_pScriptDebugging->LogError(luaVM, "This function can only be used in development mode");
lua_pushboolean(luaVM, false);
return 1;
}
// Process HTTP
#ifdef MTA_CLIENT
g_pClientGame->GetRemoteCalls()->ProcessQueuedFiles();
#else
g_pGame->GetRemoteCalls()->ProcessQueuedFiles();
#endif
// Sleep a bit
std::this_thread::sleep_for(std::chrono::milliseconds(milliseconds));
lua_pushboolean(luaVM, true);
return 1;
}
else
m_pScriptDebugging->LogCustom(luaVM, argStream.GetFullErrorMessage());
lua_pushboolean(luaVM, false);
return 1;
}
int CLuaUtilDefs::GetTok(lua_State* luaVM)
{
SString strInput = "";
unsigned int uiToken = 0;
unsigned int uiDelimiter = 0;
SString strDelimiter;
CScriptArgReader argStream(luaVM);
argStream.ReadString(strInput);
argStream.ReadNumber(uiToken);
if (argStream.NextIsNumber())
{
argStream.ReadNumber(uiDelimiter);
wchar_t wUNICODE[2] = { static_cast<wchar_t>(uiDelimiter), '\0' };
strDelimiter = UTF16ToMbUTF8(wUNICODE);
}
else // It's already a string
argStream.ReadString(strDelimiter);
if (!argStream.HasErrors())
{
unsigned int uiCount = 0;
if (uiToken > 0 && uiToken < 1024)
{
unsigned int uiCount = 1;
char* szText = new char[strInput.length() + 1];
strcpy(szText, strInput);
char* szToken = strtok(szText, strDelimiter);
// We're looking for the first part?
if (uiToken != 1)
{
// strtok count number of times
do
{
uiCount++;
szToken = strtok(NULL, strDelimiter);
} while (uiCount != uiToken);
}
// Found it?
if (szToken)
{
// Return it
lua_pushstring(luaVM, szToken);
delete[] szText;
return 1;
}
// Delete the text
delete[] szText;
}
else
m_pScriptDebugging->LogWarning(luaVM, "Token parameter sent to split must be greater than 0 and smaller than 1024");
}
else
m_pScriptDebugging->LogCustom(luaVM, argStream.GetFullErrorMessage());
lua_pushboolean(luaVM, false);
return 1;
}
int CLuaUtilDefs::tocolor(lua_State* luaVM)
{
// int tocolor ( int red, int green, int blue [, int alpha = 255 ] )
int iRed;
int iGreen;
int iBlue;
int iAlpha;
CScriptArgReader argStream(luaVM);
argStream.ReadNumber(iRed);
argStream.ReadNumber(iGreen);
argStream.ReadNumber(iBlue);
argStream.ReadNumber(iAlpha, 255);
if (!argStream.HasErrors())
{
// Make it into an unsigned long
unsigned long ulColor = COLOR_RGBA(iRed, iGreen, iBlue, iAlpha);
lua_pushinteger(luaVM, static_cast<lua_Integer>(ulColor));
return 1;
}
// Make it black so funcs dont break
unsigned long ulColor = COLOR_RGBA(0, 0, 0, 255);
lua_pushnumber(luaVM, static_cast<lua_Number>(ulColor));
return 1;
}