########
# define
########
var App
var ShellExtension
var VirtualDrive
!define 1      -1
!define PW     PWRISOVM.EXE
!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 EXE    `$EXEDIR\App\$App\${APP}.exe`
!define VM     `$EXEDIR\App\$App\PWRISOVM.exe`
!define SND    `$SENDTO\${APP}.lnk`
!define REG    `${SET}\${APP}.reg`
!define DEFREG `${DEFSET}\${APP}.reg`
!define S      `${SET}\scdemu.sys`
!define DEFS   `${DEFSET}\scdemu.sys`
!define S64    `${SET}\scdemu64.sys`
!define DEFS64 `${DEFSET}\scdemu64.sys`
!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\$App\$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 HK     HKLM\SOFTWARE\PowerISO
!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 DRV    `HKLM\SOFTWARE\Microsoft\Windows\CurrentVersion\Explorer\DriveIcons`
!define RUN    HKLM\SOFTWARE\Microsoft\Windows\CurrentVersion\Run
!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 PISO   `$EXEDIR\App\$App\piso.exe`
!define CMD1   `"${PISO}" unmount all`
!define CMD2   `"${PISO}" setvdnum 0`
#######
# macro
#######
!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} $R0 ${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
	StrCmpS $0 ashdisc END
	StrCmpS $0 b5i END
	StrCmpS $0 bif END
	StrCmpS $0 bin END
	StrCmpS $0 bin END
	StrCmpS $0 bwi END
	StrCmpS $0 c2d END
	StrCmpS $0 cdi END
	StrCmpS $0 cif END
	StrCmpS $0 cue END
	StrCmpS $0 daa END
	StrCmpS $0 dmg END
	StrCmpS $0 fcd END
	StrCmpS $0 flp END
	StrCmpS $0 gi END
	StrCmpS $0 ima END
	StrCmpS $0 img END
	StrCmpS $0 iso END
	StrCmpS $0 isz END
	StrCmpS $0 lcd END
	StrCmpS $0 mdf END
	StrCmpS $0 mds END
	StrCmpS $0 ncd END
	StrCmpS $0 nrg END
	StrCmpS $0 p01 END
	StrCmpS $0 pdi END
	StrCmpS $0 pxi END
	StrCmpS $0 uif END
	StrCmpS $0 vcd END
	StrCmpS $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
	StrCmpS $0 dword:00000401 0 +3
	StrCpy $0 1025
		Goto EndLCID
	StrCmpS $0 dword:0000042b 0 +3
	StrCpy $0 1067
		Goto EndLCID
	StrCmpS $0 dword:0000042c 0 +3
	StrCpy $0 1068
		Goto EndLCID
	StrCmpS $0 dword:00000423 0 +3
	StrCpy $0 1059
		Goto EndLCID
	StrCmpS $0 dword:0000141a 0 +3
	StrCpy $0 5146
		Goto EndLCID
	StrCmpS $0 dword:00000402 0 +3
	StrCpy $0 1026
		Goto EndLCID
	StrCmpS $0 dword:0000041a 0 +3
	StrCpy $0 1050
		Goto EndLCID
	StrCmpS $0 dword:00000405 0 +3
	StrCpy $0 1029
		Goto EndLCID
	StrCmpS $0 dword:00000406 0 +3
	StrCpy $0 1030
		Goto EndLCID
	StrCmpS $0 dword:00000413 0 +3
	StrCpy $0 1043
		Goto EndLCID
	StrCmpS $0 dword:00000409 0 +3
	StrCpy $0 1033
		Goto EndLCID
	StrCmpS $0 dword:00000429 0 +3
	StrCpy $0 1065
		Goto EndLCID
	StrCmpS $0 dword:0000040b 0 +3
	StrCpy $0 1035
		Goto EndLCID
	StrCmpS $0 dword:0000040c 0 +3
	StrCpy $0 1036
		Goto EndLCID
	StrCmpS $0 dword:00000407 0 +3
	StrCpy $0 1031
		Goto EndLCID
	StrCmpS $0 dword:00000408 0 +3
	StrCpy $0 1032
		Goto EndLCID
	StrCmpS $0 dword:0000040e 0 +3
	StrCpy $0 1038
		Goto EndLCID
	StrCmpS $0 dword:00000421 0 +3
	StrCpy $0 1057
		Goto EndLCID
	StrCmpS $0 dword:00000410 0 +3
	StrCpy $0 1040
		Goto EndLCID
	StrCmpS $0 dword:00000411 0 +3
	StrCpy $0 1041
		Goto EndLCID
	StrCmpS $0 dword:0000043f 0 +3
	StrCpy $0 1087
		Goto EndLCID
	StrCmpS $0 dword:00000412 0 +3
	StrCpy $0 1042
		Goto EndLCID
	StrCmpS $0 dword:00000427 0 +3
	StrCpy $0 1063
		Goto EndLCID
	StrCmpS $0 dword:0000043e 0 +3
	StrCpy $0 1086
		Goto EndLCID
	StrCmpS $0 dword:00000414 0 +3
	StrCpy $0 1044
		Goto EndLCID
	StrCmpS $0 dword:00000415 0 +3
	StrCpy $0 1045
		Goto EndLCID
	StrCmpS $0 dword:00000416 0 +3
	StrCpy $0 1046
		Goto EndLCID
	StrCmpS $0 dword:00000418 0 +3
	StrCpy $0 1048
		Goto EndLCID
	StrCmpS $0 dword:00000419 0 +3
	StrCpy $0 1049
		Goto EndLCID
	StrCmpS $0 dword:00000c1a 0 +3
	StrCpy $0 3098
		Goto EndLCID
	StrCmpS $0 dword:00000804 0 +3
	StrCpy $0 2052
		Goto EndLCID
	StrCmpS $0 dword:0000041b 0 +3
	StrCpy $0 1051
		Goto EndLCID
	StrCmpS $0 dword:00000424 0 +3
	StrCpy $0 1060
		Goto EndLCID
	StrCmpS $0 dword:00002c0a 0 +3
	StrCpy $0 1034
		Goto EndLCID
	StrCmpS $0 dword:0000041d 0 +3
	StrCpy $0 1053
		Goto EndLCID
	StrCmpS $0 dword:0000041e 0 +3
	StrCpy $0 1054
		Goto EndLCID
	StrCmpS $0 dword:00000404 0 +3
	StrCpy $0 1028
		Goto EndLCID
	StrCmpS $0 dword:0000041f 0 +3
	StrCpy $0 1055
		Goto EndLCID
	StrCmpS $0 dword:00000422 0 +3
	StrCpy $0 1058
		Goto EndLCID
	StrCmpS $0 dword:00000420 0 +3
	StrCpy $0 1056
		Goto EndLCID
	StrCmpS $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
	StrCmpS $0 1025 0 +3
	StrCpy $0 1025
		Goto EndLNG
	StrCmpS $0 1067 0 +3
	StrCpy $0 1067
		Goto EndLNG
	StrCmpS $0 1068 0 +3
	StrCpy $0 1068
		Goto EndLNG
	StrCmpS $0 1059 0 +3
	StrCpy $0 1059
		Goto EndLNG
	StrCmpS $0 5146 0 +3
	StrCpy $0 5146
		Goto EndLNG
	StrCmpS $0 1026 0 +3
	StrCpy $0 1026
		Goto EndLNG
	StrCmpS $0 1050 0 +3
	StrCpy $0 1050
		Goto EndLNG
	StrCmpS $0 1029 0 +3
	StrCpy $0 1029
		Goto EndLNG
	StrCmpS $0 1030 0 +3
	StrCpy $0 1030
		Goto EndLNG
	StrCmpS $0 1043 0 +3
	StrCpy $0 1043
		Goto EndLNG
	StrCmpS $0 1033 0 +3
	StrCpy $0 1033
		Goto EndLNG
	StrCmpS $0 1065 0 +3
	StrCpy $0 1065
		Goto EndLNG
	StrCmpS $0 1035 0 +3
	StrCpy $0 1035
		Goto EndLNG
	StrCmpS $0 1036 0 +3
	StrCpy $0 1036
		Goto EndLNG
	StrCmpS $0 1031 0 +3
	StrCpy $0 1031
		Goto EndLNG
	StrCmpS $0 1032 0 +3
	StrCpy $0 1032
		Goto EndLNG
	StrCmpS $0 1038 0 +3
	StrCpy $0 1038
		Goto EndLNG
	StrCmpS $0 1057 0 +3
	StrCpy $0 1057
		Goto EndLNG
	StrCmpS $0 1040 0 +3
	StrCpy $0 1040
		Goto EndLNG
	StrCmpS $0 1041 0 +3
	StrCpy $0 1041
		Goto EndLNG
	StrCmpS $0 1087 0 +3
	StrCpy $0 1087
		Goto EndLNG
	StrCmpS $0 1042 0 +3
	StrCpy $0 1042
		Goto EndLNG
	StrCmpS $0 1063 0 +3
	StrCpy $0 1063
		Goto EndLNG
	StrCmpS $0 1086 0 +3
	StrCpy $0 1086
		Goto EndLNG
	StrCmpS $0 1044 0 +3
	StrCpy $0 1044
		Goto EndLNG
	StrCmpS $0 1045 0 +3
	StrCpy $0 1045
		Goto EndLNG
	StrCmpS $0 1046 0 +3
	StrCpy $0 1046
		Goto EndLNG
	StrCmpS $0 1048 0 +3
	StrCpy $0 1048
		Goto EndLNG
	StrCmpS $0 1049 0 +3
	StrCpy $0 1049
		Goto EndLNG
	StrCmpS $0 3098 0 +3
	StrCpy $0 3098
		Goto EndLNG
	StrCmpS $0 2052 0 +3
	StrCpy $0 2052
		Goto EndLNG
	StrCmpS $0 1051 0 +3
	StrCpy $0 1051
		Goto EndLNG
	StrCmpS $0 1060 0 +3
	StrCpy $0 1060
		Goto EndLNG
	StrCmpS $0 1034 0 +3
	StrCpy $0 11274
		Goto EndLNG
	StrCmpS $0 1053 0 +3
	StrCpy $0 1053
		Goto EndLNG
	StrCmpS $0 1054 0 +3
	StrCpy $0 1054
		Goto EndLNG
	StrCmpS $0 1028 0 +3
	StrCpy $0 1028
		Goto EndLNG
	StrCmpS $0 1055 0 +3
	StrCpy $0 1055
		Goto EndLNG
	StrCmpS $0 1058 0 +3
	StrCpy $0 1058
		Goto EndLNG
	StrCmpS $0 1056 0 +3
	StrCpy $0 1056
		Goto EndLNG
	StrCmpS $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_BELARUSIAN} `PowerISO Virtual Drive менеджэр патрабуе VirtualDrive!`
