summaryrefslogtreecommitdiff
path: root/plugins/Actman/iac_jump.pas
blob: 53268ea5e3d56971de33ea7da1f60a59f421ec65 (plain)
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
unit iac_jump;

interface

implementation

uses
  windows, messages, commctrl,
  m_api, dbsettings,
  global,iac_global, editwrapper, dlgshare,
  common, mirutils, wrapper;

{$include i_cnst_jump.inc}
{$resource iac_jump.res}

const // condition code
  aeGT  = 1;
  aeLT  = 2;
  aeEQ  = 3;
  aeXR  = 4;
  aeND  = 5;

  aeEMP = 1;
  aeEQU = 2;
  aeCON = 3;
  aeSTR = 4;
  aeEND = 5;
const
  opt_value     = 'value';
  opt_condition = 'condition';
  opt_label     = 'label';
const
  ioIf     = 'IF';
  ioCond   = 'cond';
  ioNop    = 'nop';
  ioNot    = 'not';
  ioValue  = 'value';
  ioOper   = 'oper';
  ioAction = 'action';
  ioLabel  = 'label';
  ioBreak  = 'break';
  ioJump   = 'jump';
  ioPost   = 'POST';
  ioCase   = 'case';
  ioBack   = 'back';
const
  ACF_NOP   = $00000001;
  ACF_MATH  = $00000002;
  ACF_NOT   = $00000004;
  ACF_CASE  = $00000008;
  ACF_BREAK = $00000010;
  ACF_BACK  = $00000020;
  ACF_VALUE = $00000100;

const // V2
  ADV_ACT_BREAK = 1;
  ADV_COND_NOT  = $80;
  ADV_COND_GT   = 1;
  ADV_COND_LT   = 2;
  ADV_COND_EQ   = 3;

type
  tJumpAction = class(tBaseAction)
  private
    value    :pWideChar;
    actlabel :pWideChar;
    condition:integer;
  public
    constructor Create(uid:dword);
    destructor Destroy; override;

    function  DoAction(var WorkData:tWorkData):LRESULT; override;
    procedure Save(node:pointer;fmt:integer); override;
    procedure Load(node:pointer;fmt:integer); override;
  end;

//----- Support functions -----

//----- Object realization -----

constructor tJumpAction.Create(uid:dword);
begin
  inherited Create(uid);

  condition:=0;
  value    :=nil;
  actlabel :=nil;
end;

destructor tJumpAction.Destroy;
begin
  mFreeMem(value);
  mFreeMem(actlabel);

  inherited Destroy;
end;

function tJumpAction.DoAction(var WorkData:tWorkData):LRESULT;
var
  tmpint:int64;
  vnum:int_ptr;
  vstr,vlast:pWideChar;
  buf:array [0..31] of WideChar;
  res:boolean;
  vlr,vval:pWideChar;
  tmp:pWideChar;
  delta:integer;
  lptr:pBaseAction;
