Skip to content

Commit

Permalink
Move gamesRunning to globalstate
Browse files Browse the repository at this point in the history
Now music no longer plays if you change packs while in game
  • Loading branch information
beebls committed Aug 27, 2022
1 parent d77f8bc commit 2b758d5
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 9 deletions.
23 changes: 14 additions & 9 deletions src/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ import {
const Content: VFC<{ serverAPI: ServerAPI }> = ({}) => {
const {
activeSound,
gamesRunning,
setActiveSound,
soundPacks,
setSoundPacks,
Expand All @@ -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(
Expand Down Expand Up @@ -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__,
Expand Down Expand Up @@ -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
);
Expand All @@ -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);
}
}
Expand Down
12 changes: 12 additions & 0 deletions src/state/GlobalState.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import { Pack, packDbEntry } from "../classes";

interface PublicGlobalState {
menuMusic: any;
gamesRunning: Number[];
activeSound: string;
soundPacks: Pack[];
selectedMusic: string;
Expand All @@ -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;
Expand All @@ -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";
Expand All @@ -49,6 +52,7 @@ export class GlobalState {
getPublicState() {
return {
menuMusic: this.menuMusic,
gamesRunning: this.gamesRunning,
activeSound: this.activeSound,
soundPacks: this.soundPacks,
selectedMusic: this.selectedMusic,
Expand All @@ -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();
Expand Down Expand Up @@ -147,6 +156,8 @@ export const GlobalStateContextProvider: FC<ProviderProps> = ({
}, []);

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[]) =>
Expand All @@ -167,6 +178,7 @@ export const GlobalStateContextProvider: FC<ProviderProps> = ({
value={{
...publicState,
setMenuMusic,
setGamesRunning,
setActiveSound,
setSoundPacks,
setSelectedMusic,
Expand Down

0 comments on commit 2b758d5

Please sign in to comment.