OgrePlugins.cpp 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113
  1. /*
  2. * Copyright (C) 2022 The V-Gears Team
  3. *
  4. * This file is part of V-Gears
  5. *
  6. * V-Gears is free software: you can redistribute it and/or modify it under
  7. * terms of the GNU General Public License as published by the Free Software
  8. * Foundation, version 3.0 (GPLv3) of the License.
  9. *
  10. * V-Gears is distributed in the hope that it will be useful,
  11. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  12. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  13. * GNU General Public License for more details.
  14. */
  15. #include "common/OgrePlugins.h"
  16. #include <algorithm>
  17. #include <fstream>
  18. namespace VGears{
  19. namespace OgrePlugins{
  20. namespace bfs = boost::filesystem;
  21. namespace{
  22. std::string Trim(const std::string& text){
  23. const char* blanks = " \t\r\n";
  24. const size_t first = text.find_first_not_of(blanks);
  25. if (first == std::string::npos) return "";
  26. return text.substr(first, text.find_last_not_of(blanks) - first + 1);
  27. }
  28. /**
  29. * Usual directories of the Ogre plugins installed by the system.
  30. */
  31. const std::vector<bfs::path> SYSTEM_DIRECTORIES = {
  32. #ifndef _WIN32
  33. "/usr/lib/x86_64-linux-gnu/OGRE", "/usr/lib/aarch64-linux-gnu/OGRE", "/usr/lib/i386-linux-gnu/OGRE",
  34. "/usr/lib64/OGRE", "/usr/lib/OGRE", "/usr/local/lib/OGRE", "/usr/local/lib64/OGRE"
  35. #endif
  36. };
  37. }
  38. bool ReadConfig(const bfs::path& file, Config& config){
  39. config = Config();
  40. std::ifstream in(file.string());
  41. if (!in.good()) return false;
  42. std::string line;
  43. while (std::getline(in, line)){
  44. line = Trim(line);
  45. if (line.empty() || line[0] == '#') continue;
  46. const size_t equals = line.find('=');
  47. if (equals == std::string::npos) continue;
  48. const std::string key = Trim(line.substr(0, equals));
  49. const std::string value = Trim(line.substr(equals + 1));
  50. if (key == "PluginFolder") config.folder = value;
  51. else if (key == "Plugin" && !value.empty()) config.plugins.push_back(value);
  52. }
  53. return true;
  54. }
  55. std::vector<bfs::path> SearchDirectories(
  56. const bfs::path& config_directory, const std::string& config_folder,
  57. const bfs::path& executable_directory, const std::string& build_directory
  58. ){
  59. std::vector<bfs::path> directories;
  60. auto add = [&directories](const bfs::path& directory){
  61. if (directory.empty()) return;
  62. const bfs::path normal = directory.lexically_normal();
  63. if (std::find(directories.begin(), directories.end(), normal) == directories.end())
  64. directories.push_back(normal);
  65. };
  66. if (!config_folder.empty()) add(bfs::absolute(bfs::path(config_folder), config_directory));
  67. add(config_directory);
  68. if (!executable_directory.empty()){
  69. add(executable_directory);
  70. add(executable_directory / ".." / "lib" / "OGRE");
  71. add(executable_directory / ".." / "lib");
  72. }
  73. if (!build_directory.empty()) add(bfs::path(build_directory));
  74. for (const bfs::path& directory : SYSTEM_DIRECTORIES) add(directory);
  75. return directories;
  76. }
  77. std::vector<std::string> FileNames(const std::string& name, const std::string& ogre_version){
  78. #if defined(_WIN32)
  79. (void)ogre_version;
  80. return {name + ".dll"};
  81. #elif defined(__APPLE__)
  82. (void)ogre_version;
  83. return {name + ".dylib", name + ".framework"};
  84. #else
  85. return {name + ".so." + ogre_version};
  86. #endif
  87. }
  88. bfs::path Find(const std::string& name, const std::vector<bfs::path>& directories, const std::string& ogre_version){
  89. boost::system::error_code ec;
  90. const bfs::path as_path(name);
  91. if (as_path.is_absolute()) return bfs::exists(as_path, ec) ? as_path : bfs::path();
  92. const std::vector<std::string> names = FileNames(name, ogre_version);
  93. for (const bfs::path& directory : directories){
  94. for (const std::string& file_name : names){
  95. const bfs::path file = directory / file_name;
  96. if (bfs::exists(file, ec)) return file;
  97. }
  98. }
  99. return bfs::path();
  100. }
  101. }
  102. }