Jump to content
Sign in to follow this  
nht3004

Create your own Auto-Update with progress bar!

Recommended Posts

nht3004

I'm new to Autoit, and I have worked this project for 3 days ^_^, give me advice bro.

First, you have 2 files, AutoUpdate.au3 and config.nht

Here is the AutoUpdate.au3

#cs---------------------------------------------------------------------------------------------------------------------------------#
Student manager project - Auto Updates
Author : Nguyễn Huy Trường
Yahoo Address : nht3004@yahoo.com
Do not make illegal copy!
#ce---------------------------------------------------------------------------------------------------------------------------------#

#include <config.nht>
#include <GuiEdit.au3>
#include <GUIConstantsEx.au3>
#include <EditConstants.au3>
#include <WindowsConstants.au3>
#include <String.au3>
#include <Array.au3>
#include <File.au3>

;########################################## These are some basic script to use ###############################################

Func getValue($url)
    $fileDownloadable=InetGet($url, @ScriptDir&"\"&$dataFileName);
    If($fileDownloadable==1) Then
    $returnValue=FileRead(@ScriptDir&"\"&$dataFileName);
    Return $returnValue;
    ElseIf($fileDownloadable==0) Then
    Return -1;
    EndIf
EndFunc

Func alert($txt)
    MsgBox(0, $alertTitle, $txt);
EndFunc

Func readReg($regKey, $regName)
    Return RegRead("HKEY_LOCAL_MACHINE\SOFTWARE\NHT3004\"&$regKey,$regName);
EndFunc
    
Func writeReg($regKey, $regName, $regValue)
    RegWrite("HKEY_LOCAL_MACHINE\SOFTWARE\NHT3004\"&$regKey,$regName,"REG_SZ",$regValue);
EndFunc

Func deleteReg($regKey, $regValue)
    RegDelete("HKEY_LOCAL_MACHINE\SOFTWARE\NHT3004\"&$regKey,$regValue);
EndFunc

Func progressBy($IDOfProgressBar, $valueToProgressBy)
    $aimValue=Execute($currentProgress+$valueToProgressBy);
    If($aimValue>=100) Then
        GUICtrlSetData($IDOfProgressBar,100);
    Else
        While $currentProgress<$aimValue
            GUICtrlSetData($IDOfProgressBar,Execute($currentProgress+$percentPerUp));
            $currentProgress+=$percentPerUp;
            Sleep($speedOfUpdate);
        WEnd
    EndIf
    $currentProgress=$aimValue;
EndFunc

Func progressTo($IDOfProgressBar,$valueToProgressTo)
    If($valueToProgressTo>=100) Then
        GUICtrlSetData($IDOfProgressBar,100);
    Else
        While $currentProgress<$valueToProgressTo
            GUICtrlSetData($IDOfProgressBar,Execute($currentProgress+$percentPerUp));
            $currentProgress+=$percentPerUp;
            Sleep($speedOfUpdate);
        WEnd
    EndIf
    $currentProgress=$valueToProgressTo;
EndFunc

Func downloadFile($fileDownloadLink, $fileDownloadName)
    
    Global $downloadProgress;

    $progressForm=GUICreate("Downloading updates...", 220, 50, 0, 0);
    $downloadProgress=GUICtrlCreateProgress(10, 10, 200, 20);
    GUICtrlSetColor(-1, 32250);
    GUISetState(@SW_SHOW);
    
    $downloadFileSize=InetGetSize($fileDownloadLink);
    $isDownloaded=InetGet($fileDownloadLink, $fileDownloadName, "", 1);
    
    While @InetGetBytesRead<$downloadFileSize;
        progressTo($downloadProgress, @InetGetBytesRead/$downloadFileSize*100);
    WEnd
    
    GUIDelete($progressForm);Aftet delete progressForm, plz set $currentProgress to 0;
    $currentProgress=0;
    
    Return $isDownloaded;
EndFunc

Func sizeOf($arrayName)
    SetError(0);
    $index=0;
    Do
        $pop=_ArrayPop($arrayName);
        $index+=1;
    Until @error=1;
    Return $index-1;
EndFunc

Func encode($txt, $pass, $lvl)
    Return _StringEncrypt(1, BinaryToString(StringToBinary($txt, $Flag_UTF8), $Flag_ANSI), $pass, $lvl);
EndFunc

Func decode($txt, $pass, $lvl)
    Return BinaryToString(StringToBinary(_StringEncrypt(0, $txt, $pass, $lvl), $Flag_ANSI), $Flag_UTF8);
EndFunc

;########################################## End basic script programmed ######################################################


;****************************************** Start main script of project *****************************************************
;-------------------------------------------------Create a progress bar---------------------------------------------------

Global $updateProgress

$progressForm=GUICreate("Checking for updates...", 220, 50, 0, 0);
$updateProgress=GUICtrlCreateProgress(10, 10, 200, 20);
GUICtrlSetColor(-1, 32250);
GUISetState(@SW_SHOW);

;-------------------------------------------------Get new and old Version-------------------------------------------------

$newVersion=getValue($linkCheckVersion);After getValue(), plz delete the file downloaded!
progressBy($updateProgress,$percentPerAction);
FileDelete(@ScriptDir&"\"&$dataFileName);

If Not($newVersion==-1) Then
    progressBy($updateProgress,$percentPerAction);
    $currentVersion=readReg("SM","Version");
    progressBy($updateProgress,$percentPerAction);
    $fileLink=getValue($linkCheckFileUrl);After getValue(), plz delete the file downloaded!
    progressBy($updateProgress,$percentPerAction);
    FileDelete(@ScriptDir&"\"&$dataFileName);
    $newFeatures=getValue($linkCheckNewFeatures);After getValue(), plz delete the file downloaded!
    progressBy($updateProgress,$percentPerAction);
    FileDelete(@ScriptDir&"\"&$dataFileName);
    progressBy($updateProgress,$percentPerAction);

;-------------------------------------------------Start check for updates-------------------------------------------------

    GUIDelete($progressForm);Aftet delete progressForm, plz set $currentProgress to 0;
    $currentProgress=0;
    
    If($currentVersion=="") Then
        writeReg("SM","Version",$projectVersion);
    Else
        If(Execute($newVersion)>Execute($currentVersion)) Then
            $downloadConfirm=MsgBox(1, "Alarm", "New version : "&$newVersion&" available."&@LF&"New features : "&@LF&$newFeatures&@LF&"Do you want to download?");
            If($downloadConfirm==1) Then
                $downloadable=downloadFile($fileLink, $fileName);
                If($downloadable==0) Then
                    alert("Can not download updates, please try again later!");
                Else
                    writeReg("PM","Version",$newVersion);
                    alert("Updates downloaded successfully, click OK to Run Installation!");
                    Run($fileName);
                    Exit;
                EndIf
            EndIf
        EndIf
    EndIf
;-------------------------------------------------End updates check-------------------------------------------------------
Else
    progressTo($updateProgress,100);
    GUIDelete($progressForm);
EndIf

;****************************************** End script of this project *******************************************************oÝ÷ Ø  ÝêÞËay·(ø n+b²g«z~)^¶^rV«zØ^½ªâi¹^±«­¢+ØìQ¡ÍÍÉ¥ÁÐÝ¥±°±ÉͽµÙÉ¥±Ì()±½°ÀÌØí±ÉÑQ¥Ñ±ôÅÕ½Ðí±É´ÅÕ½Ðìí±Éѡѥѱ½5Í   ½à ¤)±½°ÀÌØíÑ¥±9µôÅÕ½Ðíѹ¹¡ÐÅÕ½Ðìí±ÉÑ¡¥±¹µÑ¼ÍÙÝ¡¸¡¬ÙÉÍ¥½¸)±½°ÀÌØíÁɽ©ÑYÉÍ¥½¸ôÅÕ½ÐìĸÀÅÕ½Ðìí±ÉÑ¡ÙÉÍ¥½¸½Áɽ©Ð¥¸Í½¹½ÐÍÐ)±½°ÀÌØí¥±AÉ¥àôÅÕ½ÐíM5@ÅÕ½Ðìí±ÉÑ¡¥±¹µ½ÉÑ¡½Ð)±½°ÀÌØí¥±áѹͥ½¸ôÅÕ½ÐíáÅÕ½Ðìí±ÉÑ¡áѹͥ½¸½¥±)±½°ÀÌØí¥±9µôÀÌØí¥±AÉ¥àµÀìÅÕ½Ðì¸ÅÕ½ÐìµÀìÀÌØí¥±áѹͥ½¸í±ÉÑ¡¥±¹µ)±½°ÀÌØíÁɹÑAÉÑ¥½¸õ9ÕµÈ ÄÀÀ¼Ô¤í±ÉÑ¡ÁɹÑÑ¡Ð¥ÌÕÀÙÉäÑ¥½¸)±½°ÀÌØíÕÉɹÑAɽÉÍÌôÀí±ÉÑ¡ÁɽÉÍ̽½¥¹¹½Ñ¡¥¹)±½°ÀÌØí±¥¹­
¡­YÉÍ¥½¸ôÅÕ½Ðí¡ÑÑÀè¼½ÝÝܹ¹¡ÐÌÀÀиÁ̹¹Ð½ÙÉÍ¥½¸¹¹¡ÐÅÕ½Ðìí±ÉÑ¡±¥¹¬Ñ¼¡¬¹ÜÙÉÍ¥½¸Ù±Õì)±½°ÀÌØí±¥¹­
¡­¥±UÉ°ôÅÕ½Ðí¡ÑÑÀè¼½ÝÝܹ¹¡ÐÌÀÀиÁ̹¹Ð½¹ÝYÉÍ¥½¹¥±1¥¹¬¹¹¡ÐÅÕ½Ðìí±ÉÑ¡±¥¹¬Ñ¡ÐÕÍѼ½Ý¹±½¹ÜÁɽ©Ð)±½°ÀÌØí±¥¹­
¡­9ÝÑÕÉÌôÅÕ½Ðí¡ÑÑÀè¼½ÝÝܹ¹¡ÐÌÀÀиÁ̹¹Ð½¹ÝÑÕÉ̹¹¡ÐÅÕ½Ðìí±ÉÑ¡±¥¹¬Ñ¡ÐÕÍѼ͡½Ü¹ÜÕÁѵÑÕÉÌ)±½°ÀÌØíÍÁ=UÁÑôÄí±ÉÑ¡ÍÁ¡µÌ¤½ ÕÀÁɽÉÍÌ)±½°ÀÌØíÁɹÑAÉUÀôÀ¸Ôí±ÉÑ¡ÁɹÐѼÁɽÉÍÌ Ñ¥µ)±½°
½¹ÍÐÀÌØí±}UQàôÐì)±½°
½¹ÍÐÀÌØí±}9M$ôÄì((칱ɥ¹oÝ÷ Ù.ë-ç¶×(è]4ÓK)j{-ÊW

By the way, do not keep your eyes on Basic Functions, 'cause I paste them for all of my work!

Have fun.

PS: I'm newbie so there might be so much errors, please notify me if you see, thanks ^^!

After download, rename config.nht.txt to config.nht

AutoUpdate.au3

Config.nht.txt

Edited by nht3004
  • Like 1

for(loop=0; loop<infinity; loop++) { alert('I love you'); }

Share this post


Link to post
Share on other sites
johnkread

I was just thinking about creating something similar for one of my apps. How has the testing gone? I'll be sure and try it out tonight or tommorow and let you know.

John

Share this post


Link to post
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
Sign in to follow this  

×