* Initialize the runtime * 初始化运行时 * * @param wasmModule - Optional WASM module (from es_engine.js)
(wasmModule?: unknown)
| 100 | * @param wasmModule - Optional WASM module (from es_engine.js) |
| 101 | */ |
| 102 | async initialize(wasmModule?: unknown): Promise<void> { |
| 103 | if (this._initialized) { |
| 104 | console.warn('[Runtime] Already initialized'); |
| 105 | return; |
| 106 | } |
| 107 | |
| 108 | // Initialize browser file system service |
| 109 | this._fileSystem = new BrowserFileSystemService({ |
| 110 | baseUrl: this._assetBaseUrl, |
| 111 | catalogUrl: this._assetCatalogUrl, |
| 112 | enableCache: true |
| 113 | }); |
| 114 | await this._fileSystem.initialize(); |
| 115 | |
| 116 | // Initialize asset reader |
| 117 | this._assetReader = new BrowserAssetReader(this._assetBaseUrl); |
| 118 | |
| 119 | // Create browser platform adapter |
| 120 | // 创建输入子系统,用于处理键盘、鼠标、触摸事件 |
| 121 | // Create input subsystem for keyboard, mouse, touch events |
| 122 | // 使用 'direct' 模式,因为独立 Web 构建不使用 Tauri 代理 |
| 123 | // Use 'direct' mode since standalone web builds don't use Tauri proxy |
| 124 | const platform = new BrowserPlatformAdapter({ |
| 125 | wasmModule: wasmModule ?? undefined, |
| 126 | inputSubsystemFactory: () => new WebInputSubsystem(), |
| 127 | assetBaseUrl: this._assetBaseUrl, |
| 128 | pathResolveMode: 'direct' |
| 129 | }); |
| 130 | |
| 131 | // Create game runtime |
| 132 | this._runtime = createGameRuntime({ |
| 133 | platform, |
| 134 | canvasId: this._canvasId, |
| 135 | width: this._width, |
| 136 | height: this._height, |
| 137 | autoStartRenderLoop: false |
| 138 | }); |
| 139 | |
| 140 | await this._runtime.initialize(); |
| 141 | |
| 142 | // Register file system service |
| 143 | const IFileSystemServiceKey = Symbol.for('IFileSystemService'); |
| 144 | if (!Core.services.isRegistered(IFileSystemServiceKey)) { |
| 145 | Core.services.registerInstance(IFileSystemServiceKey, this._fileSystem); |
| 146 | } |
| 147 | |
| 148 | // Set asset reader for AssetManager |
| 149 | // 设置资产读取器 |
| 150 | if (this._assetReader && this._runtime.assetManager) { |
| 151 | this._runtime.assetManager.setReader(this._assetReader); |
| 152 | |
| 153 | // Initialize AssetManager with catalog from BrowserFileSystemService |
| 154 | // 使用 BrowserFileSystemService 的 catalog 初始化 AssetManager |
| 155 | // Catalog format is now unified - no conversion needed |
| 156 | // 目录格式已统一 - 无需转换 |
| 157 | if (this._fileSystem?.catalog) { |
| 158 | const catalog = this._fileSystem.catalog; |
| 159 | this._runtime.assetManager.initializeFromCatalog(catalog); |
nothing calls this directly
no test coverage detected