从0到1构建Lively安装包:Inno Setup实战指南

从0到1构建Lively安装包:Inno Setup实战指南

【免费下载链接】lively Free and open-source software that allows users to set animated desktop wallpapers and screensavers powered by WinUI 3. 【免费下载链接】lively 项目地址: https://gitcode.com/gh_mirrors/li/lively

你还在为开源项目打包流程繁琐而烦恼吗?本文将以Lively Wallpaper项目为例,详解如何使用Inno Setup制作专业级安装程序,从基础配置到高级功能全覆盖,让你的应用分发效率提升10倍。读完本文你将掌握:安装包结构设计、多语言支持实现、依赖自动检测、版本升级逻辑四大核心技能。

安装包工程结构解析

Lively Wallpaper的安装程序工程位于src/installer/目录,采用双架构设计支持32位和64位系统,主要包含以下关键文件:

安装程序主题资源

工程采用模块化设计,将可执行文件、依赖组件和资源文件分离管理,确保安装包结构清晰且易于维护。这种结构特别适合需要支持多架构和多语言的桌面应用。

基础配置与编译流程

核心定义区块

每个Inno Setup脚本都以[Setup]区块作为配置核心,Lively安装包的基础定义如下:

#define MyAppName "Lively Wallpaper"
#define MyAppVersion "2.2.0.0"
#define MyAppPublisher "rocksdanister"
#define MyAppExeName "Lively.exe"

