Fix not showing custom touch controller layout sometimes

This commit is contained in:
redphx 2024-01-30 09:40:07 +07:00
parent a132a1b1e0
commit a90868ef2f

View File

@ -3381,11 +3381,7 @@ class TouchController {
} }
static #show() { static #show() {
if (GAME_XBOX_TITLE_ID && GAME_XBOX_TITLE_ID in TouchController.#customLayouts) { TouchController.loadCustomLayout(GAME_XBOX_TITLE_ID, 0);
TouchController.loadCustomLayout(GAME_XBOX_TITLE_ID);
} else {
TouchController.#showDefault();
}
TouchController.#showing = true; TouchController.#showing = true;
} }
@ -3428,35 +3424,38 @@ class TouchController {
return; return;
} }
let url; let url = 'https://raw.githubusercontent.com/redphx/better-xcloud/gh-pages/touch-layouts/';
if (USE_DEV_TOUCH_LAYOUT) { if (USE_DEV_TOUCH_LAYOUT) {
url = `https://raw.githubusercontent.com/redphx/better-xcloud/gh-pages/touch-layouts/dev/${xboxTitleId}.json`; url += `dev/${xboxTitleId}.json`;
} else { } else {
url = `https://raw.githubusercontent.com/redphx/better-xcloud/gh-pages/touch-layouts/${xboxTitleId}.json`; url += `${xboxTitleId}.json`;
} }
window.BX_EXPOSED.touch_layout_manager && NATIVE_FETCH(url) window.BX_EXPOSED.touch_layout_manager && NATIVE_FETCH(url)
.then(resp => resp.json()) .then(resp => resp.json(), () => {
TouchController.#customLayouts[xboxTitleId] = null;
callback(null);
})
.then(json => { .then(json => {
TouchController.#customLayouts[xboxTitleId] = json; TouchController.#customLayouts[xboxTitleId] = json;
callback(json); callback(json);
})
.reject(() => {
TouchController.#customLayouts[xboxTitleId] = null;
callback(null);
}); });
} }
static loadCustomLayout(xboxTitleId) { static loadCustomLayout(xboxTitleId, delay) {
if (!window.BX_EXPOSED.touch_layout_manager) { if (!window.BX_EXPOSED.touch_layout_manager) {
return; return;
} }
xboxTitleId = '' + xboxTitleId; xboxTitleId = '' + xboxTitleId;
TouchController.#getCustomLayout(xboxTitleId, json => { TouchController.#getCustomLayout(xboxTitleId, json => {
json && setTimeout(() => { if (!json) {
return;
}
setTimeout(() => {
window.BX_EXPOSED.touch_layout_manager.changeLayoutForScope({ window.BX_EXPOSED.touch_layout_manager.changeLayoutForScope({
type: 'showLayout', type: 'showLayout',
scope: '' + xboxTitleId, scope: xboxTitleId,
subscope: 'base', subscope: 'base',
layout: { layout: {
id: 'System.Standard', id: 'System.Standard',
@ -3466,7 +3465,7 @@ class TouchController {
}, },
} }
}); });
}, 1000); }, delay);
}); });
} }
@ -3535,25 +3534,25 @@ class TouchController {
} }
// Load custom touch layout // Load custom touch layout
let showCustom = false;
try { try {
if (msg.data.includes('/titleinfo')) { if (msg.data.includes('/titleinfo')) {
const json = JSON.parse(JSON.parse(msg.data).content); const json = JSON.parse(JSON.parse(msg.data).content);
if (json.focused) { if (json.focused) {
const xboxTitleId = parseInt(json.titleid, 16); const xboxTitleId = parseInt(json.titleid, 16);
GAME_XBOX_TITLE_ID = xboxTitleId; GAME_XBOX_TITLE_ID = xboxTitleId;
TouchController.loadCustomLayout(xboxTitleId); TouchController.loadCustomLayout(xboxTitleId, 1000);
showCustom = true;
} else { } else {
GAME_XBOX_TITLE_ID = null; GAME_XBOX_TITLE_ID = null;
} }
return;
} }
} catch (e) { console.log(e) } } catch (e) { console.log(e) }
// Dispatch a message to display generic touch controller // Dispatch a message to display generic touch controller
if (!showCustom && msg.data.includes('touchcontrols/showtitledefault')) { if (msg.data.includes('touchcontrols/showtitledefault')) {
TouchController.#showDefault(); TouchController.#show();
} }
}); });