1;***************************************************** from here on come licence ***************************************** 2 3!include "TextFunc.nsh" 4 5; Usage ... 6; Push "|" ;divider char 7; Push "string1|string2|string3|string4|string5" ;input string 8; Call SplitFirstStrPart 9; Pop $R0 ;1st part ["string1"] 10; Pop $R1 ;rest ["string2|string3|string4|string5"] 11Function SplitFirstStrPart 12 Exch $R0 13 Exch 14 Exch $R1 15 Push $R2 16 Push $R3 17 StrCpy $R3 $R1 18 StrLen $R1 $R0 19 IntOp $R1 $R1 + 1 20 loop: 21 IntOp $R1 $R1 - 1 22 StrCpy $R2 $R0 1 -$R1 23 StrCmp $R1 0 exit0 24 StrCmp $R2 $R3 exit1 loop 25 exit0: 26 StrCpy $R1 "" 27 Goto exit2 28 exit1: 29 IntOp $R1 $R1 - 1 30 StrCmp $R1 0 0 +3 31 StrCpy $R2 "" 32 Goto +2 33 StrCpy $R2 $R0 "" -$R1 34 IntOp $R1 $R1 + 1 35 StrCpy $R0 $R0 -$R1 36 StrCpy $R1 $R2 37 exit2: 38 Pop $R3 39 Pop $R2 40 Exch $R1 ;rest 41 Exch 42 Exch $R0 ;first 43FunctionEnd 44 45 46; we just ask for Artistic licences, the other are only asked for certain paks 47PageEx license 48 LicenseData "license.rtf" 49PageExEnd 50 51 52; If not installed to program dir, ask for a portable installation 53Function CheckForPortableInstall 54 ; defaults in progdir, and ending with simutrans 55 StrCpy $installinsimutransfolder "1" 56 StrCpy $multiuserinstall "1" 57 ; if the destination directory is the program dir, we must use use own documents directory for data 58 StrCmp $INSTDIR $PROGRAMFILES\Simutrans AllSetPortable 59 StrLen $1 $PROGRAMFILES 60 StrCpy $0 $INSTDIR $1 61 StrCmp $0 $PROGRAMFILES YesPortable +1 62 StrCpy $multiuserinstall "0" ; check whether we already have a simuconf.tab, to get state from file 63 IfFileExists "$INSTDIR\config\simuconf.tab" 0 PortableUnknown 64 ; now we have a config. Without single_user install, it will be multiuser 65 StrCpy $multiuserinstall "1" 66 ${ConfigRead} "$INSTDIR\config\simuconf.tab" "singleuser_install" $R0 67 IfErrors YesPortable 68 ; skip a leading space 69PortableSpaceSkip: 70 StrCpy $1 $R0 1 71 StrCmp " " $1 0 +3 72 StrCpy $R0 $R0 100 1 73 Goto PortableSpaceSkip 74 ; skip the equal char 75 StrCpy $R0 $R0 10 1 76 IntOp $multiuserinstall $R0 ^ 1 77 Goto AllSetPortable 78 79PortableUnknown: 80 ; ask whether this is a portable installation 81 MessageBox MB_YESNO|MB_ICONINFORMATION "Should this be a portable installation?" IDYES YesPortable 82 StrCpy $multiuserinstall "1" 83YesPortable: 84 ; now check, whether the path ends with "simutrans" 85 StrCpy $0 $INSTDIR 9 -9 86 StrCmp $0 simutrans +2 87 StrCpy $installinsimutransfolder "0" 88AllSetPortable: 89 ; here everything is ok 90FunctionEnd 91 92PageEx directory 93 PageCallbacks "" "" CheckForPortableInstall 94PageExEnd 95 96 97 98PageEx components 99 PageCallbacks componentsPre 100PageExEnd 101 102 103; If for the pak in this section exists, it will be preselected 104Function EnableSectionIfThere 105 Exch $R0 106 Push $R1 107 SectionGetText $R0 $R1 108 ; now only use the part until the space ... 109 Push " " 110 Push $R1 111 Call SplitFirstStrPart 112 Pop $R1 113 IfFileExists "$INSTDIR\$R1\ground.Outside.pak" 0 NotExistingPakNotSelected 114 SectionGetFlags $R0 $R1 115 IntOp $R1 $R1 | ${SF_SELECTED} 116 SectionSetFlags $R0 $R1 117 118NotExistingPakNotSelected: 119 Pop $R1 120 Exch $R0 121FunctionEnd 122 123 124; set pak for update/skip if installed 125Function componentsPre 126 Push ${pak64german} 127 Call EnableSectionIfThere 128 Push ${pak64japan} 129 Call EnableSectionIfThere 130 Push ${pak64nippon} 131 Call EnableSectionIfThere 132 Push ${pak64HO} 133 Call EnableSectionIfThere 134 Push ${pak64HAJO} 135 Call EnableSectionIfThere 136 Push ${pak64contrast} 137 Call EnableSectionIfThere 138 Push ${pak96comic} 139 Call EnableSectionIfThere 140 Push ${pak96HD} 141 Call EnableSectionIfThere 142 Push ${pak128} 143 Call EnableSectionIfThere 144 Push ${pak128britain} 145 Call EnableSectionIfThere 146 Push ${pak128german} 147 Call EnableSectionIfThere 148 Push ${pak128japan} 149 Call EnableSectionIfThere 150 Push ${pak128cz} 151 Call EnableSectionIfThere 152 Push ${pak192comic} 153 Call EnableSectionIfThere 154 Push ${pak64scifi} 155 Call EnableSectionIfThere 156 Push ${pak48excentrique} 157 Call EnableSectionIfThere 158 Push ${pak32comic} 159 Call EnableSectionIfThere 160FunctionEnd 161 162; Some paksets don't have an open source license, so we have to show additional licences 163Function CheckForClosedSource 164 165 SectionGetFlags ${pak64german} $R0 166 IntOp $R0 $R0 & ${SF_SELECTED} 167 IntCmp $R0 ${SF_SELECTED} showFW 168 169 SectionGetFlags ${pak64HAJO} $R0 170 IntOp $R0 $R0 & ${SF_SELECTED} 171 IntCmp $R0 ${SF_SELECTED} showFW 172 173 SectionGetFlags ${pak64nippon} $R0 174 IntOp $R0 $R0 & ${SF_SELECTED} 175 IntCmp $R0 ${SF_SELECTED} showFW 176 177 SectionGetFlags ${pak96comic} $R0 178 IntOp $R0 $R0 & ${SF_SELECTED} 179 IntCmp $R0 ${SF_SELECTED} showFW 180 181 SectionGetFlags ${pak96HD} $R0 182 IntOp $R0 $R0 & ${SF_SELECTED} 183 IntCmp $R0 ${SF_SELECTED} showFW 184 185 SectionGetFlags ${pak128japan} $R0 186 IntOp $R0 $R0 & ${SF_SELECTED} 187 IntCmp $R0 ${SF_SELECTED} showFW 188 189 SectionGetFlags ${pak128german} $R0 190 IntOp $R0 $R0 & ${SF_SELECTED} 191 IntCmp $R0 ${SF_SELECTED} showFW 192 193 SectionGetFlags ${pak192comic} $R0 194 IntOp $R0 $R0 & ${SF_SELECTED} 195 IntCmp $R0 ${SF_SELECTED} showFW 196 197 Abort 198 199showFW: 200 ; here is ok 201FunctionEnd 202 203PageEx License 204 LicenseData "pak128.txt" 205 PageCallbacks CheckForClosedSource "" "" 206PageExEnd 207 208 209 210; Some packs are GPL 211Function CheckForGPL 212 213 SectionGetFlags ${pak64HO} $R0 214 IntOp $R0 $R0 & ${SF_SELECTED} 215 IntCmp $R0 ${SF_SELECTED} showGPL 216 217 SectionGetFlags ${pak64contrast} $R0 218 IntOp $R0 $R0 & ${SF_SELECTED} 219 IntCmp $R0 ${SF_SELECTED} showGPL 220 221 Abort 222 223showGPL: 224 ; here is ok 225FunctionEnd 226 227PageEx License 228 LicenseData "GPL.txt" 229 PageCallbacks CheckForGPL "" "" 230PageExEnd 231 232 233 234; Some pak192.comic is CC 235Function CheckForCC 236 237 SectionGetFlags ${pak192comic} $R0 238 IntOp $R0 $R0 & ${SF_SELECTED} 239 IntCmp $R0 ${SF_SELECTED} showCC 240 241 Abort 242 243showCC: 244 ; here is ok 245FunctionEnd 246 247PageEx License 248 LicenseData "CC-BY-SA.txt" 249 PageCallbacks CheckForCC "" "" 250PageExEnd 251 252 253 254PageEx instfiles 255PageExEnd 256 257 258; ******************************** From here on Functions *************************** 259 260Function .oninit 261 InitPluginsDir 262 StrCpy $multiuserinstall "1" 263 ; avoids two instance at the same time ... 264 System::Call 'kernel32::CreateMutexA(i 0, i 0, t "SimutransMutex") i .r1 ?e' 265 Pop $R0 266 StrCmp $R0 0 +3 267 MessageBox MB_OK|MB_ICONEXCLAMATION "The installer is already running." 268 Abort 269 270 ; now find out, whether there is an old installation 271 IfFileExists "$SMPROGRAMS\Simutrans\Simutrans.lnk" 0 no_previous_menu 272 ShellLink::GetShortCutTarget "$SMPROGRAMS\Simutrans\Simutrans.lnk" 273 Pop $0 274 StrCpy $INSTDIR $0 -14 275 ; now check for portable or not 276 goto init_path_ok 277 278no_previous_menu: 279 ;# call userInfo plugin to get user info. The plugin puts the result in the stack 280 userInfo::getAccountType 281 pop $0 282 ; compare the result with the string "Admin" to see if the user is admin. 283 ; If match, jump 3 lines down. 284 strCmp $0 "Admin" +3 285 ; we are not admin: default install in a different dir 286 StrCpy $INSTDIR "C:\simutrans" 287 ; ok, we are admin 288 289init_path_ok: 290FunctionEnd 291 292 293; ConnectInternet (uses Dialer plug-in) 294; Written by Joost Verburg 295; 296; This function attempts to make a connection to the internet if there is no 297; connection available. If you are not sure that a system using the installer 298; has an active internet connection, call this function before downloading 299; files with NSISdl. 300; 301; The function requires Internet Explorer 3, but asks to connect manually if 302; IE3 is not installed. 303Function ConnectInternet 304 Push $R0 305 ClearErrors 306 Dialer::AttemptConnect 307 IfErrors noie3 308 Pop $R0 309 StrCmp $R0 "online" connected 310 MessageBox MB_OK|MB_ICONSTOP "Cannot connect to the internet." 311 Quit ;This will quit the installer. You might want to add your own error handling. 312 noie3: 313 ; IE3 not installed 314 MessageBox MB_OK|MB_ICONINFORMATION "Please connect to the internet now." 315 connected: 316 Pop $R0 317FunctionEnd 318 319 320 321Function IsPakInstalledAndCurrent 322 IntOp $R0 0 + 0 323 IfFileExists "$INSTDIR\$downloadname\ground.Outside.pak" +1 PakNotThere 324 IntOp $R0 1 | 1 325; now we have outside.pak and it should have a valid number 326 FileOpen $0 "$INSTDIR\$downloadname\ground.Outside.pak" r 327 FileSeek $0 101 328 FileRead $0 $R1 329 FileClose $0 330 StrCmp $VersionString $R1 0 PakThereButOld 331; now we are current 332 IntOp $R0 2 | 2 333 goto PakNotThere 334PakThereButOld: 335 DetailPrint "Old pak has version $R1 but current is $VersionString" 336PakNotThere: 337FunctionEnd 338 339 340; $downloadlink is then name of the link, $downloadname the name of the pak for error messages 341Function DownloadInstallZip 342 Call IsPakInstalledAndCurrent 343 IntCmp $R0 2 DownloadInstallZipSkipped 344 IntCmp $R0 0 DownloadInstallZipDo 345 SetOutPath $INSTDIR 346 RMdir /r "$downloadname.old" 347 Rename "$downloadname" "$downloadname.old" 348 DetailPrint "Old $downloadname renamed to $downloadname.old" 349 350DownloadInstallZipDo: 351 ; ok old directory rename 352 Call ConnectInternet 353 RMdir /r "$TEMP\simutrans" 354; since https fails with builtin NSISdl ... 355; NSISdl::download $downloadlink "$TEMP\$archievename" 356; Pop $R0 ;Get the return value 357; StrCmp $R0 "success" +3 358; MessageBox MB_OK "Download of $archievename failed: $R0" 359; Quit 360 inetc::get /WEAKSECURITY $downloadlink "$Temp\$archievename" /END 361 Pop $0 362 StrCmp $0 "OK" +3 363 MessageBox MB_OK "Download of $archievename failed: $R0" 364 Quit 365 366 ; remove all old files before! 367 RMdir /r "$INSTDIR\$downloadname" 368 ; we need the magic with temporary copy only if the folder does not end with simutrans ... 369 StrCmp $installinsimutransfolder "0" +4 370 CreateDirectory "$INSTDIR" 371 nsisunz::Unzip "$TEMP\$archievename" "$INSTDIR\.." 372 goto +2 373 nsisunz::Unzip "$TEMP\$archievename" "$TEMP" 374 Pop $R0 ;Get the return value 375 StrCmp $R0 "success" +4 376 MessageBox MB_OK|MB_ICONINFORMATION "$R0" 377 RMdir /r "$TEMP\simutrans" 378 Quit 379 380 Delete "$Temp\$archievename" 381 StrCmp $installinsimutransfolder "1" +3 382 CreateDirectory "$INSTDIR" 383 CopyFiles /silent "$TEMP\Simutrans\*.*" "$INSTDIR" 384 RMdir /r "$TEMP\simutrans" 385DownloadInstallZipSkipped: 386FunctionEnd 387 388 389 390; $downloadlink is then name of the link, $downloadname the name of the pak for error messages 391Function DownloadInstallNoRemoveZip 392 Call ConnectInternet 393 RMdir /r "$TEMP\simutrans" 394; since https fails with builtin NSISdl ... 395; NSISdl::download $downloadlink "$TEMP\$archievename" 396; Pop $R0 ;Get the return value 397; StrCmp $R0 "success" +3 398; MessageBox MB_OK "Download of $archievename failed: $R0" 399; Quit 400 inetc::get /WEAKSECURITY $downloadlink "$Temp\$archievename" /END 401 Pop $0 402 StrCmp $0 "OK" +3 403 MessageBox MB_OK "Download of $archievename failed: $R0" 404 Quit 405 406 ; we need the magic with temporary copy only if the folder does not end with simutrans ... 407 StrCmp $installinsimutransfolder "0" +4 408 CreateDirectory "$INSTDIR" 409 nsisunz::Unzip "$TEMP\$archievename" "$INSTDIR\.." 410 goto +2 411 nsisunz::Unzip "$TEMP\$archievename" "$TEMP" 412 Pop $R0 ;Get the return value 413 StrCmp $R0 "success" +4 414 MessageBox MB_OK|MB_ICONINFORMATION "$R0" 415 RMdir /r "$TEMP\simutrans" 416 Quit 417 418 Delete "$Temp\$archievename" 419 StrCmp $installinsimutransfolder "1" +3 420 CreateDirectory "$INSTDIR" 421 CopyFiles /silent "$TEMP\Simutrans\*.*" "$INSTDIR" 422 RMdir /r "$TEMP\simutrans" 423FunctionEnd 424 425 426 427 428; $downloadlink is then name of the link, $downloadname the name of the pak for error messages 429Function DownloadInstallAddonZip 430# DetailPrint "Download of $downloadname from\n$downloadlink to $archievename" 431 Call ConnectInternet 432 RMdir /r "$TEMP\simutrans" 433; since https fails with builtin NSISdl ... 434; NSISdl::download $downloadlink "$TEMP\$archievename" 435; Pop $R0 ;Get the return value 436; StrCmp $R0 "success" +3 437; MessageBox MB_OK "Download of $archievename failed: $R0" 438; Quit 439 inetc::get /WEAKSECURITY $downloadlink "$Temp\$archievename" /END 440 Pop $0 441 StrCmp $0 "OK" +3 442 MessageBox MB_OK "Download of $archievename failed: $R0" 443 Quit 444 445 nsisunz::Unzip "$TEMP\$archievename" "$DOCUMENTS" 446 Pop $R0 ;Get the return value 447 StrCmp $R0 "success" +4 448 DetailPrint "$0" ;print error message to log 449 Delete "$TEMP\$archievename" 450 Quit 451 452 Delete "$Temp\$archievename" 453FunctionEnd 454 455 456 457; $downloadlink is then name of the link, $downloadname the name of the pak for error messages 458Function DownloadInstallAddonZipPortable 459# DetailPrint "Download of $downloadname from\n$downloadlink to $archievename" 460 Call ConnectInternet 461 RMdir /r "$TEMP\simutrans" 462; since https fails with builtin NSISdl ... 463; NSISdl::download $downloadlink "$TEMP\$archievename" 464; Pop $R0 ;Get the return value 465; StrCmp $R0 "success" +3 466; MessageBox MB_OK "Download of $archievename failed: $R0" 467; Quit 468 inetc::get /WEAKSECURITY $downloadlink "$Temp\$archievename" /END 469 Pop $0 470 StrCmp $0 "OK" +3 471 MessageBox MB_OK "Download of $archievename failed: $R0" 472 Quit 473 474 nsisunz::Unzip "$TEMP\$archievename" "$INSTDIR\.." 475 Pop $R0 ;Get the return value 476 StrCmp $R0 "success" +4 477 DetailPrint "$0" ;print error message to log 478 Delete "$TEMP\$archievename" 479 Quit 480 481 Delete "$Temp\$archievename" 482FunctionEnd 483 484 485 486; $downloadlink is then name of the link, $downloadname the name of the pak for error messages 487Function DownloadInstallZipWithoutSimutrans 488 Call IsPakInstalledAndCurrent 489 IntCmp $R0 2 DownloadInstallZipWithoutSimutransSkip 490 IntCmp $R0 0 DownloadInstallZipWithoutSimutransDo 491 SetOutPath $INSTDIR 492 RMdir /r "$downloadname.old" 493 Rename "$downloadname" "$downloadname.old" 494 DetailPrint "Old $downloadname renamed to $downloadname.old" 495DownloadInstallZipWithoutSimutransDo: 496 ; ok, now install 497 DetailPrint "Download of $downloadname from\n$downloadlink to $archievename" 498 Call ConnectInternet 499 RMdir /r "$TEMP\simutrans" 500 CreateDirectory "$TEMP\simutrans" 501# since we also want to download from addons ... 502 inetc::get /WEAKSECURITY $downloadlink "$Temp\$archievename" /END 503 POP $0 504 StrCmp $0 "OK" +3 505 MessageBox MB_OK "Download of $archievename failed: $R0" 506 Quit 507 508 ; remove all old files before! 509 RMdir /r "$INSTDIR\$downloadname" 510 CreateDirectory "$INSTDIR" 511 nsisunz::Unzip "$TEMP\$archievename" "$INSTDIR" 512 Pop $R0 513 StrCmp $R0 "success" +4 514 Delete "$Temp\$archievename" 515 DetailPrint "$0" ;print error message to log 516 Quit 517 518 Delete "$Temp\$archievename" 519DownloadInstallZipWithoutSimutransSkip: 520FunctionEnd 521 522 523 524Function DownloadInstallCabWithoutSimutrans 525 DetailPrint "Download of $downloadname from $downloadlink to $archievename" 526 Call IsPakInstalledAndCurrent 527 IntCmp $R0 2 DownloadInstallCabWithoutSimutransSkip 528 IntCmp $R0 0 DownloadInstallCabWithoutSimutransDo 529 SetOutPath $INSTDIR 530 RMdir /r "$downloadname.old" 531 Rename "$downloadname" "$downloadname.old" 532 DetailPrint "Old $downloadname renamed to $downloadname.old" 533DownloadInstallCabWithoutSimutransDo: 534 ; ok, needs update 535 Call ConnectInternet 536 NSISdl::download $downloadlink "$Temp\$archievename" 537 Pop $R0 ;Get the return value 538 StrCmp $R0 "success" +4 539 DetailPrint "$R0" ;print error message to log 540 MessageBox MB_OK "Download of $archievename failed: $R0" 541 Quit 542 543 CabDLL::CabView "$TEMP\$archievename" 544 DetailPrint "Install of $archievename to $INSTDIR" 545 CreateDirectory "$INSTDIR\$downloadname" 546 CreateDirectory "$INSTDIR\$downloadname\config" 547 CreateDirectory "$INSTDIR\$downloadname\sound" 548 CreateDirectory "$INSTDIR\$downloadname\text" 549 CabDLL::CabExtractAll "$TEMP\$archievename" "$INSTDIR" 550 StrCmp $R0 "0" +5 551 DetailPrint "$0" ;print error message to log 552 RMdir /r "$TEMP\simutrans" 553 Delete "$Temp\$archievename" 554 Quit 555 556 Delete "$Temp\$archievename" 557DownloadInstallCabWithoutSimutransSkip: 558FunctionEnd 559 560 561 562Function DownloadInstallTgzWithoutSimutrans 563# DetailPrint "Download of $downloadname from\n$downloadlink to $archievename" 564 Call ConnectInternet 565 RMdir /r "$TEMP\simutrans" 566 NSISdl::download $downloadlink "$Temp\$archievename" 567 Pop $R0 ;Get the return value 568 StrCmp $R0 "success" +3 569 MessageBox MB_OK "Download of $archievename failed: $R0" 570 Quit 571 572 CreateDirectory "$INSTDIR" 573 ; remove all old files before! 574 RMdir /r "$INSTDIR\$downloadname" 575 untgz::extract -d "$INSTDIR" "$TEMP\$archievename" 576 StrCmp $R0 "success" +4 577 Delete "$Temp\$archievename" 578 MessageBox MB_OK "Extraction of $archievename failed: $R0" 579 Quit 580 581 Delete "$Temp\$archievename" 582FunctionEnd 583