dora_ssr/dora/
cache.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 cache_load(filename: i64) -> i32;
11	fn cache_load_async(filename: i64, func0: i32, stack0: i64);
12	fn cache_update_item(filename: i64, content: i64);
13	fn cache_update_texture(filename: i64, texture: i64);
14	fn cache_unload_item_or_type(name: i64) -> i32;
15	fn cache_unload();
16	fn cache_remove_unused();
17	fn cache_remove_unused_by_type(type_name: i64);
18}
19use crate::dora::IObject;
20/// A interface for managing various game resources.
21pub struct Cache { }
22impl Cache {
23	/// Loads a file into the cache with a blocking operation.
24	///
25	/// # Arguments
26	///
27	/// * `filename` - The name of the file to load.
28	///
29	/// # Returns
30	///
31	/// * `bool` - `true` if the file was loaded successfully, `false` otherwise.
32	pub fn load(filename: &str) -> bool {
33		unsafe { return cache_load(crate::dora::from_string(filename)) != 0; }
34	}
35	/// Loads a file into the cache asynchronously.
36	///
37	/// # Arguments
38	///
39	/// * `filenames` - The name of the file(s) to load. This can be a single string or a vector of strings.
40	/// * `handler` - A callback function that is invoked when the file is loaded.
41	pub fn load_async(filename: &str, mut handler: Box<dyn FnMut(bool)>) {
42		let mut stack0 = crate::dora::CallStack::new();
43		let stack_raw0 = stack0.raw();
44		let func_id0 = crate::dora::push_function(Box::new(move || {
45			handler(stack0.pop_bool().unwrap())
46		}));
47		unsafe { cache_load_async(crate::dora::from_string(filename), func_id0, stack_raw0); }
48	}
49	/// Updates the content of a file loaded in the cache.
50	/// If the item of filename does not exist in the cache, a new file content will be added into the cache.
51	///
52	/// # Arguments
53	///
54	/// * `filename` - The name of the file to update.
55	/// * `content` - The new content for the file.
56	pub fn update_item(filename: &str, content: &str) {
57		unsafe { cache_update_item(crate::dora::from_string(filename), crate::dora::from_string(content)); }
58	}
59	/// Updates the texture object of the specific filename loaded in the cache.
60	/// If the texture object of filename does not exist in the cache, it will be added into the cache.
61	///
62	/// # Arguments
63	///
64	/// * `filename` - The name of the texture to update.
65	/// * `texture` - The new texture object for the file.
66	pub fn update_texture(filename: &str, texture: &crate::dora::Texture2D) {
67		unsafe { cache_update_texture(crate::dora::from_string(filename), texture.raw()); }
68	}
69	/// Unloads a resource from the cache.
70	///
71	/// # Arguments
72	///
73	/// * `name` - The type name of resource to unload, could be one of "Texture", "SVG", "Clip", "Frame", "Model", "Particle", "Shader", "Font", "Sound", "Spine". Or the name of the resource file to unload.
74	///
75	/// # Returns
76	///
77	/// * `bool` - `true` if the resource was unloaded successfully, `false` otherwise.
78	pub fn unload_item_or_type(name: &str) -> bool {
79		unsafe { return cache_unload_item_or_type(crate::dora::from_string(name)) != 0; }
80	}
81	/// Unloads all resources from the cache.
82	pub fn unload() {
83		unsafe { cache_unload(); }
84	}
85	/// Removes all unused resources (not being referenced) from the cache.
86	pub fn remove_unused() {
87		unsafe { cache_remove_unused(); }
88	}
89	/// Removes all unused resources of the given type from the cache.
90	///
91	/// # Arguments
92	///
93	/// * `resource_type` - The type of resource to remove. This could be one of "Texture", "SVG", "Clip", "Frame", "Model", "Particle", "Shader", "Font", "Sound", "Spine".
94	pub fn remove_unused_by_type(type_name: &str) {
95		unsafe { cache_remove_unused_by_type(crate::dora::from_string(type_name)); }
96	}
97}