mod downloads are finally working

This commit is contained in:
Luuk van Oijen committed 2023-11-16 10:42:59 +01:00
1 parent 596a7234ed
commit 37d7a289eb
3 files changed
+41 -17

No files matched your search

+5 -1
View File
@@ -24,7 +24,11 @@ async fn main() {
if entry.path().is_file() { if entry.path().is_file() {
if let Ok(metadata) = entry.metadata() { if let Ok(metadata) = entry.metadata() {
if let Some(filename) = entry.path().file_name().map(|s| s.to_string_lossy()) { if let Some(filename) = entry.path().file_name().map(|s| s.to_string_lossy()) {
user_config.mods.push((filename.to_string(), metadata.len() as usize)); let mut name = filename.to_string();
if !name.starts_with("/") {
name = format!("/{name}");
}
user_config.mods.push((name, metadata.len() as usize));
} }
} }
} }
+20 -5
View File
@@ -24,6 +24,10 @@ use super::packet::*;
static ATOMIC_ID_COUNTER: AtomicU8 = AtomicU8::new(0); static ATOMIC_ID_COUNTER: AtomicU8 = AtomicU8::new(0);
lazy_static! {
pub static ref CLIENT_MOD_PROGRESS: Mutex<HashMap<u8, usize>> = Mutex::new(HashMap::new());
}
#[derive(PartialEq)] #[derive(PartialEq)]
pub enum ClientState { pub enum ClientState {
None, None,
@@ -246,14 +250,15 @@ impl Client {
RawPacket::from_code('-') RawPacket::from_code('-')
} else { } else {
let mut file_data = String::new(); let mut file_data = String::new();
for (name, size) in &config.mods { // TODO: Collapse these 2 loops into 1
for (name, _size) in &config.mods {
let mut mod_name = name.clone(); let mut mod_name = name.clone();
if mod_name.starts_with("/") == false { if mod_name.starts_with("/") == false {
mod_name = format!("/{mod_name}"); mod_name = format!("/{mod_name}");
} }
file_data.push_str(&format!("{mod_name};")); file_data.push_str(&format!("{mod_name};"));
} }
for (name, size) in &config.mods { for (_name, size) in &config.mods {
file_data.push_str(&format!("{size};")); file_data.push_str(&format!("{size};"));
} }
RawPacket::from_str(&file_data) RawPacket::from_str(&file_data)
@@ -277,21 +282,31 @@ impl Client {
mod_name = format!("/{mod_name}"); mod_name = format!("/{mod_name}");
} }
let mut mod_id = 0;
for (i, (bmod_name, _bmod_size)) in config.mods.iter().enumerate() {
if bmod_name == &mod_name {
mod_id = i;
}
}
let mod_path = format!("Resources/Client{mod_name}"); let mod_path = format!("Resources/Client{mod_name}");
let file_data = std::fs::read(mod_path)?; let file_data = std::fs::read(mod_path)?;
let packet = RawPacket::from_data(file_data[..(file_data.len()/2)].to_vec());
{ {
let mut lock = self.write_half.lock().await; let mut lock = self.write_half.lock().await;
lock.writable().await?; lock.writable().await?;
trace!("Sending packets!"); trace!("Sending packets!");
if let Err(e) = tcp_write_raw(lock.deref_mut(), Packet::Raw(packet)).await { if let Err(e) = lock.write(&file_data[..(file_data.len()/2)]).await {
error!("{:?}", e); error!("{:?}", e);
} }
trace!("Packets sent!"); trace!("Packets sent!");
drop(lock); drop(lock);
} }
{
let mut lock = CLIENT_MOD_PROGRESS.lock().await;
lock.insert(self.id, mod_id);
}
} }
_ => error!("Unknown packet! {:?}", packet), _ => error!("Unknown packet! {:?}", packet),
} }
+16 -11
View File
@@ -175,16 +175,26 @@ impl Server {
}, },
'D' => { 'D' => {
// Download connection (for old protocol) // Download connection (for old protocol)
// This crashes the client after sending over all mods // This crashes the client after sending over 1 mod.
// Probably related to it not waiting until a new mod is requested! // I have no idea why, perhaps I'm missing something that I'm supposed to send it.
// TODO: Implement this: https://github.com/BeamMP/BeamMP-Server/blob/master/src/TNetwork.cpp#L775
socket.readable().await; socket.readable().await;
let mut tmp = [0u8; 1]; let mut tmp = [0u8; 1];
socket.read_exact(&mut tmp).await; socket.read_exact(&mut tmp).await;
let id = tmp[0] as usize; let id = tmp[0] as usize;
debug!("[D] HandleDownload connection for client id: {}", id); debug!("[D] HandleDownload connection for client id: {}", id);
let mut mod_id = 0; let mut sent_mods = Vec::new();
'download: while let Ok(_) = socket.writable().await { 'download: while let Ok(_) = socket.writable().await {
{
let lock = CLIENT_MOD_PROGRESS.lock().await;
if lock.get(&(id as u8)).is_none() { continue; }
}
let mod_id = {
let lock = CLIENT_MOD_PROGRESS.lock().await;
*lock.get(&(id as u8)).unwrap()
};
if sent_mods.contains(&mod_id) { continue; }
debug!("[D] Starting download!"); debug!("[D] Starting download!");
let mut mod_name = { let mut mod_name = {
if mod_id >= cfg_ref.mods.len() { if mod_id >= cfg_ref.mods.len() {
@@ -194,8 +204,6 @@ impl Server {
let bmod = &cfg_ref.mods[mod_id]; // TODO: This is a bit uhh yeah let bmod = &cfg_ref.mods[mod_id]; // TODO: This is a bit uhh yeah
debug!("[D] Mod name: {}", bmod.0); debug!("[D] Mod name: {}", bmod.0);
mod_id += 1;
bmod.0.clone() bmod.0.clone()
}; };
@@ -207,19 +215,16 @@ impl Server {
let mod_path = format!("Resources/Client{mod_name}"); let mod_path = format!("Resources/Client{mod_name}");
if let Ok(file_data) = std::fs::read(mod_path) { if let Ok(file_data) = std::fs::read(mod_path) {
let packet = RawPacket::from_data(file_data[(file_data.len()/2)..].to_vec());
{ {
trace!("[D] Sending packets!"); trace!("[D] Sending packets!");
let real_packet = Packet::Raw(packet); if let Err(e) = socket.write(&file_data[(file_data.len()/2)..]).await {
let mut raw_data: Vec<u8> = real_packet.get_header().to_le_bytes().to_vec();
raw_data.extend_from_slice(real_packet.get_data());
if let Err(e) = socket.write(&raw_data).await {
error!("{:?}", e); error!("{:?}", e);
} }
trace!("[D] Packets sent!"); trace!("[D] Packets sent!");
} }
} }
sent_mods.push(mod_id);
} }
debug!("[D] Done!"); debug!("[D] Done!");
}, },