Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

add mpk unpack support #22

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
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
4 changes: 4 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -16,3 +16,7 @@ include/npaversion.hpp
*.dylib
*.lib
*.so

src/parser.cpp
src/parser.hpp
src/lexer.cpp
1 change: 1 addition & 0 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,7 @@ bison_target(parser ${CMAKE_SOURCE_DIR}/src/parser.y ${CMAKE_SOURCE_DIR}/src/par
add_flex_bison_dependency(lexer parser)

add_library(npa SHARED
src/impkfile.cpp
src/inpafile.cpp
src/npafile.cpp
src/nsbmagic.cpp
Expand Down
34 changes: 34 additions & 0 deletions include/impkfile.hpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
#ifndef I_NIPA_FILE
#define I_NIPA_FILE

#include "inpafile.hpp"

class IMpkFile : public INpaFile
{
struct
{
char Magic[6]; // M P K \0 \0 \0
uint16_t Version; // \x02 \0
uint32_t FileCount;
char Pad[0x34];
} MPKHeader;
public:
struct MpkEntry : INpaFile::Entry
{
uint32_t Unk;
string Filename;
uint32_t FileID;
};

IMpkFile(const string& Name);
~IMpkFile();

char* ReadFile(NpaIterator iter);
virtual char* ReadData(NpaIterator iter, uint32_t LocalOffset, uint32_t Size, void *(*Alloc)(size_t) = DefaultAlloc);
bool IsDirectory(NpaIterator iter);

protected:
void ReadHeader();
};

#endif
76 changes: 76 additions & 0 deletions src/impkfile.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,76 @@
#include "impkfile.hpp"
#include "fscommon.hpp"
#include "buffer.hpp"
#include <fstream>
#include <boost/algorithm/string/replace.hpp>
#include <zlib.h>

IMpkFile::IMpkFile(const string& Name) : INpaFile(Name)
{
ReadHeader();
}

IMpkFile::~IMpkFile()
{
}

void IMpkFile::ReadHeader()
{
uint32_t Size;
char* pData = fs::ReadFile(Name, Size);
if (!pData)
return;

Npa::Buffer File(pData, Size);
File.Read(MPKHeader.Magic, 6);
assert(strncmp("MPK\x00\x00\x00", MPKHeader.Magic, 6) == 0);

MPKHeader.Version = File.Read<uint16_t>();
MPKHeader.FileCount = File.Read<uint32_t>();
File.Read(MPKHeader.Pad, 0x34);

char NameBuf[224];

for (uint32_t i = 0; i < MPKHeader.FileCount; ++i)
{
MpkEntry* MPKEntry = new MpkEntry;
MPKEntry->Unk = File.Read<uint32_t>();
MPKEntry->FileID = File.Read<uint32_t>();
MPKEntry->Offset = File.Read<uint64_t>();
MPKEntry->Size = File.Read<uint64_t>();
uint64_t Size2 = File.Read<uint64_t>(); // ?
assert(("The second size has a hidden meaning?", MPKEntry->Size == Size2));
File.Read(NameBuf, 224);
MPKEntry->Filename = string(NameBuf);

string UtfPath = NpaFile::ToUtf8(MPKEntry->Filename);
boost::replace_all(UtfPath, "\\", "/");
Registry[UtfPath] = MPKEntry;
}
}


char* IMpkFile::ReadFile(NpaIterator iter)
{
return ReadData(iter, 0, ((MpkEntry*)iter->second)->Size);
}

char* IMpkFile::ReadData(NpaIterator iter, uint32_t LocalOffset, uint32_t Size, void *(*Alloc)(size_t))
{
ifstream File(Name, ios::binary);
if (!File)
return nullptr;

MpkEntry* MPKEntry = (MpkEntry*)iter->second;
uint8_t* buffer = new uint8_t[Size];
Copy link
Member

Choose a reason for hiding this comment

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

Alloc parameter should be used instead of new.


File.seekg(MPKEntry->Offset + LocalOffset);
File.read((char*)buffer, Size);

return (char*)buffer;
}

bool IMpkFile::IsDirectory(NpaIterator iter)
{
return false;
}