Fix global vars

This commit is contained in:
redphx
2024-04-24 16:50:59 +07:00
parent 9446e39eb0
commit 9d0f4c380b
30 changed files with 87 additions and 69 deletions

View File

@@ -1,3 +1,5 @@
import { AppInterface } from "../utils/global";
export enum BxEvent {
JUMP_BACK_IN_READY = 'bx-jump-back-in-ready',
POPSTATE = 'bx-popstate',

View File

@@ -1,3 +1,5 @@
import { States } from "../utils/global";
export const BxExposed = {
onPollingModeChanged: (mode: 'All' | 'None') => {
if (!States.isPlaying) {

View File

@@ -1,7 +1,7 @@
import { CE } from "../utils/html";
import { getPreferredServerRegion } from "../utils/region";
import { TitlesInfo } from "../utils/titles-info";
import { PrefKey, Preferences, getPref } from "./preferences";
import { PrefKey, getPref } from "./preferences";
import { t } from "./translation";
export class LoadingScreen {
@@ -112,8 +112,6 @@ export class LoadingScreen {
endDateStr = endDateStr.substring(0, 10) + ' ' + endDateStr.substring(11, 19);
endDateStr += ` (${LoadingScreen.#secondsToString(waitTime)})`;
let estimatedWaitTime = LoadingScreen.#secondsToString(waitTime);
let $waitTimeBox = LoadingScreen.#$waitTimeBox;
if (!$waitTimeBox) {
$waitTimeBox = CE<HTMLElement>('div', {'class': 'bx-wait-time-box'},

View File

@@ -126,7 +126,7 @@ export class MkbHandler {
const virtualGamepad = this.#getVirtualGamepad();
if (buttonIndex >= 100) {
let [valueArr, axisIndex, fullValue] = this.#STICK_MAP[buttonIndex];
let [valueArr, axisIndex] = this.#STICK_MAP[buttonIndex];
valueArr = valueArr as number[];
axisIndex = axisIndex as number;
@@ -232,7 +232,6 @@ export class MkbHandler {
const analog = mouseMapTo === MouseMapTo.LS ? GamepadStick.LEFT : GamepadStick.RIGHT;
const virtualGamepad = this.#getVirtualGamepad();
let { x, y } = this.#getStickAxes(analog);
const length = this.#vectorLength(x, y);
@@ -258,7 +257,7 @@ export class MkbHandler {
}
}
#onMouseStopped = (e: MouseEvent) => {
#onMouseStopped = () => {
this.#allowStickDecaying = true;
requestAnimationFrame(this.#decayStick);
}
@@ -273,7 +272,7 @@ export class MkbHandler {
this.#allowStickDecaying = false;
this.#detectMouseStoppedTimeout && clearTimeout(this.#detectMouseStoppedTimeout);
this.#detectMouseStoppedTimeout = setTimeout(this.#onMouseStopped.bind(this, e), 100);
this.#detectMouseStoppedTimeout = setTimeout(this.#onMouseStopped.bind(this), 100);
const deltaX = e.movementX;
const deltaY = e.movementY;
@@ -326,7 +325,7 @@ export class MkbHandler {
});
}
#onPointerLockChange = (e: Event) => {
#onPointerLockChange = () => {
if (this.#enabled && !document.pointerLockElement) {
this.stop();
this.#waitForPointerLock(true);
@@ -458,7 +457,7 @@ export class MkbHandler {
}
static setupEvents() {
window.addEventListener(BxEvent.STREAM_PLAYING, e => {
window.addEventListener(BxEvent.STREAM_PLAYING, () => {
// Enable MKB
if (getPref(PrefKey.MKB_ENABLED)) {
console.log('Emulate MKB');

View File

@@ -10,7 +10,7 @@ import { MkbHandler } from "./mkb-handler";
import { LocalDb } from "../../utils/local-db";
import { Icon } from "../../utils/html";
import { SettingElement } from "../settings";
import type { MkbConvertedPresetData, MkbPresetData, MkbStoredPresets } from "../../types/mkb";
import type { MkbPresetData, MkbStoredPresets } from "../../types/mkb";
type MkbRemapperElements = {

View File

@@ -1,5 +1,6 @@
import { States } from "../utils/global";
import { BX_FLAGS } from "./bx-flags";
import { getPref, Preferences, PrefKey } from "./preferences";
import { getPref, PrefKey } from "./preferences";
import { VibrationManager } from "./vibration-manager";
const PATCHES = {
@@ -492,7 +493,6 @@ export class Patcher {
static patch(item: any) {
// console.log('patch', '-----');
let patchName;
let appliedPatches;
for (let id in item[1]) {

View File

@@ -3,7 +3,8 @@ import { t } from "./translation";
import { SettingElement, SettingElementType } from "./settings";
import { UserAgentProfile } from "../utils/user-agent";
import { StreamStat } from "./stream/stream-stats";
import type { PreferenceSetting, PreferenceSettings } from "../types/preferences";
import type { PreferenceSettings } from "../types/preferences";
import { States } from "../utils/global";
export enum PrefKey {
LAST_UPDATE_CHECK = 'version_last_check',

View File

@@ -1,7 +1,8 @@
import { States, AppInterface } from "../utils/global";
import { CE, createButton, ButtonStyle, Icon } from "../utils/html";
import { Toast } from "../utils/toast";
import { BxEvent } from "./bx-event";
import { getPref, Preferences, PrefKey, setPref } from "./preferences";
import { getPref, PrefKey, setPref } from "./preferences";
import { t } from "./translation";
import { localRedirect } from "./ui/ui";
@@ -82,7 +83,6 @@ export class RemotePlay {
};
static #$content: HTMLElement;
static #$consoles: HTMLElement;
static #initialize() {
if (RemotePlay.#$content) {

View File

@@ -1,3 +1,4 @@
import { States, AppInterface } from "../utils/global";
import { CE } from "../utils/html";
export function takeScreenshot(callback: any) {

View File

@@ -1,5 +1,5 @@
import type { PreferenceSetting } from "../types/preferences";
import { CE } from "../utils/html";
import type { PreferenceSetting } from "./preferences";
type MultipleOptionsParams = {
size?: number;
@@ -53,8 +53,8 @@ export class SettingElement {
$control.setAttribute('size', params.size.toString());
}
for (let value in setting.multiple_options) {
const label = setting.multiple_options[value];
for (let value in setting.multipleOptions) {
const label = setting.multipleOptions[value];
const $option = CE<HTMLOptionElement>('option', {value: value}, label) as HTMLOptionElement;
$option.selected = currentValue.indexOf(value) > -1;

View File

@@ -1,6 +1,7 @@
import { t } from "../translation";
import { BxEvent } from "../bx-event";
import { CE } from "../../utils/html";
import { States } from "../../utils/global";
enum StreamBadge {
PLAYTIME = 'playtime',
@@ -85,7 +86,7 @@ export class StreamBadges {
} catch(e) {}
}
const stats = await STREAM_WEBRTC.getStats();
const stats = await States.currentStream.peerConnection?.getStats()!;
let totalIn = 0;
let totalOut = 0;
stats.forEach(stat => {

View File

@@ -1,9 +1,10 @@
import { PrefKey, Preferences } from "../preferences"
import { PrefKey } from "../preferences"
import { BxEvent } from "../bx-event"
import { getPref } from "../preferences"
import { StreamBadges } from "./stream-badges"
import { CE } from "../../utils/html"
import { t } from "../translation"
import { States } from "../../utils/global"
export enum StreamStat {
PING = 'ping',
@@ -105,13 +106,13 @@ export class StreamStats {
}
static update() {
if (StreamStats.isHidden() || !STREAM_WEBRTC) {
if (StreamStats.isHidden() || !States.currentStream.peerConnection) {
StreamStats.onStoppedPlaying();
return;
}
const PREF_STATS_CONDITIONAL_FORMATTING = getPref(PrefKey.STATS_CONDITIONAL_FORMATTING);
STREAM_WEBRTC.getStats().then(stats => {
States.currentStream.peerConnection.getStats().then(stats => {
stats.forEach(stat => {
let grade = '';
if (stat.type === 'inbound-rtp' && stat.kind === 'video') {
@@ -213,7 +214,7 @@ export class StreamStats {
}
static getServerStats() {
STREAM_WEBRTC && STREAM_WEBRTC.getStats().then(stats => {
States.currentStream.peerConnection && States.currentStream.peerConnection.getStats().then(stats => {
const allVideoCodecs: {[index: string]: RTCBasicStat} = {};
let videoCodecId;

View File

@@ -1,3 +1,4 @@
import { States } from "../../utils/global";
import { Icon } from "../../utils/html";
import { BxEvent } from "../bx-event";
import { PrefKey, getPref } from "../preferences";
@@ -44,6 +45,7 @@ class MouseHoldEvent {
this.#$elm.addEventListener('touchend', this.#onMouseUp.bind(this));
}
/*
#clearEventLiseners = () => {
this.#$elm.removeEventListener('mousedown', this.#onMouseDown);
this.#$elm.removeEventListener('click', this.#onMouseUp);
@@ -51,6 +53,7 @@ class MouseHoldEvent {
this.#$elm.removeEventListener('touchstart', this.#onMouseDown);
this.#$elm.removeEventListener('touchend', this.#onMouseUp);
}
*/
constructor($elm: HTMLElement, callback: any, duration=1000) {
this.#$elm = $elm;

View File

@@ -1,8 +1,9 @@
import { NATIVE_FETCH, States } from "../utils/global";
import { CE } from "../utils/html";
import { Toast } from "../utils/toast";
import { BxEvent } from "./bx-event";
import { BX_FLAGS } from "./bx-flags";
import { getPref, Preferences, PrefKey } from "./preferences";
import { getPref, PrefKey } from "./preferences";
import { t } from "./translation";
export class TouchController {
@@ -11,10 +12,12 @@ export class TouchController {
origin: 'better-xcloud',
});
/*
static readonly #EVENT_HIDE_CONTROLLER = new MessageEvent('message', {
data: '{"content":"","target":"/streaming/touchcontrols/hide","type":"Message"}',
origin: 'better-xcloud',
});
*/
static #$bar: HTMLElement;
static #$style: HTMLStyleElement;

View File

@@ -1,3 +1,5 @@
import { LOCALE } from "../utils/global";
const Translations = {
enUS: -1,

View File

@@ -1,8 +1,9 @@
import { States, AppInterface, SCRIPT_HOME, SCRIPT_VERSION } from "../../utils/global";
import { CE, createButton, Icon, ButtonStyle } from "../../utils/html";
import { getPreferredServerRegion } from "../../utils/region";
import { UserAgent, UserAgentProfile } from "../../utils/user-agent";
import { getPref, Preferences, PrefKey, setPref, toPrefElement } from "../preferences";
import { getLocale, t } from "../translation";
import { t } from "../translation";
const SETTINGS_UI = {
'Better xCloud': {
@@ -159,7 +160,7 @@ export function setupSettingsUi() {
if ((e.target as HTMLElement).id === 'bx_setting_' + PrefKey.BETTER_XCLOUD_LOCALE) {
// Update locale
LOCALE = getLocale();
refreshLocale();
const $btn = $reloadBtnWrapper.firstElementChild! as HTMLButtonElement;
$btn.textContent = t('settings-reloading');
@@ -345,3 +346,6 @@ export function setupSettingsUi() {
const $pageContent = document.getElementById('PageContent');
$pageContent?.parentNode?.insertBefore($container, $pageContent);
}
function refreshLocale() {
throw new Error("Function not implemented.");
}

View File

@@ -1,3 +1,4 @@
import { SCRIPT_VERSION } from "../../utils/global";
import { createButton, Icon, ButtonStyle } from "../../utils/html";
import { getPreferredServerRegion } from "../../utils/region";
import { PrefKey, getPref } from "../preferences";

View File

@@ -1,8 +1,9 @@
import { States } from "../../utils/global";
import { Icon, CE, createButton, ButtonStyle } from "../../utils/html";
import { UserAgent } from "../../utils/user-agent";
import { BxEvent } from "../bx-event";
import { MkbRemapper } from "../mkb/mkb-remapper";
import { getPref, Preferences, PrefKey, toPrefElement } from "../preferences";
import { getPref, PrefKey, toPrefElement } from "../preferences";
import { setupScreenshotButton } from "../screenshot";
import { StreamStats } from "../stream/stream-stats";
import { TouchController } from "../touch-controller";

View File

@@ -1,3 +1,4 @@
import { AppInterface } from "../utils/global";
import { BxEvent } from "./bx-event";
import { PrefKey, getPref } from "./preferences";