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

92 lines
3.2 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. #ifndef ANTKEEPER_RESOURCES_PHYSFS_DESERIALIZE_CONTEXT_HPP
  20. #define ANTKEEPER_RESOURCES_PHYSFS_DESERIALIZE_CONTEXT_HPP
  21. #include <engine/resources/deserialize-context.hpp>
  22. #include <physfs.h>
  23. #include <filesystem>
  24. /**
  25. * Deserialize context implementation using PhysicsFS.
  26. */
  27. class physfs_deserialize_context: public deserialize_context
  28. {
  29. public:
  30. /**
  31. * Constructs a PhysicsFS deserialize context, opening a file using PhysicsFS and associating it with this deserialize context.
  32. *
  33. * @param path Path to a file to open.
  34. *
  35. * @throw deserialize_error File open error.
  36. */
  37. explicit physfs_deserialize_context(const std::filesystem::path& path) noexcept(false);
  38. /**
  39. * Constructs a PhysicsFS deserialize context.
  40. */
  41. physfs_deserialize_context() noexcept = default;
  42. /**
  43. * Destructs a PhysicsFS deserialize context, internally closing a file using PhysicsFS.
  44. */
  45. virtual ~physfs_deserialize_context();
  46. /**
  47. * Opens a file using PhysicsFS and associates it with the deserialize context.
  48. *
  49. * @param path Path to a file to open.
  50. *
  51. * @throw deserialize_error File open error.
  52. */
  53. void open(const std::filesystem::path& path) noexcept(false);
  54. /**
  55. * Closes the associated file using PhysicsFS.
  56. */
  57. void close() noexcept;
  58. /**
  59. * Returns `true` if the PhysicsFS file associated with this deserialize context is open, `false` otherwise.
  60. */
  61. [[nodiscard]] bool is_open() const noexcept;
  62. [[nodiscard]] const std::filesystem::path& path() const noexcept override;
  63. [[nodiscard]] bool error() const noexcept override;
  64. [[nodiscard]] bool eof() const noexcept override;
  65. [[nodiscard]] std::size_t size() const noexcept override;
  66. [[nodiscard]] std::size_t tell() const override;
  67. void seek(std::size_t offset) override;
  68. std::size_t read8(std::byte* data, std::size_t count) noexcept(false) override;
  69. std::size_t read16_le(std::byte* data, std::size_t count) noexcept(false) override;
  70. std::size_t read16_be(std::byte* data, std::size_t count) noexcept(false) override;
  71. std::size_t read32_le(std::byte* data, std::size_t count) noexcept(false) override;
  72. std::size_t read32_be(std::byte* data, std::size_t count) noexcept(false) override;
  73. std::size_t read64_le(std::byte* data, std::size_t count) noexcept(false) override;
  74. std::size_t read64_be(std::byte* data, std::size_t count) noexcept(false) override;
  75. private:
  76. PHYSFS_File* file{nullptr};
  77. std::filesystem::path m_path;
  78. bool m_eof{true};
  79. bool m_error{false};
  80. };
  81. #endif // ANTKEEPER_RESOURCES_PHYSFS_DESERIALIZE_CONTEXT_HPP