Skip to content

Commit

Permalink
Make dgo unpacker work on pal dgos with wrong size (#714)
Browse files Browse the repository at this point in the history
* make dgo unpacker work on pal dgos with wrong size

* format string
  • Loading branch information
water111 authored Jul 24, 2021
1 parent ba10f91 commit f3f32cc
Show file tree
Hide file tree
Showing 3 changed files with 40 additions and 2 deletions.
12 changes: 11 additions & 1 deletion common/util/DgoReader.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,17 @@ DgoReader::DgoReader(std::string file_name, const std::vector<u8>& data)

// get all obj files...
for (uint32_t i = 0; i < header.object_count; i++) {
auto obj_header = reader.read<ObjectHeader>();
ObjectHeader obj_header = reader.read<ObjectHeader>();

if (reader.bytes_left() < obj_header.size && i == header.object_count - 1 &&
obj_header.size - reader.bytes_left() <= 48) {
printf(
"Warning: final file %s in DGO %s has a size missing %d bytes. It will be adjusted from "
"%d to %d bytes.\n",
obj_header.name, header.name, obj_header.size - reader.bytes_left(), obj_header.size,
(int)reader.bytes_left());
obj_header.size = reader.bytes_left();
}
assert(reader.bytes_left() >= obj_header.size);
assert_string_empty_after(obj_header.name, 60);

Expand Down
16 changes: 16 additions & 0 deletions common/util/FileUtil.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
#include "BinaryWriter.h"
#include "common/common_types.h"
#include "third-party/svpng.h"
#include "third-party/fmt/core.h"
#include "third-party/lzokay/lzokay.hpp"

#ifdef _WIN32
Expand Down Expand Up @@ -119,6 +120,21 @@ void write_text_file(const std::string& file_name, const std::string& text) {
}

std::vector<uint8_t> read_binary_file(const std::string& filename) {
// make sure file exists and isn't a directory
std::filesystem::path path(filename);

auto status = std::filesystem::status(std::filesystem::path(filename));

if (!std::filesystem::exists(status)) {
throw std::runtime_error(fmt::format("File {} cannot be opened: does not exist.", filename));
}

if (status.type() != std::filesystem::file_type::regular &&
status.type() != std::filesystem::file_type::symlink) {
throw std::runtime_error(
fmt::format("File {} cannot be opened: not a regular file or symlink.", filename));
}

auto fp = fopen(filename.c_str(), "rb");
if (!fp)
throw std::runtime_error("File " + filename +
Expand Down
14 changes: 13 additions & 1 deletion tools/dgo_unpacker.cpp
Original file line number Diff line number Diff line change
@@ -1,9 +1,11 @@
#include <cstdio>
#include <stdexcept>
#include "common/versions.h"
#include "common/util/FileUtil.h"
#include "common/util/DgoReader.h"

int main(int argc, char** argv) {
namespace {
int run(int argc, char** argv) {
printf("OpenGOAL version %d.%d\n", versions::GOAL_VERSION_MAJOR, versions::GOAL_VERSION_MINOR);
printf("DGO Unpacking Tool\n");

Expand Down Expand Up @@ -42,3 +44,13 @@ int main(int argc, char** argv) {
printf("Done\n");
return 0;
}
} // namespace

int main(int argc, char** argv) {
try {
return run(argc, argv);
} catch (const std::exception& e) {
printf("An error occurred: %s\n", e.what());
return 1;
}
}

0 comments on commit f3f32cc

Please sign in to comment.