profile_manager.cpp 2.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687
  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. });
  52. std::sort(ids.begin(), ids.end());
  53. std::vector<Profile*> result;
  54. for (const int& id : ids)
  55. result.push_back(&get_profile(id));
  56. return result;
  57. }
  58. void
  59. ProfileManager::reset_profile(int id)
  60. {
  61. physfsutil::remove_content("profile" + std::to_string(id));
  62. get_profile(id).reset();
  63. }
  64. void
  65. ProfileManager::delete_profile(int id)
  66. {
  67. physfsutil::remove_with_content("profile" + std::to_string(id));
  68. auto it = m_profiles.find(id);
  69. if (it != m_profiles.end())
  70. m_profiles.erase(it);
  71. }
  72. /* EOF */