Update Application From Web

Share your advanced PureBasic knowledge/code with the community.
collectordave
Addict
Addict
Posts: 1309
Joined: Fri Aug 28, 2015 6:10 pm
Location: Portugal

Update Application From Web

Post by collectordave »

I was looking for a way to update my application from the web site and found many posts that seemed to do a little of what I needed so pulled them together into a module.

I sometimes need to update many files in the app and sometimes just one so allowed for that as well.

Here is the code for the module with a little test at the end:-

The code holds two modules the first is for the main application.

Can anyone with more knowledge than me check this out? I want it to be crossplatform (Path Seps OK). Any constructive suggestions are welcome.

CD

Code: Select all

DeclareModule MainApp

;Main application Global Variables and Procedures
Structure Download
  Url.s
  SaveFile.s
  Active.i
EndStructure

Global Dim Updates.Download(0)
  
EndDeclareModule

Module MainApp
  
EndModule




DeclareModule DownloadUpdate
  
 Declare Open()  
  
EndDeclareModule

Module DownloadUpdate
  
  Global winUpdateDownload.i,prgFileProgress.i,txtCurrentDownload.i,txtStatus.i
 
  
  Procedure.i GetDownLoadSize(Url.s)
  
  Define Header.s,Line.s
  Define Size.i,x.i
  
  ;Get Filesize
  Header = GetHTTPHeader(url)
  Repeat
    x + 1
    Line = StringField(Header, x, Chr(10))
  Until FindString(Line, "Content-Length:", 1, #PB_String_NoCase)
  Size = Val(StringField(Line, 2, "Content-Length: "))
  
  ProcedureReturn Size
  
EndProcedure
  
  
  Procedure DownLoadFile(Url.s,SaveFileName.s)
  
  Define FileSize.i,Finished.i
  
  Finished = #False
  
  FileSize = GetDownLoadSize(url)
  
  If FileSize > 0
    ;Set Maximum On Progress Bar
    SetGadgetAttribute(prgFileProgress,#PB_ProgressBar_Maximum ,FileSize)
 
    Download = ReceiveHTTPMemory(url, #PB_HTTP_Asynchronous)
    If Download
      Repeat
        Progress = HTTPProgress(Download)
        Select Progress
          Case #PB_Http_Success
            *Buffer = FinishHTTP(Download)
            If CreateFile(0, SaveFileName) 
              WriteData(0, *Buffer, MemorySize(*Buffer))
              CloseFile(0)  
           EndIf        
           FreeMemory(*Buffer)
           Finished = #True
       
          Case #PB_Http_Failed
            Finished = #True

          Case #PB_Http_Aborted
            Finished = #True
          
          Default
            SetGadgetText(txtStatus,Str(Progress) + " Bytes Downloaded")
            SetGadgetState(prgFileProgress,Progress)
            While WindowEvent()
            Wend
          
        EndSelect
      
        Delay(5) 
      Until  Finished = #True
    Else
      Debug "Download error"
    EndIf
  EndIf
  
EndProcedure

  Procedure DealWithUpdates()
  
    ;General Procedure to copy new files into their places. Delete Old Files etc 
    ;To Finish The Update Process  
    
  EndProcedure

  Procedure Open()
    
    Define iLoop.i
    
    winUpdateDownload = OpenWindow(#PB_Any, 0, 0, 320, 100, "Downloading Updates", #PB_Window_SystemMenu)
    prgFileProgress = ProgressBarGadget(#PB_Any, 10, 40, 300, 20, 0, 0)
    txtCurrentDownload = TextGadget(#PB_Any, 10, 10, 190, 20, "Current Download")
    txtStatus = TextGadget(#PB_Any, 10, 70, 300, 20, "DownloadStatus")
  
    InitNetwork()
  
    ;Check All Files That Can be Updated
    For iLoop = 0 To ArraySize(MainApp::Updates())
      
      ;Only Download New Files
      If MainApp::Updates(iLoop)\Active = #True
        SetGadgetText(txtCurrentDownload,MainApp::Updates(iLoop)\SaveFile)
        DownLoadFile(MainApp::Updates(iLoop)\Url,MainApp::Updates(iLoop)\SaveFile)
      EndIf
      
    Next iLoop
  
    DealWithUpdates()
  
EndProcedure
  
EndModule

    
    
;Just A quick few lines to test

;Load Structured Array With URL,FileName and Active = #False
MainApp::Updates(0)\Url = "http://www.purebasic.com/download/OgreAssimpConverter.zip"
MainApp::Updates(0)\SaveFile = "C:\Temp\OgreAssimpConverter.zip"
MainApp::Updates(0)\Active = #False

;Check For Updates

;I just have a small text file on the website listing files and version numbers which I download
;Check Each Current file against Web Version Number
;If newer file on web then
; Set this files Active Flag to #True
MainApp::Updates(0)\Active = #True ;Just To test
;Endif

;Stop Application. Close Databases etc


;Open The Update Module
DownloadUpdate::Open()

;After Complete
;Open databases and start app again
Any intelligent fool can make things bigger and more complex. It takes a touch of genius — and a lot of courage to move in the opposite direction.
Papala
User
User
Posts: 38
Joined: Wed Sep 12, 2012 5:09 pm

Re: Update Application From Web

Post by Papala »

Thx for sharing :wink:
jassing
Addict
Addict
Posts: 1774
Joined: Wed Feb 17, 2010 12:00 am

Re: Update Application From Web

Post by jassing »

You need some way to know there is an update available (ie: date or version)
I always did this in a text file, and the text file listed the archive to download. In this way, you can either jump to hte most current zip/update or step thru them incrementally to get each update individually.
also, you may want to have an exe dedicated to handling the update, this makes it easier to update files that would be in use otherwise.
I added lua scripting to the exe so I could perform basic tasks (unzipping, copying files, etc) w/o changing any of the binaries, so I'm just testing the lua script
Just some thoughts.
collectordave
Addict
Addict
Posts: 1309
Joined: Fri Aug 28, 2015 6:10 pm
Location: Portugal

Re: Update Application From Web

Post by collectordave »

Thanks for the replies.

I have a small txt file on the server that lists all files and their versions if more thsan one then the structure is loaded appropiatly and all need files downloaded.

Should have mentioned somewhere that this gives you a download with progress that is xplatform as well. No window or other operating system call just pure PB.

Cd
Any intelligent fool can make things bigger and more complex. It takes a touch of genius — and a lot of courage to move in the opposite direction.
Post Reply