Skip to content

Commit

Permalink
support survivalcraft 2.1
Browse files Browse the repository at this point in the history
  • Loading branch information
qnnnnez committed Jul 14, 2017
1 parent 7557ef5 commit 8114563
Show file tree
Hide file tree
Showing 6 changed files with 28 additions and 23 deletions.
6 changes: 3 additions & 3 deletions main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -48,17 +48,17 @@ int main(int argc, char *argv[])
std::string cmdarg = argv[1];
if (cmdarg == "--help" || cmdarg == "-h")
{
printUsage (argc, argv);
printUsage(argc, argv);
return 0;
}
else if (cmdarg == "--version" || cmdarg == "-v")
{
printVersion ();
printVersion();
return 0;
}
else if (cmdarg == "--licence" || cmdarg == "--license")
{
printLicense ();
printLicense();
return 0;
}
else
Expand Down
20 changes: 12 additions & 8 deletions pack.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -176,6 +176,7 @@ namespace scpak
writer.writeUtf8Char(fallbackCode);
fList.close();

writer.writeBoolean(0);
writer.writeInt(width);
writer.writeInt(height);
writer.writeInt(1);
Expand Down Expand Up @@ -208,15 +209,18 @@ namespace scpak
throw std::runtime_error("cannot load image file: " + item.name);
if (comp != 4)
throw std::runtime_error("image must have 4 components in every pixel: " + item.name);
int mipmapLevel = 0;
if (!meta.empty())
mipmapLevel = std::stoi(meta);
item.length = sizeof(int) * 3 + calcMipmapSize(width, height, mipmapLevel) * comp;

bool keepSourceImageInTag = std::stoi(meta);
int mipmapLevel = std::stoi(meta.substr(meta.find(' ')));

item.length = 1 + sizeof(int) * 3 + calcMipmapSize(width, height, mipmapLevel) * comp;
item.data.resize(item.length);
*reinterpret_cast<int*>(item.data.data()) = width;
*(reinterpret_cast<int*>(item.data.data()) + 1) = height;
*(reinterpret_cast<int*>(item.data.data()) + 2) = mipmapLevel;
std::memcpy(item.data.data() + sizeof(int) * 3, data, width*height*comp);
MemoryBinaryWriter writer(item.data.data());
writer.writeBoolean(keepSourceImageInTag);
writer.writeInt(width);
writer.writeInt(height);
writer.writeInt(mipmapLevel);
std::copy(data, data + width*height*comp, item.data.begin() + writer.position);
stbi_image_free(data);
int offset = generateMipmap(width, height, mipmapLevel, item.data.data() + sizeof(int) * 3);
}
Expand Down
4 changes: 0 additions & 4 deletions pakfile.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -48,8 +48,6 @@ namespace scpak

void PakFile::save(std::ostream &stream)
{
// we cannot detect the length of the file and where content begins,
// so we have to write the header twice
// write file header for the first time
PakHeader header;
header.contentCount = m_contents.size();
Expand Down Expand Up @@ -78,8 +76,6 @@ namespace scpak
item.offset = static_cast<int>(stream.tellp()) - header.contentOffset;
stream.write(reinterpret_cast<char*>(item.data.data()), item.length);
}

header.fileLength = stream.tellp();
// write the header again
stream.seekp(0, std::ios::beg);
stream.write(reinterpret_cast<char*>(&header), sizeof(header));
Expand Down
1 change: 0 additions & 1 deletion pakfile.h
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,6 @@ namespace scpak
typedef struct // PakHeader
{
byte magic[4] = { byte('P'), byte('A'), byte('K'), byte('\0') };
std::int32_t fileLength;
std::int32_t contentOffset;
std::int32_t contentCount;

Expand Down
18 changes: 12 additions & 6 deletions unpack.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -147,6 +147,7 @@ namespace scpak
float scale = reader.readSingle();
int fallbackCode = reader.readUtf8Char();

bool keepSourceImageInTag = reader.readBoolean();
int width = reader.readInt32();
int height = reader.readInt32();
int mipmapLevel = reader.readInt32();
Expand Down Expand Up @@ -174,14 +175,19 @@ namespace scpak
std::string unpack_texture(const std::string &outputDir, const PakItem &item)
{
std::string fileName = outputDir + item.name + ".tga";
const byte *data = item.data.data();
const int &width = *reinterpret_cast<const int*>(data);
const int &height = *(reinterpret_cast<const int*>(data) + 1);
const int &mipmapLevel = *(reinterpret_cast<const int*>(data) + 2);
const void *imageData = reinterpret_cast<const void*>(data + sizeof(int) * 3);
MemoryBinaryReader reader(item.data.data());
bool keepSourceImageInTag = reader.readBoolean();
int width = reader.readInt32();
int height = reader.readInt32();
int mipmapLevel = reader.readInt32();
const void *imageData = reinterpret_cast<const void*>(item.data.data() + reader.position);
stbi_write_tga(fileName.c_str(), width, height, 4, imageData);

return std::to_string(mipmapLevel);
std::string meta;
meta += keepSourceImageInTag ? '1' : '0';
meta += ' ';
meta += std::to_string(mipmapLevel);
return meta;
}

void unpack_soundBuffer(const std::string &outputDir, const PakItem &item)
Expand Down

0 comments on commit 8114563

Please sign in to comment.