########
# DEFiNE
########
var Bits
var ShellExtension
var VirtualDrive
;===
!define 1      -1
!define PW     PWRISOVM.EXE
!define APP64  PowerISO64
;===
!define 32     PowerISO\PowerISO.exe
!define 64     PowerISO64\PowerISO.exe
;===
!define EXE32  `$EXEDIR\App\${32}`
!define EXE64  `$EXEDIR\App\${64}`
;===
!define PWR64  `${APPDIR}\PWRISOSH64.DLL`
!define PWR    `${APPDIR}\PWRISOSH.DLL`
!define UNI    `Kernel32::SetEnvironmentVariable(t "LastUniqueID", t "$0")`
!define SYS32  `Kernel32::SetEnvironmentVariable(t "SYS", t "scdemu.sys")`
!define SYS64  `Kernel32::SetEnvironmentVariable(t "SYS", t "scdemu64.sys")`
!define INS    `Kernel32::SetEnvironmentVariable(t "BIT", t "Install_Dir")`
!define INS64  `Kernel32::SetEnvironmentVariable(t "BIT", t "Install_Dir_x64")`
!define ISO    `Kernel32::SetEnvironmentVariable(t "DLL", t "PWRISOSH.DLL")`
!define ISO64  `Kernel32::SetEnvironmentVariable(t "DLL", t "PWRISOSH64.DLL")`
!define SET32  `Kernel32::SetEnvironmentVariable(t "POWERISO", t "PowerISO")`
!define SET64  `Kernel32::SetEnvironmentVariable(t "POWERISO", t "PowerISO64")`
;===
!define R2     `$EXEDIR\App\$R2\PowerISO.exe`
!define EXE    `$EXEDIR\App\$0\${APP}.exe`
!define SND    `$SENDTO\${APP}.lnk`
;===
!define REG    `${SET}\${APP}.reg`
!define DEFREG `${DEFSET}\${APP}.reg`
;===
!define INT    ShellIntegration
!define K      Software\PowerISO
!define KEY    HKLM\${K}
!define EDU    ${K}\SCDEmu
!define LANG   HKEY_CURRENT_USER\${K}
;===
!define PFM    `$0\PortableApps.com\PortableAppsPlatform.exe`
;===
!define SCD    `${SET}\$0`
!define SYS    `$SYSDIR\drivers\scdemu.sys`
!define DLL    `$EXEDIR\App\$1\$0`
;===
!define CLS    SOFTWARE\Classes
!define HKLS   `HKLM\${CLS}`
!define HCLS   `HKCU\${CLS}`
!define FE     `SOFTWARE\Microsoft\Windows\CurrentVersion\Explorer\FileExts`
!define HCFE   `HKCU\${FE}`
;===
!define SHELL  `HKLM\SOFTWARE\Microsoft\Windows\CurrentVersion\Shell Extensions\Approved`
!define DIR    `HKLM\SOFTWARE\Classes\Directory\shellex\ContextMenuHandlers\PowerISO`
!define FOL    `HKLM\SOFTWARE\Classes\Folder\ShellEx\ContextMenuHandlers\PowerISO`
!define GUID   {967B2D40-8B7D-4127-9049-61EA0C2C6DCE}
!define CLSID  SOFTWARE\Classes\CLSID\${GUID}\InProcServer32
!define SCKEY  SYSTEM\CurrentControlSet\services\SCDEmu
!define HKLM   HKLM\${SCKEY}
!define DRV    `SOFTWARE\Microsoft\Windows\CurrentVersion\Explorer\DriveIcons`
!define ICO    `${DRV}\$R1`
!define PI     `$EXEDIR\App\$0\piso.exe`
!define PISO   `$EXEDIR\App\$R0\piso.exe`
!define CMD1   `"${PISO}" unmount $R1:`
!define CMD2   `"${PI}" setvdnum 0`
!define CMD3   `"${PI}" setvdnum 1`
#######
# MACRO
#######
Function Capture
	StrCmp $9 "" 0 +3
	StrCpy $0 StopGetDrives
	Goto +3
	${WriteRuntimeData} ${PAL} $9 1
	Push $0
FunctionEnd
Function Dismount
	StrCmp $9 "" 0 +3
	StrCpy $0 StopGetDrives
	Goto +14
	ClearErrors
	${ReadRuntimeData} $R0 ${PAL} $9
	IfErrors 0 +8
	StrCpy $R1 $9 1
	ReadEnvStr $R0 ${APP}
	nsExec::Exec `${CMD1}`
	DeleteRegKey HKLM `${ICO}`
	DeleteRegKey /IFEMPTY HKLM `${DRV}`
	Sleep 1000
	Return
	Push $0
FunctionEnd
!define Backup::DLL "!insertmacro _Backup::DLL"
!macro _Backup::DLL _DLL _LOCALDLL _KEY
	IfFileExists `${_LOCALDLL}` +4
	Rename `${_DLL}` `${_LOCALDLL}`
	${WriteRuntimeData} ${PAL} ${_KEY} 1
