Migrate app from Tauri to Electron

Replace the Tauri/sidecar architecture with an Electron main process. Adds an electron/ directory (main, preload, handlers, core integrations for @xmcl/*), electron-builder.yml, TypeScript electron config and declarations, and updated IPC utilities (ipc.ts) so frontend uses ipcRenderer/ipcMain. Removes Tauri sidecar and src-tauri artifacts, deletes sidecar sources and keys, updates .gitignore, VSCode recommendations, package scripts and icons, and updates documentation (README, AGENTS, DEV) and frontend stores/apis to reflect the Electron-based architecture and config/auth persistence changes.
This commit is contained in:
2026-06-28 03:37:07 +08:00
parent 5b2e2860de
commit 9cb6a6e571
152 changed files with 8449 additions and 9562 deletions
+237 -236
View File
@@ -9,6 +9,7 @@
| 状态管理 | Zustand |
| Backend | Rust / Tauri 2 |
| Sidecar | Node.js / TypeScript / @xmcl/* |
| 配置存储 | YAML (serde_yaml) + Windows Registry |
| 包管理 | pnpm |
| 目标平台 | ARM64 Windows (`aarch64-pc-windows-msvc`) |
@@ -47,12 +48,146 @@ VITE_START_POP_BOUTTON="..." # 弹窗按钮文字
---
## 配置存储架构
### 概览
| 数据类型 | 存储位置 | 格式 | 说明 |
|---------|---------|------|------|
| 用户设置 | 程序目录 `Koring.yml` | YAML | 所有可配置项 |
| 账户凭证 | Windows Registry `HKCU\Software\KoringLauncher` | REG_SZ | token/xboxProfile |
| 实例配置 | 实例目录 `koring-instance.json` | JSON | per-instance |
| 任务历史 | localStorage `koring-task-history` | JSON | 临时,max 50 |
### Koring.yml 结构
```yaml
version: 1
theme:
darkMode: auto # auto | light | dark
parallax: true
a11y:
reduceMotion: false
reduceTransparency: false
highContrast: false
contentBlurOpacity: 50 # 0-100
background:
bgType: image # image | color
image: /background.png
blur: 0 # 0-20
opacity: 100 # 0-100
game:
gameDir: .minecraft
resourceDir: ""
savesDir: ""
instancesDir: .minecraft/instances
java:
javaPath: ""
memMode: auto # auto | custom
memGB: 4 # 1-16
gc: auto # auto | zgc | g1
jvmArgs: ""
advanced:
afterLaunch: close # close | minimize | keep
winMode: default # default | fullscreen | custom
customWidth: 854
customHeight: 480
gameArgs: ""
preLaunchCmd: ""
debugMode: false
download:
fileSource: mirror # mirror | official | official-only
versionSource: mirror
threads: 16 # 1-64
speedLimit: 0 # KB/s, 0=不限速
network:
securityId:
enabled: false
authUrl: ""
```
### 注册表结构
```
HKCU\Software\KoringLauncher
└─ auth
├─ username (REG_SZ)
├─ uuid (REG_SZ)
├─ accessToken (REG_SZ)
├─ refreshToken (REG_SZ)
└─ xboxProfile (REG_SZ, JSON string)
```
### 向上兼容策略
1. **版本号**`version` 字段,每次结构变更递增
2. **默认值填充** — 加载时缺失字段自动补全,不丢数据
3. **迁移函数**`migrate_v0_to_v1()` 等,按版本链执行
4. **未知字段保留** — YAML 解析器保留不认识的字段
5. **Debounce 写入** — 300ms debounce 避免频繁 IO
### Rust 实现
**新文件:**
- `src-tauri/src/config.rs``AppConfig` 结构体、`load_config()``save_config()``merge_defaults()``migrate()`
- `src-tauri/src/registry.rs``read_auth()``write_auth()``delete_auth()`
**Tauri 命令:**
- `get_config` → 返回 `AppConfig` JSON
- `save_config(cfg)` → 写入 `Koring.yml`
- `get_auth` → 从注册表读取 `AuthData`
- `save_auth(auth)` → 写入注册表
- `delete_auth_cmd` → 删除注册表认证数据
### 前端实现
**新文件:**
- `src/api/config.ts``getConfig()``saveConfig()` 类型定义
- `src/api/auth-registry.ts``getAuth()``saveAuth()``deleteAuth()`
- `src/stores/configStore.ts` — 统一配置 Zustand store
**configStore 接口:**
```ts
useConfigStore.getState().config // 完整配置
useConfigStore.getState().loaded // 是否已加载
useConfigStore.getState().init() // 从 Rust 加载
useConfigStore.getState().setTheme({...}) // 部分更新 + debounce 写回
useConfigStore.getState().setA11y({...})
useConfigStore.getState().setBackground({...})
useConfigStore.getState().setGame({...})
useConfigStore.getState().setJava({...})
useConfigStore.getState().setAdvanced({...})
useConfigStore.getState().setDownload({...})
useConfigStore.getState().setNetwork({...})
```
**Store 委托模式:**
themeStore / a11yStore / backgroundStore / authStore 都是 configStore 的薄包装层:
- 读取时从 `configStore.config.*` 同步
- 写入时通过各自的 `set*()` 同时更新本地状态和 configStore
- 提供 `sync*FromConfig()` 函数在 App 启动时同步
**App 启动流程:**
```
configStore.init() → syncThemeFromConfig() → syncA11yFromConfig() → syncBackgroundFromConfig() → authStore.initFromRegistry()
```
---
## 项目结构
```
koring-launcher/
├── src/ # 前端源码
│ ├── App.tsx # 路由入口 (Zustand 路由)
│ ├── App.tsx # 路由入口 + configStore 初始化
│ ├── index.css # 全局样式 + CSS 变量 + 动画
│ ├── layouts/
│ │ └── RootLayout.tsx # 三层布局: BackgroundLayer + ContentLayer + SystemLayer
@@ -69,7 +204,6 @@ koring-launcher/
│ │ │ └── Silk.tsx # WebGL 丝绸着色器 (Three.js)
│ │ ├── task/
│ │ │ ├── TaskButton.tsx # 标题栏任务指示器 (SVG 圆弧动画)
│ │ │ ├── TaskSheet.tsx # 任务队列侧面板 (z-[110])
│ │ │ └── TaskCard.tsx # 单个任务卡片 (进度条+日志)
│ │ ├── ui/ # shadcn/ui 组件
│ │ │ ├── sheet.tsx, button.tsx, switch.tsx, slider.tsx
@@ -78,25 +212,52 @@ koring-launcher/
│ │ ├── VersionCard.tsx # 版本/更新卡片 (Silk 背景+毛玻璃)
│ │ ├── UnderConstruction.tsx # "装修中" 占位组件
│ │ └── StartupPopup.tsx # 启动弹窗 (环境变量控制)
│ ├── stores/ # Zustand 状态管理
│ ├── stores/
│ │ ├── configStore.ts # 统一配置 store (→ Koring.yml)
│ │ ├── themeStore.ts # 主题 (委托 configStore)
│ │ ├── a11yStore.ts # 无障碍 (委托 configStore)
│ │ ├── backgroundStore.ts # 背景 (委托 configStore)
│ │ ├── authStore.ts # 认证 (→ Registry)
│ │ ├── routeStore.ts # 路由 (历史栈)
│ │ ├── taskStore.ts # 任务队列 (localStorage)
│ │ ├── instanceStore.ts # 实例管理
│ │ ├── installStore.ts # Minecraft 安装
│ │ ├── launchStore.ts # 游戏启动
│ │ ├── modsStore.ts # Mod 搜索
│ │ ├── updateStore.ts # 应用更新
│ │ └── devStore.ts # 开发者调试
│ ├── api/
│ │ ├── config.ts # AppConfig 读写 (→ Tauri invoke)
│ │ ├── auth-registry.ts # AuthData 读写 (→ Registry)
│ │ ├── instance.ts # 实例 API
│ │ ├── auth.ts # 登录 API
│ │ ├── sidecar.ts # 通用 sidecar IPC
│ │ └── ...
│ ├── hooks/
│ │ └── useTheme.ts # 同步 darkMode → .dark class
│ ├── lib/
│ │ ├── mode.ts # BUILD_MODE 常量
│ │ └── utils.ts # cn() 工具函数
│ ├── api/ # Sidecar IPC 封装
│ ├── types/
│ │ └── task.ts # Task 类型定义
│ └── pages/ # 页面组件
├── src-tauri/ # Rust 后端
│ ├── Cargo.toml # 依赖: serde, serde_json, serde_yaml, winreg
│ ├── tauri.conf.json # 窗口配置 + Bundle + Updater
│ ├── capabilities/default.json # 权限声明
│ ├── src/
│ │ ├── lib.rs # 插件注册 + splash/main 窗口逻辑
│ │ ├── commands/mod.rs # Tauri 命令 (→ sidecar)
│ │ ├── lib.rs # 插件注册 + splash/main + 命令注册
│ │ ├── config.rs # AppConfig 读写 + 迁移 + 默认值
│ │ ├── registry.rs # Windows Registry 读写
│ │ ├── commands/mod.rs # Tauri 命令 (→ sidecar + config + auth)
│ │ └── sidecar.rs # Sidecar 进程管理
│ └── binaries/ # Sidecar 二进制文件
├── splash.html # 启动动画 HTML 入口 (加载 Splash.tsx)
├── sidecar/ # Node.js Sidecar
│ └── src/
│ ├── handlers/ # 10 个 handler
│ ├── utils/paths.ts # 路径工具 + InstanceConfig
│ └── protocol/types.ts # IPC 消息类型
├── splash.html # 启动动画 HTML 入口
├── build-vs.cmd # VS 环境编译脚本
└── dev-vs.cmd # VS 环境开发脚本
```
@@ -109,274 +270,105 @@ koring-launcher/
| Key | Label | 组件 | 说明 |
|---|---|---|---|
| `home` | 首页 | `pages/home/index.tsx` | 欢迎页 |
| `home` | 首页 | `pages/home/index.tsx` | StartCard 启动组件 |
| `store` | 资源 | `pages/store/index.tsx` | 🚧 装修中 |
| `today` | 资讯 | `pages/today/index.tsx` | 🚧 装修中 |
| `play-link` | 联机 | `pages/play-link/index.tsx` | 🚧 装修中 |
| `setting` | 设置 | `pages/setting/index.tsx` | 侧边栏 + 内容区 |
### 隐藏路由 (debug)
### 隐藏路由
| Key | Label | 组件 |
|---|---|---|
| `task-queue` | 任务队列 | `pages/task-queue.tsx` |
| `debug` | 调试 | `pages/debug/index.tsx` |
| `debug-splash` | 启动动画调试 | `pages/debug/splash-debug.tsx` |
| `debug-display` | 显示效果调试 | `pages/debug/display-debug.tsx` |
| `debug-version-card` | 版本卡片调试 | `pages/debug/version-card-debug.tsx` |
| `debug-task` | 任务队列调试 | `pages/debug/task-debug.tsx` |
### 路由层级 (返回导航)
### 路由导航 (历史栈)
```
home / store / today / play-link / setting
└─ debug
├─ debug-splash
├─ debug-display
├─ debug-version-card
└─ debug-task
```
### 设置子页面 (setting 内部侧边栏)
| 分组 | Key | 组件 | 状态 |
|---|---|---|---|
| **通用** | `home` | `general/home.tsx` | ✅ 设置首页 (搜索+快捷入口) |
| | `account` | `general/account.tsx` | ✅ Koring 账户 (微软登录) |
| | `game-account` | `game/game-account.tsx` | 🚧 占位 |
| | `about` | `general/about.tsx` | ✅ 关于 (版本卡+链接) |
| | `copyright` | `general/copyright.tsx` | ✅ 版权声明 |
| **游戏** | `java-mem` | `game/java-mem.tsx` | ✅ Java/内存/JVM 参数 |
| | `game-dir` | `game/game-dir.tsx` | ✅ 游戏目录路径 |
| | `advanced` | `game/advanced.tsx` | ✅ 高级设置 |
| **个性化** | `theme-bg` | `personalization/theme-bg.tsx` | ✅ 主题+背景 (文件选择器) |
| | `ui` | `personalization/ui.tsx` | ✅ UI 设置 |
| | `lang` | `personalization/lang.tsx` | ✅ 语言设置 |
| | `a11y` | `personalization/a11y.tsx` | ✅ 无障碍 |
| **网络** | `download` | `network/download.tsx` | ✅ 下载设置 |
| | `security-id` | `network/security-id.tsx` | ✅ 第三方认证 |
| | `ether-online` | `network/ether-online.tsx` | 🚧 占位 |
| | `tawa-online` | `network/tawa-online.tsx` | 🚧 占位 |
| **其他** | `feedback` | `other/feedback.tsx` | 🚧 占位 |
| | `sponsor` | `other/sponsor.tsx` | 🚧 占位 |
| | `developer` | _(→ debug route)_ | 跳转调试页 |
路由使用动态历史栈替代静态 parentMap。每次 `navigate()` 压入历史,`goBack()` 弹出。
---
## Zustand Stores
### routeStore
### configStore (统一配置中心)
管理页面路由和标题栏模式
所有用户设置的单一数据源。读写通过 Tauri invoke 与 `Koring.yml` 同步
```ts
// State
current: RouteKey // 当前路由 key
titleBarMode: TitleBarMode // "default" | "sub" | "window"
direction: TransitionDirection // "forward" | "backward"
config: AppConfig // 完整配置
loaded: boolean // 是否已从 Rust 加载
// Actions
navigate(key) // 跳转路由 (View Transitions API 动画)
goBack() // 返回父路由 (通过 parentMap)
setTitleBarMode(m) // 手动设置标题栏模式
init() // 从 Rust 加载配置
setTheme(patch) // 部分更新 + debounce 300ms 写回
setA11y(patch)
setBackground(patch)
setGame(patch)
setJava(patch)
setAdvanced(patch)
setDownload(patch)
setNetwork(patch)
```
### themeStore
深色模式和视差设置。
### themeStore (委托 configStore)
```ts
// State
darkMode: "auto" | "light" | "dark" // 默认 "auto"
parallax: boolean // 默认 true
// Actions
setDarkMode(mode) // 应用深色模式到 DOM (.dark class)
setParallax(v) // 设置视差开关
darkMode: "auto" | "light" | "dark"
parallax: boolean
setDarkMode(mode) // 更新 DOM + configStore
setParallax(v) // configStore
syncThemeFromConfig() // 启动时从 config 同步
```
### backgroundStore
背景图片/颜色/模糊/透明度 (localStorage 持久化)。
### a11yStore (委托 configStore)
```ts
// State (localStorage: "koring-background")
type: "image" | "color" // 默认 "image"
image: string // 默认 "/background.png"
blur: number // 默认 0 (0-20)
opacity: number // 默认 1 (0-1)
// Actions
setImage(url) // 设置背景图 (通过 convertFileSrc 转换本地路径)
setColor(color) // 设置纯色背景
setBlur(blur) // 设置模糊
setOpacity(op) // 设置透明度
reset() // 恢复默认
reduceMotion, reduceTransparency, highContrast, contentBlurOpacity
syncA11yFromConfig()
```
### a11yStore
无障碍设置。
### backgroundStore (委托 configStore)
```ts
// State
reduceMotion: boolean // 默认 false
reduceTransparency: boolean // 默认 false
highContrast: boolean // 默认 false
contentBlurOpacity: number // 默认 50 (0-100)
// Actions
setReduceMotion(v)
setReduceTransparency(v)
setHighContrast(v)
setContentBlurOpacity(v)
type: "image" | "color", image, blur, opacity
setImage / setColor / setBlur / setOpacity / reset
syncBackgroundFromConfig()
```
### devStore
开发者调试控制。
### authStore (委托 Registry)
```ts
// State
forceDisableContentBlur: boolean // 默认 false
previewMode: string | null // 默认 null
previewUpdateState: PreviewUpdateState | null // 默认 null
overlayOpacity: number // 默认 30
blurAmount: number // 默认 12
// Actions
setForceDisableContentBlur(v)
setPreviewMode(v)
setPreviewUpdateState(v)
setOverlayOpacity(v)
setBlurAmount(v)
user: AuthResult | null
initFromRegistry() // 从 Windows Registry 加载
loginOffline(username) // 通过 sidecar + 保存到 registry
logout() // 清除 registry
```
### updateStore
应用更新。
### routeStore (历史栈导航)
```ts
// State
checking: boolean
downloading: boolean
installed: boolean
progress: DownloadProgress | null
update: Update | null
error: string | null
// Actions
check() // 检查更新 (调用 @tauri-apps/plugin-updater)
install() // 下载并安装
reset() // 清除状态
current: RouteKey
history: RouteKey[] // 导航历史栈
navigate(key) // 压入历史
goBack() // 弹出历史
```
### taskStore
### taskStore (localStorage)
任务队列系统 (localStorage 持久化历史)。
任务队列localStorage 持久化历史 (max 50)。
```ts
// State (localStorage: "koring-task-history", max 50)
tasks: Task[]
sheetOpen: boolean
### 其他 Store
// Derived
isRunning() // 是否有运行中/等待中的任务
activeTasks() // 运行中+等待中的任务列表
completedTasks() // 已完成/失败/取消的任务列表
// Actions
addTask(type, title, desc, executor) // 添加任务并自动开始
cancelTask(id) // 取消任务 (AbortController)
removeTask(id) // 删除任务
retryTask(id) // 重试失败任务
clearHistory() // 清空历史
openSheet() / closeSheet()
```
### authStore
账户认证 (localStorage: "koring-user")。
```ts
// State
user: AuthResult | null // { username, uuid, accessToken, expiresAt, xboxProfile }
loading: boolean
error: string | null
// Actions
loginOffline(username)
startMicrosoftLogin(clientId)
completeMicrosoftLogin(code, clientId)
logout()
```
### installStore
Minecraft 安装。
```ts
// State
versions: VersionManifest | null
installing: boolean
loading: boolean
error: string | null
// Actions
fetchVersions(type?)
install(version, gamePath, javaPath?)
installLoader(mcVersion, gamePath, loaderType, loaderVersion?, javaPath?)
```
### launchStore
游戏启动。
```ts
// State
launching: boolean
launched: boolean
gameResult: LaunchResult | null
events: GameEvent[]
error: string | null
// Actions
launch(options: LaunchOptions)
diagnose(gamePath, version)
reset()
```
### modsStore
Mod 搜索与安装。
```ts
// State
searchResults: ModSearchResult[]
currentMod: ModSearchResult | null
modVersions: ModVersionResult[]
// Actions
search(query?, gameVersion?, loader?, source?)
getDetail(projectId, source)
getVersions(projectId, gameVersion?, loader?, source?)
install(projectId, versionId, gamePath, source?)
```
### instanceStore
游戏实例管理。
```ts
// State
instances: InstanceInfo[]
currentInstance: InstanceInfo | null
// Actions
fetchInstances(instancesPath)
create(name, gamePath, mcVersion, ...)
remove(name, instancesPath)
select(name, instancesPath)
```
- `devStore` — 开发者调试 (内存)
- `installStore` — Minecraft 安装 (内存,ephemeral)
- `launchStore` — 游戏启动 (内存,ephemeral)
- `modsStore` — Mod 搜索 (内存,ephemeral)
- `updateStore` — 应用更新 (内存,ephemeral)
- `instanceStore` — 实例管理 (sidecar 查询)
---
@@ -388,7 +380,6 @@ select(name, instancesPath)
z-0 BackgroundLayer 全屏背景图 + 视差 + 模糊 + 强内容遮罩
z-1 ContentLayer 页面内容区 (top: 40px, overflow-auto)
z-100 SystemLayer 自定义标题栏 (TitleBar)
z-110 TaskSheet 任务队列面板 (右滑入)
z-200 StartupPopup 启动弹窗 (环境变量控制)
```
@@ -406,12 +397,12 @@ z-200 StartupPopup 启动弹窗 (环境变量控制)
- 强内容遮罩: 非 home 页面自动显示 (可通过 `contentBlurOpacity` 控制)
- 深色模式叠加层: `bg-black/35`
### VersionCard 版本卡片
### StartCard 启动组件 (首页)
- Silk WebGL 动画背景 + 毛玻璃叠加层
- 三种更新状态: `latest` / `hasUpdate` / `installed`
- 颜色方案: dev=amber, beta=emerald, run=blue
- 可通过 props 覆盖 mode 和 state (用于 debug)
胶囊形启动组件,位于首页左下角:
- 左: 设置齿轮图标 (→ setting)
- 中: "启动游戏" 按钮 (primary 色, rounded-full)
- 右: 实例选择图标 (Package)
### TaskQueue 任务系统
@@ -432,22 +423,32 @@ z-200 StartupPopup 启动弹窗 (环境变量控制)
## 权限 (capabilities/default.json)
```json
```
core:default, core:window:default, core:window:allow-*,
core:webview:allow-create-webview-window,
opener:default, process:default, shell:allow-execute,
updater:default, dialog:default
```
## Rust 插件
## Rust 依赖
```toml
tauri = "2"
tauri-plugin-opener, tauri-plugin-process, tauri-plugin-dialog,
tauri-plugin-shell, tauri-plugin-updater
serde = { version = "1", features = ["derive"] }
serde_json = "1"
serde_yaml = "0.9"
uuid = { version = "1", features = ["v4"] }
tokio = { version = "1", features = ["time"] }
[target.'cfg(windows)'.dependencies]
winreg = "0.52"
```
## IPC 协议
前端 → `invoke()` → Rust `commands::sidecar_request` → sidecar stdin (JSON) → sidecar stdout (JSON) → Tauri 事件 → 前端
前端 → `invoke()` → Rust `commands::*` → sidecar stdin (JSON) → sidecar stdout (JSON) → Tauri 事件 → 前端
Sidecar 命令: `install-minecraft`, `install-mod-loader`, `get-version-list`, `launch-game`, `offline-login`, `search-mods`, `install-mod`, `create-instance`, `list-instances`, `background:*`
Sidecar 命令: `install-minecraft`, `install-mod-loader`, `get-version-list`, `launch-game`, `offline-login`, `search-mods`, `install-mod`, `create-instance`, `list-instances`
Rust 直接命令: `get_config`, `save_config`, `get_auth`, `save_auth`, `delete_auth_cmd`