!define DLL      `${APPDIR}\W8MPCL.dll`
!define 64       `${DATA}\W8MPCL64.dll`
!define 32       `${DATA}\W8MPCL.dll`
!define DEF64    `${DEFDATA}\W8MPCL64.dll`
!define DEF32    `${DEFDATA}\W8MPCL.dll`
!define REG      `${SET}\Yamicsoft.reg`
!define DEFREG   `${DEFDATA}\settings\Yamicsoft.reg`
!define XML1     `${SET}\Addons.xml`
!define DEFXML1  `${DEFDATA}\settings\Addons.xml`
!define XML2     `${SET}\Config.xml`
!define DEFXML2  `${DEFDATA}\settings\Config.xml`
!define CALL1    `kernel32::Wow64EnableWow64FsRedirection(i0)`
!define CALL2    `Kernel32::SetEnvironmentVariable(t, t) i("SYSDIR", "$SYSDIR").r0`
!define CALL3    `Kernel32::SetEnvironmentVariable(t, t) i("CLR", "CLR_v4.0_32").r0`
!define CALL4    `Kernel32::SetEnvironmentVariable(t, t) i("CLR", "CLR_v4.0").r0`
!define MSG1     `${FULLNAME} is only compatible with Windows 8.`
!define MSG2     "Yamicsoft's 'Hosts Editor' can become unresponsive if the 'hosts' file is large."
!define DEL1     `${DATA}\Windows 8 Manager - IP Switcher`
!define DEL2     `${DATA}\Windows 8 Manager - IP Switcher.job`
!define ACL      `${APPDIR}\SetACL.exe`
!define ACL64    `${APPDIR}\SetACL_x64.exe`
!define KEY1     `HKLM\SOFTWARE\Microsoft\Windows NT\CurrentVersion\Schedule\CompatibilityAdapter\Signatures`

!define SetACL::Key `!insertmacro SetACL::Key`
!macro SetACL::Key _ACL _KEY
	nsExec::Exec `"${_ACL}" -on "${_KEY}" -ot reg -rec yes -silent -actn setowner -ownr "n:Administrators"`
	nsExec::Exec `"${_ACL}" -on "${_KEY}" -ot reg -rec yes -silent -actn clear -clr "dacl"`
	nsExec::Exec `"${_ACL}" -on "${_KEY}" -ot reg -rec yes -silent -actn setowner -ownr "n:Administrators"`
	nsExec::Exec `"${_ACL}" -on "${_KEY}" -ot reg -rec yes -silent -actn ace -ace "n:Everyone;p:full"`
!macroend

${SegmentFile}
${Segment.OnInit}
	Push $0
	${IsWOW64} $0
	${If} $0 != 0
		SetRegView 64
		System::Call `${CALL1}`
	${EndIf}
	Pop $0
!macroend

!macro CustomOS
	${IfNot} ${IsWin8}
		MessageBox MB_OK|MB_ICONSTOP|MB_TOPMOST `${MSG1}`
		Call Unload
		Quit
	${EndIf}
!macroend

