# ASCOM 驱动 Inno Setup 打包完整指南

## 一、工具下载

### 1.1 Inno Setup 下载

**官方下载地址：**
- 主程序：https://jrsoftware.org/isdl.php
- 推荐版本：Inno Setup 6.x（最新稳定版）

**下载选项：**
- **innosetup-6.x.x.exe**（标准安装包）
- **innosetup-6.x.x-unicode.exe**（Unicode 版本，推荐）

**QuickStart Pack（必须安装）：**
- 下载地址：https://jrsoftware.org/isdl.php#qsp
- 包含加密、预处理器等高级功能

### 1.2 ASCOM 驱动安装脚本生成器

**位置：** 随 ASCOM Platform 7 安装
- Windows 开始菜单：`开始 → ASCOM → Driver Installer Script Generator`
- 安装路径：`C:\Program Files (x86)\ASCOM\Platform 7\Developer\Installer Generator\`

## 二、安装步骤

### 2.1 安装 Inno Setup

```bash
# 双击下载的安装包
1. 接受许可协议
2. 选择安装路径（默认：C:\Program Files (x86)\Inno Setup 6）
3. 勾选 "Install Inno Setup Preprocessor"
4. 完成安装
```

### 2.2 安装 QuickStart Pack

```bash
1. 双击 is-qsp-6.x.x.exe
2. 自动识别 Inno Setup 安装路径
3. 确认安装
```

## 三、使用 ASCOM 脚本生成器创建安装包

### 3.1 启动脚本生成器

```
开始菜单 → ASCOM → Driver Installer Script Generator
```

### 3.2 填写基本信息

**Application Information（应用信息）：**
- **App Name：** MyDome（你的驱动名称）
- **App Version：** 1.0.0
- **App Publisher：** Your Name or Company
- **App Publisher URL：** http://yourwebsite.com
- **Device Type：** Dome（选择设备类型）

### 3.3 配置驱动文件

**Driver Files（驱动文件）：**
```
主 DLL 文件：
- Source: C:\MyProject\MyDome\bin\Release\MyDome.dll
- Dest: {app}\MyDome.dll
- Flags: regserver sharedfile

依赖文件（如果有）：
- ASCOM.Utilities.dll
- ASCOM.Exceptions.dll
```

### 3.4 平台版本要求

**ASCOM Platform Requirements（关键设置）：**

```ini
; 最低 ASCOM Platform 版本
#define REQUIRED_PLATFORM_VERSION "6.0"  ; 支持 Platform 6.0 及以上
; 或
#define REQUIRED_PLATFORM_VERSION "7.0"  ; 仅支持 Platform 7.0 及以上
```

**兼容性设置：**
- **Platform 6.x 兼容：** 设置 `REQUIRED_PLATFORM_VERSION = 6.0`
- **Platform 7.x 专用：** 设置 `REQUIRED_PLATFORM_VERSION = 7.0`

### 3.5 生成脚本文件

点击 **"Generate Script"** 按钮，保存为：
```
MyDome_Setup.iss
```

## 四、编辑脚本文件（高级配置）

### 4.1 打开生成的脚本

用文本编辑器或 Inno Setup 编译器打开 `MyDome_Setup.iss`

### 4.2 关键脚本段落

#### Setup 段落
```ini
[Setup]
AppId={{生成的唯一GUID}}
AppName=MyDome ASCOM Driver
AppVersion=1.0.0
AppPublisher=Your Name
AppPublisherURL=http://yourwebsite.com
DefaultDirName={pf}\ASCOM\Dome
DefaultGroupName=ASCOM Dome Drivers
OutputDir=.\Output
OutputBaseFilename=MyDome_Setup
Compression=lzma2
SolidCompression=yes
MinVersion=6.1

; ASCOM 平台版本检查
#define REQUIRED_PLATFORM_VERSION "6.0"
```

#### Files 段落
```ini
[Files]
; 主驱动 DLL
Source: "C:\MyProject\MyDome\bin\Release\MyDome.dll"; \
  DestDir: "{app}"; \
  Flags: regserver sharedfile ignoreversion

; 依赖库
Source: "C:\MyProject\MyDome\bin\Release\ASCOM.Utilities.dll"; \
  DestDir: "{app}"; \
  Flags: sharedfile ignoreversion

; 帮助文档
Source: "C:\MyProject\MyDome\Docs\*"; \
  DestDir: "{app}\Docs"; \
  Flags: recursesubdirs
```

#### Registry 段落
```ini
[Registry]
; 注册驱动信息
Root: HKCR; Subkey: "ASCOM.MyDome.Dome"; \
  ValueType: string; ValueName: ""; \
  ValueData: "MyDome ASCOM Driver"

Root: HKLM; Subkey: "Software\ASCOM\Dome Drivers\MyDome.Dome"; \
  ValueType: string; ValueName: ""; \
  ValueData: "MyDome ASCOM Driver"
```

#### Code 段落（平台检查）
```pascal
[Code]
function InitializeSetup(): Boolean;
var
  PlatformVersion: String;
  RequiredVersion: String;
begin
  Result := True;
  RequiredVersion := '{#REQUIRED_PLATFORM_VERSION}';
  
  // 检查 ASCOM Platform 是否安装
  if not RegQueryStringValue(HKLM, 
    'Software\ASCOM\Platform', 
    'PlatformVersion', 
    PlatformVersion) then
  begin
    MsgBox('ASCOM Platform ' + RequiredVersion + ' or later is required.' + #13#10 + 
           'Please install it from https://ascom-standards.org', 
           mbError, MB_OK);
    Result := False;
    Exit;
  end;
  
  // 检查版本是否满足要求
  if CompareStr(PlatformVersion, RequiredVersion) < 0 then
  begin
    MsgBox('ASCOM Platform ' + RequiredVersion + ' or later is required.' + #13#10 + 
           'Current version: ' + PlatformVersion + #13#10 + 
           'Please upgrade from https://ascom-standards.org', 
           mbError, MB_OK);
    Result := False;
  end;
