CContainer.cpp 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687
  1. #include "CContainer.h"
  2. #include "CAssetLoadException.h"
  3. #include "CTexture.h"
  4. #include "WallpaperEngine/Logging/CLog.h"
  5. #include <cstring>
  6. #include <filesystem>
  7. #include <utility>
  8. using namespace WallpaperEngine::Assets;
  9. std::filesystem::path CContainer::resolveRealFile (const std::string& filename) const {
  10. throw CAssetLoadException (filename, "Cannot resolve physical file in this container");
  11. }
  12. const ITexture* CContainer::readTexture (const std::string& filename) const {
  13. // get the texture's filename (usually .tex)
  14. const std::string texture = "materials/" + filename + ".tex";
  15. const void* textureContents = this->readFile (texture, nullptr);
  16. const ITexture* result = new CTexture (textureContents);
  17. #if !NDEBUG
  18. glObjectLabel (GL_TEXTURE, result->getTextureID (), -1, texture.c_str ());
  19. #endif /* NDEBUG */
  20. return result;
  21. }
  22. std::string CContainer::readShader (const std::string& filename) const {
  23. std::filesystem::path shader = filename;
  24. auto it = shader.begin ();
  25. // detect workshop shaders and check if there's a
  26. if (*it++ == "workshop") {
  27. const std::filesystem::path workshopId = *it++;
  28. if (++it != shader.end ()) {
  29. const std::filesystem::path shaderfile = *it;
  30. try {
  31. shader = std::filesystem::path ("zcompat") / "scene" / "shaders" / workshopId / shaderfile;
  32. // replace the old path with the new one
  33. std::string contents = this->readFileAsString (shader);
  34. sLog.out ("Replaced ", filename, " with compat ", shader);
  35. return contents;
  36. } catch (CAssetLoadException&) {}
  37. }
  38. }
  39. return this->readFileAsString ("shaders/" + filename);
  40. }
  41. std::string CContainer::readVertexShader (const std::string& filename) const {
  42. return this->readShader (filename + ".vert");
  43. }
  44. std::string CContainer::readFragmentShader (const std::string& filename) const {
  45. return this->readShader (filename + ".frag");
  46. }
  47. std::string CContainer::readIncludeShader (const std::string& filename) const {
  48. return this->readFileAsString ("shaders/" + filename);
  49. }
  50. std::string CContainer::readFileAsString (const std::string& filename) const {
  51. uint32_t length = 0;
  52. // read file contents and allocate a buffer for a string
  53. const void* contents = this->readFile (std::move (filename), &length);
  54. char* buffer = new char [length + 1];
  55. // ensure there's a 0 at the end
  56. memset (buffer, 0, length + 1);
  57. // copy over the data
  58. memcpy (buffer, contents, length);
  59. // now build the std::string to use
  60. std::string result = buffer;
  61. // free the intermediate buffer used to generate the std::string
  62. delete [] buffer;
  63. return result;
  64. }