forked from OSVR/OSVR-win-installer
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathSetup_service.nsi
446 lines (372 loc) · 14.5 KB
/
Setup_service.nsi
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
/* Copyright 2015 OSVR and contributors
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License. */
!include "FileFunc.nsh"
!include "x64.nsh"
!include "WinVer.nsh"
!include "MUI.nsh"
;--------------------------------
;The nsProcess provides simple macros for handling process control
!include "nsProcess.nsh"
;--------------------------------
;The LogicLib provides some very simple macros that allow easy construction of complex logical structures, see LogicLib.nsh
!include "LogicLib.nsh"
!define MUI_COMPONENTSPAGE_SMALLDESC ;No value
!define MUI_UI "myUI.exe" ;Value
!define MUI_INSTFILESPAGE_COLORS "FFFFFF 000000" ;Two colors
!define PRODUCT_NAME "OSVR Services"
!define PRODUCT_FRIENDLY_NAME "OSVR Services"
!define APP_EXE "OSVR_Server.exe"
!define SERVICE_EXE "OSVR_Service.exe"
!define SERVICE_NAME "OSVR Service"
!define APP_INSTALL_DIR "$PROGRAMFILES\OSVR"
!define LOCAL_DATA_PATH "${APP_INSTALL_DIR}\Data\"
!define UNINSTALL_DIR "${APP_INSTALL_DIR}"
!define SRVC_SETUP_DIR "${APP_INSTALL_DIR}\ServiceSetup"
!define LOCAL_UNINSTALLER_NAME "Uninstall.exe"
!define LOCAL_UNINSTALLER_DIR "${APP_INSTALL_DIR}"
!define OSVRINSTALLLOG "%temp%\OSVRInstall.log"
; Version number needs to be changed when we install a new distribution. It is appended to the installer name just to allow for easy identification
!define REVISION XXXXX
!define VERSION 2.5
!ifndef distroDirectory
!define distroDirectory "..\Distro"
!endif
!define /file CCVERSION "${distroDirectory}\osvr-ver.txt"
; required diskspace in KB
!define PAYLOAD_SIZE "20000"
; icon used for the shortcuts, installer, and uninstaller
icon "osvr_server.ico"
UninstallIcon "osvr_server.ico"
; Splash screen. This may go away at some point.
Function .onInit
IfSilent +6
SetOutPath $TEMP
File /oname=spltmp.bmp "splash.bmp"
advsplash::show 1000 600 400 -1 $TEMP\spltmp
Pop $0 ; $0 has '1' if the user closed the splash screen early, '0' if everything closed normally, and '-1' if some error occurred.
Delete $TEMP\spltmp.bmp
FunctionEnd
### TimeStamp
!ifndef TimeStamp
!define TimeStamp "!insertmacro _TimeStamp"
!macro _TimeStamp FormatedString
!ifdef __UNINSTALL__
Call un.__TimeStamp
!else
Call __TimeStamp
!endif
Pop ${FormatedString}
!macroend
!macro __TimeStamp UN
Function ${UN}__TimeStamp
ClearErrors
## Store the needed Registers on the stack
Push $0 ; Stack $0
Push $1 ; Stack $1 $0
Push $2 ; Stack $2 $1 $0
Push $3 ; Stack $3 $2 $1 $0
Push $4 ; Stack $4 $3 $2 $1 $0
Push $5 ; Stack $5 $4 $3 $2 $1 $0
Push $6 ; Stack $6 $5 $4 $3 $2 $1 $0
Push $7 ; Stack $7 $6 $5 $4 $3 $2 $1 $0
;Push $8 ; Stack $8 $7 $6 $5 $4 $3 $2 $1 $0
## Call System API to get the current system Time
System::Alloc 16
Pop $0
System::Call 'kernel32::GetLocalTime(i) i(r0)'
System::Call '*$0(&i2, &i2, &i2, &i2, &i2, &i2, &i2, &i2)i (.r1, .r2, n, .r3, .r4, .r5, .r6, .r7)'
System::Free $0
IntFmt $2 "%02i" $2
IntFmt $3 "%02i" $3
IntFmt $4 "%02i" $4
IntFmt $5 "%02i" $5
IntFmt $6 "%02i" $6
## Generate Timestamp
;StrCpy $0 "YEAR=$1$\nMONTH=$2$\nDAY=$3$\nHOUR=$4$\nMINUITES=$5$\nSECONDS=$6$\nMS$7"
StrCpy $0 "$1$2$3$4$5$6.$7"
## Restore the Registers and add Timestamp to the Stack
;Pop $8 ; Stack $7 $6 $5 $4 $3 $2 $1 $0
Pop $7 ; Stack $6 $5 $4 $3 $2 $1 $0
Pop $6 ; Stack $5 $4 $3 $2 $1 $0
Pop $5 ; Stack $4 $3 $2 $1 $0
Pop $4 ; Stack $3 $2 $1 $0
Pop $3 ; Stack $2 $1 $0
Pop $2 ; Stack $1 $0
Pop $1 ; Stack $0
Exch $0 ; Stack ${TimeStamp}
FunctionEnd
!macroend
!insertmacro __TimeStamp ""
!insertmacro __TimeStamp "un."
!endif
###########
;--------------------------------
;Check free space
!define sysGetDiskFreeSpaceEx 'kernel32::GetDiskFreeSpaceExA(t, *l, *l, *l) i'
; $0 - space required in kb
; $1 - path to check
; $2 - 0 = ignore quotas, 1 = obey quotas
; trashes $2
function CheckSpaceFunc
IntCmp $2 0 ignorequota
; obey quota
System::Call '${sysGetDiskFreeSpaceEx}(r1,.r2,,.)'
goto converttokb
; ignore quota
ignorequota:
System::Call '${sysGetDiskFreeSpaceEx}(r1,.,,.r2)'
converttokb:
; convert the large integer byte values into managable kb
System::Int64Op $2 / 1024
Pop $2
; check space
System::Int64Op $2 > $0
Pop $2
functionend
;--------------------------------
;General
;Name and file
Name "${PRODUCT_FRIENDLY_NAME}"
OutFile "${PRODUCT_NAME} Setup.exe"
; Admin priviledge is required
RequestExecutionLevel admin
; ShowInstDetails show
; ShowUnInstDetails show
ShowInstDetails nevershow
ShowUnInstDetails nevershow
AutoCloseWindow true
; SetCompressor lzma
;--------------------------------
; Pages
PageEx license
LicenseText "Apache License"
LicenseData License.txt
PageExEnd
Page instfiles
UninstPage uninstConfirm
UninstPage instfiles
;--------------------------------
;Installer Sections
Section "Setting" SEC00
SetShellVarContext all
LogEx::Init "$TEMP\osvr_log.txt"
SectionEnd
Section "VersionCheck" SEC001
;version Checking
${TimeStamp} $0
LogEx::Write true true "$0:Version Check"
ReadRegStr $0 HKLM "SOFTWARE\OSVR" "InstalledVersion"
${if} ${Errors}
;MessageBox MB_OK "Value not found"
Goto InstallThis
${else}
${if} ${CCVERSION} S> $0
;MessageBox MB_OK "Attempting to install version ${CCVERSION} over $0"
${TimeStamp} $0
LogEx::Write true true "$0:Replacing $0 with ${CCVERSION}"
Goto InstallThis
${else}
;MessageBox MB_OK "Attempting to install version ${CCVERSION} over $0, which is not newer than already installed. Quitting installer."
${TimeStamp} $0
LogEx::Write true true "$0:Version Check: $0 is newer than ${CCVERSION}}"
MessageBox MB_YESNO "Installed version is more recent, continue with installation?" /SD IDYES IDNO false2
Goto continue
false2:
Quit
continue:
${endif}
${endif}
/* ${GetFileVersion} "${APP_INSTALL_DIR}\bin\osvr_server.exe" $R0
${VersionCompare} $R0 ${CCVERSION} $R1
${if} "$R1" == "2"
LogEx::Write true true "$0:Replacing $R0 with ${CCVERSION}"
Goto InstallThis
${Else}
${TimeStamp} $0
LogEx::Write true true "$0:Version Check: $R0 is newer than ${CCVERSION}}"
MessageBox MB_OK "Attempting to install version ${CCVERSION}, which is not newer than already installed $R0. Quitting installer."
Quit
${EndIf} */
InstallThis:
; should check for free space here
${TimeStamp} $0
LogEx::Write true true "$0:Free space check"
StrCpy $0 ${PAYLOAD_SIZE} ; kb u need
StrCpy $1 '$PROGRAMFILES' ; check drive c: for space
Call CheckSpaceFunc
IntCmp $2 1 okay
MessageBox MB_OK "Error: Not enough disk space. Please make sure you have at least ${PAYLOAD_SIZE} KB free. $2"
Quit
okay:
SectionEnd
; Check if there is a process already running and kill it so we can install all necessary files.
Section "InitialCleanup" SEC01
${TimeStamp} $0
LogEx::Write true true "$0:InitialCleanup"
SimpleSC::ExistsService "${SERVICE_NAME}"
Pop $0
${If} $0 = 0
SimpleSC::ServiceIsRunning "${SERVICE_NAME}"
Pop $0
${If} $0 = 0
DetailPrint "${SERVICE_NAME} is running. Closing it down"
SimpleSC::StopService "${SERVICE_NAME}" 1
DetailPrint "Removing the ${SERVICE_NAME}"
SimpleSC::RemoveService "${SERVICE_NAME}"
Pop $0
${Else}
DetailPrint "${SERVICE_NAME} not started"
DetailPrint "Removing the ${SERVICE_NAME}"
SimpleSC::RemoveService "${SERVICE_NAME}"
Pop $0
${If} $0 = 0
DetailPrint "${SERVICE_NAME} removed"
${EndIf}
${EndIf}
${EndIf}
;MessageBox MB_OK "nsProcess::Unload$\n$\n"
${nsProcess::Unload}
SectionEnd
Section "MainInstall" SEC02
${TimeStamp} $0
LogEx::Write true true "$0:Main Install"
; We may want to check here first for existing earlier version before installing. We only do overwrite here and no deletion of existing items
; Copy files
SetOverwrite on
SetOutPath "${APP_INSTALL_DIR}"
File /r /x . "${distroDirectory}\*.*"
File osvr_server.ico
SetOutPath "${APP_INSTALL_DIR}\bin"
File OSVR_Service.exe
CreateDirectory "C:\ProgramData\OSVR"
CreateDirectory "C:\ProgramData\OSVR\default"
SectionEnd
Section "EndInstall" SEC04
;Give full access to all users in the system
AccessControl::GrantOnFile "${APP_INSTALL_DIR}" "(S-1-5-32-545)" "FullAccess"
${TimeStamp} $0
LogEx::Write true true "$0:Creating uninstaller"
;Create local uninstaller
CreateDirectory "${LOCAL_UNINSTALLER_DIR}"
WriteUninstaller "${LOCAL_UNINSTALLER_DIR}\${LOCAL_UNINSTALLER_NAME}"
${TimeStamp} $0
LogEx::Write true true "$0:Setting shortcuts"
; create a shortcut named "new shortcut" in the start menu programs directory
; point the new shortcut at the program uninstaller
SetOutPath "${APP_INSTALL_DIR}\bin"
CreateDirectory "$SMPROGRAMS\OSVR"
CreateShortCut "$SMPROGRAMS\OSVR\osvr_server.lnk" "${APP_INSTALL_DIR}\bin\osvr_server.exe" "" "${APP_INSTALL_DIR}\osvr_server.ico"
CreateShortCut "$SMPROGRAMS\OSVR\osvr_uninstall.lnk" "${APP_INSTALL_DIR}\uninstall.exe" "" "${APP_INSTALL_DIR}\osvr_server.ico"
SetOutPath "${APP_INSTALL_DIR}\Profiler"
CreateShortCut "$SMPROGRAMS\OSVR\osvr_config.lnk" "${APP_INSTALL_DIR}\Profiler\OSVR_Config.exe" "" "${APP_INSTALL_DIR}\osvr_server.ico"
; sets the default configuration file. If a user wants to change this, he will have to go in and edit the registry (no tools at the moment)
${TimeStamp} $0
LogEx::Write true true "$0:Setting up Run registry"
WriteRegStr HKLM "Software\OSVR" "InstallationDirectory" "${APP_INSTALL_DIR}\"
WriteRegStr HKLM "Software\OSVR" "InstalledVersion" "${CCVERSION}"
; set up environment variables
; include for some of the windows messages defines
!include "winmessages.nsh"
; HKLM (all users) vs HKCU (current user) defines
!define env_hklm 'HKLM "SYSTEM\CurrentControlSet\Control\Session Manager\Environment"'
!define env_hkcu 'HKCU "Environment"'
; set variable
WriteRegExpandStr ${env_hklm} "OSVR_INSTALL_DIR" "${APP_INSTALL_DIR}\"
; make sure windows knows about the change
SendMessage ${HWND_BROADCAST} ${WM_WININICHANGE} 0 "STR:Environment" /TIMEOUT=5000
; Write the uninstall keys for Windows
WriteRegStr HKLM "Software\Microsoft\Windows\CurrentVersion\Uninstall\osvr_server" "DisplayName" "OSVR services"
WriteRegStr HKLM "Software\Microsoft\Windows\CurrentVersion\Uninstall\osvr_server" "DisplayVersion" "${CCVERSION}"
WriteRegStr HKLM "Software\Microsoft\Windows\CurrentVersion\Uninstall\osvr_server" "UninstallString" "$\"${APP_INSTALL_DIR}\uninstall.exe$\""
WriteRegStr HKLM "Software\Microsoft\Windows\CurrentVersion\Uninstall\osvr_server" "QuietUninstallString" "$\"${APP_INSTALL_DIR}\uninstall.exe$\" /S"
WriteRegStr HKLM "Software\Microsoft\Windows\CurrentVersion\Uninstall\osvr_server" "DisplayIcon" "$\"${APP_INSTALL_DIR}\osvr_server.ico$\""
WriteRegDWORD HKLM "Software\Microsoft\Windows\CurrentVersion\Uninstall\osvr_server" "NoModify" 1
WriteRegDWORD HKLM "Software\Microsoft\Windows\CurrentVersion\Uninstall\osvr_server" "NoRepair" 1
; Install and start service
; DetailPrint "Installing ${SERVICE_NAME}"
; SimpleSC::InstallService "${SERVICE_NAME}" "${SERVICE_NAME}" "16" "2" "${APP_INSTALL_DIR}\bin\OSVR_Service.exe" "" "" ""
; Pop $0
; ${If} $0 = 0
; DetailPrint "${SERVICE_NAME} installation successful."
; ${EndIf}
; DetailPrint "Starting ${SERVICE_NAME}"
; SimpleSC::StartService "${SERVICE_NAME}" 1
; Pop $0
; ${If} $0 = 0
; DetailPrint "${SERVICE_NAME} started."
; ${EndIf}
; ; Execute the server if it exist, otherwise skip
DetailPrint "Installing ${SERVICE_NAME}"
IfFileExists "${APP_INSTALL_DIR}\bin\OSVR_Service.exe" 0 +2
Exec '"${APP_INSTALL_DIR}\bin\OSVR_Service.exe" "-i"'
Sleep 5000
DetailPrint "Starting ${SERVICE_NAME}"
IfFileExists "${APP_INSTALL_DIR}\bin\OSVR_Service.exe" 0 +2
Exec '"${APP_INSTALL_DIR}\bin\OSVR_Service.exe" "-s"'
Sleep 5000
DetailPrint "Launching the OSVR configuration utility."
IfFileExists "${APP_INSTALL_DIR}\Profiler\OSVR_Config.exe" 0 +2
Exec '"${APP_INSTALL_DIR}\Profiler\OSVR_Config.exe"'
${TimeStamp} $0
LogEx::Write true true "$0:Finished with install"
LogEx::Close
SectionEnd
Section "Uninstall"
SetShellVarContext all
SetAutoClose true
LogEx::Init "$TEMP\osvr_log.txt"
${TimeStamp} $0
LogEx::Write true true "$0:Uninstall start"
SimpleSC::ExistsService "${SERVICE_NAME}"
Pop $0
${If} $0 = 0
SimpleSC::ServiceIsRunning "${SERVICE_NAME}"
Pop $0
${If} $0 = 0
DetailPrint "${SERVICE_NAME} is running. Closing it down"
SimpleSC::StopService "${SERVICE_NAME}" 1
DetailPrint "Removing the ${SERVICE_NAME}"
SimpleSC::RemoveService "${SERVICE_NAME}"
Pop $0
${Else}
DetailPrint "${SERVICE_NAME} not started"
DetailPrint "Removing the ${SERVICE_NAME}"
SimpleSC::RemoveService "${SERVICE_NAME}"
Pop $0
${If} $0 = 0
DetailPrint "${SERVICE_NAME} removed"
${EndIf}
${EndIf}
${EndIf}
;MessageBox MB_OK "nsProcess::Unload$\n$\n"
${nsProcess::Unload}
; Clean up environment variable
; delete variable
DeleteRegValue ${env_hklm} "OSVR_INSTALL_DIR"
; make sure windows knows about the change
SendMessage ${HWND_BROADCAST} ${WM_WININICHANGE} 0 "STR:Environment" /TIMEOUT=5000
${TimeStamp} $0
LogEx::Write true true "$0:Cleaning up registry"
DeleteRegKey HKLM "Software\OSVR\"
DeleteRegKey HKLM "Software\Microsoft\Windows\CurrentVersion\Run\${PRODUCT_NAME}"
DeleteRegKey HKLM "Software\Microsoft\Windows\CurrentVersion\Uninstall\${PRODUCT_NAME}"
Delete "$SMPROGRAMS\OSVR\osvr_server.lnk"
Delete "$SMPROGRAMS\OSVR\osvr_uninstall.lnk"
Delete "$SMPROGRAMS\OSVR\osvr_config.lnk"
RMDIR /r "$SMPROGRAMS\OSVR"
; Delete the app folder
RMDIR /r ${APP_INSTALL_DIR}
; Delete the uninstaller folder
RMDIR /r ${UNINSTALL_DIR}
${TimeStamp} $0
LogEx::Write true true "$0:Finished uninstall"
LogEx::Close
SectionEnd