Skip to content

Commit 8fc6383

Browse files
Update ffbuildtool + optimize version validation
1 parent 355ec31 commit 8fc6383

File tree

3 files changed

+48
-29
lines changed

3 files changed

+48
-29
lines changed

src-tauri/Cargo.lock

+3-3
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

src-tauri/Cargo.toml

+1-1
Original file line numberDiff line numberDiff line change
@@ -27,5 +27,5 @@ uuid = { version = "1.10.0", features = ["serde", "v4"] }
2727
dns-lookup = "2.0.4"
2828
tokio = { version = "1.41.1", features = ["sync"] }
2929
reqwest = "0.12.9"
30-
ffbuildtool = { git = "https://github.com/OpenFusionProject/ffbuildtool.git", version = "2.0.0", default-features = false }
30+
ffbuildtool = { git = "https://github.com/OpenFusionProject/ffbuildtool.git", version = "3.0.0", default-features = false }
3131
tauri-plugin-shell = "2.0.2"

src-tauri/src/lib.rs

+44-25
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,14 @@ use ffbuildtool::ItemProgress;
77
use serde::{Deserialize, Serialize};
88
use state::{get_app_statics, AppState, FlatServer, FrontendServers, Server, ServerInfo, Versions};
99

10-
use std::{collections::HashMap, env, sync::OnceLock};
10+
use std::{
11+
collections::HashSet,
12+
env,
13+
sync::{
14+
atomic::{AtomicU64, Ordering},
15+
Arc, OnceLock, RwLock,
16+
},
17+
};
1118
use tokio::sync::Mutex;
1219

