[80baa6]: ScriptEdit.cpp Maximize Restore History

Download this file

ScriptEdit.cpp    1269 lines (1174 with data), 30.0 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
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
// 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"
#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
#define TA_ACTION 0
#define TA_TRIGGER 1
#define TA_IF 2
#define TA_RESPONSE 3
#define TA_THEN 4
#define TA_ENDIF 5
//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_AREA1: case SPT_AREA2: add="qualifier"; break;
case SPT_VAR1: case SPT_VAR2: 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_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_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_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)
//}}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);
}
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;
int res;
res=0;
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");
mkdir(outpath);
if(!dir_exists(outpath))
{
tmpstr.Format("%s cannot be created as output path.",outpath);
MessageBox(tmpstr,"Dialog editor",MB_OK|MB_ICONSTOP);
return -1;
}
syscommand=AssembleWeiduCommandLine(filepath,outpath); //import (compile)
res=RunWeidu(syscommand);
((CChitemDlg *) AfxGetMainWnd())->rescan_dialog(true);
((CChitemDlg *) AfxGetMainWnd())->scan_override();
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;
res=0;
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);
mkdir(weidudecompiled);
if(!dir_exists(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);
}
syscommand=AssembleWeiduCommandLine(filepath,weidudecompiled); //export
res=RunWeidu(syscommand);
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);
CheckScript(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 filepath;
CString m_text;
int fhandle;
int res;
char *pos;
res=OFN_FILEMUSTEXIST|OFN_ENABLESIZING|OFN_EXPLORER;
if(readonly) res|=OFN_READONLY;
if(m_bcs)
{
szFilter=szFiltera;
}
else
{
szFilter=szFilterb;
}
CFileDialog m_getfiledlg(TRUE, m_bcs?"bcs":"baf", m_bcs ? makeitemname(".bcs",0): makeitemname(".baf",1), res, szFilter);
if( m_getfiledlg.DoModal() == IDOK )
{
filepath=m_getfiledlg.GetPathName();
if(checkfile(filepath,"SC") )
{
m_bcs=1;
res=decompile(filepath, m_getfiledlg.GetFileTitle()); //decompile first
if(res)
{
MessageBox("Cannot decompile file!","Error",MB_OK);
return;
}
lastopenedoverride=filepath.Left(filepath.ReverseFind('\\'));
}
else
{ //remember the path when opening a script source
m_bcs=0;
lastopenedscript=filepath.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);
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;
CString filepath;
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;
if(m_bcs)
{
szFilter=szFiltera;
tmpstr=bgfolder+"override\\"+itemname;
}
else
{
szFilter=szFilterb;
tmpstr=makeitemname(".baf",1);
}
CFileDialog m_getfiledlg(FALSE, m_bcs?"bcs":"baf", tmpstr, res, szFilter);
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 bad, 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;
mkdir(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);
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;
}
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)));
}
}
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;
int commentpos;
if(m_bcs) SetWindowText("Edit script: "+itemname);
else SetWindowText("Edit script source: "+itemname);
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;
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;
}
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--;
}
break;
case TA_IF:
m_errors[i]=match_string(tmpstr, "IF",0,-100);
triggeroraction=TA_TRIGGER;
num_or=0;
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;
cwnd=GetFocus();
//make sure that the edit window has the input focus, otherwise this could
//cause various unexpected bugs
// if(!cwnd || (m_text_control.GetSafeHwnd()!=cwnd->GetSafeHwnd()) ) return;
if(!cwnd || (cwnd->GetDlgCtrlID()!=IDC_TEXT) ) return;
UpdateData(UD_RETRIEVE);
CheckScript(FORCED_CHECK);
RefreshDialog();
}
void CScriptEdit::PostNcDestroy()
{
if(m_errors) delete [] m_errors;
m_errors=NULL;
CDialog::PostNcDestroy();
}
void CScriptEdit::OnFileLoadsource()
{
m_bcs=0;
OnLoadex();
}
void CScriptEdit::OnMaxtextText()
{
MessageBox("Script is too long, sorry.","Warning",MB_ICONEXCLAMATION|MB_OK);
}
void CScriptEdit::OnSearch()
{
CString tmpstr;
if(!m_searchdlg)
{
m_searchdlg=new CFindReplaceDialog;
if(!m_searchdlg)
{
MessageBox("Insufficient memory","Error",MB_ICONEXCLAMATION|MB_OK);
return;
}
tmpstr=m_text_control.GetSelText();
tmpstr.Replace("\r\n","\n");
m_searchdlg->Create(readonly,tmpstr,NULL, FR_DOWN | FR_MATCHCASE, this);
}
}
long CScriptEdit::OnFindReplace(WPARAM /*wParam*/, LPARAM /*lParam*/)
{
int direction;
int mode; //0 - search, 1 - replace current, 2 - replace all
int match;
CString search;
CString replace;
if(!m_searchdlg) return 0;
if(m_searchdlg->IsTerminating() )
{
m_searchdlg=NULL;
return 0;
}
if(m_infind) return 0;
m_infind=1;
direction=m_searchdlg->SearchDown();
mode=!!m_searchdlg->ReplaceCurrent()+(!!m_searchdlg->ReplaceAll())*2;
//matchcase is cheaper, therefore it is marked 0
match=!m_searchdlg->MatchCase()+(!!m_searchdlg->MatchWholeWord())*2;
search=m_searchdlg->GetFindString();
replace=m_searchdlg->GetReplaceString();
mode=do_search_and_replace(direction,mode,match,search,replace);
m_infind=0;
OnSetfocusText();
return 0;
}
int CScriptEdit::perform_search_and_replace(int idx, int mode, int match, CString search, CString replace)
{
int ch;
int found;
int len;
int tmplen, tmpstart;
CString tmp2;
CString line;
line=GetLine(idx);
ch=m_text_control.LineIndex(idx);
len=m_text_control.LineLength(ch);
found=false;
switch(mode)
{
case SR_SEARCH:
tmp2=line;
if(match&1) // case insensitive trick
{
tmp2.MakeLower();
}
if(match&2) // full word trick
{
tmp2=" "+line+" "; //in this
}
tmplen=tmp2.Find(search);
if(tmplen!=-1)
{
m_text_control.SetSel(ch+tmplen,ch+tmplen+search.GetLength());
found=true;
}
break;
case SR_REPLACE: case SR_ALL:
tmplen=line.GetLength();
if(match&2)
{
line=" "+line+" ";
}
if(match&1)
{
tmp2=line;
tmp2.MakeLower();
tmpstart=tmp2.Find(search);
if(tmpstart==-1) ch=0;
else
{
line.Delete(tmpstart,len);
line.Insert(tmpstart,replace);
found=true;
}
}
else
{
found=line.Replace(search,replace);
}
if(match&2)
{
line=line.Mid(1,tmplen);
}
if(found)
{
m_text_control.SetSel(ch,ch+len);
m_text_control.ReplaceSel(line,true);
found=true;
UpdateData(UD_DISPLAY);
}
break;
}
return found;
}
int CScriptEdit::do_search_and_replace(int direction,int mode,int match,CString search,CString replace)
{
CString what;
CString operation;
int linecount;
int idx, oldidx;
int count;
int found;
idx=m_text_control.LineFromChar(-1);
linecount=m_text_control.GetLineCount();
if(!linecount) return -1;
if(idx<0 || idx>=linecount)
{
idx=0;
}
oldidx=idx;
if(match&1) //we can do this here
{
search.MakeLower();
}
if(match&2) //this too
{
search=" "+search+" ";
}
found=-1;
count=0;
if(direction)
{
direction=1;
what="bottom";
}
else
{
direction=-1;
what="top";
}
switch(mode)
{
case SR_SEARCH: operation="Search"; break; //0
case SR_REPLACE: operation="Search & replace"; break; //1
case SR_ALL: operation="Replace all"; break; //2
}
if(!mode)
{
idx+=direction;
}
while(direction<1? idx : idx<linecount)
{
if(perform_search_and_replace(idx,mode,match,search,replace))
{
found=idx;
count++;
}
if(found==-1)
{//we found nothing
if(mode==1) mode=0; //couldn't find anything in the actual position, we will search from now
}
else
{//we found something, if it is not replace all then we stop here
if(mode!=2) break;
}
idx+=direction;
}
if((found==-1) || (mode==2)) //replace all should go around even if it has found something
{
if(MessageBox(operation+" hit the "+what+" of the list, do you want to continue ?",operation,MB_SYSTEMMODAL|MB_YESNO)==IDNO)
{
return found;
}
if(direction>0)
{
idx=0;
}
else
{
idx=linecount-1;
}
while(idx!=oldidx)
{
if(perform_search_and_replace(idx,mode,match,search,replace))
{
found=idx;
count++;
}
if(found==-1)
{//we found nothing
if(mode==1) mode=0; //couldn't find anything in the actual position, we will search from now
}
else
{//we found something, if it is not replace all then we stop here
if(mode!=2) break;
}
idx+=direction;
}
}
if(found!=-1)
{
if(count>1)
{
operation.Format("Found %d entries.",count);
MessageBox(operation);
}
}
else
{
MessageBox("Couldn't find any entries containing: "+search);
}
return found;
}
void CScriptEdit::OnOptionsAutocheck()
{
editflg^=NOCHECK;
RefreshMenu();
}
void CScriptEdit::OnOptionsLargeindent()
{
editflg^=INDENT;
RefreshMenu();
}
void CScriptEdit::OnEditSelectall()
{
m_text_control.SetSel(0,-1);
}
void CScriptEdit::OnEditUndo()
{
m_text_control.Undo();
}
void CScriptEdit::OnEditCut()
{
m_text_control.Cut();
}
void CScriptEdit::OnEditCopy()
{
m_text_control.Copy();
}
void CScriptEdit::OnEditPaste()
{
m_text_control.Paste();
}
void CScriptEdit::OnEditDelete()
{
m_text_control.Clear();
}
#define EDITMENU 1
void CScriptEdit::OnContextMenu(CWnd* pWnd, CPoint point)
{
if(pWnd->GetDlgCtrlID()==IDC_TEXT)
{
CMenu *menu;
CMenu *popupmenu;
menu=GetMenu();
popupmenu=menu->GetSubMenu(EDITMENU); //this should be better defined
popupmenu->TrackPopupMenu(TPM_CENTERALIGN,point.x,point.y,this);
}
}
void CScriptEdit::OnIDS()
{
CIDSEdit dlg;
CString tmpstr;
dlg.SetReadOnly(true);
tmpstr=itemname;
itemname=m_idsname;
dlg.DoModal();
m_idsname=itemname;
itemname=tmpstr;
}
BOOL CScriptEdit::PreTranslateMessage(MSG* pMsg)
{
m_tooltip.RelayEvent(pMsg);
return CDialog::PreTranslateMessage(pMsg);
}
BOOL CScriptEdit::OnHelpInfo(HELPINFO* /*pHelpInfo*/)
{
CString tmpstr;
CString helpstr;
compiler_data compiler_data;
tmpstr=m_text_control.GetSelText();
tmpstr.MakeLower();
tmpstr.TrimLeft();
tmpstr.TrimRight();
if(action_data.Lookup(tmpstr,compiler_data) )
{
helpstr.Format("%s(%s)",tmpstr,format_parameters(compiler_data));
MessageBox(helpstr,"Script Action",MB_ICONINFORMATION|MB_OK);
return false;
}
if(trigger_data.Lookup(tmpstr,compiler_data) )
{
helpstr.Format("%s(%s)",tmpstr,format_parameters(compiler_data));
MessageBox(helpstr,"Script Trigger",MB_ICONINFORMATION|MB_OK);
return false;
}
return false;
}
void CScriptEdit::OnUpdateText()
{
if(m_text_control.GetModify() ) return; //this will be handled in onchange
OnChangeText(); //this event is called from scroll
}