Skip to content

Commit

Permalink
perf: optimize some logic for lookup gacha urls
Browse files Browse the repository at this point in the history
  • Loading branch information
lgou2w committed Oct 2, 2023
1 parent 0d97c13 commit 913dd3b
Show file tree
Hide file tree
Showing 3 changed files with 14 additions and 7 deletions.
2 changes: 1 addition & 1 deletion src-tauri/src/gacha/impl_genshin.rs
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@ impl GachaUrlFinder for GenshinGacha {
fn find_gacha_urls<P: AsRef<Path>>(&self, game_data_dir: P) -> Result<Vec<GachaUrl>> {
// See: https://github.com/lgou2w/HoYo.Gacha/issues/10
let cache_data_dir = lookup_valid_cache_data_dir(game_data_dir)?;
lookup_gacha_urls_from_endpoint(cache_data_dir, ENDPOINT)
lookup_gacha_urls_from_endpoint(cache_data_dir, ENDPOINT, true)
}
}

Expand Down
2 changes: 1 addition & 1 deletion src-tauri/src/gacha/impl_starrail.rs
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@ impl GachaUrlFinder for StarRailGacha {
fn find_gacha_urls<P: AsRef<Path>>(&self, game_data_dir: P) -> Result<Vec<GachaUrl>> {
// See: https://github.com/lgou2w/HoYo.Gacha/issues/10
let cache_data_dir = lookup_valid_cache_data_dir(game_data_dir)?;
lookup_gacha_urls_from_endpoint(cache_data_dir, ENDPOINT)
lookup_gacha_urls_from_endpoint(cache_data_dir, ENDPOINT, true)
}
}

Expand Down
17 changes: 12 additions & 5 deletions src-tauri/src/gacha/utilities.rs
Original file line number Diff line number Diff line change
Expand Up @@ -166,6 +166,7 @@ pub(super) fn lookup_valid_cache_data_dir<P: AsRef<Path>>(game_data_dir: P) -> R
pub(super) fn lookup_gacha_urls_from_endpoint<P: AsRef<Path>>(
cache_data_dir: P,
endpoint: &str,
skip_expired: bool,
) -> Result<Vec<GachaUrl>> {
let cache_data_dir = cache_data_dir.as_ref();

Expand All @@ -176,6 +177,7 @@ pub(super) fn lookup_gacha_urls_from_endpoint<P: AsRef<Path>>(

let mut result = Vec::new();
let current_local_offset = UtcOffset::current_local_offset().map_err(time::Error::from)?;
let now = OffsetDateTime::now_utc().to_offset(current_local_offset);

// Foreach the cache address table of the index file
for addr in index_file.table {
Expand All @@ -196,12 +198,12 @@ pub(super) fn lookup_gacha_urls_from_endpoint<P: AsRef<Path>>(
continue;
}

let mut url = url.to_string();

// These url start with '1/0/', only get the later part
if url.starts_with("1/0/") {
url = url[4..].to_string();
}
let url = if url.starts_with("1/0/") {
url[4..].to_string()
} else {
url.to_string()
};

// Convert creation time
let creation_time = {
Expand All @@ -211,6 +213,11 @@ pub(super) fn lookup_gacha_urls_from_endpoint<P: AsRef<Path>>(
offset_datetime.to_offset(current_local_offset)
};

// HACK: By default, this gacha url is valid for 1 day.
if skip_expired && creation_time + time::Duration::DAY < now {
continue; // It's expired
}

result.push(GachaUrl {
addr: addr.into(),
creation_time,
Expand Down

0 comments on commit 913dd3b

Please sign in to comment.