Skip to content

Commit efc6619

Browse files
committed
Simplify multimc stat importing
1 parent 2bbf05c commit efc6619

File tree

2 files changed

+19
-59
lines changed

2 files changed

+19
-59
lines changed

crates/backend/src/instance.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -52,7 +52,7 @@ pub struct Instance {
5252
pub content_state: enum_map::EnumMap<ContentFolder, ContentFolderState>,
5353
}
5454

55-
#[derive(Debug, Clone, Default, Serialize, Deserialize)]
55+
#[derive(Debug, Clone, Default, Serialize, Deserialize, PartialEq, Eq)]
5656
pub struct InstanceStats {
5757
pub total_playtime_secs: u64,
5858
pub session_count: u64,

crates/backend/src/launcher_import/multimc.rs

Lines changed: 18 additions & 58 deletions
Original file line numberDiff line numberDiff line change
@@ -21,60 +21,7 @@ struct MMCPackComponent {
2121
version: Arc<str>,
2222
}
2323

24-
fn try_load_stats_from_multimc(instance_cfg: &Path) -> Option<InstanceStats> {
25-
let instance_cfg_str = std::fs::read_to_string(instance_cfg).ok()?;
26-
27-
let mut stats = InstanceStats::default();
28-
29-
let mut section = None;
30-
for line in instance_cfg_str.split(|v| v == '\n') {
31-
let line = line.trim_ascii_start();
32-
if line.is_empty() {
33-
continue;
34-
}
35-
36-
let start = line.as_bytes()[0];
37-
match start {
38-
b';' | b'#' => continue,
39-
b'[' => {
40-
section = Some(line.trim_ascii_end());
41-
},
42-
_ => {
43-
let Some((key, value)) = line.split_once("=") else {
44-
continue;
45-
};
46-
47-
48-
let mut value = value.trim_ascii();
49-
if value.len() > 1 && value.starts_with('"') && value.ends_with('"') {
50-
value = &value[1..value.len()-1];
51-
} else if value.len() > 1 && value.starts_with('\'') && value.ends_with('\'') {
52-
value = &value[1..value.len()-1];
53-
}
54-
55-
match (section, key) {
56-
(Some("[General]"), "totalTimePlayed") => {
57-
let Ok(time_played) = value.parse::<u64>() else {
58-
continue;
59-
};
60-
stats.total_playtime_secs = time_played;
61-
},
62-
(Some("[General]"), "lastLaunchTime") => {
63-
let Ok(last_launcher_time) = value.parse::<i64>() else {
64-
continue;
65-
};
66-
stats.last_played_unix_ms = Some(last_launcher_time);
67-
}
68-
_ => {}
69-
}
70-
}
71-
}
72-
}
73-
74-
Some(stats)
75-
}
76-
77-
pub fn try_load_from_multimc(instance_cfg: &Path, mmc_pack: &Path) -> Option<InstanceConfiguration> {
24+
pub fn try_load_from_multimc(instance_cfg: &Path, mmc_pack: &Path) -> Option<(InstanceConfiguration, InstanceStats)> {
7825
let mmc_pack_bytes = std::fs::read(mmc_pack).ok()?;
7926
let instance_cfg_str = std::fs::read_to_string(instance_cfg).ok()?;
8027

@@ -96,6 +43,7 @@ pub fn try_load_from_multimc(instance_cfg: &Path, mmc_pack: &Path) -> Option<Ins
9643
}
9744

9845
let mut configuration = InstanceConfiguration::new(minecraft_version?, loader.unwrap_or(Loader::Vanilla));
46+
let mut stats = InstanceStats::default();
9947

10048
let mut override_native_workarounds = false;
10149
let mut override_performance = false;
@@ -233,6 +181,18 @@ pub fn try_load_from_multimc(instance_cfg: &Path, mmc_pack: &Path) -> Option<Ins
233181
},
234182
(Some("[General]"), "InstanceAccountId") => {
235183
override_account.1 = value.parse::<Uuid>().ok();
184+
},
185+
(Some("[General]"), "totalTimePlayed") => {
186+
let Ok(time_played) = value.parse::<u64>() else {
187+
continue;
188+
};
189+
stats.total_playtime_secs = time_played;
190+
},
191+
(Some("[General]"), "lastLaunchTime") => {
192+
let Ok(last_launcher_time) = value.parse::<i64>() else {
193+
continue;
194+
};
195+
stats.last_played_unix_ms = Some(last_launcher_time);
236196
}
237197
_ => {}
238198
}
@@ -251,7 +211,7 @@ pub fn try_load_from_multimc(instance_cfg: &Path, mmc_pack: &Path) -> Option<Ins
251211
configuration.preferred_account = override_account.1;
252212
}
253213

254-
Some(configuration)
214+
Some((configuration, stats))
255215
}
256216

257217
#[derive(Deserialize, Debug)]
@@ -491,7 +451,7 @@ fn import_instances_from_multimc(backend: &BackendState, import_job: &ImportFrom
491451
modal_action.trackers.push(tracker.clone());
492452
tracker.notify();
493453

494-
let Some(configuration) = try_load_from_multimc(&to_import.multimc_instance_cfg, &to_import.multimc_mmc_pack) else {
454+
let Some((configuration, stats)) = try_load_from_multimc(&to_import.multimc_instance_cfg, &to_import.multimc_mmc_pack) else {
495455
tracker.set_finished(bridge::modal_action::ProgressTrackerFinishType::Error);
496456
tracker.notify();
497457
continue;
@@ -531,8 +491,8 @@ fn import_instances_from_multimc(backend: &BackendState, import_job: &ImportFrom
531491
let info_path = to_import.pandora_path.join("info_v1.json");
532492
_ = crate::write_safe(&info_path, &configuration_bytes);
533493

534-
// Write stats_v1.json if we have stats in the first place.
535-
if let Some(stats) = try_load_stats_from_multimc(&to_import.multimc_instance_cfg) {
494+
// Write stats_v1.json if we have some stats
495+
if stats != InstanceStats::default() {
536496
let stats_path = to_import.pandora_path.join("stats_v1.json");
537497
if let Ok(stats_bytes) = serde_json::to_vec(&stats) {
538498
_ = crate::write_safe(&stats_path, &stats_bytes);

0 commit comments

Comments
 (0)