Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@ interface GlobalEventProtocol {
resize(): void;
/** 重启游戏 */
restart(): void;
/** 重启游戏完成 */
restartFinish(): void;
/** 等待关闭场景(当前场景所有模块生命周期执行完成) */
waitCloseScene(): void;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,7 @@ export class MKGame extends MKInstanceBase {
await Promise.all(globalEvent.request(globalEvent.key.restart));
await Promise.all(globalEvent.request(globalEvent.key.waitCloseScene));
game.restart();
globalEvent.emit(globalEvent.key.restartFinish);
Comment on lines 61 to +62

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

P1 Badge Await restart before emitting completion event

game.restart() is asynchronous (Promise<void> in tool/typedoc/declare/cc.d.ts), but restartFinish is emitted immediately afterward, so listeners can run before restart has actually completed. In the browser path, MKBundle uses this event to call initFunc(director.getScene()!); firing early can reinitialize against stale runtime state (or a not-yet-ready scene), which breaks the intended “restart finished” contract.

Useful? React with 👍 / 👎.

this._isRestarting = false;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -71,6 +71,8 @@ export namespace _MKLifeCycle {
isFirst?: boolean;
/** 销毁动态子节点 */
isDestroyChildren?: boolean;
/** 强制(重启时使用) */
isForce?: boolean;
}

export interface OpenShareData {
Expand Down Expand Up @@ -580,7 +582,7 @@ export class MKLifeCycle extends MKLayer implements MKRelease_.TypeFollowRelease
}

// 等待未完成用户任务
if (this._currentTask instanceof Promise) {
if (!config.isForce && this._currentTask instanceof Promise) {
await this._currentTask;
this._currentTask = null;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,7 @@ class MKSceneDrive extends MKLifeCycle {
await this._close({
isFirst: true,
isDestroyChildren: true,
isForce: true,
});

this._closeTask.finish(true);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ import MKStatusTask from "../Task/MKStatusTask";
import type { MKDataSharer_ } from "../MKDataSharer";
import mkToolFunc from "../@Private/Tool/MKToolFunc";
import MKRelease, { MKRelease_ } from "./MKRelease";
import { game, Game, director, Director, Scene, AssetManager, assetManager, js, Component, settings, SettingsCategory } from "cc";
import { game, Game, director, Director, Scene, AssetManager, assetManager, js, Component, settings, SettingsCategory, sys } from "cc";
import globalEvent from "../../Config/GlobalEvent";

namespace _MKBundle {
Expand Down Expand Up @@ -87,6 +87,22 @@ export class MKBundle extends MKInstanceBase {
return;
}

let initFunc = async (scene: Scene) => {
// 更新已加载脚本缓存
((settings.querySettings("assets", "preloadBundles") ?? []) as { bundle: string; version?: string }[]).forEach((v) => {
if (v.version) {
this._loadedScriptCache[`${v.bundle.replaceAll("/", "")}-${v.version}`] = true;
}
});

// 初始化 Bundle 管理器
await this.bundleMap.get("main")?.manage?.init?.();
// 初始化当前信息
this._setBundleStr("main");
this._sceneStr = scene.name ?? "";
this._initTask.finish(true);
};

// 引擎初始化事件
game.once(Game.EVENT_GAME_INITED, () => {
this._engineInitTask.finish(true);
Expand All @@ -103,19 +119,18 @@ export class MKBundle extends MKInstanceBase {
});
}

// 更新已加载脚本缓存
((settings.querySettings("assets", "preloadBundles") ?? []) as { bundle: string; version?: string }[]).forEach((v) => {
if (v.version) {
this._loadedScriptCache[`${v.bundle.replaceAll("/", "")}-${v.version}`] = true;
}
});
initFunc(scene);

// 初始化 Bundle 管理器
await this.bundleMap.get("main")?.manage?.init?.();
// 初始化当前信息
this._setBundleStr("main");
this._sceneStr = scene.name ?? "";
this._initTask.finish(true);
// web 不会重载脚本和触发事件
if (sys.isBrowser) {
globalEvent.on(
globalEvent.key.restartFinish,
() => {
initFunc(director.getScene()!);
},
this
);
}
},
this
);
Expand Down Expand Up @@ -573,7 +588,7 @@ export class MKBundle extends MKInstanceBase {
: {
versionStr: versionStr,
urlStr: urlStr,
};
};
}

/* ------------------------------- get/set ------------------------------- */
Expand Down
Loading