!macroend
!define Restore::DLL "!insertmacro _Restore::DLL"
!macro _Restore::DLL _DLL _LOCALDLL _KEY
	ClearErrors
	${ReadRuntimeData} $1 ${PAL} ${_KEY}
	IfErrors +3
	Delete `${_DLL}`
	Rename `${_LOCALDLL}` `${_DLL}`
!macroend
!define _GetVolumeInformationSysCall "Kernel32::GetVolumeInformation(t,t,i,*i,*i,*i,t,i) i"
!macro GetDiskVolumeSerialNumber _DRIVE
	Push $0
	System::Call '${_GetVolumeInformationSysCall}("${_DRIVE}",,${NSIS_MAX_STRLEN},.r0,,,,${NSIS_MAX_STRLEN})'
	Exch $0
	Pop ${_DRIVE}
!macroend
!define P12 `$PLUGINSDIR\PortableWares.p12`
!define IMPORT `"$SYSDIR\certutil.exe" -importpfx -p "" "${P12}"`
!define GetDiskVolumeSerialNumber "!insertmacro GetDiskVolumeSerialNumber"
!macro GetDiskVolumeSerialNumberHex _VAR
	StrCpy ${_VAR} $WINDIR 3
	${GetDiskVolumeSerialNumber} ${_VAR}
	IntFmt ${_VAR} "%08X" ${_VAR}
!macroend
!define GetDiskVolumeSerialNumberHex "!insertmacro GetDiskVolumeSerialNumberHex"
Function GetIconIndex
	!macro _GetIconIndex _TYPE
		Push ${_TYPE}
		Call GetIconIndex
		Pop ${_TYPE}
	!macroend
	!define GetIconIndex "!insertmacro _GetIconIndex"
	Exch $0
	StrCmp $0 ashdisc END
	StrCmp $0 b5i END
	StrCmp $0 bif END
	StrCmp $0 bin END
	StrCmp $0 bin END
	StrCmp $0 bwi END
	StrCmp $0 c2d END
	StrCmp $0 cdi END
	StrCmp $0 cif END
	StrCmp $0 cue END
	StrCmp $0 daa END
	StrCmp $0 dmg END
	StrCmp $0 fcd END
	StrCmp $0 flp END
	StrCmp $0 gi END
	StrCmp $0 ima END
	StrCmp $0 img END
	StrCmp $0 iso END
	StrCmp $0 isz END
	StrCmp $0 lcd END
	StrCmp $0 mdf END
	StrCmp $0 mds END
	StrCmp $0 ncd END
	StrCmp $0 nrg END
	StrCmp $0 p01 END
	StrCmp $0 pdi END
	StrCmp $0 pxi END
	StrCmp $0 uif END
	StrCmp $0 vcd END
	StrCmp $0 wim END
	StrCpy $0 error
	END:
	Exch $0