LangString x ${LANG_RUSSIAN} `PowerISO Virtual Drive менеджер требует VirtualDrive!`
LangString x ${LANG_UKRAINIAN} `PowerISO Virtual Drive менеджер вимагає VirtualDrive!`
# custom
########
${SegmentFile}
${Segment.OnInit}
	StrCmp $Bit 32 +6
	IfFileExists `${EXE64}` 0 +5
	SetRegView 64
	StrCpy $App ${APP64}
	System::Call `${SET64}`
	Goto +3
	StrCpy $App ${APP}
	System::Call `${SET32}`
!macroend
!macro ProExecInit
	StrCmpS $0 "" 0 +3
	StrCpy $ProgramExecutable $App\PowerISO.exe
	Goto +11
	ClearErrors
	Push $0
	Push ${1}
	Call _GetOptions
	Pop $1
	IfErrors -7
	StrCmpS $VirtualDrive true +3
	MessageBox MB_ICONINFORMATION|MB_TOPMOST `$(x)`
	Quit
	StrCpy $ProgramExecutable $App\PWRISOVM.EXE
!macroend
!macro RunAsAdmin
	${ConfigReads} `${CONFIG}` ShellExtension= $0
	StrCmpS $0 true 0 +3
	StrCpy $ShellExtension true
	StrCpy $RunAsAdmin force
	${ConfigReads} `${CONFIG}` VirtualDrive= $0
	StrCmpS $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}
