Skip to content

Commit

Permalink
Fix loading failing (#59) (#61)
Browse files Browse the repository at this point in the history
* Apply suggestions from code review
  • Loading branch information
dmadisetti authored Aug 2, 2023
1 parent 236a3b5 commit 48bcd09
Show file tree
Hide file tree
Showing 3 changed files with 69 additions and 15 deletions.
72 changes: 57 additions & 15 deletions src/client.rs
Original file line number Diff line number Diff line change
Expand Up @@ -289,6 +289,7 @@ fn execute(
} else {
queue.push_front(Command::Cli("info".to_string()));
}
log!("login");
}
["info"] => {
account = match Account::new(&response.to_string()) {
Expand All @@ -297,21 +298,13 @@ fn execute(
};
let mut state = state.lock()?;
*state = State::Loaded(0, -2);
log!("info");
}
["licenses_print"] => {
// Extract licenses
games = Vec::new();
let licenses = response.to_string();
let keys = licenses
.lines()
.enumerate()
.filter(|(i, _)| i % 4 == 0)
.map(|(_, l)| match *LICENSE_LEX.tokenize(l).as_slice() {
["packageID", id] => id.parse::<i32>().unwrap_or(-1),
_ => -1,
})
.filter(|x| x >= &0)
.collect::<Vec<i32>>();
let keys = keys_from_licenses(licenses);
let total = keys.len();
updated += total as i32;
for key in keys {
Expand All @@ -322,6 +315,7 @@ fn execute(
}
let mut state = state.lock()?;
*state = State::Loaded(0, total as i32);
log!("licenses_print");
}
["package_info_print", key] => {
let mut lines = response.lines();
Expand Down Expand Up @@ -349,13 +343,37 @@ fn execute(
State::Loaded(_, _) => {}
_ => *state = State::Loaded(updated, queue.len() as i32),
}
log!("package_info_print");
}
["app_info_print", key] => {
let mut lines = response.lines();
updated += 1;
if let Ok(game) = Game::new(key, &mut lines) {
games.push(game);
log!("Checking game");
// Bug requires additional scan
// do a proper check here in case this is ever fixed.
// A bit of a hack, but will do for now.
let mut response = response;
log!(response);
if response == "[0m" {
cmd.write("")?;
cmd.write("")?;
while !response.starts_with("[0mAppID") {
if let Ok(buf) = cmd.maybe_next() {
response = String::from_utf8_lossy(&buf).into_owned().into();
}
}
}
let mut lines = response.lines();

match Game::new(key, &mut lines) {
Ok(game) => {
log!("got game");
games.push(game);
log!(key);
}
Err(err) => {
log!(err)
}
};
}
["app_status", _id] => {
sender.send(response.to_string())?;
Expand Down Expand Up @@ -393,7 +411,16 @@ fn execute(
}
}
// Iterate to scrub past Steam> prompt
let _ = cmd.maybe_next()?;
let buf = cmd.maybe_next()?;
let mut prompt = String::from_utf8_lossy(&buf);
log!(prompt);
while prompt != "[1m\nSteam>" {
if let Ok(buf) = cmd.maybe_next() {
prompt = String::from_utf8_lossy(&buf).into_owned().into();
} else {
cmd.write("")?;
}
}
}
}
}
Expand Down Expand Up @@ -554,9 +581,24 @@ impl Drop for Client {
let _ = receiver.recv();
}
}

// Just some helpers broken out for testing
fn keys_from_licenses(licenses: String) -> Vec<i32> {
licenses
.lines()
.enumerate()
.filter(|(i, _)| i % 4 == 0)
.map(|(_, l)| match *LICENSE_LEX.tokenize(l).as_slice() {
["packageID", id] => id.parse::<i32>().unwrap_or(-1),
_ => -1,
})
.filter(|x| x >= &0)
.collect::<Vec<i32>>()
}

#[cfg(test)]
mod tests {
use crate::client::{Client, Command, State};
use crate::client::{Client, Command, State, keys_from_licenses};
use crate::util::error::STError;
use std::sync::mpsc::channel;
use std::sync::Arc;
Expand Down
8 changes: 8 additions & 0 deletions src/interface/game.rs
Original file line number Diff line number Diff line change
Expand Up @@ -99,8 +99,16 @@ impl Game {
status: Arc::new(Mutex::new(None)),
};
return Ok(game);
} else {
return Err(STError::Problem("File a github issue. Something may have changed in the Steam definition.".to_string()));
}
} else {
log!("Cannot get key", key);
log!(parse(lines));
}
} else {
log!("Cannot get nest");
log!(parse(lines));
}
Err(STError::Problem("Could not extract game.".to_string()))
}
Expand Down
4 changes: 4 additions & 0 deletions src/util/parser.rs
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,7 @@ lazy_static! {
\s*"([^"]+)"\s+"([^"]*)"\s* |
\s*"([^"]+)"\s*$ |
\s*(})\s*$ |
\s*[^}"].*$ |
"#,
);
}
Expand Down Expand Up @@ -109,6 +110,8 @@ pub fn parse(block: &mut dyn Iterator<Item = &str>) -> Datum {
block.next();
map.insert(key.to_string(), parse(block));
}
// Extra lines are sometimes present but do not match the SDL format.
// Skip them.
_ => {}
}
}
Expand All @@ -121,6 +124,7 @@ mod tests {
#[test]
fn test_parse_data() {
let mut block = r#"
[0mAppID : <id>, change number : 19486115/0, last change : Mon Jul 24 13:12:25 2023
"hmm"
{
"vdl" "format"
Expand Down

0 comments on commit 48bcd09

Please sign in to comment.