InstallLayout.cpp 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130
  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 <algorithm>
  16. #include <boost/filesystem.hpp>
  17. #include "common/DataDirectory.h"
  18. #include "common/InstallStamp.h"
  19. #include "InstallLayout.h"
  20. namespace InstallLayout{
  21. namespace{
  22. /**
  23. * Removes a file or a directory with its contents, and counts what was removed.
  24. */
  25. void Remove(const boost::filesystem::path& path, CleanResult& result){
  26. boost::system::error_code ec;
  27. const auto removed = boost::filesystem::remove_all(path, ec);
  28. if (ec) result.errors.push_back(path.string() + ": " + ec.message());
  29. else result.removed += removed;
  30. }
  31. }
  32. std::string OriginalDataDir(const std::string& root){return root + "original_data/";}
  33. const std::vector<std::string>& CleanInstallKeptGameDirs(){
  34. static const std::vector<std::string> dirs = {"video"};
  35. return dirs;
  36. }
  37. bool IsInstallation(const std::string& root){
  38. boost::system::error_code ec;
  39. return boost::filesystem::is_regular_file(root + "plugins.cfg", ec)
  40. && boost::filesystem::is_regular_file(root + "resources.cfg", ec);
  41. }
  42. CleanResult CleanInstallation(const std::string& root){
  43. namespace fs = boost::filesystem;
  44. CleanResult result;
  45. if (!IsInstallation(root)) return result;
  46. boost::system::error_code ec;
  47. // The saves, the configuration and the videos of an older version are moved out of the directories that
  48. // are deleted. Nothing is deleted if that fails.
  49. try{
  50. VGears::DataDirectory::MigrateLayout(root);
  51. }
  52. catch (const fs::filesystem_error& e){
  53. result.errors.push_back(e.what());
  54. return result;
  55. }
  56. // "data" is where older versions had the files that are now in "engine" and "game".
  57. for (const char* dir : {"original_data/", "temp/", "data/", "engine/", "defaults/"})
  58. if (fs::exists(root + dir, ec)) Remove(root + dir, result);
  59. // The record of the installations describes what was just deleted.
  60. if (fs::exists(root + VGears::InstallStamp::FILE_NAME, ec)) Remove(root + VGears::InstallStamp::FILE_NAME, result);
  61. const fs::path game(GameDir(root));
  62. if (fs::is_directory(game, ec)){
  63. const std::vector<std::string>& kept = CleanInstallKeptGameDirs();
  64. std::vector<fs::path> entries;
  65. for (fs::directory_iterator it(game, ec), end; !ec && it != end; it.increment(ec))
  66. entries.push_back(it->path());
  67. if (ec) result.errors.push_back(game.string() + ": " + ec.message());
  68. for (const fs::path& entry : entries){
  69. if (std::find(kept.begin(), kept.end(), entry.filename().string()) == kept.end())
  70. Remove(entry, result);
  71. }
  72. }
  73. return result;
  74. }
  75. std::string GameDir(const std::string& root){return root + "game/";}
  76. std::string TempDir(const std::string& root){return root + "temp/";}
  77. const std::vector<std::string>& GameSubdirs(){
  78. static const std::vector<std::string> dirs = {
  79. "kernel", "field/maps", "field/models", "battle/enemies", "battle/attacks", "battle/formations",
  80. "battle/effects", "battle/scripts", "battle/images", "battle/models/characters", "battle/models/scenes",
  81. "battle/models/enemies", "battle/models/attacks", "battle/models/effects", "world/maps", "world/textures",
  82. "world/scripts", "world/models", "ui/images/icons", "ui/images/other",
  83. "ui/images/characters", "ui/images/fonts", "ui/images/reels", "ui/images/window", "ui/fonts",
  84. "audio/music", "audio/sounds"
  85. };
  86. return dirs;
  87. }
  88. const std::vector<std::string>& TempSubdirs(){
  89. static const std::vector<std::string> dirs = {"char", "battle_models", "world_models", "spell_models", "effects"};
  90. return dirs;
  91. }
  92. const std::vector<std::string>& GameResourceDirs(){
  93. static const std::vector<std::string> dirs = {
  94. "engine/system", "engine/fonts", "engine/images", "engine/ui", "engine/scripts", "engine/texts",
  95. "game/kernel", "game/field/maps", "game/field/models", "game/battle/models", "game/battle/images",
  96. "game/world/maps", "game/world/textures", "game/world/models", "game/ui/images", "game/ui/fonts",
  97. "game/audio"
  98. };
  99. return dirs;
  100. }
  101. std::string MigrateInstallDir(
  102. std::string legacy_data_dir, const std::string& legacy_config_dir, const std::string& default_dir
  103. ){
  104. while (!legacy_data_dir.empty() && legacy_data_dir.back() == '/') legacy_data_dir.pop_back();
  105. const std::string suffix = "/data";
  106. if (
  107. legacy_data_dir.size() >= suffix.size()
  108. && legacy_data_dir.compare(legacy_data_dir.size() - suffix.size(), suffix.size(), suffix) == 0
  109. ) legacy_data_dir.erase(legacy_data_dir.size() - suffix.size());
  110. if (!legacy_data_dir.empty()) return legacy_data_dir;
  111. if (!legacy_config_dir.empty()) return legacy_config_dir;
  112. return default_dir;
  113. }
  114. }