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
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
/* Copyright (c) 2024 Li Jin, dragon-fly@qq.com
Permission 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:
The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
THE 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. */
extern "C" {
fn content_set_search_paths(var: i64);
fn content_get_search_paths() -> i64;
fn content_get_asset_path() -> i64;
fn content_get_writable_path() -> i64;
fn content_save(filename: i64, content: i64) -> i32;
fn content_exist(filename: i64) -> i32;
fn content_mkdir(path: i64) -> i32;
fn content_isdir(path: i64) -> i32;
fn content_copy(src: i64, dst: i64) -> i32;
fn content_move_to(src: i64, dst: i64) -> i32;
fn content_remove(path: i64) -> i32;
fn content_get_full_path(filename: i64) -> i64;
fn content_add_search_path(path: i64);
fn content_insert_search_path(index: i32, path: i64);
fn content_remove_search_path(path: i64);
fn content_clear_path_cache();
fn content_get_dirs(path: i64) -> i64;
fn content_get_files(path: i64) -> i64;
fn content_get_all_files(path: i64) -> i64;
fn content_load_async(filename: i64, func: i32, stack: i64);
fn content_copy_async(src_file: i64, target_file: i64, func: i32, stack: i64);
fn content_save_async(filename: i64, content: i64, func: i32, stack: i64);
fn content_zip_async(folder_path: i64, zip_file: i64, func: i32, stack: i64, func1: i32, stack1: i64);
fn content_unzip_async(zip_file: i64, folder_path: i64, func: i32, stack: i64, func1: i32, stack1: i64);
fn content_load_excel(filename: i64) -> i64;
}
/// The `Content` is a static struct that manages file searching,
/// loading and other operations related to resources.
pub struct Content { }
impl Content {
/// Sets an array of directories to search for resource files.
pub fn set_search_paths(var: &Vec<&str>) {
unsafe { content_set_search_paths(crate::dora::Vector::from_str(var)) };
}
/// Gets an array of directories to search for resource files.
pub fn get_search_paths() -> Vec<String> {
return unsafe { crate::dora::Vector::to_str(content_get_search_paths()) };
}
/// Gets the path to the directory containing read-only resources.
pub fn get_asset_path() -> String {
return unsafe { crate::dora::to_string(content_get_asset_path()) };
}
/// Gets the path to the directory where files can be written.
pub fn get_writable_path() -> String {
return unsafe { crate::dora::to_string(content_get_writable_path()) };
}
/// Saves the specified content to a file with the specified filename.
///
/// # Arguments
///
/// * `filename` - The name of the file to save.
/// * `content` - The content to save to the file.
///
/// # Returns
///
/// * `bool` - `true` if the content saves to file successfully, `false` otherwise.
pub fn save(filename: &str, content: &str) -> bool {
unsafe { return content_save(crate::dora::from_string(filename), crate::dora::from_string(content)) != 0; }
}
/// Checks if a file with the specified filename exists.
///
/// # Arguments
///
/// * `filename` - The name of the file to check.
///
/// # Returns
///
/// * `bool` - `true` if the file exists, `false` otherwise.
pub fn exist(filename: &str) -> bool {
unsafe { return content_exist(crate::dora::from_string(filename)) != 0; }
}
/// Creates a new directory with the specified path.
///
/// # Arguments
///
/// * `path` - The path of the directory to create.
///
/// # Returns
///
/// * `bool` - `true` if the directory was created, `false` otherwise.
pub fn mkdir(path: &str) -> bool {
unsafe { return content_mkdir(crate::dora::from_string(path)) != 0; }
}
/// Checks if the specified path is a directory.
///
/// # Arguments
///
/// * `path` - The path to check.
///
/// # Returns
///
/// * `bool` - `true` if the path is a directory, `false` otherwise.
pub fn isdir(path: &str) -> bool {
unsafe { return content_isdir(crate::dora::from_string(path)) != 0; }
}
/// Copies the file or directory at the specified source path to the target path.
///
/// # Arguments
///
/// * `src_path` - The path of the file or directory to copy.
/// * `dst_path` - The path to copy the file or directory to.
///
/// # Returns
///
/// * `bool` - `true` if the file or directory was successfully copied to the target path, `false` otherwise.
pub fn copy(src: &str, dst: &str) -> bool {
unsafe { return content_copy(crate::dora::from_string(src), crate::dora::from_string(dst)) != 0; }
}
/// Moves the file or directory at the specified source path to the target path.
///
/// # Arguments
///
/// * `src_path` - The path of the file or directory to move.
/// * `dst_path` - The path to move the file or directory to.
///
/// # Returns
///
/// * `bool` - `true` if the file or directory was successfully moved to the target path, `false` otherwise.
pub fn move_to(src: &str, dst: &str) -> bool {
unsafe { return content_move_to(crate::dora::from_string(src), crate::dora::from_string(dst)) != 0; }
}
/// Removes the file or directory at the specified path.
///
/// # Arguments
///
/// * `path` - The path of the file or directory to remove.
///
/// # Returns
///
/// * `bool` - `true` if the file or directory was successfully removed, `false` otherwise.
pub fn remove(path: &str) -> bool {
unsafe { return content_remove(crate::dora::from_string(path)) != 0; }
}
/// Gets the full path of a file with the specified filename.
///
/// # Arguments
///
/// * `filename` - The name of the file to get the full path of.
///
/// # Returns
///
/// * `String` - The full path of the file.
pub fn get_full_path(filename: &str) -> String {
unsafe { return crate::dora::to_string(content_get_full_path(crate::dora::from_string(filename))); }
}
/// Adds a new search path to the end of the list.
///
/// # Arguments
///
/// * `path` - The search path to add.
pub fn add_search_path(path: &str) {
unsafe { content_add_search_path(crate::dora::from_string(path)); }
}
/// Inserts a search path at the specified index.
///
/// # Arguments
///
/// * `index` - The index at which to insert the search path.
/// * `path` - The search path to insert.
pub fn insert_search_path(index: i32, path: &str) {
unsafe { content_insert_search_path(index, crate::dora::from_string(path)); }
}
/// Removes the specified search path from the list.
///
/// # Arguments
///
/// * `path` - The search path to remove.
pub fn remove_search_path(path: &str) {
unsafe { content_remove_search_path(crate::dora::from_string(path)); }
}
/// Clears the search path cache of the map of relative paths to full paths.
pub fn clear_path_cache() {
unsafe { content_clear_path_cache(); }
}
/// Gets the names of all subdirectories in the specified directory.
///
/// # Arguments
///
/// * `path` - The path of the directory to search.
///
/// # Returns
///
/// * `Vec<String>` - An array of the names of all subdirectories in the specified directory.
pub fn get_dirs(path: &str) -> Vec<String> {
unsafe { return crate::dora::Vector::to_str(content_get_dirs(crate::dora::from_string(path))); }
}
/// Gets the names of all files in the specified directory.
///
/// # Arguments
///
/// * `path` - The path of the directory to search.
///
/// # Returns
///
/// * `Vec<String>` - An array of the names of all files in the specified directory.
pub fn get_files(path: &str) -> Vec<String> {
unsafe { return crate::dora::Vector::to_str(content_get_files(crate::dora::from_string(path))); }
}
/// Gets the names of all files in the specified directory and its subdirectories.
///
/// # Arguments
///
/// * `path` - The path of the directory to search.
///
/// # Returns
///
/// * `Vec<String>` - An array of the names of all files in the specified directory and its subdirectories.
pub fn get_all_files(path: &str) -> Vec<String> {
unsafe { return crate::dora::Vector::to_str(content_get_all_files(crate::dora::from_string(path))); }
}
/// Asynchronously loads the content of the file with the specified filename.
///
/// # Arguments
///
/// * `filename` - The name of the file to load.
/// * `callback` - The function to call with the content of the file once it is loaded.
///
/// # Returns
///
/// * `String` - The content of the loaded file.
pub fn load_async(filename: &str, mut callback: Box<dyn FnMut(&str)>) {
let mut stack = crate::dora::CallStack::new();
let stack_raw = stack.raw();
let func_id = crate::dora::push_function(Box::new(move || {
callback(stack.pop_str().unwrap().as_str())
}));
unsafe { content_load_async(crate::dora::from_string(filename), func_id, stack_raw); }
}
/// Asynchronously copies a file or a folder from the source path to the destination path.
///
/// # Arguments
///
/// * `srcFile` - The path of the file or folder to copy.
/// * `targetFile` - The destination path of the copied files.
/// * `callback` - The function to call with a boolean indicating whether the file or folder was copied successfully.
///
/// # Returns
///
/// * `bool` - `true` if the file or folder was copied successfully, `false` otherwise.
pub fn copy_async(src_file: &str, target_file: &str, mut callback: Box<dyn FnMut(bool)>) {
let mut stack = crate::dora::CallStack::new();
let stack_raw = stack.raw();
let func_id = crate::dora::push_function(Box::new(move || {
callback(stack.pop_bool().unwrap())
}));
unsafe { content_copy_async(crate::dora::from_string(src_file), crate::dora::from_string(target_file), func_id, stack_raw); }
}
/// Asynchronously saves the specified content to a file with the specified filename.
///
/// # Arguments
///
/// * `filename` - The name of the file to save.
/// * `content` - The content to save to the file.
/// * `callback` - The function to call with a boolean indicating whether the content was saved successfully.
///
/// # Returns
///
/// * `bool` - `true` if the content was saved successfully, `false` otherwise.
pub fn save_async(filename: &str, content: &str, mut callback: Box<dyn FnMut(bool)>) {
let mut stack = crate::dora::CallStack::new();
let stack_raw = stack.raw();
let func_id = crate::dora::push_function(Box::new(move || {
callback(stack.pop_bool().unwrap())
}));
unsafe { content_save_async(crate::dora::from_string(filename), crate::dora::from_string(content), func_id, stack_raw); }
}
/// Asynchronously compresses the specified folder to a ZIP archive with the specified filename.
///
/// # Arguments
///
/// * `folder_path` - The path of the folder to compress, should be under the asset writable path.
/// * `zip_file` - The name of the ZIP archive to create.
/// * `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.
/// * `callback` - The function to call with a boolean indicating whether the folder was compressed successfully.
///
/// # Returns
///
/// * `bool` - `true` if the folder was compressed successfully, `false` otherwise.
pub fn zip_async(folder_path: &str, zip_file: &str, mut filter: Box<dyn FnMut(&str) -> bool>, mut callback: Box<dyn FnMut(bool)>) {
let mut stack = crate::dora::CallStack::new();
let stack_raw = stack.raw();
let func_id = crate::dora::push_function(Box::new(move || {
let result = filter(stack.pop_str().unwrap().as_str());
stack.push_bool(result);
}));
let mut stack1 = crate::dora::CallStack::new();
let stack_raw1 = stack1.raw();
let func_id1 = crate::dora::push_function(Box::new(move || {
callback(stack1.pop_bool().unwrap())
}));
unsafe { content_zip_async(crate::dora::from_string(folder_path), crate::dora::from_string(zip_file), func_id, stack_raw, func_id1, stack_raw1); }
}
/// Asynchronously decompresses a ZIP archive to the specified folder.
///
/// # Arguments
///
/// * `zip_file` - The name of the ZIP archive to decompress, should be a file under the asset writable path.
/// * `folder_path` - The path of the folder to decompress to, should be under the asset writable path.
/// * `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.
/// * `callback` - The function to call with a boolean indicating whether the archive was decompressed successfully.
///
/// # Returns
///
/// * `bool` - `true` if the folder was decompressed successfully, `false` otherwise.
pub fn unzip_async(zip_file: &str, folder_path: &str, mut filter: Box<dyn FnMut(&str) -> bool>, mut callback: Box<dyn FnMut(bool)>) {
let mut stack = crate::dora::CallStack::new();
let stack_raw = stack.raw();
let func_id = crate::dora::push_function(Box::new(move || {
let result = filter(stack.pop_str().unwrap().as_str());
stack.push_bool(result);
}));
let mut stack1 = crate::dora::CallStack::new();
let stack_raw1 = stack1.raw();
let func_id1 = crate::dora::push_function(Box::new(move || {
callback(stack1.pop_bool().unwrap())
}));
unsafe { content_unzip_async(crate::dora::from_string(zip_file), crate::dora::from_string(folder_path), func_id, stack_raw, func_id1, stack_raw1); }
}
pub fn load_excel(filename: &str) -> crate::dora::WorkBook {
unsafe { return crate::dora::WorkBook::from(content_load_excel(crate::dora::from_string(filename))); }
}
}