Move patchPollGamepads code to external file

This commit is contained in:
redphx 2024-05-23 06:55:41 +07:00
parent e18e05589a
commit 22e29e1d92
4 changed files with 25 additions and 6 deletions

View File

@ -3,9 +3,10 @@ import { BX_FLAGS } from "@utils/bx-flags";
import { getPref, PrefKey } from "@utils/preferences";
import { VibrationManager } from "@modules/vibration-manager";
import { BxLogger } from "@utils/bx-logger";
import { hashCode } from "@utils/utils";
import { hashCode, renderString } from "@utils/utils";
import { BxEvent } from "@/utils/bx-event";
import codeControllerShortcuts from "./patches/controller-shortcuts.js" with { type: "text" };
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" };
@ -175,11 +176,10 @@ if (!!window.BX_REMOTE_PLAY_CONFIG) {
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}'));
}
`;
const newCode = renderString(codeControllerShortcuts, {
gamepadVar,
});
codeBlock = codeBlock.replace('this.gamepadTimestamps.set', newCode + 'this.gamepadTimestamps.set');
}

View File

@ -0,0 +1,5 @@
const currentGamepad = ${gamepadVar};
if (currentGamepad.buttons[17] && currentGamepad.buttons[17].value === 1) {
window.dispatchEvent(new Event(BxEvent.CAPTURE_SCREENSHOT));
}

View File

@ -61,3 +61,5 @@ export namespace BxEvent {
target.dispatchEvent(event);
}
}
(window as any).BxEvent = BxEvent;

View File

@ -65,3 +65,15 @@ export function hashCode(str: string): number {
return hash;
}
export function renderString(str: string, obj: any){
return str.replace(/\$\{.+?\}/g, match => {
const key = match.substring(2, match.length - 1);
if (key in obj) {
return obj[key];
}
return match;
});
}