CContainer.cpp 2.3 KB

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