From 2b758d5d22926cca517e9d47cda13e1f1d6985a9 Mon Sep 17 00:00:00 2001 From: beebls Date: Sat, 27 Aug 2022 15:17:27 -0600 Subject: [PATCH] Move gamesRunning to globalstate Now music no longer plays if you change packs while in game --- src/index.tsx | 23 ++++++++++++++--------- src/state/GlobalState.tsx | 12 ++++++++++++ 2 files changed, 26 insertions(+), 9 deletions(-) diff --git a/src/index.tsx b/src/index.tsx index c3b7d97..9608148 100755 --- a/src/index.tsx +++ b/src/index.tsx @@ -25,6 +25,7 @@ import { const Content: VFC<{ serverAPI: ServerAPI }> = ({}) => { const { activeSound, + gamesRunning, setActiveSound, soundPacks, setSoundPacks, @@ -49,7 +50,8 @@ const Content: VFC<{ serverAPI: ServerAPI }> = ({}) => { menuMusic.StopPlayback(); setMenuMusic(null); } - if (newMusic !== "None") { + // This makes sure if you are in a game, music doesn't start playing + if (newMusic !== "None" && gamesRunning.length === 0) { const currentPack = soundPacks.find((e) => e.name === newMusic); const newMenuMusic = AudioParent.GamepadUIAudio.AudioPlaybackManager.PlayAudioURLWithRepeats( @@ -246,7 +248,6 @@ export default definePlugin((serverApi: ServerAPI) => { const state: GlobalState = new GlobalState(); let menuMusic: any = null; - let gamesRunning: Number[] = []; beforePatch( AudioParent.GamepadUIAudio.m_AudioPlaybackManager.__proto__, @@ -308,19 +309,23 @@ export default definePlugin((serverApi: ServerAPI) => { // Refer to the SteamClient.d.ts or just console.log(SteamClient) to see all of it's methods SteamClient.GameSessions.RegisterForAppLifetimeNotifications( (update: AppState) => { - const { soundPacks, menuMusic, selectedMusic } = state.getPublicState(); + const { soundPacks, menuMusic, selectedMusic, gamesRunning } = + state.getPublicState(); if (selectedMusic !== "None") { if (update.bRunning) { - gamesRunning.push(update.unAppID); + // Because gamesRunning is in globalState, array methods like push and splice don't work + state.setGamesRunning([...gamesRunning, update.unAppID]); if (menuMusic != null) { menuMusic.StopPlayback(); state.setMenuMusic(null); } } else { - for (let i = gamesRunning.length; i >= 0; i--) { - if (gamesRunning[i] === update.unAppID) gamesRunning.splice(i, 1); - } - if (gamesRunning.length === 0) { + state.setGamesRunning( + gamesRunning.filter((e) => e !== update.unAppID) + ); + + // I'm re-using the filter here because I don't believe the getPublicState() method will update the values if they are changed + if (gamesRunning.filter((e) => e !== update.unAppID).length === 0) { const currentMusic = soundPacks.find( (e) => e.name === selectedMusic ); @@ -331,7 +336,7 @@ export default definePlugin((serverApi: ServerAPI) => { }/menu_music.mp3`, 999 // if someone complains this isn't infinite, just say it's a Featureā„¢ for if you go afk ); - // You need to update menuMusic in globalState after every change so that it reflects the changes the next time it checks + // Update menuMusic in globalState after every change so that it reflects the changes the next time it checks state.setMenuMusic(newMenuMusic); } } diff --git a/src/state/GlobalState.tsx b/src/state/GlobalState.tsx index 5a1962d..418b6f6 100644 --- a/src/state/GlobalState.tsx +++ b/src/state/GlobalState.tsx @@ -4,6 +4,7 @@ import { Pack, packDbEntry } from "../classes"; interface PublicGlobalState { menuMusic: any; + gamesRunning: Number[]; activeSound: string; soundPacks: Pack[]; selectedMusic: string; @@ -18,6 +19,7 @@ interface PublicGlobalState { interface PublicGlobalStateContext extends PublicGlobalState { setMenuMusic(value: any): void; + setGamesRunning(gameArr: Number[]): void; setActiveSound(value: string): void; setSoundPacks(packArr: Pack[]): void; setSelectedMusic(value: string): void; @@ -31,6 +33,7 @@ interface PublicGlobalStateContext extends PublicGlobalState { // This class creates the getter and setter functions for all of the global state data. export class GlobalState { private menuMusic: any = null; + private gamesRunning: Number[] = []; private activeSound: string = "Default"; private soundPacks: Pack[] = []; private selectedMusic: string = "None"; @@ -49,6 +52,7 @@ export class GlobalState { getPublicState() { return { menuMusic: this.menuMusic, + gamesRunning: this.gamesRunning, activeSound: this.activeSound, soundPacks: this.soundPacks, selectedMusic: this.selectedMusic, @@ -65,6 +69,11 @@ export class GlobalState { this.forceUpdate(); } + setGamesRunning(gameArr: Number[]) { + this.gamesRunning = gameArr; + this.forceUpdate(); + } + setActiveSound(value: string) { this.activeSound = value; this.forceUpdate(); @@ -147,6 +156,8 @@ export const GlobalStateContextProvider: FC = ({ }, []); const setMenuMusic = (value: any) => globalStateClass.setMenuMusic(value); + const setGamesRunning = (gameArr: Number[]) => + globalStateClass.setGamesRunning(gameArr); const setActiveSound = (value: string) => globalStateClass.setActiveSound(value); const setSoundPacks = (packArr: Pack[]) => @@ -167,6 +178,7 @@ export const GlobalStateContextProvider: FC = ({ value={{ ...publicState, setMenuMusic, + setGamesRunning, setActiveSound, setSoundPacks, setSelectedMusic,