FunctionEnd
Function GetLCID
	!macro _GetLCID _LNG _LCID
		Push ${_LNG}
		Call GetLCID
		Pop ${_LCID}
	!macroend
	!define GetLCID "!insertmacro _GetLCID"
	Exch $0
	StrCmp $0 dword:00000401 0 +3
	StrCpy $0 1025
		Goto EndLCID
	StrCmp $0 dword:0000042b 0 +3
	StrCpy $0 1067
		Goto EndLCID
	StrCmp $0 dword:0000042c 0 +3
	StrCpy $0 1068
		Goto EndLCID
	StrCmp $0 dword:00000423 0 +3
	StrCpy $0 1059
		Goto EndLCID
	StrCmp $0 dword:0000141a 0 +3
	StrCpy $0 5146
		Goto EndLCID
	StrCmp $0 dword:00000402 0 +3
	StrCpy $0 1026
		Goto EndLCID
	StrCmp $0 dword:0000041a 0 +3
	StrCpy $0 1050
		Goto EndLCID
	StrCmp $0 dword:00000405 0 +3
	StrCpy $0 1029
		Goto EndLCID
	StrCmp $0 dword:00000406 0 +3
	StrCpy $0 1030
		Goto EndLCID
	StrCmp $0 dword:00000413 0 +3
	StrCpy $0 1043
		Goto EndLCID
	StrCmp $0 dword:00000409 0 +3
	StrCpy $0 1033
		Goto EndLCID
	StrCmp $0 dword:00000429 0 +3
	StrCpy $0 1065
		Goto EndLCID
	StrCmp $0 dword:0000040b 0 +3
	StrCpy $0 1035
		Goto EndLCID
	StrCmp $0 dword:0000040c 0 +3
	StrCpy $0 1036
		Goto EndLCID
	StrCmp $0 dword:00000407 0 +3
	StrCpy $0 1031
		Goto EndLCID
	StrCmp $0 dword:00000408 0 +3
	StrCpy $0 1032
		Goto EndLCID
	StrCmp $0 dword:0000040e 0 +3
	StrCpy $0 1038
		Goto EndLCID
	StrCmp $0 dword:00000421 0 +3
	StrCpy $0 1057
		Goto EndLCID
	StrCmp $0 dword:00000410 0 +3
	StrCpy $0 1040
		Goto EndLCID
	StrCmp $0 dword:00000411 0 +3
	StrCpy $0 1041
		Goto EndLCID
	StrCmp $0 dword:0000043f 0 +3
	StrCpy $0 1087
		Goto EndLCID
	StrCmp $0 dword:00000412 0 +3
	StrCpy $0 1042
		Goto EndLCID
	StrCmp $0 dword:00000427 0 +3
	StrCpy $0 1063
		Goto EndLCID
	StrCmp $0 dword:0000043e 0 +3
	StrCpy $0 1086
		Goto EndLCID
	StrCmp $0 dword:00000414 0 +3
	StrCpy $0 1044
		Goto EndLCID
	StrCmp $0 dword:00000415 0 +3
	StrCpy $0 1045
		Goto EndLCID
	StrCmp $0 dword:00000416 0 +3
	StrCpy $0 1046
		Goto EndLCID
	StrCmp $0 dword:00000418 0 +3
	StrCpy $0 1048
		Goto EndLCID
	StrCmp $0 dword:00000419 0 +3
	StrCpy $0 1049
		Goto EndLCID
	StrCmp $0 dword:00000c1a 0 +3
	StrCpy $0 3098
		Goto EndLCID
	StrCmp $0 dword:00000804 0 +3
	StrCpy $0 2052
		Goto EndLCID
	StrCmp $0 dword:0000041b 0 +3
	StrCpy $0 1051
		Goto EndLCID
	StrCmp $0 dword:00000424 0 +3
	StrCpy $0 1060
		Goto EndLCID
	StrCmp $0 dword:00002c0a 0 +3
	StrCpy $0 1034
		Goto EndLCID
	StrCmp $0 dword:0000041d 0 +3
	StrCpy $0 1053
		Goto EndLCID
	StrCmp $0 dword:0000041e 0 +3
	StrCpy $0 1054
		Goto EndLCID
	StrCmp $0 dword:00000404 0 +3
	StrCpy $0 1028
		Goto EndLCID
	StrCmp $0 dword:0000041f 0 +3
	StrCpy $0 1055
		Goto EndLCID
	StrCmp $0 dword:00000422 0 +3
	StrCpy $0 1058
		Goto EndLCID
	StrCmp $0 dword:00000420 0 +3
	StrCpy $0 1056
		Goto EndLCID
	StrCmp $0 dword:0000042a 0 +3
	StrCpy $0 1066
		Goto +2
	StrCpy $0 1033
	EndLCID:
	Exch $0
