make missing plugins a set

This commit is contained in:
Christof Petig 2024-03-14 00:10:39 +01:00
parent eccdc18eb7
commit 8dd4729965
4 changed files with 32 additions and 26 deletions

View File

@ -331,7 +331,7 @@ pub struct Client {
connected_server_constants: ServerConstants,
/// Requested but not yet received plugins
missing_plugins: u32,
missing_plugins: HashSet<PluginHash>,
/// Locally cached plugins needed by the server
local_plugins: Vec<PathBuf>,
}
@ -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<PathBuf> { std::mem::take(&mut self.local_plugins) }

View File

@ -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<PluginHash, PluginError> {
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<u8>,
) -> Result<(), std::io::Error> {
) -> Result<PluginHash, PluginError> {
let path = store_server_plugin(base_dir, data)?;
self.load_server_plugin(path);
Ok(())
self.load_server_plugin(path)
}
/// list all registered plugins

View File

@ -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::<PluginMgr>()
.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.

View File

@ -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::<PluginMgr>()
.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();
}
}
}
},