按 Design §7.5 蓝图落地,让 H5 启动时能从沙盒读到实际渠道/启动/设备
值,闭合 Phase 1.17 联调"app_gameconfig 不正确"现象。
新增 Source/WebView/AppDataWriter.swift:
- public struct AppDataWriter(@MainActor)
- ContainerRole enum:.lobby / .subGame(name:, dir:)
- writeInitial():写 app_data.js(12 项)+ app_gamesname.js(暂只写当前
gameStart 一项,Phase 6 子游戏完整时扩为扫描已装列表)
- writeBattery(level:):写 app_battery.js(var app_getbattery=N;)
- writeNetwork(code:):写 app_network.js(var app_getnetwork=N;)
- 字面严格对齐 msext:
* 字符串单引号包裹 'value'
* 数值无引号(version=1 / Launchtype=0 / getwifisignalLevel=1)
* app_gamesname 用 "var app_gamesname=new Array(...)"(var 后两空格)
* 大小写硬约束:Launchtype L 大、getwifisignalLevel wifi 小 + signal/Level 区分
- escape 转义反斜杠 / 单引号 / 换行(避免渠道字段含单引号导致 H5 JS 解析错)
- Logger debug 输出文件路径 + 每个 key=value 一行,便于 §7.5.8 联调对账
新增 Source/Resource/NetworkMonitor.swift:
- @MainActor public final class NetworkMonitor
- NWPathMonitor 最简 wrap:currentCode 同步快照(默认 2 WiFi,启动后被
首次 path 更新)+ onChange MainActor 回调
- shared 单例 + 幂等 start()
WebContainer.runBootPipelineSteps 接入:
- switch outcome 前提取 let resolved: ResolvedVersion,避免变量作用域
限制 case 内
- 新增 writeAppDataFiles(resolved:):启动 NetworkMonitor + 开 battery
监控 + 写 4 个文件首次值 + 挂 addObserver(battery) + onChange(network)
- 调用位置:step 5 LobbyZipUpgrader 之后、step 6 loadFileURL 之前
(与 msext NewRootVC.initJSdata 等价时序)
BuildProject 通过
Plan §6.4 里程碑加一行。
Phase 1.17 联调验证:启动后 H5 console 应读到实际 app_channel /
app_gameconfig 等值,不再是 H5 zip 包内默认占位。
Co-Authored-By: Claude Opus 4.7 <noreply@anthropic.com>
49 lines
1.4 KiB
Swift
49 lines
1.4 KiB
Swift
//
|
||
// NetworkMonitor.swift
|
||
// ylgamehall
|
||
//
|
||
// NWPathMonitor 的最简 wrap:提供 currentCode 同步快照 + onChange 事件回调。
|
||
// 用于 AppDataWriter.writeNetwork(_:) 拿首次值 + 变化重写 app_network.js。
|
||
// 详见 docs/H5-Native-Implementation-Design.md §7.5 / §8.4。
|
||
//
|
||
|
||
import Foundation
|
||
import Network
|
||
|
||
/// `app_getnetwork` 的语义码(与 msext 沿用):
|
||
/// 1 = 无网,2 = WiFi,3 = 蜂窝
|
||
@MainActor
|
||
public final class NetworkMonitor {
|
||
|
||
public static let shared = NetworkMonitor()
|
||
|
||
public private(set) var currentCode: Int = 2
|
||
|
||
/// 网络变化回调。每次状态变化时主线程触发。
|
||
public var onChange: (@MainActor (Int) -> Void)?
|
||
|
||
private let monitor = NWPathMonitor()
|
||
private let queue = DispatchQueue(label: "ylgamehall.networkmonitor", qos: .utility)
|
||
private var started = false
|
||
|
||
public init() {}
|
||
|
||
public func start() {
|
||
guard !started else { return }
|
||
started = true
|
||
monitor.pathUpdateHandler = { [weak self] path in
|
||
let code: Int = {
|
||
if path.status != .satisfied { return 1 }
|
||
if path.usesInterfaceType(.wifi) { return 2 }
|
||
return 3
|
||
}()
|
||
Task { @MainActor in
|
||
guard let self else { return }
|
||
self.currentCode = code
|
||
self.onChange?(code)
|
||
}
|
||
}
|
||
monitor.start(queue: queue)
|
||
}
|
||
}
|