diff --git a/CMakeLists.txt b/CMakeLists.txt index f12fcba..7284966 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -39,6 +39,8 @@ add_executable( wallengine main.cpp + src/WallpaperEngine/Assets/CAssetLoadException.cpp + src/WallpaperEngine/Assets/CAssetLoadException.h src/WallpaperEngine/Assets/CContainer.h src/WallpaperEngine/Assets/CContainer.cpp src/WallpaperEngine/Assets/CCombinedContainer.h diff --git a/main.cpp b/main.cpp index 01a5122..36101e6 100644 --- a/main.cpp +++ b/main.cpp @@ -151,10 +151,12 @@ int main (int argc, char* argv[]) // add the package to the list containers->add (new WallpaperEngine::Assets::CPackage (scene_path)); + std::cout << "Detected scene.pkg file at " << scene_path << ". Adding to list of searchable paths" << std::endl; } catch(std::filesystem::filesystem_error ex) { // ignore this error, the package file was not found + std::cout << "No scene.pkg file found at " << path << ". Defaulting to normal folder storage" << std::endl; } catch (std::runtime_error ex) { @@ -208,7 +210,6 @@ int main (int argc, char* argv[]) return 3; } - if (project->getType () == "scene") { WallpaperEngine::Core::CScene* scene = project->getWallpaper ()->as (); diff --git a/src/WallpaperEngine/Assets/CAssetLoadException.cpp b/src/WallpaperEngine/Assets/CAssetLoadException.cpp new file mode 100644 index 0000000..f6e70f0 --- /dev/null +++ b/src/WallpaperEngine/Assets/CAssetLoadException.cpp @@ -0,0 +1,14 @@ +#include "CAssetLoadException.h" + +using namespace WallpaperEngine::Assets; + +CAssetLoadException::CAssetLoadException(const std::string& filename, const std::string& extrainfo) + : m_message("Cannot find file " + filename + ": " + extrainfo) +{ + +} + +const char *CAssetLoadException::what() const noexcept +{ + return this->m_message.c_str (); +} diff --git a/src/WallpaperEngine/Assets/CAssetLoadException.h b/src/WallpaperEngine/Assets/CAssetLoadException.h new file mode 100644 index 0000000..a6cd511 --- /dev/null +++ b/src/WallpaperEngine/Assets/CAssetLoadException.h @@ -0,0 +1,17 @@ +#pragma once + +#include +#include + +namespace WallpaperEngine::Assets +{ + class CAssetLoadException : public std::exception + { + public: + CAssetLoadException(const std::string& filename, const std::string& extrainfo = ""); + const char* what() const noexcept override; + + private: + std::string m_message; + }; +}; \ No newline at end of file diff --git a/src/WallpaperEngine/Assets/CCombinedContainer.cpp b/src/WallpaperEngine/Assets/CCombinedContainer.cpp index ad710e8..b78a911 100644 --- a/src/WallpaperEngine/Assets/CCombinedContainer.cpp +++ b/src/WallpaperEngine/Assets/CCombinedContainer.cpp @@ -1,4 +1,5 @@ #include "CCombinedContainer.h" +#include "CAssetLoadException.h" using namespace WallpaperEngine::Assets; @@ -20,12 +21,12 @@ void* CCombinedContainer::readFile (std::string filename, uint32_t* length) // an exception will be thrown return (*cur)->readFile (filename, length); } - catch (std::exception& ex) + catch (CAssetLoadException& ex) { // not found in this container, next try } } // no container was able to load the file, abort! - throw std::runtime_error ("Cannot find the file in any of the containers"); + throw CAssetLoadException (filename, "Cannot find file in any of the containers"); } \ No newline at end of file diff --git a/src/WallpaperEngine/Assets/CDirectory.cpp b/src/WallpaperEngine/Assets/CDirectory.cpp index fc7d559..0db9340 100644 --- a/src/WallpaperEngine/Assets/CDirectory.cpp +++ b/src/WallpaperEngine/Assets/CDirectory.cpp @@ -1,10 +1,21 @@ +#include + #include "CDirectory.h" +#include "CAssetLoadException.h" using namespace WallpaperEngine::Assets; CDirectory::CDirectory (std::string basepath) : m_basepath (std::move (basepath)) { + // ensure the specified path exists + struct stat buffer; + + if (stat (this->m_basepath.c_str (), &buffer) != 0) + throw std::runtime_error ("Cannot find " + this->m_basepath + ". This folder is required for wallpaper engine to work"); + + if (!S_ISDIR(buffer.st_mode)) + throw std::runtime_error ("Cannot find " + this->m_basepath + ". There's an assets file in it's place"); } CDirectory::~CDirectory () @@ -30,7 +41,7 @@ void* CDirectory::readFile (std::string filename, uint32_t* length) FILE* fp = fopen (final.c_str (), "rb"); if (fp == nullptr) - throw std::runtime_error ("Cannot find requested file"); + throw CAssetLoadException(filename, "Cannot find file"); // go to the end, get the position and return to the beginning fseek (fp, 0, SEEK_END); @@ -43,7 +54,7 @@ void* CDirectory::readFile (std::string filename, uint32_t* length) if (fread (contents, size, 1, fp) != 1) { delete[] contents; - throw std::runtime_error ("Unexpected error when reading the file"); + throw CAssetLoadException (filename, "Unexpected error when reading the file"); } // store it in the cache too diff --git a/src/WallpaperEngine/Assets/CPackage.cpp b/src/WallpaperEngine/Assets/CPackage.cpp index 9a31a76..ae31ebe 100644 --- a/src/WallpaperEngine/Assets/CPackage.cpp +++ b/src/WallpaperEngine/Assets/CPackage.cpp @@ -3,6 +3,7 @@ // #include "CPackage.h" +#include "CAssetLoadException.h" #include #include @@ -40,7 +41,7 @@ void* CPackage::readFile (std::string filename, uint32_t* length) auto it = this->m_contents.find (filename); if (it == this->m_contents.end ()) - throw std::runtime_error ("Cannot find file in file list"); + throw CAssetLoadException(filename, "Cannot find the file in the package"); // set file length if required if (length != nullptr)