From e18e05589aa369fde910362d6df88797487a8eca Mon Sep 17 00:00:00 2001 From: redphx <96280+redphx@users.noreply.github.com> Date: Thu, 23 May 2024 06:22:25 +0700 Subject: [PATCH] Move some patch code to external files --- src/modules/patcher.ts | 58 ++++--------------- src/modules/patches/local-co-op-enable.js | 17 ++++++ src/modules/patches/remote-play-enable.js | 2 + src/modules/patches/remote-play-keep-alive.js | 7 +++ src/modules/patches/vibration-adjust.js | 11 ++++ src/types/index.d.ts | 5 +- 6 files changed, 52 insertions(+), 48 deletions(-) create mode 100644 src/modules/patches/local-co-op-enable.js create mode 100644 src/modules/patches/remote-play-enable.js create mode 100644 src/modules/patches/remote-play-keep-alive.js create mode 100644 src/modules/patches/vibration-adjust.js diff --git a/src/modules/patcher.ts b/src/modules/patcher.ts index 20d3344..5f72a7e 100644 --- a/src/modules/patcher.ts +++ b/src/modules/patcher.ts @@ -6,6 +6,11 @@ import { BxLogger } from "@utils/bx-logger"; import { hashCode } from "@utils/utils"; import { BxEvent } from "@/utils/bx-event"; +import codeLocalCoOpEnable from "./patches/local-co-op-enable.js" with { type: "text" }; +import codeRemotePlayEnable from "./patches/remote-play-enable.js" with { type: "text" }; +import codeRemotePlayKeepAlive from "./patches/remote-play-keep-alive.js" with { type: "text" }; +import codeVibrationAdjust from "./patches/vibration-adjust.js" with { type: "text" }; + type PatchArray = (keyof typeof PATCHES)[]; const ENDING_CHUNKS_PATCH_NAME = 'loadingEndingChunks'; @@ -92,31 +97,24 @@ const PATCHES = { }, remotePlayKeepAlive(str: string) { - if (!str.includes('onServerDisconnectMessage(e){')) { + const text = 'onServerDisconnectMessage(e){'; + if (!str.includes(text)) { return false; } - str = str.replace('onServerDisconnectMessage(e){', `onServerDisconnectMessage(e) { - const msg = JSON.parse(e); - if (msg.reason === 'WarningForBeingIdle' && !window.location.pathname.includes('/launch/')) { - try { - this.sendKeepAlive(); - return; - } catch (ex) { console.log(ex); } - } - `); + str = str.replace(text, text + codeRemotePlayKeepAlive); return str; }, // Enable Remote Play feature remotePlayConnectMode(str: string) { - const text = 'connectMode:"cloud-connect"'; + const text = 'connectMode:"cloud-connect",'; if (!str.includes(text)) { return false; } - return str.replace(text, `connectMode:window.BX_REMOTE_PLAY_CONFIG?"xhome-connect":"cloud-connect",remotePlayServerId:(window.BX_REMOTE_PLAY_CONFIG&&window.BX_REMOTE_PLAY_CONFIG.serverId)||''`); + return str.replace(text, codeRemotePlayEnable); }, // Disable achievement toast in Remote Play @@ -215,20 +213,8 @@ if (${gamepadVar}.buttons[17] && ${gamepadVar}.buttons[17].value === 1) { return false; } - const newCode = ` -if (!window.BX_ENABLE_CONTROLLER_VIBRATION) { - return void(0); -} -if (window.BX_VIBRATION_INTENSITY && window.BX_VIBRATION_INTENSITY < 1) { - e.leftMotorPercent = e.leftMotorPercent * window.BX_VIBRATION_INTENSITY; - e.rightMotorPercent = e.rightMotorPercent * window.BX_VIBRATION_INTENSITY; - e.leftTriggerMotorPercent = e.leftTriggerMotorPercent * window.BX_VIBRATION_INTENSITY; - e.rightTriggerMotorPercent = e.rightTriggerMotorPercent * window.BX_VIBRATION_INTENSITY; -} -`; - VibrationManager.updateGlobalVars(); - str = str.replaceAll(text, text + newCode); + str = str.replaceAll(text, text + codeVibrationAdjust); return str; }, @@ -324,27 +310,7 @@ window.dispatchEvent(new Event("${BxEvent.TOUCH_LAYOUT_MANAGER_READY}")); return false; } - let patchstr = ` -let match; -let onGamepadChangedStr = this.onGamepadChanged.toString(); - -onGamepadChangedStr = onGamepadChangedStr.replaceAll('0', 'arguments[1]'); -eval(\`this.onGamepadChanged = function \${onGamepadChangedStr}\`); - -let onGamepadInputStr = this.onGamepadInput.toString(); - -match = onGamepadInputStr.match(/(\\w+\\.GamepadIndex)/); -if (match) { - const gamepadIndexVar = match[0]; - onGamepadInputStr = onGamepadInputStr.replace('this.gamepadStates.get(', \`this.gamepadStates.get(\${gamepadIndexVar},\`); - eval(\`this.onGamepadInput = function \${onGamepadInputStr}\`); - BxLogger.info('supportLocalCoOp', '✅ Successfully patched local co-op support'); -} else { - BxLogger.error('supportLocalCoOp', '❌ Unable to patch local co-op support'); -} -`; - - const newCode = `true; ${patchstr}; true,`; + const newCode = `true; ${codeLocalCoOpEnable}; true,`; str = str.replace(text, text + newCode); return str; diff --git a/src/modules/patches/local-co-op-enable.js b/src/modules/patches/local-co-op-enable.js new file mode 100644 index 0000000..843782d --- /dev/null +++ b/src/modules/patches/local-co-op-enable.js @@ -0,0 +1,17 @@ +let match; +let onGamepadChangedStr = this.onGamepadChanged.toString(); + +onGamepadChangedStr = onGamepadChangedStr.replaceAll('0', 'arguments[1]'); +eval(`this.onGamepadChanged = function ${onGamepadChangedStr}`); + +let onGamepadInputStr = this.onGamepadInput.toString(); + +match = onGamepadInputStr.match(/(\w+\.GamepadIndex)/); +if (match) { + const gamepadIndexVar = match[0]; + onGamepadInputStr = onGamepadInputStr.replace('this.gamepadStates.get(', `this.gamepadStates.get(${gamepadIndexVar},`); + eval(`this.onGamepadInput = function ${onGamepadInputStr}`); + BxLogger.info('supportLocalCoOp', '✅ Successfully patched local co-op support'); +} else { + BxLogger.error('supportLocalCoOp', '❌ Unable to patch local co-op support'); +} diff --git a/src/modules/patches/remote-play-enable.js b/src/modules/patches/remote-play-enable.js new file mode 100644 index 0000000..56f88dc --- /dev/null +++ b/src/modules/patches/remote-play-enable.js @@ -0,0 +1,2 @@ +connectMode: window.BX_REMOTE_PLAY_CONFIG ? "xhome-connect" : "cloud-connect", +remotePlayServerId: (window.BX_REMOTE_PLAY_CONFIG && window.BX_REMOTE_PLAY_CONFIG.serverId) || '', diff --git a/src/modules/patches/remote-play-keep-alive.js b/src/modules/patches/remote-play-keep-alive.js new file mode 100644 index 0000000..5ccafac --- /dev/null +++ b/src/modules/patches/remote-play-keep-alive.js @@ -0,0 +1,7 @@ +const msg = JSON.parse(e); +if (msg.reason === 'WarningForBeingIdle' && !window.location.pathname.includes('/launch/')) { + try { + this.sendKeepAlive(); + return; + } catch (ex) { console.log(ex); } +} diff --git a/src/modules/patches/vibration-adjust.js b/src/modules/patches/vibration-adjust.js new file mode 100644 index 0000000..ce9149a --- /dev/null +++ b/src/modules/patches/vibration-adjust.js @@ -0,0 +1,11 @@ +if (!window.BX_ENABLE_CONTROLLER_VIBRATION) { + return void(0); +} + +const intensity = window.BX_VIBRATION_INTENSITY; +if (intensity && intensity < 1) { + e.leftMotorPercent *= intensity; + e.rightMotorPercent *= intensity; + e.leftTriggerMotorPercent *= intensity; + e.rightTriggerMotorPercent *= intensity; +} diff --git a/src/types/index.d.ts b/src/types/index.d.ts index e78ec97..2409859 100644 --- a/src/types/index.d.ts +++ b/src/types/index.d.ts @@ -73,5 +73,6 @@ type XcloudTitleInfo = { }; }; -declare module "*.svg"; -declare module "*.styl"; +declare module '*.js'; +declare module '*.svg'; +declare module '*.styl';