-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathEventSystem.ahk
1337 lines (1185 loc) · 45 KB
/
EventSystem.ahk
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
/*
Note: CEventSystem, CTrigger, CCondition and CAction need to be defined before any real subevent class.
Possible Problem: newly created subevents may not have local properties. This could be a problem if HasKey is used somewhere.
*/
Class CEventSystem extends CRichObject
{
;These arrays contain all types of the subevents indexed by their type names
static Triggers := RichObject()
static Conditions := RichObject()
static Actions := RichObject()
;Global placeholders can be shared between different events
static GlobalPlaceholders := RichObject()
;EventSchedule (contains copies of the event objects in the Events list) is a list of events that are currently being processed.
static EventSchedule := Array()
;Event schedule ID is assigned to each scheduled event so they can be identified by this ID.
EventScheduleID := 0
Startup()
{
;Create CEvents instance
this.Events := new CEvents()
;Temporary events are not visible in settings GUI and won't be saved. See ControlEvent -> Copy Event for usage example.
this.TemporaryEvents := new CEvents()
;Call startup functions for all subevents
for index, Trigger in this.Triggers
Trigger.Startup()
for index, Condition in this.Conditions
Condition.Startup()
for index, Action in this.Actions
Action.Startup()
;Load main events file. This will create event objects for all stored event configs in Events object.
this.Events.ReadMainEventsFile()
;Make sure the subevents can enabled themselves
for index, Event in this.Events
if(Event.Enabled)
Event.Enable()
;Trigger events with 7plusStart trigger
Trigger := new C7plusStartTrigger()
this.OnTrigger(Trigger)
;If 7plus was started with a commandline parameter through an Explorer Button trigger, process it here.
if(1 = "-id")
{
Trigger := new CExplorerButtonTrigger()
ID = %2%
Trigger.ID := ID
this.OnTrigger(Trigger)
}
;Setup the message handler for receiving triggers from other instances of 7plus (and possibly other programs) and from the Shell extension.
OnMessage(55555, "TriggerFromOtherInstance")
;Make sure that non-elevated processes can send this messages to the elevated 7plus process.
;Keyword: UIPI
DllCall("ChangeWindowMessageFilter", "UInt", 55555, "UInt", 1)
DllCall("ChangeWindowMessageFilter", "UInt", 55556, "UInt", 1)
}
OnExit()
{
for index, Event in this.Events
{
Event.Trigger.OnExit()
for ActionIndex, Action in Event.Actions
Action.OnExit()
}
for index, Event in this.TemporaryEvents
{
Event.Trigger.OnExit()
for ActionIndex, Action in Event.Actions
Action.OnExit()
}
;Save events
this.Events.WriteMainEventsFile()
}
;This function is called when a trigger event is received.
;Trigger is a CTrigger implementation instance that contains information
;that is used by the triggers of events to decide if the event should be triggered.
OnTrigger(Trigger)
{
if(!Trigger.Extends("CTrigger"))
{
Notify("Event System Error!", "Invalid trigger!`nName: " Trigger.Name "`nID: " Trigger.ID, 5, NotifyIcons.Error)
return
}
;Find matching triggers
for index, Event in EventSystem.Events
Event.TriggerThisEvent(Trigger)
for index, Event in EventSystem.TemporaryEvents
Event.TriggerThisEvent(Trigger)
return
}
;This is the main event processing function in which all scheduled events are processed.
;It checks if the conditions of an event are fulfilled and if they are, the actions of the event are performed.
;An event may stay on the EventSchedule stack as long as it needs to when the current CAction.Execute() returns -1.
EventScheduler()
{
Critical, Off
;First, check the conditions of all events in the queue to make sure an event can't influence the result of a condition check of another event.
EventPos := 1
Loop % this.EventSchedule.MaxIndex()
{
Event := this.EventSchedule[EventPos]
;Check conditions
if(Event.Conditions.Success != 1) ;Check if conditions have been evaluated before.
{
Success := Event.CheckConditions(true)
;if the conditions were not fulfilled, remove this event and continue with the next one
if(!Success)
{
this.EventSchedule.Remove(EventPos)
outputdebug % "Conditions of event " event.id " were not fulfilled."
continue
}
else
Event.Conditions.Result := 1 ;Set result so conditions don't have to be checked again when this event has a waiting action.
}
EventPos++
}
;Now the event queue contains only those events which passed the condition check. These can be processed now.
EventPos := 1
Loop % this.EventSchedule.MaxIndex()
{
Event := this.EventSchedule[EventPos]
outputdebug % "Process event ID: " Event.ID " Name: " Event.Name
Loop % Event.Actions.MaxIndex()
{
result := Event.Actions[1].Execute(Event)
if(result = 0) ;Action was cancelled, stop all further actions
{
Event.Actions := Array()
if(Settings.General.ShowExecutingEvents)
Notify("Event Cancelled", "The execution of event" Event.ID ": " Event.Name " was cancelled", 5, NotifyIcons.Info)
break
}
else if(result = -1) ;Action needs more time to finish, check back in next main loop
break
else
Event.Actions.Remove(1)
if(Settings.General.ShowExecutingEvents)
Notify("Event Executed", "The event" Event.ID ": " Event.Name " was executed", 5, NotifyIcons.Info)
}
;if no more actions in this event, consider it processed and remove it from queue
if(!Event.Actions.MaxIndex())
{
this.EventSchedule.Remove(EventPos)
OriginalEvent := this.Events.GetEventWithValue("ID", Event.ID) ;Will return the event from Events or TemporaryEvents
if(Event.DisableAfterUse && OriginalEvent)
OriginalEvent.SetEnabled(false)
if(Event.DeleteAfterUse && OriginalEvent)
this.Events.Delete(OriginalEvent)
outputdebug % "Finished execution of event ID: " event.id " Name:" event.name
continue
}
EventPos++
}
}
;Finds the queued event and subevent in which a subevent has a specific key with a specific value. Filter specifies which types of subevents should be searched.
EventFromSubEventKey(ByRef pEvent, ByRef pSubEvent, Key, Value, Filter = "TCA")
{
for index, Event in this.EventSchedule
{
if(InStr(Filter, "A"))
{
for index2, Action in Event.Actions
{
if(Action[Key] = Value)
{
pEvent := Event
pSubEvent := Action
return Event
}
}
}
if(InStr(Filter, "C"))
{
for index3, Condition in Event.Conditions
{
if(Condition[Key] = Value)
{
pEvent := Event
pSubEvent := Condition
return Event
}
}
}
if(Event.Trigger[Key] = Value && InStr(Filter, "T"))
{
pEvent := Event
pSubEvent := Event.Trigger
return Event
}
}
return 0
}
;Same as EventFromSubEventKey except that it returns only the SubEvent
SubEventFromSubEventKey(Key, Value, Filter = "TCA")
{
this.EventFromSubEventKey("", SubEvent, Key, Value, Filter)
return SubEvent
}
;Finds the event and subevent of a running event of specified type by its gui number
;This function assumes that the gui number is stored as "tmpGUINum" in the subevent of the specified type.
EventFromGUI(ByRef pEvent, ByRef pSubEvent)
{
return this.EventFromSubEventKey(pEvent, pSubEvent, "tmpGUINum", A_GUI)
}
;Same as EventFromGUI except that it returns only the SubEvent
SubEventFromGUI()
{
this.EventFromGUI("", SubEvent)
return SubEvent
}
}
EventScheduler:
SetTimer, EventScheduler, Off
EventSystem.EventScheduler()
SetTimer, EventScheduler, 100
return
Class CEvent extends CRichObject
{
ID := -1
Name := "New event"
Category := "Uncategorized"
Enabled := 1
EventComplexityLevel := 0
OneInstance := 0
DeleteAfterUse := 0
DisableAfterUse := 0
Trigger := new CHotkeyTrigger()
Conditions := new CArray()
Actions := new CArray()
PlaceHolders := Object()
;Enabled state needs to be set through this function, to allow syncing with settings window
SetEnabled(Value)
{
this.Enabled := Value
;if settings are open and updating a regular event, update its counterpart in SettingsWindow.Events
;Note that GetItemWithValue is called instead of GetEventWithValue so that only events from this CEvent instance are retrieved.
if(SettingsActive() && EventSystem.Events.GetItemWithValue("ID", this.ID))
{
SettingsEvent := SettingsWindow.Events.GetItemWithValue("ID", this.ID)
if(IsObject(SettingsEvent))
{
SettingsEvent.Enabled := Value
SettingsWindow.FillEventsList()
}
}
}
;Called when an event is deleted. It forwards this call to its trigger
Delete()
{
this.Trigger.Delete(this)
}
Enable()
{
this.SetEnabled(true)
this.Trigger.Enable(this)
}
Disable()
{
this.SetEnabled(false)
this.Trigger.Disable(this)
}
;Applies a patch to this event, overwriting only some of its values
ApplyPatch(Patch)
{
for key, Value in Patch
if(key != "PatchOnly")
this[key] := value
}
;Main function to expand placeholders. Placeholders are marked by ${key} and by %PATH%
;This function is implemented as a global function so it can be called independently of a subevent
static ExpandPlaceholders := Func("ExpandPlaceholders")
;Evaluates the conditions of this event. Returns -1 if the conditions can't be evaluated completely, 0 if they evaluated to false and 1 if they evuated to true
CheckConditions()
{
result := this.Enabled || this.Trigger.Type = "Timer" ;Check enabled state again here, because it might have changed since it was appended to queue
if(result)
{
;Group conditions separated by OR conditions
ConditionGroups := Array()
ConditionGroup := Array()
for index, Condition in this.Conditions
{
if(!Condition.Is(CORCondition))
ConditionGroup.Insert(Condition)
else
{
if(ConditionGroup.MaxIndex()) ;Skip empty groups
{
ConditionGroups.Insert(ConditionGroup)
ConditionGroup := Array()
}
}
}
if(ConditionGroup.MaxIndex()) ;Skip empty group
ConditionGroups.Insert(ConditionGroup)
for index, ConditionGroup in ConditionGroups
{
for index2, Condition in ConditionGroup
{
result := Round(Clamp(Condition.Evaluate(this), -1, 1))
if(result = -1) ;Not decided yet, check later
break
else if(Condition.Negate) ;Result is 0 or 1 before, now invert it
result := !result
;Condition did not match
if(result = 0)
break
else if(result = 1) ;This condition was fulfilled, remove it from conditions and continue with next condition
continue
}
if(result = 1)
return 1
}
}
return result
}
;Tests if this event matches to a trigger. If it does, this event is schedule for execution.
;To just trigger it without performing trigger matching, leave Trigger Parameter empty.
;It returns the event on the EventSchedule so its state can be examined later.
TriggerThisEvent(Trigger = "")
{
;Order of this if condition is important here, because Event.Trigger.Matches() can disable the event for timers
if(this.Enabled && (!IsObject(Trigger) || (this.Trigger.Type = Trigger.Type && this.Trigger.Matches(Trigger, this)) || (Trigger.Is(CTriggerTrigger) && this.ID = Trigger.TargetID)))
{
;Test if the event is already running and mustn't be run multiple times
if(!this.OneInstance || !EventSystem.EventSchedule.FindKeyWithValue("ID", this.ID))
{
EventSystem.EventSchedule.Insert(Copy := this.DeepCopy())
Copy.EventScheduleID := EventSystem.EventScheduleID
EventSystem.EventScheduleID++ ;Increment event schedule ID for next scheduled event.
}
return Copy
}
}
}
Class CEvents extends CArray
{
Categories := Array()
HighestID := -1
;Creates an event, adds it to this CEvents instance and assigns an ID that is based on the max ID of EventSystem.Events and its settings copy, and increases HighestID count of this CEvents instance
RegisterEvent(Event = "", Enable = 1)
{
;Temporary events use negative ID for find functions
if(this = EventSystem.TemporaryEvents)
HighestID := EventSystem.TemporaryEvents.HighestID - 1
else
HighestID := max(EventSystem.Events.HighestID, SettingsActive() ? SettingsWindow.Events.HighestID : 0) + 1 ;Make sure highest ID is used from both event arrays
;Assign the new highest ID
this.HighestID := HighestID
if(!IsObject(Event))
Event := new CEvent()
Event.ID := HighestID
this.Add(Event)
if(Enable)
Event.SetEnabled(true)
return Event
}
;This function is used internally to add events, to allow syncing with settings window
;It should probably not be called directly, as it doesn't adjust the ID properly. Instead, call CEvents.RegisterEvent()
Add(Event)
{
this.Insert(Event)
if(SettingsActive() && this != SettingsWindow.Events && this != EventSystem.TemporaryEvents) ;Non-temporary and non-settings event that needs to be synced with an open settings gui
{
SettingsWindow.Events.Insert(Event.DeepCopy())
SettingsWindow.RecreateTreeView()
}
}
;This function needs to be used to remove events from the main Events object, to allow syncing with settings window
;It returns true when a category was deleted.
Delete(Event, UpdateGUI = true)
{
if(!IsObject(Event) || ! (index := this.FindKeyWithValue("ID", Event.ID)))
return
Event := this[index]
if(this != EventSystem.TemporaryEvents && Event.ID < 0)
{
EventSystem.TemporaryEvents.Delete(Event, UpdateGUI)
return
}
if(this != SettingsWindow.Events)
{
;Disable the event
Event.Disable()
;Call the delete function of the event so it may do further processing
Event.Delete()
;Remove the event on the settings page too
if(SettingsActive())
{
SettingsWindow.Events.Delete(SettingsWindow.Events.GetEventWithValue("ID", Event.ID), false)
if(UpdateGUI)
SettingsWindow.RecreateTreeView()
}
}
;Actually remove the event from the list
this.Remove(index)
;Adjust HighestID to the highest ID that is present
this.HighestID := -1
for i, Event2 in this
if(Event2.ID > this.HighestID)
this.HighestID := Event2.ID
if(this.FindKeyWithValue("Category", Event.Category))
return false
this.Categories.Remove(this.Categories.IndexOf(Event.Category))
return true
}
;CEvents implements a GetEventWithValue function that should be used instead of CArray.GetItemWithValue so that it can redirect negative IDs to EventSystem.TemporaryEvents
GetEventWithValue(Key, Value)
{
;Call the regular function from CArray on the matching CEvents instance.
if(Key = "ID" && Value < 0)
return EventSystem.TemporaryEvents.GetItemWithValue(Key, Value)
else
return this.GetItemWithValue(Key, Value)
}
;Events overrides CArray.FindKeyWithValue function so that it can redirect negative IDs to EventSystem.TemporaryEvents
FindKeyWithValue(Key, Value)
{
;Call the regular function from CArray on the matching CEvents instance.
if(Key = "ID" && Value < 0 && this != EventSystem.TemporaryEvents)
return EventSystem.TemporaryEvents.FindKeyWithValue(Key, Value)
else
return Base.FindKeyWithValue(Key, Value)
}
ReadMainEventsFile()
{
this.ReadEventsFile(Settings.ConfigPath "\Events.xml")
}
WriteMainEventsFile()
{
this.WriteEventsFile(Settings.ConfigPath "\Events.xml")
}
ReadEventsFile(Path, OverwriteCategory = "", Update = "")
{
global MajorVersion, MinorVersion, BugfixVersion, PatchVersion, XMLMajorVersion, XMLMinorVersion, XMLBugfixVersion
FileRead, xml, %path%
XMLObject := XML_Read(xml)
XMLMajorVersion := XMLObject.MajorVersion
XMLMinorVersion := XMLObject.MinorVersion
XMLBugfixVersion := XMLObject.BugfixVersion
if(CompareVersion(XMLMajorVersion,MajorVersion,XMLMinorVersion,MinorVersion,XMLBugfixVersion,BugfixVersion) > 0)
Msgbox Events file was made with a newer version of 7plus. Compatibility is not guaranteed. Please update, or use at own risk!
if(Update)
Update.Message := Update.Message (XMLObject.Message? "`n" XMLObject.Message : "")
if(path = Settings.ConfigPath "\Events.xml") ;main config file, read patch version
PatchVersion := XMLObject.PatchVersion ? XMLObject.PatchVersion : 0
count := this.MaxIndex()
lowestID := 999999999999
HighestID := this.HighestID
len := max(XMLObject.Events.Event.MaxIndex(), XMLObject.Events.HasKey("Event"))
Loop % len
{
i := A_Index
;Check if end of Events is reached
if(len = 1)
XMLEvent := XMLObject.Events.Event
else
XMLEvent := XMLObject.Events.Event[i]
if(!XMLEvent)
continue
;Create new Event
Event := new CEvent()
;Event ID
if(XMLEvent.HasKey("ID"))
{
Event.ID := XMLEvent.ID
if(Event.ID < lowestID)
lowestID := Event.ID
}
else
Event.Remove("ID")
;Event Name
if(XMLEvent.HasKey("Name"))
Event.Name := XMLEvent.Name
else
Event.Remove("Name")
;Event Description
if(XMLEvent.HasKey("Description"))
Event.Description := XMLEvent.Description
else
Event.Remove("Description")
;Event Category
if(XMLEvent.HasKey("Category") || OverwriteCategory)
{
Event.Category := OverwriteCategory ? OverwriteCategory : XMLEvent.Category ? XMLEvent.Category : "Uncategorized"
this.Categories.InsertUnique(Event.Category)
}
else
Event.Remove("Category")
;Event state
if(XMLEvent.HasKey("Enabled"))
Event.Enabled := XMLEvent.Enabled
else
Event.Remove("Enabled")
;Disable after use
if(XMLEvent.HasKey("DisableAfterUse"))
Event.DisableAfterUse := XMLEvent.DisableAfterUse
else
Event.Remove("DisableAfterUse")
;Delete after use
if(XMLEvent.HasKey("DeleteAfterUse"))
Event.DeleteAfterUse := XMLEvent.DeleteAfterUse
else
Event.Remove("DeleteAfterUse")
;One Instance
if(XMLEvent.HasKey("OneInstance"))
Event.OneInstance := XMLEvent.OneInstance
else
Event.Remove("OneInstance")
;Official event identifier for update processes
if(XMLEvent.HasKey("OfficialEvent"))
Event.OfficialEvent := XMLEvent.OfficialEvent
;Complexity level indicates if an event may be hidden from the user to avoid confusion(1) or if it is always shown(0)
if(XMLEvent.HasKey("EventComplexityLevel"))
Event.EventComplexityLevel := XMLEvent.EventComplexityLevel
else
Event.Remove("EventComplexityLevel")
;Read trigger values
if(IsObject(XMLEvent.Trigger) && XMLEvent.Trigger.HasKey("Type"))
{
if(!EventSystem.Triggers.HasKey(XMLEvent.Trigger.Type))
{
if(!CTrigger.CLegacyTypes.HasKey(XMLEvent.Trigger.Type) || !IsObject(EventSystem.Triggers[CTrigger.CLegacyTypes[xmlEvent.Trigger.Type]]))
{
Notify("Event System Error!", "No known trigger of Type " XMLEvent.Trigger.Type ", skipping event " Event.ID ": " Event.Name, 5, NotifyIcons.Error)
continue
}
else
TriggerTemplate := EventSystem.Triggers[CTrigger.CLegacyTypes[XMLEvent.Trigger.Type]]
}
else
TriggerTemplate := EventSystem.Triggers[XMLEvent.Trigger.Type]
Event.Trigger := new TriggerTemplate()
Event.Trigger.ReadXML(XMLEvent.Trigger)
}
else
Event.Remove("Trigger")
;Read conditions
if(IsObject(XMLEvent.Conditions) && XMLEvent.Conditions.HasKey("Condition"))
{
if(!XMLEvent.Conditions.Condition.Is(CArray)) ;Single condition
XMLEvent.Conditions.Condition := new CArray(XMLEvent.Conditions.Condition)
for index, XMLCondition in XMLEvent.Conditions.Condition
{
;Create new condition
if(!EventSystem.Conditions.HasKey(XMLCondition.Type))
{
if(!CCondition.CLegacyTypes.HasKey(XMLCondition.Type) || !IsObject(EventSystem.Conditions[CCondition.CLegacyTypes[XMLCondition.Type]]))
{
Notify("Event System Error!", "No known Condition of Type " XMLCondition.Type ", skipping event " Event.ID ": " Event.Name, 5, NotifyIcons.Error)
continue
}
else
ConditionTemplate := EventSystem.Conditions[CCondition.CLegacyTypes[XMLCondition.Type]]
}
else
ConditionTemplate := EventSystem.Conditions[XMLCondition.Type]
Condition := new ConditionTemplate()
;Read Negation
Condition.Negate := XMLCondition.Negate
;Read condition values
Condition.ReadXML(XMLCondition)
Event.Conditions.Insert(Condition)
}
}
;Read actions
if(IsObject(XMLEvent.Actions) && XMLEvent.Actions.HasKey("Action"))
{
if(!XMLEvent.Actions.Action.Is(CArray)) ;Single Action
XMLEvent.Actions.Action := new CArray(XMLEvent.Actions.Action)
for index, XMLAction in XMLEvent.Actions.Action
{
;Create new action
if(!EventSystem.Actions.HasKey(XMLAction.Type))
{
if(!CAction.CLegacyTypes.HasKey(XMLAction.Type) || !IsObject(EventSystem.Actions[CAction.CLegacyTypes[XMLAction.Type]]))
{
Notify("Event System Error!", "No known Action of Type " XMLAction.Type ", skipping event " Event.ID ": " Event.Name, 5, NotifyIcons.Error)
continue
}
else
ActionTemplate := EventSystem.Actions[CAction.CLegacyTypes[XMLAction.Type]]
}
else
ActionTemplate := EventSystem.Actions[XMLAction.Type]
Action := new ActionTemplate()
;Read action values
Action.ReadXML(XMLAction)
Event.Actions.Insert(Action)
}
}
if(Event.HasKey("OfficialEvent") && (OldEvent := this.GetEventWithValue("OfficialEvent", Event.OfficialEvent))) ;If an official event already exists, apply this as patch
{
if(!XMLEvent.HasKey("Conditions"))
Event.Remove("Conditions")
if(!XMLEvent.HasKey("Actions"))
Event.Remove("Actions")
Event.Remove("PlaceHolders")
OldEvent.ApplyPatch(Event) ;No update messages are generated here, those are handled manually
}
else if(!Event.PatchOnly)
{
if(Update)
Update.Message := Update.Message "`n- Added Event: " Event.Name
this.Insert(Event)
}
}
;fix IDs from import
;Loop over all new events
pos := count + 1
count := this.MaxIndex()
while(pos <= count)
{
Event := this[pos]
;Make sure Event ID is higher than all previous IDs, but conserve relative differences between IDs
offset := (highestID - lowestID) + 1
Event.ID := Event.ID + offset
;Find highest ID
if(Event.ID > this.HighestID)
this.HighestID := Event.ID
;Now adjust Event ID references
for k,v in Event.Trigger
{
if(strEndsWith(k, "ID") && IsNumeric(v))
Event.Trigger[k] := Event.Trigger[k] + offset
}
for index, Condition in Event.Conditions
for k,v in Condition
if(strEndsWith(k, "ID") && IsNumeric(v))
Condition[k] := Condition[k] + offset
for index, Action in Event.Actions
for k,v in Action
if(strEndsWith(k, "ID") && IsNumeric(v))
Action[k] := Action[k] + offset
pos++
}
if(XMLObject.HasKey("Remove")) ;If Objects are to be removed
{
if(XMLObject.Remove.HasKey("OfficialEvent") && !XMLObject.Remove.OfficialEvent.Is(CArray)) ;Single condition
XMLObject.Remove.OfficialEvent := new CArray(XMLObject.Remove.OfficialEvent)
for index, OfficialEvent in XMLObject.Remove.OfficialEvent
{
if((DeathCandidate := this.GetEventWithValue("OfficialEvent", OfficialEvent)))
{
if(Update) ;Should be true if we are here
Update.Message := Update.Message "`n- Removed Event: " DeathCandidate.Name
this.Delete(DeathCandidate, false)
}
}
}
return this
}
;This function writes this event configuration into the file specified by path.
WriteEventsFile(Path)
{
global MajorVersion, MinorVersion, BugfixVersion, PatchVersion
;Create Events node
xmlObject := Object()
xmlObject.MajorVersion := MajorVersion
xmlObject.MinorVersion := MinorVersion
xmlObject.BugfixVersion := BugfixVersion
;Store Patch version information in the main events file.
if(path = Settings.ConfigPath "\Events.xml")
xmlObject.PatchVersion := PatchVersion
xmlObject.Events := Object()
xmlEvents := Array()
xmlObject.Events.Event := xmlEvents
;Write Events entries
for index, Event in this
{
xmlEvent := Object()
xmlEvents.Insert(xmlEvent)
;Don't save temporary events that are only used during runtime
if(Event.Temporary)
continue
;Write ID
xmlEvent.ID := Event.ID
;Write name
xmlEvent.Name := Event.Name
;Write description
xmlEvent.Description := Event.Description
;Write category
xmlEvent.Category := Event.Category
;Write state
xmlEvent.Enabled := Event.Enabled
;Disable after use
xmlEvent.DisableAfterUse := Event.DisableAfterUse
;Delete after use
xmlEvent.DeleteAfterUse := Event.DeleteAfterUse
;One Instance
xmlEvent.OneInstance := Event.OneInstance
;Complexity level
xmlEvent.EventComplexityLevel := Event.EventComplexityLevel
if(Event.HasKey("Officialevent"))
xmlEvent.OfficialEvent := Event.OfficialEvent
;Uncomment the lines below to save events with an "official" tag that allows to identify them in update processes
if(Settings.General.DebugEnabled && !Event.OfficialEvent && !InStr(Path, A_Temp)) ;Find an unused Event ID to be used as Official Event ID
{
Loop
{
if(this.FindKeyWithValue("OfficialEvent", A_Index))
continue ;Already in use
xmlEvent.OfficialEvent := A_Index ;Not used
Event.OfficialEvent := A_Index
break
}
}
xmlTrigger := Object()
xmlEvent.Trigger := xmlTrigger
xmlTrigger.Type := Event.Trigger.Type
for key, Value in Event.Trigger
{
if(key = "Category" || InStr(key, "tmp") = 1 || InStr(key, "_") = 1)
continue
xmlTrigger[key] := value
}
;Since some triggers might have to do special preprocessing, lets allow them to overwrite the values read above
;Event.Trigger.WriteXML(EventsFileHandle, "/Events/Event[" i "]/Trigger/")
;Write Conditions
xmlEvent.Conditions := Object("Condition", XMLConditions := new CArray())
for index2, Condition in Event.Conditions
{
xmlCondition := Object()
XMLConditions.Insert(xmlCondition)
;Write condition type, since it's stored in base object, and isn't iterated below
xmlCondition.Type := Condition.Type
for key, Value in Condition
{
if(key = "Category" || InStr(key, "tmp") = 1 || InStr(key, "_") = 1)
continue
xmlCondition[key] := value
}
}
xmlEvent.Actions := Object("Action", XMLActions := Array())
for index2, Action in Event.Actions
{
xmlAction := Object()
xmlActions.Insert(xmlAction)
;Write action type, since it's stored in base object, and isn't iterated below
xmlAction.Type := Action.Type
for key, Value in Action
{
if(key = "Category" || InStr(key, "tmp") = 1 || InStr(key, "_") = 1)
continue
xmlAction[key] := value
}
}
}
XML_Save(xmlObject, Path)
return
}
}
;Used to let CEventSystem know about the SubEvent implementation. Needs to be called like this in the class definition:
;static Type := RegisterType(this, "SomeType")
RegisterType(Class, Type)
{
if(Class.Extends("CTrigger"))
CEventSystem.Triggers.Insert(Type, Class)
else if(Class.Extends("CCondition"))
CEventSystem.Conditions.Insert(Type, Class)
else if(Class.Extends("CAction"))
CEventSystem.Actions.Insert(Type, Class)
return Type
}
;Used to let CEventSystem know about the category of this SubEvent implementation. Needs to be called like this in the class definition:
;static Category := RegisterCategory(this, "SomeCategory")
RegisterCategory(Class, Category)
{
if(Class.Extends("CTrigger"))
Categories := CTrigger.Categories
else if(Class.Extends("CCondition"))
Categories := CCondition.Categories
else if(Class.Extends("CAction"))
Categories := CAction.Categories
if(!Categories[Category].Is(CArray))
Categories[Category] := new CArray()
Categories[Category].Insert(Class)
return Category
}
;Called when an external program sends a message to 7plus to make it execute an event.
TriggerFromOtherInstance(wParam, lParam)
{
Critical, On
if(lParam = 0) ;0 = Single trigger from something else than context menus
{
Trigger := new CTriggerTrigger()
Trigger.TargetID := wParam
EventSystem.OnTrigger(Trigger)
}
else if(lParam = 1) ;1 = Trigger from context menu
{
;Read list of selected files written by shell extension
if(FileExist(A_Temp "\7plus\files.txt"))
FileRead, files, % "*t " A_Temp "\7plus\files.txt"
FileDelete, % A_Temp "\7plus\files.txt"
;if it failed (because static context menu is used), try to get it from explorer window
EventSystem.GlobalPlaceholders.Context := files ? files : Navigation.GetSelectedFilepaths()
Trigger := new CTriggerTrigger()
Trigger.TargetID := wParam
EventSystem.OnTrigger(Trigger)
}
Critical, Off
}
Class CSubEvent extends CRichObject
{
;Some functions which are defined outside this class because they are also used elsewhere.
static AddControl := Func("AddControl")
static SubmitControls := Func("SubmitControls")
static SelectFile := Func("SelectFile")
static Browse := Func("Browse")
__New()
{
if(!this.base.HasKey("Type"))
MsgBox % "Type property needs to be set for " __Class
if(!this.base.HasKey("Category"))
MsgBox % "Category property needs to be set for " __Class
}
;Can be implemented by the specific inheriting subevent.
;Not needed usually because defining static properties in the subevent automatically loads them.
ReadXML(XML)
{
for key, value in this.base
if(InStr(key, "__") != 1 && key != "base" && key != "Type" && key != "Category" && !IsObject(this.base[key]))
this.ReadVar(XML, key)
}
;Reads a property from an xml object and stores it in a subevent if the property exists.
ReadVar(xml, key)
{
Assert(IsObject(this) && IsObject(xml) && key != "", "Error reading " key " in " this.Type)
if(xml.HasKey(key))
this[key] := xml[key]
}
;Can be implemented by the specific inheriting subevent.
;Called on the SubEvent class (not the instance!) when the event system is starting up. This Subevent may read special configuration data or similar here.
Startup()
{
}
;To be implemented by the specific inheriting subevent
DisplayString()
{
return this.Type
}
;Can be implemented by the specific inheriting subevent. This implementation only works for text data.
GuiShow(GUI)
{
for key, value in this.base
if(InStr(key, "__") != 1 && key != "base" && key != "Type" && key != "Category" && !IsFunc(this[key]))
this.AddControl(GUI, "Edit", key)
}
;Can be implemented by the specific inheriting subevent
GuiSubmit(GUI)
{
this.SubmitControls(GUI)
}
}
Class CTrigger extends CSubEvent
{
;This is an object containing categories. It is automatically populated by the triggers when they set the "Category" key.
static Categories := RichObject()
;This class is used to convert old subevent types into changed ones during loading
Class CLegacyTypes
{
static ContextMenu := "Context menu"
static DoubleClickDesktop := "Double click on desktop"
static DoubleClickTaskbar := "Double click on taskbar"
static ExplorerButton := "Explorer bar button"
static ExplorerPathChanged := "Explorer path changed"
static ExplorerDoubleClickSpace := "Double click on empty space"
static MenuItem := "Menu item clicked"
static OnMessage := "On window message"
static ScreenCorner := "Screen corner"
static None := "Triggered by an action"
static 7plusStart := "On 7plus start"
static WindowActivated := "Window activated"
static WindowClosed := "Window closed"
static WindowCreated := "Window created"
static WindowStateChange := "Window state changed"
}