Jump to content

Problem with auto update


Recommended Posts

Hello,

I want create a auto-updater but i never make and google give a hard code.

 

For moment i have this :

;IniWrite("parametres.ini","Version","Version_en_cours","10.0.2008")
Global Const $s_DatFile = 'http://mywebsite.com/update.dat';
Global Const $s_DatFile_Local = @ScriptDir & '\au3_update.dat'
Global Const $b_Download_UpdateDat = 1;0 pour tester en local

$version = IniRead("parametres.ini", "version", "version_en_cours", "1")

If $b_Download_UpdateDat Then
    SplashTextOn("", "Vérification de la Version...Veuillez patienter", 450, 70, -1, -1, 0 + 1 + 16 + 32, "Times New Roman", 12, 800)
    $inetget = InetGet($s_DatFile, $s_DatFile_Local, 1);
    SplashOff()
EndIf

While 1
    If $inetget = 0 And $b_Download_UpdateDat = 1 Then
        $i_Res = MsgBox(6 + 64, 'Erreur de Connexion à Internet', 'Erreurr de internet')
        If $i_Res = 10 Then
            $inetget = InetGet($s_DatFile, $s_DatFile_Local, 1, 1)
           
        ElseIf $i_Res = 11 Then
            ExitLoop
        Else
            Exit
        EndIf
    Else
        Global $s_ReleaseVer
        Local $dData = InetRead('https://www.mywebsite.com/version.txt')
        If @error Then Exit Msgbox(16, "", "Erreur InetRead")
        $sData = BinaryToString($dData)
        $split = StringSplit($sData, "=")
    
        Local $remoteVersion = $split[2]
    
        $version = $remoteVersion

        If _compare($s_ReleaseVer, $version) Then
            $msgversion = MsgBox(4 + 64, "Une nouvelle Version est Disponible", "Souhaitez vous la télécharger?")
        Else
           
            ExitLoop
        EndIf
    EndIf

WEnd

Func _compare($Vers1, $Vers2)

    $Vers1 = StringSplit($Vers1, ".")
    $Vers2 = StringSplit($Vers2, ".")

    If $Vers1[0] < $Vers2[0] Then
        $to = $Vers1[0]
    Else
        $to = $Vers2[0]
    EndIf

    For $i = 1 To $to
        If Int($Vers1[$i]) > Int($Vers2[$i]) Then Return (1)
        If Int($Vers1[$i]) < Int($Vers2[$i]) Then Return (-1)
        If $i = $to And $Vers1[0] > $Vers2[0] Then Return (1)
        If $i = $to And $Vers1[0] < $Vers2[0] Then Return (-1)
        If $i = $to And $Vers1[0] = $Vers2[0] Then Return (0)
    Next

EndFunc   ;==>_compare

 

I have found this code here and i have try to edit :https://www.autoitscript.fr/forum/viewtopic.php?t=1697

 

For check version is ok.

 

I have see this but i never edit or delete a registry

#include <GUIConstantsEx.au3>
#include <StaticConstants.au3>
#include <WindowsConstants.au3>
#include <GUIConstants.au3>
#include <array.au3>
; ========================================
; Predefine variables
; ========================================
Global Const $s_Title = 'Telechargement de la Dernière Version'
Global Const $s_Version = '2.7'
Global Const $s_DatFile = 'http://***.********.***.fr/update.dat'
Global Const $b_Download_UpdateDat = 1
Global Const $s_DatFile_Local = @ScriptDir & '\au3_update.dat'
Global Const $s_Au3UpReg = 'HKLM\SOFTWARE\******'
Global $i_DownSize, $s_DownPath, $s_DownTemp, $s_DownFolder
Global $i_DatFileLoaded, $i_ValidAu3Path, $i_DnInitiated
Global $s_AutoUpdate, $i_SilentInstall
Global $s_CurrDate
Dim $s_ReleaseVer, $s_ReleaseFile, $i_ReleaseSize, $i_ReleaseDate, $s_ReleasePage

; ========================================
; Read registry settings
; ========================================
Global $s_DefDownDir = RegRead($s_Au3UpReg, 'DownloadDir')
If @error Then
    $s_DefDownDir = @DesktopDir
EndIf
Global $s_Au3Path = RegRead($s_Au3UpReg, 'InstallDir')
If Not @error And FileExists($s_Au3Path & '\******.exe') Then
    $s_CurrVer = RegRead($s_Au3UpReg, 'Version')
    $s_CurrDate = _FriendlyDate(FileGetTime($s_Au3Path & "\******.exe", 0, 1))
Else
    $s_Au3Path = "****** n'est pas installé sur cet ordinateur."
    $s_CurrVer = 'Aucune'
    $s_CurrDate = 'Aucune'
EndIf
; ========================================
; GUI - Main Application
; ========================================

