Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Retain audio settings between game and application exits #360

Closed
wants to merge 3 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions src/instance.rs
Original file line number Diff line number Diff line change
Expand Up @@ -384,11 +384,11 @@ pub async fn perform_update(instance: Instance) {
}

pub async fn perform_play(path: PathBuf, executable: PathBuf, name: String, do_debug: bool) {
send_message(Message::MusicMessage(MusicCommand::Pause));
send_message(Message::MusicMessage(MusicCommand::WeakPause));
if let Err(e) = play(path, executable, name, do_debug).await {
error!("Failed to run game: {:#}", e);
}
send_message(Message::MusicMessage(MusicCommand::Play));
send_message(Message::MusicMessage(MusicCommand::WeakPlay));
}

pub async fn play(path: PathBuf, executable: PathBuf, name: String, do_debug: bool) -> Result<()> {
Expand Down
20 changes: 17 additions & 3 deletions src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ use std::sync::Mutex;

use crate::install_frame::InstallFrameMessage;
use crate::instance::{Instance, InstanceMessage, InstanceState, Progress};
use crate::music::{MusicCommand, MusicState};
use crate::music::{MusicCommand, MusicState, save_music_state, load_music_state};
use crate::plugins_frame::PluginMessage;

mod archive;
Expand Down Expand Up @@ -119,14 +119,23 @@ impl Application for ESLauncher {

let music_sender = music::spawn();

let music_saved_state = if load_music_state() {MusicState::Playing} else {MusicState::Paused};

if match music_saved_state {
MusicState::Playing => music_sender.send(MusicCommand::Play).is_err(),
MusicState::Paused => music_sender.send(MusicCommand::Pause).is_err()
} {
error!("Failed to default music.");
}

check_for_update();

let (plugins_frame_state, command) = plugins_frame::PluginsFrameState::new();
(
Self {
music_sender,
music_button: button::State::default(),
music_state: MusicState::Playing,
music_state: music_saved_state,
install_frame: install_frame::InstallFrame::default(),
instances_frame: instances_frame::InstancesFrame::default(),
plugins_frame: plugins_frame_state,
Expand Down Expand Up @@ -184,7 +193,12 @@ impl Application for ESLauncher {
self.music_state = match cmd {
MusicCommand::Pause => MusicState::Paused,
MusicCommand::Play => MusicState::Playing,
}
MusicCommand::WeakPause => self.music_state,
MusicCommand::WeakPlay => self.music_state,
};
if let Err(e) = save_music_state(self.music_state == MusicState::Playing) {
error!("Failed to save music state: {:#}", e);
};
}
Message::ViewChanged(view) => self.view = view,
Message::PluginFrameLoaded(plugins) => {
Expand Down
46 changes: 46 additions & 0 deletions src/music.rs
Original file line number Diff line number Diff line change
@@ -1,17 +1,22 @@
use anyhow::{Context, Result};
use rodio::Sink;
use std::fs::File;
use std::io::{BufReader, Cursor};
use std::sync::mpsc;
use std::sync::mpsc::{Receiver, Sender};
use std::thread;
use std::time::Duration;

use crate::get_data_dir;

const SONG: &[u8] = include_bytes!("../assets/endless-prototype.ogg");

#[derive(Clone, Copy, Debug)]
pub enum MusicCommand {
Pause,
Play,
WeakPause,
WeakPlay,
}

#[derive(Clone, Copy, Debug, PartialEq, Eq)]
Expand Down Expand Up @@ -48,6 +53,17 @@ fn play(rx: &Receiver<MusicCommand>) -> Result<()> {
state = MusicState::Playing;
sink.play()
}
MusicCommand::WeakPause => {
sink.pause()
}
MusicCommand::WeakPlay => {
match state {
MusicState::Playing => {
sink.play()
}
MusicState::Paused => {}
}
}
}
}

Expand All @@ -59,3 +75,33 @@ fn play(rx: &Receiver<MusicCommand>) -> Result<()> {
thread::sleep(Duration::from_millis(100));
}
}

pub fn save_music_state(state: bool) -> Result<()> {
let mut app_save_file =
get_data_dir().ok_or_else(|| anyhow!("Failed to get app save dir"))?;
app_save_file.push("application_save.json");

let file = File::create(app_save_file)?;

serde_json::to_writer_pretty(
file,
&state,
)?;
Ok(())
}

pub fn load_music_state() -> bool {
let mut app_save_file =
get_data_dir().ok_or_else(|| anyhow!("Failed to get app save dir")).unwrap();
app_save_file.push("application_save.json");

if app_save_file.exists() {
let file = File::open(app_save_file).unwrap();

let loaded: bool = serde_json::from_reader(file).unwrap_or(true);
loaded
} else {
warn!("instances.json doesn't exist (yet?), commencing without loading Instances");
true
}
}