#region ;**** Directives created by AutoIt3Wrapper_GUI ****
#AutoIt3Wrapper_Icon=launcher_128.ico
#AutoIt3Wrapper_Compression=4
#endregion ;**** Directives created by AutoIt3Wrapper_GUI ****
#include <file.au3>
#include <array.au3>
#include <_startup.au3>
Global $launcher = "D:\Star Wars - The Old Republic\launcher.exe"
Global $LauncherWindowDesc = "[TITLE:STAR WARS™: The Old Republic™; CLASS:SolidSkinnedWindow]"
Global $sourcedir = "D:\Star Wars - The Old Republic\assets"
Global $BackupDir = "D:\Star Wars - The Old Republic\_backup_\assets"
Global $LogFilename = "D:\Star Wars - The Old Republic\logs\launcher_" & @YEAR & @MON & @MDAY & ".log"
$split = stringsplit($sourcedir, "\")
Global $destdir = stringlower(_softperfect() & "\" & $split[$split[0]])
If $CmdLine[0] >= 1 Then
If $CmdLine[1] = "restore" Then
_exit()
EndIf
EndIf
_start_launcher($launcher, $LauncherWindowDesc)
_wait_logfile($LogFilename)
Do
Sleep(100)
Until WinExists($LauncherWindowDesc)
While 1
Sleep(100)
$test = _patching_done($LogFilename)
;msgbox(0, "test", $test)
If $test = 1 Then
ExitLoop
EndIf
WEnd
$ramdisk = _softperfect()
If Not ($ramdisk = -1) Then
RunWait(@ComSpec & " /c " & 'robocopy.exe ' & '"' & $sourcedir & '"' & ' ' & '"' & $destdir & '"')
_prepare_junctions($sourcedir, $BackupDir, $destdir)
EndIf
While 1
If ProcessExists("swtor.exe") Then
Do
Sleep(100)
Until Not ProcessExists("swtor.exe")
_exit()
EndIf
If Not WinExists($LauncherWindowDesc) Then
$begin = TimerInit()
Do
Sleep(100)
$dif = TimerDiff($begin)
Until ProcessExists("swtor.exe") Or $dif >= 5000
If $dif >= 5000 Then
_exit()
EndIf
EndIf
WEnd
Func _start_launcher($launcher, $LauncherWindowDesc)
Dim $szdrive, $szdir, $szfname, $szext
$srun = $launcher
$apath = _PathSplit($srun, $szdrive, $szdir, $szfname, $szext)
Run($srun, $apath[1] & $apath[2])
Do
Sleep(100)
Until WinExists($LauncherWindowDesc)
Sleep(2000)
EndFunc ;==>_start_launcher
Func _wait_logfile($logfile)
Do
Sleep(100)
Until FileExists($logfile)
EndFunc ;==>_wait_logfile
Func _patching_done($filename)
Local $aLauncherLog
_FileReadToArray($filename, $aLauncherLog)
;msgbox(0, "test", StringLower($aLauncherLog[UBound($aLauncherLog) - 1]))
If StringInStr(StringLower($aLauncherLog[UBound($aLauncherLog) - 1]), "patching end") And _
StringInStr(StringLower($aLauncherLog[UBound($aLauncherLog) - 2]), "patch completed normally") And _
StringInStr(StringLower($aLauncherLog[UBound($aLauncherLog) - 3]), "ManifestState=Complete: retailclient_swtor") Then
Return 1
EndIf
Return 0
EndFunc ;==>_patching_done
Func _softperfect()
$file = FileOpen("C:\ProgramData\SoftPerfect\RAMDiskWS\RAMDiskWS.xml", 0)
If $file = -1 Then
Return -1
EndIf
$content = FileRead($file)
FileClose($file)
$aRamdiskLetter = StringRegExp($content, "(?i:<letter>)(.*?)(?i:</letter>)", 1)
$ramdisk = $aRamdiskLetter[0] & ":"
Return $ramdisk
EndFunc ;==>_softperfect
Func _prepare_junctions($source, $backup, $ramdisk)
DirMove($source, $backup, 1)
FileCreateNTFSLink($ramdisk, $source)
_prepare_runonce()
EndFunc ;==>_prepare_junctions
Func _remove_junctions($source, $backup)
Do ; this is to keep the script waiting until the partition containing the stuff to be restored is mounted
Sleep(100) ; this loop should exit immediately on normal game shutdown, but wait for the partition in question
Until FileExists($source) ; to be mounted first in case of a prior system crash
If _NTFSIsJunction($source) = True Then
DirRemove($source)
DirMove($backup, $source, 1)
EndIf
_kill_runonce()
EndFunc ;==>_remove_junctions
Func _exit()
_remove_junctions($sourcedir, $BackupDir)
Exit
EndFunc ;==>_exit
Func _NTFSIsJunction($string)
Local $FILE_ATTRIBUTE_REPARSE_POINT = 0x400
If Not FileExists($string) Then
Return SetError(1, 0, '')
EndIf
$rc = DllCall('kernel32.dll', 'Int', 'GetFileAttributes', 'str', $string)
If IsArray($rc) Then
If BitAND($rc[0], $FILE_ATTRIBUTE_REPARSE_POINT) = $FILE_ATTRIBUTE_REPARSE_POINT Then
Return True
EndIf
EndIf
Return False
EndFunc ;==>_NTFSIsJunction
Func _prepare_runonce()
If @Compiled = 1 Then
FileCopy(@ScriptDir & "\" & @ScriptName, @TempDir, 1)
$test = _StartupRegistry_Install(@ScriptName, @TempDir & "\" & @ScriptName & " restore", 1, 1)
EndIf
EndFunc ;==>_prepare_runonce
Func _kill_runonce()
If @Compiled = 1 Then
_StartupRegistry_Uninstall(@ScriptName, @TempDir & "\" & @ScriptName & " restore", 1, 1)
FileDelete(@TempDir & "\" & @ScriptName)
EndIf
EndFunc ;==>_kill_runonce