b3ResourcePath.cpp 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899
  1. #include "b3ResourcePath.h"
  2. #include "Bullet3Common/b3Logging.h"
  3. #ifdef __APPLE__
  4. #include <mach-o/dyld.h> /* _NSGetExecutablePath */
  5. #else
  6. #ifdef _WIN32
  7. #include <windows.h>
  8. #else
  9. //not Mac, not Windows, let's cross the fingers it is Linux :-)
  10. #include <unistd.h>
  11. #endif
  12. #endif
  13. #include "Bullet3Common/b3FileUtils.h"
  14. #define B3_MAX_EXE_PATH_LEN 4096
  15. int b3ResourcePath::getExePath(char* path, int maxPathLenInBytes)
  16. {
  17. int numBytes = 0;
  18. #if __APPLE__
  19. uint32_t bufsize = uint32_t(maxPathLenInBytes);
  20. if (_NSGetExecutablePath(path, &bufsize)!=0)
  21. {
  22. b3Warning("Cannot find executable path\n");
  23. return false;
  24. } else
  25. {
  26. numBytes = strlen(path);
  27. }
  28. #else
  29. #ifdef _WIN32
  30. //https://msdn.microsoft.com/en-us/library/windows/desktop/ms683197(v=vs.85).aspx
  31. HMODULE hModule = GetModuleHandle(NULL);
  32. numBytes = GetModuleFileNameA(hModule, path, maxPathLenInBytes);
  33. #else
  34. ///http://stackoverflow.com/questions/933850/how-to-find-the-location-of-the-executable-in-c
  35. numBytes = (int)readlink("/proc/self/exe", path, maxPathLenInBytes-1);
  36. if (numBytes > 0)
  37. {
  38. path[numBytes] = 0;
  39. } else
  40. {
  41. b3Warning("Cannot find executable path\n");
  42. }
  43. #endif //_WIN32
  44. #endif //__APPLE__
  45. return numBytes;
  46. }
  47. int b3ResourcePath::findResourcePath(const char* resourceName, char* resourcePath, int resourcePathMaxNumBytes)
  48. {
  49. //first find in a resource/<exeName> location, then in various folders within 'data' using b3FileUtils
  50. char exePath[B3_MAX_EXE_PATH_LEN];
  51. int l = b3ResourcePath::getExePath(exePath, B3_MAX_EXE_PATH_LEN);
  52. if (l)
  53. {
  54. char pathToExe[B3_MAX_EXE_PATH_LEN];
  55. int exeNamePos = b3FileUtils::extractPath(exePath,pathToExe,B3_MAX_EXE_PATH_LEN);
  56. if (exeNamePos)
  57. {
  58. sprintf(resourcePath,"%s../data/%s",pathToExe,resourceName);
  59. //printf("try resource at %s\n", resourcePath);
  60. if (b3FileUtils::findFile(resourcePath, resourcePath, resourcePathMaxNumBytes))
  61. {
  62. return strlen(resourcePath);
  63. }
  64. sprintf(resourcePath,"%s../resources/%s/%s",pathToExe,&exePath[exeNamePos],resourceName);
  65. //printf("try resource at %s\n", resourcePath);
  66. if (b3FileUtils::findFile(resourcePath, resourcePath, resourcePathMaxNumBytes))
  67. {
  68. return strlen(resourcePath);
  69. }
  70. sprintf(resourcePath,"%s.runfiles/google3/third_party/bullet/data/%s",exePath,resourceName);
  71. //printf("try resource at %s\n", resourcePath);
  72. if (b3FileUtils::findFile(resourcePath, resourcePath, resourcePathMaxNumBytes))
  73. {
  74. return strlen(resourcePath);
  75. }
  76. }
  77. }
  78. bool res = b3FileUtils::findFile(resourceName, resourcePath, resourcePathMaxNumBytes);
  79. if (res)
  80. {
  81. return strlen(resourcePath);
  82. }
  83. return 0;
  84. }