add func to path manager. fix bitmap handler. add general bitmap handler getter
This commit is contained in:
@ -10,7 +10,7 @@ namespace LibCmo::CK2::MgrImpls {
|
||||
|
||||
CKPathManager::CKPathManager(CKContext* ctx) :
|
||||
CKBaseManager(ctx, PATH_MANAGER_GUID, "Path Manager"),
|
||||
m_TempFolder() {
|
||||
m_TempFolder(), m_ExtraPathes() {
|
||||
// preset for temp folder
|
||||
// todo: add current CKContext pointer as the part of temp path.
|
||||
// thus multiple CKContext can work.
|
||||
@ -21,8 +21,15 @@ namespace LibCmo::CK2::MgrImpls {
|
||||
}
|
||||
CKPathManager::~CKPathManager() {}
|
||||
|
||||
void CKPathManager::SetTempFolder(CKSTRING u8_temp) {
|
||||
EncodingHelper::U8PathToStdPath(this->m_TempFolder, u8_temp);
|
||||
bool CKPathManager::SetTempFolder(CKSTRING u8_temp) {
|
||||
std::filesystem::path cache;
|
||||
EncodingHelper::U8PathToStdPath(cache, u8_temp);
|
||||
if (std::filesystem::is_directory(cache)) {
|
||||
m_TempFolder = cache;
|
||||
return true;
|
||||
} else {
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
std::string CKPathManager::GetTempFolder() {
|
||||
@ -41,8 +48,49 @@ namespace LibCmo::CK2::MgrImpls {
|
||||
return result;
|
||||
}
|
||||
|
||||
bool CKPathManager::AddPath(CKSTRING u8path) {
|
||||
if (u8path == nullptr) return;
|
||||
std::filesystem::path newpath;
|
||||
EncodingHelper::U8PathToStdPath(newpath, u8path);
|
||||
if (std::filesystem::is_directory(newpath)) {
|
||||
m_ExtraPathes.emplace_back(std::move(newpath));
|
||||
return true;
|
||||
} else {
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
void CKPathManager::ClearPath() {
|
||||
m_ExtraPathes.clear();
|
||||
}
|
||||
|
||||
bool CKPathManager::ResolveFileName(std::string& u8_filename) {
|
||||
// todo: finish resolve file name
|
||||
std::filesystem::path filepath;
|
||||
EncodingHelper::U8PathToStdPath(filepath, u8_filename.c_str());
|
||||
|
||||
// if it is absolute path, return it directly
|
||||
if (filepath.is_absolute()) {
|
||||
return true;
|
||||
}
|
||||
|
||||
// otherwise check it in extra path
|
||||
for (const auto& extrapath : m_ExtraPathes) {
|
||||
auto combinedpath = extrapath / filepath;
|
||||
if (std::filesystem::is_regular_file(combinedpath)) {
|
||||
// this is correct
|
||||
EncodingHelper::StdPathToU8Path(u8_filename, combinedpath);
|
||||
return true;
|
||||
}
|
||||
}
|
||||
|
||||
// test in temp folder
|
||||
auto tempfile = m_TempFolder / filepath;
|
||||
if (std::filesystem::is_regular_file(tempfile)) {
|
||||
EncodingHelper::StdPathToU8Path(u8_filename, combinedpath);
|
||||
return true;
|
||||
}
|
||||
|
||||
// failed
|
||||
return false;
|
||||
}
|
||||
|
||||
|
@ -14,9 +14,10 @@ namespace LibCmo::CK2::MgrImpls {
|
||||
|
||||
/**
|
||||
* @brief Set the temp folder of current context.
|
||||
* @param u8_temp
|
||||
* @param u8_temp The temp folder you need to assign
|
||||
* @return true if success.
|
||||
*/
|
||||
void SetTempFolder(CKSTRING u8_temp);
|
||||
bool SetTempFolder(CKSTRING u8_temp);
|
||||
/**
|
||||
* @brief Get current temp folder.
|
||||
* @return
|
||||
@ -29,15 +30,32 @@ namespace LibCmo::CK2::MgrImpls {
|
||||
*/
|
||||
std::string GetTempFilePath(CKSTRING u8_filename);
|
||||
|
||||
/**
|
||||
* @brief Add extra path for ResolveFileName
|
||||
* @param u8path The added path.
|
||||
* @return true if success.
|
||||
*/
|
||||
bool AddPath(CKSTRING u8path);
|
||||
/**
|
||||
* @brief Clear all extra path.
|
||||
*/
|
||||
void ClearPath();
|
||||
|
||||
/**
|
||||
* @brief Finds a file in the paths
|
||||
* @param u8_filename[inout] The given file path. overwritten by the final path if success.
|
||||
* @remark
|
||||
* We match file in following order.
|
||||
* + Whether given file is absolute path. return if true.
|
||||
* + User provided extra path.
|
||||
* + Virtools temp folder.
|
||||
* @return true if success
|
||||
*/
|
||||
bool ResolveFileName(std::string& u8_filename);
|
||||
|
||||
protected:
|
||||
std::filesystem::path m_TempFolder;
|
||||
XContainer::XArray<std::filesystem::path> m_ExtraPathes;
|
||||
};
|
||||
|
||||
}
|
||||
|
Reference in New Issue
Block a user