Skip to content

Commit

Permalink
chore(all): restructure sourcepp base lib
Browse files Browse the repository at this point in the history
  • Loading branch information
craftablescience committed Jun 6, 2024
1 parent fd88524 commit 12a4de4
Show file tree
Hide file tree
Showing 17 changed files with 80 additions and 81 deletions.
8 changes: 4 additions & 4 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -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")
Expand Down
12 changes: 6 additions & 6 deletions include/dmxpp/structs/Value.h
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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
4 changes: 2 additions & 2 deletions include/sourcepp/math/Angles.h
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@

#include "Vector.h"

namespace sourcepp {
namespace sourcepp::math {

using EulerAngles = Vec3f;

Expand Down Expand Up @@ -60,4 +60,4 @@ struct QuatCompressed64 {
}
};

} // namespace sourcepp
} // namespace sourcepp::math
4 changes: 2 additions & 2 deletions include/sourcepp/math/Matrix.h
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@

#include "Integer.h"

namespace sourcepp {
namespace sourcepp::math {

template<uint8_t M, uint8_t N, std::floating_point P = float>
class Matrix {
Expand All @@ -20,4 +20,4 @@ class Matrix {
P data[M][N];
};

} // namespace sourcepp
} // namespace sourcepp::math
4 changes: 2 additions & 2 deletions include/sourcepp/math/Vector.h
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

#include <concepts>

namespace sourcepp {
namespace sourcepp::math {

template<std::floating_point P>
struct Vec2 {
Expand Down Expand Up @@ -31,4 +31,4 @@ struct Vec4 {
using Vec4f = Vec4<float>;
using Vec4d = Vec4<double>;

} // namespace sourcepp
} // namespace sourcepp::math
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
#include <string_view>
#include <vector>

namespace sourcepp::detail {
namespace sourcepp::string {

bool contains(std::string_view s, char c);

Expand All @@ -22,4 +22,4 @@ void trim(std::string& s, std::string_view c);

std::vector<std::string> split(std::string_view s, char delim);

} // namespace sourcepp::detail
} // namespace sourcepp::string
8 changes: 4 additions & 4 deletions include/studiomodelpp/structs/Generic.h
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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
28 changes: 14 additions & 14 deletions include/studiomodelpp/structs/MDL.h
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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];
Expand Down Expand Up @@ -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;

Expand Down
8 changes: 4 additions & 4 deletions include/studiomodelpp/structs/VVD.h
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down
4 changes: 2 additions & 2 deletions include/vtfpp/vtfpp.h
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand Down Expand Up @@ -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{};
Expand Down
Original file line number Diff line number Diff line change
@@ -1,10 +1,12 @@
#include <sourcepp/detail/BufferUtils.h>
#include <sourcepp/buffer/Buffer.h>

#include <BufferStream.h>

#include <sourcepp/math/Integer.h>

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<int32_t>();
if (offset == 0) {
str = "";
Expand Down
Original file line number Diff line number Diff line change
@@ -1,51 +1,50 @@
#include <sourcepp/detail/StringUtils.h>
#include <sourcepp/string/String.h>

#include <algorithm>
#include <cctype>
#include <ranges>
#include <sstream>

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<std::string> detail::split(std::string_view s, char delim) {
std::vector<std::string> string::split(std::string_view s, char delim) {
std::vector<std::string> result;
std::stringstream ss(s.data());
std::string item;
Expand Down
22 changes: 10 additions & 12 deletions src/studiomodelpp/structs/MDL.cpp
Original file line number Diff line number Diff line change
@@ -1,10 +1,9 @@
#include <studiomodelpp/structs/MDL.h>

#include <BufferStream.h>
#include <sourcepp/detail/BufferUtils.h>
#include <sourcepp/buffer/Buffer.h>

using namespace sourcepp;
using namespace sourcepp::detail;
using namespace studiomodelpp::MDL;

constexpr int32_t MDL_ID = 'I' + ('D' << 8) + ('S' << 16) + ('T' << 24);
Expand Down Expand Up @@ -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)
Expand All @@ -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
Expand All @@ -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<int32_t>();
auto hitboxOffset = stream.read<int32_t>();

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();
Expand Down Expand Up @@ -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
Expand All @@ -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);
Expand All @@ -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<int32_t>();
// base
Expand All @@ -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();
Expand Down
2 changes: 1 addition & 1 deletion src/studiomodelpp/structs/VVD.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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<Vec4f>();
this->vertices.at(i).tangent = stream.read<math::Vec4f>();
}

stream.seek(fixupsOffset);
Expand Down
Loading

0 comments on commit 12a4de4

Please sign in to comment.