-
Notifications
You must be signed in to change notification settings - Fork 20
/
Copy pathSnickerstream.au3
1475 lines (1346 loc) · 73.2 KB
/
Snickerstream.au3
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
#cs
___ _ _ _
/ __|_ _ (_)__| |_____ _ _ __| |_ _ _ ___ __ _ _ __
\__ \ ' \| / _| / / -_) '_(_-< _| '_/ -_) _` | ' \
|___/_||_|_\__|_\_\___|_| /__/\__|_| \___\__,_|_|_|_|
By RattletraPM - licensed under GNU GPL V3.0
Written for AutoIt v3.3.14.4
TODO: You decide!
#ce
#include <AutoItConstants.au3>
#include <ButtonConstants.au3>
#include <ComboConstants.au3>
#include <EditConstants.au3>
#include <GuiIPAddress.au3>
#include <StaticConstants.au3>
#include <WindowsConstants.au3>
#include <GuiComboBox.au3>
#include <GuiComboBoxEx.au3>
#include <GUIConstantsEx.au3>
#include <Misc.au3>
#include <String.au3>
#include <ScreenCapture.au3>
#include <WinAPIConv.au3>
#include <Crypt.au3>
#include "include\ntr.au3"
#include "include\HzMod.au3"
#include "include\WIC.au3"
#include "include\Direct2D.au3"
#include "include\Direct2D_1.au3"
#include "include\GitAndGithub.au3"
Opt("MustDeclareVars",1)
;Globals - Streaming GUI and Drawing
Global $g_hGUI, $g_hGfxCtxt, $g_hBitmap, $g_hBMP, $g_hBMP2, $g_hGraphics, $iFPS = 0, $iInterpolation = 0, _
$aWinResize, $aWinResize2, $ix1BMP1 = 0, $ix2BMP1 = 0, $ix1BMP2 = 0, $ix2BMP2 = 0, $iy1BMP1 = 0, $iy2BMP1 = 0, $iy1BMP2 = 0, $iy2BMP2 = 0, _
$iLayoutmode = 0, $bNoDisplayIPWarn = 0, $bD2D = True, $iListenPort = 8001, $bFullscreenPopup = False
Global Const $sVersion = "v1.10", $sGUITitle = "Snickerstream " & $sVersion, $sIconPath=@TempDir&"\snickerstream.ico"
;Globals - Main and Advanced GUI
Global $sIpAddr = "0.0.0.0", $iPriorityMode = 0, $iPriorityFactor = 5, $iImageQuality = 70, $iQoS = 20, $AdvancedMenu = 0, $aHotkeys[10] = ["26","28","25","27","0D","53","20","1B","51","45"]
;Globals - Config
Global $sFname = @ScriptDir & "\settings.ini", $sPCIpAddr = $sIpAddr, $iLogLevel = 0, $bUseNTR = True, $iLogConsole = 0
Global Const $sSectionName = "Snickerstream", $aIniSections[31] = ["IpAddr", "PriorityMode", "PriorityFactor", "ImageQuality", _
"QoS", "Interpolation", "Layoutmode", "PCIpAddr", "NoDisplayIPWarn", "Loglevel", "DontCheckSettings", "UseD2D", "GDIPWarn", "NFCLastAnswer", _
"WaitRemoteplayInit", "Framelimit", "ReturnAfterMsec", "DontCheckForUpdates", "CustomWidth", "CustomHeight", "ListenPort", "AdvWarning", "TopScalingFactor", _
"BottomScalingFactor", "CenterScreens", "Hotkeys", "UseNTR", "LogConsole", "CustomWidth2", "CustomHeight2", "EnableHzMNFCPatch"], _
$sLogFname = "log.txt", $sSettingSeparator = "|"
Global Const $iD2D1InterpModes[6] = [$D2D1_INTERPOLATION_MODE_NEAREST_NEIGHBOR, $D2D1_INTERPOLATION_MODE_LINEAR, $D2D1_INTERPOLATION_MODE_CUBIC, _
$D2D1_INTERPOLATION_MODE_MULTI_SAMPLE_LINEAR, $D2D1_INTERPOLATION_MODE_ANISOTROPIC, $D2D1_INTERPOLATION_MODE_HIGH_QUALITY_CUBIC]
Global Const $sD2D1InterpModeNames[6] = ["Nearest Neighbor", "Linear", "Cubic", "Multi Sample Linear", "Anisotropic", "High Quality Cubic"]
;Globals - Misc Constants
Global Const $sSeparator = _StringRepeat("-", 33), $sGUI2 = "g_hGUI2", $iW3DSTop = 400, $iW3DSBot = 320, $iH3DS = 240
;Globals - Misc
Global $hTimer = TimerInit(), $bStreaming = False, $bEnableHzMNFCPatch = False
;Globals - GUI
Global $GUI_Rendering = 0, $GUI_Interpolation = 0 ;Those are here to prevent a "possibly used before declaration" warning (yes, the variables are declared before being used in a separate UDF but SciTE doesn't recognize it)
AutoItSetOption("TrayAutoPause", 0)
AutoItSetOption("GUIOnEventMode", 1) ;Use OnEvent mode - a script like this would be awful without it
AutoItSetOption("TCPTimeout", 5000)
AutoItSetOption("TrayAutoPause", 0)
FileInstall("res\snickerstream.ico",$sIconPath,1)
If $CmdLine[0] == 1 Then ;Custom settings.ini path
$sFname = $CmdLine[1]
EndIf
;Globals - Custom presets INI
Global Static $sPresetFname = StringLeft($sFname, StringInStr($sFname, "\", 0, -1)) & "presets.ini", $sPresetSection = $sSectionName & "Presets"
;COM error handling
Global $oError = ObjEvent("AutoIt.Error", "_ErrFunc")
Func _ErrFunc()
LogLine("COM Error, ScriptLine(" & $oError.ScriptLine & ") : Number 0x" & Hex($oError.Number, 8) & " - " & $oError.WinDescription & @CRLF, 1)
EndFunc ;==>_ErrFunc
CreateMainGUIandSettings() ;Create the GUI
ButtonStateSet($GUI_DISABLE)
LogStart()
LogLine("WARNING: The loglevel is set to 3. This will log just about EVERYTHING and could impact your performance.", 3)
LogLine("You should set your loglevel to something else unless you're troubleshooting some issues!", 3)
LogLine("OS: " & @OSVersion, 3)
If (IsAdmin() == 1 And @Compiled == 1) And (StringLeft(@OSVersion, 6) <> "WIN_XP" And @OSVersion <> "WIN_2003") Then ;This feature is not supported in Windows XP/XPe/Server 2003
LogLine("WARNING: Snickerstream was ran as admin and will now allow itself in Windows Firewall.", 1)
If FileExists(@SystemDir & "\netsh.exe") Then
ShellExecute(@SystemDir & '\netsh', 'advfirewall firewall delete rule name="Snickerstream"', Default, Default, @SW_HIDE)
ShellExecute(@SystemDir & '\netsh', 'advfirewall firewall add rule name="Snickerstream" dir=in action=allow program="' & @ScriptFullPath & '" enable=yes', Default, Default, @SW_HIDE)
ShellExecute(@SystemDir & '\netsh', 'advfirewall firewall add rule name="Snickerstream" dir=out action=allow program="' & @ScriptFullPath & '" enable=yes', Default, Default, @SW_HIDE)
LogLine("Done. It's strongly recommended to run Snickerstream as a normal user from now on.", 1)
Else
LogLine("There was an error while adding an exception to Windows Firewall! Please add one manually from the control panel.", 1)
EndIf
EndIf
CheckForUpdates()
If $__g_hD2D1DLL == -1 Then
MsgBox($MB_ICONERROR, "Direct2D init error", "There was an error while initializing Direct2D." & @CRLF & @CRLF & "This error is most likely caused by using Snickerstream on an OS" & _
" that doesn't support Direct2D (available on Windows Vista SP2 or later). You will still be able to use this tool, but you'll have to use GDI+ as its rendering library instead (no hardware" & _
" acceleration and slower, check the readme for more info).")
_GUICtrlComboBox_SetCurSel($GUI_Rendering, 1)
GUICtrlSetState($GUI_Rendering, $GUI_DISABLE)
GUICtrlSetData($GUI_Interpolation, "|Default|Low quality|High quality|Bilinear|Bicubic|Nearest-neighbor|Bilinear (HQ)|Bicubic (HQ)", "Default")
$bD2D = False
EndIf
ButtonStateSet($GUI_ENABLE)
If $bUseNTR == False Then SettingsStateSet($GUI_DISABLE)
Do
Sleep(100)
Until $bStreaming = True
Func WinSetSize()
If $ix2BMP1 >= $ix2BMP2 Then
Global $iWidth = $ix2BMP1
Else
Global $iWidth = $ix2BMP2
EndIf
If $iy2BMP2 >= $iy2BMP1 Then
Global $iHeight = Abs($iy2BMP1)
Else
Global $iHeight = Abs($iy2BMP2)
EndIf
EndFunc ;==>WinSetSize
Func CenterScreens()
If $iLayoutmode == 0 Or $iLayoutmode == 1 Or (($iLayoutmode == 6 Or $iLayoutmode == 7 Or $iLayoutmode == 8 Or $iLayoutmode==9) And @DesktopWidth > @DesktopHeight) Then
Local $iTopDiff = ($iWidth - $ix2BMP1) / 2
Local $iBotDiff = ($iWidth - $ix2BMP2) / 2
$ix1BMP1 += $iTopDiff
$ix2BMP1 += $iTopDiff
$ix1BMP2 += $iBotDiff
$ix2BMP2 += $iBotDiff
EndIf
If $iLayoutmode == 2 Or $iLayoutmode == 3 Or (($iLayoutmode == 6 Or $iLayoutmode == 7 Or $iLayoutmode == 8 Or $iLayoutmode==9) And @DesktopWidth <= @DesktopHeight) Then
Local $iTopDiff = ((-$iHeight) - $iy2BMP1) / 2
Local $iBotDiff = ((-$iHeight) - $iy2BMP2) / 2
$iy1BMP1 += $iTopDiff
$iy2BMP1 += $iTopDiff
$iy1BMP2 += $iBotDiff
$iy2BMP2 += $iBotDiff
EndIf
EndFunc ;==>CenterScreens
Func ReadCustomHotkeys()
Local $sReadHotkeys=IniRead($sFname, $sSectionName, $aIniSections[25], 0)
If $sReadHotkeys<>0 Then
Local $aNewHotkeys=StringSplit($sReadHotkeys, $sSettingSeparator)
If UBound($aNewHotkeys)==11 Then
_ArrayDelete($aNewHotkeys,0)
$aHotkeys=$aNewHotkeys
Else
IniWrite($sFname, $sSectionName, $aIniSections[25], _ArrayToString($aHotkeys,$sSettingSeparator))
EndIf
EndIf
EndFunc
Func StreamingLoop()
If $bUseNTR==False Then $iLayoutmode = 4+(2*$iLayoutmode)
ReadCustomHotkeys()
Global $iTopScalingFactor = IniRead($sFname, $sSectionName, $aIniSections[22], 1)
If $iLayoutmode == 6 Or $iLayoutmode == 8 Then ;For Fullscreen Top, non-stretched
If @DesktopWidth > @DesktopHeight Then
$iTopScalingFactor = @DesktopHeight / 240
Else
$iTopScalingFactor = @DesktopWidth / 240
EndIf
EndIf
Global $iBotScalingFactor = IniRead($sFname, $sSectionName, $aIniSections[23], 1)
If $iLayoutmode == 7 Or $iLayoutmode == 9 Then ;For Fullscreen Bottom, non-stretched
If @DesktopWidth > @DesktopHeight Then
$iBotScalingFactor = @DesktopHeight / 240
Else
$iBotScalingFactor = @DesktopWidth / 240
EndIf
EndIf
If $bD2D == False Then ;_GDIPlus_GraphicsDrawImageRect() doesn't allow for resizing. I could use another func but it will just slow down streaming,
$iTopScalingFactor = 1 ;and software mode is already slow as it is right now... so I simply decided to make individual screen scaling D2D only.
$iBotScalingFactor = 1
EndIf
If $iTopScalingFactor < 0.3 Then $iTopScalingFactor = 1
If $iBotScalingFactor < 0.3 Then $iBotScalingFactor = 1
Local $iFramelock = IniRead($sFname, $sSectionName, $aIniSections[15], 0), $iReturnAfter = IniRead($sFname, $sSectionName, $aIniSections[16], 8000), $iFramelockedFPS = 0, _
$iTimerWaitExit = TimerInit(), $sFPSString = " FPS", $sWinTitle = ""
LogLine("PC IP address: " & $sPCIpAddr, 2)
LogLine("3DS IP address: " & $sIpAddr, 2)
LogLine("Priority: " & $iPriorityMode, 2)
LogLine("Priority factor: " & $iPriorityFactor, 2)
LogLine("Image quaility: " & $iImageQuality, 2)
LogLine("QoS: " & $iQoS, 2)
LogLine("Interpolation mode: " & $iInterpolation, 2)
LogLine("Screen layout: " & $iLayoutmode, 2)
LogLine("Using Direct2D: " & $bD2D, 2)
LogLine("Using NTR: " & $bUseNTR, 2)
If $iFramelock <> 0 Then LogLine("Frame limit: " & $iFramelock, 2)
If $bUseNTR==True Then LogLine("Listening port: " & $iListenPort, 2)
LogLine("Top Screen scaling factor: " & $iTopScalingFactor, 2)
LogLine("Bottom Screen scaling factor: " & $iBotScalingFactor, 2)
LogLine("-NOTE- The IP addresses should be internal. If you set them to public DO NOT share this log online!", 2)
If $bUseNTR==True Then
UDPStartup() ;Start up the UDP protocol
Global $iSocket = UDPBind($sPCIpAddr, $iListenPort) ;Listen to whatever port has been specified by the user (default=8001)
Else
TCPStartup()
Global $iSocket = TCPConnect($sIpAddr,6464)
Local $dReadahead = -1
EndIf
Local $iBGColor = 0x303030 ;$iBGColor format RRGGBB
;Settings for Vertical (default) view mode - GUI Width & Height
Local $iGUIWidthHeight = Default, $xGUIStyle = Default, $xGUIExStyle = Default
Global $ix1BMP1 = 0, $ix2BMP1 = 400 * $iTopScalingFactor, $iy1BMP1 = 0, $iy2BMP1 = -240 * $iTopScalingFactor ;Top screen
Global $ix1BMP2 = 0, $ix2BMP2 = 320 * $iBotScalingFactor, $iy1BMP2 = $iy2BMP1, $iy2BMP2 = $iy2BMP1 + (-240 * $iBotScalingFactor) ;Bottom screen
Switch $iLayoutmode ;View modes
Case 1 ;Vertical (inverted)
Global $ix1BMP2 = 0, $ix2BMP2 = 320 * $iBotScalingFactor, $iy1BMP2 = 0, $iy2BMP2 = -240 * $iBotScalingFactor ;Bottom screen
Global $ix1BMP1 = 0, $ix2BMP1 = 400 * $iTopScalingFactor, $iy1BMP1 = $iy2BMP2, $iy2BMP1 = $iy1BMP1 + (-240 * $iTopScalingFactor) ;Top screen
Case 2 ;Horizontal
Global $ix1BMP2 = $ix2BMP1, $ix2BMP2 = $ix2BMP1 + (320 * $iBotScalingFactor), $iy1BMP2 = 0, $iy2BMP2 = -240 * $iBotScalingFactor ;Bottom screen
Case 3 ;Horizontal (inverted)
Global $ix1BMP1 = $ix2BMP2, $ix2BMP1 = $ix2BMP2 + (400 * $iTopScalingFactor), $iy1BMP1 = 0, $iy2BMP1 = -240 * $iTopScalingFactor ;Top screen
Global $ix1BMP2 = 0, $ix2BMP2 = 320 * $iBotScalingFactor, $iy1BMP2 = 0, $iy2BMP2 = -240 * $iBotScalingFactor ;Bottom screen
Case 4 ;Top only
Global $iWidth = 400 * $iTopScalingFactor, $iHeight = 240 * $iTopScalingFactor
Global $ix1BMP1 = 0, $ix2BMP1 = $iWidth, $iy1BMP1 = 0, $iy2BMP1 = -$iHeight ;Top screen
CheckOptimal()
Case 5 ;Bottom only
Global $iWidth = 320 * $iBotScalingFactor, $iHeight = 240 * $iBotScalingFactor
Global $ix1BMP2 = 0, $ix2BMP2 = $iWidth, $iy1BMP2 = 0, $iy2BMP2 = -$iHeight ;Bottom screen
CheckOptimal()
Case 6 ;Fullscreen Top (stretched)
Local $iGUIWidthHeight = 0, $xGUIStyle = $WS_POPUP, $xGUIExStyle = $WS_EX_TOPMOST ;I'm sorry for the spaghetti code for modes 6-7-8-9, but AutoIt doesn't support
Global $iWidth = @DesktopWidth, $iHeight = @DesktopHeight ;Or statements in switches, and a separate func won't do much better due to local variables and stuff...
Global $ix1BMP1 = 0, $ix2BMP1 = $iWidth, $iy1BMP1 = 0, $iy2BMP1 = -$iHeight ;Top screen
CheckOptimal()
Case 7 ;Fullscreen Bot (stretched)
Local $iGUIWidthHeight = 0, $xGUIStyle = $WS_POPUP, $xGUIExStyle = $WS_EX_TOPMOST
Global $iWidth = @DesktopWidth, $iHeight = @DesktopHeight
Global $ix1BMP2 = 0, $ix2BMP2 = $iWidth, $iy1BMP2 = 0, $iy2BMP2 = -$iHeight ;Bottom screen
CheckOptimal()
Case 8 ;Fullscreen Top (non-stretched)
Local $iGUIWidthHeight = 0, $xGUIStyle = $WS_POPUP, $xGUIExStyle = $WS_EX_TOPMOST
Global $iWidth = @DesktopWidth, $iHeight = @DesktopHeight
Global $ix1BMP1 = 0, $ix2BMP1 = 400 * $iTopScalingFactor, $iy1BMP1 = 0, $iy2BMP1 = -240 * $iTopScalingFactor ;Top screen
CheckOptimal()
Case 9 ;Fullscreen Bot (non-stretched)
Local $iGUIWidthHeight = 0, $xGUIStyle = $WS_POPUP, $xGUIExStyle = $WS_EX_TOPMOST
Global $iWidth = @DesktopWidth, $iHeight = @DesktopHeight
Global $ix1BMP2 = 0, $ix2BMP2 = 320 * $iBotScalingFactor, $iy1BMP2 = 0, $iy2BMP2 = -240 * $iBotScalingFactor ;Bottom screen
CheckOptimal()
Case 10 ;Separate windows
Global $iWidth = 400 * $iTopScalingFactor, $iHeight = 240 * $iTopScalingFactor ;Window 1
Global $ix1BMP1 = 0, $ix2BMP1 = $iWidth, $iy1BMP1 = 0, $iy2BMP1 = -$iHeight ;Top screen
Global $iWidth2 = 320 * $iBotScalingFactor, $iHeight2 = 240 * $iBotScalingFactor ;Window 2
Global $ix1BMP2 = 0, $ix2BMP2 = $iWidth2, $iy1BMP2 = 0, $iy2BMP2 = -$iHeight2 ;Bottom screen
EndSwitch
;Needed for pop-up secondary screen
If $iLayoutmode>5 And $iLayoutmode<>10 Then
Local $oJpegBuf="" ;Extra framebuffer for D2D fullscreen modes, needed for pop-up second screen
If $iLayoutmode==6 Or $iLayoutmode==8 Then
Global $ix1BMP2 = 0, $ix2BMP2 = 320*$iBotScalingFactor, $iy1BMP2 = -$iHeight, $iy2BMP2 = -$iHeight+(240*$iBotScalingFactor) ;Bottom screen
Else
Global $ix1BMP1 = 0, $ix2BMP1 = 400*$iTopScalingFactor, $iy1BMP1 = -$iHeight, $iy2BMP1 = -$iHeight+(240*$iTopScalingFactor) ;Top screen
EndIf
EndIf
If $iLayoutmode < 4 Then WinSetSize()
If IniRead($sFname, $sSectionName, $aIniSections[24], 1)==1 Then CenterScreens()
Local Const $iCustomWidth = IniRead($sFname, $sSectionName, $aIniSections[18], 0)
Local Const $iCustomHeight = IniRead($sFname, $sSectionName, $aIniSections[19], 0)
If $iCustomWidth >= 150 Then $iWidth = $iCustomWidth
If $iCustomHeight >= 200 Then $iHeight = $iCustomHeight
If $iLayoutmode == 10 Then
Local Const $iCustomWidth2 = IniRead($sFname, $sSectionName, $aIniSections[28], 0)
Local Const $iCustomHeight2 = IniRead($sFname, $sSectionName, $aIniSections[29], 0)
If $iCustomWidth2 >= 150 Then $iWidth2 = $iCustomWidth2
If $iCustomHeight2 >= 200 Then $iHeight2 = $iCustomHeight2
EndIf
Global $g_hGUI = GUICreate($sSectionName & " - Connecting...", $iWidth, $iHeight, $iGUIWidthHeight, $iGUIWidthHeight, $xGUIStyle, $xGUIExStyle) ;Create the GUI
GUISetBkColor(0, $g_hGUI)
;In order to correctly resize the window, we need to get the difference between the graphics object width/height and the actual
;window width/height. This difference is created by the window's borders and title bar
$aWinResize = WinGetPos($g_hGUI)
Global $iWidthDiff = $aWinResize[2] - $iWidth
Global $iHeightDiff = $aWinResize[3] - $iHeight
GUISetState(@SW_SHOW, $g_hGUI)
If $bD2D == True Then ;Init Direct2D
Global $oD2D_Factory = _D2D_Factory1_Create()
Global $tMatrix_Rotate = _D2D_Matrix3x2_Rotation(-90, 0, 0)
Global $oD2D_DeviceContext, $oD2D_SwapChain
_D2D_CreateHWNDContext($oD2D_Factory, $g_hGUI, $oD2D_DeviceContext, $oD2D_SwapChain)
Else ;Init GDI+
_GDIPlus_Startup()
$g_hGraphics = _GDIPlus_GraphicsCreateFromHWND($g_hGUI) ;Create a graphics object from a window handle
If $iLayoutmode < 6 Then
$g_hBitmap = _GDIPlus_BitmapCreateFromGraphics($iWidth, $iHeight, $g_hGraphics)
ElseIf $iLayoutmode == 6 Then
$g_hBitmap = _GDIPlus_BitmapCreateFromGraphics(400, 240, $g_hGraphics)
ElseIf $iLayoutmode == 7 Then
$g_hBitmap = _GDIPlus_BitmapCreateFromGraphics(320, 240, $g_hGraphics)
EndIf
$g_hGfxCtxt = _GDIPlus_ImageGetGraphicsContext($g_hBitmap)
EndIf
If $bUseNTR==True Then
CheckIfStreaming($sIpAddr)
Else
_HzModInit($iSocket, $iImageQuality, $iQoS)
EndIf
If $iLayoutmode == 10 And $bStreaming==True Then ;Create the secondary window if layout mode is separate windows
Global $g_hGUI2 = GUICreate($sSectionName & " - Bottom scr.", $iWidth2, $iHeight2, $iGUIWidthHeight, $iGUIWidthHeight, $xGUIStyle, $xGUIExStyle) ;Create the GUI
GUISetBkColor(0, $g_hGUI2)
GUISetState(@SW_SHOW, $g_hGUI2)
Global $oD2D_DeviceContext2, $oD2D_SwapChain2
_D2D_CreateHWNDContext($oD2D_Factory, $g_hGUI2, $oD2D_DeviceContext2, $oD2D_SwapChain2)
EndIf
Local $tMatrix_Rotate = _D2D_Matrix3x2_Rotation(-90, 120, 120)
If $iTopScalingFactor <> 1 Then $iy1BMP1=$iy1BMP1-$iH3DS*($iTopScalingFactor-1) ;Scaling functions do not take into account the image's rotation
If $iBotScalingFactor <> 1 Then $iy1BMP2=$iy1BMP2-$iH3DS*($iBotScalingFactor-1)
Local $iImgNum = 0, $iSubImgNum = 0, $oTemp, $aSubImages[10], $oBitmap
While $bStreaming == True
Local $iFrameTimer = TimerInit()
If WinActive($g_hGUI) == $g_hGUI Or (IsDeclared($sGUI2) And WinActive($g_hGUI2)==$g_hGUI2) Then CheckKeys() ;Check the keys that are pressed to do various functions
If $bUseNTR==True Then
Local $aJpeg = _NTRRemoteplayReadJPEG($iSocket)
Else
Local $aJpeg = _HzModReadImage($iSocket,$dReadahead)
EndIf
If IsArray($aJpeg) Then
If $bUseNTR==False Then $dReadahead=$aJpeg[2]
$iFPS += 1
If $bD2D == True Then
If $iSubImgNum == 0 Or $iSubImgNum == $iImgNum Then $oBitmap = __D2D_LoadImageFromMemory($aJpeg[1], $aJpeg[0], $bUseNTR) ;Create a D2D image from the image that's stored in memory
If $aJpeg[0] == 1 And ($oBitmap[1] <> $iW3DSTop Or $iImgNum = 0) Then ;HzMod streaming is only supported for the top screen now. I'll have to change this later
$iImgNum = ($iW3DSTop/$oBitmap[1])-1
ReDim $aSubImages[$iImgNum]
EndIf
If $iSubImgNum == $iImgNum Then ;If ALL "sub-frames" have been received and backbuffered it's time to rock!
$oD2D_DeviceContext.BeginDraw()
$oD2D_DeviceContext.SetTransform($tMatrix_Rotate)
If $aJpeg[0] == 1 And $iLayoutmode <> 5 Then
;-240 because we rotate the image of -90 degrees and we've chosen 0,0 as the transformation's origin, so the image falls "on top" of the rendering area
;Also, Width and Height are inverted because the render target is rotated
If $iLayoutmode<>7 And $iLayoutmode<>9 Then
If $oBitmap[1]<400 Then
For $i = 0 To $iImgNum-1
$oTemp = __D2D_LoadImageFromMemory($aSubImages[$i], $aJpeg[0], $bUseNTR) ;Parse them...
$oD2D_DeviceContext.DrawImage($oTemp[0], _D2D1_POINT_2F($iy1BMP1, $ix1BMP1+($oBitmap[1]*$i)*$iTopScalingFactor), _D2D1_RECT_F(0, 0, $iy2BMP1*-1, $oBitmap[1]*$iTopScalingFactor), $iInterpolation, Null) ;...Then copy them to the front buffer and wait until EndDraw!
Next
$oD2D_DeviceContext.DrawImage($oBitmap[0], _D2D1_POINT_2F($iy1BMP1, $ix1BMP1+($oBitmap[1]*$iImgNum)*$iTopScalingFactor), _D2D1_RECT_F(0, 0, $iy2BMP1*-1, $oBitmap[1]*$iTopScalingFactor), $iInterpolation, Null)
Else
$oD2D_DeviceContext.DrawImage($oBitmap[0], _D2D1_POINT_2F($iy1BMP1, $ix1BMP1), _D2D1_RECT_F(0, 0, $iy2BMP1*-1, $ix2BMP1), $iInterpolation, Null)
EndIf
EndIf
If ($iLayoutmode==7 Or $iLayoutmode==9) And $bFullscreenPopup==True Then $oJpegBuf=$oBitmap[0]
ElseIf $aJpeg[0] == 0 And ($iLayoutmode <> 4 And $iLayoutmode <> 10) Then
If $iLayoutmode<>6 And $iLayoutmode<>8 Then $oD2D_DeviceContext.DrawImage($oBitmap[0], _D2D1_POINT_2F($iy1BMP2, $ix1BMP2), _D2D1_RECT_F(0, 0, $iy2BMP2*-1, $ix2BMP2), $iInterpolation, Null)
If ($iLayoutmode==6 Or $iLayoutmode==8) And $bFullscreenPopup==True Then $oJpegBuf=$oBitmap[0]
EndIf
If ($iLayoutmode==6 Or $iLayoutmode==8) And $bFullscreenPopup==True And IsObj($oJpegBuf) Then ;Draw the other screen if fullscreen popup is enabled
$oD2D_DeviceContext.DrawImage($oJpegBuf, _D2D1_POINT_2F($iy1BMP2+($iH3DS*$iBotScalingFactor), $ix1BMP2), _D2D1_RECT_F(0, 0, ($iy2BMP2*-1)+($iH3DS*$iBotScalingFactor), $ix2BMP2), $iInterpolation, Null)
ElseIf ($iLayoutmode==7 Or $iLayoutmode==9) And $bFullscreenPopup==True And IsObj($oJpegBuf) Then
$oD2D_DeviceContext.DrawImage($oJpegBuf, _D2D1_POINT_2F($iy1BMP1+($iH3DS*$iTopScalingFactor), $ix1BMP1), _D2D1_RECT_F(0, 0, ($iy2BMP1*-1)+($iH3DS*$iTopScalingFactor), $ix2BMP1), $iInterpolation, Null)
EndIf
$oD2D_DeviceContext.EndDraw(0, 0)
$oD2D_SwapChain.Present(1, 0)
If $aJpeg[0] == 0 And $iLayoutmode == 10 Then
$oD2D_DeviceContext2.BeginDraw()
$oD2D_DeviceContext2.SetTransform($tMatrix_Rotate)
$oD2D_DeviceContext2.DrawImage($oBitmap[0], _D2D1_POINT_2F($iy1BMP2, $ix1BMP2), _D2D1_RECT_F(0, 0, $iy2BMP2*-1, $ix2BMP2), $iInterpolation, Null)
$oD2D_DeviceContext2.EndDraw(0, 0)
$oD2D_SwapChain2.Present(1, 0)
EndIf
$iSubImgNum = 0
Else
$aSubImages[$iSubImgNum] = $aJpeg[1] ;If more than one "subframe" is detected, backbuffer it
$iSubImgNum += 1
EndIf
Else
DisplayScreenGDIPlus($aJpeg[0], $aJpeg[1])
_GDIPlus_GraphicsDrawImageRect($g_hGraphics, $g_hBitmap, 0, 0, $iWidth, $iHeight) ;Copy drawn bitmap to graphics handle (GUI)
_GDIPlus_GraphicsSetInterpolationMode($g_hGraphics, $iInterpolation) ;We need to set the interpolation mode each frame in case the mode gets changed
EndIf
Local $iRemainingTime = TimerDiff($iFrameTimer) + ((1000 / $iFramelock) - TimerDiff($iFrameTimer))
If $iFramelock > 0 And $aJpeg[0] == $iPriorityMode Then
$iFramelockedFPS += 1
While TimerDiff($iFrameTimer) <= $iRemainingTime ;Nothing (it doesn't use the Sleep function because it's limited to a minimum of 10ms, read the function ref)
WEnd
EndIf
$iTimerWaitExit = TimerInit()
EndIf
If TimerDiff($hTimer) >= 1000 Then
$hTimer = TimerInit()
If $iImgNum<>0 Then $iFPS = Round($iFPS/$iImgNum)
If $iFramelock = 0 Then
$sWinTitle = $sSectionName & " - " & $iFPS & $sFPSString
Else
$sWinTitle = $sSectionName & " - " & $iFPS & $sFPSString & ", screen" & $iPriorityMode & " " & $iFramelockedFPS & $sFPSString
$iFramelockedFPS = 0
EndIf
If $iListenPort <> 8001 Then $sWinTitle &= ", port " & $iListenPort
WinSetTitle($g_hGUI, "", $sWinTitle)
LogLine("FPS:" & $iFPS, 3)
$iFPS = 0
EndIf
If $iReturnAfter > 0 And TimerDiff($iTimerWaitExit) >= $iReturnAfter Then
LogLine("(N)3DS hasn't been sending frames for more than " & $iReturnAfter & "msecs, returning to the connection window.", 1)
ReturnToConnectionWnd()
EndIf
WEnd
EndFunc ;==>StreamingLoop
Func DisplayScreenGDIPlus($iScreen, ByRef $bRawJpeg)
$g_hBMP = _GDIPlus_BitmapCreateFromMemory($bRawJpeg) ;Create a GDI+ Bitmap from the image that's stored in memory
_GDIPlus_ImageRotateFlip($g_hBMP, 3) ;The original image needs to be rotated
If $iScreen == 1 And $iLayoutmode <> 5 And $iLayoutmode <> 7 Then
_GDIPlus_GraphicsDrawImage($g_hGfxCtxt, $g_hBMP, $ix1BMP1, $iy1BMP1 * -1) ;Draw the bitmap to the backbuffer
ElseIf $iScreen = 0 And $iLayoutmode <> 4 And $iLayoutmode <> 6 Then
_GDIPlus_GraphicsDrawImage($g_hGfxCtxt, $g_hBMP, $ix1BMP2, $iy1BMP2 * -1) ;draw bitmap to backbuffer
EndIf
_GDIPlus_BitmapDispose($g_hBMP)
EndFunc ;==>DisplayScreenGDIPlus
Func __D2D_ScaleImageFactor($oScaleEffect, $oImage, $iScreen)
If $iScreen==1 Then
Local $iHorizScale=$iTopScalingFactor, $iVertScale=$iTopScalingFactor
If $iLayoutmode == 6 Then
$iVertScale=@DesktopWidth/$iW3DSTop
$iTopScalingFactor=$iVertScale
$iHorizScale=@DesktopHeight/$iH3DS
EndIf
Else
Local $iHorizScale=$iBotScalingFactor, $iVertScale=$iBotScalingFactor
If $iLayoutmode == 7 Then
$iVertScale=@DesktopWidth/$iW3DSBot
$iHorizScale=@DesktopHeight/$iH3DS
EndIf
EndIf
_D2D_Effect_SetInput($oScaleEffect, $oImage)
_D2D_Properties_SetValue($oScaleEffect, $D2D1_SCALE_PROP_CENTER_POINT, $D2D1_PROPERTY_TYPE_VECTOR2, _D2D1_VECTOR_2F(0, 0))
_D2D_Properties_SetValue($oScaleEffect, $D2D1_SCALE_PROP_INTERPOLATION_MODE, $D2D1_PROPERTY_TYPE_ENUM, _D2D1_UINT($D2D1_SCALE_INTERPOLATION_MODE_NEAREST_NEIGHBOR))
_D2D_Properties_SetValue($oScaleEffect, $D2D1_SCALE_PROP_SCALE, $D2D1_PROPERTY_TYPE_VECTOR2, _D2D1_VECTOR_2F($iHorizScale, $iVertScale))
Return _D2D_Effect_GetOutput($oScaleEffect)
EndFunc
Func __D2D_LoadImageFromMemory($dData, $iScreen, $bSwapRB)
Local $aRet[2]
Local $oWIC_ImagingFactory = _WIC_ImagingFactory_Create()
Local $tImage = DllStructCreate("byte Data[" & BinaryLen($dData) & "]")
$tImage.Data = $dData
Local $oWIC_Stream = _WIC_ImagingFactory_CreateStream($oWIC_ImagingFactory)
$oWIC_Stream.InitializeFromMemory($tImage, DllStructGetSize($tImage))
Local $oWIC_BitmapDecoder = _WIC_ImagingFactory_CreateDecoderFromStream($oWIC_ImagingFactory, $oWIC_Stream)
If @error Then Exit MsgBox(0, "Error", _D2D_ErrorMessage(@error))
Local $oWIC_Bitmap = _WIC_BitmapDecoder_GetFrame($oWIC_BitmapDecoder)
Local $oWIC_FormatConverter = _WIC_ImagingFactory_CreateFormatConverter($oWIC_ImagingFactory)
Local $tGUID_WICPixelFormat32bppPBGRA = _WinAPI_GUIDFromString($sGUID_WICPixelFormat32bppPBGRA)
$oWIC_FormatConverter.Initialize($oWIC_Bitmap, $tGUID_WICPixelFormat32bppPBGRA, 0, 0, 0, 1)
Local $iImgW, $iImgH
$oWIC_FormatConverter.GetSize($iImgW, $iImgH)
$aRet[1] = $iImgH
If $iLayoutmode <> 10 Or $iScreen == 1 Then
Local $oD2D_ColorMatrixEffect = _D2D_DeviceContext_CreateEffect($oD2D_DeviceContext, $sIID_D2D1ColorMatrix)
Local $oD2D_ScaleEffect = _D2D_DeviceContext_CreateEffect($oD2D_DeviceContext, $sIID_D2D1Scale)
Local $oD2D_Bitmap = _D2D_RenderTarget_CreateBitmapFromWicBitmap($oD2D_DeviceContext, $oWIC_FormatConverter)
ElseIf $iLayoutmode == 10 And $iScreen == 0 Then
Local $oD2D_ColorMatrixEffect = _D2D_DeviceContext_CreateEffect($oD2D_DeviceContext2, $sIID_D2D1ColorMatrix)
Local $oD2D_ScaleEffect = _D2D_DeviceContext_CreateEffect($oD2D_DeviceContext2, $sIID_D2D1Scale)
Local $oD2D_Bitmap = _D2D_RenderTarget_CreateBitmapFromWicBitmap($oD2D_DeviceContext2, $oWIC_FormatConverter)
EndIf
_D2D_Effect_SetInput($oD2D_ColorMatrixEffect, $oD2D_Bitmap)
If $bSwapRB == False Then _D2D_Properties_SetValue($oD2D_ColorMatrixEffect, $D2D1_COLORMATRIX_PROP_COLOR_MATRIX, $D2D1_PROPERTY_TYPE_MATRIX_5X4, $structSwapRBMatrix)
If $iTopScalingFactor<>1 Or $iBotScalingFactor<>1 Then
$aRet[0] = __D2D_ScaleImageFactor($oD2D_ScaleEffect, _D2D_Effect_GetOutput($oD2D_ColorMatrixEffect), $iScreen)
Else
$aRet[0] = _D2D_Effect_GetOutput($oD2D_ColorMatrixEffect)
EndIf
Return $aRet
EndFunc ;==>__D2D_LoadImageFromMemory
Func CheckOptimal()
If $bUseNTR==True Then
Local $sMsg1 = "It looks like your remoteplay settings aren't optimal. You should change:" & @CRLF & @CRLF
Local $sMsg2 = @CRLF & "then restart NTR CFW to achieve the best possible performance!" & @CRLF & @CRLF & _
"Click OK to close this message or Cancel if you don't want to be warned about your settings ever again."
Local $bNotOptimal = False
If ($iLayoutmode == 4 Or $iLayoutmode == 6 Or $iLayoutmode == 8) And $iPriorityMode == 0 Then
$sMsg1 = $sMsg1 & "* Priority mode to Top Screen" & @CRLF
$bNotOptimal = True
EndIf
If ($iLayoutmode == 5 Or $iLayoutmode == 7 Or $iLayoutmode == 9) And $iPriorityMode == 1 Then
$sMsg1 = $sMsg1 & "* Priority mode to Bottom Screen" & @CRLF
$bNotOptimal = True
EndIf
If $iPriorityFactor > 0 Then
$sMsg1 = $sMsg1 & "* Priority factor to 0" & @CRLF
$bNotOptimal = True
EndIf
If $bNotOptimal = True And IniRead($sFname, $sSectionName, $aIniSections[10], 0) == 0 Then
Local $iMsgBoxAnswer = MsgBox(49, "Warning!", $sMsg1 & $sMsg2)
Select
Case $iMsgBoxAnswer = 2 ;Cancel selected
IniWrite($sFname, $sSectionName, $aIniSections[10], 1)
EndSelect
EndIf
EndIf
EndFunc ;==>CheckOptimal
Func _CheckPressedOnce($key)
If Not IsDeclared($key & "IsPressed") Then
Assign($key & "IsPressed", False, 2)
EndIf
If _IsPressed($key) And Eval($key & "IsPressed") == False Then
Assign($key & "IsPressed", True, 4)
Return True
ElseIf _IsPressed($key) == False And Eval($key & "IsPressed") == True Then
Assign($key & "IsPressed", False, 4)
Return False
EndIf
EndFunc ;==>_CheckPressedOnce
Func ReturnToConnectionWnd()
LogLine("Returning to the connection window.", 1)
$bStreaming = False
If $bUseNTR==True Then
UDPCloseSocket($iSocket)
UDPShutdown()
Else
TCPCloseSocket($iSocket)
TCPShutdown()
EndIf
CheckAndShutdownGDIp()
GUIDelete($g_hGUI)
If $iLayoutmode == 10 Then GUIDelete($g_hGUI2)
CreateMainGUIandSettings()
EndFunc ;==>ReturnToConnectionWnd
Func CheckKeys() ;Alright, this is as polished as it's going to get.
If _CheckPressedOnce($aHotkeys[0]) == True Then ScaleGUI(1.2, False)
If _CheckPressedOnce($aHotkeys[1]) == True And $iHeight >= 200 Then ScaleGUI(1.2, True)
If _CheckPressedOnce($aHotkeys[2]) == True Then
If $iInterpolation > 0 Then $iInterpolation -= 1
If $bD2D == True Then
LogLine("Interpolation mode changed to " & $sD2D1InterpModeNames[$iInterpolation] & ".", 1)
Else
LogLine("Interpolation mode changed to " & $iInterpolation & ".", 1)
EndIf
EndIf
If _CheckPressedOnce($aHotkeys[3]) == True Then
If $bD2D == True Then
If $iInterpolation < 5 Then $iInterpolation += 1
LogLine("Interpolation mode changed to " & $sD2D1InterpModeNames[$iInterpolation] & ".", 1)
Else
If $iInterpolation < 7 Then $iInterpolation += 1
LogLine("Interpolation mode changed to " & $iInterpolation & ".", 1)
EndIf
EndIf
If _CheckPressedOnce($aHotkeys[4]) == True Then ReturnToConnectionWnd()
If _CheckPressedOnce($aHotkeys[5]) == True Then
CaptureScreenshot($g_hGUI,1)
If IsDeclared($sGUI2) Then CaptureScreenshot($g_hGUI2,0)
EndIf
If _CheckPressedOnce($aHotkeys[6]) == True Then $bFullscreenPopup = Not $bFullscreenPopup
If $bUseNTR == False Then
If _CheckPressedOnce($aHotkeys[8]) == True And $iImageQuality<=99 Then
$iImageQuality+=1
_HzModChangeQuality($iSocket, $iImageQuality)
EndIf
If _CheckPressedOnce($aHotkeys[9]) == True And $iImageQuality>1 Then
$iImageQuality-=1
_HzModChangeQuality($iSocket, $iImageQuality)
EndIf
EndIf
If _IsPressed($aHotkeys[7]) Then ExitStreaming()
EndFunc ;==>CheckKeys
Func CaptureScreenshot($hGUI,$iGUI)
Local $tPoint = DllStructCreate("int X;int Y")
DllStructSetData($tPoint, "X", 0)
DllStructSetData($tPoint, "Y", 0)
_WinAPI_ClientToScreen($hGUI, $tPoint)
Local $iAbsoluteX = DllStructGetData($tPoint, "X")
Local $iAbsoluteY = DllStructGetData($tPoint, "Y")
If $iGUI==1 Then
_ScreenCapture_Capture("screenshot" & @MDAY & @MON & @YEAR & @HOUR & @MIN & @SEC & @MSEC & $iGUI & ".bmp", $iAbsoluteX, $iAbsoluteY, ($iWidth + $iAbsoluteX) - 1, ($iHeight + $iAbsoluteY) - 1, False)
Else
_ScreenCapture_Capture("screenshot" & @MDAY & @MON & @YEAR & @HOUR & @MIN & @SEC & @MSEC & $iGUI & ".bmp", $iAbsoluteX, $iAbsoluteY, ($iWidth2 + $iAbsoluteX) - 1, ($iHeight2 + $iAbsoluteY) - 1, False)
EndIf
EndFunc
Func ScaleGUI($iMultiplier, $bDivision)
If $iLayoutmode < 6 Or $iLayoutmode==10 Then
If $bDivision == False Then
$iWidth = $iWidth * $iMultiplier
$iHeight = $iHeight * $iMultiplier
If IsDeclared($sGUI2) Then
$iWidth2 = $iWidth2 * $iMultiplier
$iHeight2 = $iHeight2 * $iMultiplier
EndIf
Else
$iWidth = $iWidth / $iMultiplier
$iHeight = $iHeight / $iMultiplier
If IsDeclared($sGUI2) Then
$iWidth2 = $iWidth2 / $iMultiplier
$iHeight2 = $iHeight2 / $iMultiplier
EndIf
EndIf
$aWinResize = WinGetPos($g_hGUI)
WinMove($g_hGUI, "", Default, Default, $iWidth + $iWidthDiff, $iHeight + $iHeightDiff)
If IsDeclared($sGUI2) Then
$aWinResize2 = WinGetPos($g_hGUI2)
WinMove($g_hGUI2, "", Default, Default, $iWidth2 + $iWidthDiff, $iHeight2 + $iHeightDiff)
EndIf
If $bD2D == False Then
_GDIPlus_GraphicsDispose($g_hGraphics)
$g_hGraphics = _GDIPlus_GraphicsCreateFromHWND($g_hGUI)
EndIf
LogLine("Window size increased to " & $iWidth & "x" & $iHeight & ".", 1)
EndIf
EndFunc ;==>ScaleGUI
Func CheckPCIP()
If $bNoDisplayIPWarn == 0 And $sPCIpAddr <> "0.0.0.0" Then
Local $iWarnButPressed = MsgBox($MB_ICONWARNING + $MB_YESNO, "Warning!", "You've set a specific IP address for your PC in the configuration file. This isn't needed in most cases and will pro" & _
"bably result in a grey screen when trying to connect to your 3DS." & @CRLF & @CRLF & "Press Yes to revert this setting to the default value and listen to all IPs associated with your comput" & _
"ers (recommended) or No to keep your custom setting and never display this warning anymore.")
If $iWarnButPressed == $IDYES Then
IniWrite($sFname, $sSectionName, $aIniSections[7], "0.0.0.0")
$sPCIpAddr = "0.0.0.0"
ElseIf $iWarnButPressed == $IDNO Then
IniWrite($sFname, $sSectionName, $aIniSections[8], 1)
EndIf
EndIf
EndFunc
Func CreateMainGUIandSettings()
;Check if the config file exists. If not, create one with the default config in it.
If FileExists($sFname) Then
$sIpAddr = IniRead($sFname, $sSectionName, $aIniSections[0], $sIpAddr)
$iPriorityMode = IniRead($sFname, $sSectionName, $aIniSections[1], $iPriorityMode)
$iPriorityFactor = IniRead($sFname, $sSectionName, $aIniSections[2], $iPriorityFactor)
$iImageQuality = IniRead($sFname, $sSectionName, $aIniSections[3], $iImageQuality)
$iQoS = IniRead($sFname, $sSectionName, $aIniSections[4], $iQoS)
$iInterpolation = IniRead($sFname, $sSectionName, $aIniSections[5], $iInterpolation)
$iLayoutmode = IniRead($sFname, $sSectionName, $aIniSections[6], $iLayoutmode)
$sPCIpAddr = IniRead($sFname, $sSectionName, $aIniSections[7], @IPAddress1)
$bNoDisplayIPWarn = IniRead($sFname, $sSectionName, $aIniSections[8], 0)
$iLogLevel = IniRead($sFname, $sSectionName, $aIniSections[9], $iLogLevel)
$bD2D = IniRead($sFname, $sSectionName, $aIniSections[11], $bD2D)
$iListenPort = IniRead($sFname, $sSectionName, $aIniSections[20], $iListenPort)
$bUseNTR = IniRead($sFname, $sSectionName, $aIniSections[26], $bUseNTR)
$iLogConsole = IniRead($sFname, $sSectionName, $aIniSections[27], $iLogConsole)
$bEnableHzMNFCPatch = IniRead($sFname, $sSectionName, $aIniSections[30], $bEnableHzMNFCPatch)
CheckPCIP()
Else
WriteConfig()
;If we use WriteConfig for the first time, the written priority mode value will be wrong. We'll (over)write the right one with
;this small piece of code.
IniWrite($sFname, $sSectionName, $aIniSections[1], Int($iPriorityMode))
EndIf
;This region has been generated by KODA, then changed slightly manually
#Region ### START Koda GUI section ### Form=\gui\snickerstreamgui.kxf
Global $SnickerstreamGUI = GUICreate($sGUITitle, 459, 202)
Local $Label1 = GUICtrlCreateLabel("IP", 24, 26, 14, 17, $SS_CENTERIMAGE)
Local $Label2 = GUICtrlCreateLabel("Screen priority", 24, 52, 71, 17, $SS_CENTERIMAGE)
Local $Label3 = GUICtrlCreateLabel("Priority factor", 24, 82, 65, 17, $SS_CENTERIMAGE)
Local $Label4 = GUICtrlCreateLabel("Image quality", 24, 106, 66, 17, $SS_CENTERIMAGE)
Global $Label5 = GUICtrlCreateLabel("QoS Value", 24, 130, 85, 17, $SS_CENTERIMAGE)
Global $GUI_IpAddr = _GUICtrlIpAddress_Create($SnickerstreamGUI, 104, 24, 105, 17)
_GUICtrlIpAddress_Set($GUI_IpAddr, "0.0.0.0")
Global $GUI_PriorityMode = GUICtrlCreateCombo("", 104, 50, 105, 25, BitOR($CBS_DROPDOWNLIST, $CBS_AUTOHSCROLL))
GUICtrlSetData(-1, "Top screen|Bottom screen", "Top screen")
Global $GUI_PriorityFactor = GUICtrlCreateInput("5", 104, 80, 105, 21, BitOR($GUI_SS_DEFAULT_INPUT, $ES_NUMBER))
Global $GUI_ImageQuality = GUICtrlCreateInput("90", 104, 104, 105, 21, BitOR($GUI_SS_DEFAULT_INPUT, $ES_NUMBER))
Global $GUI_QoS = GUICtrlCreateInput("20", 104, 128, 105, 21, BitOR($GUI_SS_DEFAULT_INPUT, $ES_NUMBER))
Local $Label6 = GUICtrlCreateLabel("Interpolation", 245, 56, 62, 17, $SS_CENTERIMAGE)
Global $GUI_Interpolation = GUICtrlCreateCombo("", 321, 54, 113, 25, BitOR($CBS_DROPDOWNLIST, $CBS_AUTOHSCROLL))
GUICtrlSetData(-1, _ArrayToString($sD2D1InterpModeNames), $sD2D1InterpModeNames[0])
Local $Label7 = GUICtrlCreateLabel("Screen layout", 245, 80, 69, 17, $SS_CENTERIMAGE)
Global $GUI_Layoutmode = GUICtrlCreateCombo("", 321, 80, 113, 25, BitOR($CBS_DROPDOWNLIST, $CBS_AUTOHSCROLL))
If $bUseNTR == True Then
GUICtrlSetData(-1, "Vertical|Vertical(inverted)|Horizontal|Horizontal(inverted)|Top screen only|Bottom screen only|Fullscreen(Top,st.)|Fullscreen(Bot,st.)|Fullscreen(Top)|Fullscreen(Bot)|Separate windows", "Vertical")
Else
GUICtrlSetData(-1, "Top screen only|Fullscreen(Top,st.)|Fullscreen(Top)", "Top screen only")
EndIf
Global $GUI_ConnectBtn = GUICtrlCreateButton("Connect!", 232, 160, 217, 33)
Global $GUI_NFCBtn = GUICtrlCreateButton("NFC patch", 384, 128, 65, 25)
Local $Group1 = GUICtrlCreateGroup("Remoteplay settings", 8, 8, 217, 185)
Global $GUI_Preset = GUICtrlCreateCombo("", 103, 160, 105, 25, BitOR($CBS_DROPDOWNLIST, $CBS_AUTOHSCROLL))
LoadPresets()
GUICtrlSetOnEvent(-1, "GUI_PresetChange")
GUICtrlCreateGroup("", -99, -99, 1, 1)
Local $Group2 = GUICtrlCreateGroup("Other settings", 232, 8, 217, 105)
GUICtrlCreateGroup("", -99, -99, 1, 1)
Global $GUI_AboutBtn = GUICtrlCreateButton("About", 232, 128, 65, 25)
Local $Label8 = GUICtrlCreateLabel("Streaming app", 245, 32, 70, 17, $SS_CENTERIMAGE)
Global $GUI_Streaming = GUICtrlCreateCombo("", 322, 27, 113, 25, BitOR($CBS_DROPDOWNLIST, $CBS_AUTOHSCROLL))
GUICtrlSetData(-1, "NTR CFW|HzMod", "NTR CFW")
If $bUseNTR == False Then _GUICtrlComboBox_SetCurSel($GUI_Streaming, 1)
GUICtrlSetOnEvent(-1, "GUI_StreamingAppChange")
Local $Label9 = GUICtrlCreateLabel("Preset", 25, 162, 34, 17, $SS_CENTERIMAGE)
Global $GUI_AdvButton = GUICtrlCreateButton("Advanced", 304, 128, 73, 25)
GUISetState(@SW_SHOW)
#EndRegion ### END Koda GUI section ###
;Set the limit for the inputs
GUICtrlSetLimit($GUI_PriorityFactor, 3)
GUICtrlSetLimit($GUI_ImageQuality, 3)
GUICtrlSetLimit($GUI_QoS, 3)
;This is needed in case the config file was loaded so it'll be shown correctly
_GUICtrlIpAddress_Set($GUI_IpAddr, $sIpAddr)
_GUICtrlComboBox_SetCurSel($GUI_PriorityMode, $iPriorityMode)
GUICtrlSetData($GUI_PriorityFactor, $iPriorityFactor)
GUICtrlSetData($GUI_ImageQuality, $iImageQuality)
GUICtrlSetData($GUI_QoS, $iQoS)
If $bD2D == True And $iInterpolation > 6 Then ;Failsafe in case we get an invalid interpolation mode for D2D
$iInterpolation = 0
EndIf
_GUICtrlComboBox_SetCurSel($GUI_Interpolation, $iInterpolation)
_GUICtrlComboBox_SetCurSel($GUI_Layoutmode, $iLayoutmode)
GUISetOnEvent($GUI_EVENT_CLOSE, "ExitMain", $SnickerstreamGUI)
GUICtrlSetOnEvent($GUI_AboutBtn, "AboutScreen")
GUICtrlSetOnEvent($GUI_NFCBtn, "SendNFC")
GUICtrlSetOnEvent($GUI_ConnectBtn, "StartStream")
GUICtrlSetOnEvent($GUI_AdvButton, "AdvMenu")
If $bUseNTR == False Then SettingsStateSet($GUI_DISABLE)
EndFunc ;==>CreateMainGUIandSettings
Func UpdateVars()
;We need to call this function when the user clicked on connect/saving presets to correctly update the variables
$sIpAddr = _GUICtrlIpAddress_Get($GUI_IpAddr)
$iPriorityMode = Int(Not _GUICtrlComboBox_GetCurSel($GUI_PriorityMode))
$iPriorityFactor = Int(GUICtrlRead($GUI_PriorityFactor))
$iImageQuality = Int(GUICtrlRead($GUI_ImageQuality))
If $iImageQuality < 10 And $bUseNTR == True Then $iImageQuality = 10
$iQoS = Int(GUICtrlRead($GUI_QoS))
$iInterpolation = Int(_GUICtrlComboBox_GetCurSel($GUI_Interpolation))
$iLayoutmode = Int(_GUICtrlComboBox_GetCurSel($GUI_Layoutmode))
EndFunc ;==>UpdateVars
Func WriteConfig()
IniWrite($sFname, $sSectionName, $aIniSections[0], $sIpAddr)
IniWrite($sFname, $sSectionName, $aIniSections[1], Int(Not $iPriorityMode))
IniWrite($sFname, $sSectionName, $aIniSections[2], $iPriorityFactor)
IniWrite($sFname, $sSectionName, $aIniSections[3], $iImageQuality)
IniWrite($sFname, $sSectionName, $aIniSections[4], $iQoS)
IniWrite($sFname, $sSectionName, $aIniSections[5], $iInterpolation)
IniWrite($sFname, $sSectionName, $aIniSections[6], $iLayoutmode)
IniWrite($sFname, $sSectionName, $aIniSections[7], $sPCIpAddr)
IniWrite($sFname, $sSectionName, $aIniSections[9], $iLogLevel)
IniWrite($sFname, $sSectionName, $aIniSections[26], $bUseNTR)
IniWrite($sFname, $sSectionName, $aIniSections[27], $iLogConsole)
If IniRead($sFname, $sSectionName, $aIniSections[14], -1) < 0 Then IniWrite($sFname, $sSectionName, $aIniSections[14], 1000)
IniWrite($sFname, $sSectionName, $aIniSections[20], $iListenPort)
If IniRead($sFname, $sSectionName, $aIniSections[25], 0)==0 Then IniWrite($sFname, $sSectionName, $aIniSections[25], _ArrayToString($aHotkeys,$sSettingSeparator))
EndFunc ;==>WriteConfig
Func SendNFC()
ButtonStateSet($GUI_DISABLE)
If Not IsDeclared("iMsgBoxAnswer") Then Local $iMsgBoxAnswer = IniRead($sFname, $sSectionName, $aIniSections[13], -1)
If $iMsgBoxAnswer == -1 Then $iMsgBoxAnswer = MsgBox(67, "Choose 3DS firmware version", "Yes -> Firmware >= 11.4" & @CRLF & "No -> Firmware <= 11.3" & @CRLF & _
"Cancel -> Go back" & @CRLF & @CRLF & "Snickerstream will remember your answer so you won't see this message again.")
Select
Case $iMsgBoxAnswer = 6 ;Yes
If $bUseNTR == True Then
Local $iRet = _NTRSendNFCPatch($sIpAddr, "005b10") ;Offset must be in little-endian
Else
Local $iRet = _HzModSendNFCPatch($sIpAddr, "005b10")
EndIf
IniWrite($sFname, $sSectionName, $aIniSections[13], 6)
LogLine("Sending NFC Patch (>=11.4).", 1)
Case $iMsgBoxAnswer = 7 ;No
If $bUseNTR == True Then
Local $iRet = _NTRSendNFCPatch($sIpAddr, "e45a10") ;Same as above
Else
Local $iRet = _HzModSendNFCPatch($sIpAddr, "e45a10")
EndIf
IniWrite($sFname, $sSectionName, $aIniSections[13], 7)
LogLine("Sending NFC Patch (<=11.3).", 1)
EndSelect
If $iMsgBoxAnswer >= 6 Then
If $iRet = -1 Then
LogLine("NFC patch failed, could not connect.", 1)
MsgBox($MB_ICONERROR, "Connection error", "Could not connect to the (N)3DS.")
Else
LogLine("NFC patch sent succesfully.", 1)
MsgBox($MB_ICONINFORMATION, "Success!", "NFC patch sent succesfully!")
EndIf
EndIf
ButtonStateSet($GUI_ENABLE)
EndFunc ;==>SendNFC
Func StartStream()
UpdateVars()
WriteConfig()
If $AdvancedMenu <> 0 Then ExitAdvanced()
GUIDelete($SnickerstreamGUI)
$bStreaming = True
StreamingLoop()
EndFunc ;==>StartStream
Func ButtonStateSet($GUIState)
GUICtrlSetState($GUI_AboutBtn, $GUIState)
GUICtrlSetState($GUI_NFCBtn, $GUIState)
GUICtrlSetState($GUI_ConnectBtn, $GUIState)
GUICtrlSetState($GUI_AdvButton, $GUIState)
EndFunc ;==>ButtonStateSet
Func ExitMain()
GUIDelete($SnickerstreamGUI)
If IsDeclared("oD2D_DeviceContext") <> 0 Then
ExitStreaming()
EndIf
LogLine("Quitting.", 1)
FileDelete($sIconPath)
Exit
EndFunc ;==>ExitMain
Func CheckAndShutdownGDIp()
If $bD2D == False Then
_GDIPlus_GraphicsDispose($g_hGfxCtxt)
_GDIPlus_GraphicsDispose($g_hGraphics)
_GDIPlus_BitmapDispose($g_hBitmap)
_GDIPlus_BitmapDispose($g_hBMP)
_GDIPlus_BitmapDispose($g_hBMP2)
_GDIPlus_Shutdown()
EndIf
EndFunc ;==>CheckAndShutdownGDIp
Func ExitStreaming()
Global $bStreaming = False
LogLine("Quitting.", 1)
;Close the UDP socket
If $bUseNTR==True Then
UDPCloseSocket($iSocket)
UDPShutdown()
Else
TCPCloseSocket($iSocket)
TCPShutdown()
EndIf
CheckAndShutdownGDIp()
GUIDelete($g_hGUI)
Exit
EndFunc ;==>ExitStreaming
Func LogStart()
Global $hConsole = -1
If $iLogLevel > 0 Then
If $iLogConsole == 0 Or $iLogConsole == 2 Then
Local $hFile = FileOpen($sLogFname, $FO_OVERWRITE)
FileWriteLine($hFile, " ___ _ _ _")
FileWriteLine($hFile, " / __|_ _ (_)__| |_____ _ _ __| |_ _ _ ___ __ _ _ __")
FileWriteLine($hFile, " \__ \ ' \| / _| / / -_) '_(_-< _| '_/ -_) _` | ' \")
FileWriteLine($hFile, " |___/_||_|_\__|_\_\___|_| /__/\__|_| \___\__,_|_|_|_|")
FileWriteLine($hFile, _StringRepeat("-", 113))
If $iLogLevel <= 3 Then
Switch Random(0, 11, 1)
Case 0
FileWriteLine($hFile, "it hac, it bric, but most importantly, it S N I C C")
Case 1
FileWriteLine($hFile, "*whispers* AL9H.")
Case 2
FileWriteLine($hFile, "Just spaghetti code on the rocks. *chuckle*")
Case 3
FileWriteLine($hFile, "One more way to get copyright striked by Nintendo.")
Case 4
FileWriteLine($hFile, "What are you waiting for, Nintendo to make one?")
Case 5
FileWriteLine($hFile, "Here's your christmas present! <3")
Case 6
FileWriteLine($hFile, "* The dog absorbs the stream.")
Case 7
FileWriteLine($hFile, "As smooth as FMVs on a Sega CD.")
Case 8
FileWriteLine($hFile, "[stability intensifies]")
Case 9
FileWriteLine($hFile, "Now with 500% more icons!")
Case 10
FileWriteLine($hFile, "For the glory of framekind.")
Case 11
FileWriteLine($hFile, "If loglevel 3 is so great, why isn't there loglevel 4?")
EndSwitch
Else
If $iLogLevel == 4 Then
FileWriteLine($hFile, "I was joking about loglevel 4, you know.")
Else
FileWriteLine($hFile, "That's it. We're not friends anymore until you set loglevel back to something valid >_>")
EndIf
EndIf
FileWriteLine($hFile, _StringRepeat("-", 113))
FileWriteLine($hFile, "VERSION : " & $sVersion)
FileWriteLine($hFile, "LOGLEVEL: " & $iLogLevel)
FileWriteLine($hFile, _StringRepeat("-", 113))
FileClose($hFile)
EndIf
If $iLogConsole > 0 Then ConsoleInit()
LogLine("Logging started.", 1)
EndIf
EndFunc ;==>LogStart
Func LogLine($sLogString, $iReqLogLevel)
If $iLogLevel >= $iReqLogLevel Then
If $iLogConsole > 0 Then ConsoleLogLine($sLogString)
If $iLogConsole == 0 Or $iLogConsole == 2 Then
Local $hFile = FileOpen($sLogFname, $FO_APPEND)
FileWriteLine($hFile, "[" & @HOUR & ":" & @MIN & "] " & $sLogString)
FileClose($hFile)
EndIf
EndIf
EndFunc ;==>LogLine
Func ConsoleInit()
Global $hK32dll = DllOpen("kernel32.dll")
$hConsole = -1
DllCall($hK32dll, "bool", "AllocConsole")
DllCall($hK32dll, "bool", "AttachConsole", "dword", -1)
If @Compiled Then
Local $aRet = DllCall($hK32dll, "handle", "GetStdHandle", "dword", -11)
If IsArray($aRet) Then
$hConsole = $aRet[0]
EndIf
Else
ConsoleLogLine("Script is not compiled or console window could not be opened. Redirecting console output to SciTE log.")
EndIf
EndFunc
Func ConsoleLogLine($sText)
If $hConsole<>-1 And $hConsole<>0 Then
DllCall($hK32dll, "bool", "WriteConsoleA", "handle", $hConsole, "str", $sText&@CRLF, "dword", StringLen($sText)+2, "dword*", 0, "ptr", 0)
Else
ConsoleWrite($sText&@CRLF)
EndIf
EndFunc
Func CheckIfStreaming($sRemoteIP)
Local $hTimerTimeout = TimerInit()
Global $bRecieved = False
Local $bChecked = False
Local Const $iTimeout = IniRead($sFname, $sSectionName, $aIniSections[14], 1000)
Local Const $sConnectionError = "Connection error"
While $bRecieved == False
Local $dRecv = UDPRecv($iSocket, 2000, 1)
If TimerDiff($hTimerTimeout) >= $iTimeout And $bChecked == False Then
WinSetTitle($g_hGUI, "", $sSectionName & " - Starting remoteplay...")
LogLine("Starting remoteplay on 3DS.", 1)
Local $iRet = _NTRInitRemoteplay($sRemoteIP, $iPriorityMode, $iPriorityFactor, $iImageQuality, $iQoS)
$hTimerTimeout = TimerInit() ;The console is confirmed to be online, reset the timer for later use
If $iRet = -1 Then
LogLine("Remoteplay init failed, could not connect.", 1)
MsgBox($MB_ICONERROR, $sConnectionError, "Could not start remoteplay on the (N)3DS.")
ExitOnStreamFail()