-
-
Notifications
You must be signed in to change notification settings - Fork 0
/
BEE2_Installer.ahk
932 lines (775 loc) · 28.2 KB
/
BEE2_Installer.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
#NoEnv
#NoTrayIcon
#SingleInstance Ignore
; Declare variables
installFlag := 3
downloadPackages := 1
downloadApp := 1
currentVersion := "0.0"
packageVersion := "v0.0"
; Decide what will happen next by parsing command line flags
If (A_Args[1])
{
If ((A_Args[1] = "-u") || (A_Args[1] = "--uninstall") || (A_Args[1] = "/U"))
installFlag := 1
If ((A_Args[1] = "-c") || (A_Args[1] = "--check-for-updates") || (A_Args[1] = "/C"))
installFlag := 2
If ((A_Args[1] = "-r") || (A_Args[1] = "--run-bee2") || (A_Args[1] = "/R"))
installFlag := 3
} else {
installFlag := 0
}
; Check for internet access
SetWorkingDir %A_Temp%
FileDelete, internet_check.tmp
UrlDownloadToFile, https://github.com/programmer2514/BEE2.4-Installer-Automatic/blob/master/internet_check.tmp?raw=true, internet_check.tmp
If ((ErrorLevel = 1) && (installFlag != 1))
{
MsgBox, 0x10, BEE2 Installer - No internet connection!, The installer was unable to check for updates.`nPlease check your internet connection and try again.
if (installFlag = 3)
{
ComObjCreate( "Shell.Application" ).Windows.FindWindowSW( 0 , 0 , 8 , 0 , 1 ).Document.Application.ShellExecute( """C:\Program Files\BEEMOD2\BEE2.exe""" )
}
ExitApp
}
FileDelete, internet_check.tmp
if (installFlag = 1)
{
GoSub, UninstallBEE2
ExitApp
}
if (installFlag = 2)
{
GoSub, CheckForUpdates
ExitApp
}
if (installFlag = 3)
{
GoSub, RunBEE2
ExitApp
}
; If the script is not elevated, relaunch as administrator and kill current instance
full_command_line := DllCall("GetCommandLine", "str")
if not (A_IsAdmin or RegExMatch(full_command_line, " /restart(?!\S)"))
{
try
{
if A_IsCompiled
Run *RunAs "%A_ScriptFullPath%" /restart
else
Run *RunAs "%A_AhkPath%" /restart "%A_ScriptFullPath%"
}
ExitApp
}
if (installFlag = 0)
{
GoSub, InstallBEE2
ExitApp
}
ExitApp
InstallBEE2:
If (installFlag = 0)
{
; Install prompt
MsgBox, 0x21, BEE2 Installer, Would you like to install Better Extended Editor for Portal 2?
; Interpret user answer
IfMsgBox, Cancel
{
MsgBox, 0x30, BEE2 Installer, Installation canceled
downloadApp := 0
downloadPackages := 0
ExitApp
}
Sleep, 1000
; Package prompt
MsgBox, 0x23, BEE2 Installer, Would you like to install additional BEE2 Packages?
; Interpret user answer
IfMsgBox, Cancel
{
MsgBox, 0x30, BEE2 Installer, Installation canceled
downloadApp := 0
downloadPackages := 0
ExitApp
}
IfMsgBox, No
{
downloadPackages := 0
}
}
; Start install GUI
Gui, New, +AlwaysOnTop +Disabled +MinSize320x240, BEE2 Installer
Gui, Add, Text, w320 vInstallText, Preparing to install...
Gui, Add, Progress, w320 h20 cGreen vInstallProgress, 0
Gui, Show, AutoSize xCenter yCenter
; Delete old install
If (installFlag = 0)
{
SetWorkingDir, C:\Program Files
FileDelete, BEEMOD2
FileRemoveDir, BEEMOD2, 1
FileCreateDir, BEEMOD2
}
; Update progress
Loop, 5
{
GuiControl,, InstallProgress, +1
Sleep, 5
}
; Create temp folder
SetWorkingDir %A_Temp%
FileRemoveDir, BEEMOD2, 1
FileCreateDir, BEEMOD2
; Update progress
Loop, 3
{
GuiControl,, InstallProgress, +1
Sleep, 5
}
; Navigate to temp folder
SetWorkingDir %A_Temp%\BEEMOD2
; Update progress
GuiControl,, InstallProgress, +1
; Download unzip.exe
UrlDownloadToFile, https://github.com/programmer2514/BEE2.4-Installer-Automatic/blob/master/unzip/unzip.exe?raw=true, unzip.exe
; Update progress
Loop, 3
{
GuiControl,, InstallProgress, +1
Sleep, 5
}
; Download latest BEE2 application
if (downloadApp = 1)
{
GuiControl,, InstallText, Downloading BEE2...
; Download GitHub API JSON data
UrlDownloadToFile, https://api.github.com/repos/BEEmod/BEE2.4/releases, beemod.json
; Parse JSON data
FileRead beemodJSON, beemod.json
beemodData := JSON.Load(beemodJSON)
beemodURL := beemodData[1].assets[1].browser_download_url
currentVersion := beemodData[1].tag_name
; Download file
UrlDownloadToFile, %beemodURL%, beemod.zip
GuiControl,, InstallText, Installing BEE2...
Sleep, 250
; Unzip and copy to program files
RunWait, unzip.exe beemod.zip -d beemod,, Hide
CopyFilesAndFolders(A_Temp . "\BEEMOD2\beemod\*", "C:\Program Files\BEEMOD2", true)
}
; Update progress
Loop, 38
{
GuiControl,, InstallProgress, +1
Sleep, 5
}
; Download latest BEE2 packages
if (downloadPackages = 1)
{
GuiControl,, InstallText, Downloading packages...
; Download GitHub API JSON data
UrlDownloadToFile, https://api.github.com/repos/BEEmod/BEE2-items/releases, packages.json
; Parse JSON data
FileRead packagesJSON, packages.json
packagesData := JSON.Load(packagesJSON)
packagesURL := packagesData[1].assets[2].browser_download_url
packagesMusicURL := packagesData[1].assets[1].browser_download_url
packageVersion := packagesData[1].tag_name
; Download files
UrlDownloadToFile, %packagesURL%, packages.zip
UrlDownloadToFile, %packagesMusicURL%, music_packages.zip
GuiControl,, InstallText, Installing packages...
Sleep, 250
; Unzip and copy to program files
RunWait, unzip.exe packages.zip -d packages,, Hide
RunWait, unzip.exe music_packages.zip -d packages,, Hide
CopyFilesAndFolders(A_Temp . "\BEEMOD2\packages", "C:\Program Files\BEEMOD2", true)
}
; Update progress
Loop, 40
{
GuiControl,, InstallProgress, +1
Sleep, 5
}
GuiControl,, InstallText, Cleaning up...
; Clean up temp files
SetWorkingDir %A_Temp%
FileRemoveDir, BEEMOD2, 1
; Update progress
Loop, 2
{
GuiControl,, InstallProgress, +1
Sleep, 5
}
GuiControl,, InstallText, Downloading icons...
; Copy self into BEE2 directory
FileCopy, %A_ScriptFullPath%, C:\Program Files\BEEMOD2
SetWorkingDir, C:\Program Files\BEEMOD2
FileDelete, BEE2.ico
UrlDownloadToFile, https://raw.githubusercontent.com/programmer2514/BEE2.4-Installer-Automatic/master/icons/bee2.ico, bee2.ico
UrlDownloadToFile, https://raw.githubusercontent.com/programmer2514/BEE2.4-Installer-Automatic/master/icons/bee2-uninstaller.ico, bee2-uninstaller.ico
UrlDownloadToFile, https://raw.githubusercontent.com/programmer2514/BEE2.4-Installer-Automatic/master/icons/bee2-updater.ico, bee2-updater.ico
; Update progress
Loop, 3
{
GuiControl,, InstallProgress, +1
Sleep, 5
}
GuiControl,, InstallText, Adding shortcuts...
; Create Start Menu folder
SetWorkingDir, C:\ProgramData\Microsoft\Windows\Start Menu\Programs
FileRemoveDir, BEE2, 1
FileCreateDir, BEE2
; Update progress
Loop, 2
{
GuiControl,, InstallProgress, +1
Sleep, 5
}
; Navigate to Start Menu folder
SetWorkingDir, C:\ProgramData\Microsoft\Windows\Start Menu\Programs\BEE2
; Update progress
GuiControl,, InstallProgress, +1
; Create BEE2, Check for updates, & Uninstall links
FileCreateShortcut, C:\Program Files\BEEMOD2\%A_ScriptName%, BEE2.lnk, C:\Program Files\BEEMOD2\, -r, Launch BEE2, C:\Program Files\BEEMOD2\bee2.ico
FileCreateShortcut, C:\Program Files\BEEMOD2\%A_ScriptName%, Uninstall.lnk, C:\Program Files\BEEMOD2\, -u, Uninstall BEE2, C:\Program Files\BEEMOD2\bee2-uninstaller.ico
FileCreateShortcut, C:\Program Files\BEEMOD2\%A_ScriptName%, Check for Updates.lnk, C:\Program Files\BEEMOD2\, -c, Check for updates to BEE2, C:\Program Files\BEEMOD2\bee2-updater.ico
; Update progress
Loop, 2
{
GuiControl,, InstallProgress, +1
Sleep, 5
}
GuiControl,, InstallText, Finishing up...
; Create text file with BEE2 version
SetWorkingDir, %A_AppData%\BEEMOD2
if (downloadApp = 1)
{
FileDelete, version.txt
FileAppend, %currentVersion%, version.txt
}
if (downloadPackages = 1)
{
FileDelete, pck_version.txt
FileAppend, %packageVersion%, pck_version.txt
}
Date_HRS := (A_YYYY * 8760) + (A_MM * 730) + (A_DD * 24) + A_Hour
FileDelete, last_update.txt
FileAppend, %Date_HRS%, last_update.txt
; Finish installation
RunWait, icacls "C:\Program Files\BEEMOD2" /grant "Everyone":F /t,, Hide
Gui, Destroy
If (downloadApp = 1)
MsgBox, 0x40, BEE2 Installer, BEEMOD v%currentVersion% has been installed successfully!
If (downloadPackages = 1)
MsgBox, 0x40, BEE2 Installer, BEEMOD packages %packageVersion% has been installed successfully!
; Run Prompt
MsgBox, 0x124, BEE2 Installer, Would you like to run BEE2 now?
IfMsgBox Yes
{
ComObjCreate( "Shell.Application" ).Windows.FindWindowSW( 0 , 0 , 8 , 0 , 1 ).Document.Application.ShellExecute( """C:\Program Files\BEEMOD2\BEE2.exe""" )
ExitApp
}
Return
UninstallBEE2:
; If the script is not elevated, relaunch as administrator and kill current instance
full_command_line := DllCall("GetCommandLine", "str")
if not (A_IsAdmin or RegExMatch(full_command_line, " /restart(?!\S)"))
{
try
{
if A_IsCompiled
{
FileCopy, %A_ScriptFullPath%, %A_Temp%, true
Run *RunAs "%A_Temp%\%A_ScriptName%" /U /restart
} else {
Run *RunAs "%A_AhkPath%" /restart "%A_ScriptFullPath%" /U
}
}
ExitApp
}
; Uninstall prompt
MsgBox, 0x21, BEE2 Uninstaller, Are you sure you would like to remove Better Extended Editor for Portal 2 and all of its components?
; Interpret user answer
IfMsgBox, Cancel
{
ExitApp
}
; Remove version text files
SetWorkingDir, %A_AppData%\BEEMOD2
FileDelete, version.txt
FileDelete, pck_version.txt
FileDelete, last_update.txt
Sleep, 1000
; Remove data prompt
MsgBox, 0x123, BEE2 Uninstaller, Would you like to remove user data (configuration, palettes, etc.)?
; Interpret user answer
IfMsgBox, Cancel
{
ExitApp
}
IfMsgBox Yes
{
MsgBox, 0x123, BEE2 Uninstaller, Are you sure you would like to remove ALL USER DATA?`nThis action cannot be undone.
IfMsgBox Yes
{
SetWorkingDir, %A_AppData%
FileRemoveDir, BEEMOD2, 1
}
}
; Remove application/packages
SetWorkingDir, C:\Program Files
FileRemoveDir, BEEMOD2, 1
; Remove start menu shortcuts
SetWorkingDir, C:\ProgramData\Microsoft\Windows\Start Menu\Programs
FileRemoveDir, BEE2, 1
MsgBox, 0x40, BEE2 Uninstaller, Better Extended Editor for Portal 2 removed successfully!
Return
CheckForUpdates:
; Reset variables
downloadPackages := 0
downloadApp := 0
newerVersionAvailable := 0
; Create temp folder
SetWorkingDir %A_Temp%
FileRemoveDir, BEEMOD2, 1
FileCreateDir, BEEMOD2
; Download GitHub API JSON data
UrlDownloadToFile, https://api.github.com/repos/BEEmod/BEE2.4/releases, beemod.json
UrlDownloadToFile, https://api.github.com/repos/BEEmod/BEE2-items/releases, packages.json
; Parse JSON data
FileRead beemodJSON, beemod.json
FileRead packagesJSON, packages.json
beemodData := JSON.Load(beemodJSON)
packagesData := JSON.Load(packagesJSON)
newVersion := beemodData[1].tag_name
newPckVersion := packagesData[1].tag_name
; Read current version text files
SetWorkingDir, %A_AppData%\BEEMOD2
FileRead currentVersion, version.txt
If FileExist("pck_version.txt")
FileRead currentPckVersion, pck_version.txt
else
currentPckVersion := "null"
If not (currentVersion = newVersion)
newerVersionAvailable := 1
If not (currentPckVersion = newPckVersion)
{
If not (currentPckVersion = "null")
{
If (newerVersionAvailable = 1)
newerVersionAvailable := 3
else
newerVersionAvailable := 2
}
}
; Ask user if they want to download newer version if available, otherwise, do nothing
If (newerVersionAvailable > 0)
{
If (installFlag = 3)
{
; If the script is not elevated, relaunch as administrator and kill current instance
full_command_line := DllCall("GetCommandLine", "str")
if not (A_IsAdmin or RegExMatch(full_command_line, " /restart(?!\S)"))
{
try
{
if A_IsCompiled
{
FileCopy, %A_ScriptFullPath%, %A_Temp%, true
Run *RunAs "%A_Temp%\%A_ScriptName%" -r /restart
} else {
Run *RunAs "%A_AhkPath%" /restart "%A_ScriptFullPath%" -r
}
}
ExitApp
}
} else
{
; If the script is not elevated, relaunch as administrator and kill current instance
full_command_line := DllCall("GetCommandLine", "str")
if not (A_IsAdmin or RegExMatch(full_command_line, " /restart(?!\S)"))
{
try
{
if A_IsCompiled
{
FileCopy, %A_ScriptFullPath%, %A_Temp%, true
Run *RunAs "%A_Temp%\%A_ScriptName%" /C /restart
} else {
Run *RunAs "%A_AhkPath%" /restart "%A_ScriptFullPath%" /C
}
}
ExitApp
}
}
If ((newerVersionAvailable = 1) || (newerVersionAvailable = 3))
{
MsgBox, 0x44, BEE2 Updater, A newer version of BEE2 (%newVersion% > %currentVersion%) is available.`nWould you like to update now?
IfMsgBox Yes
downloadApp := 1
}
If ((newerVersionAvailable = 2) || (newerVersionAvailable = 3))
{
MsgBox, 0x44, BEE2 Updater, A newer version of BEE2 packages (%newPckVersion% > %currentPckVersion%) is available.`nWould you like to update now?
IfMsgBox Yes
downloadPackages := 1
}
If ((downloadApp = 1) || (downloadPackages = 1))
GoSub, InstallBEE2
else
Return
} else
{
If (installFlag = 2)
MsgBox, 0x40, BEE2 Updater, No updates are currently available
}
Return
RunBEE2:
; Get last update time
SetWorkingDir, %A_AppData%\BEEMOD2
FileRead lastUpdate, last_update.txt
; Get date in hours
Date_HRS := (A_YYYY * 8760) + (A_MM * 730) + (A_DD * 24) + A_Hour
; Check for updates
If not (Date_HRS = lastUpdate)
{
GoSub, CheckForUpdates
}
; Run BEE2
ComObjCreate( "Shell.Application" ).Windows.FindWindowSW( 0 , 0 , 8 , 0 , 1 ).Document.Application.ShellExecute( """C:\Program Files\BEEMOD2\BEE2.exe""" )
Return
CopyFilesAndFolders(SourcePattern, DestinationFolder, DoOverwrite = false)
{
; First copy all the files (but not the folders):
FileCopy, %SourcePattern%, %DestinationFolder%, %DoOverwrite%
ErrorCount := ErrorLevel
; Now copy all the folders:
Loop, %SourcePattern%, 2 ; 2 means "retrieve folders only".
{
FileCopyDir, %A_LoopFileFullPath%, %DestinationFolder%\%A_LoopFileName%, %DoOverwrite%
ErrorCount += ErrorLevel
if ErrorLevel ; Report each problem folder by name.
MsgBox Could not copy %A_LoopFileFullPath% into %DestinationFolder%.
}
return ErrorCount
}
/**
* Lib: JSON.ahk
* JSON lib for AutoHotkey.
* Version:
* v2.1.3 [updated 04/18/2016 (MM/DD/YYYY)]
* License:
* WTFPL [http://wtfpl.net/]
* Requirements:
* Latest version of AutoHotkey (v1.1+ or v2.0-a+)
* Installation:
* Use #Include JSON.ahk or copy into a function library folder and then
* use #Include <JSON>
* Links:
* GitHub: - https://github.com/cocobelgica/AutoHotkey-JSON
* Forum Topic - http://goo.gl/r0zI8t
* Email: - cocobelgica <at> gmail <dot> com
*/
/**
* Class: JSON
* The JSON object contains methods for parsing JSON and converting values
* to JSON. Callable - NO; Instantiable - YES; Subclassable - YES;
* Nestable(via #Include) - NO.
* Methods:
* Load() - see relevant documentation before method definition header
* Dump() - see relevant documentation before method definition header
*/
class JSON
{
/**
* Method: Load
* Parses a JSON string into an AHK value
* Syntax:
* value := JSON.Load( text [, reviver ] )
* Parameter(s):
* value [retval] - parsed value
* text [in, ByRef] - JSON formatted string
* reviver [in, opt] - function object, similar to JavaScript's
* JSON.parse() 'reviver' parameter
*/
class Load extends JSON.Functor
{
Call(self, ByRef text, reviver:="")
{
this.rev := IsObject(reviver) ? reviver : false
; Object keys(and array indices) are temporarily stored in arrays so that
; we can enumerate them in the order they appear in the document/text instead
; of alphabetically. Skip if no reviver function is specified.
this.keys := this.rev ? {} : false
static quot := Chr(34), bashq := "\" . quot
, json_value := quot . "{[01234567890-tfn"
, json_value_or_array_closing := quot . "{[]01234567890-tfn"
, object_key_or_object_closing := quot . "}"
key := ""
is_key := false
root := {}
stack := [root]
next := json_value
pos := 0
while ((ch := SubStr(text, ++pos, 1)) != "") {
if InStr(" `t`r`n", ch)
continue
if !InStr(next, ch, 1)
this.ParseError(next, text, pos)
holder := stack[1]
is_array := holder.IsArray
if InStr(",:", ch) {
next := (is_key := !is_array && ch == ",") ? quot : json_value
} else if InStr("}]", ch) {
ObjRemoveAt(stack, 1)
next := stack[1]==root ? "" : stack[1].IsArray ? ",]" : ",}"
} else {
if InStr("{[", ch) {
; Check if Array() is overridden and if its return value has
; the 'IsArray' property. If so, Array() will be called normally,
; otherwise, use a custom base object for arrays
static json_array := Func("Array").IsBuiltIn || ![].IsArray ? {IsArray: true} : 0
; sacrifice readability for minor(actually negligible) performance gain
(ch == "{")
? ( is_key := true
, value := {}
, next := object_key_or_object_closing )
; ch == "["
: ( value := json_array ? new json_array : []
, next := json_value_or_array_closing )
ObjInsertAt(stack, 1, value)
if (this.keys)
this.keys[value] := []
} else {
if (ch == quot) {
i := pos
while (i := InStr(text, quot,, i+1)) {
value := StrReplace(SubStr(text, pos+1, i-pos-1), "\\", "\u005c")
static tail := A_AhkVersion<"2" ? 0 : -1
if (SubStr(value, tail) != "\")
break
}
if (!i)
this.ParseError("'", text, pos)
value := StrReplace(value, "\/", "/")
, value := StrReplace(value, bashq, quot)
, value := StrReplace(value, "\b", "`b")
, value := StrReplace(value, "\f", "`f")
, value := StrReplace(value, "\n", "`n")
, value := StrReplace(value, "\r", "`r")
, value := StrReplace(value, "\t", "`t")
pos := i ; update pos
i := 0
while (i := InStr(value, "\",, i+1)) {
if !(SubStr(value, i+1, 1) == "u")
this.ParseError("\", text, pos - StrLen(SubStr(value, i+1)))
uffff := Abs("0x" . SubStr(value, i+2, 4))
if (A_IsUnicode || uffff < 0x100)
value := SubStr(value, 1, i-1) . Chr(uffff) . SubStr(value, i+6)
}
if (is_key) {
key := value, next := ":"
continue
}
} else {
value := SubStr(text, pos, i := RegExMatch(text, "[\]\},\s]|$",, pos)-pos)
static number := "number", integer :="integer"
if value is %number%
{
if value is %integer%
value += 0
}
else if (value == "true" || value == "false")
value := %value% + 0
else if (value == "null")
value := ""
else
; we can do more here to pinpoint the actual culprit
; but that's just too much extra work.
this.ParseError(next, text, pos, i)
pos += i-1
}
next := holder==root ? "" : is_array ? ",]" : ",}"
} ; If InStr("{[", ch) { ... } else
is_array? key := ObjPush(holder, value) : holder[key] := value
if (this.keys && this.keys.HasKey(holder))
this.keys[holder].Push(key)
}
} ; while ( ... )
return this.rev ? this.Walk(root, "") : root[""]
}
ParseError(expect, ByRef text, pos, len:=1)
{
static quot := Chr(34), qurly := quot . "}"
line := StrSplit(SubStr(text, 1, pos), "`n", "`r").Length()
col := pos - InStr(text, "`n",, -(StrLen(text)-pos+1))
msg := Format("{1}`n`nLine:`t{2}`nCol:`t{3}`nChar:`t{4}"
, (expect == "") ? "Extra data"
: (expect == "'") ? "Unterminated string starting at"
: (expect == "\") ? "Invalid \escape"
: (expect == ":") ? "Expecting ':' delimiter"
: (expect == quot) ? "Expecting object key enclosed in double quotes"
: (expect == qurly) ? "Expecting object key enclosed in double quotes or object closing '}'"
: (expect == ",}") ? "Expecting ',' delimiter or object closing '}'"
: (expect == ",]") ? "Expecting ',' delimiter or array closing ']'"
: InStr(expect, "]") ? "Expecting JSON value or array closing ']'"
: "Expecting JSON value(string, number, true, false, null, object or array)"
, line, col, pos)
static offset := A_AhkVersion<"2" ? -3 : -4
throw Exception(msg, offset, SubStr(text, pos, len))
}
Walk(holder, key)
{
value := holder[key]
if IsObject(value) {
for i, k in this.keys[value] {
; check if ObjHasKey(value, k) ??
v := this.Walk(value, k)
if (v != JSON.Undefined)
value[k] := v
else
ObjDelete(value, k)
}
}
return this.rev.Call(holder, key, value)
}
}
/**
* Method: Dump
* Converts an AHK value into a JSON string
* Syntax:
* str := JSON.Dump( value [, replacer, space ] )
* Parameter(s):
* str [retval] - JSON representation of an AHK value
* value [in] - any value(object, string, number)
* replacer [in, opt] - function object, similar to JavaScript's
* JSON.stringify() 'replacer' parameter
* space [in, opt] - similar to JavaScript's JSON.stringify()
* 'space' parameter
*/
class Dump extends JSON.Functor
{
Call(self, value, replacer:="", space:="")
{
this.rep := IsObject(replacer) ? replacer : ""
this.gap := ""
if (space) {
static integer := "integer"
if space is %integer%
Loop, % ((n := Abs(space))>10 ? 10 : n)
this.gap .= " "
else
this.gap := SubStr(space, 1, 10)
this.indent := "`n"
}
return this.Str({"": value}, "")
}
Str(holder, key)
{
value := holder[key]
if (this.rep)
value := this.rep.Call(holder, key, ObjHasKey(holder, key) ? value : JSON.Undefined)
if IsObject(value) {
; Check object type, skip serialization for other object types such as
; ComObject, Func, BoundFunc, FileObject, RegExMatchObject, Property, etc.
static type := A_AhkVersion<"2" ? "" : Func("Type")
if (type ? type.Call(value) == "Object" : ObjGetCapacity(value) != "") {
if (this.gap) {
stepback := this.indent
this.indent .= this.gap
}
is_array := value.IsArray
; Array() is not overridden, rollback to old method of
; identifying array-like objects. Due to the use of a for-loop
; sparse arrays such as '[1,,3]' are detected as objects({}).
if (!is_array) {
for i in value
is_array := i == A_Index
until !is_array
}
str := ""
if (is_array) {
Loop, % value.Length() {
if (this.gap)
str .= this.indent
v := this.Str(value, A_Index)
str .= (v != "") ? v . "," : "null,"
}
} else {
colon := this.gap ? ": " : ":"
for k in value {
v := this.Str(value, k)
if (v != "") {
if (this.gap)
str .= this.indent
str .= this.Quote(k) . colon . v . ","
}
}
}
if (str != "") {
str := RTrim(str, ",")
if (this.gap)
str .= stepback
}
if (this.gap)
this.indent := stepback
return is_array ? "[" . str . "]" : "{" . str . "}"
}
} else ; is_number ? value : "value"
return ObjGetCapacity([value], 1)=="" ? value : this.Quote(value)
}
Quote(string)
{
static quot := Chr(34), bashq := "\" . quot
if (string != "") {
string := StrReplace(string, "\", "\\")
; , string := StrReplace(string, "/", "\/") ; optional in ECMAScript
, string := StrReplace(string, quot, bashq)
, string := StrReplace(string, "`b", "\b")
, string := StrReplace(string, "`f", "\f")
, string := StrReplace(string, "`n", "\n")
, string := StrReplace(string, "`r", "\r")
, string := StrReplace(string, "`t", "\t")
static rx_escapable := A_AhkVersion<"2" ? "O)[^\x20-\x7e]" : "[^\x20-\x7e]"
while RegExMatch(string, rx_escapable, m)
string := StrReplace(string, m.Value, Format("\u{1:04x}", Ord(m.Value)))
}
return quot . string . quot
}
}
/**
* Property: Undefined
* Proxy for 'undefined' type
* Syntax:
* undefined := JSON.Undefined
* Remarks:
* For use with reviver and replacer functions since AutoHotkey does not
* have an 'undefined' type. Returning blank("") or 0 won't work since these
* can't be distnguished from actual JSON values. This leaves us with objects.
* Replacer() - the caller may return a non-serializable AHK objects such as
* ComObject, Func, BoundFunc, FileObject, RegExMatchObject, and Property to
* mimic the behavior of returning 'undefined' in JavaScript but for the sake
* of code readability and convenience, it's better to do 'return JSON.Undefined'.
* Internally, the property returns a ComObject with the variant type of VT_EMPTY.
*/
Undefined[]
{
get {
static empty := {}, vt_empty := ComObject(0, &empty, 1)
return vt_empty
}
}
class Functor
{
__Call(method, ByRef arg, args*)
{
; When casting to Call(), use a new instance of the "function object"
; so as to avoid directly storing the properties(used across sub-methods)
; into the "function object" itself.
if IsObject(method)
return (new this).Call(method, arg, args*)
else if (method == "")
return (new this).Call(arg, args*)
}
}
}