begin
  result:=0;

  // Condition
  if (flags and ACF_NOP)=0 then
  begin
    // preparing value
    if WorkData.ResultType=rtInt then
      vlast:=IntToStr(buf,WorkData.LastResult)
    else
      vlast:=pWideChar(WorkData.LastResult);

    if (flags and ACF_VALUE)<>0 then
    begin
      vstr:=ParseVarString(value,WorkData.Parameter,vlast);
    end
    else
      vstr:=value;

    res:=false;
    // now comparing
    if (flags and ACF_MATH)<>0 then
    begin
      vnum:=int_ptr(GetResultNumber(WorkData));
      tmpint:=NumToInt(vstr);
      case condition of
        aeGT: res:=vnum>tmpint;
        aeLT: res:=vnum<tmpint;
        aeEQ: res:=vnum=tmpint;
        aeXR: res:=(vnum xor tmpint)<>0;
        aeND: res:=(vnum and tmpint)<>0;
      end;

    end
    else
    begin
      if (condition=aeEMP) and ((vlast=nil) or (vlast[0]=#0)) then
        res:=true
      else
      begin
        if (flags and ACF_CASE)=0 then
        begin
          StrDupW(vlr,vlast);
          StrDupW(vval,vstr);
          CharUpperW(vlr);
          CharUpperW(vval);
        end
        else
        begin
          vlr :=vlast;
          vval:=vstr;
        end;

        if (flags and ACF_BACK)<>0 then
        begin
          tmp:=vlr;
          vlr:=vval;
          vval:=tmp;
        end;

        case condition of
          aeEQU: res:=StrCmpW(vlr,vval)=0;
          aeCON: res:=StrPosW(vlr,vval)<>nil;
          aeSTR: res:=StrPosW(vlr,vval)=vlr;
          aeEND: begin
            delta:=StrLenW(vval)-StrLenW(vlr);
            if delta>=0 then
              res:=StrCmpW(vlr,vval+delta)=0;
          end;
        end;

        if (vlr<>vlast) and (vlr<>vstr) then
        begin
          mFreeMem(vlr);
          mFreeMem(vval);
        end;
      end;
    end;

    if (flags and ACF_NOT)<>0 then
      res:=not res;

    if (flags and ACF_VALUE)<>0 then
      mFreeMem(vstr);
  end
  else
    res:=true;

  // Operation
  if res then
    if (flags and ACF_BREAK)<>0 then
      result:=-1
    else
    begin
      lptr:=pBaseAction(WorkData.ActionList);
      for delta:=0 to WorkData.ActionCount-1 do
      begin
        if StrCmpW(actlabel,lptr^.ActionDescr)=0 then
        begin
          result:=delta+1;
          break;
        end;
        inc(lptr);
      end;
    end;
end;

procedure tJumpAction.Load(node:pointer;fmt:integer);
var
  section: array [0..127] of AnsiChar;
  buf:array [0..31] of WideChar;
  pc:pAnsiChar;
  tmp:pWideChar;
  oper,cond:byte;
begin
  inherited Load(node,fmt);
  case fmt of
    0: begin
      pc:=StrCopyE(section,pAnsiChar(node));
      if (flags and ACF_NOP)=0 then
      begin
        StrCopy(pc,opt_value    ); value    :=DBReadUnicode(0,DBBranch,section,nil);
        StrCopy(pc,opt_condition); condition:=DBReadByte   (0,DBBranch,section,0);
      end;
      if (flags and ACF_BREAK)=0 then
      begin
        StrCopy(pc,opt_label); actlabel:=DBReadUnicode(0,DBBranch,section,nil);
      end;
    end;

    100..199: begin
      flags:=flags and not ACF_MASK;
      pc:=StrCopyE(section,pAnsiChar(node));
      if fmt<>101 then // condition
      begin
        StrCopy(pc,opt_condition); cond:=DBReadByte(0,DBBranch,section);
        if (cond and $0F)=0 then
          flags:=flags or ACF_NOP
        else
        begin
          if (cond and ADV_COND_NOT)<>0 then flags:=flags or ACF_NOT;
          flags:=flags or ACF_MATH;
          StrCopy(pc,opt_value); StrDupW(value,IntToStr(buf,DBReadDWord(0,DBBranch,section)));

          case cond and $0F of
            ADV_COND_GT: condition:=aeGT;
            ADV_COND_LT: condition:=aeLT;
            ADV_COND_EQ: condition:=aeEQ;
          end;
        end;
      end
      else // skip condition, jump
      begin
        flags:=flags or ACF_NOP
      end;

      if fmt>101 then // inverse condition, jump to label
      begin
        flags:=flags xor ACF_NOT;
        buf[0]:='$'; buf[1]:='$';
        IntToStr(PWideChar(@buf[2]),fmt-102);
        StrDupW(actlabel,buf);
      end
      else
      begin
        StrCopy(pc,'action'); oper:=DBReadByte(0,DBBranch,section) and $0F;
        if (oper and ADV_ACT_BREAK)<>0 then
          flags:= flags or ACF_BREAK
        else
        begin
          StrCopy(pc,'operval'); actlabel:=DBReadUnicode(0,DBBranch,section);
        end;
      end;
    end;
  end;
end;

procedure tJumpAction.Save(node:pointer;fmt:integer);
var
  section: array [0..127] of AnsiChar;
  pc:pAnsiChar;
begin
  inherited Save(node,fmt);
  case fmt of
    0: begin
      pc:=StrCopyE(section,pAnsiChar(node));
      if (flags and ACF_NOP)=0 then
      begin
        StrCopy(pc,opt_value    ); DBWriteUnicode(0,DBBranch,section,value);
        StrCopy(pc,opt_condition); DBWriteByte   (0,DBBranch,section,condition);
      end;
      if (flags and ACF_BREAK)=0 then
      begin
        StrCopy(pc,opt_label); DBWriteUnicode(0,DBBranch,section,actlabel);
      end;
    end;
  end;
end;

//----- Dialog realization -----

procedure FillMathList(Dialog:HWND);
var
  wnd:HWND;
begin
  wnd:=GetDlgItem(Dialog,IDC_JMP_MATH);

  SendMessage(wnd,CB_RESETCONTENT,0,0);

  InsertString(wnd,cardinal(aeGT),'> greater');
  InsertString(wnd,cardinal(aeLT),'< lesser');
  InsertString(wnd,cardinal(aeEQ),'= equ');
  InsertString(wnd,cardinal(aeXR),'^ xor');
  InsertString(wnd,cardinal(aeND),'& and');

  SendMessage(wnd,CB_SETCURSEL,0,0);
end;

procedure FillTextList(Dialog:HWND);
var
  wnd:HWND;
begin
  wnd:=GetDlgItem(Dialog,IDC_JMP_TEXT);

  SendMessage(wnd,CB_RESETCONTENT,0,0);

  InsertString(wnd,cardinal(aeEMP),'empty');
  InsertString(wnd,cardinal(aeEQU),'= equ');
  InsertString(wnd,cardinal(aeCON),'contains');
  InsertString(wnd,cardinal(aeSTR),'starts with');
  InsertString(wnd,cardinal(aeEND),'ends with');

  SendMessage(wnd,CB_SETCURSEL,0,0);
end;

procedure FillActionList(Dialog:HWND);
var
  list,wnd:HWND;
  i,act:integer;
  arr:array [0..127] of WideChar;
  li:LV_ITEMW;
begin
  wnd:=GetDlgItem(Dialog,IDC_JMP_ACTLIST);

  SendMessage(wnd,CB_RESETCONTENT,0,0);

  list:=ActionListWindow;
  act:=SendMessageW(list,LVM_GETITEMCOUNT,0,0);
  i:=0;
  li.mask      :=LVIF_TEXT;
  li.iSubItem  :=0;
  li.pszText   :=@arr;
  li.cchTextMax:=SizeOf(arr) div SizeOf(WideChar);
  while i<act do
  begin
    li.iItem:=i;
    SendMessageW(list,LVM_GETITEMW,0,lparam(@li));
    SendMessageW(wnd,CB_ADDSTRING,0,lparam(PWideChar(@arr)));
    inc(i);
  end;
  SendMessage(wnd,CB_SETCURSEL,0,0);
end;

procedure SetFields(Dialog:HWND);
var
  bmath,btext:boolean;
begin
  if IsDlgButtonChecked(Dialog,IDC_FLAG_NOP)<>BST_UNCHECKED then
  begin
    bmath:=false;
    btext:=false;
  end
  else if IsDlgButtonChecked(Dialog,IDC_FLAG_MATH)<>BST_UNCHECKED then
  begin
    bmath:=true;
    btext:=false;
  end
  else
  begin
    bmath:=false;
    btext:=true;
  end;
  EnableWindow(GetDlgItem(Dialog,IDC_JMP_MATH ),bmath);
  EnableWindow(GetDlgItem(Dialog,IDC_JMP_TEXT ),btext);
  EnableWindow(GetDlgItem(Dialog,IDC_FLAG_CASE),btext);
  EnableWindow(GetDlgItem(Dialog,IDC_FLAG_BACK),btext);
  EnableWindow(GetDlgItem(Dialog,IDC_FLAG_NOT ),bmath or btext);
  EnableEditField(GetDlgItem(Dialog,IDC_JMP_VALUE),bmath or btext);
  if btext then
  begin
    btext:=CB_GetData(GetDlgItem(Dialog,IDC_JMP_TEXT))<>aeEMP;
    EnableWindow   (GetDlgItem(Dialog,IDC_FLAG_CASE),btext);
    EnableWindow   (GetDlgItem(Dialog,IDC_FLAG_BACK),btext);
    EnableEditField(GetDlgItem(Dialog,IDC_JMP_VALUE),btext);
  end;
end;

procedure ClearFields(Dialog:HWND);
begin
  EnableWindow(GetDlgItem(Dialog,IDC_JMP_MATH ),true);
  EnableWindow(GetDlgItem(Dialog,IDC_JMP_TEXT ),true);
  EnableWindow(GetDlgItem(Dialog,IDC_FLAG_NOT ),true);
  EnableWindow(GetDlgItem(Dialog,IDC_FLAG_CASE),true);
  EnableEditField(GetDlgItem(Dialog,IDC_JMP_VALUE),true);
  SetDlgItemTextW(Dialog,IDC_JMP_VALUE,nil);
  SetEditFlags(Dialog,IDC_JMP_VALUE,EF_ALL,0);

  CheckDlgButton(Dialog,IDC_FLAG_NOP  ,BST_UNCHECKED);
  CheckDlgButton(Dialog,IDC_FLAG_MATH ,BST_UNCHECKED);
  CheckDlgButton(Dialog,IDC_FLAG_TEXT ,BST_UNCHECKED);
  CheckDlgButton(Dialog,IDC_FLAG_NOT  ,BST_UNCHECKED);
  CheckDlgButton(Dialog,IDC_FLAG_CASE ,BST_UNCHECKED);
  CheckDlgButton(Dialog,IDC_FLAG_BACK ,BST_UNCHECKED);
  CheckDlgButton(Dialog,IDC_FLAG_BREAK,BST_UNCHECKED);
  CheckDlgButton(Dialog,IDC_FLAG_JUMP ,BST_UNCHECKED);
end;

function DlgProc(Dialog:HWND;hMessage:uint;wParam:WPARAM;lParam:LPARAM):LRESULT; stdcall;
const
  NoProcess:boolean=true;
var
  bb:boolean;
  wnd:HWND;
  tmp:dword;
begin
  result:=0;

  case hMessage of
    WM_INITDIALOG: begin
      FillMathList(Dialog);
      FillTextList(Dialog);

      TranslateDialogDefault(Dialog);

      MakeEditField(Dialog,IDC_JMP_VALUE);
    end;

    WM_ACT_SETVALUE: begin
      NoProcess:=true;
      ClearFields(Dialog);
      with tJumpAction(lParam) do
      begin
        FillActionList(Dialog);
//        SendDlgItemMessage(Dialog,IDC_JMP_ACTLIST,CB_SETCURSEL,0,0);
        // Condition
        if (flags and ACF_NOP)<>0 then
        begin
          CheckDlgButton(Dialog,IDC_FLAG_NOP,BST_CHECKED);
        end
        else
        begin
          if (flags and ACF_NOT)<>0 then
            CheckDlgButton(Dialog,IDC_FLAG_NOT,BST_CHECKED);
          SetDlgItemTextW(Dialog,IDC_JMP_VALUE,value);
          SetEditFlags(Dialog,IDC_JMP_VALUE,EF_SCRIPT,ord((flags and ACF_VALUE)<>0));

          // Math
          if (flags and ACF_MATH)<>0 then
          begin
            CheckDlgButton(Dialog,IDC_FLAG_MATH,BST_CHECKED);
            CB_SelectData(Dialog,IDC_JMP_MATH,condition);
          end
          // Text
          else
          begin
            if (flags and ACF_CASE)<>0 then
              CheckDlgButton(Dialog,IDC_FLAG_CASE,BST_CHECKED);
            if (flags and ACF_BACK)<>0 then
              CheckDlgButton(Dialog,IDC_FLAG_BACK,BST_CHECKED);
            CheckDlgButton(Dialog,IDC_FLAG_TEXT,BST_CHECKED);
            CB_SelectData(Dialog,IDC_JMP_TEXT,condition);
          end;
        end;
        SetFields(Dialog);

        //Operation
        if (flags and ACF_BREAK)<>0 then
        begin
          CheckDlgButton(Dialog,IDC_FLAG_BREAK,BST_CHECKED);
          EnableWindow(GetDlgItem(Dialog,IDC_JMP_ACTLIST),false);
        end
        else
        begin
          CheckDlgButton(Dialog,IDC_FLAG_JUMP,BST_CHECKED);
          EnableWindow(GetDlgItem(Dialog,IDC_JMP_ACTLIST),true);
          SendDlgItemMessageW(Dialog,IDC_JMP_ACTLIST,CB_SELECTSTRING,
              twparam(-1),tlparam(actlabel));
        end;

      end;
      NoProcess:=false;
    end;

    WM_ACT_RESET: begin
      NoProcess:=true;
      ClearFields(Dialog);

      CheckDlgButton(Dialog,IDC_FLAG_BREAK,BST_CHECKED);
      CheckDlgButton(Dialog,IDC_FLAG_NOP  ,BST_CHECKED);
      SetFields(Dialog);
      CB_SelectData(GetDlgItem(Dialog,IDC_JMP_MATH),aeEQ);
      CB_SelectData(GetDlgItem(Dialog,IDC_JMP_TEXT),aeEQU);
      EnableWindow(GetDlgItem(Dialog,IDC_JMP_ACTLIST),false);
      NoProcess:=false;
    end;

    WM_ACT_SAVE: begin
      with tJumpAction(lParam) do
      begin
        // Condition
        if IsDlgButtonChecked(Dialog,IDC_FLAG_NOP)<>BST_UNCHECKED then
          flags:=flags or ACF_NOP
        else
        begin
          if IsDlgButtonChecked(Dialog,IDC_FLAG_NOT)<>BST_UNCHECKED then
            flags:=flags or ACF_NOT;

          value:=GetDlgText(Dialog,IDC_JMP_VALUE);
          if (GetEditFlags(Dialog,IDC_JMP_VALUE) and EF_SCRIPT)<>0 then
            flags:=flags or ACF_VALUE;

          // math
          if IsDlgButtonChecked(Dialog,IDC_FLAG_MATH)<>BST_UNCHECKED then
          begin
            flags:=flags or ACF_MATH;
            condition:=CB_GetData(GetDlgItem(Dialog,IDC_JMP_MATH));
          end
          // text
          else
          begin
            condition:=CB_GetData(GetDlgItem(Dialog,IDC_JMP_TEXT));
            if condition<>aeEMP then
            begin
              if IsDlgButtonChecked(Dialog,IDC_FLAG_CASE)<>BST_UNCHECKED then
                flags:=flags or ACF_CASE;

              if IsDlgButtonChecked(Dialog,IDC_FLAG_BACK)<>BST_UNCHECKED then
                flags:=flags or ACF_BACK;
            end;
          end;
        end;

        // Operation
        if IsDlgButtonChecked(Dialog,IDC_FLAG_BREAK)<>BST_UNCHECKED then
          flags:=flags or ACF_BREAK
        else
        begin
          actlabel:=GetDlgText(Dialog,IDC_JMP_ACTLIST);
        end;

      end;
    end;

    WM_ACT_LISTCHANGE: begin
      if wParam=2 then
      begin
        wnd:=GetDlgItem(Dialog,IDC_JMP_ACTLIST);
        tmp:=CB_GetData(wnd);
        FillActionList(Dialog);
        CB_SelectData(wnd,tmp);
      end;
    end;

    WM_COMMAND: begin
      case wParam shr 16 of
        EN_CHANGE: if not NoProcess then
            SendMessage(GetParent(GetParent(Dialog)),PSM_CHANGED,0,0);

        CBN_SELCHANGE:  begin
          case loword(wParam) of
            IDC_JMP_TEXT: begin
              bb:=CB_GetData(lParam)<>aeEMP;
              EnableWindow   (GetDlgItem(Dialog,IDC_FLAG_CASE),bb);
              EnableWindow   (GetDlgItem(Dialog,IDC_FLAG_BACK),bb);
              EnableEditField(GetDlgItem(Dialog,IDC_JMP_VALUE),bb);
            end;
          end;
          SendMessage(GetParent(GetParent(Dialog)),PSM_CHANGED,0,0);
        end;

        BN_CLICKED: begin
          case loword(wParam) of
            IDC_FLAG_NOP,
            IDC_FLAG_MATH,
            IDC_FLAG_TEXT: SetFields(Dialog);
            IDC_FLAG_BREAK: begin
              EnableWindow(GetDlgItem(Dialog,IDC_JMP_ACTLIST),false);
            end;
            IDC_FLAG_JUMP: begin
              EnableWindow(GetDlgItem(Dialog,IDC_JMP_ACTLIST),true);
            end;
          end;
          SendMessage(GetParent(GetParent(Dialog)),PSM_CHANGED,0,0);
        end;
      end;
    end;

    WM_HELP: begin
      result:=1;
    end;

  end;
end;

//----- Export/interface functions -----

var
  vc:tActModule;

function CreateAction:tBaseAction;
begin
  result:=tJumpAction.Create(vc.Hash);
end;

function CreateDialog(parent:HWND):HWND;
begin
  result:=CreateDialogW(hInstance,'IDD_ACTJUMP',parent,@DlgProc);
end;

procedure Init;
begin
  vc.Next    :=ModuleLink;

  vc.Name    :='Jump';
  vc.Dialog  :=@CreateDialog;
  vc.Create  :=@CreateAction;
  vc.Icon    :='IDI_JUMP';

  ModuleLink :=@vc;
end;

begin
  Init;
end.