Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
33 changes: 27 additions & 6 deletions include/BasicTypes.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -157,7 +157,7 @@ class NiVersion {
// Check if file has a Fallout 76 version range
bool IsFO76() const { return file == V20_2_0_7 && stream == 155; }
// Check if file has a Starfield version range
bool IsSF() const { return file == V20_2_0_7 && stream >= 172 && stream <= 173; }
bool IsSF() const { return file == V20_2_0_7 && stream >= 172 && stream <= 175; }

// Return an Oblivion file version
static NiVersion getOB() { return NiVersion(NiFileVersion::V20_0_0_5, 11, 11); }
Expand Down Expand Up @@ -358,12 +358,13 @@ class NiStreamReversible {
}

void SyncUDEC3(Vector3& vec) {
uint32_t data;
uint32_t data = 0;

if (mode == Mode::Writing) {
data = (((uint32_t)((vec.z+1.0)*511.5)) & 1023) << 20;
data &= (((uint32_t)((vec.y+1.0)*511.5)) & 1023) << 10;
data &= (((uint32_t)((vec.x+1.0)*511.5)) & 1023);
data = (static_cast<uint32_t>(std::round((vec.x + 1.0) * 511.5)) & 1023);
data |= (static_cast<uint32_t>(std::round((vec.y + 1.0) * 511.5)) & 1023) << 10;
data |= (static_cast<uint32_t>(std::round((vec.z + 1.0) * 511.5)) & 1023) << 20;
data |= static_cast<uint32_t>(1) << 30;
}

Sync(data);
Expand All @@ -372,7 +373,27 @@ class NiStreamReversible {
vec.x = (float)(((data & 1023) / 511.5) - 1.0);
vec.y = (float)((((data >> 10) & 1023) / 511.5) - 1.0);
vec.z = (float)((((data >> 20) & 1023) / 511.5) - 1.0);
}
}
}

void SyncUDEC3(Vector3& vec, uint8_t& w) {
uint32_t data = 0;

if (mode == Mode::Writing) {
data = (static_cast<uint32_t>(std::round((vec.x + 1.0) * 511.5)) & 1023);
data |= (static_cast<uint32_t>(std::round((vec.y + 1.0) * 511.5)) & 1023) << 10;
data |= (static_cast<uint32_t>(std::round((vec.z + 1.0) * 511.5)) & 1023) << 20;
data |= static_cast<uint32_t>(w & 3) << 30;
}

Sync(data);

if (mode == Mode::Reading) {
vec.x = (float)(((data & 1023) / 511.5) - 1.0);
vec.y = (float)((((data >> 10) & 1023) / 511.5) - 1.0);
vec.z = (float)((((data >> 20) & 1023) / 511.5) - 1.0);
w = static_cast<uint8_t>((data >> 30) & 3);
}
}


Expand Down
21 changes: 17 additions & 4 deletions include/Geometry.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -591,6 +591,7 @@ class BSGeometryMeshData : public NiCloneableStreamable<BSGeometryMeshData, NiGe

uint32_t nTangents = 0;
// tangents from NiGeometryData (UDEC3 packed in file)
std::vector<uint8_t> tangentWs; // 2-bit W component of each tangent (bitangent sign)

