PixelBullet  0.0.1
A C++ game engine
Loading...
Searching...
No Matches
mesh_binary_layout_internal.h
1#pragma once
2
3#include "pixelbullet/filesystem/native_path.h"
4
5#include <cstdint>
6#include <filesystem>
7#include <limits>
8#include <string>
9#include <system_error>
10
11namespace pixelbullet::mesh_binary_internal
12{
13[[nodiscard]] inline bool AddBytes(std::uint64_t& total, const std::uint64_t bytes) noexcept
14{
15 if (bytes > std::numeric_limits<std::uint64_t>::max() - total)
16 {
17 return false;
18 }
19 total += bytes;
20 return true;
21}
22
23[[nodiscard]] inline bool MultiplyBytes(const std::uint64_t count, const std::uint64_t stride, std::uint64_t& bytes) noexcept
24{
25 if (count != 0u && stride > std::numeric_limits<std::uint64_t>::max() / count)
26 {
27 return false;
28 }
29 bytes = count * stride;
30 return true;
31}
32
33[[nodiscard]] inline bool AddRepeatedBytes(std::uint64_t& total, const std::uint64_t count, const std::uint64_t stride) noexcept
34{
35 std::uint64_t bytes = 0u;
36 return MultiplyBytes(count, stride, bytes) && AddBytes(total, bytes);
37}
38
39[[nodiscard]] inline bool TryGetFileSize(const std::filesystem::path& path, std::uint64_t& size, std::string* error_message)
40{
41 std::error_code error_code;
42 const std::uintmax_t raw_size = std::filesystem::file_size(filesystem::to_native_file_io_path(path), error_code);
43 if (error_code)
44 {
45 if (error_message != nullptr)
46 {
47 *error_message = error_code.message();
48 }
49 return false;
50 }
51
52 if (raw_size > static_cast<std::uintmax_t>(std::numeric_limits<std::uint64_t>::max()))
53 {
54 if (error_message != nullptr)
55 {
56 *error_message = "file size exceeds supported range";
57 }
58 return false;
59 }
60
61 size = static_cast<std::uint64_t>(raw_size);
62 return true;
63}
64
65[[nodiscard]] inline bool FitsFileSize(const std::uint64_t required_size, const std::uint64_t file_size) noexcept
66{
67 return required_size <= file_size;
68}
69} // namespace pixelbullet::mesh_binary_internal