inventory.cpp 21 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999
  1. /*
  2. Minetest
  3. Copyright (C) 2010-2013 celeron55, Perttu Ahola <celeron55@gmail.com>
  4. This program is free software; you can redistribute it and/or modify
  5. it under the terms of the GNU Lesser General Public License as published by
  6. the Free Software Foundation; either version 2.1 of the License, or
  7. (at your option) any later version.
  8. This program is distributed in the hope that it will be useful,
  9. but WITHOUT ANY WARRANTY; without even the implied warranty of
  10. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  11. GNU Lesser General Public License for more details.
  12. You should have received a copy of the GNU Lesser General Public License along
  13. with this program; if not, write to the Free Software Foundation, Inc.,
  14. 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
  15. */
  16. #include "inventory.h"
  17. #include "serialization.h"
  18. #include "debug.h"
  19. #include <algorithm>
  20. #include <sstream>
  21. #include "log.h"
  22. #include "itemdef.h"
  23. #include "util/strfnd.h"
  24. #include "content_mapnode.h" // For loading legacy MaterialItems
  25. #include "nameidmapping.h" // For loading legacy MaterialItems
  26. #include "util/serialize.h"
  27. #include "util/string.h"
  28. /*
  29. ItemStack
  30. */
  31. static content_t content_translate_from_19_to_internal(content_t c_from)
  32. {
  33. for (const auto &tt : trans_table_19) {
  34. if(tt[1] == c_from) {
  35. return tt[0];
  36. }
  37. }
  38. return c_from;
  39. }
  40. ItemStack::ItemStack(const std::string &name_, u16 count_,
  41. u16 wear_, IItemDefManager *itemdef) :
  42. name(itemdef->getAlias(name_)),
  43. count(count_),
  44. wear(wear_)
  45. {
  46. if (name.empty() || count == 0)
  47. clear();
  48. else if (itemdef->get(name).type == ITEM_TOOL)
  49. count = 1;
  50. }
  51. void ItemStack::serialize(std::ostream &os, bool serialize_meta) const
  52. {
  53. if (empty())
  54. return;
  55. // Check how many parts of the itemstring are needed
  56. int parts = 1;
  57. if (!metadata.empty())
  58. parts = 4;
  59. else if (wear != 0)
  60. parts = 3;
  61. else if (count != 1)
  62. parts = 2;
  63. os << serializeJsonStringIfNeeded(name);
  64. if (parts >= 2)
  65. os << " " << count;
  66. if (parts >= 3)
  67. os << " " << wear;
  68. if (parts >= 4) {
  69. os << " ";
  70. if (serialize_meta)
  71. metadata.serialize(os);
  72. else
  73. os << "<metadata size=" << metadata.size() << ">";
  74. }
  75. }
  76. void ItemStack::deSerialize(std::istream &is, IItemDefManager *itemdef)
  77. {
  78. clear();
  79. // Read name
  80. name = deSerializeJsonStringIfNeeded(is);
  81. // Skip space
  82. std::string tmp;
  83. std::getline(is, tmp, ' ');
  84. if(!tmp.empty())
  85. throw SerializationError("Unexpected text after item name");
  86. if(name == "MaterialItem")
  87. {
  88. // Obsoleted on 2011-07-30
  89. u16 material;
  90. is>>material;
  91. u16 materialcount;
  92. is>>materialcount;
  93. // Convert old materials
  94. if(material <= 0xff)
  95. material = content_translate_from_19_to_internal(material);
  96. if(material > 0xfff)
  97. throw SerializationError("Too large material number");
  98. // Convert old id to name
  99. NameIdMapping legacy_nimap;
  100. content_mapnode_get_name_id_mapping(&legacy_nimap);
  101. legacy_nimap.getName(material, name);
  102. if(name.empty())
  103. name = "unknown_block";
  104. if (itemdef)
  105. name = itemdef->getAlias(name);
  106. count = materialcount;
  107. }
  108. else if(name == "MaterialItem2")
  109. {
  110. // Obsoleted on 2011-11-16
  111. u16 material;
  112. is>>material;
  113. u16 materialcount;
  114. is>>materialcount;
  115. if(material > 0xfff)
  116. throw SerializationError("Too large material number");
  117. // Convert old id to name
  118. NameIdMapping legacy_nimap;
  119. content_mapnode_get_name_id_mapping(&legacy_nimap);
  120. legacy_nimap.getName(material, name);
  121. if(name.empty())
  122. name = "unknown_block";
  123. if (itemdef)
  124. name = itemdef->getAlias(name);
  125. count = materialcount;
  126. }
  127. else if(name == "node" || name == "NodeItem" || name == "MaterialItem3"
  128. || name == "craft" || name == "CraftItem")
  129. {
  130. // Obsoleted on 2012-01-07
  131. std::string all;
  132. std::getline(is, all, '\n');
  133. // First attempt to read inside ""
  134. Strfnd fnd(all);
  135. fnd.next("\"");
  136. // If didn't skip to end, we have ""s
  137. if(!fnd.at_end()){
  138. name = fnd.next("\"");
  139. } else { // No luck, just read a word then
  140. fnd.start(all);
  141. name = fnd.next(" ");
  142. }
  143. fnd.skip_over(" ");
  144. if (itemdef)
  145. name = itemdef->getAlias(name);
  146. count = stoi(trim(fnd.next("")));
  147. if(count == 0)
  148. count = 1;
  149. }
  150. else if(name == "MBOItem")
  151. {
  152. // Obsoleted on 2011-10-14
  153. throw SerializationError("MBOItem not supported anymore");
  154. }
  155. else if(name == "tool" || name == "ToolItem")
  156. {
  157. // Obsoleted on 2012-01-07
  158. std::string all;
  159. std::getline(is, all, '\n');
  160. // First attempt to read inside ""
  161. Strfnd fnd(all);
  162. fnd.next("\"");
  163. // If didn't skip to end, we have ""s
  164. if(!fnd.at_end()){
  165. name = fnd.next("\"");
  166. } else { // No luck, just read a word then
  167. fnd.start(all);
  168. name = fnd.next(" ");
  169. }
  170. count = 1;
  171. // Then read wear
  172. fnd.skip_over(" ");
  173. if (itemdef)
  174. name = itemdef->getAlias(name);
  175. wear = stoi(trim(fnd.next("")));
  176. }
  177. else
  178. {
  179. do // This loop is just to allow "break;"
  180. {
  181. // The real thing
  182. // Apply item aliases
  183. if (itemdef)
  184. name = itemdef->getAlias(name);
  185. // Read the count
  186. std::string count_str;
  187. std::getline(is, count_str, ' ');
  188. if (count_str.empty()) {
  189. count = 1;
  190. break;
  191. }
  192. count = stoi(count_str);
  193. // Read the wear
  194. std::string wear_str;
  195. std::getline(is, wear_str, ' ');
  196. if(wear_str.empty())
  197. break;
  198. wear = stoi(wear_str);
  199. // Read metadata
  200. metadata.deSerialize(is);
  201. // In case fields are added after metadata, skip space here:
  202. //std::getline(is, tmp, ' ');
  203. //if(!tmp.empty())
  204. // throw SerializationError("Unexpected text after metadata");
  205. } while(false);
  206. }
  207. if (name.empty() || count == 0)
  208. clear();
  209. else if (itemdef && itemdef->get(name).type == ITEM_TOOL)
  210. count = 1;
  211. }
  212. void ItemStack::deSerialize(const std::string &str, IItemDefManager *itemdef)
  213. {
  214. std::istringstream is(str, std::ios::binary);
  215. deSerialize(is, itemdef);
  216. }
  217. std::string ItemStack::getItemString(bool include_meta) const
  218. {
  219. std::ostringstream os(std::ios::binary);
  220. serialize(os, include_meta);
  221. return os.str();
  222. }
  223. std::string ItemStack::getDescription(IItemDefManager *itemdef) const
  224. {
  225. std::string desc = metadata.getString("description");
  226. if (desc.empty())
  227. desc = getDefinition(itemdef).description;
  228. return desc.empty() ? name : desc;
  229. }
  230. ItemStack ItemStack::addItem(ItemStack newitem, IItemDefManager *itemdef)
  231. {
  232. // If the item is empty or the position invalid, bail out
  233. if(newitem.empty())
  234. {
  235. // nothing can be added trivially
  236. }
  237. // If this is an empty item, it's an easy job.
  238. else if(empty())
  239. {
  240. *this = newitem;
  241. newitem.clear();
  242. }
  243. // If item name or metadata differs, bail out
  244. else if (name != newitem.name
  245. || metadata != newitem.metadata)
  246. {
  247. // cannot be added
  248. }
  249. // If the item fits fully, add counter and delete it
  250. else if(newitem.count <= freeSpace(itemdef))
  251. {
  252. add(newitem.count);
  253. newitem.clear();
  254. }
  255. // Else the item does not fit fully. Add all that fits and return
  256. // the rest.
  257. else
  258. {
  259. u16 freespace = freeSpace(itemdef);
  260. add(freespace);
  261. newitem.remove(freespace);
  262. }
  263. return newitem;
  264. }
  265. bool ItemStack::itemFits(ItemStack newitem,
  266. ItemStack *restitem,
  267. IItemDefManager *itemdef) const
  268. {
  269. // If the item is empty or the position invalid, bail out
  270. if(newitem.empty())
  271. {
  272. // nothing can be added trivially
  273. }
  274. // If this is an empty item, it's an easy job.
  275. else if(empty())
  276. {
  277. newitem.clear();
  278. }
  279. // If item name or metadata differs, bail out
  280. else if (name != newitem.name
  281. || metadata != newitem.metadata)
  282. {
  283. // cannot be added
  284. }
  285. // If the item fits fully, delete it
  286. else if(newitem.count <= freeSpace(itemdef))
  287. {
  288. newitem.clear();
  289. }
  290. // Else the item does not fit fully. Return the rest.
  291. else
  292. {
  293. u16 freespace = freeSpace(itemdef);
  294. newitem.remove(freespace);
  295. }
  296. if(restitem)
  297. *restitem = newitem;
  298. return newitem.empty();
  299. }
  300. ItemStack ItemStack::takeItem(u32 takecount)
  301. {
  302. if(takecount == 0 || count == 0)
  303. return ItemStack();
  304. ItemStack result = *this;
  305. if(takecount >= count)
  306. {
  307. // Take all
  308. clear();
  309. }
  310. else
  311. {
  312. // Take part
  313. remove(takecount);
  314. result.count = takecount;
  315. }
  316. return result;
  317. }
  318. ItemStack ItemStack::peekItem(u32 peekcount) const
  319. {
  320. if(peekcount == 0 || count == 0)
  321. return ItemStack();
  322. ItemStack result = *this;
  323. if(peekcount < count)
  324. result.count = peekcount;
  325. return result;
  326. }
  327. /*
  328. Inventory
  329. */
  330. InventoryList::InventoryList(const std::string &name, u32 size, IItemDefManager *itemdef):
  331. m_name(name),
  332. m_size(size),
  333. m_itemdef(itemdef)
  334. {
  335. clearItems();
  336. }
  337. void InventoryList::clearItems()
  338. {
  339. m_items.clear();
  340. for (u32 i=0; i < m_size; i++) {
  341. m_items.emplace_back();
  342. }
  343. setModified();
  344. }
  345. void InventoryList::setSize(u32 newsize)
  346. {
  347. if (newsize == m_items.size())
  348. return;
  349. m_items.resize(newsize);
  350. m_size = newsize;
  351. setModified();
  352. }
  353. void InventoryList::setWidth(u32 newwidth)
  354. {
  355. m_width = newwidth;
  356. setModified();
  357. }
  358. void InventoryList::setName(const std::string &name)
  359. {
  360. m_name = name;
  361. setModified();
  362. }
  363. void InventoryList::serialize(std::ostream &os, bool incremental) const
  364. {
  365. //os.imbue(std::locale("C"));
  366. os<<"Width "<<m_width<<"\n";
  367. for (const auto &item : m_items) {
  368. if (item.empty()) {
  369. os<<"Empty";
  370. } else {
  371. os<<"Item ";
  372. item.serialize(os);
  373. }
  374. // TODO: Implement this:
  375. // if (!incremental || item.checkModified())
  376. // os << "Keep";
  377. os<<"\n";
  378. }
  379. os<<"EndInventoryList\n";
  380. }
  381. void InventoryList::deSerialize(std::istream &is)
  382. {
  383. //is.imbue(std::locale("C"));
  384. setModified();
  385. u32 item_i = 0;
  386. m_width = 0;
  387. while (is.good()) {
  388. std::string line;
  389. std::getline(is, line, '\n');
  390. std::istringstream iss(line);
  391. //iss.imbue(std::locale("C"));
  392. std::string name;
  393. std::getline(iss, name, ' ');
  394. if (name == "EndInventoryList" || name == "end") {
  395. // If partial incremental: Clear leftover items (should not happen!)
  396. for (size_t i = item_i; i < m_items.size(); ++i)
  397. m_items[i].clear();
  398. return;
  399. }
  400. if (name == "Width") {
  401. iss >> m_width;
  402. if (iss.fail())
  403. throw SerializationError("incorrect width property");
  404. }
  405. else if(name == "Item")
  406. {
  407. if(item_i > getSize() - 1)
  408. throw SerializationError("too many items");
  409. ItemStack item;
  410. item.deSerialize(iss, m_itemdef);
  411. m_items[item_i++] = item;
  412. }
  413. else if(name == "Empty")
  414. {
  415. if(item_i > getSize() - 1)
  416. throw SerializationError("too many items");
  417. m_items[item_i++].clear();
  418. } else if (name == "Keep") {
  419. ++item_i; // Unmodified item
  420. }
  421. }
  422. // Contents given to deSerialize() were not terminated properly: throw error.
  423. std::ostringstream ss;
  424. ss << "Malformatted inventory list. list="
  425. << m_name << ", read " << item_i << " of " << getSize()
  426. << " ItemStacks." << std::endl;
  427. throw SerializationError(ss.str());
  428. }
  429. InventoryList::InventoryList(const InventoryList &other)
  430. {
  431. *this = other;
  432. }
  433. InventoryList & InventoryList::operator = (const InventoryList &other)
  434. {
  435. m_items = other.m_items;
  436. m_size = other.m_size;
  437. m_width = other.m_width;
  438. m_name = other.m_name;
  439. m_itemdef = other.m_itemdef;
  440. //setDirty(true);
  441. return *this;
  442. }
  443. bool InventoryList::operator == (const InventoryList &other) const
  444. {
  445. if(m_size != other.m_size)
  446. return false;
  447. if(m_width != other.m_width)
  448. return false;
  449. if(m_name != other.m_name)
  450. return false;
  451. for (u32 i = 0; i < m_items.size(); i++)
  452. if (m_items[i] != other.m_items[i])
  453. return false;
  454. return true;
  455. }
  456. const std::string &InventoryList::getName() const
  457. {
  458. return m_name;
  459. }
  460. u32 InventoryList::getSize() const
  461. {
  462. return m_items.size();
  463. }
  464. u32 InventoryList::getWidth() const
  465. {
  466. return m_width;
  467. }
  468. u32 InventoryList::getUsedSlots() const
  469. {
  470. u32 num = 0;
  471. for (const auto &m_item : m_items) {
  472. if (!m_item.empty())
  473. num++;
  474. }
  475. return num;
  476. }
  477. u32 InventoryList::getFreeSlots() const
  478. {
  479. return getSize() - getUsedSlots();
  480. }
  481. const ItemStack& InventoryList::getItem(u32 i) const
  482. {
  483. assert(i < m_size); // Pre-condition
  484. return m_items[i];
  485. }
  486. ItemStack& InventoryList::getItem(u32 i)
  487. {
  488. assert(i < m_size); // Pre-condition
  489. return m_items[i];
  490. }
  491. ItemStack InventoryList::changeItem(u32 i, const ItemStack &newitem)
  492. {
  493. if(i >= m_items.size())
  494. return newitem;
  495. ItemStack olditem = m_items[i];
  496. m_items[i] = newitem;
  497. setModified();
  498. return olditem;
  499. }
  500. void InventoryList::deleteItem(u32 i)
  501. {
  502. assert(i < m_items.size()); // Pre-condition
  503. m_items[i].clear();
  504. setModified();
  505. }
  506. ItemStack InventoryList::addItem(const ItemStack &newitem_)
  507. {
  508. ItemStack newitem = newitem_;
  509. if(newitem.empty())
  510. return newitem;
  511. /*
  512. First try to find if it could be added to some existing items
  513. */
  514. for(u32 i=0; i<m_items.size(); i++)
  515. {
  516. // Ignore empty slots
  517. if(m_items[i].empty())
  518. continue;
  519. // Try adding
  520. newitem = addItem(i, newitem);
  521. if(newitem.empty())
  522. return newitem; // All was eaten
  523. }
  524. /*
  525. Then try to add it to empty slots
  526. */
  527. for(u32 i=0; i<m_items.size(); i++)
  528. {
  529. // Ignore unempty slots
  530. if(!m_items[i].empty())
  531. continue;
  532. // Try adding
  533. newitem = addItem(i, newitem);
  534. if(newitem.empty())
  535. return newitem; // All was eaten
  536. }
  537. // Return leftover
  538. return newitem;
  539. }
  540. ItemStack InventoryList::addItem(u32 i, const ItemStack &newitem)
  541. {
  542. if(i >= m_items.size())
  543. return newitem;
  544. ItemStack leftover = m_items[i].addItem(newitem, m_itemdef);
  545. if (leftover != newitem)
  546. setModified();
  547. return leftover;
  548. }
  549. bool InventoryList::itemFits(const u32 i, const ItemStack &newitem,
  550. ItemStack *restitem) const
  551. {
  552. if(i >= m_items.size())
  553. {
  554. if(restitem)
  555. *restitem = newitem;
  556. return false;
  557. }
  558. return m_items[i].itemFits(newitem, restitem, m_itemdef);
  559. }
  560. bool InventoryList::roomForItem(const ItemStack &item_) const
  561. {
  562. ItemStack item = item_;
  563. ItemStack leftover;
  564. for(u32 i=0; i<m_items.size(); i++)
  565. {
  566. if(itemFits(i, item, &leftover))
  567. return true;
  568. item = leftover;
  569. }
  570. return false;
  571. }
  572. bool InventoryList::containsItem(const ItemStack &item, bool match_meta) const
  573. {
  574. u32 count = item.count;
  575. if (count == 0)
  576. return true;
  577. for (auto i = m_items.rbegin(); i != m_items.rend(); ++i) {
  578. if (count == 0)
  579. break;
  580. if (i->name == item.name && (!match_meta || (i->metadata == item.metadata))) {
  581. if (i->count >= count)
  582. return true;
  583. count -= i->count;
  584. }
  585. }
  586. return false;
  587. }
  588. ItemStack InventoryList::removeItem(const ItemStack &item)
  589. {
  590. ItemStack removed;
  591. for (auto i = m_items.rbegin(); i != m_items.rend(); ++i) {
  592. if (i->name == item.name) {
  593. u32 still_to_remove = item.count - removed.count;
  594. ItemStack leftover = removed.addItem(i->takeItem(still_to_remove),
  595. m_itemdef);
  596. // Allow oversized stacks
  597. removed.count += leftover.count;
  598. if (removed.count == item.count)
  599. break;
  600. }
  601. }
  602. if (!removed.empty())
  603. setModified();
  604. return removed;
  605. }
  606. ItemStack InventoryList::takeItem(u32 i, u32 takecount)
  607. {
  608. if(i >= m_items.size())
  609. return ItemStack();
  610. ItemStack taken = m_items[i].takeItem(takecount);
  611. if (!taken.empty())
  612. setModified();
  613. return taken;
  614. }
  615. void InventoryList::moveItemSomewhere(u32 i, InventoryList *dest, u32 count)
  616. {
  617. // Take item from source list
  618. ItemStack item1;
  619. if (count == 0)
  620. item1 = changeItem(i, ItemStack());
  621. else
  622. item1 = takeItem(i, count);
  623. if (item1.empty())
  624. return;
  625. ItemStack leftover;
  626. leftover = dest->addItem(item1);
  627. if (!leftover.empty()) {
  628. // Add the remaining part back to the source item
  629. addItem(i, leftover);
  630. }
  631. }
  632. u32 InventoryList::moveItem(u32 i, InventoryList *dest, u32 dest_i,
  633. u32 count, bool swap_if_needed, bool *did_swap)
  634. {
  635. if(this == dest && i == dest_i)
  636. return count;
  637. // Take item from source list
  638. ItemStack item1;
  639. if(count == 0)
  640. item1 = changeItem(i, ItemStack());
  641. else
  642. item1 = takeItem(i, count);
  643. if(item1.empty())
  644. return 0;
  645. // Try to add the item to destination list
  646. u32 oldcount = item1.count;
  647. item1 = dest->addItem(dest_i, item1);
  648. // If something is returned, the item was not fully added
  649. if(!item1.empty())
  650. {
  651. // If olditem is returned, nothing was added.
  652. bool nothing_added = (item1.count == oldcount);
  653. // If something else is returned, part of the item was left unadded.
  654. // Add the other part back to the source item
  655. addItem(i, item1);
  656. // If olditem is returned, nothing was added.
  657. // Swap the items
  658. if (nothing_added && swap_if_needed) {
  659. // Tell that we swapped
  660. if (did_swap != NULL) {
  661. *did_swap = true;
  662. }
  663. // Take item from source list
  664. item1 = changeItem(i, ItemStack());
  665. // Adding was not possible, swap the items.
  666. ItemStack item2 = dest->changeItem(dest_i, item1);
  667. // Put item from destination list to the source list
  668. changeItem(i, item2);
  669. }
  670. }
  671. return (oldcount - item1.count);
  672. }
  673. /*
  674. Inventory
  675. */
  676. Inventory::~Inventory()
  677. {
  678. clear();
  679. }
  680. void Inventory::clear()
  681. {
  682. for (auto &m_list : m_lists) {
  683. delete m_list;
  684. }
  685. m_lists.clear();
  686. setModified();
  687. }
  688. Inventory::Inventory(IItemDefManager *itemdef)
  689. {
  690. m_itemdef = itemdef;
  691. setModified();
  692. }
  693. Inventory::Inventory(const Inventory &other)
  694. {
  695. *this = other;
  696. }
  697. Inventory & Inventory::operator = (const Inventory &other)
  698. {
  699. // Gracefully handle self assignment
  700. if(this != &other)
  701. {
  702. clear();
  703. m_itemdef = other.m_itemdef;
  704. for (InventoryList *list : other.m_lists) {
  705. m_lists.push_back(new InventoryList(*list));
  706. }
  707. setModified();
  708. }
  709. return *this;
  710. }
  711. bool Inventory::operator == (const Inventory &other) const
  712. {
  713. if(m_lists.size() != other.m_lists.size())
  714. return false;
  715. for(u32 i=0; i<m_lists.size(); i++)
  716. {
  717. if(*m_lists[i] != *other.m_lists[i])
  718. return false;
  719. }
  720. return true;
  721. }
  722. void Inventory::serialize(std::ostream &os, bool incremental) const
  723. {
  724. //std::cout << "Serialize " << (int)incremental << ", n=" << m_lists.size() << std::endl;
  725. for (const InventoryList *list : m_lists) {
  726. if (!incremental || list->checkModified()) {
  727. os << "List " << list->getName() << " " << list->getSize() << "\n";
  728. list->serialize(os, incremental);
  729. } else {
  730. os << "KeepList " << list->getName() << "\n";
  731. }
  732. }
  733. os<<"EndInventory\n";
  734. }
  735. void Inventory::deSerialize(std::istream &is)
  736. {
  737. std::vector<InventoryList *> new_lists;
  738. new_lists.reserve(m_lists.size());
  739. while (is.good()) {
  740. std::string line;
  741. std::getline(is, line, '\n');
  742. std::istringstream iss(line);
  743. std::string name;
  744. std::getline(iss, name, ' ');
  745. if (name == "EndInventory" || name == "end") {
  746. // Remove all lists that were not sent
  747. for (auto &list : m_lists) {
  748. if (std::find(new_lists.begin(), new_lists.end(), list) != new_lists.end())
  749. continue;
  750. delete list;
  751. list = nullptr;
  752. setModified();
  753. }
  754. m_lists.erase(std::remove(m_lists.begin(), m_lists.end(),
  755. nullptr), m_lists.end());
  756. return;
  757. }
  758. if (name == "List") {
  759. std::string listname;
  760. u32 listsize;
  761. std::getline(iss, listname, ' ');
  762. iss>>listsize;
  763. InventoryList *list = getList(listname);
  764. bool create_new = !list;
  765. if (create_new)
  766. list = new InventoryList(listname, listsize, m_itemdef);
  767. else
  768. list->setSize(listsize);
  769. list->deSerialize(is);
  770. new_lists.push_back(list);
  771. if (create_new)
  772. m_lists.push_back(list);
  773. } else if (name == "KeepList") {
  774. // Incrementally sent list
  775. std::string listname;
  776. std::getline(iss, listname, ' ');
  777. InventoryList *list = getList(listname);
  778. if (list) {
  779. new_lists.push_back(list);
  780. } else {
  781. errorstream << "Inventory::deSerialize(): Tried to keep list '" <<
  782. listname << "' which is non-existent." << std::endl;
  783. }
  784. }
  785. // Any additional fields will throw errors when received by a client
  786. // older than PROTOCOL_VERSION 38
  787. }
  788. // Contents given to deSerialize() were not terminated properly: throw error.
  789. std::ostringstream ss;
  790. ss << "Malformatted inventory (damaged?). "
  791. << m_lists.size() << " lists read." << std::endl;
  792. throw SerializationError(ss.str());
  793. }
  794. InventoryList * Inventory::addList(const std::string &name, u32 size)
  795. {
  796. setModified();
  797. s32 i = getListIndex(name);
  798. if(i != -1)
  799. {
  800. if(m_lists[i]->getSize() != size)
  801. {
  802. delete m_lists[i];
  803. m_lists[i] = new InventoryList(name, size, m_itemdef);
  804. m_lists[i]->setModified();
  805. }
  806. return m_lists[i];
  807. }
  808. //don't create list with invalid name
  809. if (name.find(' ') != std::string::npos)
  810. return nullptr;
  811. InventoryList *list = new InventoryList(name, size, m_itemdef);
  812. list->setModified();
  813. m_lists.push_back(list);
  814. return list;
  815. }
  816. InventoryList * Inventory::getList(const std::string &name)
  817. {
  818. s32 i = getListIndex(name);
  819. if(i == -1)
  820. return NULL;
  821. return m_lists[i];
  822. }
  823. std::vector<const InventoryList*> Inventory::getLists()
  824. {
  825. std::vector<const InventoryList*> lists;
  826. for (auto list : m_lists) {
  827. lists.push_back(list);
  828. }
  829. return lists;
  830. }
  831. bool Inventory::deleteList(const std::string &name)
  832. {
  833. s32 i = getListIndex(name);
  834. if(i == -1)
  835. return false;
  836. setModified();
  837. delete m_lists[i];
  838. m_lists.erase(m_lists.begin() + i);
  839. return true;
  840. }
  841. const InventoryList * Inventory::getList(const std::string &name) const
  842. {
  843. s32 i = getListIndex(name);
  844. if(i == -1)
  845. return NULL;
  846. return m_lists[i];
  847. }
  848. const s32 Inventory::getListIndex(const std::string &name) const
  849. {
  850. for(u32 i=0; i<m_lists.size(); i++)
  851. {
  852. if(m_lists[i]->getName() == name)
  853. return i;
  854. }
  855. return -1;
  856. }
  857. //END