Changeset View
Changeset View
Standalone View
Standalone View
source/blender/blenkernel/intern/asset_catalog.cc
| Show All 18 Lines | |||||
| */ | */ | ||||
| #include <fstream> | #include <fstream> | ||||
| #include <set> | #include <set> | ||||
| #include "BKE_asset_catalog.hh" | #include "BKE_asset_catalog.hh" | ||||
| #include "BKE_asset_library.h" | #include "BKE_asset_library.h" | ||||
| #include "BLI_fileops.h" | #include "BLI_fileops.hh" | ||||
| #include "BLI_path_util.h" | #include "BLI_path_util.h" | ||||
| /* For S_ISREG() and S_ISDIR() on Windows. */ | /* For S_ISREG() and S_ISDIR() on Windows. */ | ||||
| #ifdef WIN32 | #ifdef WIN32 | ||||
| # include "BLI_winstuff.h" | # include "BLI_winstuff.h" | ||||
| #endif | #endif | ||||
| #include "CLG_log.h" | |||||
| static CLG_LogRef LOG = {"bke.asset_service"}; | |||||
| namespace blender::bke { | namespace blender::bke { | ||||
| const CatalogFilePath AssetCatalogService::DEFAULT_CATALOG_FILENAME = "blender_assets.cats.txt"; | const CatalogFilePath AssetCatalogService::DEFAULT_CATALOG_FILENAME = "blender_assets.cats.txt"; | ||||
| const int AssetCatalogDefinitionFile::SUPPORTED_VERSION = 1; | const int AssetCatalogDefinitionFile::SUPPORTED_VERSION = 1; | ||||
| const std::string AssetCatalogDefinitionFile::VERSION_MARKER = "VERSION "; | const std::string AssetCatalogDefinitionFile::VERSION_MARKER = "VERSION "; | ||||
| const std::string AssetCatalogDefinitionFile::HEADER = | const std::string AssetCatalogDefinitionFile::HEADER = | ||||
| ▲ Show 20 Lines • Show All 263 Lines • ▼ Show 20 Lines | void AssetCatalogService::load_from_disk() | ||||
| load_from_disk(asset_library_root_); | load_from_disk(asset_library_root_); | ||||
| } | } | ||||
| void AssetCatalogService::load_from_disk(const CatalogFilePath &file_or_directory_path) | void AssetCatalogService::load_from_disk(const CatalogFilePath &file_or_directory_path) | ||||
| { | { | ||||
| BLI_stat_t status; | BLI_stat_t status; | ||||
| if (BLI_stat(file_or_directory_path.data(), &status) == -1) { | if (BLI_stat(file_or_directory_path.data(), &status) == -1) { | ||||
| /* TODO(@sybren): throw an appropriate exception. */ | /* TODO(@sybren): throw an appropriate exception. */ | ||||
| CLOG_WARN(&LOG, "path not found: %s", file_or_directory_path.data()); | |||||
| return; | return; | ||||
| } | } | ||||
| if (S_ISREG(status.st_mode)) { | if (S_ISREG(status.st_mode)) { | ||||
| load_single_file(file_or_directory_path); | load_single_file(file_or_directory_path); | ||||
| } | } | ||||
| else if (S_ISDIR(status.st_mode)) { | else if (S_ISDIR(status.st_mode)) { | ||||
| load_directory_recursive(file_or_directory_path); | load_directory_recursive(file_or_directory_path); | ||||
| Show All 10 Lines | |||||
| void AssetCatalogService::load_directory_recursive(const CatalogFilePath &directory_path) | void AssetCatalogService::load_directory_recursive(const CatalogFilePath &directory_path) | ||||
| { | { | ||||
| /* TODO(@sybren): implement proper multi-file support. For now, just load | /* TODO(@sybren): implement proper multi-file support. For now, just load | ||||
| * the default file if it is there. */ | * the default file if it is there. */ | ||||
| CatalogFilePath file_path = asset_definition_default_file_path_from_dir(directory_path); | CatalogFilePath file_path = asset_definition_default_file_path_from_dir(directory_path); | ||||
| if (!BLI_exists(file_path.data())) { | if (!BLI_exists(file_path.data())) { | ||||
| /* No file to be loaded is perfectly fine. */ | /* No file to be loaded is perfectly fine. */ | ||||
| CLOG_INFO(&LOG, 2, "path not found: %s", file_path.data()); | |||||
| return; | return; | ||||
| } | } | ||||
| this->load_single_file(file_path); | this->load_single_file(file_path); | ||||
| } | } | ||||
| void AssetCatalogService::load_single_file(const CatalogFilePath &catalog_definition_file_path) | void AssetCatalogService::load_single_file(const CatalogFilePath &catalog_definition_file_path) | ||||
| { | { | ||||
| ▲ Show 20 Lines • Show All 471 Lines • ▼ Show 20 Lines | |||||
| { | { | ||||
| catalogs_.remove(catalog_id); | catalogs_.remove(catalog_id); | ||||
| } | } | ||||
| void AssetCatalogDefinitionFile::parse_catalog_file( | void AssetCatalogDefinitionFile::parse_catalog_file( | ||||
| const CatalogFilePath &catalog_definition_file_path, | const CatalogFilePath &catalog_definition_file_path, | ||||
| AssetCatalogParsedFn catalog_loaded_callback) | AssetCatalogParsedFn catalog_loaded_callback) | ||||
| { | { | ||||
| std::fstream infile(catalog_definition_file_path); | fstream infile(catalog_definition_file_path, std::ios::in); | ||||
| if (!infile.is_open()) { | |||||
| CLOG_ERROR(&LOG, "%s: unable to open file", catalog_definition_file_path.c_str()); | |||||
| return; | |||||
| } | |||||
| bool seen_version_number = false; | bool seen_version_number = false; | ||||
| std::string line; | std::string line; | ||||
| while (std::getline(infile, line)) { | while (std::getline(infile, line)) { | ||||
| const StringRef trimmed_line = StringRef(line).trim(); | const StringRef trimmed_line = StringRef(line).trim(); | ||||
| if (trimmed_line.is_empty() || trimmed_line[0] == '#') { | if (trimmed_line.is_empty() || trimmed_line[0] == '#') { | ||||
| continue; | continue; | ||||
| } | } | ||||
| ▲ Show 20 Lines • Show All 114 Lines • ▼ Show 20 Lines | |||||
| { | { | ||||
| char directory[PATH_MAX]; | char directory[PATH_MAX]; | ||||
| BLI_split_dir_part(dest_file_path.c_str(), directory, sizeof(directory)); | BLI_split_dir_part(dest_file_path.c_str(), directory, sizeof(directory)); | ||||
| if (!ensure_directory_exists(directory)) { | if (!ensure_directory_exists(directory)) { | ||||
| /* TODO(Sybren): pass errors to the UI somehow. */ | /* TODO(Sybren): pass errors to the UI somehow. */ | ||||
| return false; | return false; | ||||
| } | } | ||||
| std::ofstream output(dest_file_path); | fstream output(dest_file_path, std::ios::out); | ||||
| /* TODO(@sybren): remember the line ending style that was originally read, then use that to write | /* TODO(@sybren): remember the line ending style that was originally read, then use that to write | ||||
| * the file again. */ | * the file again. */ | ||||
| /* Write the header. */ | /* Write the header. */ | ||||
| output << HEADER; | output << HEADER; | ||||
| output << "" << std::endl; | output << "" << std::endl; | ||||
| output << VERSION_MARKER << SUPPORTED_VERSION << std::endl; | output << VERSION_MARKER << SUPPORTED_VERSION << std::endl; | ||||
| ▲ Show 20 Lines • Show All 138 Lines • Show Last 20 Lines | |||||