| 31 | } |
| 32 | |
| 33 | impl Executor { |
| 34 | pub(crate) fn load_save_data(&mut self) -> Result<(), EngineError> { |
| 35 | let mut load_items = Vec::with_capacity(16); |
| 36 | for i in 0..16 { |
| 37 | let path = format!("{}{}.toml", ENGINE_CONFIG.save_path(), i); |
| 38 | if let Ok(content) = fs::read_to_string(&path) { |
| 39 | let SaveData { |
| 40 | script, |
| 41 | block_index, |
| 42 | explain, |
| 43 | image_path, |
| 44 | } = toml::from_str(&content).map_err(|e| SaveError::Deserialize { |
| 45 | path: path.clone(), |
| 46 | source: e, |
| 47 | })?; |
| 48 | let image = Image::load_from_path(Path::new(&image_path)).unwrap_or_default(); |
| 49 | load_items.push(SaveItem { |
| 50 | bg: image, |
| 51 | explain: explain.to_shared_string(), |
| 52 | index: block_index as i32, |
| 53 | name: script.to_shared_string(), |
| 54 | }); |
| 55 | } else { |
| 56 | let sava_data = |
| 57 | SaveData::new("".to_string(), 0, "空的".to_string(), "".to_string()); |
| 58 | let content = toml::to_string_pretty(&sava_data).map_err(SaveError::from)?; |
| 59 | load_items.push(SaveItem { |
| 60 | bg: Image::default(), |
| 61 | explain: "空的".to_shared_string(), |
| 62 | index: 0, |
| 63 | name: "".to_shared_string(), |
| 64 | }); |
| 65 | fs::write(&path, content).map_err(|e| SaveError::Write { |
| 66 | path: path.clone(), |
| 67 | source: e, |
| 68 | })?; |
| 69 | } |
| 70 | } |
| 71 | |
| 72 | let weak = self.get_weak(); |
| 73 | if let Some(window) = weak.upgrade() { |
| 74 | window.set_save_items(Rc::new(VecModel::from(load_items)).into()); |
| 75 | } |
| 76 | Ok(()) |
| 77 | } |
| 78 | } |