From a376f443efdfc63021c9d30830a37f10d780636d Mon Sep 17 00:00:00 2001 From: redphx <96280+redphx@users.noreply.github.com> Date: Tue, 21 May 2024 16:51:55 +0700 Subject: [PATCH] Map the Share button on Xbox Series controller with the capturing screenshot feature --- src/index.ts | 4 ++++ src/modules/patcher.ts | 36 +++++++++++++++++++++++++++++------- src/utils/bx-event.ts | 2 ++ 3 files changed, 35 insertions(+), 7 deletions(-) diff --git a/src/index.ts b/src/index.ts index 74698ce..212a161 100644 --- a/src/index.ts +++ b/src/index.ts @@ -192,6 +192,10 @@ window.addEventListener(BxEvent.STREAM_STOPPED, e => { GameBar.getInstance().disable(); }); +window.addEventListener(BxEvent.CAPTURE_SCREENSHOT, e => { + Screenshot.takeScreenshot(); +}); + function main() { // Monkey patches diff --git a/src/modules/patcher.ts b/src/modules/patcher.ts index 1537124..20d3344 100644 --- a/src/modules/patcher.ts +++ b/src/modules/patcher.ts @@ -155,15 +155,37 @@ if (!!window.BX_REMOTE_PLAY_CONFIG) { return str.replace(text, 'this.shouldCollectStats=!1'); }, - blockGamepadStatsCollector(str: string) { - const text = 'this.inputPollingIntervalStats.addValue'; - if (!str.includes(text)) { + patchPollGamepads(str: string) { + const index = str.indexOf('},this.pollGamepads=()=>{'); + if (index === -1) { return false; } - str = str.replace('this.inputPollingIntervalStats.addValue', ''); - str = str.replace('this.inputPollingDurationStats.addValue', ''); - return str; + const nextIndex = str.indexOf('setTimeout(this.pollGamepads', index); + if (nextIndex === -1) { + return false; + } + + let codeBlock = str.substring(index, nextIndex); + + // Block gamepad stats collecting + if (getPref(PrefKey.BLOCK_TRACKING)) { + codeBlock = codeBlock.replaceAll('this.inputPollingIntervalStats.addValue', ''); + } + + // Map the Share button on Xbox Series controller with the capturing screenshot feature + const match = codeBlock.match(/this\.gamepadTimestamps\.set\((\w+)\.index/); + if (match) { + const gamepadVar = match[1]; + const newCode = ` +if (${gamepadVar}.buttons[17] && ${gamepadVar}.buttons[17].value === 1) { + window.dispatchEvent(new Event('${BxEvent.CAPTURE_SCREENSHOT}')); +} +`; + codeBlock = codeBlock.replace('this.gamepadTimestamps.set', newCode + 'this.gamepadTimestamps.set'); + } + + return str.substring(0, index) + codeBlock + str.substring(nextIndex); }, enableXcloudLogger(str: string) { @@ -619,7 +641,7 @@ let PLAYING_PATCH_ORDERS: PatchArray = [ BX_FLAGS.EnableXcloudLogging && 'enableConsoleLogging', - getPref(PrefKey.BLOCK_TRACKING) && 'blockGamepadStatsCollector', + 'patchPollGamepads', getPref(PrefKey.STREAM_COMBINE_SOURCES) && 'streamCombineSources', diff --git a/src/utils/bx-event.ts b/src/utils/bx-event.ts index de5e10d..a30956b 100644 --- a/src/utils/bx-event.ts +++ b/src/utils/bx-event.ts @@ -34,6 +34,8 @@ export enum BxEvent { GAME_BAR_ACTION_ACTIVATED = 'bx-game-bar-action-activated', MICROPHONE_STATE_CHANGED = 'bx-microphone-state-changed', + + CAPTURE_SCREENSHOT = 'bx-capture-screenshot', } export enum XcloudEvent {