mirror of
https://github.com/redphx/better-xcloud.git
synced 2025-08-10 23:27:46 +02:00
Migrate PatcherCache to singleton class
This commit is contained in:
@@ -1116,7 +1116,7 @@ export class Patcher {
|
||||
return nativeBind.apply(this, arguments);
|
||||
}
|
||||
|
||||
PatcherCache.init();
|
||||
PatcherCache.getInstance().init();
|
||||
|
||||
if (typeof arguments[1] === 'function') {
|
||||
BxLogger.info(LOG_TAG, 'Restored Function.prototype.bind()');
|
||||
@@ -1141,11 +1141,12 @@ export class Patcher {
|
||||
let appliedPatches: PatchArray;
|
||||
|
||||
const patchesMap: Record<string, PatchArray> = {};
|
||||
const patcherCache = PatcherCache.getInstance();
|
||||
|
||||
for (let id in item[1]) {
|
||||
appliedPatches = [];
|
||||
|
||||
const cachedPatches = PatcherCache.getPatches(id);
|
||||
const cachedPatches = patcherCache.getPatches(id);
|
||||
if (cachedPatches) {
|
||||
patchesToCheck = cachedPatches.slice(0);
|
||||
patchesToCheck.push(...PATCH_ORDERS);
|
||||
@@ -1212,7 +1213,7 @@ export class Patcher {
|
||||
}
|
||||
|
||||
if (Object.keys(patchesMap).length) {
|
||||
PatcherCache.saveToCache(patchesMap);
|
||||
patcherCache.saveToCache(patchesMap);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1222,17 +1223,20 @@ export class Patcher {
|
||||
}
|
||||
|
||||
export class PatcherCache {
|
||||
private static readonly KEY_CACHE = 'better_xcloud_patches_cache';
|
||||
private static readonly KEY_SIGNATURE = 'better_xcloud_patches_cache_signature';
|
||||
private static instance: PatcherCache;
|
||||
public static getInstance = () => PatcherCache.instance ?? (PatcherCache.instance = new PatcherCache());
|
||||
|
||||
private static CACHE: any;
|
||||
private readonly KEY_CACHE = 'better_xcloud_patches_cache';
|
||||
private readonly KEY_SIGNATURE = 'better_xcloud_patches_cache_signature';
|
||||
|
||||
private static isInitialized = false;
|
||||
private CACHE: any;
|
||||
|
||||
private isInitialized = false;
|
||||
|
||||
/**
|
||||
* Get patch's signature
|
||||
*/
|
||||
private static getSignature(): number {
|
||||
private getSignature(): number {
|
||||
const scriptVersion = SCRIPT_VERSION;
|
||||
const patches = JSON.stringify(ALL_PATCHES);
|
||||
|
||||
@@ -1253,31 +1257,31 @@ export class PatcherCache {
|
||||
return sig;
|
||||
}
|
||||
|
||||
static clear() {
|
||||
clear() {
|
||||
// Clear cache
|
||||
window.localStorage.removeItem(PatcherCache.KEY_CACHE);
|
||||
PatcherCache.CACHE = {};
|
||||
window.localStorage.removeItem(this.KEY_CACHE);
|
||||
this.CACHE = {};
|
||||
}
|
||||
|
||||
static checkSignature() {
|
||||
const storedSig = window.localStorage.getItem(PatcherCache.KEY_SIGNATURE) || 0;
|
||||
const currentSig = PatcherCache.getSignature();
|
||||
private checkSignature() {
|
||||
const storedSig = window.localStorage.getItem(this.KEY_SIGNATURE) || 0;
|
||||
const currentSig = this.getSignature();
|
||||
|
||||
if (currentSig !== parseInt(storedSig as string)) {
|
||||
// Save new signature
|
||||
BxLogger.warning(LOG_TAG, 'Signature changed');
|
||||
window.localStorage.setItem(PatcherCache.KEY_SIGNATURE, currentSig.toString());
|
||||
window.localStorage.setItem(this.KEY_SIGNATURE, currentSig.toString());
|
||||
|
||||
PatcherCache.clear();
|
||||
this.clear();
|
||||
} else {
|
||||
BxLogger.info(LOG_TAG, 'Signature unchanged');
|
||||
}
|
||||
}
|
||||
|
||||
private static cleanupPatches(patches: PatchArray): PatchArray {
|
||||
private cleanupPatches(patches: PatchArray): PatchArray {
|
||||
return patches.filter(item => {
|
||||
for (const id in PatcherCache.CACHE) {
|
||||
const cached = PatcherCache.CACHE[id];
|
||||
for (const id in this.CACHE) {
|
||||
const cached = this.CACHE[id];
|
||||
|
||||
if (cached.includes(item)) {
|
||||
return false;
|
||||
@@ -1288,17 +1292,17 @@ export class PatcherCache {
|
||||
});
|
||||
}
|
||||
|
||||
static getPatches(id: string): PatchArray {
|
||||
return PatcherCache.CACHE[id];
|
||||
getPatches(id: string): PatchArray {
|
||||
return this.CACHE[id];
|
||||
}
|
||||
|
||||
static saveToCache(subCache: Record<string, PatchArray>) {
|
||||
saveToCache(subCache: Record<string, PatchArray>) {
|
||||
for (const id in subCache) {
|
||||
const patchNames = subCache[id];
|
||||
|
||||
let data = PatcherCache.CACHE[id];
|
||||
let data = this.CACHE[id];
|
||||
if (!data) {
|
||||
PatcherCache.CACHE[id] = patchNames;
|
||||
this.CACHE[id] = patchNames;
|
||||
} else {
|
||||
for (const patchName of patchNames) {
|
||||
if (!data.includes(patchName)) {
|
||||
@@ -1309,20 +1313,20 @@ export class PatcherCache {
|
||||
}
|
||||
|
||||
// Save to storage
|
||||
window.localStorage.setItem(PatcherCache.KEY_CACHE, JSON.stringify(PatcherCache.CACHE));
|
||||
window.localStorage.setItem(this.KEY_CACHE, JSON.stringify(this.CACHE));
|
||||
}
|
||||
|
||||
static init() {
|
||||
if (PatcherCache.isInitialized) {
|
||||
init() {
|
||||
if (this.isInitialized) {
|
||||
return;
|
||||
}
|
||||
PatcherCache.isInitialized = true;
|
||||
this.isInitialized = true;
|
||||
|
||||
PatcherCache.checkSignature();
|
||||
this.checkSignature();
|
||||
|
||||
// Read cache from storage
|
||||
PatcherCache.CACHE = JSON.parse(window.localStorage.getItem(PatcherCache.KEY_CACHE) || '{}');
|
||||
BxLogger.info(LOG_TAG, PatcherCache.CACHE);
|
||||
this.CACHE = JSON.parse(window.localStorage.getItem(this.KEY_CACHE) || '{}');
|
||||
BxLogger.info(LOG_TAG, this.CACHE);
|
||||
|
||||
if (window.location.pathname.includes('/play/')) {
|
||||
PATCH_ORDERS.push(...PLAYING_PATCH_ORDERS);
|
||||
@@ -1331,8 +1335,8 @@ export class PatcherCache {
|
||||
}
|
||||
|
||||
// Remove cached patches from PATCH_ORDERS & PLAYING_PATCH_ORDERS
|
||||
PATCH_ORDERS = PatcherCache.cleanupPatches(PATCH_ORDERS);
|
||||
PLAYING_PATCH_ORDERS = PatcherCache.cleanupPatches(PLAYING_PATCH_ORDERS);
|
||||
PATCH_ORDERS = this.cleanupPatches(PATCH_ORDERS);
|
||||
PLAYING_PATCH_ORDERS = this.cleanupPatches(PLAYING_PATCH_ORDERS);
|
||||
|
||||
BxLogger.info(LOG_TAG, PATCH_ORDERS.slice(0));
|
||||
BxLogger.info(LOG_TAG, PLAYING_PATCH_ORDERS.slice(0));
|
||||
|
@@ -1034,7 +1034,7 @@ export class SettingsNavigationDialog extends NavigationDialog {
|
||||
|
||||
private onGlobalSettingChanged(e: Event) {
|
||||
// Clear PatcherCache;
|
||||
isFullVersion() && PatcherCache.clear();
|
||||
isFullVersion() && PatcherCache.getInstance().clear();
|
||||
|
||||
this.$btnReload.classList.add('bx-danger');
|
||||
|
||||
|
Reference in New Issue
Block a user