Skip to content

Commit de39e53

Browse files
committed
fix(replays): refactor background replay parsing logic to prevent possible panics
1 parent 2b4eb6a commit de39e53

File tree

1 file changed

+34
-33
lines changed

1 file changed

+34
-33
lines changed

src/task.rs

Lines changed: 34 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -532,35 +532,40 @@ fn parse_replay_data_in_background(
532532
debug!("game type is: {}, not sending", &game_type);
533533
break;
534534
}
535+
535536
let (metadata_provider, game_version) = { (wows_data.game_metadata.clone(), wows_data.game_version) };
536537
if let Some(metadata_provider) = metadata_provider {
537538
let mut replay = Replay::new(replay_file, Arc::clone(&metadata_provider));
538539
match replay.parse(game_version.to_string().as_str()) {
539540
Ok(report) => {
540541
if should_send_replays.load(Ordering::Relaxed) {
541542
// Send the replay builds to the remote server
542-
for player in report.player_entities() {
543+
for vehicle in report.player_entities() {
543544
#[cfg(not(feature = "shipbuilds_debugging"))]
544545
let url = "https://shipbuilds.com/api/ship_builds";
545546
#[cfg(feature = "shipbuilds_debugging")]
546547
let url = "http://192.168.1.215:3000/api/ship_builds";
547548

548-
// TODO: Bulk API
549-
let res = client
550-
.post(url)
551-
.json(&build_tracker::BuildTrackerPayload::build_from(
552-
player,
553-
player.player().map(|player| player.realm().to_string()).unwrap(),
554-
report.version(),
555-
game_type.clone(),
556-
&metadata_provider,
557-
))
558-
.send();
559-
if let Err(e) = res {
560-
error!("error sending request: {:?}", e);
561-
if e.is_connect() {
562-
break 'main_loop;
549+
if let Some(player) = vehicle.player() {
550+
// TODO: Bulk API
551+
let res = client
552+
.post(url)
553+
.json(&build_tracker::BuildTrackerPayload::build_from(
554+
vehicle,
555+
player.realm().to_string(),
556+
report.version(),
557+
game_type.clone(),
558+
&metadata_provider,
559+
))
560+
.send();
561+
if let Err(e) = res {
562+
error!("error sending request: {:?}", e);
563+
if e.is_connect() {
564+
break 'main_loop;
565+
}
563566
}
567+
} else {
568+
error!("no player for replay?");
564569
}
565570
}
566571
debug!("Successfully sent all builds");
@@ -627,23 +632,19 @@ pub fn start_background_parsing_thread(
627632
// Try to see if we have any historical replays we can send
628633
match std::fs::read_dir(&wows_data.replays_dir) {
629634
Ok(read_dir) => {
630-
for file in read_dir {
631-
if let Ok(file) = file {
632-
let path = file.path();
633-
if path.extension().map(|ext| ext != "wowsreplay").unwrap_or(false) || path.file_name().map(|name| name == "temp.wowsreplay").unwrap_or(false)
634-
{
635-
continue;
636-
}
635+
for file in read_dir.flatten() {
636+
let path = file.path();
637+
if path.extension().map(|ext| ext != "wowsreplay").unwrap_or(false) || path.file_name().map(|name| name == "temp.wowsreplay").unwrap_or(false) {
638+
continue;
639+
}
637640

638-
let path_str = path.to_string_lossy();
639-
let sent_replay = { sent_replays.read().contains(path_str.as_ref()) } || cfg!(feature = "shipbuilds_debugging");
641+
let path_str = path.to_string_lossy();
642+
let sent_replay = { sent_replays.read().contains(path_str.as_ref()) } || cfg!(feature = "shipbuilds_debugging");
640643

641-
if !sent_replay {
642-
if let Ok(_) = parse_replay_data_in_background(&path, &*wows_data, &client, Arc::clone(&should_send_replays), Arc::clone(&player_tracker))
643-
{
644-
sent_replays.write().insert(path_str.into_owned());
645-
}
646-
}
644+
if !sent_replay
645+
&& parse_replay_data_in_background(&path, &wows_data, &client, Arc::clone(&should_send_replays), Arc::clone(&player_tracker)).is_ok()
646+
{
647+
sent_replays.write().insert(path_str.into_owned());
647648
}
648649
}
649650
}
@@ -654,14 +655,14 @@ pub fn start_background_parsing_thread(
654655
}
655656

656657
debug!("Beginning backgorund replay receive loop");
657-
while let Some(path) = rx.recv().ok() {
658+
while let Ok(path) = rx.recv() {
658659
let path_str = path.to_string_lossy();
659660
let sent_replay = { sent_replays.read().contains(path_str.as_ref()) };
660661

661662
if !sent_replay {
662663
debug!("Attempting to send replay at {}", path_str);
663664
let wows_data = wows_data.read();
664-
if let Ok(_) = parse_replay_data_in_background(&path, &*wows_data, &client, Arc::clone(&should_send_replays), Arc::clone(&player_tracker)) {
665+
if parse_replay_data_in_background(&path, &wows_data, &client, Arc::clone(&should_send_replays), Arc::clone(&player_tracker)).is_ok() {
665666
sent_replays.write().insert(path_str.into_owned());
666667
}
667668
} else {

0 commit comments

Comments
 (0)