1320
use log::*;
@@ -245,14 +252,13 @@ async fn prep_launch(
245252

246253
#[tauri::command]
247254
async fn validate_version_game(app_handle: tauri::AppHandle, uuid: Uuid) -> CommandResult<bool> {
248-
static TRACKING: OnceLock<std::sync::Mutex<HashMap<Uuid, u64>>> = OnceLock::new();
255+
static IN_PROGRESS: OnceLock<RwLock<HashSet<Uuid>>> = OnceLock::new();
249256

250-
fn callback(uuid: &Uuid, _item_name: &str, item_progress: ItemProgress) {
257+
let total_size = Arc::new(AtomicU64::new(0));
258+
let callback = move |uuid: &Uuid, _item_name: &str, item_progress: ItemProgress| {
251259
if let ItemProgress::Completed(sz) = item_progress {
252-
let mut tracking = TRACKING.get().unwrap().lock().unwrap();
253-
let old_sz = tracking.get(uuid).copied().unwrap();
260+
let old_sz = total_size.fetch_add(sz, Ordering::AcqRel);
254261
let new_sz = old_sz + sz;
255-
tracking.insert(*uuid, new_sz);
256262
let event = ValidationEvent {
257263
uuid: *uuid,
258264
sz: new_sz,
@@ -261,7 +267,8 @@ async fn validate_version_game(app_handle: tauri::AppHandle, uuid: Uuid) -> Comm
261267
warn!("Failed to emit validated_item_game event: {}", e);
262268
}
263269
}
264-
}
270+
};
271+
let callback = Arc::new(callback);
265272

266273
let internal = async {
267274
let state = app_handle.state::<Mutex<AppState>>();
@@ -275,21 +282,27 @@ async fn validate_version_game(app_handle: tauri::AppHandle, uuid: Uuid) -> Comm
275282
drop(state); // give up the state lock
276283

277284
{
278-
let mut tracking = TRACKING
279-
.get_or_init(|| std::sync::Mutex::new(HashMap::new()))
280-
.lock()
285+
let tracking = IN_PROGRESS
286+
.get_or_init(|| std::sync::RwLock::new(HashSet::new()))
287+
.read()
281288
.unwrap();
282-
if tracking.contains_key(&uuid) {
289+
if tracking.contains(&uuid) {
283290
return Err(format!("Already validating game cache for {}", uuid).into());
284291
}
285-
tracking.insert(uuid, 0);
292+
}
293+
294+
{
295+
let mut tracking = IN_PROGRESS.get().unwrap().write().unwrap();
296+
tracking.insert(uuid);
286297
}
287298

288299
let passed = version
289300
.validate_uncompressed(&path.to_string_lossy(), Some(callback))
290301
.await
291302
.is_ok_and(|corrupted| corrupted.is_empty());
292-
TRACKING.get().unwrap().lock().unwrap().remove(&uuid);
303+
304+
let mut tracking = IN_PROGRESS.get().unwrap().write().unwrap();
305+
tracking.remove(&uuid);
293306
Ok(passed)
294307
};
295308
debug!("validate_version_game {}", uuid);
@@ -298,14 +311,13 @@ async fn validate_version_game(app_handle: tauri::AppHandle, uuid: Uuid) -> Comm
298311

299312
#[tauri::command]
300313
async fn validate_version_offline(app_handle: tauri::AppHandle, uuid: Uuid) -> CommandResult<bool> {
301-
static TRACKING: OnceLock<std::sync::Mutex<HashMap<Uuid, u64>>> = OnceLock::new();
314+
static IN_PROGRESS: OnceLock<std::sync::RwLock<HashSet<Uuid>>> = OnceLock::new();
302315

303-
fn callback(uuid: &Uuid, _item_name: &str, item_progress: ItemProgress) {
316+
let total_size = Arc::new(AtomicU64::new(0));
317+
let callback = move |uuid: &Uuid, _item_name: &str, item_progress: ItemProgress| {
304318
if let ItemProgress::Completed(sz) = item_progress {
305-
let mut tracking = TRACKING.get().unwrap().lock().unwrap();
306-
let old_sz = tracking.get(uuid).copied().unwrap();
319+
let old_sz = total_size.fetch_add(sz, Ordering::AcqRel);
307320
let new_sz = old_sz + sz;
308-
tracking.insert(*uuid, new_sz);
309321
let event = ValidationEvent {
310322
uuid: *uuid,
311323
sz: new_sz,
@@ -317,7 +329,8 @@ async fn validate_version_offline(app_handle: tauri::AppHandle, uuid: Uuid) -> C
317329
warn!("Failed to emit validated_item_offline event: {}", e);
318330
}
319331
}
320-
}
332+
};
333+
let callback = Arc::new(callback);
321334

322335
let internal = async {
323336
let state = app_handle.state::<Mutex<AppState>>();
@@ -331,21 +344,27 @@ async fn validate_version_offline(app_handle: tauri::AppHandle, uuid: Uuid) -> C
331344
drop(state); // give up the state lock
332345

333346
{
334-
let mut tracking = TRACKING
335-
.get_or_init(|| std::sync::Mutex::new(HashMap::new()))
336-
.lock()
347+
let tracking = IN_PROGRESS
348+
.get_or_init(|| std::sync::RwLock::new(HashSet::new()))
349+
.read()
337350
.unwrap();
338-
if tracking.contains_key(&uuid) {
351+
if tracking.contains(&uuid) {
339352
return Err(format!("Already validating offline cache for {}", uuid).into());
340353
}
341-
tracking.insert(uuid, 0);
354+
}
355+
356+
{
357+
let mut tracking = IN_PROGRESS.get().unwrap().write().unwrap();
358+
tracking.insert(uuid);
342359
}
343360

344361
let passed = version
345362
.validate_compressed(&path.to_string_lossy(), Some(callback))
346363
.await
347364
.is_ok_and(|corrupted| corrupted.is_empty());
348-
TRACKING.get().unwrap().lock().unwrap().remove(&uuid);
365+
366+
let mut tracking = IN_PROGRESS.get().unwrap().write().unwrap();
367+
tracking.remove(&uuid);
349368
Ok(passed)
350369
};
351370
debug!("validate_version_offline {}", uuid);

0 commit comments

Comments
 (0)