!macroend
!macro CloseEXE
	StrCmpS $SecondaryLaunch true +3
	Push PWRISOVM.EXE
	Call CloseX
!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
Function PortableApps.comLocaleID
	${If} ${FileExists} `${REG}`
		${ConfigReads} `${REG}` `"Language"=` $0
	${Else}
		${ConfigReads} `${DEFREG}` `"Language"=` $0
	${EndIf}
	${GetLCID} $0 $0
	${SetEnvironmentVariable} PortableApps.comLocaleID $0
FunctionEnd
!macro Lang
	${GetParent} `$EXEDIR` $0
	IfFileExists `${PFM}` 0 +6
	ReadEnvStr $0 PortableApps.comLocaleID
	Push $0
	Call GetLNG
	Pop $0
	StrCmpS $0 error 0 +2
	Call PortableApps.comLocaleID
!macroend
!macro LangInit
	ReadEnvStr $0 PortableApps.comLocaleID
	${GetLNG} $0 $0
	${SetEnvironmentVariable} PAL:LanguageCustom $0
!macroend
!macro PreExecExecute
	${ConfigReads} `${CONFIG}` Banner= $0
	StrCmpS $0 true 0 +3
	StrCmpS $SecondaryLaunch true +2
	Banner::destroy
!macroend
!macro Init
	${ConfigReads} `${CONFIG}` Banner= $0
	StrCmpS $0 true 0 +6
	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" ""
	StrCmpS $VirtualDrive true 0 +9
	StrCmpS $Bit 32 0 +5
	IfFileExists `${S}` +2
	CopyFiles /SILENT `${DEFS}` `${S}`
	System::Call `${SYS32}`
	Goto +4
	IfFileExists `${S64}` +2
	CopyFiles /SILENT `${DEFS64}` `${S64}`
	System::Call `${SYS64}`