uint32_t nTotalWeights = 0;
std::vector<std::vector<BoneWeight>> skinWeights;
Expand All @@ -612,10 +613,12 @@ struct BSGeometryMesh {
uint32_t numVerts = 0;
uint32_t flags = 0; // Often 64

// in official files, this is 41 characters: hex characters from sha1 of the mesh data split into 2 parts
// with a path separator. The game does not seem to check the digest, so the same name can be used for
// replacement, or probably a human-readable one
NiString meshName;
// When internalGeom is false (default), meshName holds the external .mesh path
// (41 hex chars from sha1, or a human-readable name). When true, mesh data is
// serialized inline in the NIF and meshName is unused.
bool internalGeom = false;

NiString meshName;

BSGeometryMeshData meshData;
void Sync(NiStreamReversible& stream);
Expand Down Expand Up @@ -651,6 +654,16 @@ class BSGeometry : public NiCloneableStreamable<BSGeometry, NiShape> {

uint8_t MeshCount() { return (uint8_t) meshes.size(); }

// Flag 0x200 (512) on BSGeometry controls whether mesh data is embedded inline
// in the NIF (internal) or stored as separate .mesh files (external).
bool HasInternalGeomData() const { return (flags & 0x200) != 0; }
void SetInternalGeomData(bool internal) {
if (internal)
flags |= 0x200;
else
flags &= ~uint32_t(0x200);
}

// SelectMesh provides a way to choose which mesh from the BSGeometryMesh list data accesessors will use.
// If this is not called, functions to retrieve vertices, triangles, etc will default to the first mesh.
// Returns a pointer to the mesh data selected.
Expand Down
5 changes: 4 additions & 1 deletion include/NifUtil.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ void ApplyMapToTriangles(std::vector<Triangle>& tris,
const std::vector<IndexType1>& map,
std::vector<IndexType2>* deletedTris = nullptr) {
const size_t mapsz = map.size();
int di = 0;
size_t di = 0;
for (IndexType2 si = 0; si < static_cast<IndexType2>(tris.size()); ++si) {
const Triangle& stri = tris[si];
// Triangle's indices are unsigned, but IndexType might be signed.
Expand Down Expand Up @@ -195,6 +195,9 @@ inline bool is_relative_path(std::string_view path) noexcept {
// Helper to trim whitespace characters including newlines from the start and end of a string
void trim_whitespace(std::string& str);

std::unique_ptr<std::istream> GetBinaryInputFileStream(const std::filesystem::path& path);
std::unique_ptr<std::ostream> GetBinaryOutputFileStream(const std::filesystem::path& path);

// Convenience wrapper for std::find
template<typename Container, typename Value = typename Container::value>
auto find(Container& cont, Value&& val) {
Expand Down
51 changes: 40 additions & 11 deletions src/Geometry.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ See the included GPLv3 LICENSE file
#include "NifUtil.hpp"

#include <array>
#include <cmath>

using namespace nifly;

Expand Down Expand Up @@ -1599,6 +1600,24 @@ void BSGeometryMeshData::Sync(NiStreamReversible& stream) {
SetTangents(true);
SetVertexColors(true);

// When writing, update counts from actual data sizes
if (stream.GetMode() == NiStreamReversible::Mode::Writing) {
nTriIndices = static_cast<uint32_t>(tris.size()) * 3;
nVertices = static_cast<uint32_t>(vertices.size());
numVertices = static_cast<uint16_t>(std::min(nVertices, static_cast<uint32_t>(0xFFFF)));
nUV1 = uvSets.size() > 0 ? static_cast<uint32_t>(uvSets[0].size()) : 0;
nUV2 = uvSets.size() > 1 ? static_cast<uint32_t>(uvSets[1].size()) : 0;
Comment on lines +1603 to +1609
Copy link

Copilot AI Apr 11, 2026

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

In BSGeometryMeshData::Sync, the new writing-mode count refresh clamps numVertices to 0xFFFF, but later the function unconditionally does numVertices = (uint16_t)nVertices after syncing nVertices, which overrides the clamp and can wrap if nVertices > 65535. Consider only assigning numVertices from nVertices in reading mode, or keep numVertices consistent with the same clamp when writing.

Copilot uses AI. Check for mistakes.
nColors = static_cast<uint32_t>(vColors.size());
nNormals = static_cast<uint32_t>(normals.size());
nTangents = static_cast<uint32_t>(tangents.size());
nTotalWeights = 0;
for (auto& vw : skinWeights)
nTotalWeights += static_cast<uint32_t>(vw.size());
nLODS = static_cast<uint32_t>(lods.size());
nMeshlets = static_cast<uint32_t>(meshletList.size());
nCullData = static_cast<uint32_t>(cullDataList.size());
}

stream.Sync(version);
if (version > 2)
return;
Expand All @@ -1615,9 +1634,10 @@ void BSGeometryMeshData::Sync(NiStreamReversible& stream) {
stream.Sync(nWeightsPerVert);

stream.Sync(nVertices);
// maybe not a good idea to do the below, in case some meshes have over 65k verts, however since
// triangles still use 16 bit indices, the total count must still fit under that limit ...
numVertices = (uint16_t) nVertices;
if (stream.GetMode() == NiStreamReversible::Mode::Reading)
numVertices = static_cast<uint16_t>(nVertices);
else
numVertices = static_cast<uint16_t>(std::min(nVertices, static_cast<uint32_t>(0xFFFF)));
vertices.resize(nVertices);
for (uint32_t v = 0; v < nVertices; v++) {
if (stream.GetMode() == NiStreamReversible::Mode::Reading) {
Expand All @@ -1636,13 +1656,11 @@ void BSGeometryMeshData::Sync(NiStreamReversible& stream) {
}
else {
auto pack = [&](float component, float posScale) {
uint16_t factor;
int16_t val;
if (component < 0)
factor = 32768;
val = static_cast<int16_t>(std::round((component / (scale * posScale)) * 32768.0f));
else
factor = 32767;

uint16_t val = (uint16_t) ((component / (scale * posScale)) * factor);
val = static_cast<int16_t>(std::round((component / (scale * posScale)) * 32767.0f));
stream.Sync(val);
};

Expand Down Expand Up @@ -1683,9 +1701,9 @@ void BSGeometryMeshData::Sync(NiStreamReversible& stream) {

stream.Sync(nTangents);
tangents.resize(nTangents);
tangentWs.resize(nTangents, 1);
for (uint32_t t = 0; t < nTangents; t++) {
stream.SyncUDEC3(tangents[t]);
// need to calculate tangent basis and bitangents on read?
stream.SyncUDEC3(tangents[t], tangentWs[t]);
}

/*
Expand Down Expand Up @@ -1737,7 +1755,15 @@ void BSGeometryMesh::Sync(NiStreamReversible& stream) {
stream.Sync(triSize);
stream.Sync(numVerts);
stream.Sync(flags);
meshName.Sync(stream, 4);

if (internalGeom) {
// Mesh data is embedded inline in the NIF (flag 0x200 on BSGeometry)
meshData.Sync(stream);
}
else {
// External .mesh file path reference
meshName.Sync(stream, 4);
}
}

void BSGeometry::Sync(NiStreamReversible& stream) {
Expand All @@ -1753,6 +1779,8 @@ void BSGeometry::Sync(NiStreamReversible& stream) {
if (stream.GetMode() == NiStreamReversible::Mode::Reading)
meshes.clear();

bool internal = HasInternalGeomData();

size_t meshCount = meshes.size();
for (uint32_t i = 0; i < 4; i++) {
uint8_t testByte = i < meshCount;
Expand All @@ -1762,6 +1790,7 @@ void BSGeometry::Sync(NiStreamReversible& stream) {
BSGeometryMesh mesh{};
meshes.push_back(mesh);
}
meshes[i].internalGeom = internal;
meshes[i].Sync(stream);
}
}
Expand Down
18 changes: 15 additions & 3 deletions src/NifFile.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -896,7 +896,7 @@ NiGeometryData* NifFile::GetGeometryData(NiShape* shape) const {
std::vector<std::reference_wrapper<std::string>> NifFile::GetExternalGeometryPathRefs(NiShape* shape) const {
std::vector<std::reference_wrapper<std::string>> meshPaths;
auto bsgeo = dynamic_cast<BSGeometry*>(shape);
if (bsgeo) {
if (bsgeo && !bsgeo->HasInternalGeomData()) {
for (uint8_t i = 0; i < bsgeo->MeshCount(); i++) {
auto mesh = bsgeo->SelectMesh(i);
meshPaths.push_back(mesh->meshName.get());
Expand All @@ -922,9 +922,9 @@ bool NifFile::SaveExternalShapeData(NiShape* shape, std::ostream& outfile, uint8
auto bsgeo = dynamic_cast<BSGeometry*>(shape);
if (bsgeo && (shapeIndex < bsgeo->MeshCount())) {
NiOStream meshStream(&outfile, nullptr);
NiStreamReversible s(nullptr, &meshStream,NiStreamReversible::Mode::Reading);
NiStreamReversible s(nullptr, &meshStream, NiStreamReversible::Mode::Writing);
auto mesh = bsgeo->SelectMesh(shapeIndex);
mesh->Sync(s);
mesh->meshData.Sync(s);
bsgeo->ReleaseMesh();
}
return true;
Expand Down Expand Up @@ -2078,6 +2078,18 @@ void NifFile::FinalizeData() {
}
}

auto bsgeo = dynamic_cast<BSGeometry*>(shape);
if (bsgeo) {
for (uint8_t i = 0; i < bsgeo->MeshCount(); i++) {
auto mesh = bsgeo->SelectMesh(i);
if (mesh && !mesh->meshData.vertices.empty()) {
mesh->triSize = static_cast<uint32_t>(mesh->meshData.tris.size()) * 3;
mesh->numVerts = static_cast<uint32_t>(mesh->meshData.vertices.size());
}
bsgeo->ReleaseMesh();
}
}

if (hdr.GetVersion().IsOB()) {
// Move tangents and bitangents from shape back to binary extra data
if (shape->HasTangents()) {
Expand Down
20 changes: 20 additions & 0 deletions src/NifUtil.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,8 @@ See the included GPLv3 LICENSE file

#include "NifUtil.hpp"

#include <fstream>

namespace nifly {

void trim_whitespace(std::string& str) {
Expand All @@ -33,4 +35,22 @@ void trim_whitespace(std::string& str) {
str = str.substr(i, j - i + 1);
}

std::unique_ptr<std::istream> GetBinaryInputFileStream(const std::filesystem::path& path) {
if (std::filesystem::exists(path)) {
auto fileStream = std::make_unique<std::ifstream>(path, std::ios::in | std::ios::binary);
if (fileStream && !fileStream->fail())
return fileStream;
}

return nullptr;
}

std::unique_ptr<std::ostream> GetBinaryOutputFileStream(const std::filesystem::path& path) {
auto fileStream = std::make_unique<std::ofstream>(path, std::ios::out | std::ios::binary);
if (fileStream && !fileStream->fail())
return fileStream;

return nullptr;
}

} // namespace nifly
Loading