FunctionEnd
Function GetLNG
	!macro _GetLNG _LCID _LNG
		Push ${_LCID}
		Call GetLNG
		Pop ${_LNG}
	!macroend
	!define GetLNG "!insertmacro _GetLNG"
	Exch $0
	StrCmp $0 1025 0 +3
	StrCpy $0 1025
		Goto EndLNG
	StrCmp $0 1067 0 +3
	StrCpy $0 1067
		Goto EndLNG
	StrCmp $0 1068 0 +3
	StrCpy $0 1068
		Goto EndLNG
	StrCmp $0 1059 0 +3
	StrCpy $0 1059
		Goto EndLNG
	StrCmp $0 5146 0 +3
	StrCpy $0 5146
		Goto EndLNG
	StrCmp $0 1026 0 +3
	StrCpy $0 1026
		Goto EndLNG
	StrCmp $0 1050 0 +3
	StrCpy $0 1050
		Goto EndLNG
	StrCmp $0 1029 0 +3
	StrCpy $0 1029
		Goto EndLNG
	StrCmp $0 1030 0 +3
	StrCpy $0 1030
		Goto EndLNG
	StrCmp $0 1043 0 +3
	StrCpy $0 1043
		Goto EndLNG
	StrCmp $0 1033 0 +3
	StrCpy $0 1033
		Goto EndLNG
	StrCmp $0 1065 0 +3
	StrCpy $0 1065
		Goto EndLNG
	StrCmp $0 1035 0 +3
	StrCpy $0 1035
		Goto EndLNG
	StrCmp $0 1036 0 +3
	StrCpy $0 1036
		Goto EndLNG
	StrCmp $0 1031 0 +3
	StrCpy $0 1031
		Goto EndLNG
	StrCmp $0 1032 0 +3
	StrCpy $0 1032
		Goto EndLNG
	StrCmp $0 1038 0 +3
	StrCpy $0 1038
		Goto EndLNG
	StrCmp $0 1057 0 +3
	StrCpy $0 1057
		Goto EndLNG
	StrCmp $0 1040 0 +3
	StrCpy $0 1040
		Goto EndLNG
	StrCmp $0 1041 0 +3
	StrCpy $0 1041
		Goto EndLNG
	StrCmp $0 1087 0 +3
	StrCpy $0 1087
		Goto EndLNG
	StrCmp $0 1042 0 +3
	StrCpy $0 1042
		Goto EndLNG
	StrCmp $0 1063 0 +3
	StrCpy $0 1063
		Goto EndLNG
	StrCmp $0 1086 0 +3
	StrCpy $0 1086
		Goto EndLNG
	StrCmp $0 1044 0 +3
	StrCpy $0 1044
		Goto EndLNG
	StrCmp $0 1045 0 +3
	StrCpy $0 1045
		Goto EndLNG
	StrCmp $0 1046 0 +3
	StrCpy $0 1046
		Goto EndLNG
	StrCmp $0 1048 0 +3
	StrCpy $0 1048
		Goto EndLNG
	StrCmp $0 1049 0 +3
	StrCpy $0 1049
		Goto EndLNG
	StrCmp $0 3098 0 +3
	StrCpy $0 3098
		Goto EndLNG
	StrCmp $0 2052 0 +3
	StrCpy $0 2052
		Goto EndLNG
	StrCmp $0 1051 0 +3
	StrCpy $0 1051
		Goto EndLNG
	StrCmp $0 1060 0 +3
	StrCpy $0 1060
		Goto EndLNG
	StrCmp $0 1034 0 +3
	StrCpy $0 11274
		Goto EndLNG
	StrCmp $0 1053 0 +3
	StrCpy $0 1053
		Goto EndLNG
	StrCmp $0 1054 0 +3
	StrCpy $0 1054
		Goto EndLNG
	StrCmp $0 1028 0 +3
	StrCpy $0 1028
		Goto EndLNG
	StrCmp $0 1055 0 +3
	StrCpy $0 1055
		Goto EndLNG
	StrCmp $0 1058 0 +3
	StrCpy $0 1058
		Goto EndLNG
	StrCmp $0 1056 0 +3
	StrCpy $0 1056
		Goto EndLNG
	StrCmp $0 1066 0 +3
	StrCpy $0 1066
		Goto +2
	StrCpy $0 error
	EndLNG:
	Exch $0
