diff --git a/CMakeLists.txt b/CMakeLists.txt index 2a2590705..4c459af34 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -48,14 +48,14 @@ endif() # Shared code list(APPEND ${PROJECT_NAME}_SOURCES - "${CMAKE_CURRENT_SOURCE_DIR}/include/sourcepp/detail/BufferUtils.h" - "${CMAKE_CURRENT_SOURCE_DIR}/include/sourcepp/detail/StringUtils.h" + "${CMAKE_CURRENT_SOURCE_DIR}/include/sourcepp/buffer/Buffer.h" "${CMAKE_CURRENT_SOURCE_DIR}/include/sourcepp/math/Angles.h" "${CMAKE_CURRENT_SOURCE_DIR}/include/sourcepp/math/Integer.h" "${CMAKE_CURRENT_SOURCE_DIR}/include/sourcepp/math/Matrix.h" "${CMAKE_CURRENT_SOURCE_DIR}/include/sourcepp/math/Vector.h" - "${CMAKE_CURRENT_SOURCE_DIR}/src/sourcepp/detail/BufferUtils.cpp" - "${CMAKE_CURRENT_SOURCE_DIR}/src/sourcepp/detail/StringUtils.cpp") + "${CMAKE_CURRENT_SOURCE_DIR}/include/sourcepp/string/String.h" + "${CMAKE_CURRENT_SOURCE_DIR}/src/sourcepp/buffer/Buffer.cpp" + "${CMAKE_CURRENT_SOURCE_DIR}/src/sourcepp/string/String.cpp") add_library(${PROJECT_NAME} STATIC ${${PROJECT_NAME}_SOURCES}) target_link_libraries(${PROJECT_NAME} PUBLIC bufferstream) target_include_directories(${PROJECT_NAME} PUBLIC "${CMAKE_CURRENT_SOURCE_DIR}/include") diff --git a/include/dmxpp/structs/Value.h b/include/dmxpp/structs/Value.h index 2c1472507..be300bf95 100644 --- a/include/dmxpp/structs/Value.h +++ b/include/dmxpp/structs/Value.h @@ -39,17 +39,17 @@ struct Color { uint8_t a; }; -using Vector2 = sourcepp::Vec2f; +using Vector2 = sourcepp::math::Vec2f; -using Vector3 = sourcepp::Vec3f; +using Vector3 = sourcepp::math::Vec3f; -using Vector4 = sourcepp::Vec4f; +using Vector4 = sourcepp::math::Vec4f; -using EulerAngles = sourcepp::EulerAngles; +using EulerAngles = sourcepp::math::EulerAngles; -using Quaternion = sourcepp::Quat; +using Quaternion = sourcepp::math::Quat; -using Matrix4x4 = sourcepp::Matrix<4,4>; +using Matrix4x4 = sourcepp::math::Matrix<4,4>; using Generic = std::variant< Invalid, diff --git a/include/sourcepp/detail/BufferUtils.h b/include/sourcepp/buffer/Buffer.h similarity index 80% rename from include/sourcepp/detail/BufferUtils.h rename to include/sourcepp/buffer/Buffer.h index 40b9ff43a..7edbca79a 100644 --- a/include/sourcepp/detail/BufferUtils.h +++ b/include/sourcepp/buffer/Buffer.h @@ -6,8 +6,8 @@ class BufferStream; -namespace sourcepp::detail { +namespace sourcepp::buffer { void readStringAtOffset(BufferStream& stream, std::string& str, std::ios::seekdir offsetFrom = std::ios::cur, std::size_t subtractFromOffset = sizeof(int)); -} // namespace sourcepp::detail +} // namespace sourcepp::buffer diff --git a/include/sourcepp/math/Angles.h b/include/sourcepp/math/Angles.h index 06636ba49..104a42644 100644 --- a/include/sourcepp/math/Angles.h +++ b/include/sourcepp/math/Angles.h @@ -4,7 +4,7 @@ #include "Vector.h" -namespace sourcepp { +namespace sourcepp::math { using EulerAngles = Vec3f; @@ -60,4 +60,4 @@ struct QuatCompressed64 { } }; -} // namespace sourcepp +} // namespace sourcepp::math diff --git a/include/sourcepp/math/Matrix.h b/include/sourcepp/math/Matrix.h index 9c413a87c..783a986fd 100644 --- a/include/sourcepp/math/Matrix.h +++ b/include/sourcepp/math/Matrix.h @@ -4,7 +4,7 @@ #include "Integer.h" -namespace sourcepp { +namespace sourcepp::math { template class Matrix { @@ -20,4 +20,4 @@ class Matrix { P data[M][N]; }; -} // namespace sourcepp +} // namespace sourcepp::math diff --git a/include/sourcepp/math/Vector.h b/include/sourcepp/math/Vector.h index 0326273f6..3daba3060 100644 --- a/include/sourcepp/math/Vector.h +++ b/include/sourcepp/math/Vector.h @@ -2,7 +2,7 @@ #include -namespace sourcepp { +namespace sourcepp::math { template struct Vec2 { @@ -31,4 +31,4 @@ struct Vec4 { using Vec4f = Vec4; using Vec4d = Vec4; -} // namespace sourcepp +} // namespace sourcepp::math diff --git a/include/sourcepp/detail/StringUtils.h b/include/sourcepp/string/String.h similarity index 87% rename from include/sourcepp/detail/StringUtils.h rename to include/sourcepp/string/String.h index c4bcc3d9c..921c7f7a5 100644 --- a/include/sourcepp/detail/StringUtils.h +++ b/include/sourcepp/string/String.h @@ -4,7 +4,7 @@ #include #include -namespace sourcepp::detail { +namespace sourcepp::string { bool contains(std::string_view s, char c); @@ -22,4 +22,4 @@ void trim(std::string& s, std::string_view c); std::vector split(std::string_view s, char delim); -} // namespace sourcepp::detail +} // namespace sourcepp::string diff --git a/include/studiomodelpp/structs/Generic.h b/include/studiomodelpp/structs/Generic.h index aa62a2f3f..13e618adc 100644 --- a/include/studiomodelpp/structs/Generic.h +++ b/include/studiomodelpp/structs/Generic.h @@ -13,8 +13,8 @@ constexpr int MAX_BONES_PER_VERTEX = 3; struct BBox { int32_t bone; int32_t group; - sourcepp::Vec3f bboxMin; - sourcepp::Vec3f bboxMax; + sourcepp::math::Vec3f bboxMin; + sourcepp::math::Vec3f bboxMax; //int32_t hitboxNameOffset; std::string name; @@ -33,8 +33,8 @@ struct Movement { float velocityStart; float velocityEnd; float yawEnd; - sourcepp::Vec3f movement; - sourcepp::Vec3f relativePosition; + sourcepp::math::Vec3f movement; + sourcepp::math::Vec3f relativePosition; }; } // namespace studiomodelpp diff --git a/include/studiomodelpp/structs/MDL.h b/include/studiomodelpp/structs/MDL.h index ac1a57326..27905722b 100644 --- a/include/studiomodelpp/structs/MDL.h +++ b/include/studiomodelpp/structs/MDL.h @@ -25,13 +25,13 @@ struct Bone { int32_t parent; int32_t boneController[6]; - sourcepp::Vec3f position; - sourcepp::Quat rotationQuat; - sourcepp::Vec3f rotationEuler; - sourcepp::Vec3f positionScale; - sourcepp::Vec3f rotationScale; - sourcepp::Matrix<3,4> poseToBose; - sourcepp::Quat alignment; + sourcepp::math::Vec3f position; + sourcepp::math::Quat rotationQuat; + sourcepp::math::Vec3f rotationEuler; + sourcepp::math::Vec3f positionScale; + sourcepp::math::Vec3f rotationScale; + sourcepp::math::Matrix<3,4> poseToBose; + sourcepp::math::Quat alignment; Flags flags; int32_t procType; int32_t procIndex; @@ -215,7 +215,7 @@ struct Mesh { int32_t meshID; - sourcepp::Vec3f center; + sourcepp::math::Vec3f center; //int32_t modelVertexData; //int32_t numLODVertexes[MAX_LOD_COUNT]; @@ -290,12 +290,12 @@ struct MDL { std::string name; //int32_t dataLength; - sourcepp::Vec3f eyePosition; - sourcepp::Vec3f illuminationPosition; - sourcepp::Vec3f hullMin; - sourcepp::Vec3f hullMax; - sourcepp::Vec3f viewBBoxMin; - sourcepp::Vec3f viewBBoxMax; + sourcepp::math::Vec3f eyePosition; + sourcepp::math::Vec3f illuminationPosition; + sourcepp::math::Vec3f hullMin; + sourcepp::math::Vec3f hullMax; + sourcepp::math::Vec3f viewBBoxMin; + sourcepp::math::Vec3f viewBBoxMax; Flags flags; diff --git a/include/studiomodelpp/structs/VVD.h b/include/studiomodelpp/structs/VVD.h index e1aecf54e..c3256ccfe 100644 --- a/include/studiomodelpp/structs/VVD.h +++ b/include/studiomodelpp/structs/VVD.h @@ -27,11 +27,11 @@ struct BoneWeight { struct Vertex { BoneWeight boneWeight; - sourcepp::Vec3f position{}; - sourcepp::Vec3f normal{}; - sourcepp::Vec2f uv{}; + sourcepp::math::Vec3f position{}; + sourcepp::math::Vec3f normal{}; + sourcepp::math::Vec2f uv{}; - sourcepp::Vec4f tangent{}; // Taken from tangents data section + sourcepp::math::Vec4f tangent{}; // Taken from tangents data section }; struct VVD { diff --git a/include/vtfpp/vtfpp.h b/include/vtfpp/vtfpp.h index e05589c30..ecd8ddcee 100644 --- a/include/vtfpp/vtfpp.h +++ b/include/vtfpp/vtfpp.h @@ -146,7 +146,7 @@ class VTF { [[nodiscard]] uint16_t getStartFrame() const; - [[nodiscard]] sourcepp::Vec3f getReflectivity() const; + [[nodiscard]] sourcepp::math::Vec3f getReflectivity() const; [[nodiscard]] float getBumpMapScale() const; @@ -195,7 +195,7 @@ class VTF { uint16_t startFrame{}; //uint8_t _padding0[4]; - sourcepp::Vec3f reflectivity{}; + sourcepp::math::Vec3f reflectivity{}; //uint8_t _padding1[4]; float bumpMapScale{}; diff --git a/src/sourcepp/detail/BufferUtils.cpp b/src/sourcepp/buffer/Buffer.cpp similarity index 57% rename from src/sourcepp/detail/BufferUtils.cpp rename to src/sourcepp/buffer/Buffer.cpp index f144eee53..0f45131fe 100644 --- a/src/sourcepp/detail/BufferUtils.cpp +++ b/src/sourcepp/buffer/Buffer.cpp @@ -1,10 +1,12 @@ -#include +#include #include #include -void sourcepp::detail::readStringAtOffset(BufferStream& stream, std::string& str, std::ios::seekdir offsetFrom, std::size_t subtractFromOffset) { +using namespace sourcepp; + +void buffer::readStringAtOffset(BufferStream& stream, std::string& str, std::ios::seekdir offsetFrom, std::size_t subtractFromOffset) { int offset = stream.read(); if (offset == 0) { str = ""; diff --git a/src/sourcepp/detail/StringUtils.cpp b/src/sourcepp/string/String.cpp similarity index 62% rename from src/sourcepp/detail/StringUtils.cpp rename to src/sourcepp/string/String.cpp index fe72d2497..38dd9f478 100644 --- a/src/sourcepp/detail/StringUtils.cpp +++ b/src/sourcepp/string/String.cpp @@ -1,51 +1,50 @@ -#include +#include #include #include -#include #include using namespace sourcepp; -bool detail::contains(std::string_view s, char c) { - return std::ranges::find(s, c) != std::ranges::end(s); +bool string::contains(std::string_view s, char c) { + return std::find(s.begin(), s.end(), c) != s.end(); } // https://stackoverflow.com/a/217605 -void detail::ltrim(std::string& s) { +void string::ltrim(std::string& s) { s.erase(s.begin(), std::find_if(s.begin(), s.end(), [](char c) { return !std::isspace(c); })); } -void detail::rtrim(std::string& s) { +void string::rtrim(std::string& s) { s.erase(std::find_if(s.rbegin(), s.rend(), [](char c) { return !std::isspace(c); }).base(), s.end()); } -void detail::trim(std::string& s) { +void string::trim(std::string& s) { rtrim(s); ltrim(s); } -void detail::ltrim(std::string& s, std::string_view chars) { +void string::ltrim(std::string& s, std::string_view chars) { s.erase(s.begin(), std::find_if(s.begin(), s.end(), [chars](char c) { return !contains(chars, c); })); } -void detail::rtrim(std::string& s, std::string_view chars) { +void string::rtrim(std::string& s, std::string_view chars) { s.erase(std::find_if(s.rbegin(), s.rend(), [chars](char c) { return !contains(chars, c); }).base(), s.end()); } -void detail::trim(std::string& s, std::string_view c) { +void string::trim(std::string& s, std::string_view c) { rtrim(s, c); ltrim(s, c); } // https://stackoverflow.com/a/46931770 -std::vector detail::split(std::string_view s, char delim) { +std::vector string::split(std::string_view s, char delim) { std::vector result; std::stringstream ss(s.data()); std::string item; diff --git a/src/studiomodelpp/structs/MDL.cpp b/src/studiomodelpp/structs/MDL.cpp index 2d820e5d2..79cc9c8d3 100644 --- a/src/studiomodelpp/structs/MDL.cpp +++ b/src/studiomodelpp/structs/MDL.cpp @@ -1,10 +1,9 @@ #include #include -#include +#include using namespace sourcepp; -using namespace sourcepp::detail; using namespace studiomodelpp::MDL; constexpr int32_t MDL_ID = 'I' + ('D' << 8) + ('S' << 16) + ('T' << 24); @@ -73,7 +72,7 @@ bool MDL::open(const std::byte* data, std::size_t size) { for (int i = 0; i < boneCount; i++) { auto& bone = this->bones.emplace_back(); - readStringAtOffset(stream, bone.name); + buffer::readStringAtOffset(stream, bone.name); stream .read(bone.parent) .read(bone.boneController) @@ -88,7 +87,7 @@ bool MDL::open(const std::byte* data, std::size_t size) { .read(bone.procType) .read(bone.procIndex) .read(bone.physicsBone); - readStringAtOffset(stream, bone.surfacePropName, std::ios::cur, sizeof(int32_t) * 12 + sizeof(Vec3f) * 4 + sizeof(Quat) * 2 + sizeof(Matrix<3,4>) + sizeof(Bone::Flags)); + buffer::readStringAtOffset(stream, bone.surfacePropName, std::ios::cur, sizeof(int32_t) * 12 + sizeof(math::Vec3f) * 4 + sizeof(math::Quat) * 2 + sizeof(math::Matrix<3,4>) + sizeof(Bone::Flags)); stream.read(bone.contents); // _unused0 @@ -109,12 +108,12 @@ bool MDL::open(const std::byte* data, std::size_t size) { auto& hitboxSet = this->hitboxSets.emplace_back(); - readStringAtOffset(stream, hitboxSet.name); + buffer::readStringAtOffset(stream, hitboxSet.name); auto hitboxCount = stream.read(); auto hitboxOffset = stream.read(); for (int j = 0; j < hitboxCount; j++) { - auto hitboxPos = hitboxOffset + j * (sizeof(int32_t) * 11 + sizeof(Vec3f) * 2); + auto hitboxPos = hitboxOffset + j * (sizeof(int32_t) * 11 + sizeof(math::Vec3f) * 2); stream.seek(hitboxSetPos + hitboxPos); auto& hitbox = hitboxSet.hitboxes.emplace_back(); @@ -152,9 +151,7 @@ bool MDL::open(const std::byte* data, std::size_t size) { for (int i = 0; i < materialCount; i++) { auto& material = this->materials.emplace_back(); - // Needs to be read from the base of the data structure - readStringAtOffset(stream, material.name); - + buffer::readStringAtOffset(stream, material.name); stream.read(material.flags); // used @@ -166,7 +163,8 @@ bool MDL::open(const std::byte* data, std::size_t size) { stream.seek(materialDirOffset); for (int i = 0; i < materialDirCount; i++) { auto& materialDir = this->materialDirectories.emplace_back(); - readStringAtOffset(stream, materialDir, std::ios::beg, 0); + + buffer::readStringAtOffset(stream, materialDir, std::ios::beg, 0); } stream.seek(skinReferenceOffset); @@ -184,7 +182,7 @@ bool MDL::open(const std::byte* data, std::size_t size) { auto& bodyPart = this->bodyParts.emplace_back(); - readStringAtOffset(stream, bodyPart.name); + buffer::readStringAtOffset(stream, bodyPart.name); auto modelsCount = stream.read(); // base @@ -210,7 +208,7 @@ bool MDL::open(const std::byte* data, std::size_t size) { .read(model.verticesOffset); for (int k = 0; k < meshesCount; k++) { - auto meshPos = meshesOffset + k * (sizeof(int32_t) * (18 + MAX_LOD_COUNT) + sizeof(Vec3f)); + auto meshPos = meshesOffset + k * (sizeof(int32_t) * (18 + MAX_LOD_COUNT) + sizeof(math::Vec3f)); stream.seek(bodyPartPos + modelPos + meshPos); auto& mesh = model.meshes.emplace_back(); diff --git a/src/studiomodelpp/structs/VVD.cpp b/src/studiomodelpp/structs/VVD.cpp index 87c281132..490cc5c10 100644 --- a/src/studiomodelpp/structs/VVD.cpp +++ b/src/studiomodelpp/structs/VVD.cpp @@ -53,7 +53,7 @@ bool VVD::open(const std::byte* data, std::size_t size, const MDL::MDL& mdl) { stream.seek(tangentsOffset); for (std::size_t i = 0; i < this->numVerticesInLOD[0]; i++) { - this->vertices.at(i).tangent = stream.read(); + this->vertices.at(i).tangent = stream.read(); } stream.seek(fixupsOffset); diff --git a/src/vmfpp/vmfpp.cpp b/src/vmfpp/vmfpp.cpp index 47d00caec..83f833d7c 100644 --- a/src/vmfpp/vmfpp.cpp +++ b/src/vmfpp/vmfpp.cpp @@ -4,9 +4,9 @@ #include #include -#include +#include -using namespace sourcepp::detail; +using namespace sourcepp; using namespace vmfpp; constexpr char CHAR_ENTER_SCOPE = '{'; @@ -24,7 +24,7 @@ bool readNode(std::istream& stream, Node* scope, std::string_view key = NODE_ROO continue; } // New scope - else if (contains(line, CHAR_ENTER_SCOPE)) { + else if (string::contains(line, CHAR_ENTER_SCOPE)) { // Scope is opened after the key is read // There may be situations where there is no key, so we must account for that if ((key.empty() && (scope->getChildren().size() + scope->getValues().size()) > 0) || key == NODE_ROOT_KEY) { @@ -36,18 +36,18 @@ bool readNode(std::istream& stream, Node* scope, std::string_view key = NODE_ROO } } // Exit scope - else if (contains(line, CHAR_EXIT_SCOPE)) { + else if (string::contains(line, CHAR_EXIT_SCOPE)) { break; } // Value - else if (contains(line, CHAR_QUOTE)) { - auto p = split(line, CHAR_QUOTE); + else if (string::contains(line, CHAR_QUOTE)) { + auto p = string::split(line, CHAR_QUOTE); if (std::count(line.begin(), line.end(), CHAR_QUOTE) == 3) { // Multi-line property value, because value quotes aren't closed // Read lines until we encounter a closing quote while (std::getline(stream, line)) { - if (contains(line, CHAR_QUOTE)) { - p[3] += '\n' + split(line, CHAR_QUOTE)[0]; + if (string::contains(line, CHAR_QUOTE)) { + p[3] += '\n' + string::split(line, CHAR_QUOTE)[0]; break; } p[3] += '\n' + line; @@ -59,8 +59,8 @@ bool readNode(std::istream& stream, Node* scope, std::string_view key = NODE_ROO else { Node newScope; std::string newKey = line; - trim(newKey, CHAR_DISCARD_CUTSET); - trim(newKey); + string::trim(newKey, CHAR_DISCARD_CUTSET); + string::trim(newKey); if (!readNode(stream, &newScope, newKey)) { return false; } diff --git a/src/vtfpp/vtfpp.cpp b/src/vtfpp/vtfpp.cpp index dc3f217a7..01977a716 100644 --- a/src/vtfpp/vtfpp.cpp +++ b/src/vtfpp/vtfpp.cpp @@ -206,7 +206,7 @@ uint16_t VTF::getStartFrame() const { return this->startFrame; } -Vec3f VTF::getReflectivity() const { +math::Vec3f VTF::getReflectivity() const { return this->reflectivity; }