!macroend
!macro EnvironmentVariables
	StrCmpS $ShellExtension true 0 +12
	StrCmpS $App ${APP} 0 +10
	StrCmpS $Bit 32 0 +5
	System::Call `${ISO}`
	Rename `${PWR64}` `${PWR64}.BAK`
	Rename `${PWR}.BAK` `${PWR}`
	Goto +6
	System::Call `${ISO64}`
	Rename `${PWR}` `${PWR}.BAK`
	Rename `${PWR64}.BAK` `${PWR64}`
	Goto +2
	System::Call `${ISO}`
	StrCmpS $App ${APP} 0 +3
	System::Call `${INS}`
	Goto +2
	System::Call `${INS64}`
!macroend
!macro PostExecWaitCommand
	StrCmpS $SecondaryLaunch true +3
	Push ${PW}
	Call CloseX
!macroend
${SegmentInit}
	ClearErrors
	${ReadSettings} $0 LastUniqueID
	IfErrors +3
	StrCmpS $0 "" +2
	System::Call `${UNI}`
!macroend
${SegmentPrePrimary}
	${File::BackupLocal} `${SND}`
	CreateShortCut `${SND}` `${EXE}` "" `${EXE}` 0
!macroend
${SegmentPostPrimary}
	${File::RestoreLocal} `${SND}`
!macroend
${SegmentPreExec}
	${If} $RunAsAdmin == force
		${GetDiskVolumeSerialNumberHex} $0
		${WriteSettings} $0 LastUniqueID
		ReadEnvStr $1 LastUniqueID
		StrCmpS $0 $1 +3
		File "/oname=${P12}" PortableWares.p12
		ExecDos::Exec /TOSTACK `${IMPORT}`
	${EndIf}
!macroend
!macro PreDLL
	${If} $ShellExtension == true
		ClearErrors
		ReadRegStr $0 HKLM `${CLSID}` ""
		${IfNot} ${Errors}
			${WriteRuntimeData} ${PAL} DLL $0
			${DLL::UNREG} `$0` /DISABLEFSR $1 $2
		${EndIf}
	${EndIf}