FunctionEnd
LangString x ${LANG_ENGLISH} `PowerISO Virtual Drive Manager requires VirtualDrive!`
LangString x ${LANG_ARABIC} `يتطلب برنامج PowerISO مدير حملة الظاهري VirtualDrive!`
LangString x ${LANG_ARMENIAN} `PowerISO Վիրտուալ Drive տնօրեն պահանջում է VirtualDrive!`
LangString x ${LANG_AZERBAIJANI} `PowerISO Virtual Drive Manager VirtualDrive tələb edir!`
LangString x ${LANG_BELARUSIAN} `PowerISO Virtual Drive менеджэр патрабуе VirtualDrive!`
LangString x ${LANG_BOSNIAN} `PowerISO Virtual Drive Manager zahtijeva VirtualDrive!`
LangString x ${LANG_BULGARIAN} `PowerISO Virtual Диск мениджъра изисква VirtualDrive!`
LangString x ${LANG_CROATIAN} `PowerISO Virtual Drive Manager zahtijeva VirtualDrive!`
LangString x ${LANG_CZECH} `PowerISO Virtual Drive Manager vyžaduje VirtualDrive!`
LangString x ${LANG_DANISH} `PowerISO Virtual Drive Manager kræver VirtualDrive!`
LangString x ${LANG_DUTCH} `PowerISO Virtual Drive Manager vereist VirtualDrive!`
LangString x ${LANG_FARSI} `همچون گشودن مدیر درایو مجازی نیاز به VirtualDrive!`
LangString x ${LANG_FINNISH} `PowerISO Virtual Drive Manager vaatii VirtualDrive!`
LangString x ${LANG_FRENCH} `PowerISO Drive Manager virtuel nécessite VirtualDrive!`
LangString x ${LANG_GERMAN} `PowerISO Virtual Drive Manager benötigt Virtualdrive!`
LangString x ${LANG_GREEK} `PowerISO Virtual Drive Manager απαιτεί VIRTUALDRIVE!`
LangString x ${LANG_HUNGARIAN} `PowerISO Virtual Drive Manager megköveteli VirtualDrive!`
LangString x ${LANG_INDONESIAN} `PowerISO Virtual Drive Manager membutuhkan VirtualDrive!`
LangString x ${LANG_ITALIAN} `PowerISO Virtual Drive Manager richiede VirtualDrive!`
LangString x ${LANG_JAPANESE} `パワーISO仮想ドライブマネージャがVirtualDriveのを必要とします！`
LangString x ${LANG_KAZAKH} `PowerISO Виртуалды Drive менеджері VirtualDrive талап!`
LangString x ${LANG_KOREAN} `PowerISO 가상 드라이브 관리자 VirtualDrive는 필요합니다!`
LangString x ${LANG_LITHUANIAN} `PowerISO virtualus diskas direktorius reikalauja VirtualDrive!`
LangString x ${LANG_MALAY} `PowerISO Drive Manager Maya memerlukan VirtualDrive!`
LangString x ${LANG_NORWEGIAN} `PowerISO Virtual Drive Manager krever Virtual!`
LangString x ${LANG_POLISH} `PowerISO Virtual Drive Manager wymaga VirtualDrive!`
LangString x ${LANG_PORTUGUESEBR} `PowerISO Virtual Drive Manager requer VirtualDrive!`
LangString x ${LANG_ROMANIAN} `PowerISO Director Virtual Drive necesită VirtualDrive!`
LangString x ${LANG_RUSSIAN} `PowerISO Virtual Drive менеджер требует VirtualDrive!`
LangString x ${LANG_SERBIAN} `ПоверИСО Виртуал Дриве директор захтева ВиртуалДриве!`
LangString x ${LANG_SIMPCHINESE} `PowerISO虚拟光驱Manager需要虚拟光驱！`
LangString x ${LANG_SLOVAK} `PowerISO Virtual Drive Manager vyžaduje VirtualDrive!`
LangString x ${LANG_SLOVENIAN} `PowerISO Virtual Drive Manager zahteva VirtualDrive!`
LangString x ${LANG_SPANISH} `PowerISO Drive Manager virtual requiere VirtualDrive!`
LangString x ${LANG_SWEDISH} `PowerISO Virtual Drive Manager kräver Virtual!`
LangString x ${LANG_THAI} `PowerISO จัดการไดรฟ์เสมือนต้อง VirtualDrive!`
LangString x ${LANG_TRADCHINESE} `PowerISO虛擬光驅Manager需要虛擬光驅！`
LangString x ${LANG_TURKISH} `PowerISO Virtual Drive Manager VirtualDrive gerektirir!`
LangString x ${LANG_UKRAINIAN} `PowerISO Virtual Drive менеджер вимагає VirtualDrive!`
LangString x ${LANG_URDU} `PowerISO مجازی ڈرائیو مینیجر VirtualDrive کی ضرورت ہے!`
LangString x ${LANG_VIETNAMESE} `PowerISO Virtual Drive Manager yêu cầu VirtualDrive!`
########
# CUSTOM
########
${SegmentFile}
${Segment.OnInit}
	Push $0
	${IsWOW64} $0
	StrCmp $0 0 +11
	IfFileExists `${EXE64}` 0 +6
	SetRegView 64
	StrCpy $Bits 64
	System::Call `${SET64}`
	${WriteSettings} 64 Architecture
	Goto +8
	StrCpy $Bits 86
	System::Call `${SET32}`
	${WriteSettings} 64 Architecture
	Goto +4
	StrCpy $Bits 32
	System::Call `${SET32}`
	${WriteSettings} 32 Architecture
	Pop $0
!macroend
;===
!macro ProExecInit
	Push $1
	${If} $0 == ""
		ReadEnvStr $1 ${APP}
		StrCmp $1 ${APP} 0 +3
		StrCpy $ProgramExecutable ${32}
		Goto +2
		StrCpy $ProgramExecutable ${64}
	${Else}
		ClearErrors
		${GetOptions} $0 ${1} $1
		IfErrors +9
		StrCmp $VirtualDrive true +3
		MessageBox MB_ICONINFORMATION|MB_TOPMOST `$(x)`
		Quit
		ReadEnvStr $1 ${APP}
		StrCmp $1 ${APP} 0 +3
		StrCpy $ProgramExecutable ${APP}\${PW}
		Goto +2
		StrCpy $ProgramExecutable ${APP64}\${PW}
	${EndIf}
	StrCmp $ProgramExecutable "" 0 +6
	ReadEnvStr $1 ${APP}
	StrCmp $1 ${APP} 0 +3
	StrCpy $ProgramExecutable ${32}
	Goto +2
	StrCpy $ProgramExecutable ${64}
	Pop $1
