Skip to content

Commit

Permalink
[game] Add manual refresh of song db
Browse files Browse the repository at this point in the history
  • Loading branch information
Drewol committed Oct 20, 2024
1 parent 73320fa commit a4ae75e
Show file tree
Hide file tree
Showing 13 changed files with 120 additions and 46 deletions.
3 changes: 2 additions & 1 deletion Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

7 changes: 6 additions & 1 deletion game/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -99,7 +99,12 @@ chrono = { version = "0.4.38", default-features = false, features = [
"now",
"std",
] }
log4rs = { version = "1.3.0", features = ["console_appender", "file_appender", "pattern_encoder", "console_writer"], default-features = false }
log4rs = { version = "1.3.0", features = [
"console_appender",
"file_appender",
"pattern_encoder",
"console_writer",
], default-features = false }

[dependencies.winit]
version = "0.29"
Expand Down
8 changes: 6 additions & 2 deletions game/src/button_codes.rs
Original file line number Diff line number Diff line change
Expand Up @@ -170,6 +170,7 @@ pub enum UscButton {
FX(Side),
Start,
Back,
Refresh,
/// (Side, Direction)
Laser(Side, Side),
Other(Button),
Expand All @@ -189,7 +190,8 @@ impl UscButton {
Side::Right => 6,
},
UscButton::Start => 0,
UscButton::Back => 255,
UscButton::Back | UscButton::Refresh => 255,

UscButton::Laser(_, _) => 255,
UscButton::Other(_) => 255,
}
Expand All @@ -209,6 +211,7 @@ impl UscButton {
},
UscButton::Start => "Start",
UscButton::Back => "Back",
UscButton::Refresh => "Refresh",
UscButton::Laser(side, dir) => match (side, dir) {
(Side::Left, Side::Left) => "<--Left Laser",
(Side::Left, Side::Right) => "Left Laser-->",
Expand Down Expand Up @@ -251,6 +254,7 @@ impl From<UscButton> for u8 {
},
UscButton::Start => 6,
UscButton::Back => 7,
UscButton::Refresh => 8,
UscButton::Other(_) => 255,
UscButton::Laser(_, _) => 255,
}
Expand Down Expand Up @@ -295,7 +299,7 @@ impl From<UscButton> for Button {
},
UscButton::Start => Button::Start,
UscButton::Back => Button::Select,
UscButton::Laser(_, _) => Button::Unknown,
UscButton::Laser(_, _) | UscButton::Refresh => Button::Unknown,
UscButton::Other(c) => c,
}
}
Expand Down
7 changes: 6 additions & 1 deletion game/src/config.rs
Original file line number Diff line number Diff line change
Expand Up @@ -166,6 +166,7 @@ pub struct SongSelectSettings {
}

