AssetStream.cpp 1.5 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556
  1. // Jolt Physics Library (https://github.com/jrouwe/JoltPhysics)
  2. // SPDX-FileCopyrightText: 2025 Jorrit Rouwe
  3. // SPDX-License-Identifier: MIT
  4. #include <TestFramework.h>
  5. #include <Utils/AssetStream.h>
  6. #include <Utils/Log.h>
  7. JPH_SUPPRESS_WARNINGS_STD_BEGIN
  8. #include <filesystem>
  9. #ifdef JPH_PLATFORM_LINUX
  10. #include <unistd.h>
  11. #endif
  12. JPH_SUPPRESS_WARNINGS_STD_END
  13. String AssetStream::sGetAssetsBasePath()
  14. {
  15. static String result = []() {
  16. // Start with the application path
  17. #ifdef JPH_PLATFORM_WINDOWS
  18. char application_path[MAX_PATH] = { 0 };
  19. GetModuleFileName(nullptr, application_path, MAX_PATH);
  20. #elif defined(JPH_PLATFORM_LINUX)
  21. char application_path[PATH_MAX] = { 0 };
  22. int count = readlink("/proc/self/exe", application_path, PATH_MAX);
  23. if (count > 0)
  24. application_path[count] = 0;
  25. #else
  26. #error Unsupported platform
  27. #endif
  28. // Find the asset path
  29. filesystem::path asset_path(application_path);
  30. while (!asset_path.empty())
  31. {
  32. filesystem::path parent_path = asset_path.parent_path();
  33. if (parent_path == asset_path)
  34. break;
  35. asset_path = parent_path;
  36. if (filesystem::exists(asset_path / "Assets"))
  37. break;
  38. }
  39. asset_path /= "Assets";
  40. asset_path /= "";
  41. return String(asset_path.string());
  42. }();
  43. return result;
  44. }
  45. AssetStream::AssetStream(const char *inFileName, std::ios_base::openmode inOpenMode) :
  46. mStream((sGetAssetsBasePath() + inFileName).c_str(), inOpenMode)
  47. {
  48. if (!mStream.is_open())
  49. FatalError("Failed to open file %s", inFileName);
  50. }