SavemapRead.cpp 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324
  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 <tinyxml.h>
  16. #include "common/Logger.h"
  17. #include "engine/save/Savemap.h"
  18. namespace{
  19. /**
  20. * Calls a function for each child element of a node that has a name.
  21. */
  22. template<typename Function>
  23. void ForEachElement(TiXmlNode& parent, const char* name, Function function){
  24. for (
  25. TiXmlElement* element = parent.FirstChildElement(name); element != nullptr;
  26. element = element->NextSiblingElement(name)
  27. ) function(*element);
  28. }
  29. /**
  30. * Reads an integer attribute.
  31. *
  32. * @param[in] missing Value returned if there is no such attribute, or it's not a number.
  33. */
  34. int ReadInt(const TiXmlElement& element, const char* name, const int missing = -1){
  35. int value = missing;
  36. element.QueryIntAttribute(name, &value);
  37. return value;
  38. }
  39. /**
  40. * Sets a value from an attribute, if the attribute has a number that is not negative.
  41. */
  42. void ReadUnsigned(const TiXmlElement& element, const char* name, unsigned int& value){
  43. const int read = ReadInt(element, name);
  44. if (read >= 0) value = read;
  45. }
  46. /**
  47. * Reads the skills that an Enemy Skill materia has learned.
  48. */
  49. void ReadEnemySkills(TiXmlElement& materia_element, SavemapData::Materia& materia){
  50. for (bool& learned : materia.enemy_skill_learned) learned = false;
  51. ForEachElement(materia_element, "EnemySkill", [&](TiXmlElement& skills){
  52. ForEachElement(skills, "Skill", [&](TiXmlElement& skill){
  53. const int id = ReadInt(skill, "id");
  54. if (id < 0 || id >= static_cast<int>(SavemapData::MAX_ENEMY_SKILLS)) return;
  55. materia.enemy_skill_learned[id] = (ReadInt(skill, "learned") == 1);
  56. });
  57. });
  58. }
  59. /**
  60. * Reads a list of materia: the ones of the inventory, the stash or a piece of equipment.
  61. */
  62. void ReadMaterias(TiXmlElement& list, SavemapData::Materia* materias, const unsigned int count){
  63. ForEachElement(list, "Materia", [&](TiXmlElement& element){
  64. const int slot = ReadInt(element, "slot");
  65. const int id = ReadInt(element, "id", -2);
  66. const int ap = ReadInt(element, "ap");
  67. const int enemy_skill = ReadInt(element, "enemy_skill", 0);
  68. if (
  69. slot < 0 || slot >= static_cast<int>(count) || id < -1 || ap < 0
  70. || (enemy_skill != 0 && enemy_skill != 1)
  71. ) return;
  72. materias[slot].id = id;
  73. materias[slot].ap = ap;
  74. materias[slot].enemy_skill = (enemy_skill == 1);
  75. ReadEnemySkills(element, materias[slot]);
  76. });
  77. }
  78. /**
  79. * Reads a stat of a character.
  80. */
  81. void ReadStat(const TiXmlElement& element, SavemapData::Character& character){
  82. struct Entry{
  83. const char* id;
  84. SavemapData::Character::Stat SavemapData::Character::* stat;
  85. const char* second;
  86. int minimum_base;
  87. };
  88. static const Entry STATS[] = {
  89. {"str", &SavemapData::Character::str, "bonus", 0},
  90. {"vit", &SavemapData::Character::vit, "bonus", 0},
  91. {"mag", &SavemapData::Character::mag, "bonus", 0},
  92. {"spr", &SavemapData::Character::spr, "bonus", 0},
  93. {"dex", &SavemapData::Character::dex, "bonus", 0},
  94. {"lck", &SavemapData::Character::lck, "bonus", 0},
  95. {"hp", &SavemapData::Character::hp, "current", 1},
  96. {"mp", &SavemapData::Character::mp, "current", 1}
  97. };
  98. std::string id;
  99. element.QueryStringAttribute("id", &id);
  100. for (const Entry& entry : STATS){
  101. if (id != entry.id) continue;
  102. const int base = ReadInt(element, "base");
  103. const int second = ReadInt(element, entry.second);
  104. if (base < entry.minimum_base || second < 0) return;
  105. (character.*entry.stat).base = base;
  106. (character.*entry.stat).extra = second;
  107. }
  108. }
  109. /**
  110. * Reads the limit breaks of a character.
  111. */
  112. void ReadLimits(TiXmlElement& limits, SavemapData::Character& character){
  113. const int current = ReadInt(limits, "current");
  114. if (current >= 0 && current < static_cast<int>(SavemapData::MAX_LIMIT_LEVELS))
  115. character.limit_level = current;
  116. const int bar = ReadInt(limits, "bar");
  117. if (bar >= 0 && bar <= static_cast<int>(SavemapData::MAX_LIMIT_BAR))
  118. character.limit_bar = bar;
  119. ForEachElement(limits, "Level", [&](TiXmlElement& element){
  120. const int level = ReadInt(element, "level");
  121. if (level < 0 || level >= static_cast<int>(SavemapData::MAX_LIMIT_LEVELS)) return;
  122. ReadUnsigned(element, "uses", character.limit_uses[level]);
  123. ForEachElement(element, "Technique", [&](TiXmlElement& technique){
  124. const int id = ReadInt(technique, "id");
  125. if (id < 0 || id >= static_cast<int>(SavemapData::MAX_LIMIT_TECHNIQUES)) return;
  126. character.limits_learned[level][id] = (ReadInt(technique, "learned") == 1);
  127. });
  128. });
  129. }
  130. /**
  131. * Reads a weapon or an armor of a character, with its materia.
  132. */
  133. void ReadEquipment(TiXmlElement& element, SavemapData::Character::Equipment& equipment){
  134. const int id = ReadInt(element, "id");
  135. if (id >= 0) equipment.id = id;
  136. // The list of materia is the first thing inside.
  137. TiXmlNode* first = element.FirstChild();
  138. if (first == nullptr || first->ToElement() == nullptr || first->ValueStr() != "Materia")
  139. return;
  140. ReadMaterias(*first->ToElement(), equipment.materia, SavemapData::MAX_EQUIP_SLOTS);
  141. }
  142. /**
  143. * Reads a character.
  144. */
  145. void ReadCharacter(TiXmlElement& element, SavemapData::Character& character){
  146. const int char_id = ReadInt(element, "char_id", -2);
  147. if (char_id >= -1) character.char_id = char_id;
  148. element.QueryStringAttribute("name", &character.name);
  149. character.enabled = (ReadInt(element, "enabled") == 1);
  150. character.locked = (ReadInt(element, "locked") == 1);
  151. const int level = ReadInt(element, "level");
  152. if (level > 0) character.level = level;
  153. ReadUnsigned(element, "kills", character.kills);
  154. character.back_row = (ReadInt(element, "back_row") == 1);
  155. ReadUnsigned(element, "exp", character.exp);
  156. ReadUnsigned(element, "exp_to_next", character.exp_to_next);
  157. for (
  158. TiXmlElement* child = element.FirstChildElement(); child != nullptr;
  159. child = child->NextSiblingElement()
  160. ){
  161. const std::string& name = child->ValueStr();
  162. if (name == "Stats"){
  163. ForEachElement(*child, "Stat", [&](TiXmlElement& stat){ReadStat(stat, character);});
  164. }
  165. else if (name == "Limits") ReadLimits(*child, character);
  166. else if (name == "Weapon") ReadEquipment(*child, character.weapon);
  167. else if (name == "Armor") ReadEquipment(*child, character.armor);
  168. else if (name == "Accessory"){
  169. const int accessory = ReadInt(*child, "id", -2);
  170. if (accessory >= -1) character.accessory = accessory;
  171. }
  172. // TODO: Status
  173. }
  174. }
  175. }
  176. void Savemap::Read(std::string file_name){
  177. // TODO: This need a try/catch, it's impossible to validate every value and type in the file.
  178. TiXmlDocument xml_file(file_name);
  179. if (xml_file.LoadFile() == false){
  180. empty_ = true;
  181. return;
  182. }
  183. TiXmlElement* root = xml_file.RootElement();
  184. if (root == nullptr || root->ValueStr() != "Savemap"){
  185. LOG_ERROR(
  186. "Savemap: " + file_name + " is not a valid savemap file! No <Savemap> in root."
  187. );
  188. empty_ = true;
  189. return;
  190. }
  191. root->QueryIntAttribute("slot", &slot_);
  192. root->QueryStringAttribute("key", &control_);
  193. // Games saved without a control key, or with an empty one, get a placeholder.
  194. if (control_.size() < 2) control_ = "X";
  195. empty_ = false;
  196. for (
  197. TiXmlElement* element = root->FirstChildElement(); element != nullptr;
  198. element = element->NextSiblingElement()
  199. ){
  200. const std::string& name = element->ValueStr();
  201. if (name == "WindowColours") ReadWindowColours(*element);
  202. else if (name == "Settings") ReadSettings(*element);
  203. else if (name == "Money") ReadUnsigned(*element, "quantity", money_);
  204. else if (name == "GameTime") ReadUnsigned(*element, "seconds", seconds_);
  205. else if (name == "Countdown") ReadUnsigned(*element, "seconds", countdown_);
  206. else if (name == "Location") ReadLocation(*element);
  207. else if (name == "Party") ReadParty(*element);
  208. else if (name == "Items") ReadItems(*element);
  209. else if (name == "KeyItems") ReadKeyItems(*element);
  210. else if (name == "Materia") ReadMaterias(*element, materia_, MAX_MATERIA_SLOTS);
  211. else if (name == "MateriaStash")
  212. ReadMaterias(*element, materia_stash_, MAX_STASH_SLOTS);
  213. else if (name == "Characters") ReadCharacters(*element);
  214. else if (name == "DataBanks") ReadDataBanks(*element);
  215. }
  216. }
  217. void Savemap::ReadWindowColours(TiXmlElement& element){
  218. static const char* const COMPONENTS[3] = {"red", "green", "blue"};
  219. ForEachElement(element, "Corner", [&](TiXmlElement& corner){
  220. std::string position;
  221. corner.QueryStringAttribute("position", &position);
  222. for (int c = 0; c < 4; c ++){
  223. if (position != CORNER_NAMES[c]) continue;
  224. for (int i = 0; i < 3; i ++){
  225. const int colour = ReadInt(corner, COMPONENTS[i]);
  226. if (colour >= 0 && colour <= static_cast<int>(MAX_COLOUR))
  227. window_colours_[c][i] = colour;
  228. }
  229. }
  230. });
  231. }
  232. void Savemap::ReadSettings(TiXmlElement& element){
  233. ForEachElement(element, "Setting", [&](TiXmlElement& setting){
  234. std::string name;
  235. int value = 0;
  236. if (
  237. setting.QueryStringAttribute("name", &name) == TIXML_SUCCESS
  238. && setting.QueryIntAttribute("value", &value) == TIXML_SUCCESS
  239. ) settings_[name] = value;
  240. });
  241. }
  242. void Savemap::ReadLocation(TiXmlElement& element){
  243. element.QueryFloatAttribute("x", &location_.x);
  244. element.QueryFloatAttribute("y", &location_.y);
  245. element.QueryFloatAttribute("z", &location_.z);
  246. ReadUnsigned(element, "triangle", location_.triangle);
  247. const int angle = ReadInt(element, "orientation");
  248. if (angle >= -360 && angle <= 360) location_.angle = angle;
  249. element.QueryStringAttribute("field", &location_.field);
  250. element.QueryStringAttribute("name", &location_.name);
  251. }
  252. void Savemap::ReadParty(TiXmlElement& element){
  253. ForEachElement(element, "Member", [&](TiXmlElement& member){
  254. const int position = ReadInt(member, "position");
  255. const int character = ReadInt(member, "character_id");
  256. if (
  257. position >= 0 && position < static_cast<int>(MAX_PARTY_MEMBERS)
  258. && character >= 0 && character < static_cast<int>(MAX_CHARACTERS)
  259. ) party_[position] = character;
  260. });
  261. }
  262. void Savemap::ReadItems(TiXmlElement& element){
  263. ForEachElement(element, "Item", [&](TiXmlElement& item){
  264. const int slot = ReadInt(item, "slot");
  265. const int id = ReadInt(item, "id");
  266. const int quantity = ReadInt(item, "quantity");
  267. if (slot >= 0 && slot < static_cast<int>(MAX_ITEM_SLOTS) && id >= 0 && quantity > 0){
  268. items_[slot].id = id;
  269. items_[slot].quantity = quantity;
  270. }
  271. });
  272. }
  273. void Savemap::ReadKeyItems(TiXmlElement& element){
  274. ForEachElement(element, "Item", [&](TiXmlElement& item){
  275. const int id = ReadInt(item, "id");
  276. if (
  277. id >= 0 && id < static_cast<int>(MAX_KEY_ITEM_SLOTS) && ReadInt(item, "owned") == 1
  278. ) key_items_[id] = true;
  279. });
  280. }
  281. void Savemap::ReadCharacters(TiXmlElement& element){
  282. ForEachElement(element, "Character", [&](TiXmlElement& character){
  283. const int id = ReadInt(character, "id");
  284. if (id < 0 || id >= static_cast<int>(MAX_CHARACTERS)) return;
  285. characters_[id].id = id;
  286. ReadCharacter(character, characters_[id]);
  287. });
  288. }
  289. void Savemap::ReadDataBanks(TiXmlElement& element){
  290. ForEachElement(element, "Bank", [&](TiXmlElement& bank){
  291. const int id = ReadInt(bank, "id");
  292. if (id < 0 || id >= static_cast<int>(BANK_COUNT)) return;
  293. ForEachElement(bank, "Address", [&](TiXmlElement& address){
  294. const int number = ReadInt(address, "address");
  295. const int value = ReadInt(address, "value", 0);
  296. if (number >= 0 && number < static_cast<int>(BANK_ADDRESS_COUNT))
  297. data_[id][number] = value;
  298. });
  299. });
  300. }