@@ -211,6 +211,80 @@ else
211211 echo " (AppWindowAdapter.ets not found, skipping)"
212212fi
213213
214+ # --- Patch WebAbilityStage.ets for startup timing fix ------------------------
215+
216+ echo " ==> Patching web_engine WebAbilityStage for SetContextPaths timing ..."
217+
218+ WEB_ABILITY_STAGE=" ${WEB_ENGINE_DIR} /src/main/ets/application/WebAbilityStage.ets"
219+ if [ -f " ${WEB_ABILITY_STAGE} " ]; then
220+ python3 -c "
221+ import sys
222+
223+ with open('${WEB_ABILITY_STAGE} ', 'r') as f:
224+ content = f.read()
225+
226+ original = content
227+
228+ # 1. Add synchronous DI init + SetContextPaths to onCreate(), before
229+ # runTaskAsync() is called. This ensures the native module has the
230+ # HarmonyOS directory paths BEFORE the Electron runtime starts and
231+ # tries to call app.getPath('appData').
232+ old_oncreate = ''' onCreate(): void {
233+ LogUtil.info(TAG, '[ohoswindow] in WebAbilityStage onCreate.');
234+ this.runTaskAsync();
235+ }'''
236+
237+ new_oncreate = ''' onCreate(): void {
238+ LogUtil.info(TAG, '[ohoswindow] in WebAbilityStage onCreate.');
239+ // Initialize DI container and set context paths synchronously,
240+ // before the Electron runtime starts. This ensures app.getPath()
241+ // works when the Electron main process loads app-props.js.
242+ // (Moved from runTaskAsync to fix race condition on slower devices.)
243+ if (!GlobalThisHelper.isLaunched()) {
244+ let appContext = this.context.getApplicationContext();
245+ GlobalThisHelper.appInit(new CommonDependencyProvider(appContext));
246+ }
247+ JsBindingUtils.SetContextPaths();
248+ this.runTaskAsync();
249+ }'''
250+
251+ content = content.replace(old_oncreate, new_oncreate)
252+
253+ # 2. Remove the appInit block from runTaskAsync() (now done in onCreate)
254+ old_appinit = ''' if (!GlobalThisHelper.isLaunched()) {
255+ let appContext = this.context.getApplicationContext();
256+ GlobalThisHelper.appInit(new CommonDependencyProvider(appContext));
257+ }
258+ import'''
259+
260+ new_appinit = ''' import'''
261+
262+ content = content.replace(old_appinit, new_appinit)
263+
264+ # 3. Remove SetContextPaths from the async import().then() callback
265+ # (now done synchronously in onCreate)
266+ old_setpaths = ''' this.nativeThemeAdapter = Inject.get(NativeThemeAdapter);
267+ JsBindingUtils.SetContextPaths();
268+ }).catch'''
269+
270+ new_setpaths = ''' this.nativeThemeAdapter = Inject.get(NativeThemeAdapter);
271+ }).catch'''
272+
273+ content = content.replace(old_setpaths, new_setpaths)
274+
275+ if content == original:
276+ print(' WARNING: WebAbilityStage.ets pattern not found — file may already be patched or structure changed')
277+ sys.exit(0)
278+
279+ with open('${WEB_ABILITY_STAGE} ', 'w') as f:
280+ f.write(content)
281+
282+ print(' WebAbilityStage patched: SetContextPaths moved to synchronous onCreate()')
283+ "
284+ else
285+ echo " (WebAbilityStage.ets not found, skipping)"
286+ fi
287+
214288# --- Check signing materials ------------------------------------------------
215289
216290echo " ==> Checking signing materials ..."
0 commit comments