Test new structure of custom touch layout

This commit is contained in:
redphx 2024-02-09 17:46:05 +07:00
parent 2d9ee16531
commit 70a8fc9866

View File

@ -19,7 +19,7 @@ const SCRIPT_HOME = 'https://github.com/redphx/better-xcloud';
const ENABLE_XCLOUD_LOGGER = false; const ENABLE_XCLOUD_LOGGER = false;
const ENABLE_PRELOAD_BX_UI = false; const ENABLE_PRELOAD_BX_UI = false;
const USE_DEV_TOUCH_LAYOUT = false; const USE_DEV_TOUCH_LAYOUT = true;
const ENABLE_NATIVE_MKB_BETA = false; const ENABLE_NATIVE_MKB_BETA = false;
window.NATIVE_MKB_TITLES = [ window.NATIVE_MKB_TITLES = [
@ -3498,53 +3498,51 @@ class TouchController {
}, 10); }, 10);
} }
static getCustomLayouts(xboxTitleId) { static #dispatchLayouts(data) {
const dispatchLayouts = data => {
BxEvent.dispatch(window, BxEvent.CUSTOM_TOUCH_LAYOUTS_LOADED, { BxEvent.dispatch(window, BxEvent.CUSTOM_TOUCH_LAYOUTS_LOADED, {
data: data, data: data,
}); });
}; };
static getCustomLayouts(xboxTitleId, retries) {
xboxTitleId = '' + xboxTitleId; xboxTitleId = '' + xboxTitleId;
if (xboxTitleId in TouchController.#customLayouts) { if (xboxTitleId in TouchController.#customLayouts) {
dispatchLayouts(TouchController.#customLayouts[xboxTitleId]); TouchController.#dispatchLayouts(TouchController.#customLayouts[xboxTitleId]);
return; return;
} }
let url = 'https://raw.githubusercontent.com/redphx/better-xcloud/gh-pages/touch-layouts/'; retries = retries || 1;
if (USE_DEV_TOUCH_LAYOUT) { if (retries > 2) {
url += `dev/${xboxTitleId}.json`; TouchController.#customLayouts[xboxTitleId] = null;
} else { // Wait for BX_EXPOSED.touch_layout_manager
url += `${xboxTitleId}.json`; setTimeout(() => TouchController.#dispatchLayouts(null), 1000);
return;
} }
const baseUrl = `https://raw.githubusercontent.com/redphx/better-xcloud/gh-pages/touch-layouts${USE_DEV_TOUCH_LAYOUT ? '/dev' : ''}`;
const url = `${baseUrl}/${xboxTitleId}.json`;
// Get layout info
NATIVE_FETCH(url) NATIVE_FETCH(url)
.then(resp => resp.json()) .then(resp => resp.json())
.then(json => { .then(json => {
// Normalize data const layouts = {};
const schema_version = json.schema_version || 1;
let layout;
try {
if (schema_version === 1) {
json.layouts = {
default: {
name: 'Default',
content: json.layout,
},
};
json.default_layout = 'default';
delete json.layout;
}
} catch (e) {}
json.layouts.forEach(async file => {
const layoutUrl = `${baseUrl}/layouts/${file}.json`;
const json = await (await NATIVE_FETCH(layoutUrl)).json();
Object.assign(layouts, json.layouts);
});
json.layouts = layouts;
TouchController.#customLayouts[xboxTitleId] = json; TouchController.#customLayouts[xboxTitleId] = json;
// Wait for BX_EXPOSED.touch_layout_manager // Wait for BX_EXPOSED.touch_layout_manager
setTimeout(() => dispatchLayouts(json), 1000); setTimeout(() => TouchController.#dispatchLayouts(json), 1000);
}) })
.catch(() => { .catch(() => {
TouchController.#customLayouts[xboxTitleId] = null; // Retry
// Wait for BX_EXPOSED.touch_layout_manager TouchController.getCustomLayouts(xboxTitleId, retries + 1);
setTimeout(() => dispatchLayouts(null), 1000);
}); });
} }