Opt("GuiResizeMode", $GUI_DOCKALL)
$gui_Main = GUICreate($s_Title, 350, 310 + 20)
$me_Mn_Help = GUICtrlCreateMenu('&Menu')
$me_Mn_Proxy = GUICtrlCreateMenuItem('Utiliser une connexion avec &Proxy', $me_Mn_Help)
$me_Mn_VisitSite = GUICtrlCreateMenuItem('Aller sur le &Forum de ******', $me_Mn_Help)
$me_Mn_About = GUICtrlCreateMenuItem('&A Propos', $me_Mn_Help)
$lb_separator = GUICtrlCreateLabel('', 0, 0, 350, 2, $SS_SUNKEN)
$gr_Instal_Details = GUICtrlCreateGroup("Détails sur la Version Installée", 5, 5, 340, 70)
GUICtrlCreateLabel('Version Actuelle: ' & $s_CurrVer, 15, 25, 325, 15)
GUICtrlCreateLabel('Date: ' & $s_CurrDate, 15, 40, 325, 15)
GUICtrlCreateLabel("Répertoire d'installation: " & $s_Au3Path, 15, 55, 325, 15)
GUICtrlSetFont(-1, 8, 290, 2)
$gr_Mn_Release = GUICtrlCreateGroup("Dernière Version", 5, 85, 340, 60)
$lb_Mn_ReleaseVer = GUICtrlCreateLabel('Version: Chargement...', 15, 105, 145, 15)
$lb_Mn_ReleaseDate = GUICtrlCreateLabel('Date: Chargement...', 15, 120, 145, 15)
GUIStartGroup()
$ra_Mn_DoneNotify = GUICtrlCreateRadio('&Ne rien faire à la fin du Telechargement', 70, 155, 340, 15)
$ra_Mn_DoneRun = GUICtrlCreateRadio("&Lancer l'installation à la fin du Telechargement", 70, 175, 340, 15)
; Check default done option
If RegRead($s_Au3UpReg, 'DoneOption') = 'Run' Then
    GUICtrlSetState($ra_Mn_DoneRun, $GUI_CHECKED)
Else
    GUICtrlSetState($ra_Mn_DoneNotify, $GUI_CHECKED)
EndIf
$bt_Mn_Close = GUICtrlCreateButton('&Quitter', 10, 275, 330, 25)
; ========================================
; Control Set - Download Buttons
; ========================================
$bt_Mn_ReleaseDl = GUICtrlCreateButton('Télécharger la Dernière Version', 80, 195, 180, 30)
GUICtrlSetState(-1, $GUI_DISABLE)
$lb_Mn_ReleaseSize = GUICtrlCreateLabel('Taille: Chargement...', 100, 230, 165, 15, $SS_CENTER)
$lb_Mn_ReleasePage = GUICtrlCreateLabel('&Page de Telechargement', 100, 245, 165, 15, $SS_CENTER)
GUICtrlSetState(-1, $GUI_DISABLE)
GUICtrlSetFont(-1, 9, 400, 4)
GUICtrlSetColor(-1, 0x0000ff)
GUICtrlSetCursor(-1, 0)
$a_DownButtons = StringSplit($bt_Mn_ReleaseDl & '.' & $lb_Mn_ReleaseSize & '.' & $lb_Mn_ReleasePage, '.')

; ========================================
; Control Set - Download Display
; ========================================
$lb_Mn_DwnToTtl = GUICtrlCreateLabel('Installer dans:', 5, 195, 290, 15, $SS_LEFTNOWORDWRAP)
$lb_Mn_DwnToTxt = GUICtrlCreateLabel('', 5, 210, 290, 15, $SS_LEFTNOWORDWRAP)
$pg_Mn_Progress = GUICtrlCreateProgress(5, 225, 340, 20)
$lb_Mn_Progress = GUICtrlCreateLabel('', 5, 250, 290, 15)
$bt_Mn_OpenFile = GUICtrlCreateButton('&Ouvrir', 105, 275, 75, 25)
GUICtrlSetState(-1, $GUI_DISABLE)
$bt_Mn_OpenFolder = GUICtrlCreateButton('Ouvrir &Répertoire', 185, 275, 75, 25)
GUICtrlSetState(-1, $GUI_DISABLE)
$a_DownDisplay = StringSplit($lb_Mn_DwnToTtl & '.' & _
        $lb_Mn_DwnToTxt & '.' & _
        $pg_Mn_Progress & '.' & _
        $lb_Mn_Progress & '.' & _
        $bt_Mn_OpenFile & '.' & _
        $bt_Mn_OpenFolder, '.')
_GuiCtrlGroupSetState($a_DownDisplay, $GUI_HIDE)
; ========================================
; GUI - About
; ========================================
$gui_About = GUICreate('A Propos', 400, 120, -1, -1, BitOR($WS_CAPTION, $WS_SYSMENU), -1, $gui_Main)
GUICtrlCreateLabel($s_Title & '<><> v' & $s_Version & ' - Utilitaire de téléchargement' & @LF & _
        @LF & _
        "Cette application est un utilitaire pour telecharger facilement" & _
        "la dernière Version de ******." & _
        'Merci à Rob Saunders pour son aide dans le codage.', 5, 5, 390, 75)
