/
ScriptEdit.cpp
1433 lines (1330 loc) · 35 KB
/
ScriptEdit.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
// ScriptEdit.cpp : implementation file
//
#include "stdafx.h"
#include <fcntl.h>
#include <sys/stat.h>
#include <direct.h>
#include "2da.h"
#include "chitemDlg.h"
#include "ScriptEdit.h"
#include "2daEdit.h"
#include "options.h"
#include "compat.h"
#include "WeiduLog.h"
#include "StrRefDlg.h"
#include "Decompiler.h"
#include "Compiler.h"
#include "MyFileDialog.h"
#ifdef _DEBUG
#define new DEBUG_NEW
#undef THIS_FILE
static char THIS_FILE[] = __FILE__;
#endif
#define SHOW_MESSAGES 1
#define FORCED_CHECK 2
#define WHOLE_CHECK 4
#define MAX_SCRIPT_LEN 6553599
#define LINES 30
//static function
//displays compiler data in human readable format
CString format_parameters(compiler_data &compiler_data)
{
CString ret, add;
int i;
for(i=0;i<compiler_data.parnum;i++)
{
switch(compiler_data.parameters[i].type)
{
case SPT_ZERO: add="0"; break;
case SPT_1AREA: case SPT_2AREA:
case SPT_AREA1: case SPT_AREA2: case SPT_AREA3: case SPT_AREA4: add="qualifier"; break;
case SPT_VAR1: case SPT_VAR2: case SPT_VAR3: case SPT_VAR4: add="variable"; break;
case SPT_OBJECT:case SPT_OVERRIDE: case SPT_SECONDOB: add="object"; break;
case SPT_INTEGER:case SPT_INTEGER2:case SPT_INTEGER3:
if(compiler_data.parameters[i].idsfile.IsEmpty()) add="integer";
else add="ids:"+compiler_data.parameters[i].idsfile;
break;
/*case SPT_STRING:*/case SPT_STRING1: case SPT_STRING2: add="string"; break;
case SPT_STRREF:case SPT_STRREF2: case SPT_STRREF3: add="strref"; break;
case SPT_ACTION: add="action"; break;
case SPT_POINT: add="point"; break;
case SPT_RES1:case SPT_RES2:case SPT_RES3:case SPT_RES4:
case SPT_RESREF1: case SPT_RESREF2: add="resource"; break;
case SPT_DEAD1: case SPT_DEAD2: add="deathvar"; break;
case SPT_COLUMN1: case SPT_COLUMN2: add="xplist"; break;
case SPT_TOKEN1: case SPT_TOKEN2: add="token"; break;
case SPT_NUMLIST1: case SPT_NUMLIST2: add="spellist"; break;
default: add="unknown";
}
if(i) ret+=", "+add;
else ret+=add;
}
return ret;
}
//a rather lame windows-style way of outputting a richedit control
unsigned long __stdcall writestream_callback(unsigned long cookie, LPBYTE buffer, long cb, long *pcb)
{
int res;
res=write(cookie,buffer,cb)!=cb;
*pcb=cb;
return res;
}
int writestream(int fhandle, CRichEditCtrl &ctrl)
{
EDITSTREAM es;
es.dwCookie=fhandle;
es.dwError=0;
es.pfnCallback=writestream_callback;
ctrl.StreamOut(SF_TEXT,es);
return es.dwError;
}
/////////////////////////////////////////////////////////////////////////////
// CScriptEdit dialog
CScriptEdit::CScriptEdit(CWnd* pParent /*=NULL*/)
: CDialog(CScriptEdit::IDD, pParent)
{
//{{AFX_DATA_INIT(CScriptEdit)
//}}AFX_DATA_INIT
m_errors=NULL;
m_count=0;
m_firsterror=0;
m_bcs=1;
m_searchdlg=NULL;
}
void CScriptEdit::DoDataExchange(CDataExchange* pDX)
{
CDialog::DoDataExchange(pDX);
//{{AFX_DATA_MAP(CScriptEdit)
DDX_Control(pDX, IDC_TEXT, m_text_control);
//}}AFX_DATA_MAP
}
BEGIN_MESSAGE_MAP(CScriptEdit, CDialog)
//{{AFX_MSG_MAP(CScriptEdit)
ON_BN_CLICKED(IDC_LOAD, OnLoad)
ON_BN_CLICKED(IDC_LOADEX, OnLoadex)
ON_BN_CLICKED(IDC_SAVEAS, OnSaveas)
ON_BN_CLICKED(IDC_NEW, OnNew)
ON_EN_SETFOCUS(IDC_TEXT, OnSetfocusText)
ON_BN_CLICKED(IDC_CHECK, OnCheck)
ON_COMMAND(ID_FILE_SAVESCRIPT, OnSaveAsBAF)
ON_COMMAND(ID_FILE_SAVECOMPILESCRIPT, OnSaveAsBCS)
ON_COMMAND(ID_FILE_LOADSOURCE, OnFileLoadsource)
ON_COMMAND(ID_SEARCH, OnSearch)
ON_EN_KILLFOCUS(IDC_TEXT, OnKillfocusText)
ON_EN_MAXTEXT(IDC_TEXT, OnMaxtextText)
ON_COMMAND(ID_OPTIONS_AUTOCHECK, OnOptionsAutocheck)
ON_COMMAND(ID_OPTIONS_LARGEINDENT, OnOptionsLargeindent)
ON_COMMAND(ID_EDIT_SELECTALL, OnEditSelectall)
ON_COMMAND(ID_EDIT_UNDO, OnEditUndo)
ON_COMMAND(ID_EDIT_CUT, OnEditCut)
ON_COMMAND(ID_EDIT_COPY, OnEditCopy)
ON_COMMAND(ID_EDIT_PASTE, OnEditPaste)
ON_COMMAND(ID_EDIT_DELETE, OnEditDelete)
ON_WM_CONTEXTMENU()
ON_COMMAND(ID_TOOLS_IDSBROWSER, OnIDS)
ON_WM_HELPINFO()
ON_EN_CHANGE(IDC_TEXT, OnChangeText)
ON_EN_UPDATE(IDC_TEXT, OnUpdateText)
ON_COMMAND(ID_TOOLS_LOOKUPSTRREF, OnToolsLookupstrref)
ON_COMMAND(ID_OPTIONS_USEINTERNALCOMPILER, OnOptionsUseinternalcompiler)
ON_COMMAND(ID_OPTIONS_USEINTERNALDECOMPILER, OnOptionsUseinternaldecompiler)
ON_COMMAND(ID_FILE_NEW, OnNew)
ON_COMMAND(ID_FILE_LOAD, OnLoad)
ON_COMMAND(ID_FILE_SAVEAS, OnSaveas)
ON_COMMAND(ID_FILE_LOADEXTERNALSCRIPT, OnLoadex)
ON_COMMAND(ID_CHECK, OnCheck)
ON_COMMAND(ID_OPTIONS_LOGGING, OnOptionsLogging)
//}}AFX_MSG_MAP
ON_REGISTERED_MESSAGE( WM_FINDREPLACE, OnFindReplace )
END_MESSAGE_MAP()
/////////////////////////////////////////////////////////////////////////////
// CScriptEdit message handlers
BOOL CScriptEdit::OnInitDialog()
{
CFont Font;
CString tmpstr,tmpstr1,tmpstr2;
the_ids.new_ids();
m_idsname="";
if(editflg&INDENT) m_indent=2;
else m_indent=4;
CDialog::OnInitDialog();
m_text_control.LimitText(MAX_SCRIPT_LEN);
m_text_control.ModifyStyle(0,ES_SAVESEL);
m_text_control.SetOptions(ECOOP_OR,ECO_AUTOWORDSELECTION|ECO_SAVESEL|ECO_WANTRETURN|ECO_NOHIDESEL);
//enables the update event for a rich edit control
m_text_control.SetEventMask(ENM_CHANGE|ENM_UPDATE|ENM_MOUSEEVENTS|ENM_KEYEVENTS|ENM_SCROLL|ENM_SELCHANGE);
Font.CreateFont(15,0,0,0,0,0,0,0,0,0,0,0,0,"Courier New");
m_text_control.SetFont(&Font);
Font.DeleteObject();
// UpdateData(UD_DISPLAY);
CheckScript(WHOLE_CHECK|FORCED_CHECK);
RefreshDialog();
RefreshMenu();
m_infind=0;
//tooltips
{
m_tooltip.Create(this,TTS_NOPREFIX);
m_tooltip.SetMaxTipWidth(200);
m_tooltip.SetTipBkColor(RGB(240,224,160));
m_tooltip.AddTool(GetDlgItem(IDCANCEL), IDS_CANCEL);
tmpstr1.LoadString(IDS_LOAD);
tmpstr2.LoadString(IDS_SCRIPT);
tmpstr.Format(tmpstr1, tmpstr2);
m_tooltip.AddTool(GetDlgItem(IDC_LOAD), tmpstr);
tmpstr1.LoadString(IDS_LOADEX);
tmpstr.Format(tmpstr1, tmpstr2);
m_tooltip.AddTool(GetDlgItem(IDC_LOADEX), tmpstr);
tmpstr1.LoadString(IDS_SAVE);
tmpstr.Format(tmpstr1, tmpstr2);
m_tooltip.AddTool(GetDlgItem(IDC_SAVEAS), tmpstr);
tmpstr1.LoadString(IDS_NEW);
tmpstr.Format(tmpstr1, tmpstr2);
m_tooltip.AddTool(GetDlgItem(IDC_NEW), tmpstr);
tmpstr1.LoadString(IDS_CHECK);
tmpstr.Format(tmpstr1, tmpstr2);
m_tooltip.AddTool(GetDlgItem(IDC_CHECK), tmpstr);
}
return TRUE;
}
int CScriptEdit::RunWeidu(CString syscommand)
{
CString tmpstr;
int res;
//flushing dialog.tlk
if(global_changed[0]==true)
{
((CChitemDlg *) AfxGetMainWnd())->write_file_progress(0);
}
if(global_changed[1]==true)
{
((CChitemDlg *) AfxGetMainWnd())->write_file_progress(1);
}
if(weiduflg&WEI_LOGGING)
{
unlink(WEIDU_LOG);
}
res=my_system(syscommand);
if(weiduflg&WEI_LOGGING)
{
if(file_exists(WEIDU_LOG) )
{
CWeiduLog dlg;
dlg.DoModal();
}
else MessageBox("No logfile has been created by WeiDU.","Script editor",MB_OK);
}
if(res)
{
tmpstr.Format("[%s] returned: %d",syscommand, res);
MessageBox(tmpstr,"Script editor",MB_OK|MB_ICONSTOP);
}
return res;
}
int CScriptEdit::compile(CString filepath)
{
CString tmpstr;
CString outpath;
CString syscommand;
CString tmpname;
int res;
res=0;
if(editflg&INTERNALCOMPILER)
{
if(weidupath.IsEmpty())
{
MessageBox("Please set up WeiDU before use.","Script editor",MB_OK|MB_ICONSTOP);
return -1;
}
if(!file_exists(weidupath) )
{
MessageBox("WeiDU executable not found.","Script editor",MB_OK|MB_ICONSTOP);
return -1;
}
}
chdir(bgfolder);
outpath.Format("override");
if(!assure_dir_exists(bgfolder+outpath))
{
tmpstr.Format("%s cannot be created as output path.",outpath);
MessageBox(tmpstr,"Dialog editor",MB_OK|MB_ICONSTOP);
return -1;
}
if(editflg&INTERNALCOMPILER)
{
syscommand=AssembleWeiduCommandLine(filepath,outpath, false); //import (compile)
res=RunWeidu(syscommand);
((CChitemDlg *) AfxGetMainWnd())->rescan_dialog(true);
((CChitemDlg *) AfxGetMainWnd())->scan_override();
}
else
{
Compiler *scmp = new Compiler(weiduflg&WEI_LOGGING);
int x = filepath.ReverseFind('\\');
int y = filepath.ReverseFind('/');
if (x>y) y=x;
if (y>=0)
{
tmpname = filepath.Mid(y+1);
}
else
{
tmpname = filepath;
}
y = tmpname.ReverseFind('.');
if (y>0)
{
tmpname=tmpname.Left(y);
}
//setting up compiler with options
res = scmp->Compile(filepath, bgfolder+"\\"+outpath+"\\"+tmpname+".bcs");
//actually this is not needed, delete will close all anyway
delete scmp;
if (res)
{
MessageBox("Compiler error.","Script editor",MB_OK|MB_ICONSTOP);
res = 0;
}
}
return res;
}
//decompiles file and returns the decompiled filename
//filepath is the long path of the compiled file
int CScriptEdit::decompile(CString &filepath, CString tmpname)
{
CString tmpstr;
CString syscommand;
int res;
unlink(bgfolder+weidudecompiled+"\\"+tmpname+".baf");
res=0;
//actually, if the bit is set, it means weidu is used
if(editflg&INTERNALDECOMP)
{
if(weidupath.IsEmpty())
{
MessageBox("Please set up WeiDU before use.","Script editor",MB_OK|MB_ICONSTOP);
return -1;
}
if(!file_exists(weidupath) )
{
MessageBox("WeiDU executable not found.","Script editor",MB_OK|MB_ICONSTOP);
return -1;
}
}
chdir(bgfolder);
if(!assure_dir_exists(bgfolder+weidudecompiled))
{
tmpstr.Format("%s cannot be created as output path.",weidudecompiled);
MessageBox(tmpstr,"Dialog editor",MB_OK|MB_ICONSTOP);
return -1;
}
if(global_changed[0]==true)
{
((CChitemDlg *) AfxGetMainWnd())->write_file_progress(0);
}
if(global_changed[1]==true)
{
((CChitemDlg *) AfxGetMainWnd())->write_file_progress(1);
}
if(editflg&INTERNALDECOMP)
{
syscommand=AssembleWeiduCommandLine(filepath,weidudecompiled, true); //export
res=RunWeidu(syscommand);
}
else
{
Decompiler *sdec = new Decompiler(weiduflg&WEI_LOGGING);
//setting up decompiler with options
res = sdec->Decompile(filepath, bgfolder+weidudecompiled+"\\"+tmpname+".baf");
//actually this is not needed, delete will close all anyway
sdec->CloseAll();
delete sdec;
if (res)
{
MessageBox("Decompiler error.","Script editor",MB_OK|MB_ICONSTOP);
res = 0;
}
}
filepath=bgfolder+weidudecompiled+"\\"+tmpname+".baf";
return res;
}
void CScriptEdit::OnLoad()
{
CString filepath;
CString m_text;
int fhandle;
int res;
char *pos;
pickerdlg.m_restype=REF_BCS;
pickerdlg.m_picked=itemname;
res=pickerdlg.DoModal();
if(res==IDOK)
{
filepath=pickerdlg.m_picked+".bcs";
itemname=pickerdlg.m_picked;
m_bcs=1;
res=decompile(filepath, itemname); //this will decompile the bcs
if(res)
{
MessageBox("Cannot decompile file!","Error",MB_OK);
}
else
{
fhandle=open(filepath, O_RDONLY|O_BINARY);
if(!fhandle)
{
MessageBox("Cannot open file!","Error",MB_OK);
res=-1;
}
if(!res)
{//fhandle will always get initialized if res was 0
res=filelength(fhandle);
if(res>=0)
{
pos=m_text.GetBufferSetLength(res);
if(pos)
{
if(read(fhandle,pos,res)!=res) res=-1;
else res=0;
}
else res=-1;
m_text.ReleaseBuffer();
m_text_control.SetWindowText(m_text);
}
close(fhandle);
}
}
switch(res)
{
case 0:
if((m_bcs==1) && (editflg&REMBAF) )
{//removing decompiled baf
unlink(filepath);
}
break;
default:
MessageBox("Cannot read script!","Error",MB_OK);
OnNew();
return;
}
}
UpdateData(UD_DISPLAY);
the_script.m_changed=false;
CheckScript(WHOLE_CHECK|FORCED_CHECK);
RefreshDialog();
}
//static char BASED_CODE szFiltera[] = "Script files (*.bcs)|*.bcs|Script Source files (*.baf)|*.baf|All files (*.*)|*.*||";
static char BASED_CODE szFilterb[] = "Script Source files (*.baf)|*.baf|Script files (*.bcs)|*.bcs|All files (*.*)|*.*||";
void CScriptEdit::OnLoadex()
{
char BASED_CODE *szFilter;
CString m_text;
CString fpath;
int fhandle;
int res;
char *pos;
res=OFN_FILEMUSTEXIST|OFN_ENABLESIZING|OFN_EXPLORER;
if(readonly) res|=OFN_READONLY;
szFilter=szFilterb;
CMyFileDialog m_getfiledlg(TRUE, m_bcs?"bcs":"baf", m_bcs ? makeitemname(".bcs",0): makeitemname(".baf",1), res, szFilter);
m_getfiledlg.m_ofn.nFilterIndex = m_bcs+1;
if( m_getfiledlg.DoModal() == IDOK )
{
filepath = fpath = m_getfiledlg.GetPathName();
if(checkfile(fpath,"SC") )
{
m_bcs=1;
res=decompile(filepath, m_getfiledlg.GetFileTitle()); //decompile first
if(res)
{
MessageBox("Cannot decompile file: "+fpath+"!","Error",MB_OK);
return;
}
lastopenedoverride=fpath.Left(filepath.ReverseFind('\\'));
}
else
{ //remember the path when opening a script source
m_bcs=0;
lastopenedscript=fpath.Left(filepath.ReverseFind('\\'));
}
fhandle=open(filepath, O_RDONLY|O_BINARY);
if(fhandle<1)
{
MessageBox("Cannot open file!","Error",MB_OK);
return;
}
readonly=m_getfiledlg.GetReadOnlyPref();
res=filelength(fhandle);
if(res>=0)
{
pos=m_text.GetBufferSetLength(res);
if(pos)
{
if(read(fhandle,pos,res)!=res) res=-9;
else res=0;
}
else res=-9;
}
m_text.ReleaseBuffer();
close(fhandle);
m_text_control.SetWindowText(m_text);
switch(res)
{
case 0:
if((m_bcs==1) && (editflg&REMBAF) )
{//removing decompiled baf
unlink(filepath);
}
itemname=m_getfiledlg.GetFileTitle();
itemname.MakeUpper();
break;
case -9: //special error: cannot read decompiled script
MessageBox("Cannot read script!","Error",MB_OK);
default: //that error was already handled elsewhere
OnNew();
return;
}
}
UpdateData(UD_DISPLAY);
the_script.m_changed=false;
CheckScript(WHOLE_CHECK|FORCED_CHECK);
RefreshDialog();
}
void CScriptEdit::OnSaveas()
{
OnSaveasGeneral(-1);
}
void CScriptEdit::OnSaveAsBCS()
{
OnSaveasGeneral(1);
}
void CScriptEdit::OnSaveAsBAF()
{
OnSaveasGeneral(0);
}
void CScriptEdit::OnSaveasGeneral(int type)
{
char BASED_CODE *szFilter;
CString tmpstr;
CString newname;
int fhandle;
int res;
if(readonly)
{
MessageBox("You opened it read only!","Warning",MB_ICONEXCLAMATION|MB_OK);
return;
}
res=OFN_HIDEREADONLY|OFN_ENABLESIZING|OFN_EXPLORER;
if(type!=-1) m_bcs=type;
szFilter=szFilterb;
if(m_bcs)
{
//szFilter=szFiltera;
tmpstr=bgfolder+"override\\"+itemname;
}
else
{
//szFilter=szFilterb;
tmpstr=makeitemname(".baf",1);
}
CMyFileDialog m_getfiledlg(FALSE, m_bcs?"bcs":"baf", tmpstr, res, szFilter);
m_getfiledlg.m_ofn.nFilterIndex = m_bcs+1;
restart:
if( m_getfiledlg.DoModal() == IDOK )
{
filepath=m_getfiledlg.GetPathName();
filepath.MakeLower();
if(filepath.Right(4)==".baf") m_bcs=0;
else if(filepath.Right(4)==".bcs") m_bcs=1; //this is the default
if(m_bcs)
{
if(filepath.Right(4)!=".bcs") filepath+=".bcs";
lastopenedoverride=filepath.Left(filepath.ReverseFind('\\'));
}
else
{
if(filepath.Right(4)!=".baf") filepath+=".baf";
lastopenedscript=filepath.Left(filepath.ReverseFind('\\'));
}
newname=m_getfiledlg.GetFileName();
newname.MakeUpper();
if(newname.Right(4)==".BCS") newname=newname.Left(newname.GetLength()-4);
else if(newname.Right(4)==".BAF") newname=newname.Left(newname.GetLength()-4);
if(newname.GetLength()>8 || newname.GetLength()<1 || newname.Find(" ",0)!=-1)
{
tmpstr.Format("The resource name '%s' is wrong, it should be 8 characters long and without spaces.",newname);
MessageBox(tmpstr,"Warning",MB_ICONEXCLAMATION|MB_OK);
goto restart;
}
if(newname!=itemname && file_exists(filepath) )
{
res=MessageBox("Do you want to overwrite "+newname+"?","Warning",MB_ICONQUESTION|MB_YESNO);
if(res==IDNO) goto restart;
}
itemname=newname;
res=0;
if(filepath.Right(4)==".bcs")
{
chdir(bgfolder);
tmpstr=bgfolder+weidudecompiled;
assure_dir_exists(tmpstr);
if(!dir_exists(tmpstr) )
{
res=-3;
}
else tmpstr+="\\"+newname+".baf";
}
if(!res)
{
fhandle=creat(tmpstr,S_IWRITE);
if(fhandle>0)
{
//this is the intended way of extracting info from a richedit control
res=writestream(fhandle,m_text_control);
close(fhandle);
}
else res=-2;
if(m_bcs)
{
if(!res) res=compile(tmpstr);
}
}
switch(res)
{
case -3:
newname.Format("Cannot create output path:%s",tmpstr);
MessageBox(tmpstr,"Script editor",MB_ICONSTOP|MB_OK);
break;
case -2:
newname.Format("Cannot create file:%s",tmpstr);
MessageBox(tmpstr,"Script editor",MB_ICONSTOP|MB_OK);
break;
case 0:
if(m_bcs) MessageBox("Script saved and compiled...","Script editor",MB_ICONINFORMATION|MB_OK);
else MessageBox("Script saved...","Script editor",MB_ICONINFORMATION|MB_OK);
break;
default:
MessageBox("Error during save.","Script editor",MB_ICONSTOP|MB_OK);
}
}
UpdateData(UD_DISPLAY);
the_script.m_changed=false;
CheckScript(0);
RefreshDialog();
}
void CScriptEdit::NewScript()
{
itemname="new script";
if(m_errors) delete [] m_errors;
m_errors=NULL;
m_count=0;
m_firsterror=0;
m_bcs=1;
the_script.m_changed=false;
}
void CScriptEdit::OnNew()
{
NewScript();
m_text_control.SetWindowText("");
UpdateData(UD_DISPLAY);
CheckScript(FORCED_CHECK|WHOLE_CHECK);
RefreshDialog();
}
void format_line(CString &line, int indent)
{
if(line.IsEmpty()) return;
//line.SetAt(0,(char) toupper(line.GetAt(0)));
line=CString(TCHAR(' '),indent)+line;
}
int match_string(CString &line, CString token, int indent, int error)
{
int maxi, maxj;
int i,j;
maxi=line.GetLength();
if(!maxi) return 0;
maxj=token.GetLength();
i=0;
j=0;
while(j<maxj)
{
if(token[j]=='%')
{
switch(token[++j])
{
case 'd': //number
if(i>=maxi) return error;
while((i<maxi) && (line[i]>='0') && (line[i]<='9') ) i++;
break;
}
j++;
continue;
}
if(i>=maxi) return error; //end of line without full parse
if(toupper(line[i])!=token[j]) return error; //difference
i++;
j++;
}
if(i<maxi) return error; //excess on line
line.MakeUpper();
line=CString(TCHAR(' '),indent)+line;
return 0;
}
void CScriptEdit::RefreshMenu()
{
CMenu *pMenu;
pMenu=GetMenu();
if(pMenu)
{
pMenu->CheckMenuItem(ID_OPTIONS_AUTOCHECK, MF_SET(!(editflg&NOCHECK)));
pMenu->CheckMenuItem(ID_OPTIONS_LARGEINDENT, MF_SET(!(editflg&INDENT)));
pMenu->CheckMenuItem(ID_OPTIONS_USEINTERNALDECOMPILER, MF_SET(!(editflg&INTERNALDECOMP)));
pMenu->CheckMenuItem(ID_OPTIONS_USEINTERNALCOMPILER, MF_SET(!(editflg&INTERNALCOMPILER)));
pMenu->CheckMenuItem(ID_OPTIONS_LOGGING, MF_SET(weiduflg&WEI_LOGGING));
}
}
void CScriptEdit::RefreshDialog()
{
CString tmpstr;
CButton *cb;
int topline;
int i;
if(m_firsterror>=0)
{
topline=m_text_control.GetFirstVisibleLine();
for(i=0;i<LINES;i++)
{
cb=(CButton *) GetDlgItem(IDC_U1+i);
if(topline+i>=m_count)
{
cb->ShowWindow(false);
}
else
{
cb->ShowWindow(true);
tmpstr.Format("%d",topline+i+1);
cb->SetWindowText(tmpstr);
if(m_errors[topline+i]&0xffff) cb->SetCheck(false);
else cb->SetCheck(true);
}
}
}
else
{
topline=m_text_control.GetFirstVisibleLine();
for(i=0;i<LINES;i++)
{
cb=(CButton *) GetDlgItem(IDC_U1+i);
if(topline+i>=m_count)
{
cb->ShowWindow(false);
}
else
{
cb->ShowWindow(true);
tmpstr.Format("%d",topline+i+1);
cb->SetWindowText(tmpstr);
cb->SetCheck(true);
}
}
}
}
CString CScriptEdit::GetLine(int idx)
{
int len;
CString line;
len=m_text_control.LineLength(m_text_control.LineIndex(idx));
m_text_control.GetLine(idx,line.GetBufferSetLength(len+4),len);
line.ReleaseBuffer(len);
return line;
}
void CScriptEdit::CheckScript(int messages)
{
CString tmpstr;
CString linestr;
CString line;
trigger trigger;
action action;
int i;
int triggeroraction;
int startline, endline;
int num_or;
bool tr_override;
int commentpos;
if(m_bcs)
{
tmpstr.Format("Edit script: %s%s",itemname, the_script.m_changed?"*":"");
}
else
{
tmpstr.Format("Edit script source: %s%s",itemname, the_script.m_changed?"*":"");
}
SetWindowText(tmpstr);
if(!(messages&FORCED_CHECK) )
{
if(editflg&NOCHECK) return;
if(!m_text_control.GetModify()) return;
}
m_text_control.SetModify(false);
m_count=m_text_control.GetLineCount();
if(m_errors) delete [] m_errors;
triggeroraction=TA_IF;
if(m_count<0)
{
m_errors=new int[1];
m_errors[0]=m_count;
m_firsterror=0;
endline=m_count=1;
}
else
{
m_firsterror=-1;
m_errors=new int[m_count];
if(messages&WHOLE_CHECK)
{
startline=0;
endline=m_count;
}
else
{
startline=m_text_control.GetFirstVisibleLine();
endline=min(m_count,startline+LINES);
while(startline>0)
{
if(GetLine(startline)=="IF") break;
startline--;
}
}
num_or=0;
tr_override=false;
for(i=startline;i<endline;i++)
{
line=GetLine(i);
commentpos=line.Find("//");
if(commentpos>=0)
{
tmpstr=line.Left(commentpos);
}
else tmpstr=line;
tmpstr.TrimRight();
tmpstr.TrimLeft();
if(!tmpstr.IsEmpty())
{
switch(triggeroraction)
{
case TA_ACTION:
m_errors[i]=match_string(tmpstr, "END",0,-130);
if(!m_errors[i])
{
triggeroraction=TA_IF;
break;
}
m_errors[i]=match_string(tmpstr, "RESPONSE #%d",m_indent,-110);
if(!m_errors[i])
{
triggeroraction=TA_ACTION;
break;
}
m_errors[i]=compile_action(tmpstr, action, 0);
format_line(tmpstr,m_indent*2);
break;
case TA_TRIGGER:
m_errors[i]=match_string(tmpstr, "THEN",0,-120);
if(!m_errors[i])
{
if(num_or)
{
m_errors[i]=CE_INCOMPLETE_OR;
}
if(tr_override)
{
m_errors[i]=CE_INCOMPLETE_TROVERRIDE;
}
triggeroraction=TA_RESPONSE;
break;
}
m_errors[i]=compile_trigger(tmpstr, trigger);
format_line(tmpstr, num_or?m_indent+2:m_indent);
if((trigger.opcode&0x1fff)==TR_OR)
{
if(num_or) m_errors[i]=CE_INCOMPLETE_OR;
num_or=trigger.bytes[0];
}
else
{
if(num_or) num_or--;
}
if((trigger.opcode==TR_OVERRIDE) && is_this_bgee())
{
if(tr_override) m_errors[i]=CE_INCOMPLETE_TROVERRIDE;
else tr_override=true;
}
else
{
//override object???
tr_override=false;
}
break;
case TA_IF:
m_errors[i]=match_string(tmpstr, "IF",0,-100);
triggeroraction=TA_TRIGGER;
num_or=0;
tr_override=false;
break;
case TA_RESPONSE:
m_errors[i]=match_string(tmpstr, "RESPONSE #%d",m_indent,-110);
triggeroraction=TA_ACTION;
break;
}
if(m_errors[i])
{
if(m_firsterror==-1) m_firsterror=i;
}
}
else m_errors[i]=0;
if(commentpos>=0) line=tmpstr+line.Mid(commentpos);
else line=tmpstr;
}
}
if(messages&SHOW_MESSAGES)
{
if(m_firsterror>=0)
{
for(i=m_firsterror;i<endline;i++)
{
if(m_errors[i])
{
tmpstr.Format("Error in script line #%d: %s",i+1,get_compile_error(m_errors[i]) );
if(MessageBox(tmpstr,"Dialog editor",MB_ICONEXCLAMATION|MB_OKCANCEL)==IDCANCEL)
{
m_firsterror=i; //positioning to the cancelled line
break;
}
}
}
}
}
}
void CScriptEdit::OnKillfocusText()
{
UpdateData(UD_RETRIEVE);
}
void CScriptEdit::OnSetfocusText()
{
if(!m_firsterror) CheckScript(0);
RefreshDialog();
}
void CScriptEdit::OnCheck()
{
int startchar, linelength;
CheckScript(WHOLE_CHECK|FORCED_CHECK|SHOW_MESSAGES);
if(m_firsterror>=0)
{
m_text_control.SetFocus();
startchar=m_text_control.LineIndex(m_firsterror);
if(!(editflg&SELECTION))
{
linelength=m_text_control.LineLength(startchar);
m_text_control.SetSel(startchar, startchar+linelength);
}
else
{
m_text_control.SetSel(startchar, startchar);
}
}
else MessageBox("The script appears correct.","Script editor",MB_OK);
}
void CScriptEdit::OnChangeText()
{
CWnd *cwnd;