dora_ssr/dora/content.rs
1/* Copyright (c) 2016-2025 Li Jin <dragon-fly@qq.com>
2
3Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:
4
5The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
6
7THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */
8
9extern "C" {
10 fn content_set_search_paths(val: i64);
11 fn content_get_search_paths() -> i64;
12 fn content_set_asset_path(val: i64);
13 fn content_get_asset_path() -> i64;
14 fn content_set_writable_path(val: i64);
15 fn content_get_writable_path() -> i64;
16 fn content_get_app_path() -> i64;
17 fn content_save(filename: i64, content: i64) -> i32;
18 fn content_exist(filename: i64) -> i32;
19 fn content_mkdir(path: i64) -> i32;
20 fn content_isdir(path: i64) -> i32;
21 fn content_is_absolute_path(path: i64) -> i32;
22 fn content_copy(src: i64, dst: i64) -> i32;
23 fn content_move_to(src: i64, dst: i64) -> i32;
24 fn content_remove(path: i64) -> i32;
25 fn content_get_full_path(filename: i64) -> i64;
26 fn content_add_search_path(path: i64);
27 fn content_insert_search_path(index: i32, path: i64);
28 fn content_remove_search_path(path: i64);
29 fn content_clear_path_cache();
30 fn content_get_dirs(path: i64) -> i64;
31 fn content_get_files(path: i64) -> i64;
32 fn content_get_all_files(path: i64) -> i64;
33 fn content_load_async(filename: i64, func0: i32, stack0: i64);
34 fn content_copy_async(src_file: i64, target_file: i64, func0: i32, stack0: i64);
35 fn content_save_async(filename: i64, content: i64, func0: i32, stack0: i64);
36 fn content_zip_async(folder_path: i64, zip_file: i64, func0: i32, stack0: i64, func1: i32, stack1: i64);
37 fn content_unzip_async(zip_file: i64, folder_path: i64, func0: i32, stack0: i64, func1: i32, stack1: i64);
38 fn content_load_excel(filename: i64) -> i64;
39}
40/// The `Content` is a static struct that manages file searching,
41/// loading and other operations related to resources.
42pub struct Content { }
43impl Content {
44 /// Sets an array of directories to search for resource files.
45 pub fn set_search_paths(val: &Vec<&str>) {
46 unsafe { content_set_search_paths(crate::dora::Vector::from_str(val)) };
47 }
48 /// Gets an array of directories to search for resource files.
49 pub fn get_search_paths() -> Vec<String> {
50 return unsafe { crate::dora::Vector::to_str(content_get_search_paths()) };
51 }
52 /// Sets the path to the directory containing read-only resources. Can only be altered by the user on platform Windows, MacOS and Linux.
53 pub fn set_asset_path(val: &str) {
54 unsafe { content_set_asset_path(crate::dora::from_string(val)) };
55 }
56 /// Gets the path to the directory containing read-only resources. Can only be altered by the user on platform Windows, MacOS and Linux.
57 pub fn get_asset_path() -> String {
58 return unsafe { crate::dora::to_string(content_get_asset_path()) };
59 }
60 /// Sets the path to the directory where files can be written. Can only be altered by the user on platform Windows, MacOS and Linux. Default is the same as `appPath`.
61 pub fn set_writable_path(val: &str) {
62 unsafe { content_set_writable_path(crate::dora::from_string(val)) };
63 }
64 /// Gets the path to the directory where files can be written. Can only be altered by the user on platform Windows, MacOS and Linux. Default is the same as `appPath`.
65 pub fn get_writable_path() -> String {
66 return unsafe { crate::dora::to_string(content_get_writable_path()) };
67 }
68 /// Gets the path to the directory for the application storage.
69 pub fn get_app_path() -> String {
70 return unsafe { crate::dora::to_string(content_get_app_path()) };
71 }
72 /// Saves the specified content to a file with the specified filename.
73 ///
74 /// # Arguments
75 ///
76 /// * `filename` - The name of the file to save.
77 /// * `content` - The content to save to the file.
78 ///
79 /// # Returns
80 ///
81 /// * `bool` - `true` if the content saves to file successfully, `false` otherwise.
82 pub fn save(filename: &str, content: &str) -> bool {
83 unsafe { return content_save(crate::dora::from_string(filename), crate::dora::from_string(content)) != 0; }
84 }
85 /// Checks if a file with the specified filename exists.
86 ///
87 /// # Arguments
88 ///
89 /// * `filename` - The name of the file to check.
90 ///
91 /// # Returns
92 ///
93 /// * `bool` - `true` if the file exists, `false` otherwise.
94 pub fn exist(filename: &str) -> bool {
95 unsafe { return content_exist(crate::dora::from_string(filename)) != 0; }
96 }
97 /// Creates a new directory with the specified path.
98 ///
99 /// # Arguments
100 ///
101 /// * `path` - The path of the directory to create.
102 ///
103 /// # Returns
104 ///
105 /// * `bool` - `true` if the directory was created, `false` otherwise.
106 pub fn mkdir(path: &str) -> bool {
107 unsafe { return content_mkdir(crate::dora::from_string(path)) != 0; }
108 }
109 /// Checks if the specified path is a directory.
110 ///
111 /// # Arguments
112 ///
113 /// * `path` - The path to check.
114 ///
115 /// # Returns
116 ///
117 /// * `bool` - `true` if the path is a directory, `false` otherwise.
118 pub fn isdir(path: &str) -> bool {
119 unsafe { return content_isdir(crate::dora::from_string(path)) != 0; }
120 }
121 /// Checks if the specified path is an absolute path.
122 ///
123 /// # Arguments
124 ///
125 /// * `path` - The path to check.
126 ///
127 /// # Returns
128 ///
129 /// * `bool` - `true` if the path is an absolute path, `false` otherwise.
130 pub fn is_absolute_path(path: &str) -> bool {
131 unsafe { return content_is_absolute_path(crate::dora::from_string(path)) != 0; }
132 }
133 /// Copies the file or directory at the specified source path to the target path.
134 ///
135 /// # Arguments
136 ///
137 /// * `src_path` - The path of the file or directory to copy.
138 /// * `dst_path` - The path to copy the file or directory to.
139 ///
140 /// # Returns
141 ///
142 /// * `bool` - `true` if the file or directory was successfully copied to the target path, `false` otherwise.
143 pub fn copy(src: &str, dst: &str) -> bool {
144 unsafe { return content_copy(crate::dora::from_string(src), crate::dora::from_string(dst)) != 0; }
145 }
146 /// Moves the file or directory at the specified source path to the target path.
147 ///
148 /// # Arguments
149 ///
150 /// * `src_path` - The path of the file or directory to move.
151 /// * `dst_path` - The path to move the file or directory to.
152 ///
153 /// # Returns
154 ///
155 /// * `bool` - `true` if the file or directory was successfully moved to the target path, `false` otherwise.
156 pub fn move_to(src: &str, dst: &str) -> bool {
157 unsafe { return content_move_to(crate::dora::from_string(src), crate::dora::from_string(dst)) != 0; }
158 }
159 /// Removes the file or directory at the specified path.
160 ///
161 /// # Arguments
162 ///
163 /// * `path` - The path of the file or directory to remove.
164 ///
165 /// # Returns
166 ///
167 /// * `bool` - `true` if the file or directory was successfully removed, `false` otherwise.
168 pub fn remove(path: &str) -> bool {
169 unsafe { return content_remove(crate::dora::from_string(path)) != 0; }
170 }
171 /// Gets the full path of a file with the specified filename.
172 ///
173 /// # Arguments
174 ///
175 /// * `filename` - The name of the file to get the full path of.
176 ///
177 /// # Returns
178 ///
179 /// * `String` - The full path of the file.
180 pub fn get_full_path(filename: &str) -> String {
181 unsafe { return crate::dora::to_string(content_get_full_path(crate::dora::from_string(filename))); }
182 }
183 /// Adds a new search path to the end of the list.
184 ///
185 /// # Arguments
186 ///
187 /// * `path` - The search path to add.
188 pub fn add_search_path(path: &str) {
189 unsafe { content_add_search_path(crate::dora::from_string(path)); }
190 }
191 /// Inserts a search path at the specified index.
192 ///
193 /// # Arguments
194 ///
195 /// * `index` - The index at which to insert the search path.
196 /// * `path` - The search path to insert.
197 pub fn insert_search_path(index: i32, path: &str) {
198 unsafe { content_insert_search_path(index, crate::dora::from_string(path)); }
199 }
200 /// Removes the specified search path from the list.
201 ///
202 /// # Arguments
203 ///
204 /// * `path` - The search path to remove.
205 pub fn remove_search_path(path: &str) {
206 unsafe { content_remove_search_path(crate::dora::from_string(path)); }
207 }
208 /// Clears the search path cache of the map of relative paths to full paths.
209 pub fn clear_path_cache() {
210 unsafe { content_clear_path_cache(); }
211 }
212 /// Gets the names of all subdirectories in the specified directory.
213 ///
214 /// # Arguments
215 ///
216 /// * `path` - The path of the directory to search.
217 ///
218 /// # Returns
219 ///
220 /// * `Vec<String>` - An array of the names of all subdirectories in the specified directory.
221 pub fn get_dirs(path: &str) -> Vec<String> {
222 unsafe { return crate::dora::Vector::to_str(content_get_dirs(crate::dora::from_string(path))); }
223 }
224 /// Gets the names of all files in the specified directory.
225 ///
226 /// # Arguments
227 ///
228 /// * `path` - The path of the directory to search.
229 ///
230 /// # Returns
231 ///
232 /// * `Vec<String>` - An array of the names of all files in the specified directory.
233 pub fn get_files(path: &str) -> Vec<String> {
234 unsafe { return crate::dora::Vector::to_str(content_get_files(crate::dora::from_string(path))); }
235 }
236 /// Gets the names of all files in the specified directory and its subdirectories.
237 ///
238 /// # Arguments
239 ///
240 /// * `path` - The path of the directory to search.
241 ///
242 /// # Returns
243 ///
244 /// * `Vec<String>` - An array of the names of all files in the specified directory and its subdirectories.
245 pub fn get_all_files(path: &str) -> Vec<String> {
246 unsafe { return crate::dora::Vector::to_str(content_get_all_files(crate::dora::from_string(path))); }
247 }
248 /// Asynchronously loads the content of the file with the specified filename.
249 ///
250 /// # Arguments
251 ///
252 /// * `filename` - The name of the file to load.
253 /// * `callback` - The function to call with the content of the file once it is loaded.
254 ///
255 /// # Returns
256 ///
257 /// * `String` - The content of the loaded file.
258 pub fn load_async(filename: &str, mut callback: Box<dyn FnMut(&str)>) {
259 let mut stack0 = crate::dora::CallStack::new();
260 let stack_raw0 = stack0.raw();
261 let func_id0 = crate::dora::push_function(Box::new(move || {
262 callback(stack0.pop_str().unwrap().as_str())
263 }));
264 unsafe { content_load_async(crate::dora::from_string(filename), func_id0, stack_raw0); }
265 }
266 /// Asynchronously copies a file or a folder from the source path to the destination path.
267 ///
268 /// # Arguments
269 ///
270 /// * `srcFile` - The path of the file or folder to copy.
271 /// * `targetFile` - The destination path of the copied files.
272 /// * `callback` - The function to call with a boolean indicating whether the file or folder was copied successfully.
273 ///
274 /// # Returns
275 ///
276 /// * `bool` - `true` if the file or folder was copied successfully, `false` otherwise.
277 pub fn copy_async(src_file: &str, target_file: &str, mut callback: Box<dyn FnMut(bool)>) {
278 let mut stack0 = crate::dora::CallStack::new();
279 let stack_raw0 = stack0.raw();
280 let func_id0 = crate::dora::push_function(Box::new(move || {
281 callback(stack0.pop_bool().unwrap())
282 }));
283 unsafe { content_copy_async(crate::dora::from_string(src_file), crate::dora::from_string(target_file), func_id0, stack_raw0); }
284 }
285 /// Asynchronously saves the specified content to a file with the specified filename.
286 ///
287 /// # Arguments
288 ///
289 /// * `filename` - The name of the file to save.
290 /// * `content` - The content to save to the file.
291 /// * `callback` - The function to call with a boolean indicating whether the content was saved successfully.
292 ///
293 /// # Returns
294 ///
295 /// * `bool` - `true` if the content was saved successfully, `false` otherwise.
296 pub fn save_async(filename: &str, content: &str, mut callback: Box<dyn FnMut(bool)>) {
297 let mut stack0 = crate::dora::CallStack::new();
298 let stack_raw0 = stack0.raw();
299 let func_id0 = crate::dora::push_function(Box::new(move || {
300 callback(stack0.pop_bool().unwrap())
301 }));
302 unsafe { content_save_async(crate::dora::from_string(filename), crate::dora::from_string(content), func_id0, stack_raw0); }
303 }
304 /// Asynchronously compresses the specified folder to a ZIP archive with the specified filename.
305 ///
306 /// # Arguments
307 ///
308 /// * `folder_path` - The path of the folder to compress, should be under the asset writable path.
309 /// * `zip_file` - The name of the ZIP archive to create.
310 /// * `filter` - An optional function to filter the files to include in the archive. The function takes a filename as input and returns a boolean indicating whether to include the file. If not provided, all files will be included.
311 /// * `callback` - The function to call with a boolean indicating whether the folder was compressed successfully.
312 ///
313 /// # Returns
314 ///
315 /// * `bool` - `true` if the folder was compressed successfully, `false` otherwise.
316 pub fn zip_async(folder_path: &str, zip_file: &str, mut filter: Box<dyn FnMut(&str) -> bool>, mut callback: Box<dyn FnMut(bool)>) {
317 let mut stack0 = crate::dora::CallStack::new();
318 let stack_raw0 = stack0.raw();
319 let func_id0 = crate::dora::push_function(Box::new(move || {
320 let result = filter(stack0.pop_str().unwrap().as_str());
321 stack0.push_bool(result);
322 }));
323 let mut stack1 = crate::dora::CallStack::new();
324 let stack_raw1 = stack1.raw();
325 let func_id1 = crate::dora::push_function(Box::new(move || {
326 callback(stack1.pop_bool().unwrap())
327 }));
328 unsafe { content_zip_async(crate::dora::from_string(folder_path), crate::dora::from_string(zip_file), func_id0, stack_raw0, func_id1, stack_raw1); }
329 }
330 /// Asynchronously decompresses a ZIP archive to the specified folder.
331 ///
332 /// # Arguments
333 ///
334 /// * `zip_file` - The name of the ZIP archive to decompress, should be a file under the asset writable path.
335 /// * `folder_path` - The path of the folder to decompress to, should be under the asset writable path.
336 /// * `filter` - An optional function to filter the files to include in the archive. The function takes a filename as input and returns a boolean indicating whether to include the file. If not provided, all files will be included.
337 /// * `callback` - The function to call with a boolean indicating whether the archive was decompressed successfully.
338 ///
339 /// # Returns
340 ///
341 /// * `bool` - `true` if the folder was decompressed successfully, `false` otherwise.
342 pub fn unzip_async(zip_file: &str, folder_path: &str, mut filter: Box<dyn FnMut(&str) -> bool>, mut callback: Box<dyn FnMut(bool)>) {
343 let mut stack0 = crate::dora::CallStack::new();
344 let stack_raw0 = stack0.raw();
345 let func_id0 = crate::dora::push_function(Box::new(move || {
346 let result = filter(stack0.pop_str().unwrap().as_str());
347 stack0.push_bool(result);
348 }));
349 let mut stack1 = crate::dora::CallStack::new();
350 let stack_raw1 = stack1.raw();
351 let func_id1 = crate::dora::push_function(Box::new(move || {
352 callback(stack1.pop_bool().unwrap())
353 }));
354 unsafe { content_unzip_async(crate::dora::from_string(zip_file), crate::dora::from_string(folder_path), func_id0, stack_raw0, func_id1, stack_raw1); }
355 }
356 pub fn load_excel(filename: &str) -> crate::dora::WorkBook {
357 unsafe { return crate::dora::WorkBook::from(content_load_excel(crate::dora::from_string(filename))); }
358 }
359}