简体   繁体   English

基本安装的简单 NSIS 配方

[英]Simple NSIS recipe for basic installation

I'm preparing to develop my very first Windows installer for a game I'm developing.我正准备为我正在开发的游戏开发我的第一个 Windows 安装程序。 I've heard NSIS recommended, but my needs are basic and I can't figure out what exactly is required.我听说过 NSIS 推荐,但我的需求很基本,我无法弄清楚到底需要什么。

I'd like a short NSIS script that can install a directory of files in the "Program Files" directory (requesting permission if necessary), create a simple shortcut in the Start menu, and register an uninstaller in the Add/Remove Programs control panel.我想要一个简短的 NSIS 脚本,它可以在“程序文件”目录中安装一个文件目录(必要时请求权限),在“开始”菜单中创建一个简单的快捷方式,并在“添加/删除程序”控制面板中注册一个卸载程序.

The uninstaller would just delete the directory and remove the shortcut, prompting for reboot if the files are in use.卸载程序只会删除目录并删除快捷方式,如果文件正在使用,则会提示重新启动。

Finally, I'd like to support upgrades by uninstalling all of the old files and installing new ones, prompting the user to reboot if the old files are in use.最后,我想通过卸载所有旧文件并安装新文件来支持升级,如果旧文件正在使用,提示用户重新启动。

I've seen some sample NSH scripts online that do bits and pieces of this;我在网上看到了一些示例 NSH 脚本,这些脚本会做一些零碎的事情; I'm trying to sling it together myself and it's looking like it's going to be hundreds of lines long.我正在尝试自己将它吊在一起,看起来它会有数百行。 My code smells like the wrong thing.我的代码闻起来像是错误的东西。

Is the program I'm describing really supposed to be hundreds of lines of NSH?我描述的程序真的应该是数百行 NSH 吗? Surely there's an easier way?当然有更简单的方法吗?

Download Eclipse and afterwards install the EclipseNSIS plugin into Eclipse. 下载 Eclipse ,然后将EclipseNSIS 插件安装到 Eclipse 中。 That not only gives you a full featured editor for NSIS files (with code completion and all the like), but also has a wizard to create the script for you by answering just some questions (application title, icon to be used, ...).这不仅为您提供了一个功能齐全的 NSIS 文件编辑器(具有代码完成等),而且还有一个向导可以通过回答一些问题(应用程序标题、要使用的图标、... ).

This should be pretty close (I did not test this script) You should fill in the blanks and replace certain strings...这应该非常接近(我没有测试这个脚本)你应该填写空白并替换某些字符串......

!define Name "Foo"
Name "${Name}"
Outfile "${Name} setup.exe"
RequestExecutionLevel admin ;Require admin rights on NT6+ (When UAC is turned on)
InstallDir "$ProgramFiles\${Name}"

!include LogicLib.nsh
!include MUI.nsh

Function .onInit
SetShellVarContext all
UserInfo::GetAccountType
pop $0
${If} $0 != "admin" ;Require admin rights on NT4+
    MessageBox mb_iconstop "Administrator rights required!"
    SetErrorLevel 740 ;ERROR_ELEVATION_REQUIRED
    Quit
${EndIf}
FunctionEnd

!insertmacro MUI_PAGE_WELCOME
!insertmacro MUI_PAGE_DIRECTORY
!insertmacro MUI_PAGE_INSTFILES
!insertmacro MUI_PAGE_FINISH
!insertmacro MUI_UNPAGE_CONFIRM
!insertmacro MUI_UNPAGE_INSTFILES
!insertmacro MUI_LANGUAGE "English"


Section
SetOutPath "$INSTDIR"
WriteUninstaller "$INSTDIR\Uninstall.exe"
WriteRegStr HKLM "Software\Microsoft\Windows\CurrentVersion\Uninstall\Foo By Bar Inc."   "DisplayName" "${Name}"
WriteRegStr HKLM "Software\Microsoft\Windows\CurrentVersion\Uninstall\Foo By Bar Inc."   "UninstallString" "$INSTDIR\Uninstall.exe"
;TODO: Install your files with the File command
CreateShortCut "$SMPROGRAMS\${Name}.lnk" "$INSTDIR\Foo.exe"
SectionEnd

Section "Uninstall"
;TODO: Delete your files
Delete "$SMPROGRAMS\${Name}.lnk"
DeleteRegKey HKLM "Software\Microsoft\Windows\CurrentVersion\Uninstall\Foo By Bar Inc."
Delete "$INSTDIR\Uninstall.exe"
RMDir "$INSTDIR"
SectionEnd

A great wizard for NSIS is the HM NIS Edit, which can guide you through your first installer, including what you mention above I believe. NSIS 的一个很棒的向导是 HM NIS Edit,它可以指导您完成第一个安装程序,包括您上面提到的我相信。

http://hmne.sourceforge.net/ http://hmne.sourceforge.net/

If you want to provide an updater in NSIS, I would recommend a few things:如果你想在 NSIS 中提供更新程序,我会推荐一些东西:

  1. Create a way for your application to check for updates from your server, usually done when it first loads为您的应用程序创建一种检查服务器更新的方法,通常在首次加载时完成
  2. Download the new installer and run it (from your application, although anyone probably can get it from your website as well).下载新的安装程序并运行它(从您的应用程序,尽管任何人也可以从您的网站获得它)。
  3. If your old program is running, you need to shut it down.如果您的旧程序正在运行,您需要将其关闭。 NSIS has plugins that can find a process and kill it. NSIS 有可以找到进程并杀死它的插件。 This is not super 'nice' but can be necessary.这不是超级“好”,但可能是必要的。

I recommend this process plugin我推荐这个流程插件

If you have DLL's in use you can't just delete them.如果您正在使用 DLL,则不能直接删除它们。 However, in NSIS you can rename them, and then mark them for deletion on the NEXT reboot, like so:但是,在 NSIS 中,您可以重命名它们,然后将它们标记为在 NEXT 重新启动时删除,如下所示:

Delete /REBOOTOK file.txt

Then copy your new DLL's into the folder.然后将新的 DLL 复制到该文件夹中。

For NSIS, there are a ton of plugins, samples and great help, but learning the language takes a little time.对于 NSIS,有大量的插件、示例和很大的帮助,但学习这门语言需要一点时间。 Inno is a lot easier to learn, but harder to customize when you get to the nitty gritty. Inno 更容易学习,但当你深入了解细节时更难定制。 However, I would say the two are comparable overall, and either should work just fine.但是,我想说这两者总体上是可比的,并且都应该可以正常工作。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM