serialization.h 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109
  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. #pragma once
  17. #include "irrlichttypes.h"
  18. #include "exceptions.h"
  19. #include <iostream>
  20. #include <string_view>
  21. /*
  22. Map format serialization version
  23. --------------------------------
  24. For map data (blocks, nodes, sectors).
  25. NOTE: The goal is to increment this so that saved maps will be
  26. loadable by any version. Other compatibility is not
  27. maintained.
  28. 0: original networked test with 1-byte nodes
  29. 1: update with 2-byte nodes
  30. 2: lighting is transmitted in param
  31. 3: optional fetching of far blocks
  32. 4: block compression
  33. 5: sector objects NOTE: block compression was left accidentally out
  34. 6: failed attempt at switching block compression on again
  35. 7: block compression switched on again
  36. 8: server-initiated block transfers and all kinds of stuff
  37. 9: block objects
  38. 10: water pressure
  39. 11: zlib'd blocks, block flags
  40. 12: UnlimitedHeightmap now uses interpolated areas
  41. 13: Mapgen v2
  42. 14: NodeMetadata
  43. 15: StaticObjects
  44. 16: larger maximum size of node metadata, and compression
  45. 17: MapBlocks contain timestamp
  46. 18: new generator (not really necessary, but it's there)
  47. 19: new content type handling
  48. 20: many existing content types translated to extended ones
  49. 21: dynamic content type allocation
  50. 22: minerals removed, facedir & wallmounted changed
  51. 23: new node metadata format
  52. 24: 16-bit node ids and node timers (never released as stable)
  53. 25: Improved node timer format
  54. 26: Never written; read the same as 25
  55. 27: Added light spreading flags to blocks
  56. 28: Added "private" flag to NodeMetadata
  57. 29: Switched compression to zstd, a bit of reorganization
  58. */
  59. // This represents an uninitialized or invalid format
  60. #define SER_FMT_VER_INVALID 255
  61. // Highest supported serialization version
  62. #define SER_FMT_VER_HIGHEST_READ 29
  63. // Saved on disk version
  64. #define SER_FMT_VER_HIGHEST_WRITE 29
  65. // Lowest supported serialization version
  66. #define SER_FMT_VER_LOWEST_READ 0
  67. // Lowest serialization version for writing
  68. // Can't do < 24 anymore; we have 16-bit dynamically allocated node IDs
  69. // in memory; conversion just won't work in this direction.
  70. #define SER_FMT_VER_LOWEST_WRITE 24
  71. inline bool ser_ver_supported(s32 v) {
  72. return v >= SER_FMT_VER_LOWEST_READ && v <= SER_FMT_VER_HIGHEST_READ;
  73. }
  74. /*
  75. Compression functions
  76. */
  77. void compressZlib(const u8 *data, size_t data_size, std::ostream &os, int level = -1);
  78. inline void compressZlib(std::string_view data, std::ostream &os, int level = -1)
  79. {
  80. compressZlib(reinterpret_cast<const u8*>(data.data()), data.size(), os, level);
  81. }
  82. void decompressZlib(std::istream &is, std::ostream &os, size_t limit = 0);
  83. void compressZstd(const u8 *data, size_t data_size, std::ostream &os, int level = 0);
  84. inline void compressZstd(std::string_view data, std::ostream &os, int level = 0)
  85. {
  86. compressZstd(reinterpret_cast<const u8*>(data.data()), data.size(), os, level);
  87. }
  88. void decompressZstd(std::istream &is, std::ostream &os);
  89. // These choose between zstd, zlib and a self-made one according to version
  90. void compress(const u8 *data, u32 size, std::ostream &os, u8 version, int level = -1);
  91. inline void compress(std::string_view data, std::ostream &os, u8 version, int level = -1)
  92. {
  93. compress(reinterpret_cast<const u8*>(data.data()), data.size(), os, version, level);
  94. }
  95. void decompress(std::istream &is, std::ostream &os, u8 version);