Execute scripts of the map after creating gameScene

This commit is contained in:
GRL 2021-08-16 14:07:24 +02:00 committed by David Négrier
parent 41c31d3d2f
commit ad95fa5e11
2 changed files with 65 additions and 51 deletions

View File

@ -293,57 +293,67 @@ class IframeListener {
this.iframes.delete(iframe); this.iframes.delete(iframe);
} }
registerScript(scriptUrl: string): void { registerScript(scriptUrl: string): Promise<void> {
console.log("Loading map related script at ", scriptUrl); return new Promise<void>((resolve, reject) => {
console.log("Loading map related script at ", scriptUrl);
if (!process.env.NODE_ENV || process.env.NODE_ENV === "development") { if (!process.env.NODE_ENV || process.env.NODE_ENV === "development") {
// Using external iframe mode ( // Using external iframe mode (
const iframe = document.createElement("iframe"); const iframe = document.createElement("iframe");
iframe.id = IframeListener.getIFrameId(scriptUrl); iframe.id = IframeListener.getIFrameId(scriptUrl);
iframe.style.display = "none"; iframe.style.display = "none";
iframe.src = "/iframe.html?script=" + encodeURIComponent(scriptUrl); iframe.src = "/iframe.html?script=" + encodeURIComponent(scriptUrl);
// We are putting a sandbox on this script because it will run in the same domain as the main website. // We are putting a sandbox on this script because it will run in the same domain as the main website.
iframe.sandbox.add("allow-scripts"); iframe.sandbox.add("allow-scripts");
iframe.sandbox.add("allow-top-navigation-by-user-activation"); iframe.sandbox.add("allow-top-navigation-by-user-activation");
document.body.prepend(iframe); iframe.addEventListener("load", () => {
resolve();
});
this.scripts.set(scriptUrl, iframe); document.body.prepend(iframe);
this.registerIframe(iframe);
} else {
// production code
const iframe = document.createElement("iframe");
iframe.id = IframeListener.getIFrameId(scriptUrl);
iframe.style.display = "none";
// We are putting a sandbox on this script because it will run in the same domain as the main website. this.scripts.set(scriptUrl, iframe);
iframe.sandbox.add("allow-scripts"); this.registerIframe(iframe);
iframe.sandbox.add("allow-top-navigation-by-user-activation"); } else {
// production code
const iframe = document.createElement("iframe");
iframe.id = IframeListener.getIFrameId(scriptUrl);
iframe.style.display = "none";
//iframe.src = "data:text/html;charset=utf-8," + escape(html); // We are putting a sandbox on this script because it will run in the same domain as the main website.
iframe.srcdoc = iframe.sandbox.add("allow-scripts");
"<!doctype html>\n" + iframe.sandbox.add("allow-top-navigation-by-user-activation");
"\n" +
'<html lang="en">\n' +
"<head>\n" +
'<script src="' +
window.location.protocol +
"//" +
window.location.host +
'/iframe_api.js" ></script>\n' +
'<script src="' +
scriptUrl +
'" ></script>\n' +
"<title></title>\n" +
"</head>\n" +
"</html>\n";
document.body.prepend(iframe); //iframe.src = "data:text/html;charset=utf-8," + escape(html);
iframe.srcdoc =
"<!doctype html>\n" +
"\n" +
'<html lang="en">\n' +
"<head>\n" +
'<script src="' +
window.location.protocol +
"//" +
window.location.host +
'/iframe_api.js" ></script>\n' +
'<script src="' +
scriptUrl +
'" ></script>\n' +
"<title></title>\n" +
"</head>\n" +
"</html>\n";
this.scripts.set(scriptUrl, iframe); iframe.addEventListener("load", () => {
this.registerIframe(iframe); resolve();
} });
document.body.prepend(iframe);
this.scripts.set(scriptUrl, iframe);
this.registerIframe(iframe);
}
});
} }
private getBaseUrl(src: string, source: MessageEventSource | null): string { private getBaseUrl(src: string, source: MessageEventSource | null): string {

View File

@ -165,7 +165,7 @@ export class GameScene extends DirtyScene {
value: RoomJoinedMessageInterface | PromiseLike<RoomJoinedMessageInterface> value: RoomJoinedMessageInterface | PromiseLike<RoomJoinedMessageInterface>
) => void; ) => void;
// A promise that will resolve when the "create" method is called (signaling loading is ended) // A promise that will resolve when the "create" method is called (signaling loading is ended)
private createPromise: Promise<void>; public createPromise: Promise<void>;
private createPromiseResolve!: (value?: void | PromiseLike<void>) => void; private createPromiseResolve!: (value?: void | PromiseLike<void>) => void;
private iframeSubscriptionList!: Array<Subscription>; private iframeSubscriptionList!: Array<Subscription>;
private peerStoreUnsubscribe!: () => void; private peerStoreUnsubscribe!: () => void;
@ -403,12 +403,6 @@ export class GameScene extends DirtyScene {
}); });
}); });
} }
// Now, let's load the script, if any
const scripts = this.getScriptUrls(this.mapFile);
for (const script of scripts) {
iframeListener.registerScript(script);
}
} }
//hook initialisation //hook initialisation
@ -567,6 +561,12 @@ export class GameScene extends DirtyScene {
} }
this.createPromiseResolve(); this.createPromiseResolve();
// Now, let's load the script, if any
const scripts = this.getScriptUrls(this.mapFile);
const scriptPromises = [];
for (const script of scripts) {
scriptPromises.push(iframeListener.registerScript(script));
}
this.userInputManager.spaceEvent(() => { this.userInputManager.spaceEvent(() => {
this.outlinedItem?.activate(); this.outlinedItem?.activate();
@ -584,6 +584,7 @@ export class GameScene extends DirtyScene {
this.triggerOnMapLayerPropertyChange(); this.triggerOnMapLayerPropertyChange();
if (!this.room.isDisconnected()) { if (!this.room.isDisconnected()) {
this.scene.sleep();
this.connect(); this.connect();
} }
@ -607,6 +608,10 @@ export class GameScene extends DirtyScene {
this.chatVisibilityUnsubscribe = chatVisibilityStore.subscribe((v) => { this.chatVisibilityUnsubscribe = chatVisibilityStore.subscribe((v) => {
this.openChatIcon.setVisible(!v); this.openChatIcon.setVisible(!v);
}); });
Promise.all([this.connectionAnswerPromise as Promise<unknown>, ...scriptPromises]).then(() => {
this.scene.wake();
});
} }
/** /**
@ -747,7 +752,6 @@ export class GameScene extends DirtyScene {
// Analyze tags to find if we are admin. If yes, show console. // Analyze tags to find if we are admin. If yes, show console.
if (this.scene.isSleeping()) { if (this.scene.isSleeping()) {
this.scene.wake();
this.scene.stop(ReconnectingSceneName); this.scene.stop(ReconnectingSceneName);
} }