Skip to content

Commit

Permalink
feat: Interrupt when scrolling
Browse files Browse the repository at this point in the history
  • Loading branch information
wormtql committed Oct 17, 2021
1 parent 5fdb480 commit d7e793e
Show file tree
Hide file tree
Showing 4 changed files with 47 additions and 11 deletions.
2 changes: 1 addition & 1 deletion Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[package]
name = "yas"
version = "0.1.2"
version = "0.1.3"
edition = "2018"

# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html
Expand Down
16 changes: 15 additions & 1 deletion src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -52,15 +52,29 @@ fn set_dpi_awareness() {
}
}

fn get_version() -> String {
let s = include_str!("../Cargo.toml");
for line in s.lines() {
if line.starts_with("version = ") {
let temp = line.split("\"").collect::<Vec<_>>();
return String::from(temp[temp.len() - 2]);
}
}

String::from("unknown_version")
}

fn main() {
Builder::new().filter_level(LevelFilter::Info).init();

if !utils::is_admin() {
utils::error_and_quit("请以管理员身份运行该程序")
}

let version = get_version();

let matches = App::new("YAS - 原神圣遗物导出器")
.version("0.1.2")
.version(version.as_str())
.author("wormtql <[email protected]>")
.about("Genshin Impact Artifact Exporter")
.arg(Arg::with_name("max-row").long("max-row").takes_value(true).help("最大扫描行数"))
Expand Down
38 changes: 30 additions & 8 deletions src/scanner/yas_scanner.rs
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,13 @@ pub struct YasScanner {
scanned_count: u32,
}

enum ScrollResult {
TLE,
Interrupt,
Success,
Skip,
}

#[derive(Debug)]
pub struct YasScanResult {
name: String,
Expand Down Expand Up @@ -201,11 +208,15 @@ impl YasScanner {
}
}

fn scroll_one_row(&mut self) -> bool {
fn scroll_one_row(&mut self) -> ScrollResult {
let mut state = 0;
let mut count = 0;
let max_scroll = 20;
while count < max_scroll {
if utils::is_rmb_down() {
return ScrollResult::Interrupt;
}

self.enigo.mouse_scroll_y(-5);
utils::sleep(self.config.scroll_stop);
count += 1;
Expand All @@ -217,29 +228,33 @@ impl YasScanner {
self.avg_scroll_one_row = (self.avg_scroll_one_row * self.scrolled_rows as f64 + count as f64) / (self.scrolled_rows as f64 + 1.0);
info!("avg scroll/row: {}", self.avg_scroll_one_row);
self.scrolled_rows += 1;
return true;
return ScrollResult::Success;
}
}

false
ScrollResult::TLE
}

fn scroll_rows(&mut self, count: u32) {
fn scroll_rows(&mut self, count: u32) -> ScrollResult {
if self.scrolled_rows >= 5 {
let scroll = ((self.avg_scroll_one_row * count as f64 - 3.0).round() as u32).max(0);
for _ in 0..scroll {
self.enigo.mouse_scroll_y(-1);
}
utils::sleep(400);
self.align_row();
return;
return ScrollResult::Skip;
}

for _ in 0..count {
if !self.scroll_one_row() {
break;
match self.scroll_one_row() {
ScrollResult::TLE => return ScrollResult::TLE,
ScrollResult::Interrupt => return ScrollResult::Interrupt,
_ => (),
}
}

ScrollResult::Success
}

fn align_row(&mut self) -> bool {
Expand Down Expand Up @@ -543,7 +558,14 @@ impl YasScanner {
let remain_row = (remain + self.col - 1) / self.col;
let scroll_row = remain_row.min(self.row);
start_row = self.row - scroll_row;
self.scroll_rows(scroll_row);
match self.scroll_rows(scroll_row) {
ScrollResult::TLE => {
error!("翻页出现问题");
break 'outer;
},
ScrollResult::Interrupt => break 'outer,
_ => (),
}

utils::sleep(100);
}
Expand Down

0 comments on commit d7e793e

Please sign in to comment.