Installer tool code

Share your advanced PureBasic knowledge/code with the community.
BalrogSoft
Enthusiast
Enthusiast
Posts: 203
Joined: Sat Apr 26, 2003 6:33 pm
Location: Spain
Contact:

Installer tool code

Post by BalrogSoft »

Hi, this is the installer tool that i made to include on ScriptVM pack, it's very little, all the project dont have more of 500 lines, but it's good to show how to made an installer program, this is the installer program, you can use this icon for the installer program, and your genereated installer will have a default installer icon, http://www.balrogsoftware.com/imagenes/install.ico
compile as install.exe:

Code: Select all

; Installer utility program

Global Image

Procedure ExtractFile(File.s)
  Address=NextPackFile()
  Size = PackFileSize()
  MemAddress=AllocateMemory(Size)
  CopyMemory(Address,MemAddress,Size)
  If CreateFile(0,File.s)
    WriteData(MemAddress,Size)
    CloseFile(0)
  Else
    MessageBox_(0,"Can't create this file:"+Chr(10)+File.s,WindowName.s,#MB_ICONWARNING)
    ClosePack()
    End
  EndIf
  FreeMemory(MemAddress)
EndProcedure

Procedure.s GetTempFileName()
  temppath.s=Space(255)
  GetTempPath_(255,temppath)
  stemp.s=Space(255)
  GetTempFileName_( temppath, "KPD", 0, @sTemp.s)
  ProcedureReturn sTemp.s
EndProcedure

Enumeration
  #Installer_Window
EndEnumeration
;
Enumeration
  #SVM_LOGO
  #Text
  #Frame3D_0
  #Text_3
  #InstallPath
  #OpenRequester
  #Install
  #Exit
  #ProgressBar
EndEnumeration

a$=Space(255) : GetModuleFileName_(0,@a$,255) : appname$=GetFilePart(a$)

If ReadFile(0, appname$)

  FileSeek(Lof()-12)
  lenpack.l = ReadLong()

  FileSeek(Lof()-8)
  leninstall.l = ReadLong()
 
  FileSeek(Lof()-4)
  lenicon.l = ReadLong()
 
  *mempack = AllocateMemory(lenpack)
  FileSeek(Lof()-12-leninstall-lenpack-lenicon)
  ReadData(*mempack, lenpack) 
  
  *meminstall = AllocateMemory(leninstall)
  FileSeek(Lof()-12-leninstall-lenicon)
  ReadData(*meminstall, leninstall)
  
  *memicon = AllocateMemory(lenicon)
  FileSeek(Lof()-12-lenicon)
  ReadData(*memicon, lenicon) 
     
  tmpfilepack.s=GetTempFilename()
  If CreateFile(1, tmpfilepack.s)
    WriteData(*mempack, lenpack)
    CloseFile(1)
  EndIf
  tmpfileinstall.s=GetTempFilename()
  If CreateFile(1, tmpfileinstall.s)
    WriteData(*meminstall, leninstall) 
    CloseFile(1)
  EndIf
  tmpfileicon.s=GetTempFilename()
  If CreateFile(1, tmpfileicon.s)
    WriteData(*memicon, lenicon) 
    CloseFile(1)
  EndIf

  CloseFile(0)
EndIf
    
Global WindowName.s, WindowText.s, FinalMessage.s

NewList InstallerScript.s()

If ReadFile(0, tmpfileinstall.s)
  WindowName.s=ReadString()
  WindowText.s=ReadString()
  FinalMessage.s=ReadString()
  ProgramDir.s=ReadString()
  While Eof(0)=0
    LastElement(InstallerScript())
    AddElement(InstallerScript())
    InstallerScript()=ReadString()
  Wend
  CloseFile(0)
EndIf

If lenicon>0
  LoadImage(0,tmpfileicon.s)
  UseImage(0)
  Image=ImageID()
EndIf

Procedure Open_Installer_Window()
  If OpenWindow(#Installer_Window, 357, 261, 220, 138,  #PB_Window_SystemMenu | #PB_Window_TitleBar | #PB_Window_ScreenCentered ,  WindowName.s)
    If CreateGadgetList(WindowID())
      ImageGadget(#SVM_LOGO, 5, 5, 32, 32, Image)
      TextGadget(#Text, 40, 5, 175, 30,  WindowText.s, #PB_Text_Center)
      Frame3DGadget(#Frame3D_0, 5, 40, 210, 95, WindowName.s)
      TextGadget(#Text_3, 15, 65, 30, 15, "Path:")
      StringGadget(#InstallPath, 45, 60, 135, 20, "",#PB_String_ReadOnly)
      ButtonGadget(#OpenRequester, 185, 60, 20, 20, "...")
      ButtonGadget(#Install, 15, 85, 90, 20, "Install")
      ButtonGadget(#Exit, 115, 85, 90, 20, "Exit")
      ProgressBarGadget(#ProgressBar, 15, 115, 190, 10, 0, 100)
      
    EndIf
  EndIf
EndProcedure

Open_Installer_Window()

Repeat
  
  Event = WaitWindowEvent()
  
  If Event = #PB_EventCloseWindow
    Quit=1
  EndIf
  
  If Event = #PB_EventGadget
    
    GadgetID = EventGadgetID()
    
    If  GadgetID = #Install
      InstallDir.s=GetGadgetText(#InstallPath)
      CreateDirectory(InstallDir.s)

      If OpenPack(tmpfilepack)
      
        ItemNb=CountList(InstallerScript())
        CountFiles=0

        For It=0 To ItemNb-1
          SelectElement(InstallerScript(),It)    
          If StringField(InstallerScript(),1,"|")="INSTALL"
            ExtractFile(InstallDir.s+StringField(InstallerScript(),3,"|")+StringField(InstallerScript(),2,"|"))
          ElseIf StringField(InstallerScript(),1,"|")="MAKEDIR" 
            CreateDirectory(InstallDir.s+StringField(InstallerScript(),2,"|"))
          EndIf
          percent.f=((It+1)/ItemNb)*100
          SetGadgetState(#ProgressBar, percent.f)
        Next It
      
        ClosePack()
      
      EndIf
    
      MessageBox_(0, FinalMessage.s, WindowName.s,#MB_ICONINFORMATION)
      Quit=1
      
    ElseIf GadgetID = #Exit
      Quit=1
      
    ElseIf GadgetID = #InstallPath
      InstallDir.s=GetGadgetText(#InstallPath)
      
    ElseIf GadgetID = #OpenRequester
      InstallDir.s=PathRequester("A directory called '"+ProgramDir.s+"' will be created."+Chr(10)+Chr(10)+"Select a directory:","")
     
      If Right(InstallDir.s,1)<>"\" : InstallDir+"\" : EndIf
      If Len(InstallDir)>1
        InstallDir.s+ProgramDir.s
      Else 
        InstallDir.s=""
      EndIf
      SetGadgetText(#InstallPath,InstallDir.s)
       
    EndIf
    
  EndIf
  
Until Quit=1

FreeMemory(-1)
DeleteFile(tmpfilepack)
DeleteFile(tmpfileinstall)
DeleteFile(tmpfileicon)

End
This is the installer tool creation, with this code generate the pack, the actions txt file and include on a stand-alone executable file:

Code: Select all

; Installer tool by Balrog Software

Enumeration
  #Window_0
EndEnumeration

Enumeration
  #Frame3D_0
  #Text_0
  #WindowName
  #WindowText
  #Text_1
  #FinalMessage
  #Text_2
  #ListIcon_Actions
  #Frame3D_1
  #AddFile
  #DeleteFile
  #CreateDir
  #IconFile
  #Text_3
  #LoadIconFile
  #Frame3D_3
  #Text_5
  #OutputFile
  #LoadExe
  #GenerateInstaller
  #Frame3D_4
  #Text_6
  #MainDir
  #SVM_LOGO
  #Text
  #Frame3D_5
  #Text_7
  #InstallPath
  #OpenRequester
  #Install
  #Exit
  #ProgressBar
EndEnumeration

Global Image
Image = CatchImage(0, ?Image)

Procedure Open_Window_Installer()
  If OpenWindow(#Window_0, 216, 0, 485, 271,  #PB_Window_SystemMenu | #PB_Window_TitleBar | #PB_Window_ScreenCentered , "ScriptVM Installer Tool")
    If CreateGadgetList(WindowID())
     Frame3DGadget(#Frame3D_0, 5, 5, 235, 125, "Config information")
      TextGadget(#Text_0, 15, 30, 70, 15, "App. name")
      StringGadget(#WindowName, 90, 25, 140, 20, "Application Name")
      StringGadget(#WindowText, 90, 50, 140, 20, "Window Text")
      TextGadget(#Text_1, 15, 55, 70, 15, "Window text")
      StringGadget(#FinalMessage, 90, 75, 140, 20, "")
      TextGadget(#Text_2, 15, 80, 70, 15, "Final message")
      StringGadget(#IconFile, 90, 100, 115, 20, "")
      TextGadget(#Text_3, 15, 105, 65, 15, "Icon image:")
      ButtonGadget(#LoadIconFile, 210, 100, 20, 20, "...")

      ListIconGadget(#ListIcon_Actions, 255, 25, 215, 100, "Action", 70)
      AddGadgetColumn(#ListIcon_Actions, 2, "File name", 100)
      AddGadgetColumn(#ListIcon_Actions, 3, "Destination", 150)
      Frame3DGadget(#Frame3D_1, 245, 5, 235, 180, "Installer actions")
      ButtonGadget(#AddFile, 255, 130, 65, 20, "Add file")
      ButtonGadget(#DeleteFile, 330, 130, 65, 20, "Delete entry")
      ButtonGadget(#CreateDir, 405, 130, 65, 20, "Create dir")
      TextGadget(#Text_6, 255, 160, 85, 15, "Program directory:")
      StringGadget(#MainDir, 345, 155, 125, 20, "")
      
      Frame3DGadget(#Frame3D_3, 245, 190, 235, 75, "Make Installer")
      TextGadget(#Text_5, 255, 215, 55, 15, "Output file:")
      StringGadget(#OutputFile, 310, 210, 135, 20, "")
      ButtonGadget(#LoadExe, 450, 210, 20, 20, "...")
      ButtonGadget(#GenerateInstaller, 255, 235, 110, 20, "Generate Installer")
      ProgressBarGadget(#ProgressBar, 370, 235, 100, 20, 0, 100)
      Frame3DGadget(#Frame3D_4, 5, 135, 235, 130, "Output installer view") 
      
      ImageGadget(#SVM_LOGO, 17, 150, 32, 32, Image)
      TextGadget(#Text, 52, 150, 175, 30,  "Window Text", #PB_Text_Center)
      Frame3DGadget(#Frame3D_5, 17, 185, 210, 75, "Application Name")
      TextGadget(#Text_7, 27, 215, 40, 15, "Path:")
      StringGadget(#InstallPath, 57, 210, 135, 20, "",#PB_String_ReadOnly)
      ButtonGadget(#OpenRequester, 197, 210, 20, 20, "...")
      ButtonGadget(#Install,27, 235, 90, 20, "Install")
      ButtonGadget(#Exit, 127, 235, 90, 20, "Exit")
      
    EndIf
  EndIf
EndProcedure

DataSection:
Image:
IncludeBinary "svm.ico"
EndDataSection

Open_Window_Installer()

WindowName.s="Application Name"
WindowText.s="Window Text"

NewList InstallerScript.s()
NewList InstallerFile.s()

Repeat
  
  Event = WaitWindowEvent()
  
  If Event = #PB_EventGadget

    GadgetID = EventGadgetID()
    
    If GadgetID = #WindowName
      WindowName.s=GetGadgetText(#WindowName)
      SetGadgetText(#Frame3D_5,WindowName)
    
    ElseIf GadgetID = #WindowText
      WindowText.s=GetGadgetText(#WindowText)
      SetGadgetText(#Text,WindowText)
    
    ElseIf GadgetID = #FinalMessage
      FinalMessage.s=GetGadgetText(#FinalMessage)
      
    ElseIf GadgetID = #ListIcon_Actions
      ItemNb=CountGadgetItems(#ListIcon_Actions)
      For It=1 To ItemNb
        If GetGadgetItemState(#ListIcon_Actions, It)=#PB_ListIcon_Selected
          ItemSelected=It
        EndIf 
      Next It
      
    ElseIf GadgetID = #AddFile
      If Len(ProgramDir.s)>1
      NewFile.s=OpenFileRequester("Load filename", "","",0,#PB_Requester_MultiSelection)
      If NewFile
        AddGadgetItem(#ListIcon_Actions,-1,"INSTALL"+Chr(10)+GetFilePart(NewFile)+Chr(10)+"{PATH}\"+ProgramDir.s+Dir.s+GetFilePart(NewFile)+Chr(10))    
        LastElement(InstallerScript())
        LastElement(InstallerFile())
        AddElement(InstallerScript())
        AddElement(InstallerFile())
        
        InstallerScript()="INSTALL|"+GetFilePart(NewFile)+"|"+Dir.s
        InstallerFile()=NewFile
        Repeat
          NewFile.s=NextSelectedFileName()
          If NewFile
            AddGadgetItem(#ListIcon_Actions,-1,"INSTALL"+Chr(10)+GetFilePart(NewFile)+Chr(10)+"{PATH}\"+ProgramDir.s+Dir.s+GetFilePart(NewFile)+Chr(10))    
            LastElement(InstallerScript())
            LastElement(InstallerFile())
            AddElement(InstallerScript())
            AddElement(InstallerFile())
            
            InstallerScript()="INSTALL|"+GetFilePart(NewFile)+"|"+Dir.s
            InstallerFile()=NewFile
          EndIf
        Until Len(NewFile)=0
      EndIf
      Else
      MessageBox_(0,"Set first a root program directory name","Error",#MB_ICONWARNING)
      EndIf
      
    ElseIf GadgetID = #DeleteFile
      RemoveGadgetItem(#ListIcon_Actions, ItemSelected)
      SelectElement(InstallerScript(), ItemSelected)
      SelectElement(InstallerFile(), ItemSelected)
      DeleteElement(InstallerScript())
      DeleteElement(InstallerFile())

    ElseIf GadgetID = #CreateDir
      Dir.s=InputRequester("Create directory","Enter directory name","")
      If Left(Dir.s,1)="\" : Dir.s=Right(Dir.s,Len(Dir.s)-1) : EndIf
      If Right(Dir.s,1)<>"\" : Dir.s+"\" : EndIf
      
      AddGadgetItem(#ListIcon_Actions,-1,"MAKEDIR"+Chr(10)+""+Chr(10)+"{PATH}\"+ProgramDir+Dir+Chr(10))    
      LastElement(InstallerScript())
      LastElement(InstallerFile())
      AddElement(InstallerScript())
      AddElement(InstallerFile())
      InstallerScript()="MAKEDIR|"+Dir.s
      InstallerFile()="{DIR}"
      
    ElseIf GadgetID = #IconFile
      IconFile.s=GetGadgetText(#IconFile)
      
    ElseIf GadgetID = #LoadIconFile
      IconFile.s=OpenFileRequester("Load icon file of 32x32", "","icon file format|*.ico",0)

      SetGadgetText(#IconFile,IconFile)
      LoadImage(1,IconFile)
      SetGadgetState(#SVM_LOGO, UseImage(1))

    ElseIf GadgetID = #OutputFile
      OutputFile.s=GetGadgetText(#OutputFile)
      
    ElseIf GadgetID = #LoadExe
      OutputFile.s=SaveFileRequester("Set output executable name", "","executable format|*.exe",0)
      If UCase(GetExtensionPart(OutputFile.s))<>"EXE"
        OutputFile.s+".exe"
      EndIf 
      SetGadgetText(#OutputFile, OutputFile.s)
      
    ElseIf GadgetID = #GenerateInstaller
      ItemNb=CountList(InstallerScript())
      CountFiles=0
      
      For It=0 To ItemNb-1
        SelectElement(InstallerScript(),It)
        If StringField(InstallerScript(),1,"|")="INSTALL"
          CountFiles+1
        EndIf
      Next It
      SetGadgetState(#ProgressBar,0)
      If CreatePack("tmp.pak")
        For It=0 To ItemNb-1
          SelectElement(InstallerFile(),It)
          
          AddPackFile(InstallerFile(),9)
          percent.f=((It+1)/(ItemNb))*100
          SetGadgetState(#ProgressBar, percent.f)
        Next It
        ClosePack()
      EndIf
      If CreateFile(0,"InstallerActions.txt")
        WriteStringN(WindowName)
        WriteStringN(WindowText)
        WriteStringN(FinalMessage)
        WriteStringN(ProgramDir.s)
        For It=0 To ItemNb-1
          SelectElement(InstallerScript(),It)
          WriteStringN(InstallerScript())
        Next It
        CloseFile(0)
      EndIf
       
      If ReadFile(0,"tmp.pak") 
        lenpack = Lof() 
        If lenpack>0
          *mempack = AllocateMemory(lenpack) 
          ReadData(*mempack, lenpack) 
        EndIf
        CloseFile(0) 
      EndIf 
      
      If ReadFile(0,"InstallerActions.txt") 
        leninstall = Lof() 
        If leninstall>0
          *meminstall = AllocateMemory(leninstall) 
          ReadData(*meminstall, leninstall) 
        EndIf
        CloseFile(0) 
      EndIf
      
      If ReadFile(0,IconFile.s) 
        lenicon = Lof()  
        If lenicon>0
          *memicon = AllocateMemory(lenicon)
          ReadData(*memicon, lenicon) 
        EndIf
        CloseFile(0) 
      EndIf
      
      If lenpack>0 And leninstall>0 And lenicon>0 And Len(FinalMessage.s)>0
        ;Create the installer program with attatched pack archive. 
        If CreateFile(0, OutputFile) 
          WriteData(?filestart, ?fileend-?filestart)  
          WriteData(*mempack, lenpack)  
          WriteData(*meminstall, leninstall)
          WriteData(*memicon, lenicon)
          WriteLong(lenpack)
          WriteLong(leninstall)
          WriteLong(lenicon)
          
          CloseFile(0) 
          MessageBox_(0,"Installer executable was created","Installer",#MB_ICONEXCLAMATION) 
        EndIf 
      Else
        MessageBox_(0,"Can't create install components","Error",#MB_ICONWARNING)
      EndIf
      FreeMemory(*mempack)
      FreeMemory(*meminstall)
      FreeMemory(*memicon)
      DeleteFile("tmp.pak")
      DeleteFile("InstallerActions.txt")
        
    ElseIf GadgetID = #MainDir
      ProgramDir.s=GetGadgetText(#MainDir)
      If Right(ProgramDir.s,1)<>"\" : ProgramDir.s+"\" : EndIf
        
    EndIf
    
  EndIf
  
Until Event = #PB_EventCloseWindow

End

;Include the extraction program 
filestart: 
IncludeBinary "install.exe" 
fileend: 
You will need this icon to compile the last source:
http://www.balrogsoftware.com/imagenes/svm.ico

I hope that it could help or interest to someone, bye.
TronDoc
Enthusiast
Enthusiast
Posts: 310
Joined: Wed Apr 30, 2003 3:50 am
Location: 3DoorsDown

Post by TronDoc »

I know several programmers who will be interested in this - thanks!
Joe
peace
[pI 166Mhz 32Mb w95]
[pII 350Mhz 256Mb atir3RagePro WinDoze '98 FE & 2k]
[Athlon 1.3Ghz 160Mb XPHome & RedHat9]
El_Choni
TailBite Expert
TailBite Expert
Posts: 1007
Joined: Fri Apr 25, 2003 6:09 pm
Location: Spain

Post by El_Choni »

Thx for this, Balrog (do oyou ever stop coding??)
El_Choni
User avatar
NoahPhense
Addict
Addict
Posts: 1999
Joined: Thu Oct 16, 2003 8:30 pm
Location: North Florida

Re: Installer tool code

Post by NoahPhense »

That's sweet.. maybe I build into it, a .prj routine, so that you can load
and save your installs/builds?

- np
Dreglor
Enthusiast
Enthusiast
Posts: 759
Joined: Sat Aug 02, 2003 11:22 pm
Location: OR, USA

Post by Dreglor »

ahh you beat me to this grr
guess i go make mine better :)
~Dreglor
User avatar
NoahPhense
Addict
Addict
Posts: 1999
Joined: Thu Oct 16, 2003 8:30 pm
Location: North Florida

..

Post by NoahPhense »

Dreglor wrote:ahh you beat me to this grr
guess i go make mine better :)
No no, take it on.. It would be a while before I could do it.

- np
Post Reply