[NSIS] Generic Installation and Patch script

Started by Blizzard, January 01, 2011, 07:25:46 am

Previous topic - Next topic

Blizzard

January 01, 2011, 07:25:46 am Last Edit: March 29, 2013, 12:14:25 pm by Blizzard
Since G_G asked for them, here they are. These are basically stripped down versions of the installation creation and patch creation scripts that I use for CP. The scripts have a couple of basic configurable features. You can do a lot more with NSIS than just this. Before you start using these scripts, make sure that you have double checked that all variables are set properly. The script language doesn't seem to support macros and since it doesn't support variables outside of functions, there are a several places where you have to replace the predefined strings with yours (such as the game name). I have added a couple of comments here and there to guide you through the script. Remember that this script could work out of the box for your game, but it most probably won't if you have a custom folder structures and additional files in your game.

Make sure to read and at least partially understand these scripts when you use them. Remember that these are merely generic scripts that are supposed to save you the hassle of organizing your game within Windows and other stuff. You have to customize the script to suit it your needs, there is no way around that.

The installation script will make sure that a proper installation with all necessary registry entries and an uninstaller are created. The patch script allows you configure from which to which version the patch is done. It will also abort if somebody attempts to install the patch without the game being properly installed or if the version of the installed game doesn't match the "from" version of the patch. Keep in mind that a proper installation with all registry entries will work like this, but if somebody messes around with the registry, they can "break" the installation. e.g. You can open the registry and manually set the version string to a different version which will allow you to install a patch on the game that you actually aren't supposed to.

Install Creation Script: ShowHide
Code: Install Creation Script
	!include "MUI2.nsh"
!include "WinMessages.nsh"

Var StartMenuFolder
Var Company
Var GameName
Var Version

# general settings
Name "Awesome Generic Game" # your game name
OutFile "setup.exe"
InstallDir "$PROGRAMFILES\Awesome Generic Game" # where to install the game, default is program files
InstallDirRegKey HKLM "Software\My Company\Awesome Generic Game" "" # registry entry for the game
RequestExecutionLevel admin

!define MUI_ABORTWARNING

!define MUI_HEADERIMAGE
!define MUI_STARTMENUPAGE_REGISTRY_ROOT "HKCU"
!define MUI_STARTMENUPAGE_REGISTRY_KEY "Software\My Company\Awesome Generic Game"
!define MUI_STARTMENUPAGE_REGISTRY_VALUENAME "Awesome Generic Game"

#!insertmacro MUI_PAGE_LICENSE ".\license.txt" # if you have a license
!insertmacro MUI_PAGE_DIRECTORY
!insertmacro MUI_PAGE_STARTMENU Application $StartMenuFolder
!insertmacro MUI_PAGE_INSTFILES
 
!insertmacro MUI_UNPAGE_CONFIRM
!insertmacro MUI_UNPAGE_INSTFILES
!insertmacro MUI_LANGUAGE "English"

# if you have your own icons for the installer and uninstaller
#Icon ".\game.ico"
#UninstallIcon ".\game.ico"

Function un.onInit

StrCpy $GameName "Awesome Generic Game" # your game name

IfFileExists "$INSTDIR\uninstall.exe" Found
MessageBox MB_OK "The uninstallation path incorrect."
Abort
Found:
MessageBox MB_YESNO "This will uninstall $GameName. Do you want to continue?" IDYES NoAbort
Abort # causes uninstaller to quit.
NoAbort:
FunctionEnd

# installer
Section "Install Section" SecInstall

StrCpy $Company "My Company" # your "company"
StrCpy $GameName "Awesome Generic Game" # your game name
StrCpy $Version "1.0" # your game version, is used for patching

# install files
SetOutPath "$INSTDIR"
File /nonfatal "Game.exe"
File /nonfatal "Game.rgssad"
File /nonfatal "Game.ini"
#File "RGSSXXXX.dll" # would be smart to add this file into your game distribution for RTP independent games

# use this if you want only specific folders/files
#SetOutPath "$INSTDIR\Data"
#File /nonfatal "Data\*.*"

# this would simply package all the files in the current directory, is it recommended you use this
#File /r "*.*"

# add more files and folders here if you have any

# uninstaller
WriteUninstaller "$INSTDIR\uninstall.exe"
# registry entries
WriteRegStr HKLM "Software\$Company\$GameName" "Install_Dir" $INSTDIR
WriteRegStr HKLM "Software\Microsoft\Windows\CurrentVersion\Uninstall\$GameName" \
"InstallLocation" "$INSTDIR"
WriteRegStr HKLM "Software\Microsoft\Windows\CurrentVersion\Uninstall\$GameName" \
"DisplayName" "$GameName $Version"
WriteRegStr HKLM "Software\Microsoft\Windows\CurrentVersion\Uninstall\$GameName" \
"DisplayVersion" $Version
WriteRegStr HKLM "Software\Microsoft\Windows\CurrentVersion\Uninstall\$GameName" \
"UninstallString" "$INSTDIR\uninstall.exe"
WriteRegDWORD HKLM "Software\Microsoft\Windows\CurrentVersion\Uninstall\$GameName" \
"NoModify" 0x00000001
WriteRegDWORD HKLM "Software\Microsoft\Windows\CurrentVersion\Uninstall\$GameName" \
"NoRepair" 0x00000001

