8个版本
0.1.7 | 2023年12月7日 |
---|---|
0.1.6 | 2023年5月6日 |
0.1.5 | 2023年3月19日 |
119 在 压缩 分类中
每月下载量 23
55KB
1K SLoC
fast-assets
易于使用的资源管理器,可以通过将文件作为 Vec<u8>
来操作,以管理任何类型的文件,它旨在适应任何软件/游戏/框架。
功能
- 搜索文件
- 索引文件
- 加载资源
- 加载压缩资源
- 依赖检查器
- 扩展
- 文件重定向
- 写入文件(仅非压缩文件)
- 下载器(从网络下载文件)
- 易于文件移动/复制/删除
压缩支持
- ZIP (.zip)
加载预定义索引
用于加载压缩文件
- 从CSV文件
示例
let mut index = fast_assets::index::Index::new("./", "\\w+\\.rs");
index.set_csv_separator('/');
index.add_from_file("index/index.csv");
folder/subfolder/file.txt
archives/archive.zip/file.txt
入门
初始化
// Search all un-compressed files and archives,
// using extern index allow to add files compressed,
// the manager will automatically manage the decompression
let mut index = fast_assets::index::Index::new("./", "\\w+\\.rs");
index.search();
index.add_from_file("index/index.csv");
// The decompression cache is what will manage your compressed files and their caches.
let dc = fast_assets::decompression_manager::DecompressionManager::default();
let mut manager = fast_assets::manager::AssetsManager::new(index, dc);
// Create a file and add it to the index
manage.create_file("myFile.text");
在内存中加载/卸载文件
加载
// Load a compressed file
manager.load("index.json").unwrap();
// Load a not compressed file
manager.load("text.csv").unwrap();
// Load a not compressed file using full path
manager.load("fr/text.csv").unwrap();
// Load a compressed file using full path
manager.load("lang.zip/fr/text.csv").unwrap();
卸载
// AssetsManager::unload() need two parameters:
// decompression_cache: bool => If the file was compressed and if true it will put the file in the cache.
// filename: &str => The file that will be unloaded
// UnLoad and put in cache a compressed file
manager.unload("index.json", true);
// UnLoad a not compressed file
manager.unload("text.csv", false);
// Tips: if you want always keep in cache, set always at true,
// it will have no impact on the unloading of a uncompressed file
使用正则表达式
// Get all the files that matching the regex into a Vec<Pathbuf>
let files = manager.get_files_matching_regex("\\w+\\.csv");
// Using it:
manager.get(&files[0].file_name().unwrap().to_string_lossy());
访问数据
你必须知道两件事可能会失败
- 文件未索引
- 文件不存在
因此你需要通过它们来访问数据
manager.get("text.csv").unwrap().unwrap();
manager.get_ref("text.csv").unwrap().unwrap();
manager.get_mut("text.csv").unwrap().unwrap();
// In the case where you have multiple file with the same name:
manager.get("en/text.csv").unwrap();
manager.get_ref("fr/text.csv").unwrap();
manager.get_mut("it/text.csv").unwrap();
// If you to set the data you can call:
manager.set_data("text.csv", b"Hello, World!".to_vec());
如果文件被放入缓存,它将自动重新加载。
保存数据
仅保存非压缩文件的文件数据。返回一个简单的 std::io::Result<()> 作为结果。如果文件不再存在,它将创建一个新文件。
manager.save("text.csv").unwrap();
移除文件引用
在管理器和解压缩管理器中,每个加载的文件都将有一个引用,但它们使用内存,因此要进行完全卸载,您需要移除它们。对于缓存的文件,它们将根据特质Drop删除其缓存文件。
manager.remove("text.csv").unwrap().unwrap();
// Tips: You don't need to unload before excepted if you need to put in cache
依赖检查器
依赖检查器(DependencieManager)的目标是在管理器中搜索未索引的文件。
JSON文件源
如以下示例所示,JSON文件定义了某些文件的依赖关系。JSON文件的组织不是递归的,因此您不能在另一个文件的依赖关系中定义文件的依赖关系。
{
"dependencies": {
"text.csv": [
"index.json",
"other.csv"
],
"index.json": [
"text.csv"
]
}
}
初始化依赖关系
// Create DependencieManager object
let mut dependencie_manager = DependencieManager::default();
// Load a file containing the dependencies required
dependencie_manager.load_file(&mut manager, "deps.json");
让我们检查依赖关系
现在您必须加载依赖关系,并且要检查它们,您需要调用三个命令
- 更新
- 检查是否有效
- 获取缺失的依赖关系
// Take a look in the manager to get if a dependency is missing
dependencie_manager.update(&mut manager);
// Return true if all dependencies are present
dependencie_manager.check_if_valid("text.csv");
// Get all the missing dependencies
dependencie_manager.get_missing_dependencies("text.csv");
扩展
这是一个添加自定义功能的简单方法。它旨在让您添加对新压缩格式的支持...
创建扩展
扩展是一个特性,它添加以下功能
/// Called when loading a file, and return true if continue the existing process
fn on_load(&mut self, _: &mut AssetsManager, path: &mut Option<String>) -> bool;
/// Called when unloading a file, and return true if continue the existing process
fn on_unload(&mut self, _: &mut AssetsManager, path: &mut &str, use_cache: &mut bool);
/// Called when remove a file reference, and return true if continue the existing process
fn on_remove(&mut self, _: &mut AssetsManager, path: &mut &str) -> bool;
/// Called when loading file/files from archive
fn on_archive(&mut self, _: &mut DecompressionManager, ext: &str, path: &str);
```
#### Add it to the AssetsManager
```rust
let my_extension = MyExtension::default();
manager.add_extension(Box::new(my_extension));
```
### Redirect System
Sometimes it's useful to specify a path and in background the assets manager use the good file path.
So it's implemented.
#### Add redirect
##### From simple command
```rust
index.add_redirect("base_path", "new_path");
```
##### From file
```json
{
"redirect": {
"base_path": "new_path"
}
}
```
```rust
index.add_redirect_from_file("redirect.json");
```
### Downloader
```rust
// Create an instance of the downloader
let downloader = crate::downloader::Downloader::default();
// If you want prevent a download failure
downloader.can_download("https://crates.io/assets/cargo.png");
downloader.can_download("https://www.rust-lang.net.cn/");
downloader.can_download("https://github.com/eVisualUser/bellecour-gamebook/blob/main/hello_world/hello_world.zip");
// Here using _sync method version to not have to handle the async.
// All errors produced will be output in the console.
downloader.download_sync(String::from("https://crates.io/assets/cargo.png"), String::from("crates.png"));
downloader.download_sync(String::from("https://www.rust-lang.net.cn/"), String::from("rust_lang.html"));
downloader.download_sync(String::from("https://github.com/eVisualUser/bellecour-gamebook/blob/main/hello_world/hello_world.zip"), String::from("HelloWorld.zip"));
```
### Easy File move/copy/remove
There is few useful methods to control your file, and update the index as well.
#### Create a File
```rust
// Create the file and add it to the index
manager.create_file("myFile.txt");
```
#### Copy a file
```rust
// Copy the file to another location
manager.copy_file("myFile.txt", "folder/myFile.txt");
```
#### Move a file
```rust
// Copy the file and remove the original
manager.move_file("myFile.txt", "folder/myFile.txt");
```
#### Remove a file
```rust
// Remove a file from the index and from the directory
manager.remove_file("myFile.txt");
```
依赖项
~15–25MB
~371K SLoC