From 8dd4729965b82cec8b29a118960a65d62b0ae9c4 Mon Sep 17 00:00:00 2001 From: Christof Petig Date: Thu, 14 Mar 2024 00:10:39 +0100 Subject: [PATCH] make missing plugins a set --- client/src/lib.rs | 19 ++++++++++--------- common/state/src/plugin/mod.rs | 13 +++++++------ voxygen/src/menu/char_selection/mod.rs | 14 ++++++++------ voxygen/src/menu/main/mod.rs | 12 +++++++----- 4 files changed, 32 insertions(+), 26 deletions(-) diff --git a/client/src/lib.rs b/client/src/lib.rs index 18ee331920..a5ab702d33 100644 --- a/client/src/lib.rs +++ b/client/src/lib.rs @@ -331,7 +331,7 @@ pub struct Client { connected_server_constants: ServerConstants, /// Requested but not yet received plugins - missing_plugins: u32, + missing_plugins: HashSet, /// Locally cached plugins needed by the server local_plugins: Vec, } @@ -943,7 +943,7 @@ impl Client { _ = ping_interval.tick() => ping_stream.send(PingMsg::Ping)?, } }; - let missing_plugins_num = missing_plugins.len(); + let missing_plugins_set = missing_plugins.iter().collect(); if !missing_plugins.is_empty() { stream.send(ClientGeneral::RequestPlugins(missing_plugins))?; } @@ -1027,7 +1027,7 @@ impl Client { dt_adjustment: 1.0, connected_server_constants: server_constants, - missing_plugins: missing_plugins_num as u32, + missing_plugins: missing_plugins_set, local_plugins, }) } @@ -2580,7 +2580,8 @@ impl Client { frontend_events.push(Event::Notification(n)); }, ServerGeneral::PluginData(d) => { - tracing::info!("plugin data #1 {}", d.len()); + let plugin_len = d.len(); + tracing::info!(?plugin_len, "plugin data"); frontend_events.push(Event::PluginDataReceived(d)); }, _ => unreachable!("Not a general msg"), @@ -3228,15 +3229,15 @@ impl Client { } /// another plugin data received, is this the last one - pub fn decrement_missing_plugins(&mut self) -> u32 { - if self.missing_plugins > 0 { - self.missing_plugins -= 1; + pub fn plugin_received(&mut self, hash: PluginHash) -> usize { + if !self.missing_plugins.remove(&hash) { + tracing::warn!(?hash, "received unrequested plugin"); } - self.missing_plugins + self.missing_plugins.len() } /// number of requested plugins - pub fn missing_plugins(&self) -> u32 { self.missing_plugins } + pub fn num_missing_plugins(&self) -> usize { self.missing_plugins.len() } /// extract list of locally cached plugins to load pub fn take_local_plugins(&mut self) -> Vec { std::mem::take(&mut self.local_plugins) } diff --git a/common/state/src/plugin/mod.rs b/common/state/src/plugin/mod.rs index 1fa71a8de1..5ff2c32017 100644 --- a/common/state/src/plugin/mod.rs +++ b/common/state/src/plugin/mod.rs @@ -227,23 +227,24 @@ impl PluginMgr { } /// Add a plugin received from the server - pub fn load_server_plugin(&mut self, path: PathBuf) { - let _ = Plugin::from_path(path.clone()).map(|plugin| { + pub fn load_server_plugin(&mut self, path: PathBuf) -> Result { + Plugin::from_path(path.clone()).map(|plugin| { if let Err(e) = common::assets::register_tar(path.clone()) { error!("Plugin {:?} tar error {e:?}", path.as_path()); } + let hash = plugin.hash.clone(); self.plugins.push(plugin); - }); + hash + }) } pub fn cache_server_plugin( &mut self, base_dir: &Path, data: Vec, - ) -> Result<(), std::io::Error> { + ) -> Result { let path = store_server_plugin(base_dir, data)?; - self.load_server_plugin(path); - Ok(()) + self.load_server_plugin(path) } /// list all registered plugins diff --git a/voxygen/src/menu/char_selection/mod.rs b/voxygen/src/menu/char_selection/mod.rs index 3b2421139e..7641ad3fa8 100644 --- a/voxygen/src/menu/char_selection/mod.rs +++ b/voxygen/src/menu/char_selection/mod.rs @@ -71,7 +71,7 @@ impl CharSelectionState { impl PlayState for CharSelectionState { fn enter(&mut self, global_state: &mut GlobalState, _: Direction) { // Load the player's character list - if self.client.borrow().missing_plugins() == 0 { + if self.client.borrow().num_missing_plugins() == 0 { self.client.borrow_mut().load_character_list(); } @@ -282,14 +282,16 @@ impl PlayState for CharSelectionState { tracing::info!("plugin data {}", data.len()); let mut client = self.client.borrow_mut(); #[cfg(feature = "plugins")] - let _ = client + if let Ok(hash) = client .state() .ecs() .write_resource::() - .cache_server_plugin(&global_state.config_dir, data); - if client.decrement_missing_plugins() == 0 { - // now load characters (plugins might contain items) - client.load_character_list(); + .cache_server_plugin(&global_state.config_dir, data) + { + if client.plugin_received(hash) == 0 { + // now load characters (plugins might contain items) + client.load_character_list(); + } } }, // TODO: See if we should handle StartSpectate here instead. diff --git a/voxygen/src/menu/main/mod.rs b/voxygen/src/menu/main/mod.rs index 6fa7da0f6b..3c27443b30 100644 --- a/voxygen/src/menu/main/mod.rs +++ b/voxygen/src/menu/main/mod.rs @@ -284,14 +284,16 @@ impl PlayState for MainMenuState { tracing::info!("plugin data {}", data.len()); if let InitState::Pipeline(client) = &mut self.init { #[cfg(feature = "plugins")] - let _ = client + if let Ok(hash) = client .state() .ecs() .write_resource::() - .cache_server_plugin(&global_state.config_dir, data); - if client.decrement_missing_plugins() == 0 { - // now load characters (plugins might contain items) - client.load_character_list(); + .cache_server_plugin(&global_state.config_dir, data) + { + if client.plugin_received(hash) == 0 { + // now load characters (plugins might contain items) + client.load_character_list(); + } } } },