DerRaphael
Joined: 23 Nov 2007 Posts: 604 Location: 127.0.0.1
|
Posted: Tue Aug 12, 2008 4:21 am Post subject: [function] AutoUpdater 1.1-yet another selfupdating function |
|
|
hi all,
this is a small script showing a technique of how to update a script. atm it only updates text script. to save binary buffers the save-part has to be updated. it supports self-integrity checking aswell as different error messages.
it requires two additional files being on server
- latestVersion.ahk
a sample of how this should be structures is here or within the script
- scriptName.md5
This is a file containing the md5 digest of the file to download as from md5sum standard output or from selfgenerated hash. in either case the md5 has to be the 1st word on 1st line
and here we go:
| Code: |
#include httpQuery-0-3-3.ahk
; http://www.autohotkey.com/forum/viewtopic.php?t=33506
MyServer := "http://itholic.org/" ; This is where data comes from
MyVersionInfo := "ahk/demo/latestVersion.txt" ; thats the name of latest version info
/* latestVersion.txt - Usage example
1.1
20080811130000
*/
MyMD5Sum := "ahk/demo/versionCheckEN.skript.md5" ; The md5 of latest online file
MyFileDownload := "ahk/demo/versionCheckEN.skript.ahk" ; name of resource
ThisVersion := "1.0"
ThisVersionTime := "20080811125900" ; 2008-08-11 12:59:00
; This Value might come from an iniRead
ShouldDoUpdate := 1
If (ShouldDoUpdate=1) {
UpdateMySelf(Progress:=1)
}
ExitApp
UpdateMySelf(Progress=0){ ; (w) derRaphael / zLib license style release
global
; Lets get the latest info from web
length := httpQuery(VersionInfo := "",MyServer MyVersionInfo)
VarSetCapacity(VersionInfo,-1)
StringSplit,tmp,VersionInfo,`n,`r
netVersion := tmp1
VersionTime := tmp2
if ( ThisVersion != netVersion ) {
CheckTime := ThisVersionTime
EnvSub,CheckTime,%VersionTime%,Seconds
if ( CheckTime < 0 ) { ; Check = ThisVersion-LatestVersion
MsgBox,65,Message,% "There is a newer version available.`n"
. "Should i update?"
IfMsgBox,ok
{
length := httpQuery(md5:="",MyServer MyMD5Sum) ; The md5 hash of the new Script
VarSetCapacity(md5,-1) ; located online
; The script will be downloaded here. If the function has been called to show update
; informations, a progressbar of the download will be displayed
if (Progress=1) {
httpQueryOps := "updateSize"
Progress, 0,, Updating, Please wait
SetTimer,ProgressUpdate,10
}
length := httpQuery(Script:="",MyServer MyFileDownload)
VarSetCapacity(Script,-1)
if (Progress=1) {
httpQueryOps := ""
Progress, OFF
SetTimer,ProgressUpdate,OFF
}
ScriptMD5 := hash(Script,length)
If (ScriptMD5=RegExReplace(md5,"\s.*")) { ; Versions are equal
SplitPath,A_ScriptFullPath,,,Extension,NameNoExt
FileMove,%A_ScriptFullPath%,%A_ScriptDir%\%NameNoExt%-%ThisVersion%.%Extension%
FileAppend,%Script%,%A_ScriptFullPath%
MsgBox,65,Message: Success, Updated successfully. Press OK to restart.
} else {
MsgBox,16,Message: Error, There was an updating Error. The old version remains
}
}
} else if ( CheckTime > 0 ) {
MsgBox,16,Message: Error, This version is more 'uptodate' than the latest available online version.
} else {
MsgBox,64,Message, No new update.
}
}
return
ProgressUpdate:
Progress, % Round((HttpQueryCurrentSize/HttpQueryFullSize)*100)
return
}
HASH(ByRef sData, nLen, SID = 3) { ; SID = 3: MD5, 4: SHA1
; Laszlo: http://www.autohotkey.com/forum/viewtopic.php?p=113252#113252
DllCall("advapi32\CryptAcquireContextA", UIntP,hProv, UInt,0, UInt,0, UInt,1, UInt,0xF0000000)
DllCall("advapi32\CryptCreateHash", UInt,hProv, UInt,0x8000|0|SID, UInt,0, UInt,0, UIntP, hHash)
DllCall("advapi32\CryptHashData", UInt,hHash, UInt,&sData, UInt,nLen, UInt,0)
DllCall("advapi32\CryptGetHashParam", UInt,hHash, UInt,2, UInt,0, UIntP,nSize, UInt,0)
VarSetCapacity(HashVal, nSize, 0)
DllCall("advapi32\CryptGetHashParam", UInt,hHash, UInt,2, UInt,&HashVal, UIntP,nSize, UInt,0)
DllCall("advapi32\CryptDestroyHash", UInt,hHash)
DllCall("advapi32\CryptReleaseContext", UInt,hProv, UInt,0)
IFormat := A_FormatInteger
SetFormat Integer, H
Loop %nSize%
sHash .= SubStr(*(&HashVal+A_Index-1)+0x100,-1)
SetFormat Integer, %IFormat%
Return sHash
} |
have fun!
greets
dR _________________
Last edited by DerRaphael on Wed Aug 13, 2008 1:36 pm; edited 1 time in total |
|