diff --git a/src/main.rs b/src/main.rs index f592705..72cb5ac 100644 --- a/src/main.rs +++ b/src/main.rs @@ -322,32 +322,42 @@ impl Game { fn get_levels() -> Vec { let mut chapters = Vec::::new(); - let mut add_level = |path| { - let l = read_to_string(path) - .ok() - .as_deref() - .and_then(|s| serde_json::from_str(s).ok()); - if let Some(level) = l { - chapters.push(level); - } - }; for d in read_dir("levels").unwrap().flatten() { - if d.path().is_dir() { - for d in read_dir(d.path()).unwrap().flatten() { - add_level(d.path()); + if let Ok(text) = read_to_string(d.path()) { + if let Ok(chapter) = serde_json::from_str(&text) { + chapters.push(chapter); } - } else { - add_level(d.path()); } } chapters.sort_unstable_by_key(|c| c.title.clone()); - let mut levels = Vec::new(); + let mut level_list = Vec::new(); for c in chapters { - levels.push(LevelListEntry::Chapter(c.title, c.levels.len())); - levels.extend(c.levels.into_iter().map(LevelListEntry::Level)); + level_list.push(LevelListEntry::Chapter(c.title, c.levels.len())); + level_list.extend(c.levels.into_iter().map(LevelListEntry::Level)); } - levels + + // user levels + let mut custom_levels = Vec::new(); + let custom_level_dir = userdata_dir().join("levels"); + if custom_level_dir.is_dir() { + for d in read_dir(custom_level_dir).unwrap().flatten() { + if let Ok(text) = read_to_string(d.path()) { + if let Ok(level) = serde_json::from_str(&text) { + custom_levels.push(level); + } + } + } + } + level_list.push(LevelListEntry::Chapter( + "Custom levels".into(), + custom_levels.len(), + )); + for l in custom_levels { + level_list.push(LevelListEntry::Level(l)); + } + + level_list } fn get_solutions() -> HashMap> {