Electron-builder自定义安装路径配置与更新隔离问题咨询
解决方案:分离Launcher与Game目录并配置electron-updater独立更新
一、修正NSIS自定义脚本(解决路径嵌套问题)
你之前的脚本导致了路径嵌套(比如用户选择D:\MyGame,最终程序会装到D:\MyGame\Launcher\Launcher),需要调整变量逻辑,确保根目录下同时生成Launcher和Game文件夹:
!macro preInit ; 保存用户选择的根安装目录到自定义变量 StrCpy $ROOT_INSTDIR "$INSTDIR" ; 将程序实际安装目录设为根目录下的Launcher文件夹 StrCpy $INSTDIR "$ROOT_INSTDIR\Launcher" !macroend !macro customInstall ; 在用户选择的根目录下创建Game文件夹 CreateDirectory "$ROOT_INSTDIR\Game" !macroend
二、调整electron-builder的build配置
1. 打包游戏文件到Game目录
如果你的游戏文件需要随安装包一起分发,修改files配置,将游戏文件指向根目录的Game(通过../Game相对路径实现,因为程序实际安装目录是Launcher):
"build": { "appId": "com.yourcompany.gamelauncher", "productName": "My Game", "directories": { "output": "release", "buildResources": "app-resource" }, "files": [ // 以下是Launcher相关文件,默认安装到Launcher目录 "dist/**/*", { "from": "assets", "to": "assets" }, { "from": "../dist/app", "to": "app" }, { "from": "./build", "to": "build" }, "./package.json", "./.env", // 游戏文件:打包后安装到根目录的Game文件夹 { "from": "path/to/your/game/files", // 替换为你的游戏文件实际路径 "to": "../Game" } ], "win": { "icon": "icon.ico", "target": [ "nsis" ], "signAndEditExecutable": true, "requestedExecutionLevel": "requireAdministrator" }, "nsis": { "oneClick": false, "allowToChangeInstallationDirectory": true, "deleteAppDataOnUninstall": false, "include": "build/installer.nsh" } }
2. 确保electron-updater仅更新Launcher
electron-updater默认以当前应用所在目录(即Launcher)为更新目标,只要保持默认配置即可——更新包只会覆盖Launcher内的文件,不会触及同级的Game目录。
三、对注册表项的影响说明
- 卸载路径与版本信息:注册表中记录的卸载路径会指向
Launcher目录下的卸载程序,版本信息、应用名称等仍会沿用productName和appId配置,不会因为目录分离而异常。 - 卸载行为:默认卸载只会删除
Launcher目录,不会自动删除Game目录。如果需要卸载时清理Game,可以在NSIS脚本中添加uninstall宏:
!macro customUnInstall ; 删除根目录下的Game文件夹(可选,根据需求添加) RMDir /r "$ROOT_INSTDIR\Game" !macroend
- 注册表关联:所有与应用相关的注册表项(如协议关联、右键菜单等)仍会正常绑定到Launcher中的主程序,不影响功能使用。
内容的提问来源于stack exchange,提问作者MinhSu - Justin
相关产品推荐
相关产品推荐