#[derive(Debug, Serialize, Deserialize, Clone, Copy)]
#[serde(default)]
pub struct Keybinds {
bt_a: PhysicalKey,
bt_b: PhysicalKey,
Expand All @@ -175,6 +176,7 @@ pub struct Keybinds {
fx_r: PhysicalKey,
start: PhysicalKey,
back: PhysicalKey,
refresh: PhysicalKey,
laser_l: (PhysicalKey, PhysicalKey),
laser_r: (PhysicalKey, PhysicalKey),
}
Expand All @@ -190,6 +192,7 @@ impl Keybinds {
fx_r,
start,
back,
refresh,
laser_l: (ll_l, ll_r),
laser_r: (rl_l, rl_r),
} = self;
Expand All @@ -204,6 +207,7 @@ impl Keybinds {
k if k == fx_r => Some(UscButton::FX(kson::Side::Right)),
k if k == start => Some(UscButton::Start),
k if k == back => Some(UscButton::Back),
k if k == refresh => Some(UscButton::Refresh),
k if k == ll_l => Some(UscButton::Laser(kson::Side::Left, kson::Side::Left)),
k if k == ll_r => Some(UscButton::Laser(kson::Side::Left, kson::Side::Right)),
k if k == rl_l => Some(UscButton::Laser(kson::Side::Right, kson::Side::Left)),
Expand All @@ -217,14 +221,15 @@ impl Default for Keybinds {
fn default() -> Self {
use winit::keyboard::KeyCode;
Self {
bt_a: PhysicalKey::Code(KeyCode::KeyD),
bt_a: PhysicalKey::Code(KeyCode::KeyD), // D
bt_b: PhysicalKey::Code(KeyCode::KeyF), // F
bt_c: PhysicalKey::Code(KeyCode::KeyJ), // J
bt_d: PhysicalKey::Code(KeyCode::KeyK), // K
fx_l: PhysicalKey::Code(KeyCode::KeyC), // C
fx_r: PhysicalKey::Code(KeyCode::KeyM), // M
start: PhysicalKey::Code(KeyCode::Digit1), // 1
back: PhysicalKey::Code(KeyCode::Escape), // Esc
refresh: PhysicalKey::Code(KeyCode::F5), // F5
laser_l: (
PhysicalKey::Code(KeyCode::KeyW),
PhysicalKey::Code(KeyCode::KeyE),
Expand Down
6 changes: 3 additions & 3 deletions game/src/game_main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -188,11 +188,13 @@ impl GameMain {

{
for ele in self.service_provider.get_all_mut::<dyn WorkerService>() {
profile_scope!("Worker update");
ele.write().expect("Worker service closed").update()
}
}

if self.companion_update == 0 {
profile_scope!("Companion update");
let server = self.companion_server.read().unwrap();

if server.active.load(std::sync::atomic::Ordering::Relaxed) {
Expand Down Expand Up @@ -276,8 +278,6 @@ impl GameMain {
} = self;

knob_state.zero_deltas();
puffin::profile_scope!("Frame");
puffin::GlobalProfiler::lock().new_frame();

for lua in lua_arena.read().expect("Lock error").0.iter() {
lua.set_app_data(frame_input.clone());
Expand Down Expand Up @@ -626,7 +626,7 @@ impl GameMain {
},
..
} => {
if !text_input_active && GameConfig::get().keyboard_buttons {
if !text_input_active {
for button in GameConfig::get()
.keybinds
.iter()
Expand Down
1 change: 1 addition & 0 deletions game/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -231,6 +231,7 @@ impl Scenes {
knob_state: crate::button_codes::LaserState,
app_control_tx: std::sync::mpsc::Sender<ControlMessage>,
) {
profile_function!();
let new_transition = self.transition.is_some();
if self.should_outro {
if let Some(tr) = self.transition.as_mut() {
Expand Down
1 change: 1 addition & 0 deletions game/src/settings_dialog.rs
Original file line number Diff line number Diff line change
Expand Up @@ -313,6 +313,7 @@ impl SettingsDialog {
UscButton::Back => self.show = false,
UscButton::Laser(_, _) => {}
UscButton::Other(_) => {}
UscButton::Refresh => {}
}

_ = self.lua.globals().set("SettingsDiag", &*self);
Expand Down
96 changes: 64 additions & 32 deletions game/src/song_provider/files.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ use crate::{
block_on,
config::{GameConfig, SongSelectSettings},
game::{gauge::Gauge, HitSummary, HitWindow},
log_result,
results::{calculate_clear_mark, Score},
song_provider::SongFilterType,
songselect::{Difficulty, Song},
Expand Down Expand Up @@ -47,6 +48,16 @@ enum ImporterState {
Loading(String),
}

impl std::fmt::Display for ImporterState {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
match self {
ImporterState::Idle => f.write_str(""),
ImporterState::Starting => f.write_str("Importer Starting"),
ImporterState::Loading(m) => f.write_fmt(format_args!("Importer: {m}")),
}
}
}

enum WorkerEvent {
SongProvider(SongProviderEvent),
ImporterState(ImporterState),
Expand Down Expand Up @@ -149,8 +160,8 @@ impl FileSongProvider {
database,
worker,
worker_rx,
score_bus: bus::Bus::new(32),
song_bus: bus::Bus::new(32),
score_bus: bus::Bus::new(128),
song_bus: bus::Bus::new(128),
worker_tx,
sort: *sorting,
filter: filter.clone(),
Expand Down Expand Up @@ -271,7 +282,7 @@ async fn read_song_dir(
while let Some(e) = dir.next_entry().await? {
let p = e.path();
if p.is_dir() {
let msg = format!("Reading {}", p.display());
let msg = format!("{}", p.display());
worker_tx.send(WorkerEvent::ImporterState(ImporterState::Loading(msg)));
let dir = tokio::fs::read_dir(p).await?;
Box::pin(read_song_dir(dir, worker_tx, worker_db)).await;
Expand Down Expand Up @@ -443,41 +454,56 @@ impl WorkerService for FileSongProvider {
.ready()
.is_some()
.then(|| panic!("Song file provider worker returned")); //panics if worker paniced
let ev = self.worker_rx.try_recv().ok();

match ev {
Some(WorkerEvent::ImporterState(s)) => {
if self.last_full_update.elapsed().unwrap().as_secs() > 2 {
self.worker_tx.send(WorkerControlMessage::LoadDb);
self.last_full_update = SystemTime::now();
let mut importer_dirty = false;
while let Some(ev) = self.worker_rx.try_recv().ok() {
match ev {
WorkerEvent::ImporterState(s) => {
if self.last_full_update.elapsed().unwrap().as_secs() > 2 {
self.worker_tx.send(WorkerControlMessage::LoadDb);
self.last_full_update = SystemTime::now();
}
self.importer_state = s;
importer_dirty = true;
}
self.importer_state = s;
}
Some(WorkerEvent::SongProvider(mut ev)) => {
match &mut ev {
SongProviderEvent::SongsAdded(s) => {
//TODO: Consider full update event
let mut new_songs = vec![];
for s in s.iter() {
if self.all_songs.insert(s.id.clone(), s.clone()).is_none() {
new_songs.push(s.clone());
WorkerEvent::SongProvider(mut ev) => {
match &mut ev {
SongProviderEvent::SongsAdded(s) => {
//TODO: Consider full update event
let mut new_songs = vec![];
for s in s.iter() {
if self.all_songs.insert(s.id.clone(), s.clone()).is_none() {
new_songs.push(s.clone());
}
}
}

*s = new_songs
*s = new_songs
}
SongProviderEvent::SongsRemoved(r) => {
self.all_songs.retain(|k, _| !r.contains(k))
}
SongProviderEvent::OrderChanged(_) => {}
SongProviderEvent::StatusUpdate(_) => {}
}
SongProviderEvent::SongsRemoved(r) => {
self.all_songs.retain(|k, _| !r.contains(k))
match &ev {
SongProviderEvent::OrderChanged(_) => {}
_ => self.set_sort(self.sort),
}
SongProviderEvent::OrderChanged(_) => {}
log_result!(self
.song_bus
.try_broadcast(ev)
.map_err(|_| "Song event bus full"));
}
match &ev {
SongProviderEvent::OrderChanged(_) => {}
_ => self.set_sort(self.sort),
}
self.song_bus.broadcast(ev);
_ => (),
}
_ => (),
}

if importer_dirty {
log_result!(self
.song_bus
.try_broadcast(SongProviderEvent::StatusUpdate(
self.importer_state.to_string(),
))
.map_err(|_| "Song event bus full"));
}
}
}
Expand Down Expand Up @@ -571,7 +597,6 @@ impl SongProvider for FileSongProvider {
bail!("No chart found")
};

info!("Got chart: {:?}", &chart.preview_file);
let Some(path) = chart.preview_file.take() else {
bail!("No preview file")
};
Expand Down Expand Up @@ -707,6 +732,13 @@ impl SongProvider for FileSongProvider {
);
res
}

fn refresh(&mut self) {
if let ImporterState::Idle = self.importer_state {
self.importer_state = ImporterState::Starting;
self.worker_tx.send(WorkerControlMessage::Refresh);
}
}
}

impl ScoreProvider for FileSongProvider {
Expand Down
2 changes: 2 additions & 0 deletions game/src/song_provider/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@ pub enum SongProviderEvent {
SongsAdded(Vec<Arc<Song>>),
SongsRemoved(HashSet<SongId>),
OrderChanged(Vec<SongId>),
StatusUpdate(String),
}

#[derive(Debug, Clone)]
Expand Down Expand Up @@ -326,6 +327,7 @@ pub trait SongProvider: Send {
/// Returns: `(music, skip, duration)`
fn get_preview(&self, id: &SongId) -> Promise<PreviewResult>;
fn get_all(&self) -> (Vec<Arc<Song>>, Vec<SongId>);
fn refresh(&mut self) {}
}

pub trait ScoreProvider {
Expand Down
4 changes: 4 additions & 0 deletions game/src/song_provider/nautica.rs
Original file line number Diff line number Diff line change
Expand Up @@ -560,6 +560,10 @@ impl SongProvider for NauticaSongProvider {
fn get_available_sorts(&self) -> Vec<super::SongSort> {
vec![]
}

fn refresh(&mut self) {
self.query_changed();
}
}

fn download_song(id: Uuid, diff: u8, on_loaded: Sender<Datum>) -> anyhow::Result<LoadSongFn> {
Expand Down
Loading

0 comments on commit a4ae75e

Please sign in to comment.