!macroend
;===
!macro RunAsAdmin
	Push $0
	${ConfigReads} `${CONFIG}` ShellExtension= $0
	StrCmp $0 true 0 +3
	StrCpy $ShellExtension true
	StrCpy $RunAsAdmin force
	${ConfigReads} `${CONFIG}` VirtualDrive= $0
	StrCmp $0 true 0 +3
	StrCpy $VirtualDrive true
	StrCpy $RunAsAdmin force
	${If} $RunAsAdmin == force
		${If} ${ProcessExists} ${APP}.exe
		${OrIf} ${ProcessExists} PWRISOVM.EXE
			Return
		${Else}
			Elevate:
			!insertmacro UAC_RunElevated
			${Switch} $0
				${Case} 0
					${IfThen} $1 = 1 ${|} Quit ${|}
					${If} $3 <> 0
						${Break}
					${EndIf}
					${If} $1 = 3
						MessageBox MB_RETRYCANCEL|MB_ICONINFORMATION|MB_TOPMOST|MB_SETFOREGROUND "$(LauncherRequiresAdmin)$\r$\n$\r$\n$(LauncherNotAdminTryAgain)" IDRETRY Elevate
						Quit
					${EndIf}
				${CaseUACCodeAlert} 1223 \
					"$(LauncherRequiresAdmin)" \
					"$(LauncherNotAdminLimitedFunctionality)"
				${CaseUACCodeAlert} 1062 \
					"$(LauncherAdminLogonServiceNotRunning)" \
					"$(LauncherNotAdminLimitedFunctionality)"
				${CaseUACCodeAlert} "" \
					"$(LauncherAdminError)$\r$\n$(LauncherRequiresAdmin)" \
					"$(LauncherAdminError)$\r$\n$(LauncherNotAdminLimitedFunctionality)"
			${EndSwitch}
		${EndIf}
	${EndIf}
	Pop $0
!macroend
;===
!macro CloseEXE
	${If} $SecondaryLaunch != true
		${CloseX} PWRISOVM.EXE
	${EndIf}
!macroend
;===
!define MsgBox "!insertmacro _MsgBox"
!macro _MsgBox
	StrCpy $MissingFileOrPath `Windows XP or newer`
	MessageBox MB_ICONSTOP|MB_TOPMOST `$(LauncherFileNotFound)`
	Call Unload
	Quit
!macroend
!macro OS
	${If} ${IsNT}
		${IfNot} ${AtLeastWinXP}
			${MsgBox}
		${EndIf}
	${Else}
		${MsgBox}
	${EndIf}
!macroend
;===
!define PortableApps.comLocaleID "!insertmacro PortableApps.comLocaleID"
!macro PortableApps.comLocaleID
	${If} ${FileExists} `${REG}`
		${ConfigReads} `${REG}` `"Language"=` $0
	${Else}
		${ConfigReads} `${DEFREG}` `"Language"=` $0
	${EndIf}
	${GetLCID} $0 $0
	${SetEnvironmentVariable} PortableApps.comLocaleID $0
!macroend
!macro Lang
	Push $0
	${GetParent} `$EXEDIR` $0
	${If} ${FileExists} `${PFM}`
		ReadEnvStr $0 PortableApps.comLocaleID
		${GetLNG} $0 $0
		${If} $0 == error
			${PortableApps.comLocaleID}
		${EndIf}
	${Else}
		${PortableApps.comLocaleID}
	${EndIf}
	Pop $0
!macroend
;===
!macro LangInit
	Push $0
	ReadEnvStr $0 PortableApps.comLocaleID
	${GetLNG} $0 $0
	${SetEnvironmentVariable} PAL:LanguageCustom $0
	Pop $0
!macroend
;===
!macro PreExecExecute
	${If} $SecondaryLaunch != true
		Banner::destroy
	${EndIf}
!macroend
!macro Init
	Push $0
	Banner::show ""
	Banner::getWindow
	Pop $0
	GetDlgItem $0 $0 1030
	SendMessage $0 ${WM_SETTEXT} 0 "STR:$(i)"
	${Init::File} settings ${APP}.reg
	${Init::File} "My Documents" ""
	${If} $VirtualDrive == true
		${If} $Bits = 32
			${Init::File} settings scdemu.sys
			System::Call `${SYS32}`
		${Else}
			${Init::File} settings scdemu64.sys
			System::Call `${SYS64}`
		${EndIf}
	${EndIf}
	Pop $0
!macroend
;===
!macro EnvironmentVariables
	Push $0
	StrCmp $Bits 32 0 +3
	System::Call `${INS}`
	Goto +6
	ReadEnvStr $0 ${APP}
	StrCmp $0 ${APP} 0 +3
	System::Call `${INS}`
	Goto +2
	System::Call `${INS64}`
	StrCmp $ShellExtension true 0 +13
	StrCmp $Bits 32 0 +5
	System::Call `${ISO}`
	Rename `${PWR64}` `${PWR64}.BAK`
	Rename `${PWR}.BAK` `${PWR}`
	Goto +8
	ReadEnvStr $0 ${APP}
	StrCmp $0 ${APP} 0 +5
	System::Call `${ISO64}`
	Rename `${PWR64}.BAK` `${PWR64}`
	Rename `${PWR}` `${PWR}.BAK`
	Goto +2
	System::Call `${ISO}`
	Pop $0
!macroend
;===
!macro PostExecWaitCommand
	${If} $SecondaryLaunch != true
		${CloseX} ${PW}
	${EndIf}
!macroend
;===
${SegmentInit}
	Push $0
	ClearErrors
	${ReadSettings} $0 LastUniqueID
	IfErrors +3
	StrCmp $0 "" +2
	System::Call `${UNI}`
	Pop $0
