From da7c3a50c1972b748a377f44b1517f9930746705 Mon Sep 17 00:00:00 2001 From: devZoGok Date: Mon, 2 Jan 2023 14:56:49 +0200 Subject: [PATCH] Corrected individual asset path reading AssetManagerTest class --- CMakeLists.txt | 4 +-- assetManager.cpp | 49 +++++++++++++++++++------------ assetManager.h | 1 + assetManagerTest.cpp | 48 ++++++++++++++++++++++++++++++ assetManagerTest.h | 26 ++++++++++++++++ main.cpp | 2 ++ tests/fileLoad/a.png | 0 tests/fileLoad/b.abc | 0 tests/recursiveLoad/folderA/a.png | 0 tests/recursiveLoad/folderB/b.png | 0 10 files changed, 110 insertions(+), 20 deletions(-) create mode 100644 assetManagerTest.cpp create mode 100644 assetManagerTest.h create mode 100644 tests/fileLoad/a.png create mode 100644 tests/fileLoad/b.abc create mode 100644 tests/recursiveLoad/folderA/a.png create mode 100644 tests/recursiveLoad/folderB/b.png diff --git a/CMakeLists.txt b/CMakeLists.txt index 1a47c84..5e432b8 100755 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -27,7 +27,6 @@ endif() set(LIB_SRC ${RENDER} ${MATH} ${UTILS} ${GUI} ${ARMATURE} ${ASSET_MANAGER} ${ASSET_READERS} ${ANIM}) - include_directories(external/tinydir) include_directories(external/glm) include_directories(external/glm/glm) @@ -88,7 +87,8 @@ if(BUILD_TESTS) set(RENDER_TEST nodeTest.cpp cameraTest.cpp shaderTest.cpp particleEmitterTest.cpp) set(ARMATURE_TEST boneTest.cpp ikSolverTest.cpp) set(ANIMATION_TEST animationChannelTest.cpp) - set(TEST_SRC main.cpp ${LIB_SRC} ${RENDER_TEST} ${ARMATURE_TEST} ${ANIMATION_TEST}) + set(ASSET_MANAGER_TEST assetManagerTest.cpp) + set(TEST_SRC main.cpp ${LIB_SRC} ${RENDER_TEST} ${ARMATURE_TEST} ${ANIMATION_TEST} ${ASSET_MANAGER_TEST}) add_executable(vb01Tests ${TEST_SRC}) target_link_libraries(vb01Tests ${LIB_NAME} ${DEPS} cppunit) diff --git a/assetManager.cpp b/assetManager.cpp index c9b4c72..63b106b 100644 --- a/assetManager.cpp +++ b/assetManager.cpp @@ -8,6 +8,7 @@ #include #include +#include using namespace std; @@ -23,46 +24,58 @@ namespace vb01{ void AssetManager::readDir(string path, vector &contents, bool recursive){ tinydir_dir dir; - tinydir_open_sorted(&dir, path.c_str()); + + if(tinydir_open_sorted(&dir, path.c_str()) == -1){ + contents.push_back(path); + return; + } for(int i = 0; i < dir.n_files; i++){ tinydir_file file; tinydir_readfile_n(&dir, &file, i); + if(file.name[0] == '.') + continue; + if(file.is_dir && recursive) - readDir(path + file.name, contents, recursive); + readDir(path + file.name + "/", contents, recursive); else if(!file.is_dir) - contents.push_back(file.name); + contents.push_back(path + file.name); } tinydir_close(&dir); } void AssetManager::load(string path, bool recursive){ - if(getAsset(path)) - return; - vector files; readDir(path, files, recursive); - string format = path.substr(path.find_last_of(".") + 1, string::npos); - AbstractAssetReader *assetReader = nullptr; - vector imageFormats = vector{"png", "jpeg", "jpg"}; + for(string file : files){ + if(getAsset(file)) + continue; - if(find(imageFormats.begin(), imageFormats.end(), format) != imageFormats.end()) - assetReader = ImageReader::getSingleton(); + string format = file.substr(file.find_last_of(".") + 1, string::npos); + AbstractAssetReader *assetReader = nullptr; + vector imageFormats = vector{"png", "jpeg", "jpg"}; - vector fontFormats = vector{"ttf"}; + if(find(imageFormats.begin(), imageFormats.end(), format) != imageFormats.end()) + assetReader = ImageReader::getSingleton(); - if(find(fontFormats.begin(), fontFormats.end(), format) != fontFormats.end()) - assetReader = FontReader::getSingleton(); + vector fontFormats = vector{"ttf"}; - vector modelFormats = vector{"xml"}; + if(find(fontFormats.begin(), fontFormats.end(), format) != fontFormats.end()) + assetReader = FontReader::getSingleton(); - if(find(modelFormats.begin(), modelFormats.end(), format) != modelFormats.end()) - assetReader = XmlModelReader::getSingleton(); + vector modelFormats = vector{"xml"}; - assets.push_back(assetReader->readAsset(path)); + if(find(modelFormats.begin(), modelFormats.end(), format) != modelFormats.end()) + assetReader = XmlModelReader::getSingleton(); + + if(assetReader) + assets.push_back(assetReader->readAsset(file)); + else + cout << "No asset reader for " << file << endl; + } } Asset* AssetManager::getAsset(string path){ diff --git a/assetManager.h b/assetManager.h index 7a4eb97..b859551 100644 --- a/assetManager.h +++ b/assetManager.h @@ -13,6 +13,7 @@ namespace vb01{ static AssetManager* getSingleton(); void load(std::string, bool = false); Asset* getAsset(std::string); + inline std::vector getAssets(){return assets;} private: AssetManager(){} void readDir(std::string, std::vector&, bool); diff --git a/assetManagerTest.cpp b/assetManagerTest.cpp new file mode 100644 index 0000000..f9d4593 --- /dev/null +++ b/assetManagerTest.cpp @@ -0,0 +1,48 @@ +#include "assetManagerTest.h" +#include "assetManager.h" + +#include +#include + +namespace vb01{ + using namespace std; + + void AssetManagerTest::setUp(){ + assetManager = AssetManager::getSingleton(); + + string basePath = "../tests/"; + recursiveLoadPath = basePath + "recursiveLoad/"; + fileLoadPath = basePath + "fileLoad/"; + } + + void AssetManagerTest::tearDown(){ + } + + void AssetManagerTest::testLoad(){ + vector recursiveLoadFiles = vector{"folderA/a.png", "folderB/b.png"}; + assetManager->load(recursiveLoadPath, true); + + for(string file : recursiveLoadFiles){ + Asset *asset = assetManager->getAsset(recursiveLoadPath + file); + CPPUNIT_ASSERT(asset != nullptr); + } + + string file = fileLoadPath + "a.png"; + assetManager->load(file); + CPPUNIT_ASSERT(assetManager->getAsset(file) != nullptr); + + assetManager->load(file); + vector assets = assetManager->getAssets(); + int numSameAssets = 0; + + for(int i = 0; i < assets.size(); i++) + if(assets[i]->path == file) + numSameAssets++; + + CPPUNIT_ASSERT(numSameAssets == 1); + + string nonReadableFile = fileLoadPath + "b.abc"; + assetManager->load(nonReadableFile); + CPPUNIT_ASSERT(!assetManager->getAsset(nonReadableFile)); + } +} diff --git a/assetManagerTest.h b/assetManagerTest.h new file mode 100644 index 0000000..f068a00 --- /dev/null +++ b/assetManagerTest.h @@ -0,0 +1,26 @@ +#ifndef ASSET_MANAGER_TEST_H +#define ASSET_MANAGER_TEST_H + +#include +#include + +namespace vb01{ + class AssetManager; + + class AssetManagerTest : public CppUnit::TestFixture{ + CPPUNIT_TEST_SUITE(AssetManagerTest); + CPPUNIT_TEST(testLoad); + CPPUNIT_TEST_SUITE_END(); + + public: + AssetManagerTest(){} + void setUp(); + void tearDown(); + void testLoad(); + private: + AssetManager *assetManager = nullptr; + std::string recursiveLoadPath, fileLoadPath; + }; +} + +#endif diff --git a/main.cpp b/main.cpp index 8ff7e1a..50895b8 100755 --- a/main.cpp +++ b/main.cpp @@ -10,6 +10,7 @@ #include "shaderTest.h" #include "particleEmitterTest.h" #include "ikSolverTest.h" +#include "assetManagerTest.h" using namespace CppUnit; using namespace vb01; @@ -23,6 +24,7 @@ int main(){ runner.addTest(ShaderTest::suite()); runner.addTest(ParticleEmitterTest::suite()); runner.addTest(IkSolverTest::suite()); + runner.addTest(AssetManagerTest::suite()); runner.run(); return 0; } diff --git a/tests/fileLoad/a.png b/tests/fileLoad/a.png new file mode 100644 index 0000000..e69de29 diff --git a/tests/fileLoad/b.abc b/tests/fileLoad/b.abc new file mode 100644 index 0000000..e69de29 diff --git a/tests/recursiveLoad/folderA/a.png b/tests/recursiveLoad/folderA/a.png new file mode 100644 index 0000000..e69de29 diff --git a/tests/recursiveLoad/folderB/b.png b/tests/recursiveLoad/folderB/b.png new file mode 100644 index 0000000..e69de29