$lb_Ab_VisitSite = GUICtrlCreateLabel("Visiter le forum", 5, 100, 145, 15)
GUICtrlSetFont(-1, 9, 400, 4)
GUICtrlSetColor(-1, 0x0000ff)
GUICtrlSetCursor(-1, 0)
GUICtrlSetTip(-1, 'http://******.****.com')
$lb_Ab_ContactAuthor = GUICtrlCreateLabel("Demander de l'aide", 5, 100, 145, 15)
GUICtrlSetFont(-1, 9, 400, 4)
GUICtrlSetColor(-1, 0x0000ff)
GUICtrlSetCursor(-1, 0)
GUICtrlSetTip(-1, '******@gmail.com')
$bt_Ab_Close = GUICtrlCreateButton('&Quitter', 220, 90, 75, 25)
; ========================================
; Application start
; ========================================
GUISetState(@SW_SHOW, $gui_Main)
; Download update data file
If $b_Download_UpdateDat Then
    InetGet($s_DatFile, $s_DatFile_Local, 1)
Else
    FileCopy(@ScriptDir & '\update.dat', $s_DatFile_Local) ; to test locally
EndIf
; Harness GUI Events
While 1
    $a_GMsg = GUIGetMsg(1)
    If Not @InetGetActive And Not $i_DatFileLoaded Then
        If @InetGetBytesRead = -1 And $b_Download_UpdateDat Then
            $i_Res = MsgBox(5 + 16 + 8192, 'Erreur', 'Erreur de connexion au serveur.' & @LF & _
                    'Veuillez vérifier que:' & @LF & _
                    ' - Vous êtes bien connectés à Internet' & @LF & _
                    ' - Vous pouvez acceder  au forum http://******.***.com' & @LF & _
                    " - Votre pare-feu ne bloque pas l'accès Internet à ce programme")
            If $i_Res = 4 Then
                InetGet($s_DatFile, $s_DatFile_Local, 1, 1)
            Else
                Exit
            EndIf
        Else
            _LoadUpdateData()
            $i_ReleaseSizeKB = Round($i_ReleaseSize / 1024)

            If _CompareVersions($s_ReleaseVer, $s_CurrVer) Then
                GUICtrlSetData($gr_Mn_Release, 'Dernière Version *New*')
                GUICtrlSetColor($gr_Mn_Release, 0x0000ff)
            EndIf
            GUICtrlSetData($lb_Mn_ReleaseVer, 'Version: ' & $s_ReleaseVer)
            GUICtrlSetData($lb_Mn_ReleaseDate, 'Date: ' & _FriendlyDate($i_ReleaseDate))
            GUICtrlSetData($lb_Mn_ReleaseSize, 'Taille: ' & $i_ReleaseSizeKB & ' KB')
            GUICtrlSetTip($lb_Mn_ReleasePage, $s_ReleasePage)
            GUICtrlSetState($bt_Mn_ReleaseDl, $GUI_ENABLE)
            GUICtrlSetState($lb_Mn_ReleasePage, $GUI_ENABLE)
            $i_DatFileLoaded = 1
        EndIf
    EndIf
    If $i_DnInitiated Then
        If @InetGetActive Then
            $i_DnPercent = Int(@InetGetBytesRead / $i_DownSize * 100)
            $s_DnBytes = Round(@InetGetBytesRead / 1024) & ' KB'
            $s_DnSize = Round($i_DownSize / 1024) & ' KB'
            GUICtrlSetData($pg_Mn_Progress, $i_DnPercent)
            GUICtrlSetData($lb_Mn_Progress, 'Progression: ' & $i_DnPercent & '% (' & $s_DnBytes & ' of ' & $s_DnSize & ')')
        Else
            GUICtrlSetData($pg_Mn_Progress, 100)
            If Not FileMove($s_DownTemp, $s_DownPath, 1) Then
                MsgBox(16 + 8192, 'Erreur', 'Erreur en déplacant le fichier.')
                GUICtrlSetData($lb_Mn_Progress, 'Erreur')
            Else
                If GUICtrlRead($ra_Mn_DoneRun) = $GUI_CHECKED Then
                    _Start('"' & $s_DownPath & '"')
                    Exit
                Else
                    GUICtrlSetData($lb_Mn_Progress, 'Telechargement Réussi!')
                    GUICtrlSetData($bt_Mn_Close, '&Quitter')
                    GUICtrlSetState($bt_Mn_OpenFile, $GUI_ENABLE)
                    GUICtrlSetState($bt_Mn_OpenFolder, $GUI_ENABLE)
                    $i_Response = MsgBox(4 + 64 + 256 + 8192, $s_Title, 'Telechargement Reussi!' & @LF & _
                            "Voulez vous lancer l'installation?")
                    If $i_Response = 6 Then
                        _Start('"' & $s_DownPath & '"')
                        Exit
                    EndIf
                EndIf
            EndIf
            $i_DnInitiated = 0
        EndIf
    EndIf
    If $a_GMsg[1] = $gui_Main Then
        Select
            ; Radio buttons
            Case $a_GMsg[0] = $ra_Mn_DoneRun
                RegWrite($s_Au3UpReg, 'DoneOption', 'REG_SZ', 'Run')
            Case $a_GMsg[0] = $ra_Mn_DoneNotify
                RegWrite($s_Au3UpReg, 'DoneOption', 'REG_SZ', 'Notify')
                ; Download buttons
            Case $a_GMsg[0] = $bt_Mn_ReleaseDl
                $tmp = StringInStr($s_ReleaseFile, '/', 0, -1)
                $s_DefFileName = StringTrimLeft($s_ReleaseFile, $tmp)
                $i_DownSize = $i_ReleaseSize
                _DownloadFile($s_ReleaseFile, '******-setup.exe')

            Case $a_GMsg[0] = $lb_Mn_ReleasePage
                _Start($s_ReleasePage)

                ; Open buttons
            Case $a_GMsg[0] = $bt_Mn_OpenFile
                _Start('"' & $s_DownPath & '"')
                Exit
            Case $a_GMsg[0] = $bt_Mn_OpenFolder
                _Start('"' & EnvGet('windir') & '\explorer.exe" /select,"' & $s_DownPath & '"')
                Exit
                ; Menu items
            Case $a_GMsg[0] = $me_Mn_Proxy
                If BitAND(GUICtrlRead($me_Mn_Proxy), $GUI_CHECKED) = $GUI_CHECKED Then
                    GUICtrlSetState($me_Mn_Proxy, $GUI_UNCHECKED)
                    HttpSetProxy(0)
                Else
                    GUICtrlSetState($me_Mn_Proxy, $GUI_CHECKED)
                    HttpSetProxy(1)
                EndIf
            Case $a_GMsg[0] = $me_Mn_VisitSite
                _Start('http://www.******.***.com')
            Case $a_GMsg[0] = $me_Mn_About
                GUISetState(@SW_SHOW, $gui_About)
                ; Close buttons
            Case $a_GMsg[0] = $bt_Mn_Close
                _CancelDownload()
            Case $a_GMsg[0] = $GUI_EVENT_CLOSE
                _CancelDownload(1)
        EndSelect
    ElseIf $a_GMsg[1] = $gui_About Then
        Select
            Case $a_GMsg[0] = $lb_Ab_VisitSite
                _Start('http://www.******.***.com')
            Case $a_GMsg[0] = $lb_Ab_ContactAuthor
                _Start('"mailto:******@gmail.com?Subject=Question à Propos du Telechargement de Nouvelle Version"')
            Case $a_GMsg[0] = $GUI_EVENT_CLOSE Or $a_GMsg[0] = $bt_Ab_Close
                GUISetState(@SW_HIDE, $gui_About)
        EndSelect
    EndIf
