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
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
use crate::exec;
#[derive(Debug)]
pub struct FileError {
pub kind: miniquad::fs::Error,
pub path: String,
}
impl std::error::Error for FileError {}
impl std::fmt::Display for FileError {
fn fmt(&self, f: &mut std::fmt::Formatter) -> std::fmt::Result {
write!(f, "Couldn't load file {}: {}", self.path, self.kind)
}
}
impl FileError {
pub fn new(kind: miniquad::fs::Error, path: &str) -> Self {
Self {
kind,
path: path.to_string(),
}
}
}
pub async fn load_file(path: &str) -> Result<Vec<u8>, FileError> {
fn load_file_inner(path: &str) -> exec::FileLoadingFuture {
use std::sync::{Arc, Mutex};
let contents = Arc::new(Mutex::new(None));
let path = path.to_owned();
{
let contents = contents.clone();
let err_path = path.clone();
miniquad::fs::load_file(&path, move |bytes| {
*contents.lock().unwrap() =
Some(bytes.map_err(|kind| FileError::new(kind, &err_path)));
});
}
exec::FileLoadingFuture { contents }
}
#[cfg(target_os = "ios")]
let _ = std::env::set_current_dir(std::env::current_exe().unwrap().parent().unwrap());
#[cfg(not(target_os = "android"))]
let path = if let Some(ref pc_assets) = crate::get_context().pc_assets_folder {
format!("{}/{}", pc_assets, path)
} else {
path.to_string()
};
load_file_inner(&path).await
}
pub async fn load_string(path: &str) -> Result<String, FileError> {
let data = load_file(path).await?;
Ok(String::from_utf8_lossy(&data).to_string())
}
pub fn set_pc_assets_folder(path: &str) {
crate::get_context().pc_assets_folder = Some(path.to_string());
}