InstallVerifier.cpp 8.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187
  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 "InstallVerifier.h"
  16. #include <boost/filesystem.hpp>
  17. namespace fs = boost::filesystem;
  18. namespace{
  19. /**
  20. * Collects the results of the checks.
  21. */
  22. class Checker{
  23. public:
  24. Checker(const std::string& root, VerifyResult& result): root_(root), result_(result){
  25. if (!root_.empty() && root_.back() != '/') root_ += "/";
  26. }
  27. /**
  28. * A file must exist.
  29. */
  30. void File(const std::string& group, const std::string& path){
  31. ++ result_.checked;
  32. boost::system::error_code ec;
  33. if (!fs::is_regular_file(root_ + path, ec)) Fail(group, path + " is missing");
  34. }
  35. /**
  36. * A file whose name starts with a prefix (a library with a version number) must exist in a directory.
  37. */
  38. void FileWithPrefix(const std::string& group, const std::string& dir, const std::string& prefix){
  39. ++ result_.checked;
  40. boost::system::error_code ec;
  41. for (fs::directory_iterator it(root_ + dir, ec), end; !ec && it != end; it.increment(ec)){
  42. if (it->path().filename().string().compare(0, prefix.size(), prefix) == 0) return;
  43. }
  44. Fail(group, "no " + prefix + "* file in " + (dir.empty() ? "the installation directory" : dir));
  45. }
  46. /**
  47. * A directory must have at least a number of files with an extension, searching in subdirectories.
  48. *
  49. * @param[in] extension Extension with the dot, or the full file name if it has none (like "map.xml").
  50. * An empty string matches every file.
  51. */
  52. void Files(
  53. const std::string& group, const std::string& dir, const std::string& extension, int minimum
  54. ){
  55. ++ result_.checked;
  56. int found = 0;
  57. boost::system::error_code ec;
  58. for (fs::recursive_directory_iterator it(root_ + dir, ec), end; !ec && it != end; it.increment(ec)){
  59. if (!fs::is_regular_file(it->path(), ec)) continue;
  60. const std::string name = it->path().filename().string();
  61. if (
  62. extension.empty() || (name.size() >= extension.size()
  63. && name.compare(name.size() - extension.size(), extension.size(), extension) == 0)
  64. ){
  65. if (++ found >= minimum) return;
  66. }
  67. }
  68. std::string wanted = extension.empty() ? "files" : extension + " files";
  69. Fail(
  70. group, dir + " has " + std::to_string(found) + " " + wanted + ", at least "
  71. + std::to_string(minimum) + " expected"
  72. );
  73. }
  74. void Fail(const std::string& group, const std::string& message){
  75. result_.problems.push_back({group, message});
  76. }
  77. private:
  78. std::string root_;
  79. VerifyResult& result_;
  80. };
  81. }
  82. VerifyResult VerifyInstallation(const std::string& root, const std::string& reference_dir){
  83. VerifyResult result;
  84. Checker check(root, result);
  85. // Configuration, plugins and the files of the repository, which the game always needs.
  86. const std::string config = "Configuration";
  87. check.File(config, "plugins.cfg");
  88. check.File(config, "resources.cfg");
  89. check.FileWithPrefix(config, "", "RenderSystem_GL");
  90. check.FileWithPrefix(config, "", "Codec_STBI");
  91. const std::string game = "Game files";
  92. for (const char* file : {
  93. "data/config/config.cfg", "data/system/system.fontdef", "data/system/system.lua", "data/system/system.material",
  94. "data/fonts/_fonts.xml", "data/screens/_screens.xml", "data/scripts/_scripts.xml", "data/texts/_texts.xml"
  95. }) check.File(game, file);
  96. // Everything in the repository's data folder.
  97. if (!reference_dir.empty()){
  98. std::string reference = reference_dir;
  99. if (reference.back() != '/') reference += "/";
  100. boost::system::error_code ec;
  101. std::string installed = root;
  102. if (installed.empty() || installed.back() != '/') installed += "/";
  103. int missing = 0;
  104. std::string first;
  105. for (fs::recursive_directory_iterator it(reference, ec), end; !ec && it != end; it.increment(ec)){
  106. if (!fs::is_regular_file(it->path(), ec)) continue;
  107. ++ result.checked;
  108. const std::string relative = it->path().string().substr(reference.size());
  109. if (!fs::exists(installed + relative, ec)){
  110. if (missing ++ == 0) first = relative;
  111. }
  112. }
  113. if (missing > 0){
  114. check.Fail(
  115. "Copied data files",
  116. std::to_string(missing) + " file(s) of the V-Gears data folder are missing, for example " + first
  117. );
  118. }
  119. }
  120. // Converted game data.
  121. const std::string kernel = "Kernel data";
  122. for (const char* file : {
  123. "accessories", "armors", "attacks", "characters", "commands", "growth", "initial_savemap", "items", "key_items",
  124. "materia", "summons", "weapons"
  125. }) check.File(kernel, std::string("data/gamedata/") + file + ".lua");
  126. const std::string battle = "Battle data";
  127. check.File(battle, "data/gamedata/attack/_index.xml");
  128. check.Files(battle, "data/gamedata/enemy", ".xml", 1);
  129. check.Files(battle, "data/gamedata/formation", ".xml", 1);
  130. const std::string battle_models = "Battle models";
  131. check.File(battle_models, "data/models/battle/characters.xml");
  132. check.File(battle_models, "data/models/battle/scenes.xml");
  133. check.Files(battle_models, "data/models/battle", ".mesh", 1);
  134. const std::string fields = "Field maps";
  135. check.File(fields, "data/fields/_fields.xml");
  136. check.File(fields, "data/fields/md1stin/map.xml");
  137. check.Files(fields, "data/fields", "map.xml", 500);
  138. const std::string field_models = "Field models";
  139. for (const char* file : {"cloud.mesh", "cloud.skeleton", "cloud.material"})
  140. check.File(field_models, std::string("data/models/fields/") + file);
  141. check.Files(field_models, "data/models/fields", ".mesh", 100);
  142. const std::string world = "World map";
  143. for (const char* file : {"WM0.MAP", "WM2.MAP", "WM3.MAP"}) check.File(world, std::string("data/wm/") + file);
  144. // The scripts of the maps are decompiled by the installer, the other ones are copied from the
  145. // V-Gears data folder.
  146. for (const char* file : {"wm0.lua", "wm2.lua", "wm3.lua", "triangle_triggers.lua", "vehicles.lua", "script_stubs.lua"})
  147. check.File(world, std::string("data/scripts/worldmap/") + file);
  148. check.File(world, "data/gamedata/world_encounters.xml");
  149. check.Files(world, "data/models/world/terrain/0", ".mesh", 1);
  150. const std::string world_models = "World map models";
  151. check.Files(world_models, "temp/world_models", "", 1);
  152. check.Files(world_models, "data/models/world", ".skeleton", 1);
  153. // The sounds and the music are converted with FFmpeg and TiMidity: without them there are no ogg files.
  154. const std::string sounds = "Sound effects";
  155. check.File(sounds, "data/audio/sounds/_sounds.xml");
  156. check.Files(sounds, "data/audio/sounds", ".ogg", 100);
  157. const std::string music = "Music";
  158. check.File(music, "data/audio/musics/_musics.xml");
  159. check.Files(music, "data/audio/musics", ".ogg", 50);
  160. const std::string images = "Images";
  161. check.Files(images, "data/images/icons", "", 1);
  162. return result;
  163. }