[Setup]
AppId={{E3E43E1B-DEC8-44BF-84A6-243DBA3F2CB1}}
AppName={#MyAppName}
AppVersion={#MyAppVersion}
DefaultDirName={autopf}\{#MyAppName}
OutputBaseFilename=lively_installer
SetupIconFile=Icons\appicon_96.ico
Compression=lzma
SolidCompression=yes

这段代码定义了应用名称、版本号、发布者信息和输出文件设置。其中AppId是全局唯一标识符,用于Windows卸载程序识别应用,建议使用Inno Setup IDE的"Tools | Generate GUID"功能生成新ID。

编译命令与参数

编译安装包需使用Inno Setup Compiler (ISCC),在项目根目录执行以下命令:

# 编译32位安装包
ISCC.exe src/installer/Script.iss

# 编译64位安装包
ISCC.exe src/installer/Script_x64.iss

编译成功后,安装包将生成在脚本中OutputDir指定的目录(默认与脚本同目录),文件名为lively_installer.exe。64位版本通过ArchitecturesInstallIn64BitMode=x64参数实现架构区分。

多语言支持实现方案

Lively安装包支持23种语言,其多语言架构通过三个层次实现:

1. 语言定义区块

在[Languages]区块中声明支持的语言及对应的翻译文件:

[Languages]
Name: "english"; MessagesFile: "compiler:Default.isl"; LicenseFile: "License\License.txt"
Name: "chinese"; MessagesFile: "Languages\ChineseSimplified.isl"
Name: "japanese"; MessagesFile: "compiler:Languages\Japanese.isl"
; 其他18种语言...

2. 自定义消息翻译

通过[CustomMessages]区块定义应用特定文本的多语言翻译:

[CustomMessages]
english.DeleteEverythigMsgBox=Do you want to delete data folder?
chinese.DeleteEverythigMsgBox=您要删除数据文件夹吗?
japanese.DeleteEverythigMsgBox=データフォルダを削除しますか?
korean.DeleteEverythigMsgBox=데이터 폴더를 삭제하시겠습니까?

这些翻译在代码中通过{cm:MessageName}语法引用,如卸载时的数据文件夹删除确认:

SuppressibleMsgBox(ExpandConstant('{cm:DeleteEverythigMsgBox}')+ ' ' + ExpandConstant('{localappdata}\Lively Wallpaper') + ' ?',
  mbConfirmation, MB_YESNO, IDNO)

3. 本地化文件组织

所有语言翻译文件集中存放在Languages/目录,采用语言名称.isl命名规范。对于非Unicode语言,需确保文件保存为UTF-8带BOM格式,避免安装界面出现乱码。

多语言支持文件结构

依赖管理与自动安装

专业安装程序的核心功能之一是自动处理运行时依赖,Lively安装包实现了三类关键依赖的自动检测与安装:

Visual C++ 运行时

通过MSI产品状态查询判断VC运行时是否已安装:

function VCRedistNeedsInstall: Boolean;
begin
  Result := not IsMsiProductInstalled('{36F68A90-239C-34DF-B58C-64B30153CE35}', PackVersionComponents(14, 30, 30704, 0));
end;

需要安装时,从临时目录执行VC redist安装程序:

[Files]
Source: "VC\VC_redist.x64.exe"; DestDir: {tmp}; Flags: deleteafterinstall

[Run]
Filename: "{tmp}\VC_redist.x64.exe"; Parameters: /install /quiet /norestart; Check: VCRedistNeedsInstall

.NET 桌面运行时

使用微软官方的NetCoreCheck工具检测.NET运行时:

function NetCoreNeedsInstall(version: String): Boolean;
var
	netcoreRuntime: String;
	resultCode: Integer;
begin
  netcoreRuntime := 'Microsoft.WindowsDesktop.App'
	Result := not(Exec(ExpandConstant('{tmp}\netcorecheck.exe', netcoreRuntime + ' ' + version, '', SW_HIDE, ewWaitUntilTerminated, resultCode) and (resultCode = 0));
end;

对应的安装配置:

[Files]
Source: "dotnetcore\windowsdesktop-runtime-9.0.8-win-x64.exe"; DestDir: {tmp}; Flags: deleteafterinstall
Source: "dotnetcore\netcorecheck.exe"; DestDir: {tmp}; Flags: deleteafterinstall

[Run]
Filename: "{tmp}\windowsdesktop-runtime-9.0.8-win-x64.exe"; Parameters: /install /quiet /norestart; Check: NetCoreNeedsInstall('9.0.8')

依赖安装流程

安装程序会按以下顺序处理依赖:

  1. 解压依赖安装包到临时目录
  2. 按VC Redist → .NET Runtime顺序检测并安装
  3. 安装完成后自动清理临时文件

这种设计确保用户无需手动下载和安装任何依赖组件,实现真正的"一键安装"体验。

高级功能:版本升级与卸载逻辑

专业安装程序必须处理版本升级和干净卸载,Lively通过以下机制实现:

自动卸载旧版本

在安装新版本前,通过查询注册表获取旧版本卸载字符串并执行:

function UnInstallOldVersion(): Integer;
var
  sUnInstallString: String;
  iResultCode: Integer;
begin
  sUnInstallString := GetUninstallString();
  if sUnInstallString <> '' then begin
    sUnInstallString := RemoveQuotes(sUnInstallString);
    if Exec(sUnInstallString, '/VERYSILENT /NORESTART','', SW_HIDE, ewWaitUntilTerminated, iResultCode) then
      Result := 3  // 成功卸载
    else
      Result := 2; // 卸载失败
  end else
    Result := 1; // 未找到旧版本
end;

这段代码在安装过程的ssInstall阶段执行,确保旧版本被彻底移除后再安装新版本。

智能数据清理

卸载程序提供数据文件夹清理选项:

procedure CurUninstallStepChanged(CurUninstallStep: TUninstallStep);
begin
  if CurUninstallStep = usPostUninstall then
  begin
    if SuppressibleMsgBox(ExpandConstant('{cm:DeleteEverythigMsgBox}')+ ' ' + ExpandConstant('{localappdata}\Lively Wallpaper') + ' ?',
      mbConfirmation, MB_YESNO, IDNO) = IDYES then
    begin
      DelTree(ExpandConstant('{localappdata}\Lively Wallpaper'), True, True, True);
    end;
  end;
end;

此功能保护用户数据安全,仅在用户明确确认后才删除应用数据。

应用互斥检测

通过AppMutex机制防止安装程序与运行中的应用冲突:

[Setup]
AppMutex=LIVELY:DESKTOPWALLPAPERSYSTEM

卸载时检测并提示关闭正在运行的应用:

if CheckForMutexes('LIVELY:DESKTOPWALLPAPERSYSTEM') and
   (SuppressibleMsgBox('Application is running, do you want to close it?',
           mbConfirmation, MB_OKCANCEL, IDOK) = IDOK) then
begin
  ShellExec('open','taskkill.exe','/f /im {#MyAppExeName}','',SW_HIDE,ewWaitUntilTerminated,ErrorCode);
end;

实战技巧与最佳实践

编译优化

  1. 压缩算法选择:使用lzma压缩算法可获得比默认算法高30%的压缩率:

    Compression=lzma
    SolidCompression=yes
    
  2. 条件编译:通过命令行参数实现差异化编译:

    ISCC.exe Script.iss /dMyAppVersion=2.2.0.0 /dOutputName=lively_setup
    

调试技巧

  1. 日志输出:添加/LOG参数生成详细安装日志:

    lively_installer.exe /LOG="C:\lively_install.log"
    
  2. 调试模式:在脚本中添加#define Debug并使用条件编译输出调试信息:

    #ifdef Debug
      MsgBox('Current directory: ' + ExpandConstant('{app}'), mbInformation, MB_OK);
    #endif
    

安全最佳实践

  1. 权限控制:使用最低权限安装:

    PrivilegesRequired=lowest
    
  2. 数字签名:对最终安装包进行代码签名,防止被篡改:

    SignTool=sign /f "cert.pfx" /p password $f
    

总结与扩展

本文详细解析了Lively Wallpaper安装程序的实现原理,涵盖从基础配置到高级功能的各个方面。通过Inno Setup这一强大工具,我们构建了一个支持多语言、多架构、自动处理依赖、智能升级的专业级安装程序。

要进一步提升安装包质量,可考虑添加以下功能:

  • 安装进度条自定义动画
  • 应用安装完成后的教程引导
  • 系统兼容性检测
  • 静默安装配置文件支持

完整的安装程序源码可在src/installer/目录查看,建议结合Inno Setup官方文档深入学习各配置选项的详细用法。

通过掌握这些技术,你可以为任何Windows桌面应用构建专业、可靠的安装体验,大幅提升用户满意度和应用分发效率。

【免费下载链接】lively Free and open-source software that allows users to set animated desktop wallpapers and screensavers powered by WinUI 3. 【免费下载链接】lively 项目地址: https://gitcode.com/gh_mirrors/li/lively

创作声明:本文部分内容由AI辅助生成(AIGC),仅供参考

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值