nameidmapping.cpp 1.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748
  1. /*
  2. Minetest
  3. Copyright (C) 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 "nameidmapping.h"
  17. #include "exceptions.h"
  18. #include "util/serialize.h"
  19. void NameIdMapping::serialize(std::ostream &os) const
  20. {
  21. writeU8(os, 0); // version
  22. writeU16(os, m_id_to_name.size());
  23. for (const auto &i : m_id_to_name) {
  24. writeU16(os, i.first);
  25. os << serializeString16(i.second);
  26. }
  27. }
  28. void NameIdMapping::deSerialize(std::istream &is)
  29. {
  30. int version = readU8(is);
  31. if (version != 0)
  32. throw SerializationError("unsupported NameIdMapping version");
  33. u32 count = readU16(is);
  34. m_id_to_name.clear();
  35. m_name_to_id.clear();
  36. for (u32 i = 0; i < count; i++) {
  37. u16 id = readU16(is);
  38. std::string name = deSerializeString16(is);
  39. m_id_to_name[id] = name;
  40. m_name_to_id[name] = id;
  41. }
  42. }