💿🐜 Antkeeper source code https://antkeeper.com
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

176 lines
4.1 KiB

  1. /*
  2. * Copyright (C) 2023 Christopher J. Howard
  3. *
  4. * This file is part of Antkeeper source code.
  5. *
  6. * Antkeeper source code is free software: you can redistribute it and/or modify
  7. * it under the terms of the GNU General Public License as published by
  8. * the Free Software Foundation, either version 3 of the License, or
  9. * (at your option) any later version.
  10. *
  11. * Antkeeper source code is distributed in the hope that it will be useful,
  12. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  13. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  14. * GNU General Public License for more details.
  15. *
  16. * You should have received a copy of the GNU General Public License
  17. * along with Antkeeper source code. If not, see <http://www.gnu.org/licenses/>.
  18. */
  19. #include "resources/deserialize-context.hpp"
  20. #include "resources/deserialize-error.hpp"
  21. #include <physfs.h>
  22. deserialize_context::deserialize_context(void* handle):
  23. handle(handle),
  24. m_eof(false),
  25. m_error(false)
  26. {}
  27. std::size_t deserialize_context::read8(std::byte* data, std::size_t count)
  28. {
  29. const PHYSFS_sint64 status = PHYSFS_readBytes(reinterpret_cast<PHYSFS_File*>(handle), data, count);
  30. if (status < 0)
  31. {
  32. m_error = true;
  33. throw deserialize_error(PHYSFS_getLastError());
  34. //return 0;
  35. }
  36. if (status != count)
  37. {
  38. m_eof = true;
  39. m_error = true;
  40. throw deserialize_error(PHYSFS_getLastError());
  41. //return static_cast<std::size_t>(count);
  42. }
  43. return count;
  44. }
  45. std::size_t deserialize_context::read16_le(std::byte* data, std::size_t count)
  46. {
  47. PHYSFS_File* file = reinterpret_cast<PHYSFS_File*>(handle);
  48. PHYSFS_uint16* data16 = reinterpret_cast<PHYSFS_uint16*>(data);
  49. for (std::size_t i = 0; i < count; ++i)
  50. {
  51. if (!PHYSFS_readULE16(file, data16))
  52. {
  53. m_eof = (PHYSFS_eof(file) != 0);
  54. m_error = true;
  55. throw deserialize_error(PHYSFS_getLastError());
  56. //return i;
  57. }
  58. ++data16;
  59. }
  60. return count;
  61. }
  62. std::size_t deserialize_context::read16_be(std::byte* data, std::size_t count)
  63. {
  64. PHYSFS_File* file = reinterpret_cast<PHYSFS_File*>(handle);
  65. PHYSFS_uint16* data16 = reinterpret_cast<PHYSFS_uint16*>(data);
  66. for (std::size_t i = 0; i < count; ++i)
  67. {
  68. if (!PHYSFS_readUBE16(file, data16))
  69. {
  70. m_eof = (PHYSFS_eof(file) != 0);
  71. m_error = true;
  72. throw deserialize_error(PHYSFS_getLastError());
  73. //return i;
  74. }
  75. ++data16;
  76. }
  77. return count;
  78. }
  79. std::size_t deserialize_context::read32_le(std::byte* data, std::size_t count)
  80. {
  81. PHYSFS_File* file = reinterpret_cast<PHYSFS_File*>(handle);
  82. PHYSFS_uint32* data32 = reinterpret_cast<PHYSFS_uint32*>(data);
  83. for (std::size_t i = 0; i < count; ++i)
  84. {
  85. if (!PHYSFS_readULE32(file, data32))
  86. {
  87. m_eof = (PHYSFS_eof(file) != 0);
  88. m_error = true;
  89. throw deserialize_error(PHYSFS_getLastError());
  90. //return i;
  91. }
  92. ++data32;
  93. }
  94. return count;
  95. }
  96. std::size_t deserialize_context::read32_be(std::byte* data, std::size_t count)
  97. {
  98. PHYSFS_File* file = reinterpret_cast<PHYSFS_File*>(handle);
  99. PHYSFS_uint32* data32 = reinterpret_cast<PHYSFS_uint32*>(data);
  100. for (std::size_t i = 0; i < count; ++i)
  101. {
  102. if (!PHYSFS_readUBE32(file, data32))
  103. {
  104. m_eof = (PHYSFS_eof(file) != 0);
  105. m_error = true;
  106. throw deserialize_error(PHYSFS_getLastError());
  107. //return i;
  108. }
  109. ++data32;
  110. }
  111. return count;
  112. }
  113. std::size_t deserialize_context::read64_le(std::byte* data, std::size_t count)
  114. {
  115. PHYSFS_File* file = reinterpret_cast<PHYSFS_File*>(handle);
  116. PHYSFS_uint64* data64 = reinterpret_cast<PHYSFS_uint64*>(data);
  117. for (std::size_t i = 0; i < count; ++i)
  118. {
  119. if (!PHYSFS_readULE64(file, data64))
  120. {
  121. m_eof = (PHYSFS_eof(file) != 0);
  122. m_error = true;
  123. throw deserialize_error(PHYSFS_getLastError());
  124. //return i;
  125. }
  126. ++data64;
  127. }
  128. return count;
  129. }
  130. std::size_t deserialize_context::read64_be(std::byte* data, std::size_t count)
  131. {
  132. PHYSFS_File* file = reinterpret_cast<PHYSFS_File*>(handle);
  133. PHYSFS_uint64* data64 = reinterpret_cast<PHYSFS_uint64*>(data);
  134. for (std::size_t i = 0; i < count; ++i)
  135. {
  136. if (!PHYSFS_readUBE64(file, data64))
  137. {
  138. m_eof = (PHYSFS_eof(file) != 0);
  139. m_error = true;
  140. throw deserialize_error(PHYSFS_getLastError());
  141. //return i;
  142. }
  143. ++data64;
  144. }
  145. return count;
  146. }