1
0
mirror of https://github.com/zumbiepig/MineXLauncher.git synced 2025-06-08 09:24:48 +00:00
This commit is contained in:
zumbiepig 2024-09-11 15:22:11 -07:00
parent 67dc2b893a
commit 35cee6be36
No known key found for this signature in database
GPG Key ID: 17C891BE28B953DE

View File

@ -7,6 +7,7 @@ declare global {
interface Window { interface Window {
console: Console; console: Console;
gameWindow: Window | null; gameWindow: Window | null;
gameWindowIframe: Window | null;
game: { game: {
play: (version?: string) => void; play: (version?: string) => void;
stop: (error?: string) => void; stop: (error?: string) => void;
@ -125,24 +126,33 @@ const game = {
window.gameWindow.document.body.append(iframe); window.gameWindow.document.body.append(iframe);
if (iframe.contentWindow) { if (iframe.contentWindow) {
window.gameWindow.onload = () => { (
if (iframe.contentWindow) { ['debug', 'log', 'info', 'warn', 'error'] as (
iframe.contentWindow.console.debug = (msg: string) => | 'debug'
consoleLog('debug', msg); | 'log'
iframe.contentWindow.console.log = (msg: string) => | 'info'
consoleLog('log', msg); | 'warn'
iframe.contentWindow.console.info = (msg: string) => | 'error'
consoleLog('info', msg); )[]
iframe.contentWindow.console.warn = (msg: string) => ).forEach((type) => {
consoleLog('warn', msg); if (iframe.contentWindow)
iframe.contentWindow.console.error = (msg: string) => iframe.contentWindow.console[type] = (msg: string) =>
consoleLog('error', msg); consoleLog(type, msg);
} });
};
window.gameWindow.onbeforeunload = () => game.stop(); ['keydown', 'keyup'].forEach((eventType) =>
iframe.contentWindow.onbeforeunload = () => game.stop(); window.gameWindow?.addEventListener(eventType, (event) =>
iframe.contentWindow?.dispatchEvent(
new KeyboardEvent(eventType, event),
),
),
);
window.gameWindow.focus();
window.gameWindow.document.documentElement.requestFullscreen();
} }
window.gameWindowIframe = iframe.contentWindow;
} }
} else { } else {
window.gameWindow.focus(); window.gameWindow.focus();
@ -155,25 +165,34 @@ const game = {
const waitForCrash = setInterval(() => { const waitForCrash = setInterval(() => {
if (window.gameWindow?.closed) { if (window.gameWindow?.closed) {
clearInterval(waitForCrash); clearInterval(waitForCrash);
game.stop(); game.stop(undefined, { killed: true });
} else { } else {
window.gameWindow?.document window.gameWindowIframe?.document
.querySelectorAll('div') .querySelectorAll('div')
.forEach((element: HTMLElement) => { .forEach((element: HTMLElement) => {
if (element.innerText.includes('Game Crashed!')) { if (
element.innerText.includes(
"Game Crashed! I have fallen and I can't get up!",
)
) {
clearInterval(waitForCrash); clearInterval(waitForCrash);
game.stop(element.innerHTML); game.stop(element.innerText);
} }
}); });
} }
}, 50); }, 50);
} }
}, },
stop: function (error?: string) { stop: function (error?: string, options?: { killed: boolean }) {
if (window !== window.top) window.top?.game.stop(error); if (window !== window.top) window.top?.game.stop(error);
else { else {
if (window.gameWindow && !window.gameWindow.closed) { if (
window.gameWindow &&
(!window.gameWindow.closed || options?.killed) &&
window.gameWindowIframe
) {
window.gameWindow.onbeforeunload = null; window.gameWindow.onbeforeunload = null;
window.gameWindowIframe.onbeforeunload = null;
window.gameWindow.close(); window.gameWindow.close();
storage.session.set('playingGame', false); storage.session.set('playingGame', false);
if (error) { if (error) {
@ -182,6 +201,7 @@ const game = {
} else { } else {
consoleLog('error', 'MineXLauncher: Game process killed by user'); consoleLog('error', 'MineXLauncher: Game process killed by user');
} }
window.focus();
} }
} }
}, },
@ -189,11 +209,8 @@ const game = {
selectedVersion = path; selectedVersion = path;
const selector = document.querySelector('.installations div .selector'); const selector = document.querySelector('.installations div .selector');
if (selector?.textContent) { if (selector?.textContent) {
if (name) { if (name) selector.textContent = `Selected: ${name}`;
selector.textContent = `Selected: ${name}`; else selector.textContent = `Selected: ${path}`;
} else {
selector.textContent = `Selected: ${path}`;
}
} }
versionSelector.close(); versionSelector.close();
}, },