Вы не можете выбрать более 25 тем Темы должны начинаться с буквы или цифры, могут содержать дефисы(-) и должны содержать не более 35 символов.
 
 
 
 
 
 

173 строки
4.0 KiB

  1. //
  2. // Lol Engine
  3. //
  4. // Copyright: (c) 2010-2013 Sam Hocevar <sam@hocevar.net>
  5. // This program is free software; you can redistribute it and/or
  6. // modify it under the terms of the Do What The Fuck You Want To
  7. // Public License, Version 2, as published by Sam Hocevar. See
  8. // http://www.wtfpl.net/ for more details.
  9. //
  10. #include <lol/engine-internal.h>
  11. #include <cctype>
  12. #if HAVE_UNISTD_H
  13. # include <unistd.h>
  14. #endif
  15. #if _WIN32
  16. # define WIN32_LEAN_AND_MEAN
  17. # include <direct.h>
  18. #endif
  19. namespace lol
  20. {
  21. namespace System
  22. {
  23. #if _WIN32
  24. # define SEPARATOR '\\'
  25. #else
  26. # define SEPARATOR '/'
  27. #endif
  28. static array<String> data_dir;
  29. void Init(int argc, char *argv[],
  30. String const &projectdir,
  31. String const &solutiondir,
  32. String const &sourcesubdir)
  33. {
  34. using namespace std;
  35. Log::Debug("project dir: “%s”\n", projectdir.C());
  36. Log::Debug("solution dir: “%s”\n", solutiondir.C());
  37. Log::Debug("source subdir: “%s”\n", sourcesubdir.C());
  38. /*
  39. * Retrieve binary directory, defaulting to no directory on Android
  40. * and emscripten, and the current directory on other platforms.
  41. */
  42. #if __ANDROID__ || EMSCRIPTEN
  43. String binarydir = "";
  44. #else
  45. String binarydir = ".";
  46. char *cwd = nullptr;
  47. # if HAVE_GETCWD
  48. cwd = getcwd(nullptr, 0);
  49. # elif HAVE__GETCWD || (_WIN32 && !_XBOX)
  50. cwd = _getcwd(nullptr, 0);
  51. # endif
  52. if (cwd)
  53. {
  54. binarydir = cwd;
  55. free(cwd);
  56. }
  57. binarydir += SEPARATOR;
  58. if (argc > 0)
  59. {
  60. char const *last_sep = strrchr(argv[0], SEPARATOR);
  61. if (last_sep)
  62. binarydir = String(argv[0], last_sep - argv[0] + 1);
  63. }
  64. #endif
  65. bool got_rootdir = false;
  66. /*
  67. * If project dir and solution dir are set, add them to data; also
  68. * add current directory in case we were launched from another place.
  69. */
  70. if (!got_rootdir && projectdir.Count() && solutiondir.Count())
  71. {
  72. /* This data dir is for standalone executables */
  73. String rootdir = binarydir;
  74. if (rootdir.Count() && rootdir.Last() != SEPARATOR)
  75. rootdir += SEPARATOR;
  76. AddDataDir(rootdir);
  77. /* This data dir is for engine stuff */
  78. rootdir = solutiondir;
  79. if (rootdir.Count() && rootdir.Last() != SEPARATOR)
  80. rootdir += SEPARATOR;
  81. rootdir += "../src/"; /* FIXME: use SEPARATOR? */
  82. AddDataDir(rootdir);
  83. /* This data dir is for project-specific stuff */
  84. rootdir = projectdir;
  85. if (rootdir.Count() && rootdir.Last() != SEPARATOR)
  86. rootdir += SEPARATOR;
  87. AddDataDir(rootdir);
  88. got_rootdir = true;
  89. }
  90. /*
  91. * If no project dir, use the executable location as the starting point
  92. * to guess the data dir.
  93. */
  94. if (!got_rootdir)
  95. {
  96. /* First climb back the hierarchy to get to the engine root and
  97. * add a data dir for engine stuff. */
  98. String rootdir = binarydir;
  99. if (rootdir.Count() && rootdir.Last() != SEPARATOR)
  100. rootdir += SEPARATOR;
  101. for (int i = 1; i < sourcesubdir.Count(); ++i)
  102. {
  103. if ((sourcesubdir[i] == SEPARATOR
  104. && sourcesubdir[i - 1] != SEPARATOR)
  105. || i == sourcesubdir.Count() - 1)
  106. rootdir += "../";
  107. }
  108. rootdir += "src/";
  109. AddDataDir(rootdir);
  110. /* This data dir is for project-specific stuff */
  111. rootdir = binarydir;
  112. AddDataDir(rootdir);
  113. got_rootdir = true;
  114. }
  115. Log::Debug("binary dir: “%s”\n", binarydir.C());
  116. for (int i = 0; i < data_dir.Count(); ++i)
  117. Log::Debug("data dir %d/%d: “%s”\n", i + 1, data_dir.Count(),
  118. data_dir[i].C());
  119. }
  120. /*
  121. * Data directory handling
  122. */
  123. void AddDataDir(String const &dir)
  124. {
  125. data_dir << dir;
  126. }
  127. array<String> GetPathList(String const &file)
  128. {
  129. array<String> ret;
  130. for (int i = 0; i < data_dir.Count(); ++i)
  131. ret << data_dir[i] + file;
  132. if (ret.Count() == 0)
  133. ret << file;
  134. return ret;
  135. }
  136. } /* namespace System */
  137. } /* namespace lol */