InstallStamp.cpp 6.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169
  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 <ctime>
  17. #include <fstream>
  18. #include <set>
  19. #include <sstream>
  20. #include "common/InstallStamp.h"
  21. namespace VGears{
  22. namespace InstallStamp{
  23. namespace bfs = boost::filesystem;
  24. const char* const FILE_NAME = "install.info";
  25. namespace{
  26. /**
  27. * Joins a list with commas.
  28. */
  29. std::string Join(const std::vector<std::string>& items){
  30. std::string joined;
  31. for (const std::string& item : items) joined += (joined.empty() ? "" : ",") + item;
  32. return joined;
  33. }
  34. /**
  35. * Splits a list separated by commas, skipping empty items.
  36. */
  37. std::vector<std::string> Split(const std::string& list){
  38. std::vector<std::string> items;
  39. std::stringstream stream(list);
  40. std::string item;
  41. while (std::getline(stream, item, ',')) if (!item.empty()) items.push_back(item);
  42. return items;
  43. }
  44. /**
  45. * Removes the spaces at both ends.
  46. */
  47. std::string Trim(std::string text){
  48. const auto space = [](unsigned char c){return std::isspace(c) != 0;};
  49. while (!text.empty() && space(text.back())) text.pop_back();
  50. while (!text.empty() && space(text.front())) text.erase(0, 1);
  51. return text;
  52. }
  53. }
  54. bool Read(const bfs::path& directory, Stamp& stamp){
  55. stamp = Stamp();
  56. std::ifstream file((directory / FILE_NAME).string());
  57. if (!file.is_open()) return false;
  58. std::string line;
  59. while (std::getline(file, line)){
  60. line = Trim(line);
  61. if (line.empty() || line[0] == '#') continue;
  62. const size_t equal = line.find('=');
  63. if (equal == std::string::npos) continue;
  64. const std::string key = Trim(line.substr(0, equal));
  65. const std::string value = Trim(line.substr(equal + 1));
  66. if (key == "complete") stamp.complete = value == "1";
  67. else if (key == "installer") stamp.installer = value;
  68. else if (key == "date") stamp.date = value;
  69. else if (key == "disk") stamp.disk = value;
  70. else if (key == "installing") stamp.installing = Split(value);
  71. else if (key.compare(0, 5, "part.") == 0 && key.size() > 5){
  72. int version = 0;
  73. try{version = std::stoi(value);}
  74. catch (...){version = 0;}
  75. stamp.parts[key.substr(5)] = version;
  76. }
  77. }
  78. return true;
  79. }
  80. bool Write(const bfs::path& directory, const Stamp& stamp){
  81. std::ofstream file((directory / FILE_NAME).string(), std::ios::out | std::ios::trunc);
  82. if (!file.is_open()) return false;
  83. file << "# Record of the installations of the game data, written by v-gears-installer.\n"
  84. << "# The game and the verifier read it to know if the data must be installed again.\n"
  85. << "complete=" << (stamp.complete ? 1 : 0) << "\n"
  86. << "installer=" << stamp.installer << "\n"
  87. << "date=" << stamp.date << "\n"
  88. << "disk=" << stamp.disk << "\n";
  89. if (!stamp.installing.empty()) file << "installing=" << Join(stamp.installing) << "\n";
  90. for (const auto& part : stamp.parts) file << "part." << part.first << "=" << part.second << "\n";
  91. return static_cast<bool>(file);
  92. }
  93. void Start(
  94. Stamp& stamp, const std::vector<std::string>& parts, const std::string& installer,
  95. const std::string& date, const std::string& disk
  96. ){
  97. // The parts of an installation that didn't finish, and are not installed again now, are not usable.
  98. if (!stamp.complete){
  99. for (const std::string& part : stamp.installing){
  100. if (std::find(parts.begin(), parts.end(), part) == parts.end()) stamp.parts[part] = 0;
  101. }
  102. }
  103. stamp.complete = false;
  104. stamp.installing = parts;
  105. stamp.installer = installer;
  106. stamp.date = date;
  107. stamp.disk = disk;
  108. }
  109. void Finish(Stamp& stamp){
  110. for (const std::string& part : stamp.installing) stamp.parts[part] = DATA_VERSION;
  111. stamp.installing.clear();
  112. stamp.complete = true;
  113. }
  114. std::vector<std::string> Outdated(const Stamp& stamp){
  115. std::set<std::string> outdated;
  116. for (const auto& part : stamp.parts) if (part.second < DATA_VERSION) outdated.insert(part.first);
  117. if (!stamp.complete) outdated.insert(stamp.installing.begin(), stamp.installing.end());
  118. return std::vector<std::string>(outdated.begin(), outdated.end());
  119. }
  120. std::vector<std::string> Problems(const bfs::path& directory, bool* found){
  121. std::vector<std::string> problems;
  122. Stamp stamp;
  123. const bool read = Read(directory, stamp);
  124. if (found != nullptr) *found = read;
  125. if (!read) return problems;
  126. if (!stamp.complete){
  127. problems.push_back(
  128. "The last installation (" + stamp.date + ") didn't finish: it was interrupted or it failed. "
  129. "Install the data again."
  130. );
  131. }
  132. std::vector<std::string> old;
  133. for (const auto& part : stamp.parts){
  134. const bool interrupted = std::find(stamp.installing.begin(), stamp.installing.end(), part.first)
  135. != stamp.installing.end();
  136. if (part.second < DATA_VERSION && !(interrupted && !stamp.complete)) old.push_back(part.first);
  137. }
  138. if (!old.empty()){
  139. problems.push_back(
  140. "These parts were installed by an older installer, or their installation didn't finish, and must be "
  141. "installed again: " + Join(old) + "."
  142. );
  143. }
  144. return problems;
  145. }
  146. std::string Now(){
  147. const std::time_t now = std::time(nullptr);
  148. char text[32];
  149. std::strftime(text, sizeof(text), "%Y-%m-%d %H:%M:%S", std::localtime(&now));
  150. return text;
  151. }
  152. }
  153. }