profile_manager.cpp 2.0 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788
  1. // SuperTux
  2. // Copyright (C) 2023 Vankata453
  3. //
  4. // This program is free software: you can redistribute it and/or modify
  5. // it under the terms of the GNU General Public License as published by
  6. // the Free Software Foundation, either version 3 of the License, or
  7. // (at your option) any later version.
  8. //
  9. // This program is distributed in the hope that it will be useful,
  10. // but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  12. // GNU General Public License for more details.
  13. //
  14. // You should have received a copy of the GNU General Public License
  15. // along with this program. If not, see <http://www.gnu.org/licenses/>.
  16. #include "supertux/profile_manager.hpp"
  17. #include <algorithm>
  18. #include "physfs/util.hpp"
  19. #include "supertux/gameconfig.hpp"
  20. #include "supertux/globals.hpp"
  21. ProfileManager::ProfileManager() :
  22. m_profiles()
  23. {
  24. }
  25. Profile&
  26. ProfileManager::get_current_profile()
  27. {
  28. return get_profile(g_config->profile);
  29. }
  30. Profile&
  31. ProfileManager::get_profile(int id)
  32. {
  33. auto it = m_profiles.find(id);
  34. if (it != m_profiles.end())
  35. {
  36. return *it->second;
  37. }
  38. else
  39. {
  40. m_profiles[id] = std::make_unique<Profile>(id);
  41. return *m_profiles[id];
  42. }
  43. }
  44. std::vector<Profile*>
  45. ProfileManager::get_profiles()
  46. {
  47. std::vector<int> ids;
  48. physfsutil::enumerate_files("/", [&ids](const std::string& filename) {
  49. if (filename.substr(0, 7) == "profile")
  50. ids.push_back(std::stoi(filename.substr(7)));
  51. return false;
  52. });
  53. std::sort(ids.begin(), ids.end());
  54. std::vector<Profile*> result;
  55. for (const int& id : ids)
  56. result.push_back(&get_profile(id));
  57. return result;
  58. }
  59. void
  60. ProfileManager::reset_profile(int id)
  61. {
  62. physfsutil::remove_content("profile" + std::to_string(id));
  63. get_profile(id).reset();
  64. }
  65. void
  66. ProfileManager::delete_profile(int id)
  67. {
  68. physfsutil::remove_with_content("profile" + std::to_string(id));
  69. auto it = m_profiles.find(id);
  70. if (it != m_profiles.end())
  71. m_profiles.erase(it);
  72. }
  73. /* EOF */