validator.cpp 9.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309
  1. /*
  2. * validator.cpp, part of VCMI engine
  3. *
  4. * Authors: listed in file AUTHORS in main folder
  5. *
  6. * License: GNU General Public License v2.0 or later
  7. * Full text of license available in license.txt file, in main folder
  8. *
  9. */
  10. #include "StdInc.h"
  11. #include "validator.h"
  12. #include "mapcontroller.h"
  13. #include "ui_validator.h"
  14. #include "../lib/entities/hero/CHero.h"
  15. #include "../lib/mapping/CMap.h"
  16. #include "../lib/mapObjects/MapObjects.h"
  17. #include "../lib/modding/CModHandler.h"
  18. #include "../lib/modding/ModDescription.h"
  19. #include "../lib/spells/CSpellHandler.h"
  20. Validator::Validator(const CMap * map, QWidget *parent) :
  21. QDialog(parent),
  22. ui(new Ui::Validator)
  23. {
  24. ui->setupUi(this);
  25. screenGeometry = QApplication::primaryScreen()->availableGeometry();
  26. setWindowFlags(Qt::Dialog | Qt::WindowTitleHint | Qt::WindowCloseButtonHint);
  27. showValidationResults(map);
  28. }
  29. Validator::~Validator()
  30. {
  31. delete ui;
  32. }
  33. std::set<Validator::Issue> Validator::validate(const CMap * map)
  34. {
  35. std::set<Validator::Issue> issues;
  36. if(!map)
  37. {
  38. issues.insert({ tr("Map is not loaded"), true });
  39. return issues;
  40. }
  41. try
  42. {
  43. //check player settings
  44. int hplayers = 0;
  45. int cplayers = 0;
  46. std::map<PlayerColor, int> amountOfTowns;
  47. std::map<PlayerColor, int> amountOfHeroes;
  48. for(int i = 0; i < map->players.size(); ++i)
  49. {
  50. auto & p = map->players[i];
  51. if (p.canAnyonePlay())
  52. amountOfTowns[PlayerColor(i)] = 0;
  53. if(p.canComputerPlay)
  54. ++cplayers;
  55. if(p.canHumanPlay)
  56. ++hplayers;
  57. if(p.allowedFactions.empty())
  58. issues.insert({ tr("No factions allowed for player %1").arg(i), true });
  59. }
  60. if(hplayers + cplayers == 0)
  61. issues.insert({ tr("No players allowed to play this map"), true });
  62. if(hplayers + cplayers == 1)
  63. issues.insert({ tr("Map is allowed for one player and cannot be started"), true });
  64. if(!hplayers)
  65. issues.insert({ tr("No human players allowed to play this map"), true });
  66. std::set<const CHero * > allHeroesOnMap; //used to find hero duplicated
  67. //checking all objects in the map
  68. for(auto o : map->objects)
  69. {
  70. //owners for objects
  71. if(o->getOwner() == PlayerColor::UNFLAGGABLE)
  72. {
  73. if(o->asOwnable())
  74. {
  75. issues.insert({ tr("Ownable object %1 is UNFLAGGABLE but must have NEUTRAL or player owner").arg(o->instanceName.c_str()), true });
  76. }
  77. }
  78. if(o->getOwner() != PlayerColor::NEUTRAL && o->getOwner().getNum() < map->players.size())
  79. {
  80. if(!map->players[o->getOwner().getNum()].canAnyonePlay())
  81. issues.insert({ tr("Object %1 is assigned to non-playable player %2").arg(o->instanceName.c_str(), o->getOwner().toString().c_str()), true });
  82. }
  83. //count towns
  84. if(auto * ins = dynamic_cast<CGTownInstance *>(o.get()))
  85. {
  86. ++amountOfTowns[ins->getOwner()];
  87. }
  88. //checking and counting heroes and prisons
  89. if(auto * ins = dynamic_cast<CGHeroInstance *>(o.get()))
  90. {
  91. if(ins->ID == Obj::PRISON)
  92. {
  93. if(ins->getOwner() != PlayerColor::NEUTRAL)
  94. issues.insert({ tr("Prison %1 must be a NEUTRAL").arg(ins->instanceName.c_str()), true });
  95. }
  96. else
  97. {
  98. if(ins->getOwner() == PlayerColor::NEUTRAL)
  99. issues.insert({ tr("Hero %1 must have an owner").arg(ins->instanceName.c_str()), true });
  100. ++amountOfHeroes[ins->getOwner()];
  101. }
  102. if(ins->getHeroTypeID().hasValue())
  103. {
  104. if(map->allowedHeroes.count(ins->getHeroTypeID()) == 0)
  105. issues.insert({ tr("Hero %1 is prohibited by map settings").arg(ins->getHeroType()->getNameTranslated().c_str()), false });
  106. if(!allHeroesOnMap.insert(ins->getHeroType()).second)
  107. issues.insert({ tr("Hero %1 has duplicate on map").arg(ins->getHeroType()->getNameTranslated().c_str()), false });
  108. }
  109. else if(ins->ID != Obj::RANDOM_HERO)
  110. issues.insert({ tr("Hero %1 has an empty type and must be removed").arg(ins->instanceName.c_str()), true });
  111. }
  112. //checking for arts
  113. if(auto * ins = dynamic_cast<CGArtifact *>(o.get()))
  114. {
  115. if(ins->ID == Obj::SPELL_SCROLL)
  116. {
  117. if (ins->getArtifactInstance())
  118. {
  119. if (map->allowedSpells.count(ins->getArtifactInstance()->getScrollSpellID()) == 0)
  120. issues.insert({ tr("Spell scroll %1 is prohibited by map settings").arg(ins->getArtifactInstance()->getScrollSpellID().toEntity(LIBRARY->spells())->getNameTranslated().c_str()), false });
  121. }
  122. else
  123. issues.insert({ tr("Spell scroll %1 doesn't have instance assigned and must be removed").arg(ins->instanceName.c_str()), true });
  124. }
  125. else
  126. {
  127. if(ins->ID == Obj::ARTIFACT && map->allowedArtifact.count(ins->getArtifactType()) == 0)
  128. {
  129. issues.insert({ tr("Artifact %1 is prohibited by map settings").arg(ins->getObjectName().c_str()), false });
  130. }
  131. }
  132. }
  133. }
  134. //verification of starting towns
  135. for (const auto & [player, counter] : amountOfTowns)
  136. {
  137. if (counter == 0)
  138. {
  139. // FIXME: heroesNames are empty even though heroes are on the map
  140. // if(map->players[playerTCounter.first].heroesNames.empty())
  141. if(amountOfHeroes.count(player) == 0)
  142. issues.insert({ tr("Player %1 has no towns and heroes assigned").arg(player + 1), true });
  143. else
  144. issues.insert({ tr("Player %1 doesn't have any starting town").arg(player + 1), false });
  145. }
  146. }
  147. //verification of map name and description
  148. if(map->name.empty())
  149. issues.insert({ tr("Map name is not specified"), false });
  150. if(map->description.empty())
  151. issues.insert({ tr("Map description is not specified"), false });
  152. //verification for mods
  153. for(auto & mod : MapController::modAssessmentMap(*map))
  154. {
  155. if(!map->mods.count(mod.first))
  156. issues.insert({ MapController::modMissingMessage(mod.second), true });
  157. }
  158. }
  159. catch(const std::exception & e)
  160. {
  161. issues.insert({ tr("Exception occurs during validation: %1").arg(e.what()), true });
  162. }
  163. catch(...)
  164. {
  165. issues.insert({ tr("Unknown exception occurs during validation"), true });
  166. }
  167. return issues;
  168. }
  169. void Validator::showValidationResults(const CMap * map)
  170. {
  171. show();
  172. setAttribute(Qt::WA_DeleteOnClose);
  173. ui->listWidget->setItemDelegate(new ValidatorItemDelegate(ui->listWidget));
  174. for(auto const & issue : Validator::validate(map))
  175. {
  176. auto * item = new QListWidgetItem(QIcon(issue.critical ? ":/icons/mod-delete.png" : ":/icons/mod-update.png"),
  177. issue.message, ui->listWidget);
  178. ui->listWidget->addItem(item);
  179. }
  180. if(ui->listWidget->count() == 0)
  181. {
  182. QPixmap greenTick(":/icons/mod-enabled.png");
  183. QString validMessage = tr("The map is valid and has no issues.");
  184. auto * item = new QListWidgetItem(QIcon(greenTick), validMessage, ui->listWidget);
  185. ui->listWidget->addItem(item);
  186. }
  187. ui->listWidget->updateGeometry();
  188. adjustWindowSize();
  189. }
  190. void Validator::adjustWindowSize()
  191. {
  192. const int minWidth = 350;
  193. const int minHeight = 50;
  194. const int padding = 30; // reserved space for eventual scrollbars
  195. const int screenMarginVertical = 300;
  196. const int screenMarginHorizontal = 350;
  197. int contentHeight = minHeight;
  198. int contentWidth = minWidth;
  199. QStyleOptionViewItem option;
  200. option.initFrom(ui->listWidget);
  201. int listWidgetWidth = ui->listWidget->viewport()->width();
  202. for(int i = 0; i < ui->listWidget->count(); ++i)
  203. {
  204. option.rect = QRect(0, 0, listWidgetWidth, 0);
  205. auto itemSize = ui->listWidget->itemDelegate()->sizeHint(option, ui->listWidget->model()->index(i, 0));
  206. contentHeight += itemSize.height();
  207. contentWidth = qMax(contentWidth, itemSize.width());
  208. }
  209. int screenWidth = screenGeometry.width();
  210. int screenHeight = screenGeometry.height();
  211. int finalWidth = qMin(contentWidth + padding, screenWidth - screenMarginHorizontal);
  212. int finalHeight = qMin(contentHeight + padding, screenHeight - screenMarginVertical);
  213. QWidget * parentWidget = ui->listWidget->parentWidget();
  214. if(parentWidget)
  215. {
  216. parentWidget->setMinimumWidth(finalWidth + padding);
  217. parentWidget->setMinimumHeight(finalHeight + padding);
  218. }
  219. ui->listWidget->resize(finalWidth, finalHeight);
  220. move((screenWidth - finalWidth) / 2, (screenHeight - finalHeight) / 2);
  221. }
  222. void ValidatorItemDelegate::paint(QPainter * painter, const QStyleOptionViewItem & option, const QModelIndex & index) const
  223. {
  224. painter->save();
  225. QStyleOptionViewItem opt(option);
  226. QFontMetrics metrics(option.fontMetrics);
  227. initStyleOption(&opt, index);
  228. const QRect iconRect = option.rect.adjusted(iconPadding, iconPadding, 0, 0);
  229. const QRect textRect = option.rect.adjusted(offsetForIcon, 0, -textPaddingRight, 0);
  230. if(!opt.icon.isNull())
  231. {
  232. opt.icon.paint(painter, iconRect, Qt::AlignTop | Qt::AlignLeft);
  233. }
  234. QTextOption textOption;
  235. int textWidth = metrics.horizontalAdvance(opt.text);
  236. if(textWidth + offsetForIcon + textPaddingRight > screenGeometry.width() - screenMargin)
  237. {
  238. textOption.setWrapMode(QTextOption::WordWrap);
  239. }
  240. painter->drawText(textRect, opt.text, textOption);
  241. painter->restore();
  242. }
  243. QSize ValidatorItemDelegate::sizeHint(const QStyleOptionViewItem & option, const QModelIndex & index) const
  244. {
  245. QFontMetrics metrics(option.fontMetrics);
  246. QString text = index.data(Qt::DisplayRole).toString();
  247. QStringList lines = text.split('\n');
  248. int textWidth = minItemWidth;
  249. int requiredHeight = 0;
  250. for(auto line : lines)
  251. textWidth = std::max(metrics.horizontalAdvance(line), textWidth);
  252. requiredHeight = qMax(requiredHeight, lines.size() * metrics.height());
  253. int finalWidth = qMax(textWidth + offsetForIcon, minItemWidth);
  254. finalWidth = qMin(finalWidth, screenGeometry.width() - screenMargin - offsetForIcon);
  255. QRect textBoundingRect = metrics.boundingRect(QRect(0, 0, finalWidth, 0),
  256. Qt::TextWordWrap, text);
  257. int finalHeight = qMax(textBoundingRect.height() + itemPaddingBottom, requiredHeight);
  258. return QSize(finalWidth, finalHeight);
  259. }