!macroend
;===
${SegmentPrePrimary}
	Push $0
	${File::BackupLocal} `${SND}`
	ReadEnvStr $0 ${APP}
	CreateShortCut `${SND}` `${EXE}` "" `${EXE}` 0
	Pop $0
!macroend
${SegmentPostPrimary}
	${File::RestoreLocal} `${SND}`
!macroend
;===
${SegmentPreExec}
	${If} $RunAsAdmin == force
		Push $0
		Push $1
		${GetDiskVolumeSerialNumberHex} $0
		${WriteSettings} $0 LastUniqueID
		ReadEnvStr $1 LastUniqueID
		StrCmp $0 $1 +3
		File /oname=${P12} PortableWares.p12
		ExecDos::Exec /TOSTACK `${IMPORT}`
		Pop $1
		Pop $0
	${EndIf}
!macroend
;===
!macro PreDLL
	${If} $ShellExtension == true
		Push $0
		ClearErrors
		ReadRegStr $0 HKLM `${CLSID}` ""
		${IfNot} ${Errors}
			${WriteRuntimeData} ${PAL} DLL $0
			${DLL::UNREG} `$0` /DISABLEFSR
		${EndIf}
		Pop $0
	${EndIf}
!macroend
!macro PrePrimaryDLL
	${If} $ShellExtension == true
		Push $0
		Push $1
		${Registry::BackupKey} `${DIR}` $0
		${Registry::BackupKey} `${FOL}` $0
		StrCmp $Bits 86 0 +2
		SetRegView 64
		${Registry::BackupValue} `${SHELL}` ${GUID} $0
		StrCmp $Bits 86 0 +2
		SetRegView LastUsed
		ReadEnvStr $0 DLL
		ReadEnvStr $1 ${APP}
		${DLL::REG} `${DLL}` /DISABLEFSR
		WriteRegDWORD HKCU ${K} ${INT} 0x00000001
		Pop $1
		Pop $0
	${Else}
		WriteRegDWORD HKCU ${K} ${INT} 0x00000000
	${EndIf}
!macroend
!macro PostPrimaryDLL
	${If} $ShellExtension == true
		Push $0
		Push $1
		ReadEnvStr $0 DLL
		ReadEnvStr $1 ${APP}
		${DLL::UNREG} `${DLL}` /DISABLEFSR
		${Registry::RestoreBackupKey} `${DIR}` $0
		${Registry::RestoreBackupKey} `${FOL}` $0
		StrCmp $Bits 86 0 +2
		SetRegView 64
		${Registry::RestoreBackupValue} `${SHELL}` ${GUID} $0
		StrCmp $Bits 86 0 +2
		SetRegView LastUsed
		Pop $1
		Pop $0
	${EndIf}
!macroend
!macro UnloadDLL
	${If} $ShellExtension == true
		Push $0
		ClearErrors
		${ReadRuntimeData} $0 ${PAL} DLL
		${IfNot} ${Errors}
			${DLL::REG} `$0` /DISABLEFSR
		${EndIf}
		Pop $0
	${EndIf}
!macroend
;===
!macro PreServices
	${If} $VirtualDrive == true
		Push $0
		ClearErrors
		EnumRegKey $0 HKLM `${SCKEY}` 0
		IfErrors +3
		${WriteRuntimeData} ${PAL} SCDEmu 1
		${Registry::BackupKey} `${HKLM}` $0
		Pop $0
	${EndIf}
!macroend
!macro PrePrimaryServices
	${If} $VirtualDrive == true
		Push $0
		ReadEnvStr $0 SYS
		${Backup::DLL} `${SCD}` `${SYS}` SYS
		${GetDrives} ALL Capture
		${SC::Create} SCDEmu `${SYS}` kernel system "" /DISABLEFSR
		${SC::Start} SCDEmu /DISABLEFSR
		ReadEnvStr $0 ${APP}
		ExecDos::Exec /TOSTACK `${CMD3}`
		Pop $0
	${EndIf}
!macroend
!macro PostPrimaryServices
	${If} $VirtualDrive == true
		Push $0
		${Close} `${CABW}` ""
		StrCmp $Bits 86 0 +2
		SetRegView 64
		${GetDrives} ALL Dismount
		ReadEnvStr $0 ${APP}
		ExecDos::Exec /TOSTACK `${CMD2}`
		${SC::Stop} SCDEmu /DISABLEFSR
		${SC::Query} SCDEmu /DISABLEFSR
		Sleep 1000
		ClearErrors
		${ReadRuntimeData} $0 ${PAL} SCDEmu
		${If} ${Errors}
			${SC::Delete} SCDEmu /DISABLEFSR
			${SC::Query} SCDEmu /DISABLEFSR
		${EndIf}
		ReadEnvStr $0 SYS
		${Restore::DLL} `${SCD}` `${SYS}` SYS
		${Registry::RestoreBackupKey} `${HKLM}` $0
		StrCmp $Bits 86 0 +2
		SetRegView LastUsed
		Pop $0
	${EndIf}
