AutoHotkey Homepage AutoHotkey Community
Let's help each other out
 
 FAQFAQ   SearchSearch   MemberlistMemberlist   RegisterRegister 
 ProfileProfile   Log in to check your private messagesLog in to check your private messages   Log inLog in 

[function] AutoUpdater 1.1-yet another selfupdating function

 
Post new topic   Reply to topic    AutoHotkey Community Forum Index -> Scripts & Functions
View previous topic :: View next topic  
Author Message
DerRaphael



Joined: 23 Nov 2007
Posts: 604
Location: 127.0.0.1

PostPosted: Tue Aug 12, 2008 4:21 am    Post subject: [function] AutoUpdater 1.1-yet another selfupdating function Reply with quote

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

  1. latestVersion.ahk
    a sample of how this should be structures is here or within the script
  2. 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
Back to top
View user's profile Send private message
Barrow



Joined: 03 Jun 2008
Posts: 3

PostPosted: Tue Aug 12, 2008 1:22 pm    Post subject: Reply with quote

Very cool, thanks for sharing. Smile
Back to top
View user's profile Send private message
whois
Guest





PostPosted: Wed Aug 13, 2008 12:51 pm    Post subject: Reply with quote

Wow this will be very nice for some of my scripts.
Back to top
DerRaphael



Joined: 23 Nov 2007
Posts: 604
Location: 127.0.0.1

PostPosted: Wed Aug 13, 2008 1:37 pm    Post subject: Reply with quote

fixed a lil typo which prevented the script to dump the "more up to date than internetversion" msg.

greets
dR
Back to top
View user's profile Send private message
Display posts from previous:   
Post new topic   Reply to topic    AutoHotkey Community Forum Index -> Scripts & Functions All times are GMT
Page 1 of 1

 
Jump to:  
You can post new topics in this forum
You can reply to topics in this forum


Powered by phpBB © 2001, 2005 phpBB Group