-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathJSBoot.cpp
1401 lines (1145 loc) · 31.6 KB
/
JSBoot.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
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
#include "JSBoot.h"
#include "D2Ptrs.h"
#include "Helpers.h"
#include "JSUnit.h"
#include "MapHeader.h"
#include "JSPresetUnit.h"
#include "JSScreenHook.h"
#include "JSRoom.h"
#include "Script.h"
#include "ScriptEngine.h"
#include "Console.h"
#include "JSScript.h"
#include "JSParty.h"
#include "D2Skills.h"
#include "MPQStats.h"
#include "JSArea.h"
#include "JSExits.h"
#include "Room.h"
#include "JSFileTools.h"
#include "JSMenu.h"
#include "JSProfile.h"
#include "Events.h"
#include "JSControl.h"
#include "JSHash.h"
JSAPI_FUNC(my_gamePrint)
{
if (!JS_IsString(argv[0]))
{
return JS_FALSE;
}
wchar_t *output = nullptr;
JS_ToUnicodeString(ctx, &output, argv[0]);
D2CLIENT_PrintGameString(output, 0);
free(output);
return JS_TRUE;
}
JSAPI_FUNC(my_stringToEUC)
{
if (argc < 1 || !JS_IsString(argv[0]))
return JS_FALSE;
wchar_t *wVal = nullptr;
JS_ToUnicodeString(ctx, &wVal, argv[0]);
char *szText = UnicodeToAnsi(wVal, CP_ACP);
JSValue val = JS_NewString(ctx, szText);
free(szText);
free(wVal);
return val;
}
JSAPI_FUNC(my_print)
{
if (argc < 1 || !JS_IsString(argv[0]))
return JS_FALSE;
wchar_t *wVal = nullptr;
JS_ToUnicodeString(ctx, &wVal, argv[0]);
// wchar_t *wVal = GetPrintString(szVal);
Print(L"%ls", wVal);
free(wVal);
return JS_TRUE;
}
JSAPI_FUNC(my_say)
{
if (argc < 1 || !JS_IsString(argv[0]))
return JS_FALSE;
wchar_t *szMessage = nullptr;
JS_ToUnicodeString(ctx, &szMessage, argv[0]);
Say(L"%ls", szMessage);
free(szMessage);
return JS_TRUE;
}
JSAPI_FUNC(my_getPath)
{
GAME_READY();
if (argc < 5)
JS_THROW_SINGLE_LINE(ctx, "Not enough parameters were passed to getPath!");
uint32_t lvl = 0, x = 0, y = 0, dx = 0, dy = 0, reductionType = 0, radius = 20;
JS_ToUint32(ctx, &lvl, argv[0]);
JS_ToUint32(ctx, &x, argv[1]);
JS_ToUint32(ctx, &y, argv[2]);
JS_ToUint32(ctx, &dx, argv[3]);
JS_ToUint32(ctx, &dy, argv[4]);
if (lvl == 0)
JS_THROW_SINGLE_LINE(ctx, "Invalid level passed to getPath");
if (argc > 6)
JS_ToUint32(ctx, &reductionType, argv[5]);
if (argc > 7)
JS_ToUint32(ctx, &radius, argv[6]);
Level *level = GetLevel(lvl);
if (!level)
return JS_FALSE;
ActMap *map = ActMap::GetMap(level);
Point start(x, y), end(dx, dy);
PathReducer *reducer = nullptr;
switch (reductionType)
{
case 0:
reducer = new WalkPathReducer(map, DiagonalShortcut, radius);
break;
case 1:
reducer = new TeleportPathReducer(map, DiagonalShortcut, radius);
break;
case 2:
reducer = new NoPathReducer(map);
break;
// case 3:
// reducer = new JSPathReducer(map, cx, JS_THIS_OBJECT(cx, vp), JS_ARGV(cx, vp)[7], JS_ARGV(cx, vp)[8], JS_ARGV(cx, vp)[9]);
// break;
default:
JS_THROW_ERROR(ctx, "Invalid path reducer value!");
break;
}
PointList list;
AStarPath<> path(map, reducer);
path.GetPath(start, end, list, true);
map->CleanUp();
int count = list.size();
JSValue array = JS_NewArray(ctx);
for (int i = 0; i < count; i++)
{
JSValue point = JS_NewObject(ctx);
JS_SetPropertyStr(ctx, point, "x", JS_NewUint32(ctx, list[i].first));
JS_SetPropertyStr(ctx, point, "y", JS_NewUint32(ctx, list[i].second));
JS_DefinePropertyValueUint32(ctx, array, i, point, JS_PROP_C_W_E);
}
delete reducer;
map->CleanUp();
return array;
}
JSAPI_FUNC(my_dumpLevel)
{
uint32_t lvl = 0;
JS_ToUint32(ctx, &lvl, argv[0]);
const char *file = JS_ToCString(ctx, argv[1]);
Level *level = GetLevel(lvl);
if (!level)
return JS_FALSE;
ActMap *map = ActMap::GetMap(level);
map->DumpLevel(file);
JS_FreeCString(ctx, file);
map->CleanUp();
// delete map;
return JS_TRUE;
}
JSAPI_FUNC(my_delay)
{
uint32_t nDelay = 0;
if (JS_ToUint32(ctx, &nDelay, argv[0]) < 0)
return JS_FALSE;
Script *script = (Script *)JS_GetContextOpaque(ctx);
script->WaitForExecEvent(nDelay);
return JS_TRUE;
}
JSAPI_FUNC(my_getTickCount)
{
return JS_NewUint32(ctx, GetTickCount());
}
JSAPI_FUNC(my_version)
{
Print(L"\u00FFc4D2Boot\u00FFc1 \u00FFc3%ls for Diablo II 1.13d.", D2BOOT_VERSION);
return JS_NewUTF8String(ctx, D2BOOT_VERSION);
}
JSAPI_FUNC(my_debugLog)
{
for (int i = 0; i < argc; i++)
{
if (!JS_IsNull(argv[i]))
{
wchar_t *Text = nullptr;
JS_ToUnicodeString(ctx, &Text, argv[i]);
if (Text == nullptr)
{
JS_ReportError(ctx, "Could not get string for value");
return JS_FALSE;
}
Log(L"%ls", Text);
free(Text);
}
}
return JS_TRUE;
}
JSAPI_FUNC(my_getLocaleString)
{
if (argc < 1 || !JS_IsNumber(argv[0]))
return JS_FALSE;
uint32_t localeId;
JS_ToUint32(ctx, &localeId, argv[0]);
// no need to free
wchar_t *wString = D2LANG_GetLocaleText(localeId);
return JS_NewUTF8String(ctx, wString);
}
JSAPI_FUNC(my_getUIFlag)
{
GAME_READY();
if (argc < 1 || !JS_IsNumber(argv[0]))
return JS_FALSE;
uint32_t nUIId;
JS_ToUint32(ctx, &nUIId, argv[0]);
return JS_NewBool(ctx, D2CLIENT_GetUIState(nUIId));
}
JSAPI_FUNC(my_addEventListener)
{
if (JS_IsString(argv[0]) && JS_IsFunction(ctx, argv[1]))
{
const char *evtName = JS_ToCString(ctx, argv[0]);
if (evtName && strlen(evtName))
{
Script *self = (Script *)JS_GetContextOpaque(ctx);
JSValue thisVal = JS_UNDEFINED;
if (argc > 2 && JS_IsObject(argv[2]))
{
thisVal = argv[2];
}
self->RegisterEvent(evtName, argv[1], thisVal);
}
else
{
JS_FreeCString(ctx, evtName);
JS_THROW_ERROR(ctx, "Event name is invalid!");
}
JS_FreeCString(ctx, evtName);
}
return JS_TRUE;
}
JSAPI_FUNC(my_removeEventListener)
{
if (JS_IsString(argv[0]) && JS_IsFunction(ctx, argv[1]))
{
const char *evtName = JS_ToCString(ctx, argv[0]);
if (evtName && strlen(evtName))
{
Script *self = (Script *)JS_GetContextOpaque(ctx);
self->UnregisterEvent(evtName, argv[1]);
}
else
{
JS_FreeCString(ctx, evtName);
JS_THROW_ERROR(ctx, "Event name is invalid!");
}
JS_FreeCString(ctx, evtName);
}
return JS_TRUE;
}
JSAPI_FUNC(my_showConsole)
{
Console::Show();
return JS_TRUE;
}
JSAPI_FUNC(my_hideConsole)
{
Console::Hide();
return JS_TRUE;
}
JSAPI_FUNC(my_handler)
{
return JS_NewUint32(ctx, (uint32_t)Vars.hHandle);
}
JSAPI_FUNC(my_getPacket)
{
if (!Vars.bEnableUnsupported)
JS_THROW_SINGLE_LINE(ctx, "getPacket requires EnableUnsupported = true in d2boot.ini");
BYTE *aPacket;
uint32_t len = 0, size = 0;
if (JS_IsObject(argv[0]))
{
aPacket = JS_GetArrayBuffer(ctx, &size, argv[0]);
if (!aPacket)
JS_THROW_SINGLE_LINE(ctx, "invalid ArrayBuffer parameter");
}
else
{
if (argc % 2 != 0)
JS_THROW_SINGLE_LINE(ctx, "invalid packet format");
aPacket = new BYTE[2 * argc];
for (int i = 0; i < argc; i += 2, len += size)
{
JS_ToUint32(ctx, &size, argv[i]);
JS_ToUint32(ctx, (uint32_t *)&aPacket[len], argv[i + 1]);
}
}
D2NET_ReceivePacket(aPacket, len);
delete[] aPacket;
return JS_TRUE;
}
JSAPI_FUNC(my_sendPacket)
{
if (!Vars.bEnableUnsupported)
JS_THROW_SINGLE_LINE(ctx, "sendPacket requires EnableUnsupported = true in d2boot.ini");
BYTE *aPacket;
uint32_t len = 0;
if (JS_IsObject(argv[0]))
{
aPacket = JS_GetArrayBuffer(ctx, &len, argv[0]);
if (!aPacket)
JS_THROW_SINGLE_LINE(ctx, "invalid ArrayBuffer parameter");
}
else
{
if (argc % 2 != 0)
JS_THROW_SINGLE_LINE(ctx, "invalid packet format");
// 2*argc(4*argc/2) mean all val is int type, overflow?
aPacket = new BYTE[2 * argc];
uint32_t size = 0;
for (int i = 0; i < argc; i += 2, len += size)
{
// (uint32_t *)&aPacket[len] is safe, set uint32_t to char
if (JS_ToUint32(ctx, &size, argv[i]) < 0 || JS_ToUint32(ctx, (uint32_t *)&aPacket[len], argv[i + 1]) < 0)
JS_THROW_SINGLE_LINE(ctx, "invalid packet data");
}
}
D2NET_SendPacket(len, 1, aPacket);
delete[] aPacket;
aPacket = nullptr;
return JS_TRUE;
}
JSAPI_FUNC(my_load)
{
Script *script = (Script *)JS_GetContextOpaque(ctx);
if (!script)
{
return JS_FALSE;
}
wchar_t *file = nullptr;
JS_ToUnicodeString(ctx, &file, argv[0]);
if (wcslen(file) > (_MAX_FNAME + _MAX_PATH - wcslen(Vars.szScriptPath)))
{
free(file);
return JS_FALSE;
}
ScriptState scriptState = script->GetState();
if (scriptState == Command)
scriptState = (ClientState() == ClientStateInGame ? InGame : OutOfGame);
wchar_t buf[_MAX_PATH + _MAX_FNAME];
swprintf_s(buf, _countof(buf), L"%s/%s", Vars.szScriptPath, file);
// StringReplaceChar(buf, L'/', L'\\', _countof(buf));
// MTODO support args?
char **args = nullptr;
// if (argc > 1)
// {
// args = new char *[argc - 1];
// for (int i = 1; i < argc; i++)
// {
// args[i - 1] = (char *)JS_ToCString(ctx, argv[i]);
// }
// }
Script *newScript = ScriptEngine::CompileFile(buf, scriptState, argc - 1, (void **)args);
if (newScript)
{
ScriptEngine::EvalScript(newScript);
}
else
{
// TODO: Should this actually be there? No notification is bad, but do we want this? maybe throw an exception?
Print(L"File \"%ls\" not found.", file);
return JS_FALSE;
}
free(file);
return JS_TRUE;
}
JSAPI_FUNC(my_clearAllEvents)
{
Script *self = (Script *)JS_GetContextOpaque(ctx);
self->ClearAllEvents();
return JS_TRUE;
}
static JSClassID dialogLine_class_id;
JSAPI_FUNC(my_clickDialog)
{
TransactionDialogsLine_t *tdl = (TransactionDialogsLine_t *)JS_GetOpaque(this_val, dialogLine_class_id);
if (tdl && tdl->bMaybeSelectable)
tdl->handler();
else
JS_THROW_SINGLE_LINE(ctx, "That dialog is not currently clickable.");
return JS_TRUE;
}
JSAPI_FUNC(my_getDialogLines)
{
TransactionDialogsInfo_t *pTdi = *p_D2CLIENT_pTransactionDialogsInfo;
JSValue returnArray = JS_NULL, jsLine, jsText, jsSelectable;
AutoCriticalRoom *cRoom = new AutoCriticalRoom;
if (pTdi)
{
returnArray = JS_NewArray(ctx);
for (DWORD i = 0; i < pTdi->numLines; ++i)
{
jsText = JS_NewUTF8String(ctx, pTdi->dialogLines[i].text);
jsSelectable = JS_NewBool(ctx, pTdi->dialogLines[i].bMaybeSelectable);
jsLine = BuildObject(ctx, dialogLine_class_id, &pTdi->dialogLines[i]);
JS_SetPropertyStr(ctx, jsLine, "text", jsText);
JS_SetPropertyStr(ctx, jsLine, "selectable", jsSelectable);
JS_SetPropertyStr(ctx, jsLine, "handler", JS_NewCFunction(ctx, my_clickDialog, "handler", 1));
JS_DefinePropertyValueUint32(ctx, returnArray, i, jsLine, JS_PROP_C_W_E);
}
}
delete cRoom;
return returnArray;
}
JSAPI_FUNC(my_getWaypoint)
{
if (argc < 1 || !JS_IsNumber(argv[0]))
return JS_FALSE;
GAME_READY();
uint32_t nWaypointId;
JS_ToUint32(ctx, &nWaypointId, argv[0]);
if (nWaypointId > 40)
nWaypointId = 0;
return JS_NewBool(ctx, !!D2COMMON_CheckWaypoint((*p_D2CLIENT_WaypointTable), nWaypointId));
}
JSAPI_FUNC(my_getBaseStat)
{
if (argc > 2)
{
const char *szStatName = nullptr, *szTableName = nullptr;
uint32_t nBaseStat = 0;
uint32_t nClassId = 0;
uint32_t nStat = -1;
if (JS_IsString(argv[0]))
{
szTableName = JS_ToCString(ctx, argv[0]);
if (!szTableName)
JS_THROW_SINGLE_LINE(ctx, "Invalid table value");
}
else if (JS_IsNumber(argv[0]))
JS_ToUint32(ctx, &nBaseStat, argv[0]);
else
JS_THROW_SINGLE_LINE(ctx, "Invalid table value");
JS_ToUint32(ctx, &nClassId, argv[1]);
if (JS_IsString(argv[2]))
{
szStatName = JS_ToCString(ctx, argv[2]);
if (!szStatName)
{
JS_FreeCString(ctx, szTableName);
JS_THROW_ERROR(ctx, "Invalid column value");
}
}
else if (JS_IsNumber(argv[2]))
JS_ToUint32(ctx, &nStat, argv[2]);
else
{
JS_FreeCString(ctx, szTableName);
JS_THROW_ERROR(ctx, "Invalid column value");
}
JSValue rval;
JS_FillBaseStat(ctx, &rval, nBaseStat, nClassId, nStat, szTableName, szStatName);
// JS_SET_RVAL(cx, vp, rval);
JS_FreeCString(ctx, szTableName);
JS_FreeCString(ctx, szStatName);
return rval;
}
return JS_FALSE;
}
JSAPI_FUNC(my_getIsTalkingNPC)
{
GAME_READY();
return JS_NewBool(ctx, IsScrollingText());
}
JSAPI_FUNC(my_getCollision)
{
GAME_READY();
if (argc < 3)
return JS_FALSE;
int32_t nLevelId, nX, nY;
JS_ToInt32(ctx, &nLevelId, argv[0]);
JS_ToInt32(ctx, &nX, argv[1]);
JS_ToInt32(ctx, &nY, argv[2]);
Point point(nX, nY);
Level *level = GetLevel(nLevelId);
if (!level)
JS_THROW_SINGLE_LINE(ctx, "Level Not loaded");
ActMap *map = ActMap::GetMap(level);
// if(!map->IsValidPoint(point)) //return avoid instead and make it not lvl depenant
// { map->CleanUp(); THROW_ERROR(cx, "Invalid point!");}
JSValue rval = JS_NewInt32(ctx, map->GetMapData(point, true));
map->CleanUp();
return rval;
}
JSAPI_FUNC(my_getCursorType)
{
uint32_t nType = 0;
if (argc > 0)
nType = JS_ToUint32(ctx, &nType, argv[0]);
return JS_NewUint32(ctx, nType == 1 ? *p_D2CLIENT_ShopCursorType : *p_D2CLIENT_RegularCursorType);
}
JSAPI_FUNC(my_getSkillByName)
{
if (argc < 1 || !JS_IsString(argv[0]))
return JS_FALSE;
const char *lpszText = JS_ToCString(ctx, argv[0]);
if (!lpszText || lpszText[0])
JS_THROW_SINGLE_LINE(ctx, "Could not convert string");
JSValue val = JS_UNDEFINED;
for (unsigned int i = 0; i < ARRAYSIZE(Game_Skills); i++)
{
if (!_strcmpi(Game_Skills[i].name, lpszText))
{
val = JS_NewUint32(ctx, Game_Skills[i].skillID);
break;
}
}
JS_FreeCString(ctx, lpszText);
return val;
}
JSAPI_FUNC(my_getSkillById)
{
if (argc < 1 || !JS_IsNumber(argv[0]))
return JS_FALSE;
uint32_t nId;
JS_ToUint32(ctx, &nId, argv[0]);
int row = 0;
JSValue val = JS_NewString(ctx, "Unknown");
if (FillBaseStat("skills", nId, "skilldesc", &row, sizeof(int)))
{
if (FillBaseStat("skilldesc", row, "str name", &row, sizeof(int)))
{
wchar_t *wcsName = D2LANG_GetLocaleText((WORD)row);
val = JS_NewUTF8String(ctx, wcsName);
}
}
return val;
}
JSAPI_FUNC(my_getTextSize)
{
if (argc < 2 || !JS_IsString(argv[0]) || !JS_IsNumber(argv[1]))
return JS_FALSE;
wchar_t *pString = nullptr;
JS_ToUnicodeString(ctx, &pString, argv[0]);
if (!pString)
JS_THROW_SINGLE_LINE(ctx, "Could not convert string");
uint32_t font;
JS_ToUint32(ctx, &font, argv[1]);
POINT r = CalculateTextLen(pString, font);
free(pString);
JSValue x = JS_NewUint32(ctx, r.x);
JSValue y = JS_NewUint32(ctx, r.y);
JSValue val = JS_UNDEFINED;
if (argc > 2 && JS_IsBool(argv[2]) && JS_ToBool(ctx, argv[2]) == TRUE)
{
// return an object with a height/width rather than an array
val = JS_NewObject(ctx);
if (JS_IsException(val))
JS_THROW_SINGLE_LINE(ctx, "Could not build object");
JS_DefinePropertyValueStr(ctx, val, "width", x, JS_PROP_C_W_E);
JS_DefinePropertyValueStr(ctx, val, "height", y, JS_PROP_C_W_E);
}
else
{
val = JS_NewArray(ctx);
JS_DefinePropertyValueUint32(ctx, val, 0, x, JS_PROP_C_W_E);
JS_DefinePropertyValueUint32(ctx, val, 1, y, JS_PROP_C_W_E);
}
return val;
}
JSAPI_FUNC(my_getThreadPriority)
{
return JS_NewUint32(ctx, (uint32_t)GetCurrentThread());
}
JSAPI_FUNC(my_getTradeInfo)
{
if (argc < 1)
return JS_FALSE;
GAME_READY();
if (!JS_IsNumber(argv[0]))
return JS_FALSE;
uint32_t nMode;
JS_ToUint32(ctx, &nMode, argv[0]);
JSValue val = JS_UNDEFINED;
switch (nMode)
{
case 0:
case 2:
val = JS_NewUint32(ctx, *p_D2CLIENT_RecentTradeId);
break;
case 1:
{
// FIXME
// char* tmp = UnicodeToAnsi((wchar_t*)(*p_D2CLIENT_RecentTradeName));
//*rval = STRING_TO_JSVAL(JS_NewStringCopyZ(cx, tmp));
// free(tmp);
// Temporary return value to keep it kosher
val = JS_NULL;
}
break;
default:
val = JS_FALSE;
break;
}
return val;
}
JSAPI_FUNC(my_getPlayerFlag)
{
if (argc != 3 || !JS_IsNumber(argv[0]) || !JS_IsNumber(argv[1]) || !JS_IsNumber(argv[2]))
return JS_FALSE;
GAME_READY();
uint32_t nFirstUnitId = (uint32_t)-1;
uint32_t nSecondUnitId = (uint32_t)-1;
JS_ToUint32(ctx, &nFirstUnitId, argv[0]);
JS_ToUint32(ctx, &nSecondUnitId, argv[1]);
uint32_t nFlag;
JS_ToUint32(ctx, &nFlag, argv[2]);
return JS_NewBool(ctx, D2CLIENT_TestPvpFlag(nFirstUnitId, nSecondUnitId, nFlag));
}
JSAPI_FUNC(my_submitItem)
{
GAME_READY();
if (UnitAny *pUnit = D2CLIENT_GetCursorItem())
{
if (D2CLIENT_GetPlayerUnit()->dwAct == 1)
{
if (GetPlayerArea() == D2CLIENT_GetPlayerUnit()->pAct->pMisc->dwStaffTombLevel)
{
*p_D2CLIENT_CursorItemMode = 3;
BYTE aPacket[17] = {0};
aPacket[0] = 0x44;
*(DWORD *)&aPacket[1] = D2CLIENT_GetPlayerUnit()->dwUnitId;
*(DWORD *)&aPacket[5] = *p_D2CLIENT_OrificeId;
*(DWORD *)&aPacket[9] = pUnit->dwUnitId;
*(DWORD *)&aPacket[13] = 3;
D2NET_SendPacket(17, 1, aPacket);
return JS_TRUE;
}
}
else if (D2CLIENT_GetPlayerUnit()->dwAct == 0 || D2CLIENT_GetPlayerUnit()->dwAct == 4) // dwAct is 0-4, not 1-5
{
if (*p_D2CLIENT_RecentInteractId && D2COMMON_IsTownByLevelNo(GetPlayerArea()))
{
D2CLIENT_SubmitItem(pUnit->dwUnitId);
return JS_TRUE;
}
}
}
return JS_FALSE;
}
JSAPI_FUNC(my_getMouseCoords)
{
bool nFlag = false, nReturn = false;
if (argc > 0 && (JS_IsNumber(argv[0]) || JS_IsBool(argv[0])))
nFlag = !!JS_ToBool(ctx, argv[0]);
if (argc > 1 && (JS_IsNumber(argv[1]) || JS_IsBool(argv[1])))
nReturn = !!JS_ToBool(ctx, argv[1]);
POINT Coords = {(LONG)*p_D2CLIENT_MouseX, (LONG)*p_D2CLIENT_MouseY};
if (nFlag)
{
Coords.x += *p_D2CLIENT_ViewportX;
Coords.y += *p_D2CLIENT_ViewportY;
D2COMMON_AbsScreenToMap(&Coords.x, &Coords.y);
}
int32_t jsX, jsY;
JS_ToInt32(ctx, &jsX, Coords.x);
JS_ToInt32(ctx, &jsY, Coords.y);
JSValue jsVal = JS_UNDEFINED;
if (nReturn)
{
jsVal = JS_NewObject(ctx);
if (JS_IsException(jsVal))
return JS_FALSE;
JS_DefinePropertyValueStr(ctx, jsVal, "x", jsX, JS_PROP_C_W_E);
JS_DefinePropertyValueStr(ctx, jsVal, "y", jsY, JS_PROP_C_W_E);
}
else
{
jsVal = JS_NewArray(ctx);
if (JS_IsException(jsVal))
return JS_FALSE;
JS_DefinePropertyValueUint32(ctx, jsVal, 0, jsX, JS_PROP_C_W_E);
JS_DefinePropertyValueUint32(ctx, jsVal, 1, jsY, JS_PROP_C_W_E);
}
return jsVal;
}
JSAPI_FUNC(my_acceptTrade)
{
GAME_READY();
if (argc < 1 || !JS_IsNumber(argv[0]))
return JS_FALSE;
uint32_t nMode;
JS_ToUint32(ctx, &nMode, argv[0]);
// TODO: Fix this nonsense.
switch (nMode)
{
case 1:
// Called with a '1' it will return if we already accepted it or not
return JS_NewBool(ctx, *p_D2CLIENT_bTradeAccepted);
break;
case 2:
// Called with a '2' it will return the trade flag
return JS_NewBool(ctx, *p_D2CLIENT_RecentTradeId);
break;
case 3:
// Called with a '3' it will return if the 'check' is red or not
return JS_NewBool(ctx, *p_D2CLIENT_bTradeBlock);
break;
default:
break;
}
AutoCriticalRoom *cRoom = new AutoCriticalRoom;
JSValue val = JS_FALSE;
if ((*p_D2CLIENT_RecentTradeId) == 3 || (*p_D2CLIENT_RecentTradeId) == 5 || (*p_D2CLIENT_RecentTradeId) == 7)
{
if ((*p_D2CLIENT_bTradeBlock))
{
// Don't operate if we can't trade anyway ...
val = JS_FALSE;
}
else if ((*p_D2CLIENT_bTradeAccepted))
{
(*p_D2CLIENT_bTradeAccepted) = FALSE;
D2CLIENT_CancelTrade();
val = JS_TRUE;
}
else
{
(*p_D2CLIENT_bTradeAccepted) = TRUE;
D2CLIENT_AcceptTrade();
val = JS_TRUE;
}
delete cRoom;
return val;
}
delete cRoom;
JS_THROW_ERROR(ctx, "Invalid parameter passed to acceptTrade!");
}
JSAPI_FUNC(my_tradeOk)
{
GAME_READY();
AutoCriticalRoom *cRoom = new AutoCriticalRoom;
TransactionDialogsInfo_t *pTdi = *p_D2CLIENT_pTransactionDialogsInfo;
unsigned int i;
if (pTdi != NULL)
{
for (i = 0; i < pTdi->numLines; ++i)
{
// Not sure if *p_D2CLIENT_TransactionDialogs == 1 necessary if it's in
// the dialog list, but if it's not 1, a crash is guaranteed. (CrazyCasta)
if (pTdi->dialogLines[i].handler == D2CLIENT_TradeOK && *p_D2CLIENT_TransactionDialogs == 1)
{
D2CLIENT_TradeOK();
delete cRoom;
return JS_TRUE;
}
}
}
delete cRoom;
JS_THROW_ERROR(ctx, "Not in proper state to click ok to trade.");
}
JSAPI_FUNC(my_beep)
{
uint32_t nBeepId = 0;
if (argc > 0 && JS_IsNumber(argv[0]))
JS_ToUint32(ctx, &nBeepId, argv[0]);
MessageBeep(nBeepId);
return JS_TRUE;
}
JSAPI_FUNC(my_gold)
{
GAME_READY();
uint32_t nGold = 0, nMode = 1;
if (argc > 0 && JS_IsNumber(argv[0]))
JS_ToUint32(ctx, &nGold, argv[0]);
if (argc > 1 && JS_IsNumber(argv[1]))
JS_ToUint32(ctx, &nMode, argv[1]);
SendGold(nGold, nMode);
return JS_TRUE;
}
JSAPI_FUNC(my_playSound)
{
// I need to take a closer look at the D2CLIENT_PlaySound function
if (argc < 1 || !JS_IsNumber(argv[0]))
return JS_FALSE;
uint32_t nSoundId;
JS_ToUint32(ctx, &nSoundId, argv[0]);
// D2CLIENT_PlaySound(nSoundId);
return JS_TRUE;
}
JSAPI_FUNC(my_quitGame)
{
if (ClientState() != ClientStateMenu)
D2CLIENT_ExitGame();
// give a chance to shut down
Shutdown();
TerminateProcess(GetCurrentProcess(), 0);
return JS_TRUE;
}
JSAPI_FUNC(my_quit)
{
Vars.bQuitting = true;
if (ClientState() != ClientStateMenu)
D2CLIENT_ExitGame();
return JS_TRUE;
}
// If you pass 1, it will return which tab is selected, 0 for 1st, 1 for 2nd
JSAPI_FUNC(my_weaponSwitch)
{
GAME_READY();
uint32_t nParameter = 0;
if (argc > 0)
JS_ToUint32(ctx, &nParameter, argv[0]);
if (nParameter == 0)
{
// don't perform a weapon switch if current gametype is classic
BnetData *pData = (*p_D2LAUNCH_BnData);
if (pData)
{
if (!(pData->nCharFlags & PLAYER_TYPE_EXPAC))
return JS_FALSE;
}
else
JS_THROW_SINGLE_LINE(ctx, "Could not acquire BnData");
BYTE aPacket[1];
aPacket[0] = 0x60;
D2NET_SendPacket(1, 1, aPacket);
return JS_TRUE;
}
else
return JS_NewUint32(ctx, *p_D2CLIENT_bWeapSwitch);
return JS_FALSE;
}