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
1 change: 1 addition & 0 deletions NFS3/FEDATA/FedataFile.h
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
#pragma once

#include "../../Common/IRawData.h"
#include "../../Shared/VIV/VivArchive.h"

namespace LibOpenNFS::NFS3 {

Expand Down
29 changes: 28 additions & 1 deletion NFS3/NFS3Loader.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ namespace LibOpenNFS::NFS3 {

Car::PhysicsData carPhysicsData;

if (std::filesystem::exists(carOutPath)) {
if (std::filesystem::exists(fcePath.str()) && std::filesystem::exists(fedataPath.str()) && std::filesystem::exists(carpPath.str())) {
LogInfo("VIV has already been extracted to %s, skipping", carOutPath.c_str());
} else {
ASSERT(Shared::VivArchive::Load(vivPath.str(), vivFile), "Could not open VIV file: " << vivPath.str());
Expand Down Expand Up @@ -98,6 +98,33 @@ namespace LibOpenNFS::NFS3 {
return track;
}

FedataFile Loader::LoadCarMenuData(std::string const &carBasePath, std::string const &carOutPath) {
LogInfo("Loading NFS3 car menu data from %s into %s", carBasePath.c_str(), carOutPath.c_str());

std::filesystem::path p(carBasePath);
std::string carName = p.filename().string();
std::string const fedataFileName = "fedata.eng";
std::stringstream vivPath, fcePath, fedataPath, carpPath;
vivPath << carBasePath << "/car.viv";
fedataPath << carOutPath << "/" << fedataFileName;

Shared::VivArchive vivFile;
FedataFile fedataFile;

if (std::filesystem::exists(fedataPath.str())) {
LogInfo("Fedata file has already been extracted to %s, skipping", carOutPath.c_str());
} else {
ASSERT(Shared::VivArchive::Load(vivPath.str(), vivFile), "Could not open VIV file: " << vivPath.str());
ASSERT(Shared::VivArchive::ExtractFile(carOutPath, vivFile, fedataFileName),
"Could not extract fedata file from VIV file: " << vivPath.str() << "to: " << carOutPath);
}
if (!FedataFile::Load(fedataPath.str(), fedataFile)) {
LogWarning("Could not load FeData file: %s", fedataPath.str().c_str());
}

return fedataFile;
}

Car::MetaData Loader::_ParseAssetData(FceFile const &fceFile, FedataFile const &fedataFile) {
LogInfo("Parsing FCE File into ONFS Structures");

Expand Down
2 changes: 2 additions & 0 deletions NFS3/NFS3Loader.h
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,8 @@ namespace LibOpenNFS::NFS3 {
static Car LoadCar(std::string const &carBasePath, std::string const &carOutPath);
static Track LoadTrack(std::string const &trackBasePath);

static FedataFile LoadCarMenuData(std::string const &carBasePath, std::string const &carOutPath);

private:
static Car::MetaData _ParseAssetData(FceFile const &fceFile, FedataFile const &fedataFile);
static Car::PhysicsData _ParsePhysicsData(Shared::CarpFile const &carpFile);
Expand Down
19 changes: 9 additions & 10 deletions NFS4/PC/FEDATA/FedataFile.cpp
Original file line number Diff line number Diff line change
@@ -1,10 +1,12 @@
#include "FedataFile.h"


#include <Common/Logging.h>

namespace LibOpenNFS::NFS4 {
bool FedataFile::Load(std::string const &fedataPath, FedataFile &fedataFile, uint8_t const nPriColours) {
bool FedataFile::Load(std::string const &fedataPath, FedataFile &fedataFile) {
std::ifstream fedata(fedataPath, std::ios::in | std::ios::binary);

fedataFile.m_nPriColours = nPriColours;
bool const loadStatus{fedataFile._SerializeIn(fedata)};
fedata.close();

Expand All @@ -31,18 +33,15 @@ namespace LibOpenNFS::NFS4 {
// Jump to location of FILEPOS table for car colour names
ifstream.seekg(COLOUR_TABLE_OFFSET, std::ios::beg);
// Read that table in
std::vector<uint32_t> colourNameOffsets(m_nPriColours);
onfs_check(safe_read(ifstream, colourNameOffsets));
uint32_t colourNameOffset;
onfs_check(safe_read(ifstream, colourNameOffset));

for (uint8_t colourIdx = 0; colourIdx < m_nPriColours; ++colourIdx) {
ifstream.seekg(colourNameOffsets[colourIdx], std::ios::beg);
std::string colourName;
std::getline(ifstream, colourName, '\0');
std::string colourName;
ifstream.seekg(colourNameOffset, std::ios::beg);
while (std::getline(ifstream, colourName, '\0')) {
primaryColourNames.emplace_back(colourName.begin(), colourName.end());
}

//uint32_t colourNameLength = colourIdx < (fceHeader->nColours - 1) ? (colourNameOffsets[colourIdx + 1] - colourNameOffsets[colourIdx]) : 32;

return true;
}

Expand Down
2 changes: 1 addition & 1 deletion NFS4/PC/FEDATA/FedataFile.h
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ namespace LibOpenNFS::NFS4 {
public:
FedataFile() = default;

static bool Load(std::string const &fedataPath, FedataFile &fedataFile, uint8_t nPriColours);
static bool Load(std::string const &fedataPath, FedataFile &fedataFile);
static void Save(std::string const &fedataPath, FedataFile &fedataFile);

std::string menuName;
Expand Down
40 changes: 38 additions & 2 deletions NFS4/PC/NFS4Loader.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -40,15 +40,15 @@ namespace LibOpenNFS::NFS4 {

Car::PhysicsData carPhysicsData;

if (std::filesystem::exists(carOutPath)) {
if (std::filesystem::exists(fcePath.str()) && std::filesystem::exists(fedataPath.str()) && std::filesystem::exists(carpPath.str())) {
LogInfo("VIV has already been extracted to %s, skipping", carOutPath.c_str());
} else {
ASSERT(Shared::VivArchive::Load(vivPath.str(), vivFile), "Could not open VIV file: " << vivPath.str());
ASSERT(Shared::VivArchive::Extract(carOutPath, vivFile),
"Could not extract VIV file: " << vivPath.str() << "to: " << carOutPath);
}
ASSERT(NFS4::FceFile::Load(fcePath.str(), fceFile), "Could not load FCE file: " << fcePath.str());
if (!FedataFile::Load(fedataPath.str(), fedataFile, fceFile.nColours)) {
if (!FedataFile::Load(fedataPath.str(), fedataFile)) {
LogWarning("Could not load FeData file: %s", fedataPath.str().c_str());
}
if (Shared::CarpFile::Load(carpPath.str(), carpFile)) {
Expand Down Expand Up @@ -105,6 +105,42 @@ namespace LibOpenNFS::NFS4 {
return track;
}

FedataFile Loader::LoadCarMenuData(std::string const &carBasePath, std::string const &carOutPath, NFSVersion version) {
LogInfo("Loading NFS4 car menu data from %s into %s", carBasePath.c_str(), carOutPath.c_str());

std::filesystem::path p(carBasePath);
std::string carName = p.filename().replace_extension("").string();
std::string const fedataFileName = "fedata.eng";

std::stringstream vivPath, fedataPath;
vivPath << carBasePath;
fedataPath << carOutPath << "/" << fedataFileName;

if (version == NFSVersion::NFS_4) {
vivPath << "/car.viv";
} else {
// MCO
vivPath << ".viv";
}

Shared::VivArchive vivFile;
FceFile fceFile;
FedataFile fedataFile;

if (std::filesystem::exists(fedataPath.str())) {
LogInfo("Fedata file has already been extracted to %s, skipping", carOutPath.c_str());
} else {
ASSERT(Shared::VivArchive::Load(vivPath.str(), vivFile), "Could not open VIV file: " << vivPath.str());
ASSERT(Shared::VivArchive::ExtractFile(carOutPath, vivFile, fedataFileName),
"Could not extract fedata file from VIV file: " << vivPath.str() << "to: " << carOutPath);
}
if (!FedataFile::Load(fedataPath.str(), fedataFile)) {
LogWarning("Could not load FeData file: %s", fedataPath.str().c_str());
}

return fedataFile;
}

Car::MetaData Loader::_ParseAssetData(FceFile const &fceFile, FedataFile const &fedataFile, NFSVersion version) {
LogInfo("Parsing FCE File into ONFS Structures");
Car::MetaData carMetadata;
Expand Down
2 changes: 2 additions & 0 deletions NFS4/PC/NFS4Loader.h
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,8 @@ namespace LibOpenNFS::NFS4 {
static Car LoadCar(std::string const &carBasePath, std::string const &carOutPath, NFSVersion version);
static Track LoadTrack(std::string const &trackBasePath);

static FedataFile LoadCarMenuData(std::string const &carBasePath, std::string const &carOutPath, NFSVersion version);

private:
static Car::MetaData _ParseAssetData(FceFile const &fceFile, FedataFile const &fedataFile, NFSVersion version);
static Car::PhysicsData _ParsePhysicsData(Shared::CarpFile const &carpFile);
Expand Down
38 changes: 37 additions & 1 deletion Shared/VIV/VivArchive.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -24,14 +24,20 @@ namespace LibOpenNFS::Shared {
}

bool VivArchive::Extract(std::string const &outPath, VivArchive &vivFile) {
std::filesystem::create_directories(outPath);
if (!std::filesystem::exists(outPath))
std::filesystem::create_directories(outPath);

for (uint32_t fileIdx = 0; fileIdx < vivFile.nFiles; ++fileIdx) {
VivEntry &curFile{vivFile.files.at(fileIdx)};

std::stringstream out_file_path;
out_file_path << outPath << "/" << curFile.filename;

if (std::filesystem::exists(out_file_path.str())) {
LogInfo("Not extracting file %s, because it already exists", out_file_path.str().c_str());
continue;
}

std::ofstream out(out_file_path.str(), std::ios::out | std::ios::binary);
if (!out.is_open()) {
LogWarning("Error while creating output file %s", out_file_path.str().c_str());
Expand All @@ -43,6 +49,36 @@ namespace LibOpenNFS::Shared {
return true;
}

bool VivArchive::ExtractFile(std::string const &outPath, VivArchive &vivFile, std::string const &fileName) {
if (!std::filesystem::exists(outPath))
std::filesystem::create_directories(outPath);

std::stringstream out_file_path;
out_file_path << outPath << "/" << fileName;
if (std::filesystem::exists(out_file_path.str())) {
return true;
}

for (uint32_t fileIdx = 0; fileIdx < vivFile.nFiles; ++fileIdx) {
VivEntry &curFile{vivFile.files.at(fileIdx)};

if (curFile.filename != fileName) {
continue;
}

std::ofstream out(out_file_path.str(), std::ios::out | std::ios::binary);
if (!out.is_open()) {
LogWarning("Error while creating output file %s", out_file_path.str().c_str());
return false;
}
out.write((char *)curFile.data.data(), curFile.data.size());
out.close();
return true;
}

return false;
}

bool VivArchive::_SerializeIn(std::ifstream &ifstream) {
onfs_check(safe_read(ifstream, vivHeader));

Expand Down
1 change: 1 addition & 0 deletions Shared/VIV/VivArchive.h
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ namespace LibOpenNFS::Shared {
static bool Load(std::string const &vivPath, VivArchive &vivFile);
static void Save(std::string const &vivPath, VivArchive &vivFile);
static bool Extract(std::string const &outPath, VivArchive &vivFile);
static bool ExtractFile(std::string const &outPath, VivArchive &vivFile, std::string const &fileName);

char vivHeader[4];
uint32_t vivSize;
Expand Down