WEnd


; ========================================
; Function Declarations
; ========================================
; App. specific functions
Func _DownloadFile($s_DownUrl, $s_DownName)
    $s_DownTemp = @TempDir & '\' & $s_DownName
   
    $len = InetGetSize($s_DownUrl)
    If @error Then
        MsgBox(16 + 8192, 'Erreur', "Impossible de commencer le Telechargement." & @CRLF & @CRLF & $s_DownUrl)
        Return
    EndIf
   
    InetGet($s_DownUrl, $s_DownTemp, 1, 1)
    $s_DownPath = FileSaveDialog('Enregistrer', $s_DefDownDir, 'Executables (*.exe)', 16, $s_DownName)
    If Not @error Then
        If Not (StringRight($s_DownPath, 4) = '.exe') Then
            $s_DownPath = $s_DownPath & '.exe'
        EndIf
        $tmp = StringInStr($s_DownPath, '\', 0, -1)
        $s_DownFolder = StringLeft($s_DownPath, $tmp)
        RegWrite($s_Au3UpReg, 'DownloadDir', 'REG_SZ', $s_DownFolder)
        GUICtrlSetData($lb_Mn_DwnToTxt, _ClipPath($s_DownPath, 55))
        GUICtrlSetData($lb_Mn_Progress, 'Progression du Telechargement: Calcul...')
        _GuiCtrlGroupSetState($a_DownButtons, $GUI_HIDE)
        _GuiCtrlGroupSetState($a_DownButtons, $GUI_DISABLE)
        _GuiCtrlGroupSetState($a_DownDisplay, $GUI_SHOW)

        GUICtrlSetData($bt_Mn_Close, 'Cancel')
        $i_DnInitiated = 1
    Else
        InetGet('Abort')
        FileDelete($s_DownTemp)
    EndIf
EndFunc   ;==>_DownloadFile


Func _CancelDownload($i_Flag = 0)
    If $i_DnInitiated Then
        $i_Response = MsgBox(4 + 64 + 256 + 8192, $s_Title, 'Mettre en Pause est Impossible.' & @LF & _
                'Votre téléchargement va être perdu.' & @LF & _
                'Continuer?')
        If $i_Response = 6 Then
            $i_DnInitiated = 0
            InetGet('abort')
            FileDelete($s_DownTemp)
            If $i_Flag = 1 Then
                Exit
            EndIf
            _GuiCtrlGroupSetState($a_DownDisplay, $GUI_HIDE)
            GUICtrlSetData($bt_Mn_Close, '&Quitter')
            If $s_PreBetaVer <> '' Then
                GUICtrlSetPos($bt_Mn_Close, 10, 275, 330 + 175, 25)
            Else
                GUICtrlSetPos($bt_Mn_Close, 10, 275, 330, 25)
            EndIf
            GUICtrlSetData($pg_Mn_Progress, 0)
            _GuiCtrlGroupSetState($a_DownButtons, $GUI_SHOW)
            _GuiCtrlGroupSetState($a_DownButtons, $GUI_ENABLE)
        EndIf
    Else
        Exit
    EndIf
EndFunc   ;==>_CancelDownload


Func _LoadUpdateData()
    Global $s_ReleaseVer, $s_ReleaseFile, $s_ReleasePage, $i_ReleaseSize, $i_ReleaseDate

    $s_ReleaseVer = IniRead($s_DatFile_Local, '******', 'version', 'Erreur de lecture')
    $s_ReleaseFile = IniRead($s_DatFile_Local, '******', 'setup', '')
    $s_ReleasePage = IniRead($s_DatFile_Local, '******', 'index', 'http://******.***.com')
    $i_ReleaseSize = IniRead($s_DatFile_Local, '******', 'filesize', 0)
    $i_ReleaseDate = IniRead($s_DatFile_Local, '******', 'filetime', 0)
    FileDelete($s_DatFile_Local)
EndFunc   ;==>_LoadUpdateData


; Utility functions
Func _Start($s_StartPath)
    If @OSTYPE = 'WIN32_NT' Then
        $s_StartStr = @ComSpec & ' /c start "" '
    Else
        $s_StartStr = @ComSpec & ' /c start '
    EndIf
    Run($s_StartStr & $s_StartPath, '', @SW_HIDE)
EndFunc   ;==>_Start


Func _GuiCtrlGroupSetState(ByRef $a_GroupArray, $i_State)
    For $i = 1 To $a_GroupArray[0]
        GUICtrlSetState($a_GroupArray[$i], $i_State)
    Next
EndFunc   ;==>_GuiCtrlGroupSetState


Func _ClipPath($s_Path, $i_ClipLen)
    Local $i_Half, $s_Left, $s_Right
    If StringLen($s_Path) > $i_ClipLen Then
        $i_Half = Int($i_ClipLen / 2)
        $s_Left = StringLeft($s_Path, $i_Half)
        $s_Right = StringRight($s_Path, $i_Half)
        $s_Path = $s_Left & '...' & $s_Right
    EndIf
    Return $s_Path
EndFunc   ;==>_ClipPath


Func _NumSuffix($i_Num)
    Local $s_Num
    Switch Int($i_Num)
        Case 1, 21, 31
            $s_Num = Int($i_Num) & 'st'
        Case 2, 22
            $s_Num = Int($i_Num) & 'nd'
        Case 3, 23
            $s_Num = Int($i_Num) & 'rd'
        Case Else
            $s_Num = Int($i_Num) & 'th'
    EndSwitch
    Return $s_Num
EndFunc   ;==>_NumSuffix


Func _FriendlyDate($s_Date)
    Local $a_Months = StringSplit('Janvier,Fevrier,Mars,Avril,Mai,Juin,Juillet,Aout,Septembre,Octobre,Novembre,Decembre', ',')
    Local $s_Year, $s_Month, $s_Day
    $s_Year = StringLeft($s_Date, 4)
    $s_Month = StringMid($s_Date, 5, 2)
    $s_Month = $a_Months[Int(StringMid($s_Date, 5, 2))]
    $s_Day = StringMid($s_Date, 7, 2)
    $s_Day = _NumSuffix(StringMid($s_Date, 7, 2))
    Return $s_Month & ' ' & $s_Day & ', ' & $s_Year
EndFunc   ;==>_FriendlyDate


Func _StringInArray($a_Array, $s_String)
    Local $i_ArrayLen = UBound($a_Array) - 1
    For $i = 0 To $i_ArrayLen
        If $a_Array[$i] = $s_String Then
            Return $i
        EndIf
    Next
    SetError(1)
    Return 0
EndFunc   ;==>_StringInArray


Func _CompareVersions($s_Vers1, $s_Vers2, $i_ReturnFlag = 0)
    If $s_Vers1 = '' Then Return 0
    Local $i, $i_Vers1, $i_Vers2, $i_Top
    Local $a_Vers1 = StringSplit($s_Vers1, '.')
    Local $a_Vers2 = StringSplit($s_Vers2, '.')
    $i_Top = $a_Vers1[0]
    If $a_Vers1[0] < $a_Vers2[0] Then
        $i_Top = $a_Vers2[0]
    EndIf
    For $i = 1 To $i_Top
        $i_Vers1 = 0
        $i_Vers2 = 0
        If $i <= $a_Vers1[0] Then
            $i_Vers1 = Number($a_Vers1[$i])
        EndIf
        If $i <= $a_Vers2[0] Then
            $i_Vers2 = Number($a_Vers2[$i])
        EndIf
        If $i_Vers1 > $i_Vers2 Then
            $v_Return = 1
            ExitLoop
        ElseIf $i_Vers1 < $i_Vers2 Then
            $v_Return = 0
            ExitLoop
        Else
            $v_Return = -1
        EndIf
    Next
    If $i_ReturnFlag Then
        Select
            Case $v_Return = -1
                SetError(1)
                Return 0
            Case $v_Return = 1
                Return $s_Vers1
            Case $v_Return = 0
                Return $s_Vers2
        EndSelect
    ElseIf $v_Return = -1 Then
        SetError(1)
        Return 0
    Else
        Return $v_Return
    EndIf
EndFunc   ;==>_CompareVersions


Func _Status($s_MainText, $s_SubText = '', $i_BytesRead = -1, $i_DownSize = -1)
    Global $i_ProgOn
    Global $i_StatusPercent
    If @OSVersion = "WIN_XP" Or @OSVersion = "WIN_2000" Or @OSVersion = "WIN_2003" Then
        If $s_SubText <> '' Then
            $s_SubText = @LF & $s_SubText
        EndIf
        If $i_BytesRead = -1 Then
            TrayTip($s_Title, $s_MainText & $s_SubText, 10, 16)
        Else
            $s_DownStatus = Round($i_BytesRead / 1024) & ' of ' & Round($i_DownSize / 1024) & ' KB'
            TrayTip($s_Title, $s_MainText & $s_SubText & @LF & $s_DownStatus, 10, 16)
        EndIf
    Else
        If Not $i_ProgOn Then
            ProgressOn($s_Title, $s_MainText, $s_SubText, -1, -1, 2 + 16)
            $i_ProgOn = 1
        Else
            If $i_BytesRead = -1 Then
                ProgressSet($i_StatusPercent, $s_SubText, $s_MainText)
            Else
                $s_DownStatus = 'Downloading ' & Round($i_BytesRead / 1024) & ' of ' & Round($i_DownSize / 1024) & ' KB'
                $i_StatusPercent = Round($i_BytesRead / $i_DownSize * 100)
                ProgressSet($i_StatusPercent, $s_DownStatus, $s_MainText)
            EndIf
        EndIf
    EndIf
EndFunc   ;==>_Status

 

But for download and replace file how can i make without registry system but with ini or other?

Please

Edited by AutoDEV
add code
Link to comment
Share on other sites

I use something like this in my projects:

Global $sDomain = 'https://your-domain.tld/'
Global $AppVersion = '1.52.6.0'

Local $Updates = GetAvailableUpdates($sDomain)
; let's say I want to apply the second update
Local $UpdateVersion = GetUpdateVersion($Updates[2])
$CompareVersions = CompareVersions($UpdateVersion)
If $CompareVersions = 0 Then
    MsgBox(0x30, 'Update', 'No update is required.', 10)
ElseIf $CompareVersions > 0 Then
    Local $Update = 'update_' & StringReplace($UpdateVersion, '.', '_') & '.dat'
    ApplyUpdate($Update)
Else
    MsgBox(0x10, 'Update', 'This update is a lower version.', 10)
EndIf
                    
Func GetAvailableUpdates($sDomain)
    Local $sRequest = InetRead($sDomain & 'update.php?command=list')
    Return StringSplit(BinaryToString($sRequest), '|')
EndFunc

Func GetUpdateSize($sFile)
    Local $sRequest = InetRead($Domain & 'update.php?&command=size&file=' & $sFile)
    Return BinaryToString($sRequest)
EndFunc

Func GetUpdateVersion($sFile)
    ; Update files on server are something like
    ; update_1_74_8_2.dat
    Local $sVersion = StringReplace($sFile, 'update_', '')
    $sVersion = StringReplace($sVersion, '.dat', '')
    $sVersion = StringReplace($sVersion, '_', '.')
    Return $sVersion
EndFunc

Func CompareVersions($sFile)
    Local $aCurrent = StringSplit(StringTrimLeft($AppVersion, 1), '.')
    Local $aVersion = StringSplit(StringTrimLeft($sFile, 1), '.')
    If IsArray($aCurrent) And IsArray($aVersion) Then
        If $aCurrent[0] = $aVersion[0] Then
            For $Index = 1 To $aCurrent[0]
                If Number($aCurrent[$Index]) > Number($aVersion[$Index]) Then Return -1
                If Number($aCurrent[$Index]) < Number($aVersion[$Index]) Then Return 1
            Next
        EndIf
    EndIf
    Return 0
EndFunc

Func ApplyUpdate($sFile)
    Local $iRead = 0
    Local $iUpdateSize = GetUpdateSize($sFile)
    If $iUpdateSize Then
        Local $ExePatch = StringTrimRight(@ScriptDir & '\' & $sFile, 4) & '.exe'
        Local $hRequest = InetGet($sDomain & 'update.php?command=' & $sFile, $ExePatch, 1, 1) ; INET_FORCERELOAD, INET_DOWNLOADBACKGROUND
        Do
            $iRead = InetGetInfo($hRequest, 0)
            ConsoleWrite('Progress: ' & Int($iRead * 100 / $iUpdateSize) & '%')
        Until InetGetInfo($hRequest, 2)     ; INET_DOWNLOADCOMPLETE
        If InetGetInfo($hRequest, 3) Then   ; INET_DOWNLOADSUCCESS
            Run($ExePatch & ' --update', @ScriptDir, @SW_HIDE)
        Else
            MsgBox(0x10, 'Error', 'Update file is corrupted or incomplete.', 10)
        EndIf
        InetClose($hRequest)
    Else
        MsgBox(0x10, 'Error', 'Unable to get the size of update.', 10)
    EndIf
EndFunc

and update.php looks like that

$Command = isset($_GET['command']) ? $_GET['command'] : '';
$Command = preg_replace("/[^0-9a-zA-Z\_.]/", "", $Command);

$File = isset($_GET['file']) ? $_GET['file'] : '';
$File = preg_replace("/[^0-9a-zA-Z\_.]/", "", $File);

$dir    = '../../app_versions';

if ($Command == 'list') {
    $files = scandir($dir);
    array_shift($files);
    array_shift($files);
    echo implode('|', $files);
    die;
} elseif ($Command == 'size') {
    if (file_exists($dir . '/' . $File)) {
        echo filesize($dir . '/' . $File);
        die;
    } else {
        echo '0';
        die;
    }
} else {
    if (file_exists($dir . '/' . $Command)) {
        readfile($dir . '/' . $Command);
        die;
    }
}

Since most hosts don't allow you to upload executable files, just change to file extension to .dat or something else or use some sort of the encryption or compression for your files.

Edited by Andreik

When the words fail... music speaks.

Link to comment
Share on other sites

6 hours ago, AutoDEV said:

But for download and replace file how can i make without registry system but with ini or other?

2 hours ago, AutoDEV said:

Sorry but is very hard.

If you have a hard time with an INI file, anything will be difficult. And what are you updating when you don't know how to use an INI file ?

Follow the link to my code contribution ( and other things too ).
FAQ - Please Read Before Posting.
autoit_scripter_blue_userbar.png

Link to comment
Share on other sites

One last chance. I made an example for you with user interface.

Save the script below as MyApp.au3

Global Const $sDomain = 'https://your-domain.tld'
Global Const $sAppVersion = '1.0.0'     ; Current app version

Global $hMain, $cCurrentVersion, $cNewestVersion, $cProgress, $iUpdateSize

; This will check if the application
; has been updated and delete the updater
DeleteUpdater()

; MyApp UI
$hMain = GUICreate('My app', 360, 130)
$cCVLabel = GUICtrlCreateLabel('Current version', 10, 10, 100, 30, 0x200)
$cCurrentVersion = GUICtrlCreateLabel($sAppVersion, 110, 10, 50, 30, 0x1201)
$cNVLabel = GUICtrlCreateLabel('Newest version', 10, 50, 100, 30, 0x200)
$cNewestVersion = GUICtrlCreateLabel('', 110, 50, 50, 30, 0x1201)
$cCheckForUpdate = GUICtrlCreateButton('Check for update', 200, 10, 150, 30)
$cUpdate = GUICtrlCreateButton('Update', 200, 50, 150, 30)
$cProgress = GUICtrlCreateProgress(10, 90, 340, 30)
GUICtrlSetFont($cCVLabel, 10, 500, 0, 'Tahoma')
GUICtrlSetFont($cCurrentVersion, 10, 500, 0, 'Tahoma')
GUICtrlSetFont($cNVLabel, 10, 500, 0, 'Tahoma')
GUICtrlSetFont($cNewestVersion, 10, 500, 0, 'Tahoma')
GUICtrlSetFont($cCheckForUpdate, 10, 500, 0, 'Tahoma')
GUICtrlSetFont($cUpdate, 10, 500, 0, 'Tahoma')
GUISetState(@SW_SHOW, $hMain)

While True
    Switch GUIGetMsg()
        Case $cCheckForUpdate
            CheckForUpdate()
        Case $cUpdate
            UpdateApp()
        Case -3
            Exit
    EndSwitch
WEnd

; This function check if there are new versions available
Func CheckForUpdate()
    Local $sVersion = InetRead($sDomain & '/versions.txt')
    If $sVersion Then
        $sVersion = BinaryToString($sVersion)
        Local $aSplit = StringSplit($sVersion, '|')
        If IsArray($aSplit) Then
            GUICtrlSetData($cNewestVersion, $aSplit[1])
            $iUpdateSize = $aSplit[2]
        EndIf
    EndIf
EndFunc

; This function check if the current version is the newest version,
; otherwise will download the last version and apply the update
Func UpdateApp()
    Local $sCurrentVersion = GUICtrlRead($cCurrentVersion)
    Local $sNewestVersion = GUICtrlRead($cNewestVersion)
    If $sCurrentVersion And $sNewestVersion Then
        Local $fCompare = CompareVersions($sCurrentVersion, $sNewestVersion)
        Switch $fCompare
            Case -1, 0
                MsgBox(0x40, 'Info', 'Current version is the last available.', 10)
            Case 1
                Local $sFileName = StringReplace($sNewestVersion, '.', '_') & '.dat'
                Local $hRequest = InetGet($sDomain & '/versions/' & $sFileName, @ScriptDir & '\update.dat', 1, 1)
                Do
                    $iRead = InetGetInfo($hRequest, 0)
                    GUICtrlSetData($cProgress, Int($iRead * 100 / $iUpdateSize))
                Until InetGetInfo($hRequest, 2)
                GUICtrlSetData($hRequest, 100)
                If InetGetInfo($hRequest, 3) Then
                    FileMove(@ScriptDir & '\update.dat', @ScriptDir & '\update.exe')
                    ShellExecute(@ScriptDir & '\update.exe', ' --update')
                Else
                    MsgBox(0x10, 'Error', 'Download error.', 10)
                EndIf
        EndSwitch
    Else
        MsgBox(0x10, 'Error', 'Please check first if a newer version is available.', 10)
    EndIf
EndFunc

; This function compare current application version with the newest available
; as it is in versions.txt
Func CompareVersions($sCurrentVersion, $sNewestVersion)
    Local $aCurrentVersion = StringSplit($sCurrentVersion, '.')
    Local $aNewestVersion = StringSplit($sNewestVersion, '.')
    If Not IsArray($aCurrentVersion) Or Not IsArray($aNewestVersion) Then Return 0
    If $aCurrentVersion[0] <> $aNewestVersion[0] Then Return 0
    For $Index = 1 To $aCurrentVersion[0]
        If Number($aCurrentVersion[$Index]) > Number($aNewestVersion[$Index]) Then Return -1
        If Number($aCurrentVersion[$Index]) < Number($aNewestVersion[$Index]) Then Return 1
    Next
    Return 0
EndFunc

; This function delete the updater
Func DeleteUpdater()
    If IsArray($CmdLine) Then
        If $CmdLine[0] = 1 Then
            If StringLeft($CmdLine[1], 6) = '--del:' Then
                Local $sProc = StringTrimLeft($CmdLine[1], 6)
                Local $Timeout = 15000
                Local $Timer = TimerInit()
                While (ProcessExists($sProc))
                    ProcessClose($sProc)
                    Sleep(100)
                    If TimerDiff($Timer) > $Timeout Then ExitLoop
                WEnd
                FileDelete(@ScriptDir & '\' & $sProc)
            EndIf
        EndIf
    EndIf
EndFunc

Assume that this script is your actual application. Create two directories: FirstBuild and LastBuild. Then build the script as executable and this will be your first application version (MyApp.exe). Move this executable in FirstBuild directory. To create a new version just change in the script above this line

Global Const $sAppVersion = '1.0.0'     ; Current app version

with a new app version, for example

Global Const $sAppVersion = '1.1.0'     ; Current app version

and build the script again (MyApp.exe). Move this executable in LastBuild directory.

Then prepare a versions.txt file that will contain the info about newest version of the application. This file should look like this:

1.1.0|918528

where first part (1.1.0) it's the latest app version and second part (918528) it's the size of MyApp.exe from LastBuild directory in bytes.

Before uploading this files to your host you have to build an updater. The updater code it's simple and will never change, just provide to FileInstall the full path to latest version of MyApp.exe from LastBuild directory (don't use variables for this, it has to be literal path).

If IsArray($CmdLine) Then
    Switch $CmdLine[0]
        Case 1
            Switch $CmdLine[1]
                Case '--update'
                    Deploy()
            EndSwitch
    EndSwitch
EndIf

Func Deploy()
    Local $Timeout = 15000
    Local $Timer = TimerInit()
    While (ProcessExists('MyApp.exe'))
        ProcessClose('MyApp.exe')
        Sleep(100)
        If TimerDiff($Timer) > $Timeout Then ExitLoop
    WEnd
    If Not ProcessExists('MyApp.exe') Then
        FileInstall('<Full Path to latest version of MyApp.exe>', @ScriptDir & '\MyApp.exe', 1)
        Run('MyApp.exe --del:update.exe', @ScriptDir, @SW_HIDE)
    Else
        MsgBox(0x10, 'Error', 'Update error!', 10)
    EndIf
EndFunc

Build the updater script and rename the executable as 1_1_0.dat since hosts don't accept executable files to be uploaded on the servers. Upload version.txt in your root directory and for 1_1_0.dat create a directory named "versions" in root directory and upload the file. That's all.

Now go and run MyApp.exe from FirstBuild directory, press the button Check for updates and the app will display your latest available version, then you can press Update button and the application will be updated. The current application will be closed by the updater and restarted as latest version. When the latest application will start it will delete the updater.

 

Edited by Andreik

When the words fail... music speaks.

Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
 Share

  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...