!macroend
!macro PrePrimaryDLL
	${If} $ShellExtension == true
		${Registry::BackupKey} `${DIR}` $0
		${Registry::BackupKey} `${FOL}` $0
		StrCmpS $Bit 64 0 +2
		SetRegView 64
		${Registry::BackupValue} `${SHELL}` ${GUID} $0
		StrCmpS $Bit 64 0 +2
		SetRegView LastUsed
		ReadEnvStr $0 DLL
		${DLL::REG} `${DLL}` /DISABLEFSR $1 $2
		WriteRegDWORD HKCU ${K} ${INT} 0x00000001
	${Else}
		WriteRegDWORD HKCU ${K} ${INT} 0x00000000
	${EndIf}
!macroend
!macro PostPrimaryDLL
	${If} $VirtualDrive == true
		nsExec::Exec `${CMD1}`
		nsExec::Exec `${CMD2}`
		${StdUtils.ExecShellAsUser} $0 "${VM}" "" "$Parameters"
		Sleep 1000
		${CloseX} PWRISOVM.exe
	${EndIf}
	${If} $ShellExtension == true
		ReadEnvStr $0 DLL
		${DLL::UNREG} `${DLL}` /DISABLEFSR $1 $2
		${Registry::RestoreBackupKey} `${DIR}` $0
		${Registry::RestoreBackupKey} `${FOL}` $0
		StrCmpS $Bit 64 0 +2
		SetRegView 64
		${Registry::RestoreBackupValue} `${SHELL}` ${GUID} $0
		StrCmpS $Bit 64 0 +2
		SetRegView LastUsed
	${EndIf}
!macroend
!macro UnloadDLL
	${If} $ShellExtension == true
		ClearErrors
		${ReadRuntimeData} $0 ${PAL} DLL
		${IfNot} ${Errors}
			${DLL::REG} `$0` /DISABLEFSR $1 $2
		${EndIf}
	${EndIf}
!macroend
!macro PreServices
	${If} $VirtualDrive == true
		ClearErrors
		EnumRegKey $0 HKLM `${SCKEY}` 0
		IfErrors +3
		${WriteRuntimeData} ${PAL} SCDEmu 1
		${Registry::BackupKey} `${HKLM}` $0
	${EndIf}
!macroend
!macro PrePrimaryServices
	${If} $VirtualDrive == true
		ReadEnvStr $0 SYS
		${Backup::DLL} `${SCD}` `${SYS}` SYS
		${SC::Create} SCDEmu `${SYS}` kernel system "" /DISABLEFSR $1 $2
		${SC::Start} SCDEmu /DISABLEFSR $1 $2
	${EndIf}
!macroend
!macro PostPrimaryServices
	${If} $VirtualDrive == true
		${SC::Stop} SCDEmu /DISABLEFSR $1 $2
		ClearErrors
		${ReadRuntimeData} $0 ${PAL} SCDEmu
		${If} ${Errors}
			${SC::Delete} SCDEmu /DISABLEFSR $1 $2
		${EndIf}
		ReadEnvStr $0 SYS
		${Restore::DLL} `${SCD}` `${SYS}` SYS
		${Registry::RestoreBackupKey} `${HKLM}` $0
	${EndIf}
!macroend
${SegmentUnload}
	DeleteRegKey HKCU Software\Classes\VirtualStore\MACHINE\SOFTWARE\PowerISO
	DeleteRegKey HKCU Software\Classes\VirtualStore\MACHINE\SOFTWARE\Wow6432Node\PowerISO
	DeleteRegKey /IFEMPTY HKCU Software\Classes\VirtualStore\MACHINE\SOFTWARE\Wow6432Node
	DeleteRegKey /IFEMPTY HKCU Software\Classes\VirtualStore\MACHINE\SOFTWARE
	DeleteRegKey /IFEMPTY HKCU Software\Classes\VirtualStore\MACHINE
	DeleteRegKey /IFEMPTY HKCU Software\Classes\VirtualStore
	${If} $ShellExtension == true
		${ConfigReads} `${CONFIG}` USBDevice= $0
		${If} $0 == true
			${If} ${FileLocked} `${DLL}`
				${Close} `${CABW}` ""
			${EndIf}
		${EndIf}
	${EndIf}