end;
```

## 五、编译安装包

### 5.1 使用 Inno Setup 编译器

```bash
方法一：图形界面
1. 打开 Inno Setup Compiler
2. File → Open → 选择 MyDome_Setup.iss
3. Build → Compile（或按 F9）
4. 等待编译完成

方法二：命令行
cd C:\MyProject\MyDome\Installer
"C:\Program Files (x86)\Inno Setup 6\ISCC.exe" MyDome_Setup.iss
```

### 5.2 输出文件

编译成功后，在 `Output` 目录下生成：
```
MyDome_Setup.exe  （安装程序）
```

## 六、支持老版本 ASCOM Platform

### 6.1 版本兼容策略

**完全兼容 Platform 6.x：**
```ini
#define REQUIRED_PLATFORM_VERSION "6.0"
```

**条件编译（同时支持 6.x 和 7.x）：**
```pascal
[Code]
function CheckASCOMVersion(): Boolean;
var
  V6Installed, V7Installed: Boolean;
  Version: String;
begin
  // 检查 Platform 6.x
  V6Installed := RegQueryStringValue(HKLM, 
    'Software\ASCOM\Platform', 
    'PlatformVersion', 
    Version);
  
  // 检查 Platform 7.x
  V7Installed := RegQueryStringValue(HKLM, 
    'Software\ASCOM\Platform 7', 
    'PlatformVersion', 
    Version);
  
  Result := V6Installed or V7Installed;
  
  if not Result then
  begin
    MsgBox('ASCOM Platform 6.0 or later is required.', 
           mbError, MB_OK);
  end;
end;

function InitializeSetup(): Boolean;
begin
  Result := CheckASCOMVersion();
end;
```

### 6.2 依赖库处理

**Platform 6.x 依赖：**
- `ASCOM.Utilities.dll`（位于 `{commoncf}\ASCOM\Platform 6\`）

**Platform 7.x 依赖：**
- `ASCOM.Utilities.dll`（位于 `{commoncf}\ASCOM\Platform 7\`）

**解决方案：**
```ini
[Files]
; 不打包 ASCOM 依赖库，让驱动动态加载系统已安装的版本
; 这样可以自动兼容 6.x 和 7.x
Source: "MyDome.dll"; DestDir: "{app}"; Flags: regserver
```

## 七、测试安装包

### 7.1 测试环境准备

1. 干净的虚拟机或测试电脑
2. 安装 ASCOM Platform 6.x 或 7.x
3. 安装你的驱动安装包

### 7.2 验证步骤

```bash
1. 双击 MyDome_Setup.exe
2. 按照向导完成安装
3. 打开 ASCOM Diagnostics
4. 选择 Dome → MyDome
5. 点击 Properties（测试配置对话框）
6. 点击 Connect（测试连接）
```

### 7.3 卸载测试

```bash
控制面板 → 程序和功能 → 卸载 MyDome ASCOM Driver
验证：
- 注册表项已清除
- 文件已删除
- ASCOM Chooser 中不再显示
```

## 八、高级功能

### 8.1 数字签名

```ini
[Setup]
SignTool=signtool
SignedUninstaller=yes
```

配置签名工具：
```bash
Tools → Configure Sign Tools
Name: signtool
Command: "C:\Program Files (x86)\Windows Kits\10\bin\x64\signtool.exe" ^
  sign /f "C:\MyCert.pfx" /p "password" /t http://timestamp.digicert.com $f
```

### 8.2 多语言支持

```ini
[Languages]
Name: "english"; MessagesFile: "compiler:Default.isl"
Name: "chinese"; MessagesFile: "compiler:Languages\ChineseSimplified.isl"
```

### 8.3 自定义卸载

```ini
[UninstallRun]
Filename: "regsvr32.exe"; Parameters: "/u /s ""{app}\MyDome.dll"""
```

## 九、常见问题

### 9.1 注册失败

**原因：** 权限不足
**解决：** 右键 → 以管理员身份运行安装程序

### 9.2 COM 注册错误

```ini
[Files]
; 确保使用 regserver 标志
Source: "MyDome.dll"; DestDir: "{app}"; Flags: regserver restartreplace
```

### 9.3 卸载不干净

添加卸载代码：
```pascal
[Code]
procedure CurUninstallStepChanged(CurUninstallStep: TUninstallStep);
begin
  if CurUninstallStep = usPostUninstall then
  begin
    // 清理注册表
    RegDeleteKeyIncludingSubkeys(HKLM, 'Software\ASCOM\Dome Drivers\MyDome.Dome');
  end;
end;
```

## 十、最佳实践

1. **版本号规范：** 遵循语义化版本 (1.0.0 → 1.0.1 → 1.1.0)
2. **测试覆盖：** 在 Platform 6.x 和 7.x 上都测试
3. **文档完善：** 包含 README、变更日志
4. **更新机制：** 检查并安装更新的功能
5. **日志记录：** 安装过程记录详细日志

## 十一、脚本模板下载

完整的脚本模板可以从以下位置获取：
- ASCOM 论坛：https://ascomtalk.groups.io/
- GitHub 示例：https://github.com/ASCOMInitiative/ASCOMPlatform

## 十二、相关资源

- **Inno Setup 官方文档：** https://jrsoftware.org/ishelp/
- **ASCOM 开发者指南：** https://ascom-standards.org/COMDeveloper/Index.htm
- **驱动打包教程：** https://ascom-standards.org/COMDeveloper/DriverDist.htm
