2
0
mirror of https://github.com/Alia5/GlosSI.git synced 2024-10-30 15:20:38 +00:00
GlosSI/SteamTweaks/src/GlosSITweaks.ts

125 lines
3.7 KiB
TypeScript
Raw Normal View History

2023-01-30 11:05:33 +00:00
import type { SteamConfig } from './common/util/types';
2023-01-31 21:02:12 +00:00
import { fetchWithTimeout } from './common/util/util';
2023-02-08 22:03:03 +00:00
import type { GlosSISettings } from './@types/GlosSISettings';
2023-01-31 21:02:12 +00:00
2023-01-30 11:05:33 +00:00
class SteamTargetApi {
public static GlosSIActive = true;
public static readonly ACTIVE_FAIL_THRESHOLD = 2;
private activeFailCounter = 0;
private static ActiveCheckTimer = 0;
public constructor() {
if (SteamTargetApi.ActiveCheckTimer !== 0) {
clearInterval(SteamTargetApi.ActiveCheckTimer);
}
SteamTargetApi.ActiveCheckTimer = setInterval(() => {
void this.getGlosSIActive().then((active) => {
if (!active) {
this.activeFailCounter++;
if (this.activeFailCounter >= SteamTargetApi.ACTIVE_FAIL_THRESHOLD) {
window?.GlosSITweaks?.GlosSI?.uninstall?.();
}
}
});
}, 666);
}
public async getGlosSIActive() {
return fetchWithTimeout('http://localhost:8756/running', { timeout: 500 })
2023-02-08 22:03:03 +00:00
.then(
() => {
SteamTargetApi.GlosSIActive = true;
return true;
}
).catch(() => {
SteamTargetApi.GlosSIActive = false;
return false;
});
2023-02-08 22:03:03 +00:00
}
2023-01-30 11:05:33 +00:00
public getSteamSettings(): Promise<SteamConfig> {
return fetch('http://localhost:8756/steam_settings')
.then(
(res) => res.json().then(
(json) => (json as SteamConfig).UserLocalConfigStore as SteamConfig
)
);
}
2023-01-31 21:02:12 +00:00
2023-02-08 22:03:03 +00:00
public getGlosSISettings() {
return fetch('http://localhost:8756/settings')
2023-01-31 21:02:12 +00:00
.then(
2023-02-08 22:03:03 +00:00
(res) => res.json().then(
(json) => json as GlosSISettings
)
);
2023-01-31 21:02:12 +00:00
}
2023-02-08 22:03:03 +00:00
2023-01-30 11:05:33 +00:00
}
2023-01-31 21:02:12 +00:00
2023-01-30 11:05:33 +00:00
class GlosSIApiCtor {
public readonly SteamTarget: SteamTargetApi = new SteamTargetApi();
2023-01-30 11:05:33 +00:00
}
interface GlosSITweaks {
2023-02-08 22:03:03 +00:00
[tweakName: string]: { readonly install: () => unknown; readonly uninstall?: () => void };
2023-01-30 11:05:33 +00:00
}
declare global {
interface Window {
2023-02-08 22:03:03 +00:00
GlosSITweaks: GlosSITweaks;
2023-01-30 11:05:33 +00:00
GlosSIApi: InstanceType<typeof GlosSIApiCtor>;
}
// eslint-disable-next-line
const GlosSIApi: InstanceType<typeof GlosSIApiCtor>;
2023-02-08 22:03:03 +00:00
// eslint-disable-next-line
const GlosSITweaks: GlosSITweaks;
2023-01-30 11:05:33 +00:00
}
const installGlosSIApi = () => {
window.GlosSITweaks = {
GlosSI: {
install: () => {
const api = new GlosSIApiCtor();
Object.assign(window, { GlosSIApi: api });
},
uninstall: () => {
Object.entries(window.GlosSITweaks)
2023-02-08 22:03:03 +00:00
.filter(([tweakName]) => (tweakName !== 'GlosSI'))
.forEach(([, obj]) => obj.uninstall?.());
2023-01-30 11:05:33 +00:00
// eslint-disable-next-line @typescript-eslint/ban-ts-comment
// @ts-ignore
delete window.GlosSIApi;
// eslint-disable-next-line @typescript-eslint/ban-ts-comment
// @ts-ignore
delete window.GlosSITweaks;
2023-01-30 11:05:33 +00:00
}
}
};
window.GlosSITweaks.GlosSI.install();
2023-01-31 21:02:12 +00:00
const glossiCheckInterval = setInterval(() => {
if (window.GlosSIApi) {
2023-02-08 22:03:03 +00:00
void window.GlosSIApi.SteamTarget.getGlosSIActive().then((active) => {
2023-01-31 21:02:12 +00:00
if (!active) {
window?.GlosSITweaks?.GlosSI?.uninstall?.();
}
});
return;
}
2023-02-08 22:03:03 +00:00
clearTimeout(glossiCheckInterval);
}, 5000);
2023-01-31 21:02:12 +00:00
2023-01-30 11:05:33 +00:00
};
if (!window.GlosSITweaks || !window.GlosSIApi) {
installGlosSIApi();
}
2023-01-31 21:02:12 +00:00
export default !!window.GlosSITweaks && !!window.GlosSIApi;