💿🐜 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.

144 lines
3.4 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/serialize-context.hpp"
  20. #include "resources/serialize-error.hpp"
  21. #include <physfs.h>
  22. serialize_context::serialize_context(void* handle):
  23. handle(handle),
  24. m_error(false)
  25. {}
  26. std::size_t serialize_context::write8(const std::byte* data, std::size_t count)
  27. {
  28. const PHYSFS_sint64 status = PHYSFS_writeBytes(reinterpret_cast<PHYSFS_File*>(handle), data, count);
  29. if (status < 0)
  30. {
  31. m_error = true;
  32. throw serialize_error(PHYSFS_getLastError());
  33. //return 0;
  34. }
  35. if (status != count)
  36. {
  37. m_error = true;
  38. throw serialize_error(PHYSFS_getLastError());
  39. //return static_cast<std::size_t>(count);
  40. }
  41. return count;
  42. }
  43. std::size_t serialize_context::write16(const std::byte* data, std::size_t count)
  44. {
  45. PHYSFS_File* file = reinterpret_cast<PHYSFS_File*>(handle);
  46. const PHYSFS_uint16* data16 = reinterpret_cast<const PHYSFS_uint16*>(data);
  47. for (std::size_t i = 0; i < count; ++i)
  48. {
  49. if constexpr (serialize_context::endian == std::endian::little)
  50. {
  51. if (!PHYSFS_writeULE16(file, *data16))
  52. {
  53. m_error = true;
  54. throw serialize_error(PHYSFS_getLastError());
  55. //return i;
  56. }
  57. }
  58. else
  59. {
  60. if (!PHYSFS_writeUBE16(file, *data16))
  61. {
  62. m_error = true;
  63. throw serialize_error(PHYSFS_getLastError());
  64. //return i;
  65. }
  66. }
  67. ++data16;
  68. }
  69. return count;
  70. }
  71. std::size_t serialize_context::write32(const std::byte* data, std::size_t count)
  72. {
  73. PHYSFS_File* file = reinterpret_cast<PHYSFS_File*>(handle);
  74. const PHYSFS_uint32* data32 = reinterpret_cast<const PHYSFS_uint32*>(data);
  75. for (std::size_t i = 0; i < count; ++i)
  76. {
  77. if constexpr (serialize_context::endian == std::endian::little)
  78. {
  79. if (!PHYSFS_writeULE32(file, *data32))
  80. {
  81. m_error = true;
  82. throw serialize_error(PHYSFS_getLastError());
  83. //return i;
  84. }
  85. }
  86. else
  87. {
  88. if (!PHYSFS_writeUBE32(file, *data32))
  89. {
  90. m_error = true;
  91. throw serialize_error(PHYSFS_getLastError());
  92. //return i;
  93. }
  94. }
  95. ++data32;
  96. }
  97. return count;
  98. }
  99. std::size_t serialize_context::write64(const std::byte* data, std::size_t count)
  100. {
  101. PHYSFS_File* file = reinterpret_cast<PHYSFS_File*>(handle);
  102. const PHYSFS_uint64* data64 = reinterpret_cast<const PHYSFS_uint64*>(data);
  103. for (std::size_t i = 0; i < count; ++i)
  104. {
  105. if constexpr (serialize_context::endian == std::endian::little)
  106. {
  107. if (!PHYSFS_writeULE64(file, *data64))
  108. {
  109. m_error = true;
  110. throw serialize_error(PHYSFS_getLastError());
  111. //return i;
  112. }
  113. }
  114. else
  115. {
  116. if (!PHYSFS_writeUBE64(file, *data64))
  117. {
  118. m_error = true;
  119. throw serialize_error(PHYSFS_getLastError());
  120. //return i;
  121. }
  122. }
  123. ++data64;
  124. }
  125. return count;
  126. }