ability to exclude folders from resource registry

This commit is contained in:
Dmitry Stepanov
2025-04-05 18:15:52 +03:00
parent 202ed9a7b3
commit b05874da14
3 changed files with 60 additions and 14 deletions
+1
View File
@@ -805,6 +805,7 @@ mod tests {
io, io,
loaders, loaders,
Path::new(TEST_FOLDER1).join("resources.registry"), Path::new(TEST_FOLDER1).join("resources.registry"),
Default::default(),
)); ));
assert!(std::fs::exists(make_metadata_file_path(TEST_FOLDER1, 0)).unwrap()); assert!(std::fs::exists(make_metadata_file_path(TEST_FOLDER1, 0)).unwrap());
+16 -9
View File
@@ -485,9 +485,10 @@ impl ResourceManagerState {
path.display() path.display()
); );
let task_resource_io = self.resource_io.clone(); let resource_io = self.resource_io.clone();
let task_resource_registry = self.resource_registry.clone(); let resource_registry = self.resource_registry.clone();
let registry_status = task_resource_registry.lock().status.clone(); let excluded_folders = resource_registry.lock().excluded_folders.clone();
let registry_status = resource_registry.lock().status.clone();
registry_status.mark_as_loading(); registry_status.mark_as_loading();
let task_loaders = self.loaders.clone(); let task_loaders = self.loaders.clone();
self.task_pool.spawn_task(async move { self.task_pool.spawn_task(async move {
@@ -495,16 +496,21 @@ impl ResourceManagerState {
// Wasm is an exception, because it does not have a file system. // Wasm is an exception, because it does not have a file system.
#[cfg(not(target_arch = "wasm32"))] #[cfg(not(target_arch = "wasm32"))]
{ {
let new_data = let new_data = ResourceRegistry::scan(
ResourceRegistry::scan(task_resource_io.clone(), task_loaders, &path).await; resource_io.clone(),
if let Err(error) = new_data.save(&path, &*task_resource_io).await { task_loaders,
&path,
excluded_folders,
)
.await;
if let Err(error) = new_data.save(&path, &*resource_io).await {
err!( err!(
"Unable to write the resource registry at the {} path! Reason: {:?}", "Unable to write the resource registry at the {} path! Reason: {:?}",
path.display(), path.display(),
error error
) )
} }
let mut lock = task_resource_registry.lock(); let mut lock = resource_registry.lock();
lock.set_container(new_data); lock.set_container(new_data);
registry_status.mark_as_loaded(); registry_status.mark_as_loaded();
@@ -518,9 +524,10 @@ impl ResourceManagerState {
#[cfg(target_arch = "wasm32")] #[cfg(target_arch = "wasm32")]
{ {
// Then load the registry. // Then load the registry.
match registry::RegistryContainer::load_from_file(&path, &*task_resource_io).await { match crate::registry::RegistryContainer::load_from_file(&path, &*resource_io).await
{
Ok(registry) => { Ok(registry) => {
let mut lock = task_resource_registry.lock(); let mut lock = resource_registry.lock();
lock.set_container(registry); lock.set_container(registry);
registry_status.mark_as_loaded(); registry_status.mark_as_loaded();
+43 -5
View File
@@ -21,6 +21,7 @@
use crate::{ use crate::{
io::ResourceIo, loader::ResourceLoadersContainer, metadata::ResourceMetadata, state::WakersList, io::ResourceIo, loader::ResourceLoadersContainer, metadata::ResourceMetadata, state::WakersList,
}; };
use fxhash::FxHashSet;
use fyrox_core::{ use fyrox_core::{
append_extension, info, io::FileError, ok_or_return, parking_lot::Mutex, replace_slashes, warn, append_extension, info, io::FileError, ok_or_return, parking_lot::Mutex, replace_slashes, warn,
Uuid, Uuid,
@@ -160,10 +161,26 @@ async fn make_relative_path_async<P: AsRef<Path>>(
/// Resource registry is responsible for UUID mapping of resource files. It maintains a map of /// Resource registry is responsible for UUID mapping of resource files. It maintains a map of
/// `UUID -> Resource Path`. /// `UUID -> Resource Path`.
#[derive(Default, Clone)] #[derive(Clone)]
pub struct ResourceRegistry { pub struct ResourceRegistry {
pub paths: RegistryContainer, pub paths: RegistryContainer,
pub status: ResourceRegistryStatusFlag, pub status: ResourceRegistryStatusFlag,
pub excluded_folders: FxHashSet<PathBuf>,
}
impl Default for ResourceRegistry {
fn default() -> Self {
let mut excluded_folders = FxHashSet::default();
// Exclude build artifacts folder by default.
excluded_folders.insert(PathBuf::from("target"));
Self {
paths: Default::default(),
status: Default::default(),
excluded_folders,
}
}
} }
impl ResourceRegistry { impl ResourceRegistry {
@@ -262,6 +279,7 @@ impl ResourceRegistry {
resource_io: Arc<dyn ResourceIo>, resource_io: Arc<dyn ResourceIo>,
loaders: Arc<Mutex<ResourceLoadersContainer>>, loaders: Arc<Mutex<ResourceLoadersContainer>>,
root: impl AsRef<Path>, root: impl AsRef<Path>,
excluded_folders: FxHashSet<PathBuf>,
) -> RegistryContainer { ) -> RegistryContainer {
let registry_path = root.as_ref(); let registry_path = root.as_ref();
let registry_folder = registry_path let registry_folder = registry_path
@@ -276,11 +294,13 @@ impl ResourceRegistry {
let mut container = RegistryContainer::default(); let mut container = RegistryContainer::default();
let file_iterator = ok_or_return!( let mut paths_to_visit = ok_or_return!(
resource_io.walk_directory(&registry_folder).await, resource_io.read_directory(&registry_folder).await,
container container
); )
for fs_path in file_iterator { .collect::<Vec<_>>();
while let Some(fs_path) = paths_to_visit.pop() {
let path = match make_relative_path_async(&fs_path, &*resource_io).await { let path = match make_relative_path_async(&fs_path, &*resource_io).await {
Ok(path) => path, Ok(path) => path,
Err(err) => { Err(err) => {
@@ -294,6 +314,24 @@ impl ResourceRegistry {
} }
}; };
if excluded_folders.contains(&path) {
warn!(
"Skipping {} folder, because it is in the excluded folders list!",
path.display()
);
continue;
}
if resource_io.is_dir(&path).await {
// Continue iterating on subfolders.
if let Ok(iter) = resource_io.read_directory(&path).await {
paths_to_visit.extend(iter);
}
continue;
}
if !loaders.lock().is_supported_resource(&path) { if !loaders.lock().is_supported_resource(&path) {
if path if path
.extension() .extension()