InstallVerifier.cpp 10.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234
  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. #include "common/InstallStamp.h"
  18. #include "common/OgrePlugins.h"
  19. /**
  20. * Directory with the Ogre plugins, found when V-Gears was built. Set by CMake (VGEARS_OGRE_PLUGIN_DIR).
  21. */
  22. #ifndef VGEARS_OGRE_PLUGIN_DIR
  23. #define VGEARS_OGRE_PLUGIN_DIR ""
  24. #endif
  25. namespace fs = boost::filesystem;
  26. namespace{
  27. /**
  28. * Collects the results of the checks.
  29. */
  30. class Checker{
  31. public:
  32. Checker(const std::string& root, VerifyResult& result): root_(root), result_(result){
  33. if (!root_.empty() && root_.back() != '/') root_ += "/";
  34. }
  35. /**
  36. * A file must exist.
  37. */
  38. void File(const std::string& group, const std::string& path){
  39. ++ result_.checked;
  40. boost::system::error_code ec;
  41. if (!fs::is_regular_file(root_ + path, ec)) Fail(group, path + " is missing");
  42. }
  43. /**
  44. * The Ogre plugins listed in plugins.cfg must be found where the game looks for them.
  45. *
  46. * @param[in] group The group of the problems.
  47. * @param[in] search Where to look for them.
  48. */
  49. void OgrePlugins(const std::string& group, const VerifyPlugins& search){
  50. if (search.ogre_version.empty()) return;
  51. namespace plugins = VGears::OgrePlugins;
  52. const fs::path file(root_ + "plugins.cfg");
  53. boost::system::error_code ec;
  54. // A missing plugins.cfg is already reported.
  55. if (!fs::is_regular_file(file, ec)) return;
  56. ++ result_.checked;
  57. plugins::Config config;
  58. if (!plugins::ReadConfig(file, config)){
  59. Fail(group, "plugins.cfg can't be read");
  60. return;
  61. }
  62. const std::vector<fs::path> directories = plugins::SearchDirectories(
  63. file.parent_path(), config.folder, fs::path(search.executable_dir), VGEARS_OGRE_PLUGIN_DIR
  64. );
  65. for (const std::string& name : config.plugins){
  66. ++ result_.checked;
  67. if (plugins::Find(name, directories, search.ogre_version).empty())
  68. Fail(group, "the Ogre plugin " + name + " is not in the installation directory or the system");
  69. }
  70. }
  71. /**
  72. * A directory must have at least a number of files with an extension, searching in subdirectories.
  73. *
  74. * @param[in] extension Extension with the dot, or the full file name if it has none (like "map.xml").
  75. * An empty string matches every file.
  76. */
  77. void Files(
  78. const std::string& group, const std::string& dir, const std::string& extension, int minimum
  79. ){
  80. ++ result_.checked;
  81. int found = 0;
  82. boost::system::error_code ec;
  83. for (fs::recursive_directory_iterator it(root_ + dir, ec), end; !ec && it != end; it.increment(ec)){
  84. if (!fs::is_regular_file(it->path(), ec)) continue;
  85. const std::string name = it->path().filename().string();
  86. if (
  87. extension.empty() || (name.size() >= extension.size()
  88. && name.compare(name.size() - extension.size(), extension.size(), extension) == 0)
  89. ){
  90. if (++ found >= minimum) return;
  91. }
  92. }
  93. std::string wanted = extension.empty() ? "files" : extension + " files";
  94. Fail(
  95. group, dir + " has " + std::to_string(found) + " " + wanted + ", at least "
  96. + std::to_string(minimum) + " expected"
  97. );
  98. }
  99. void Fail(const std::string& group, const std::string& message){
  100. result_.problems.push_back({group, message});
  101. }
  102. private:
  103. std::string root_;
  104. VerifyResult& result_;
  105. };
  106. }
  107. VerifyResult VerifyInstallation(
  108. const std::string& root, const std::string& reference_dir, const VerifyPlugins& plugins
  109. ){
  110. VerifyResult result;
  111. Checker check(root, result);
  112. // The record of the installations: whether the last one finished, and whether a part is outdated.
  113. const std::string installation = "Installation";
  114. ++ result.checked;
  115. bool recorded = false;
  116. for (const std::string& problem : VGears::InstallStamp::Problems(fs::path(root), &recorded))
  117. result.problems.push_back({installation, problem});
  118. if (!recorded){
  119. result.problems.push_back({
  120. installation, "there is no record of the installation: it was made by an older installer, whose data may "
  121. "be outdated. Install the data again."
  122. });
  123. }
  124. // Configuration, plugins and the files of the repository, which the game always needs.
  125. const std::string config = "Configuration";
  126. check.File(config, "plugins.cfg");
  127. check.File(config, "resources.cfg");
  128. // The plugins don't have to be in the installation directory: the game also looks for them next to its
  129. // executable and in the system.
  130. check.OgrePlugins(config, plugins);
  131. const std::string game = "Game files";
  132. for (const char* file : {
  133. "data/config/config.cfg", "data/system/system.fontdef", "data/system/system.lua", "data/system/system.material",
  134. "data/fonts/_fonts.xml", "data/screens/_screens.xml", "data/scripts/_scripts.xml", "data/texts/_texts.xml"
  135. }) check.File(game, file);
  136. // Everything in the repository's data folder.
  137. if (!reference_dir.empty()){
  138. std::string reference = reference_dir;
  139. if (reference.back() != '/') reference += "/";
  140. boost::system::error_code ec;
  141. std::string installed = root;
  142. if (installed.empty() || installed.back() != '/') installed += "/";
  143. int missing = 0;
  144. std::string first;
  145. for (fs::recursive_directory_iterator it(reference, ec), end; !ec && it != end; it.increment(ec)){
  146. if (!fs::is_regular_file(it->path(), ec)) continue;
  147. ++ result.checked;
  148. const std::string relative = it->path().string().substr(reference.size());
  149. if (!fs::exists(installed + relative, ec)){
  150. if (missing ++ == 0) first = relative;
  151. }
  152. }
  153. if (missing > 0){
  154. check.Fail(
  155. "Copied data files",
  156. std::to_string(missing) + " file(s) of the V-Gears data folder are missing, for example " + first
  157. );
  158. }
  159. }
  160. // Converted game data.
  161. const std::string kernel = "Kernel data";
  162. for (const char* file : {
  163. "accessories", "armors", "attacks", "characters", "commands", "growth", "initial_savemap", "items", "key_items",
  164. "materia", "summons", "weapons"
  165. }) check.File(kernel, std::string("data/gamedata/") + file + ".lua");
  166. const std::string battle = "Battle data";
  167. check.File(battle, "data/gamedata/attack/_index.xml");
  168. check.Files(battle, "data/gamedata/enemy", ".xml", 1);
  169. check.Files(battle, "data/gamedata/formation", ".xml", 1);
  170. const std::string battle_models = "Battle models";
  171. check.File(battle_models, "data/models/battle/characters.xml");
  172. check.File(battle_models, "data/models/battle/scenes.xml");
  173. check.Files(battle_models, "data/models/battle", ".mesh", 1);
  174. const std::string fields = "Field maps";
  175. check.File(fields, "data/fields/_fields.xml");
  176. check.File(fields, "data/fields/md1stin/map.xml");
  177. check.Files(fields, "data/fields", "map.xml", 500);
  178. const std::string field_models = "Field models";
  179. for (const char* file : {"cloud.mesh", "cloud.skeleton", "cloud.material"})
  180. check.File(field_models, std::string("data/models/fields/") + file);
  181. check.Files(field_models, "data/models/fields", ".mesh", 100);
  182. const std::string world = "World map";
  183. for (const char* file : {"WM0.MAP", "WM2.MAP", "WM3.MAP"}) check.File(world, std::string("data/wm/") + file);
  184. // The scripts of the maps are decompiled by the installer, the other ones are copied from the
  185. // V-Gears data folder.
  186. for (const char* file : {"wm0.lua", "wm2.lua", "wm3.lua", "triangle_triggers.lua", "vehicles.lua", "script_stubs.lua"})
  187. check.File(world, std::string("data/scripts/worldmap/") + file);
  188. check.File(world, "data/gamedata/world_encounters.xml");
  189. check.Files(world, "data/models/world/terrain/0", ".mesh", 1);
  190. const std::string world_models = "World map models";
  191. check.Files(world_models, "temp/world_models", "", 1);
  192. check.Files(world_models, "data/models/world", ".skeleton", 1);
  193. // The sounds and the music are converted with FFmpeg and TiMidity: without them there are no ogg files.
  194. const std::string sounds = "Sound effects";
  195. check.File(sounds, "data/audio/sounds/_sounds.xml");
  196. check.Files(sounds, "data/audio/sounds", ".ogg", 100);
  197. const std::string music = "Music";
  198. check.File(music, "data/audio/musics/_musics.xml");
  199. check.Files(music, "data/audio/musics", ".ogg", 50);
  200. const std::string images = "Images";
  201. check.Files(images, "data/images/icons", "", 1);
  202. // Fonts, built from WINDOW.BIN. They are installed even when images are not.
  203. const std::string fonts = "Fonts";
  204. for (const char* file : {
  205. "data/fonts/ffvii_en.png", "data/fonts/ffvii_menu.png", "data/fonts/ffvii_digits.png", "data/fonts/ffvii_timer.png"
  206. }) check.File(fonts, file);
  207. return result;
  208. }