# start menu
!insertmacro MUI_STARTMENU_WRITE_BEGIN Application
CreateDirectory "$SMPROGRAMS\$StartMenuFolder"
CreateShortCut "$SMPROGRAMS\$StartMenuFolder\$GameName.lnk" "$INSTDIR\Game.exe"
CreateShortCut "$SMPROGRAMS\$StartMenuFolder\Uninstall.lnk" "$INSTDIR\uninstall.exe"
!insertmacro MUI_STARTMENU_WRITE_END

CreateShortCut "$DESKTOP\$GameName.lnk" "$INSTDIR\Game.exe"

# if you have fonts, this piece of code can come in handy
/*
# install fonts
MessageBox MB_YESNO "Do you want the installer to install the required fonts?" IDNO NoFonts
# for every font that you have, copy from here...
IfFileExists "$FONTS\Brush.ttf" +4 0
CopyFiles "$INSTDIR\Fonts\Brush.ttf" "$FONTS" # assuming your fonts are in the Fonts folder in your game, change if necessary
IfFileExists "$FONTS\Brush.ttf" 0 +2
System::Call "GDI32::AddFontResourceA(t) i ('$FONTS\Brush.ttf') .s"
# ... to here

SendMessage ${HWND_BROADCAST} ${WM_FONTCHANGE} 0 0
Goto EndFonts
NoFonts:
MessageBox MB_OK "Please install the fonts manually before running $GameName if the fonts are not installed on your system already."
EndFonts:
*/

SectionEnd

# uninstaller
Section "Uninstall"

# remove start menu folder
!insertmacro MUI_STARTMENU_GETFOLDER Application $StartMenuFolder
Delete "$SMPROGRAMS\*.*"
RMDir "$SMPROGRAMS\$StartMenuFolder"
Delete "$DESKTOP\$GameName.lnk"
# remove from registry
DeleteRegKey HKLM "Software\Microsoft\Windows\CurrentVersion\Uninstall\$GameName"

# remove all files
RMDir /r "$INSTDIR"

SectionEnd

Patch Creation Script: ShowHide
Code: Patch Creation Script
	!include "MUI2.nsh"
!include "WinMessages.nsh"

# general settings
Name "Awesome Generic Game (Patch 1.0 to 1.1)" # make sure you change this
OutFile "patch.exe"
RequestExecutionLevel admin

Var Company
Var GameName
Var Version
Var OldVersion
Var CurrentVersion
Var Dir

!define MUI_ABORTWARNING
!define MUI_HEADERIMAGE

#!insertmacro MUI_PAGE_LICENSE ".\license.txt" # if you have a license
!insertmacro MUI_PAGE_INSTFILES
!insertmacro MUI_LANGUAGE "English"

#Icon "..\game.ico" # if you have your own icon

Function .onInit

StrCpy $Company "My Company" # your "company"
StrCpy $GameName "Awesome Generic Game" # your game name
StrCpy $OldVersion "1.0" # the previous version
StrCpy $Version "1.1" # your new game version

ReadRegStr $Dir HKLM "Software\$Company\$GameName" "Install_Dir"
IfFileExists "$Dir\Game.exe" Found
MessageBox MB_OK "Could not find installation of $GameName! Patcher will abort now."
Abort
Found:
ReadRegStr $CurrentVersion HKLM "Software\Microsoft\Windows\CurrentVersion\Uninstall\$GameName" \
"DisplayVersion"
StrCmp $OldVersion $CurrentVersion PatchOk
MessageBox MB_OK "Error! Version required is $OldVersion. Version found is $CurrentVersion!"
Abort
PatchOk:
MessageBox MB_YESNO "This will patch $GameName from $OldVersion to $Version. Do you want to continue?" IDYES NoAbort
Abort
NoAbort:
FunctionEnd

# installer
Section "Install Section" SecInstall

# install files
SetOutPath $Dir
File /nonfatal "Game.rgssad"

# misc
SetOutPath "$Dir\Data"
File /nonfatal "Data\*.*"

# add more files for patching here, you can obviously use "File /r" if you have already prepared everything in advance

WriteRegStr HKLM "Software\Microsoft\Windows\CurrentVersion\Uninstall\$GameName" \
"DisplayName" "$GameName $Version"
WriteRegStr HKLM "Software\Microsoft\Windows\CurrentVersion\Uninstall\$GameName" \
"DisplayVersion" $Version

SectionEnd
Check out Daygames and our games:

King of Booze 2      King of Booze: Never Ever
Drinking Game for Android      Never have I ever for Android
Drinking Game for iOS      Never have I ever for iOS


Quote from: winkioI do not speak to bricks, either as individuals or in wall form.

Quote from: Barney StinsonWhen I get sad, I stop being sad and be awesome instead. True story.

G_G

I see you windin and grindin up on that flo
You see me looking at you and you already know
I wanna love you!

You are beast blizzard.

G_G

Sorry for double post. Its been more then a week though. Not sure if you would know Blizz but is there a way to do the following:
-Check to see if XNA 3.1 or above is installed
-If not run XNA 3.1 installer
-Install my game

This would get my game more downloads if everything was packed into one installer.

winkio


Blizzard

I think you can make an installation by simply using a Release build configuration and don't have to use NSIS.
Check out Daygames and our games:

King of Booze 2      King of Booze: Never Ever
Drinking Game for Android      Never have I ever for Android
Drinking Game for iOS      Never have I ever for iOS


Quote from: winkioI do not speak to bricks, either as individuals or in wall form.

Quote from: Barney StinsonWhen I get sad, I stop being sad and be awesome instead. True story.

G_G

Well I tried that on a friends computer. It would install the game but it wouldn't install the required files and the game wouldn't even start. I guess I'll try it again and bring it to school.