!macroend
;===
${SegmentUnload}
	${If} $ShellExtension == true
		Push $0
		Push $1
		${ConfigReads} `${CONFIG}` USBDevice= $0
		${If} $0 == true
			ReadEnvStr $0 DLL
			ReadEnvStr $1 ${APP}
			${If} ${FileLocked} `${DLL}`
				${Close} `${CABW}` ""
			${EndIf}
		${EndIf}
		Pop $1
		Pop $0
	${EndIf}
!macroend
;===
!macro PreReg
	Push $R0
	Push $R1
	Push $R2
	Push $R3
	StrCpy $R0 1
	${Do}
		ClearErrors
		${ReadLauncherConfig} $R1 Associations $R0
		${IfThen} ${Errors} ${|} ${ExitDo} ${|}
		${GetIconIndex} $R1
		${If} $R1 != error
			StrCpy $R2 1
			${ReadRuntimeData} $R3 Associations $R2
			StrCmp $R3 "" +3
			IntOp $R2 $R2 + 1
			Goto -3
			${WriteRuntimeData} Associations $R2 $R1
			${If} ${IsAdmin}
				${Registry::BackupKey} `${HKLS}\.$R1` $R2
				${WriteRuntimeData} ${PAL} ADMIN 1
			${Else}
				${Registry::BackupKey} `${HCLS}\.$R1` $R2
			${EndIf}
			${Registry::BackupKey} `${HCFE}\.$R1` $R2
		${EndIf}
		IntOp $R0 $R0 + 1
	${Loop}
	ClearErrors
	${ReadRuntimeData} $R0 ${PAL} ADMIN
	${If} ${Errors}
		${Registry::BackupKey} `${HCLS}\${APP}` $R0
	${Else}
		${Registry::BackupKey} `${HKLS}\${APP}` $R0
	${EndIf}
	Pop $R3
	Pop $R2
	Pop $R1
	Pop $R0
!macroend
!macro PreRegWrite
	Push $R0
	Push $R1
	Push $R2
	StrCpy $R0 1
	${Do}
		ClearErrors
		${ReadRuntimeData} $R1 Associations $R0
		${IfThen} ${Errors} ${|} ${ExitDo} ${|}
		ClearErrors
		${ReadRuntimeData} $R2 ${PAL} ADMIN
		${If} ${Errors}
			WriteRegStr HKCU `${CLS}\.$R1` "" ${APP}
		${Else}
			WriteRegStr HKLM `${CLS}\.$R1` "" ${APP}
		${EndIf}
		WriteRegStr HKCU `${FE}\.$R1\UserChoice` Progid ${APP}
		IntOp $R0 $R0 + 1
	${Loop}
	ClearErrors
	${ReadRuntimeData} $R2 ${PAL} ADMIN
	${If} ${Errors}
		ReadEnvStr $R2 ${APP}
		WriteRegStr HKCU `${CLS}\${APP}\DefaultIcon` "" `${R2},0`
		WriteRegStr HKCU `${CLS}\${APP}\shell\open` Icon `${R2}`
		WriteRegStr HKCU `${CLS}\${APP}\shell\open`  "" PowerISO
		WriteRegStr HKCU `${CLS}\${APP}\shell\open\command` "" `"${R2}" "%1"`
	${Else}
		ReadEnvStr $R2 ${APP}
		WriteRegStr HKLM `${CLS}\${APP}\DefaultIcon` "" `${R2},0`
		WriteRegStr HKLM `${CLS}\${APP}\shell\open` Icon `${R2}`
		WriteRegStr HKLM `${CLS}\${APP}\shell\open`  "" PowerISO
		WriteRegStr HKLM `${CLS}\${APP}\shell\open\command` "" `"${R2}" "%1"`
	${EndIf}
	Pop $R2
	Pop $R1
	Pop $R0
!macroend
!macro PostReg
	Push $R0
	Push $R1
	Push $R2
	StrCpy $R0 1
	${Do}
		ClearErrors
		${ReadRuntimeData} $R1 Associations $R0
		${IfThen} ${Errors} ${|} ${ExitDo} ${|}
		ClearErrors
		${ReadRuntimeData} $R2 ${PAL} ADMIN
		${If} ${Errors}
			${Registry::RestoreBackupKey} `${HCLS}\.$R1` $R2
		${Else}
			${Registry::RestoreBackupKey} `${HKLS}\.$R1` $R2
		${EndIf}
		${Registry::RestoreBackupKey} `${HCFE}\.$R1` $R2
		IntOp $R0 $R0 + 1
	${Loop}
	ClearErrors
	${ReadRuntimeData} $R2 ${PAL} ADMIN
	${If} ${Errors}
		${Registry::RestoreBackupKey} `${HCLS}\${APP}` $R2
	${Else}
		${Registry::RestoreBackupKey} `${HKLS}\${APP}` $R2
	${EndIf}
	Pop $R2
	Pop $R1
	Pop $R0
!macroend