2
0

validator.cpp 9.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311
  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. if(!o)
  71. continue;
  72. //owners for objects
  73. if(o->getOwner() == PlayerColor::UNFLAGGABLE)
  74. {
  75. if(o->asOwnable())
  76. {
  77. issues.insert({ tr("Ownable object %1 is UNFLAGGABLE but must have NEUTRAL or player owner").arg(o->instanceName.c_str()), true });
  78. }
  79. }
  80. if(o->getOwner() != PlayerColor::NEUTRAL && o->getOwner().getNum() < map->players.size())
  81. {
  82. if(!map->players[o->getOwner().getNum()].canAnyonePlay())
  83. issues.insert({ tr("Object %1 is assigned to non-playable player %2").arg(o->instanceName.c_str(), o->getOwner().toString().c_str()), true });
  84. }
  85. //count towns
  86. if(auto * ins = dynamic_cast<CGTownInstance *>(o.get()))
  87. {
  88. ++amountOfTowns[ins->getOwner()];
  89. }
  90. //checking and counting heroes and prisons
  91. if(auto * ins = dynamic_cast<CGHeroInstance *>(o.get()))
  92. {
  93. if(ins->ID == Obj::PRISON)
  94. {
  95. if(ins->getOwner() != PlayerColor::NEUTRAL)
  96. issues.insert({ tr("Prison %1 must be a NEUTRAL").arg(ins->instanceName.c_str()), true });
  97. }
  98. else
  99. {
  100. if(ins->getOwner() == PlayerColor::NEUTRAL)
  101. issues.insert({ tr("Hero %1 must have an owner").arg(ins->instanceName.c_str()), true });
  102. ++amountOfHeroes[ins->getOwner()];
  103. }
  104. if(ins->getHeroTypeID().hasValue())
  105. {
  106. if(map->allowedHeroes.count(ins->getHeroTypeID()) == 0)
  107. issues.insert({ tr("Hero %1 is prohibited by map settings").arg(ins->getHeroType()->getNameTranslated().c_str()), false });
  108. if(!allHeroesOnMap.insert(ins->getHeroType()).second)
  109. issues.insert({ tr("Hero %1 has duplicate on map").arg(ins->getHeroType()->getNameTranslated().c_str()), false });
  110. }
  111. else if(ins->ID != Obj::RANDOM_HERO)
  112. issues.insert({ tr("Hero %1 has an empty type and must be removed").arg(ins->instanceName.c_str()), true });
  113. }
  114. //checking for arts
  115. if(auto * ins = dynamic_cast<CGArtifact *>(o.get()))
  116. {
  117. if(ins->ID == Obj::SPELL_SCROLL)
  118. {
  119. if (ins->getArtifactInstance())
  120. {
  121. if (map->allowedSpells.count(ins->getArtifactInstance()->getScrollSpellID()) == 0)
  122. issues.insert({ tr("Spell scroll %1 is prohibited by map settings").arg(ins->getArtifactInstance()->getScrollSpellID().toEntity(LIBRARY->spells())->getNameTranslated().c_str()), false });
  123. }
  124. else
  125. issues.insert({ tr("Spell scroll %1 doesn't have instance assigned and must be removed").arg(ins->instanceName.c_str()), true });
  126. }
  127. else
  128. {
  129. if(ins->ID == Obj::ARTIFACT && map->allowedArtifact.count(ins->getArtifactType()) == 0)
  130. {
  131. issues.insert({ tr("Artifact %1 is prohibited by map settings").arg(ins->getObjectName().c_str()), false });
  132. }
  133. }
  134. }
  135. }
  136. //verification of starting towns
  137. for (const auto & [player, counter] : amountOfTowns)
  138. {
  139. if (counter == 0)
  140. {
  141. // FIXME: heroesNames are empty even though heroes are on the map
  142. // if(map->players[playerTCounter.first].heroesNames.empty())
  143. if(amountOfHeroes.count(player) == 0)
  144. issues.insert({ tr("Player %1 has no towns and heroes assigned").arg(player + 1), true });
  145. else
  146. issues.insert({ tr("Player %1 doesn't have any starting town").arg(player + 1), false });
  147. }
  148. }
  149. //verification of map name and description
  150. if(map->name.empty())
  151. issues.insert({ tr("Map name is not specified"), false });
  152. if(map->description.empty())
  153. issues.insert({ tr("Map description is not specified"), false });
  154. //verification for mods
  155. for(auto & mod : MapController::modAssessmentMap(*map))
  156. {
  157. if(!map->mods.count(mod.first))
  158. issues.insert({ MapController::modMissingMessage(mod.second), true });
  159. }
  160. }
  161. catch(const std::exception & e)
  162. {
  163. issues.insert({ tr("Exception occurs during validation: %1").arg(e.what()), true });
  164. }
  165. catch(...)
  166. {
  167. issues.insert({ tr("Unknown exception occurs during validation"), true });
  168. }
  169. return issues;
  170. }
  171. void Validator::showValidationResults(const CMap * map)
  172. {
  173. show();
  174. setAttribute(Qt::WA_DeleteOnClose);
  175. ui->listWidget->setItemDelegate(new ValidatorItemDelegate(ui->listWidget));
  176. for(auto const & issue : Validator::validate(map))
  177. {
  178. auto * item = new QListWidgetItem(QIcon(issue.critical ? ":/icons/mod-delete.png" : ":/icons/mod-update.png"),
  179. issue.message, ui->listWidget);
  180. ui->listWidget->addItem(item);
  181. }
  182. if(ui->listWidget->count() == 0)
  183. {
  184. QPixmap greenTick(":/icons/mod-enabled.png");
  185. QString validMessage = tr("The map is valid and has no issues.");
  186. auto * item = new QListWidgetItem(QIcon(greenTick), validMessage, ui->listWidget);
  187. ui->listWidget->addItem(item);
  188. }
  189. ui->listWidget->updateGeometry();
  190. adjustWindowSize();
  191. }
  192. void Validator::adjustWindowSize()
  193. {
  194. const int minWidth = 350;
  195. const int minHeight = 50;
  196. const int padding = 30; // reserved space for eventual scrollbars
  197. const int screenMarginVertical = 300;
  198. const int screenMarginHorizontal = 350;
  199. int contentHeight = minHeight;
  200. int contentWidth = minWidth;
  201. QStyleOptionViewItem option;
  202. option.initFrom(ui->listWidget);
  203. int listWidgetWidth = ui->listWidget->viewport()->width();
  204. for(int i = 0; i < ui->listWidget->count(); ++i)
  205. {
  206. option.rect = QRect(0, 0, listWidgetWidth, 0);
  207. auto itemSize = ui->listWidget->itemDelegate()->sizeHint(option, ui->listWidget->model()->index(i, 0));
  208. contentHeight += itemSize.height();
  209. contentWidth = qMax(contentWidth, itemSize.width());
  210. }
  211. int screenWidth = screenGeometry.width();
  212. int screenHeight = screenGeometry.height();
  213. int finalWidth = qMin(contentWidth + padding, screenWidth - screenMarginHorizontal);
  214. int finalHeight = qMin(contentHeight + padding, screenHeight - screenMarginVertical);
  215. QWidget * parentWidget = ui->listWidget->parentWidget();
  216. if(parentWidget)
  217. {
  218. parentWidget->setMinimumWidth(finalWidth + padding);
  219. parentWidget->setMinimumHeight(finalHeight + padding);
  220. }
  221. ui->listWidget->resize(finalWidth, finalHeight);
  222. move((screenWidth - finalWidth) / 2, (screenHeight - finalHeight) / 2);
  223. }
  224. void ValidatorItemDelegate::paint(QPainter * painter, const QStyleOptionViewItem & option, const QModelIndex & index) const
  225. {
  226. painter->save();
  227. QStyleOptionViewItem opt(option);
  228. QFontMetrics metrics(option.fontMetrics);
  229. initStyleOption(&opt, index);
  230. const QRect iconRect = option.rect.adjusted(iconPadding, iconPadding, 0, 0);
  231. const QRect textRect = option.rect.adjusted(offsetForIcon, 0, -textPaddingRight, 0);
  232. if(!opt.icon.isNull())
  233. {
  234. opt.icon.paint(painter, iconRect, Qt::AlignTop | Qt::AlignLeft);
  235. }
  236. QTextOption textOption;
  237. int textWidth = metrics.horizontalAdvance(opt.text);
  238. if(textWidth + offsetForIcon + textPaddingRight > screenGeometry.width() - screenMargin)
  239. {
  240. textOption.setWrapMode(QTextOption::WordWrap);
  241. }
  242. painter->drawText(textRect, opt.text, textOption);
  243. painter->restore();
  244. }
  245. QSize ValidatorItemDelegate::sizeHint(const QStyleOptionViewItem & option, const QModelIndex & index) const
  246. {
  247. QFontMetrics metrics(option.fontMetrics);
  248. QString text = index.data(Qt::DisplayRole).toString();
  249. QStringList lines = text.split('\n');
  250. int textWidth = minItemWidth;
  251. int requiredHeight = 0;
  252. for(auto line : lines)
  253. textWidth = std::max(metrics.horizontalAdvance(line), textWidth);
  254. requiredHeight = qMax(requiredHeight, lines.size() * metrics.height());
  255. int finalWidth = qMax(textWidth + offsetForIcon, minItemWidth);
  256. finalWidth = qMin(finalWidth, screenGeometry.width() - screenMargin - offsetForIcon);
  257. QRect textBoundingRect = metrics.boundingRect(QRect(0, 0, finalWidth, 0),
  258. Qt::TextWordWrap, text);
  259. int finalHeight = qMax(textBoundingRect.height() + itemPaddingBottom, requiredHeight);
  260. return QSize(finalWidth, finalHeight);
  261. }