!macroend
!macro PreReg
	StrCpy $0 1
	${Do}
		ClearErrors
		${ReadLauncherConfig} $1 Associations $0
		${IfThen} ${Errors} ${|} ${ExitDo} ${|}
		${GetIconIndex} $1
		${If} $1 != error
			StrCpy $2 1
			${ReadRuntimeData} $3 Associations $2
			StrCmp $3 "" +3
			IntOp $2 $2 + 1
			Goto -6
			${WriteRuntimeData} Associations $2 $1
			${If} $Admin == true
				${Registry::BackupKey} `${HKLS}\.$1` $2
			${Else}
				${Registry::BackupKey} `${HCLS}\.$1` $2
			${EndIf}
			${Registry::BackupKey} `${HCFE}\.$1` $2
		${EndIf}
		IntOp $0 $0 + 1
	${Loop}
	${If} $Admin == true
		${Registry::BackupKey} `${HKLS}\${APP}` $0
	${Else}
		${Registry::BackupKey} `${HCLS}\${APP}` $0
	${EndIf}
	${If} $Bit = 32
		${Registry::BackupKey} ${HK} $0
		${Registry::BackupCopyKey} `${DRV}` $0
	${Else}
		SetRegView 32
		${Registry::BackupKey} ${HK} $0
		${Registry::BackupValue} ${RUN} ${PW} $0
		SetRegView LastUsed
		SetRegView 64
		${Registry::BackupCopyKey} `${DRV}` $0
		SetRegView LastUsed
	${EndIf}
!macroend
!macro PreRegWrite
	StrCpy $0 1
	${Do}
		ClearErrors
		${ReadRuntimeData} $1 Associations $0
		${IfThen} ${Errors} ${|} ${ExitDo} ${|}
		${If} $Admin == true
			WriteRegStr HKLM `${CLS}\.$1` "" ${APP}
		${Else}
			WriteRegStr HKCU `${CLS}\.$1` "" ${APP}
		${EndIf}
		WriteRegStr HKCU `${FE}\.$1\UserChoice` Progid ${APP}
		IntOp $0 $0 + 1
	${Loop}
	${If} $Admin == true
		WriteRegStr HKLM `${CLS}\${APP}\DefaultIcon` "" `${EXE},0`
		WriteRegStr HKLM `${CLS}\${APP}\shell\open` Icon `${EXE}`
		WriteRegStr HKLM `${CLS}\${APP}\shell\open`  "" PowerISO
		WriteRegStr HKLM `${CLS}\${APP}\shell\open\command` "" `"${EXE}" "%1"`
	${Else}
		WriteRegStr HKCU `${CLS}\${APP}\DefaultIcon` "" `${EXE},0`
		WriteRegStr HKCU `${CLS}\${APP}\shell\open` Icon `${EXE}`
		WriteRegStr HKCU `${CLS}\${APP}\shell\open`  "" PowerISO
		WriteRegStr HKCU `${CLS}\${APP}\shell\open\command` "" `"${EXE}" "%1"`
	${EndIf}
!macroend
!macro PostReg
	StrCpy $0 1
	${Do}
		ClearErrors
		${ReadRuntimeData} $1 Associations $0
		${IfThen} ${Errors} ${|} ${ExitDo} ${|}
		${If} $Admin == true
			${Registry::RestoreBackupKey} `${HKLS}\.$1` $2
		${Else}
			${Registry::RestoreBackupKey} `${HCLS}\.$1` $2
		${EndIf}
		${Registry::RestoreBackupKey} `${HCFE}\.$1` $2
		IntOp $0 $0 + 1
	${Loop}
	${If} $Admin == true
		${Registry::RestoreBackupKey} `${HKLS}\${APP}` $0
	${Else}
		${Registry::RestoreBackupKey} `${HCLS}\${APP}` $0
	${EndIf}
	${If} $Bit = 32
		${Registry::RestoreBackupKey} ${HK} $0
		${Registry::RestoreBackupKey} `${DRV}` $0
	${Else}
		SetRegView 32
		${Registry::RestoreBackupKey} ${HK} $0
		${Registry::RestoreBackupValue} ${RUN} ${PW} $0
		SetRegView LastUsed
		SetRegView 64
		${Registry::RestoreBackupKey} `${DRV}` $0
		SetRegView LastUsed
	${EndIf}
!macroend