Automatically reset game setting's value if it has the same value as global's

This commit is contained in:
redphx 2025-01-30 16:10:51 +07:00
parent 96de61c301
commit fe418e6918
8 changed files with 83 additions and 53 deletions

File diff suppressed because one or more lines are too long

File diff suppressed because one or more lines are too long

View File

@ -155,7 +155,6 @@
display: flex;
gap: 10px;
padding: 16px 10px;
margin: 0;
background: #2a2a2a;
border-bottom: 1px solid #343434;
@ -305,6 +304,7 @@
border-left: 4px solid orange !important;
border-top-left-radius: 0 !important;
border-bottom-left-radius: 0 !important;
padding-left: 6px !important;
}
}
}

View File

@ -198,7 +198,6 @@ export class SettingsManager {
}
const value = getGamePref(this.targetGameId, key, true)!;
if ('setValue' in $elm) {
($elm as any).setValue(value);
} else {
@ -227,8 +226,8 @@ export class SettingsManager {
continue;
}
const oldValue = getGamePref(oldGameId, key, true, true);
const newValue = getGamePref(this.targetGameId, key, true, true);
const oldValue = getGamePref(oldGameId, key, true);
const newValue = getGamePref(this.targetGameId, key, true);
if (oldValue === newValue) {
continue;
@ -314,8 +313,7 @@ export class SettingsManager {
// Only switch to game settings if it's not empty
const gameSettings = STORAGE.Stream.getGameSettings(id);
const customSettings = gameSettings && !gameSettings.isEmpty();
const selectedId = customSettings ? id : -1;
const selectedId = (gameSettings && !gameSettings.isEmpty()) ? id : -1;
setGameIdPref(selectedId);

View File

@ -1310,8 +1310,7 @@ export class SettingsDialog extends NavigationDialog {
}
// Delete settings
const gameSettings = STORAGE.Stream.getGameSettings(targetGameId);
const deleted = gameSettings?.deleteSetting(pref);
const deleted = STORAGE.Stream.deleteSettingByGame(targetGameId, pref);
if (deleted) {
BxEventBus.Stream.emit('setting.changed', {
storageKey: `${StorageKey.STREAM}.${targetGameId}`,

View File

@ -63,7 +63,7 @@ export class BaseSettingsStorage<T extends AnyPref> {
return this.definitions[key];
}
hasSetting<K extends keyof PrefTypeMap<K>>(key: K): boolean {
hasSetting(key: T): boolean {
return key in this.settings;
}
@ -196,4 +196,15 @@ export class BaseSettingsStorage<T extends AnyPref> {
return value.toString();
}
deleteSetting(pref: T) {
if (this.hasSetting(pref)) {
delete this.settings[pref];
this.saveSettings();
return true;
}
return false;
}
}

View File

@ -10,15 +10,4 @@ export class GameSettingsStorage extends BaseSettingsStorage<StreamPref> {
isEmpty() {
return Object.keys(this.settings).length === 0;
}
deleteSetting(pref: StreamPref) {
if (this.hasSetting(pref)) {
delete this.settings[pref];
this.saveSettings();
return true;
}
return false;
}
}

View File

@ -398,7 +398,13 @@ export class StreamSettingsStorage extends BaseSettingsStorage<StreamPref> {
getGameSettings(id: number) {
if (id > -1) {
if (!this.gameSettings[id]) {
this.gameSettings[id] = new GameSettingsStorage(id);
const gameStorage = new GameSettingsStorage(id);
this.gameSettings[id] = gameStorage;
// Remove values same as global's
for (const key in gameStorage.settings) {
this.getSettingByGame(id, key);
}
}
return this.gameSettings[id];
@ -408,22 +414,27 @@ export class StreamSettingsStorage extends BaseSettingsStorage<StreamPref> {
}
getSetting<K extends keyof PrefTypeMap<K>>(key: K, checkUnsupported?: boolean): PrefTypeMap<K>[K] {
return this.getSettingByGame(this.xboxTitleId, key, true, checkUnsupported)!;
return this.getSettingByGame(this.xboxTitleId, key, checkUnsupported)!;
}
getSettingByGame<K extends keyof PrefTypeMap<K>>(id: number, key: K, returnBaseValue: boolean=true, checkUnsupported?: boolean): PrefTypeMap<K>[K] | undefined {
getSettingByGame<K extends keyof PrefTypeMap<K>>(id: number, key: K, checkUnsupported?: boolean): PrefTypeMap<K>[K] | undefined {
const gameSettings = this.getGameSettings(id);
if (gameSettings?.hasSetting(key)) {
return gameSettings.getSetting(key, checkUnsupported);
if (gameSettings?.hasSetting(key as StreamPref)) {
let gameValue = gameSettings.getSetting(key, checkUnsupported);
const globalValue = super.getSetting(key, checkUnsupported);
// Remove value if it's the same as global's
if (globalValue === gameValue) {
this.deleteSettingByGame(id, key as StreamPref);
gameValue = globalValue;
}
return gameValue;
}
if (returnBaseValue) {
return super.getSetting(key, checkUnsupported);
}
return undefined;
}
setSetting<V = any>(key: StreamPref, value: V, origin: SettingActionOrigin): V {
return this.setSettingByGame(this.xboxTitleId, key, value, origin);
}
@ -439,6 +450,15 @@ export class StreamSettingsStorage extends BaseSettingsStorage<StreamPref> {
return super.setSetting(key, value, origin);
}
deleteSettingByGame(id: number, key: StreamPref): boolean {
const gameSettings = this.getGameSettings(id);
if (gameSettings) {
return gameSettings.deleteSetting(key);
}
return false;
}
hasGameSetting(id: number, key: StreamPref): boolean {
const gameSettings = this.getGameSettings(id);
return !!(gameSettings && gameSettings.hasSetting(key));