-
Notifications
You must be signed in to change notification settings - Fork 154
Expand file tree
/
Copy pathChatScriptIDE.cpp
More file actions
3532 lines (3280 loc) · 120 KB
/
ChatScriptIDE.cpp
File metadata and controls
3532 lines (3280 loc) · 120 KB
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
// ChatScriptIDE.cpp : Defines the entry point for the application.
//
#include "stdafx.h"
#include "ChatScriptIDE.h"
#include <string>
#include <WindowsX.h>
#include "../SRC/common.h"
using namespace std;
/* entry points:
DebugCall
DebugAction
DebugVar
DebugMessage
DebugMark
*/
typedef struct WINDOWSTRUCT
{
HWND window;
int linesPerPage;
int charsPerPage;
int maxLines;
int maxChars;
RECT rect;
SIZE metrics;
int xposition;
int yposition;
} WINDOWSTRUCT;
WINDOWSTRUCT scriptData, statData, varData, sentenceData, stackData, inputData, outputData;
#define NO_BREAK 0
#define BREAK_CALL 1
#define BREAK_ACTION 2
#define BREAK_VAR 3
#define BREAK_MSG 4
typedef struct MAPDATA
{
uint64 botid; // restriction if any
MAPDATA* next; // maps are kept in order start to finish of a file,
char* filename;
int fileline;
char* name;
} MAPDATA;
typedef struct LINEDATA
{
LINEDATA* next;
LINEDATA* prior;
MAPDATA* mapentry;
char text[1]; // actually it can be any size
} LINEDATA;
typedef struct FILEDATA
{
FILEDATA* next;
char* filename;
LINEDATA* filelines; // actually, char*, char*, linecharacters
MAPDATA* map; // start of map info on this file
char* status;
int charsize;
int lineCount;
} FILEDATA;
typedef struct CLICKLIST
{
FILEDATA* file;
CLICKLIST* next;
int yposition;
int mousey;
} CLICKLIST;
static void UpdateWindowMetrics(WINDOWSTRUCT& data, bool font = false);
CLICKLIST* clickList = NULL;
static void AddTab(char* name);
FILEDATA* currentFileData = NULL;
static int offsetCode = -1;
int breakType = NO_BREAK;
static bool WhereAmI(int depth);
static char breakAt[400][40];
static int rulesInMap = 0;
static int rulesExecuted = 0;
static int breakIndex = 0;
int sentenceMode = 0;
static bool sysfail = false;
static int nextdepth = -1;
static int nextlevel = -1;
static int idedepth = -1;
static int ideoutputlevel = -1;
static char* fnvars = NULL;
static int fnlevel = -1;
static void ClearStops();
static uintptr_t csThread = 0;
static MAPDATA* lastItem = NULL;
static char filenames[1000];
static char varnames[1000];
static char varbreaknames[1000];
static MAPDATA* firstMap = NULL;
static MAPDATA* priorMapLine = NULL;
static char* DebugInput(char* input);
static void MakeCurrentFile(char* name, uint64 botid);
static char windowTitle[MAX_WORD_SIZE];
static int breakpointCount = 0;
static FILEDATA* fileList = NULL;
static char varAt[100][40];
static int varIndex = 0;
LINEDATA* firstline = NULL;
LINEDATA* priorline = NULL;
LINEDATA* line = NULL;
int outlevel = 0;
#define MAX_LOADSTRING 100
#define ID_EDITCHILD 100 // output
#define ID_EDITCHILD1 101 // input
HWND hParent = NULL;
static void RemoveBreak(char* name);
HWND hGoButton, hStopButton, hClearButton, hNextButton, hInButton, hOutButton;
HWND hLocalsButton, hBackButton, hBreakMessageButton;
HWND hFontButton, hFailButton;
HWND hDialog;
int fontsize = 30;
size_t editLen = 0;
bool changingEdit = false;
SCROLLINFO si;
static int ProcessBreak(char* input, int flags);
HFONT hFont, hFontBigger, hButtonFont;
HPEN hPen;
HBRUSH hBrush;
HBRUSH hBrushBlue;
HBRUSH hBrushOrange;
int mouseLine = -1;
int codeLine = -1; // where are we
FILEDATA* codeFile = NULL;
int codeOffset = -1;
char* codePtr = NULL;
int mouseCharacter = -1;
char transientBreak[200];
RECT titlerect;
RECT numrect;
RECT tagrect;
static char* DebugVar(char* name, char* value);
static char* DebugMark(char* name, char* value);
int outdepth = -1;
static char* DebugOutput(char* output);
// Global Variables:
HINSTANCE hInst; // current instance
char szTitle[MAX_LOADSTRING]; // The title bar text
char szWindowClass[MAX_LOADSTRING]; // the main scriptData.window class name
char* DoneCallback(char* buffer);
// Forward declarations of functions included in this code module:
ATOM MyRegisterClass(HINSTANCE hInstance, LPCSTR szWindowClass);
BOOL InitInstance(HINSTANCE, int);
LRESULT CALLBACK WndProc(HWND, UINT, WPARAM, LPARAM);
INT_PTR CALLBACK About(HWND, UINT, WPARAM, LPARAM);
static FILEDATA* GetFile(char* name, uint64 botid);
int APIENTRY wWinMain(_In_ HINSTANCE hInstance,
_In_opt_ HINSTANCE hPrevInstance,
_In_ LPWSTR lpCmdLine,
_In_ int nCmdShow)
{
UNREFERENCED_PARAMETER(hPrevInstance);
UNREFERENCED_PARAMETER(lpCmdLine);
if (!SetCurrentDirectory((LPCSTR)".."))
{
printf("no change dir");
}
// TODO: Place code here.
// Initialize global strings
LoadString(hInstance, IDS_APP_TITLE, szTitle, MAX_LOADSTRING);
LoadString(hInstance, IDC_CHATSCRIPTIDE, szWindowClass, MAX_LOADSTRING);
ATOM j = MyRegisterClass(hInstance, (LPCSTR)szWindowClass);
// Perform application initialization:
if (!InitInstance(hInstance, nCmdShow))
{
return FALSE;
}
HACCEL hAccelTable = LoadAccelerators(hInstance, MAKEINTRESOURCE(IDC_CHATSCRIPTIDE));
MSG msg;
// Main message loop:
while (GetMessage(&msg, nullptr, 0, 0))
{
if (!TranslateAccelerator(msg.hwnd, hAccelTable, &msg))
{
TranslateMessage(&msg);
DispatchMessage(&msg);
}
}
return (int)msg.wParam;
}
static MAPDATA* FindFileMap(char* filename, uint64 botid)
{
MAPDATA* at = firstMap;
while (at)
{
// map has link, file, line, name
// we match either full path or tail naming only
if ((at->botid == botid || at->botid & botid) &&
(!stricmp(at->name, filename) || !stricmp(at->filename, filename)))
{
return at;
}
at = at->next; // use forward ptr
}
return NULL;
}
static MAPDATA* FindLine(int line)
{
return NULL;
}
static void ReadWindow(HWND window, FILE* in)
{
char buffer[500];
ReadALine(buffer, in);
char* data = buffer;
char word[MAX_WORD_SIZE];
data = ReadCompiledWord(data, word);
data = ReadCompiledWord(data, word);
int x = atoi(word);
data = ReadCompiledWord(data, word);
int y = atoi(word);
data = ReadCompiledWord(data, word);
int z = atoi(word);
data = ReadCompiledWord(data, word);
int q = atoi(word);
MoveWindow(window, x, y, z, q, true);
}
static void RestoreWindows()
{
FILE* in = FopenReadOnly("idesettings.txt");
if (!in) return;
ReadWindow(scriptData.window, in);
UpdateWindowMetrics(scriptData, true);
ReadWindow(inputData.window, in);
ReadWindow(outputData.window, in);
UpdateWindowMetrics(outputData, true);
ReadWindow(statData.window, in);
UpdateWindowMetrics(statData, true);
ReadWindow(varData.window, in);
UpdateWindowMetrics(varData, true);
ReadWindow(stackData.window, in);
UpdateWindowMetrics(stackData, true);
ReadWindow(hGoButton, in);
ReadWindow(hInButton, in);
ReadWindow(hOutButton, in);
ReadWindow(hNextButton, in);
ReadWindow(hStopButton, in);
ReadWindow(hBreakMessageButton, in);
ReadWindow(hClearButton, in);
ReadWindow(hLocalsButton, in);
ReadWindow(hBackButton, in);
ReadWindow(hFontButton, in);
ReadWindow(hFailButton, in);
char buffer[100];
ReadALine(buffer, in);
fontsize = atoi(buffer + 10);
fclose(in);
}
static void SaveWindow(HWND window, char* name, FILE* out)
{
RECT rect;
GetWindowRect(window, &rect);
fprintf(out, "%s: %d %d %d %d \r\n", name, rect.left, rect.top, rect.right, rect.bottom);
}
static void SaveWindows()
{
FILE* out = FopenBinaryWrite("idesettings.txt");
if (!out) return;
SaveWindow(scriptData.window, "script", out);
SaveWindow(inputData.window, "input", out);
SaveWindow(outputData.window, "output", out);
SaveWindow(statData.window, "statistics", out);
SaveWindow(varData.window, "variables", out);
SaveWindow(stackData.window, "stack", out);
SaveWindow(hGoButton, "go", out);
SaveWindow(hInButton, "in", out);
SaveWindow(hOutButton, "out", out);
SaveWindow(hNextButton, "next", out);
SaveWindow(hStopButton, "stop", out);
SaveWindow(hBreakMessageButton, "msgbreak", out);
SaveWindow(hClearButton, "clear", out);
SaveWindow(hLocalsButton, "locals", out);
SaveWindow(hBackButton, "back", out);
SaveWindow(hFontButton, "font", out);
SaveWindow(hFailButton, "fail", out);
fprintf(out, "fontsize: %d\r\n", fontsize);
fclose(out);
}
static void DumpMaps()
{
FILE* out = FopenBinaryWrite("tmp/tmp.txt");
MAPDATA* map = firstMap;
while (map)
{
fprintf(out, "%d %s %s\r\n", map->fileline, map->filename, map->name);
map = map->next;
}
fclose(out);
}
static MAPDATA* FindMap(char* name, char* filename)
{
MAPDATA* at = firstMap;
char* oldname = NULL;
while (at)
{
if (at->filename != oldname)
{
oldname = at->filename;
Log(STDDEBUGLOG, "%s\r\n", oldname);
}
char* entry = at->name; // map has link, file, line, name
if (filename && stricmp(at->filename, filename)) { ; }
else
{
if (*entry == '^' || *entry == '~') priorMapLine = at;
if (!stricmp(name, entry))
return at;
}
at = at->next; // use forward ptr
}
return NULL;
}
static MAPDATA* FindLabelMap(char* label, uint64 botid)
{
MAPDATA* at = firstMap;
while (at)
{
// map has link, file, line, name
// we match either full path or tail naming only
if ((at->botid == botid || at->botid & botid) &&
*at->name == '~')
{
char* hyphen = strchr(at->name, '-');
if (hyphen && strchr(at->name, '.')) // tag + label
{
if (!stricmp(hyphen + 1, label))
return at;
}
}
at = at->next; // use forward ptr
}
return NULL;
}
static void DefineVertScroll(WINDOWSTRUCT& data, int pos)
{
if (!data.window) return;
si.fMask = SIF_POS | SIF_RANGE;
if (pos >= 0) si.nPos = pos;
si.nMin = 0;
si.nMax = data.maxLines;
SetScrollInfo(data.window, SB_VERT, &si, true);
InvalidateRgn(data.window, NULL, true);
}
static char* ShowActions(char* actions, int level)
{
static char src[100];
sprintf(src, "lvl:%d ", level);
char* at = src + strlen(src);
strncpy(at, actions, 40);
at[40] = 0;
at = strchr(at, '`');
if (at) *at = 0;
return src;
}
static char* GetDefn(int depth)
{
CALLFRAME* frame = GetCallFrame(depth);
if (*frame->label != '^') return NULL; // not a callframe
char* definition = frame->definition;
if (!definition) return NULL;
return ++definition; // ( xxx xxx )
}
static void StackVariables(int depth)
{
fnvars = 0;
fnlevel = -1;
if (depth <= 0 || depth > globalDepth) return;
fnvars = GetDefn(depth);
sprintf(windowTitle, "local variables");
SetWindowText(varData.window, (LPCTSTR)windowTitle);
fnlevel = depth;
// now at "(xxx xxx xxx )" argument list + locals
}
static void BackClick()
{
if (!clickList) return;
CLICKLIST* click = clickList;
clickList = click->next;
mouseLine = click->mousey;
scriptData.yposition = click->yposition;
}
static void AddClick()
{
// already there
if (clickList && clickList->file == currentFileData &&
clickList->yposition == scriptData.yposition) return;
CLICKLIST* click = (CLICKLIST*)malloc(sizeof(CLICKLIST));
click->next = clickList;
clickList = click;
click->file = currentFileData;
click->yposition = scriptData.yposition;
click->mousey = mouseLine;
}
static void RestoreClick()
{
if (!clickList) return;
CLICKLIST* click = clickList;
clickList = clickList->next;
scriptData.yposition = click->yposition;
mouseLine = click->mousey;
currentFileData = click->file;
AddTab(click->file->filename);
scriptData.maxLines = currentFileData->lineCount;
scriptData.maxChars = currentFileData->charsize;
free(click);
InvalidateRgn(scriptData.window, NULL, true);
}
static void FreeClicklist()
{
while (clickList)
{
CLICKLIST* next = clickList->next;
free(clickList);
clickList = next;
}
}
static char* ChaseFnVariable(char* name, int mydepth)
{
if (globalDepth <= 0) return NULL; // not stopped in execution
int depth = (mydepth <= 0) ? 1 : mydepth; // go to current
char label[MAX_WORD_SIZE];
sprintf(label, "%s ", name);
CALLFRAME* frame = GetCallFrame(depth);
if (*name == '$') while (++depth <= globalDepth) // go deeper to see
{
frame = GetCallFrame(depth);
if (*frame->label == '~' && strchr(frame->label, '.')) continue;
char* defn = frame->definition;
if (!defn) continue;
char* end = strchr((char*)defn, ')');
*end = 0;
// find variable in list
char* found = strstr((char*)defn, label);
*end = ')';
if (!found) continue; // not used by this
// find which display arg it is
*found = 0;
int n = 0;
char* at = (char*)defn + 2;
char arg[MAX_WORD_SIZE];
while (*at)
{
at = ReadCompiledWord(at, arg);
if (*arg == USERVAR_PREFIX) ++n; // we dont count ^args
}
*found = *label;
// need the nth arg old display value
char* val = frame->display[n];
return (!*val) ? "null" : val;
}
if (*name == '^')
{
// find which display arg it is
char* defn = frame->definition;
if (!defn) return "null"; // not yet visible
char* at = (char*)defn + 2;
char arg[MAX_WORD_SIZE];
int n = 1; // ARGUMENT(1)
while (*at)
{
at = ReadCompiledWord(at, arg);
if (!stricmp(arg, name)) break;
++n;
if (n > frame->arguments) return "null"; // not there, may not have been even called
}
char* answer = callArgumentList[frame->varBaseIndex + n];
return answer;
}
return GetUserVariable(name); // current value is correct
}
static void DefineHorzScroll(WINDOWSTRUCT& data, int pos)
{
if (!data.window) return;
si.fMask = SIF_POS | SIF_RANGE;
if (pos >= 0) si.nPos = pos;
si.nMin = 0;
si.nMax = data.maxChars;
SetScrollInfo(data.window, SB_HORZ, &si, true);
InvalidateRgn(data.window, NULL, true);
}
static void ShowStack()
{
if (stackData.window) // compute scroll data for stack window
{
int maxchar = 0;
for (int i = 1; i <= globalDepth; ++i)
{
CALLFRAME* frame = GetCallFrame(i);
size_t len = strlen(frame->label);
if (len > maxchar) maxchar = len;
}
stackData.maxChars = maxchar;
stackData.maxLines = globalDepth;
stackData.xposition = 0;
if (globalDepth > stackData.linesPerPage)
stackData.yposition = globalDepth - stackData.linesPerPage;
DefineVertScroll(stackData, stackData.yposition);
DefineHorzScroll(stackData, 0);
InvalidateRgn(stackData.window, NULL, true);
StackVariables(globalDepth);
InvalidateRgn(varData.window, NULL, true);
}
}
static void ShowVariables()
{
if (varData.window) InvalidateRgn(varData.window, NULL, true);
}
char* DoneCallback(char* buffer)
{
sprintf(windowTitle, "%s=>%s (%I64u)", loginID, computerID, myBot);
SetWindowText(scriptData.window, (LPCTSTR)windowTitle);
ShowVariables();
stackData.maxLines = 0;
stackData.maxChars = 0;
DefineVertScroll(stackData, 0);
DefineHorzScroll(stackData, 0);
// no reason to update stack window, we have finished volley
return buffer;
}
static void UpdateWindowMetrics(WINDOWSTRUCT& data, bool font)
{
HDC dc = GetDC(data.window);
if (font) SelectObject(dc, hFontBigger);
else SelectObject(dc, hFont);
GetTextExtentPoint32(dc, (LPCTSTR)"12TzqbABCDEFGHIJKLMNOPQRSTUVWXYZ", 32, &data.metrics);
ReleaseDC(data.window, dc);
data.metrics.cx /= 32;
data.metrics.cx += 1;
data.charsPerPage = ((data.rect.right - data.rect.left) / data.metrics.cx) - 1;
data.linesPerPage = ((data.rect.bottom - data.rect.top) / data.metrics.cy);
si.fMask = SIF_RANGE | SIF_PAGE;
si.nPage = data.charsPerPage;
si.nMin = 0;
si.nMax = data.maxChars;
SetScrollInfo(data.window, SB_HORZ, &si, false);
// we can display this many lines
si.nPage = data.linesPerPage;
si.nMin = 0;
si.nMax = data.maxLines;
SetScrollInfo(data.window, SB_VERT, &si, false);
}
static void UpdateMetrics()
{
UpdateWindowMetrics(scriptData);
scriptData.rect.top = scriptData.metrics.cy;
titlerect.bottom = scriptData.metrics.cy;
InvalidateRgn(scriptData.window, NULL, true);
if (varData.window)
{
UpdateWindowMetrics(varData);
InvalidateRgn(varData.window, NULL, true);
}
if (outputData.window)
{
UpdateWindowMetrics(outputData);
InvalidateRgn(outputData.window, NULL, true);
}
if (statData.window)
{
UpdateWindowMetrics(statData);
InvalidateRgn(statData.window, NULL, true);
}
if (stackData.window)
{
UpdateWindowMetrics(stackData);
InvalidateRgn(stackData.window, NULL, true);
}
}
void EraseVariable(int which)
{
if (!*varnames) return;
char* var = varnames;
size_t len;
char name[MAX_WORD_SIZE];
char* end;
while (*var)
{
// get next variable name
end = strstr(var, "\r\n");
if (!end) break; *end = 0;
if (which-- == 0) break;
*end = '\r';
var = end + 2;
}
ReadCompiledWord(var, name);
if (end) *end = '\r';
char junk[MAX_WORD_SIZE];
sprintf(junk, "%s\r\n", name);
char* found = strstr(varnames, junk);
if (!found) return;
len = strlen(name);
--varData.maxLines;
char* after = found + len + 2;
memmove(found, after, strlen(after) + 2); // remove name
}
static void VerifyMap()
{
FILEDATA* file = fileList;
int count = 0;
bool found = false;
while (file)
{
LINEDATA* fileLines = file->filelines;
while (fileLines)
{
MAPDATA* map = fileLines->mapentry;
if (map->name && (map->name, "control.9")) found = true;
++count;
fileLines = fileLines->next;
}
file = file->next;
}
if (found)
{
int xx = 0;
}
else
{
int xx = 0;
}
}
static void DoBreakVariable(int which)
{
if (!*varnames && !fnvars)
{
return;
}
char* var = (fnvars) ? fnvars : varnames;
size_t len;
char name[MAX_WORD_SIZE];
char* end;
while (*var)
{
// get next variable name
end = strstr(var, "\r\n");
if (!end) end = strstr(var, " "); // fnvars
if (!end) end = strstr(var, ")"); // fnvars
if (!end) break;
*end = 0;
if (which-- == 0) break;
*end = '\r';
var = end + 2;
}
ReadCompiledWord(var, name);
if (*name == '^')
{
DebugPrint("cannot breakpoint a ^var\r\n");
return; // cannot break on ^var
}
if (end) *end = '\r';
char junk[MAX_WORD_SIZE];
sprintf(junk, "%s\r\n", name);
char* found = strstr(varbreaknames, junk);
len = strlen(name);
if (found) // remove name from existing list
{
char* after = found + len + 2;
memmove(found, after, strlen(after) + 2); // remove name
if (!*varbreaknames) debugVar = NULL;
return;
}
// insert at start of list
memmove(varbreaknames + len + 2, varbreaknames, strlen(varbreaknames) + 1);
memmove(varbreaknames, name, len);
varbreaknames[len] = '\r';
varbreaknames[len + 1] = '\n';
debugVar = DebugVar;
}
static void AddVariable(char* name)
{
char junk[MAX_WORD_SIZE];
sprintf(junk, "%s\r\n", name);
MakeLowerCase(junk);
char* found = strstr(varnames, junk);
size_t len = strlen(name);
if (found) // remove name from existing list
{
--varData.maxLines;
char* after = found + len + 2;
memmove(found, after, strlen(after) + 2); // remove name
}
// insert at start of list
memmove(varnames + len + 2, varnames, strlen(varnames) + 1);
memmove(varnames, junk, len);
varnames[len] = '\r';
varnames[len + 1] = '\n';
++varData.maxLines;
DefineVertScroll(varData, 0);
if (*loginID && !csThread) ReadUserData(); // but not if suspended or unstarted
ShowVariables();
if (!csThread) ResetToPreUser();// but not if suspended
}
static int NameLine(char* name)
{
MAPDATA* symbol = FindMap(name, NULL); // find name in any file
if (!symbol) return -1;
char* file = symbol->filename;
MakeCurrentFile(file, symbol->botid);
return symbol->fileline;
}
uint64 FindBotID(char* name)
{
char id[MAX_WORD_SIZE];
MakeLowerCopy(id, name);
strcat(id, "`bot");
MAPDATA* at = firstMap;
while (at)
{
char* entry = at->name; // map has link, file, line, name
if (!stricmp(id, entry))
return at->botid;
at = at->next; // use forward ptr
}
return (uint64)(int64)-1;
}
static void RemoveTab(char* name)
{
char fullname[1000];
sprintf(fullname, " %s ", name);
char* found = strstr(filenames, fullname);
size_t len = strlen(fullname);
if (found) // remove short name + space from existing list
{
char* after = found + len - 1;
memmove(found, after, strlen(after) + 1); // remove name
if (filenames[1] == ' ') // if it was leader, change leader
{
memmove(filenames, filenames + 1, strlen(filenames) + 1);
char word[MAX_WORD_SIZE];
ReadCompiledWord(filenames, word);
AddTab(word); // reinsert properly
}
InvalidateRgn(scriptData.window, NULL, true);
}
}
static void AddTab(char* name)
{
RemoveTab(name);
size_t len = strlen(name);
// remove 2 extra spaces on separator, make single space
char old[1000];
char* triple = strstr(filenames, " ");
if (triple)
{
strcpy(old, triple + 4);
strcpy(triple + 1, old);
}
strcpy(old, filenames);
*filenames = ' ';
strcpy(filenames + 1, name);
strcat(filenames, " ");
if (*old) strcat(filenames, old + 1); //leave off extra space
}
MAPDATA* AlignName(char* name)
{
char namex[1000];
strcpy(namex, name);
char* at = strchr(namex, '(');
if (at) *at = 0;
at = strchr(namex, '{');
if (at) *at = 0;
MAPDATA* symbol = FindMap(namex, NULL); // find name in any file
if (!symbol)symbol = FindLabelMap(namex, NULL);
if (!symbol) return NULL;
char* file = symbol->filename;
FILEDATA* oldfile = currentFileData;
AddClick();
MakeCurrentFile(file, symbol->botid);
mouseLine = symbol->fileline; // 1-based, whereas yposition is 0-based
mouseCharacter = 0;
scriptData.maxLines = currentFileData->lineCount;
scriptData.maxChars = currentFileData->charsize;
if (oldfile != currentFileData)
{
scriptData.yposition = symbol->fileline - 3; // start with item showing in context
scriptData.xposition = 0; // reset
}
else if (mouseLine > (scriptData.yposition + 1 + scriptData.linesPerPage))
scriptData.yposition = symbol->fileline - 3; // start with item showing in context
else if (mouseLine < scriptData.yposition + 1)
scriptData.yposition = symbol->fileline - 3; // start with item showing in context
else if ((symbol->fileline - scriptData.yposition - 1) < (scriptData.yposition + scriptData.linesPerPage)) { ; } // same page
else if (currentFileData->lineCount > scriptData.maxLines)
scriptData.yposition = symbol->fileline - 3; // start with item showing in context
else scriptData.yposition = 0;
if (scriptData.yposition < 0) scriptData.yposition = 0; // file name must not back up
DefineVertScroll(scriptData, scriptData.yposition);
scriptData.maxChars = currentFileData->charsize;
DefineHorzScroll(scriptData, 0);
return symbol;
}
void FindBot(char* name)
{
myBot = -1; // all access pass
if (name) // happens when we have logged in and gotten a bot
{
AlignName(name);
myBot = FindBotID(name);
sprintf(windowTitle, "%s=>%s (%I64u)", loginID, computerID, myBot);
SetWindowText(scriptData.window, (LPCTSTR)windowTitle);
return;
}
// happens on startup before login has occured
if (*defaultbot)
{
AlignName(defaultbot);
myBot = FindBotID(defaultbot);
sprintf(windowTitle, "%s=>%s (%I64u)", "", defaultbot, myBot);
SetWindowText(scriptData.window, (LPCTSTR)windowTitle);
return;
}
WORDP D = FindWord("defaultbot");
FACT* F = GetVerbNondeadHead(D);
while (F)
{
if (F->verb == F->object)
{
char name[MAX_WORD_SIZE];
D = Meaning2Word(F->subject);
sprintf(name, "^%s", D->word);
AlignName(name);
myBot = FindBotID(name);
sprintf(windowTitle, "%s=>%s (%I64u)", "", name, myBot);
SetWindowText(scriptData.window, (LPCTSTR)windowTitle);
break;
}
F = GetVerbNondeadNext(F);
}
}
//
// FUNCTION: MyRegisterClass()
//
// PURPOSE: Registers the scriptData.window class.
//
ATOM MyRegisterClass(HINSTANCE hInstance, LPCSTR szWindowClass)
{
WNDCLASSEX wcex;
wcex.cbSize = sizeof(WNDCLASSEX);
wcex.style = CS_HREDRAW | CS_VREDRAW;
wcex.lpfnWndProc = WndProc;
wcex.cbClsExtra = 0;
wcex.cbWndExtra = 0;
wcex.hInstance = hInstance;
wcex.hIcon = LoadIcon(hInstance, MAKEINTRESOURCE(IDI_CHATSCRIPTIDE));
wcex.hCursor = LoadCursor(nullptr, IDC_ARROW);
wcex.hbrBackground = (HBRUSH)(COLOR_WINDOW + 1);
wcex.lpszMenuName = MAKEINTRESOURCE(IDC_CHATSCRIPTIDE);
wcex.lpszClassName = szWindowClass;
wcex.hIconSm = LoadIcon(wcex.hInstance, MAKEINTRESOURCE(IDI_SMALL));
return RegisterClassEx(&wcex);
}
static char* GetStartCharacter(LINEDATA* line)
{
if (!line) return NULL;
char* msg = (char*)&line->text;
if (scriptData.xposition)
{
int count = scriptData.xposition;
while (count-- && *msg) ++msg;
}
return msg;
}
static LINEDATA* GetStartLine(int count)
{
if (!currentFileData) return NULL;
LINEDATA* at = currentFileData->filelines;
if (count)
{
while (count-- && at) at = at->next; // use forward ptr
}
return at;
}
static MAPDATA* MapAllocate(uint64 botid, char* file, char* name, char* line)
{
size_t len = strlen(name);
MAPDATA* map = (MAPDATA*)malloc(sizeof(MAPDATA)); // link, file, line, name
map->next = NULL; // forward link
map->botid = botid;
map->filename = file; // set file name
map->fileline = (line) ? atoi(line) : 0; // set line number
// when name is a number (line number) or if/else/loop then line is an offset into block or rule data
// rule: ~medical_glean.43.0-MEDICALBODYPARTKIND u: 246
char* x = (char*)malloc(len + 1);
strcpy(x, name);
map->name = x;
if (!firstMap) firstMap = map; // ordered
if (priorMapLine) priorMapLine->next = map; // set old forward ptr
priorMapLine = map;
return map;
}
static LINEDATA* ReadFile(char* name, size_t& maxchars, int& maxlines, uint64 botid)
{
FILE* in = fopen(name, "rb");
if (!in) return NULL;
char buffer[20000];
firstline = NULL;
priorline = NULL;
line = NULL;
maxlines = 0;
maxchars = 0;
MAPDATA* map = FindFileMap(name, botid); // get map for this
if (!map)
{
fclose(in);
return NULL;
}
int currentMapLine = map->fileline;
while (fgets(buffer, 20000, in)) // make linked list of all text
{
++maxlines;
char* at = buffer;
while ((at = strchr(at, '\t')))