${SegmentInit}
	Push $0
	${IfThen} ${ProcessExists} 1-ClickCleaner.exe ${|} ${TerminateProcess} 1-ClickCleaner.exe $0 ${|}
	${IfThen} ${ProcessExists} ComputerManager.exe ${|} ${TerminateProcess} ComputerManager.exe $0 ${|}
	${IfThen} ${ProcessExists} ContextMenuManager.exe ${|} ${TerminateProcess} ContextMenuManager.exe $0 ${|}
	${IfThen} ${ProcessExists} DesktopCleaner.exe ${|} ${TerminateProcess} DesktopCleaner.exe $0 ${|}
	${IfThen} ${ProcessExists} DiskAnalyzer.exe ${|} ${TerminateProcess} DiskAnalyzer.exe $0 ${|}
	${IfThen} ${ProcessExists} DuplicateFilesFinder.exe ${|} ${TerminateProcess} DuplicateFilesFinder.exe $0 ${|}
	${IfThen} ${ProcessExists} FileSecurity.exe ${|} ${TerminateProcess} FileSecurity.exe $0 ${|}
	${IfThen} ${ProcessExists} FileSplitter.exe ${|} ${TerminateProcess} FileSplitter.exe $0 ${|}
	${IfThen} ${ProcessExists} FileUndelete.exe ${|} ${TerminateProcess} FileUndelete.exe $0 ${|}
	${IfThen} ${ProcessExists} HiddenDeviceManager.exe ${|} ${TerminateProcess} HiddenDeviceManager.exe $0 ${|}
	${IfThen} ${ProcessExists} IEManager.exe ${|} ${TerminateProcess} IEManager.exe $0 ${|}
	${IfThen} ${ProcessExists} IPSwitcher.exe ${|} ${TerminateProcess} IPSwitcher.exe $0 ${|}
	${IfThen} ${ProcessExists} JumpListQuickLauncher.exe ${|} ${TerminateProcess} JumpListQuickLauncher.exe $0 ${|}
	${IfThen} ${ProcessExists} JunkFileCleaner.exe ${|} ${TerminateProcess} JunkFileCleaner.exe $0 ${|}
	${IfThen} ${ProcessExists} LockScreenChanger.exe ${|} ${TerminateProcess} LockScreenChanger.exe $0 ${|}
	${IfThen} ${ProcessExists} LockSystem.exe ${|} ${TerminateProcess} LockSystem.exe $0 ${|}
	${IfThen} ${ProcessExists} ModernUISettings.exe ${|} ${TerminateProcess} ModernUISettings.exe $0 ${|}
	${IfThen} ${ProcessExists} MyTask.exe ${|} ${TerminateProcess} MyTask.exe $0 ${|}
	${IfThen} ${ProcessExists} OptimizationWizard.exe ${|} ${TerminateProcess} OptimizationWizard.exe $0 ${|}
	${IfThen} ${ProcessExists} PinnedManager.exe ${|} ${TerminateProcess} PinnedManager.exe $0 ${|}
	${IfThen} ${ProcessExists} PrivacyProtector.exe ${|} ${TerminateProcess} PrivacyProtector.exe $0 ${|}
	${IfThen} ${ProcessExists} ProcessManager.exe ${|} ${TerminateProcess} ProcessManager.exe $0 ${|}
	${IfThen} ${ProcessExists} RegistryCleaner.exe ${|} ${TerminateProcess} RegistryCleaner.exe $0 ${|}
	${IfThen} ${ProcessExists} RegistryDefrag.exe ${|} ${TerminateProcess} RegistryDefrag.exe $0 ${|}
	${IfThen} ${ProcessExists} RegistryTools.exe ${|} ${TerminateProcess} RegistryTools.exe $0 ${|}
	${IfThen} ${ProcessExists} RepairCenter.exe ${|} ${TerminateProcess} RepairCenter.exe $0 ${|}
	${IfThen} ${ProcessExists} RunShortcutCreator.exe ${|} ${TerminateProcess} RunShortcutCreator.exe $0 ${|}
	${IfThen} ${ProcessExists} ServiceManager.exe ${|} ${TerminateProcess} ServiceManager.exe $0 ${|}
	${IfThen} ${ProcessExists} SmartUninstaller.exe ${|} ${TerminateProcess} SmartUninstaller.exe $0 ${|}
	${IfThen} ${ProcessExists} StartupManager.exe ${|} ${TerminateProcess} StartupManager.exe $0 ${|}
	${IfThen} ${ProcessExists} SuperCopy.exe ${|} ${TerminateProcess} SuperCopy.exe $0 ${|}
	${IfThen} ${ProcessExists} SystemInfo.exe ${|} ${TerminateProcess} SystemInfo.exe $0 ${|}
	${IfThen} ${ProcessExists} TaskSchedulerManager.exe ${|} ${TerminateProcess} TaskSchedulerManager.exe $0 ${|}
	${IfThen} ${ProcessExists} VisualCustomizer.exe ${|} ${TerminateProcess} VisualCustomizer.exe $0 ${|}
	${IfThen} ${ProcessExists} WinStoreAppUninstaller.exe ${|} ${TerminateProcess} WinStoreAppUninstaller.exe $0 ${|}
	${IfThen} ${ProcessExists} WinUtilities.exe ${|} ${TerminateProcess} WinUtilities.exe $0 ${|}
	${IfThen} ${ProcessExists} WinXMenuEditor.exe ${|} ${TerminateProcess} WinXMenuEditor.exe $0 ${|}
	Pop $0
!macroend

!macro CustomVarInit
	Push $0
	System::Call `${CALL2}`
	${IsWOW64} $0
	${If} $0 = 0
		System::Call `${CALL3}`
	${Else}
		System::Call `${CALL4}`
	${EndIf}
	Pop $0
!macroend

!macro CustomInit
	IfFileExists `${REG}` +2
	CopyFiles /SILENT `${DEFREG}` `${REG}`
	IfFileExists `${XML1}` +2
	CopyFiles /SILENT `${DEFXML1}` `${XML1}`
	IfFileExists `${XML2}` +2
	CopyFiles /SILENT `${DEFXML2}` `${XML2}`
	IfFileExists `${32}` +2
	CopyFiles /SILENT `${DEF32}` `${32}`
	IfFileExists `${64}` +2
	CopyFiles /SILENT `${DEF64}` `${64}`
!macroend

!macro CustomPreRegistryValue
	Push $0
	${IsWOW64} $0
	${If} $0 = 0
		${SetACL::Key} `${ACL}` `${KEY1}`
	${Else}
		${SetACL::Key} `${ACL64}` `${KEY1}`
	${EndIf}
	Pop $0
!macroend

${SegmentPrePrimary}
	Push $0
	${ReadSettings} $0 HostsFileWarning
	${If} $0 != 1
		MESSAGEBOX MB_OK|MB_ICONINFORMATION|MB_TOPMOST `${MSG2}`
		${WriteSettings} 1 HostsFileWarning
	${EndIf}
	${IsWOW64} $0
	${If} $0 = 0
		Rename `${32}` `${DLL}`
	${Else}
		Rename `${64}` `${DLL}`
	${EndIf}
	Pop $0
!macroend

${SegmentPostPrimary}
	Push $0
	${IsWOW64} $0
	${If} $0 = 0
		Delete `${32}`
		Rename `${DLL}` `${32}`
	${Else}
		Delete `${64}`
		Rename `${DLL}` `${64}`
	${EndIf}
	Delete `${DEL1}`
	Delete `${DEL2}`
	Pop $0
!macroend