AutoHotkey Community

It is currently May 27th, 2012, 1:20 pm

All times are UTC [ DST ]




Post new topic Reply to topic  [ 5 posts ] 
Author Message
PostPosted: August 12th, 2008, 5:21 am 
Offline

Joined: November 23rd, 2007, 10:23 am
Posts: 841
Location: ~/.
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

_________________
Image
    All scripts, unless otherwise noted, are hereby released under CC-BY


Last edited by derRaphael on August 13th, 2008, 2:36 pm, edited 1 time in total.

Report this post
Top
 Profile  
Reply with quote  
 Post subject:
PostPosted: August 12th, 2008, 2:22 pm 
Offline
User avatar

Joined: June 3rd, 2008, 6:26 pm
Posts: 28
Very cool, thanks for sharing. :)


Report this post
Top
 Profile  
Reply with quote  
 Post subject:
PostPosted: August 13th, 2008, 1:51 pm 
Wow this will be very nice for some of my scripts.


Report this post
Top
  
Reply with quote  
 Post subject:
PostPosted: August 13th, 2008, 2:37 pm 
Offline

Joined: November 23rd, 2007, 10:23 am
Posts: 841
Location: ~/.
fixed a lil typo which prevented the script to dump the "more up to date than internetversion" msg.

greets
dR


Report this post
Top
 Profile  
Reply with quote  
 Post subject:
PostPosted: June 3rd, 2009, 7:24 pm 
Offline

Joined: May 30th, 2009, 1:58 am
Posts: 8
Location: Greenville, SC
Does anyone have an updated link for the latestVersion.txt file? Nevermind, I see the usage sample in the source. Still trying to figure out how to make this work. :)


Report this post
Top
 Profile  
Reply with quote  
Display posts from previous:  Sort by  
Post new topic Reply to topic  [ 5 posts ] 

All times are UTC [ DST ]


Who is online

Users browsing this forum: Bon and 16 guests


You can post new topics in this forum
You can reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot post attachments in this forum

Search for:
Powered by phpBB® Forum Software © phpBB Group