From 72fabc932954f4b0680bb04de67620fac9a07b4e Mon Sep 17 00:00:00 2001 From: Jack Andersen Date: Sat, 12 Oct 2019 18:54:07 -1000 Subject: [PATCH] Minor blender exception detection bug fix --- hecl/extern/athena | 2 +- hecl/extern/boo | 2 +- hecl/include/hecl/Blender/Connection.hpp | 50 ++++++++++++------------ hecl/include/hecl/FourCC.hpp | 9 +++-- hecl/lib/Blender/Connection.cpp | 42 ++++++++++++-------- hecl/lib/ProjectPath.cpp | 5 +-- 6 files changed, 60 insertions(+), 50 deletions(-) diff --git a/hecl/extern/athena b/hecl/extern/athena index 42581c922..ee012692b 160000 --- a/hecl/extern/athena +++ b/hecl/extern/athena @@ -1 +1 @@ -Subproject commit 42581c922a4574f4f34df134a454effa9f9cc8d0 +Subproject commit ee012692ba6981a5889d38e76e717e2cb0216e57 diff --git a/hecl/extern/boo b/hecl/extern/boo index 922fcbb3c..314563614 160000 --- a/hecl/extern/boo +++ b/hecl/extern/boo @@ -1 +1 @@ -Subproject commit 922fcbb3c23677b3ccc53737fd23ada165ccf9f3 +Subproject commit 314563614e0e2e2a6f020a423096900e79220119 diff --git a/hecl/include/hecl/Blender/Connection.hpp b/hecl/include/hecl/Blender/Connection.hpp index 1e4432141..7ba8b6581 100644 --- a/hecl/include/hecl/Blender/Connection.hpp +++ b/hecl/include/hecl/Blender/Connection.hpp @@ -45,10 +45,10 @@ class HMDLBuffers; extern logvisor::Module BlenderLog; struct PoolSkinIndex { - size_t m_poolSz = 0; + std::size_t m_poolSz = 0; std::unique_ptr m_poolToSkinIndex; - void allocate(size_t poolSz) { + void allocate(std::size_t poolSz) { m_poolSz = poolSz; if (poolSz) m_poolToSkinIndex.reset(new uint32_t[poolSz]); @@ -145,15 +145,15 @@ struct Vector4f { }; struct Matrix3f { std::array m; - atVec3f& operator[](size_t idx) { return m[idx]; } - const atVec3f& operator[](size_t idx) const { return m[idx]; } + atVec3f& operator[](std::size_t idx) { return m[idx]; } + const atVec3f& operator[](std::size_t idx) const { return m[idx]; } }; struct Matrix4f { std::array val; Matrix4f() = default; Matrix4f(Connection& conn) { read(conn); } void read(Connection& conn); - const atVec4f& operator[](size_t idx) const { return val[idx]; } + const atVec4f& operator[](std::size_t idx) const { return val[idx]; } }; struct Index { uint32_t val; @@ -289,9 +289,9 @@ struct Material { /** Intermediate mesh representation prepared by blender from a single mesh object */ struct Mesh { - static constexpr size_t MaxColorLayers = 4; - static constexpr size_t MaxUVLayers = 8; - static constexpr size_t MaxSkinEntries = 16; + static constexpr std::size_t MaxColorLayers = 4; + static constexpr std::size_t MaxUVLayers = 8; + static constexpr std::size_t MaxSkinEntries = 16; HMDLTopology topology; @@ -325,10 +325,10 @@ struct Mesh { bool operator==(const SkinBind& other) const { return vg_idx == other.vg_idx && weight == other.weight; } }; std::vector> skins; - std::vector contiguousSkinVertCounts; + std::vector contiguousSkinVertCounts; - static size_t countSkinBinds(const std::array& arr) { - size_t ret = 0; + static std::size_t countSkinBinds(const std::array& arr) { + std::size_t ret = 0; for (const auto& b : arr) if (b.valid()) ++ret; @@ -553,7 +553,7 @@ struct Armature { std::vector bones; const Bone* lookupBone(const char* name) const; const Bone* getParent(const Bone* bone) const; - const Bone* getChild(const Bone* bone, size_t child) const; + const Bone* getChild(const Bone* bone, std::size_t child) const; const Bone* getRoot() const; Armature(Connection& conn); }; @@ -734,8 +734,8 @@ class Connection { uint32_t _writeStr(const char* str, uint32_t len, int wpipe); uint32_t _writeStr(const char* str, uint32_t len) { return _writeStr(str, len, m_writepipe[1]); } uint32_t _writeStr(std::string_view view) { return _writeStr(view.data(), view.size()); } - size_t _readBuf(void* buf, size_t len); - size_t _writeBuf(const void* buf, size_t len); + std::size_t _readBuf(void* buf, std::size_t len); + std::size_t _writeBuf(const void* buf, std::size_t len); std::string _readStdString() { uint32_t bufSz; _readBuf(&bufSz, 4); @@ -868,14 +868,14 @@ public: private: friend struct Mesh; - HMDLBuffers(HMDLMeta&& meta, size_t vboSz, const std::vector& iboData, std::vector&& surfaces, + HMDLBuffers(HMDLMeta&& meta, std::size_t vboSz, const std::vector& iboData, std::vector&& surfaces, const Mesh::SkinBanks& skinBanks); public: HMDLMeta m_meta; - size_t m_vboSz; + std::size_t m_vboSz; std::unique_ptr m_vboData; - size_t m_iboSz; + std::size_t m_iboSz; std::unique_ptr m_iboData; struct Surface { @@ -895,16 +895,16 @@ public: namespace std { template <> struct hash { - size_t operator()(const hecl::blender::Vector2f& val) const noexcept { - size_t h = std::hash()(val.val.simd[0]); + std::size_t operator()(const hecl::blender::Vector2f& val) const noexcept { + std::size_t h = std::hash()(val.val.simd[0]); hecl::hash_combine_impl(h, std::hash()(val.val.simd[1])); return h; } }; template <> struct hash { - size_t operator()(const hecl::blender::Vector3f& val) const noexcept { - size_t h = std::hash()(val.val.simd[0]); + std::size_t operator()(const hecl::blender::Vector3f& val) const noexcept { + std::size_t h = std::hash()(val.val.simd[0]); hecl::hash_combine_impl(h, std::hash()(val.val.simd[1])); hecl::hash_combine_impl(h, std::hash()(val.val.simd[2])); return h; @@ -912,8 +912,8 @@ struct hash { }; template <> struct hash { - size_t operator()(const hecl::blender::Vector4f& val) const noexcept { - size_t h = std::hash()(val.val.simd[0]); + std::size_t operator()(const hecl::blender::Vector4f& val) const noexcept { + std::size_t h = std::hash()(val.val.simd[0]); hecl::hash_combine_impl(h, std::hash()(val.val.simd[1])); hecl::hash_combine_impl(h, std::hash()(val.val.simd[2])); hecl::hash_combine_impl(h, std::hash()(val.val.simd[3])); @@ -922,8 +922,8 @@ struct hash { }; template <> struct hash> { - size_t operator()(const array& val) const noexcept { - size_t h = 0; + std::size_t operator()(const array& val) const noexcept { + std::size_t h = 0; for (const auto& bind : val) { if (!bind.valid()) break; diff --git a/hecl/include/hecl/FourCC.hpp b/hecl/include/hecl/FourCC.hpp index 11dabed0c..0f426c441 100644 --- a/hecl/include/hecl/FourCC.hpp +++ b/hecl/include/hecl/FourCC.hpp @@ -46,6 +46,7 @@ public: constexpr bool operator!=(uint32_t other) const noexcept { return !operator==(other); } std::string toString() const { return std::string(std::begin(fcc), std::end(fcc)); } + constexpr std::string_view toStringView() const { return std::string_view(fcc, std::size(fcc)); } constexpr uint32_t toUint32() const noexcept { return num; } constexpr const char* getChars() const noexcept { return fcc; } constexpr char* getChars() noexcept { return fcc; } @@ -58,10 +59,10 @@ using BigDNA = athena::io::DNA; /** FourCC with DNA read/write */ class DNAFourCC final : public BigDNA, public FourCC { public: - DNAFourCC() : FourCC() {} - DNAFourCC(const FourCC& other) : FourCC() { num = other.toUint32(); } - DNAFourCC(const char* name) : FourCC(name) {} - DNAFourCC(uint32_t n) : FourCC(n) {} + constexpr DNAFourCC() : FourCC() {} + constexpr DNAFourCC(const FourCC& other) : FourCC() { num = other.toUint32(); } + constexpr DNAFourCC(const char* name) : FourCC(name) {} + constexpr DNAFourCC(uint32_t n) : FourCC(n) {} AT_DECL_EXPLICIT_DNA_YAML }; template <> diff --git a/hecl/lib/Blender/Connection.cpp b/hecl/lib/Blender/Connection.cpp index 6960ad5a6..4e80cf17f 100644 --- a/hecl/lib/Blender/Connection.cpp +++ b/hecl/lib/Blender/Connection.cpp @@ -36,7 +36,7 @@ namespace std { template <> struct hash> { - size_t operator()(const std::pair& val) const noexcept { + std::size_t operator()(const std::pair& val) const noexcept { /* this will potentially truncate the second value if 32-bit size_t, * however, its application here is intended to operate in 16-bit indices */ return val.first | (val.second << 16); @@ -86,7 +86,7 @@ static void InstallAddon(const SystemChar* path) { std::fwrite(HECL_ADDON, 1, HECL_ADDON_SZ, fp.get()); } -static int Read(int fd, void* buf, size_t size) { +static int Read(int fd, void* buf, std::size_t size) { int intrCount = 0; do { auto ret = read(fd, buf, size); @@ -101,7 +101,7 @@ static int Read(int fd, void* buf, size_t size) { return -1; } -static int Write(int fd, const void* buf, size_t size) { +static int Write(int fd, const void* buf, std::size_t size) { int intrCount = 0; do { auto ret = write(fd, buf, size); @@ -116,6 +116,14 @@ static int Write(int fd, const void* buf, size_t size) { return -1; } +static std::size_t BoundedStrLen(const char* buf, std::size_t maxLen) { + std::size_t ret; + for (ret = 0; ret < maxLen; ++ret) + if (buf[ret] == '\0') + break; + return ret; +} + uint32_t Connection::_readStr(char* buf, uint32_t bufSz) { uint32_t readLen; int ret = Read(m_readpipe[0], &readLen, sizeof(readLen)); @@ -138,8 +146,9 @@ uint32_t Connection::_readStr(char* buf, uint32_t bufSz) { } constexpr std::string_view exception_str{"EXCEPTION"}; - if (readLen >= exception_str.size()) { - if (exception_str.compare(0, exception_str.size(), buf) == 0) { + const std::size_t readStrLen = BoundedStrLen(buf, readLen); + if (readStrLen >= exception_str.size()) { + if (exception_str.compare(0, exception_str.size(), std::string_view(buf, readStrLen)) == 0) { _blenderDied(); return 0; } @@ -168,14 +177,14 @@ uint32_t Connection::_writeStr(const char* buf, uint32_t len, int wpipe) { return static_cast(ret); } -size_t Connection::_readBuf(void* buf, size_t len) { +std::size_t Connection::_readBuf(void* buf, std::size_t len) { const auto error = [this] { _blenderDied(); return 0U; }; auto* cBuf = static_cast(buf); - size_t readLen = 0; + std::size_t readLen = 0; do { const int ret = Read(m_readpipe[0], cBuf, len); @@ -184,8 +193,9 @@ size_t Connection::_readBuf(void* buf, size_t len) { } constexpr std::string_view exception_str{"EXCEPTION"}; - if (len >= exception_str.size()) { - if (exception_str.compare(0, exception_str.size(), static_cast(buf)) == 0) { + const std::size_t readStrLen = BoundedStrLen(static_cast(buf), len); + if (readStrLen >= exception_str.size()) { + if (exception_str.compare(0, exception_str.size(), std::string_view(static_cast(buf), readStrLen)) == 0) { _blenderDied(); } } @@ -198,14 +208,14 @@ size_t Connection::_readBuf(void* buf, size_t len) { return readLen; } -size_t Connection::_writeBuf(const void* buf, size_t len) { +std::size_t Connection::_writeBuf(const void* buf, std::size_t len) { const auto error = [this] { _blenderDied(); return 0U; }; const auto* cBuf = static_cast(buf); - size_t writeLen = 0; + std::size_t writeLen = 0; do { const int ret = Write(m_writepipe[1], cBuf, len); @@ -925,9 +935,9 @@ Mesh Mesh::getContiguousSkinningVersion() const { newMesh.norm.clear(); newMesh.contiguousSkinVertCounts.clear(); newMesh.contiguousSkinVertCounts.reserve(skins.size()); - for (size_t i = 0; i < skins.size(); ++i) { + for (std::size_t i = 0; i < skins.size(); ++i) { std::unordered_map, uint32_t> contigMap; - size_t vertCount = 0; + std::size_t vertCount = 0; for (Surface& surf : newMesh.surfaces) { for (Surface::Vert& vert : surf.verts) { if (vert.iPos == 0xffffffff) @@ -1057,7 +1067,7 @@ uint32_t Mesh::SkinBanks::addSurface(const Mesh& mesh, const Surface& surf, int continue; if (!VertInBank(bank.m_skinIdxs, v.iSkin) && !VertInBank(toAdd, v.iSkin)) { toAdd.push_back(v.iSkin); - if (skinSlotCount > 0 && bank.m_skinIdxs.size() + toAdd.size() > size_t(skinSlotCount)) { + if (skinSlotCount > 0 && bank.m_skinIdxs.size() + toAdd.size() > std::size_t(skinSlotCount)) { toAdd.clear(); done = false; break; @@ -1192,7 +1202,7 @@ const Bone* Armature::getParent(const Bone* bone) const { return &bones[bone->parent]; } -const Bone* Armature::getChild(const Bone* bone, size_t child) const { +const Bone* Armature::getChild(const Bone* bone, std::size_t child) const { if (child >= bone->children.size()) return nullptr; int32_t cIdx = bone->children[child]; @@ -1692,7 +1702,7 @@ void Token::shutdown() { Token::~Token() { shutdown(); } -HMDLBuffers::HMDLBuffers(HMDLMeta&& meta, size_t vboSz, const std::vector& iboData, +HMDLBuffers::HMDLBuffers(HMDLMeta&& meta, std::size_t vboSz, const std::vector& iboData, std::vector&& surfaces, const Mesh::SkinBanks& skinBanks) : m_meta(std::move(meta)) , m_vboSz(vboSz) diff --git a/hecl/lib/ProjectPath.cpp b/hecl/lib/ProjectPath.cpp index d9bcd11a4..dfaf2f9de 100644 --- a/hecl/lib/ProjectPath.cpp +++ b/hecl/lib/ProjectPath.cpp @@ -269,15 +269,14 @@ static bool RegexSearchLast(const T& str, std::match_results