1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
use crate::utils::log::{Log, MessageKind};
use fyrox_core::{append_extension, io};
use ron::ser::PrettyConfig;
use serde::de::DeserializeOwned;
use serde::Serialize;
use std::fs::File;
use std::path::Path;
pub trait ImportOptions: Serialize + DeserializeOwned + Default + Clone {
fn save(&self, path: &Path) -> bool {
if let Ok(file) = File::create(path) {
if ron::ser::to_writer_pretty(file, self, PrettyConfig::default()).is_ok() {
return true;
}
}
false
}
}
pub async fn try_get_import_settings<T>(resource_path: &Path) -> Option<T>
where
T: ImportOptions,
{
let settings_path = append_extension(resource_path, "options");
match io::load_file(&settings_path).await {
Ok(bytes) => match ron::de::from_bytes::<T>(&bytes) {
Ok(options) => Some(options),
Err(e) => {
Log::writeln(
MessageKind::Error,
format!(
"Malformed options file {} for {} resource! Reason: {:?}",
settings_path.display(),
resource_path.display(),
e
),
);
None
}
},
Err(e) => {
Log::writeln(
MessageKind::Information,
format!(
"Unable to load options file {} for {} resource, fallback to defaults! Reason: {:?}",
settings_path.display(),
resource_path.display(),
e
),
);
None
}
}
}