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 --dump-compiledcode option to extract embedded compiledCode.bin from installer #161

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
42 changes: 42 additions & 0 deletions src/cli/extract.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -890,6 +890,42 @@ void create_output_directory(const extract_options & o) {

} // anonymous namespace

void dump_compiledcode(std::istream & is, const loader::offsets & offsets, const setup::info & info, const extract_options & o) {
setup::version version;
is.seekg(offsets.header_offset);
version.load(is);

std::string filename = "compiledCode.bin";

if(!o.quiet) {
std::cout << "Dumping compiled setup code to \""
<< color::white << filename << color::reset << "\"\n";
} else if(!o.silent) {
std::cout << filename << '\n';
}

fs::path path = o.output_dir / filename;
util::ofstream ofs;
try {
ofs.open(path, std::ios_base::out | std::ios_base::trunc | std::ios_base::binary);
if(!ofs.is_open()) {
throw std::exception();
}
} catch(...) {
throw std::runtime_error("Could not open output file \"" + path.string() + '"');
}

try {
ofs.write(info.header.compiled_code.c_str(), static_cast<std::streamsize>(info.header.compiled_code.length()));
} catch(const std::exception & e) {
std::ostringstream oss;
oss << "Stream error while dumping compiled setup code!\n";
oss << " ├─ detected setup version: " << version << '\n';
oss << " └─ error reason: " << e.what();
throw format_error(oss.str());
}
}

void process_file(const fs::path & installer, const extract_options & o) {

bool is_directory;
Expand Down Expand Up @@ -992,6 +1028,12 @@ void process_file(const fs::path & installer, const extract_options & o) {
throw format_error(oss.str());
}

if(o.dump_compiledcode) {
create_output_directory(o);
dump_compiledcode(ifs, offsets, info, o);
return;
}

if(o.gog_galaxy && (o.list || o.test || o.extract || o.list_languages)) {
gog::parse_galaxy_files(info, o.gog);
}
Expand Down
5 changes: 5 additions & 0 deletions src/cli/extract.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,9 @@
#include <boost/cstdint.hpp>
#include <boost/filesystem/path.hpp>

#include "loader/offsets.hpp"
#include "setup/filename.hpp"
#include "setup/info.hpp"

struct format_error : public std::runtime_error {
explicit format_error(const std::string & reason) : std::runtime_error(reason) { }
Expand All @@ -60,6 +62,7 @@ struct extract_options {
#ifdef DEBUG
bool dump_headers; //!< Dump setup headers
#endif
bool dump_compiledcode; //!< Dump compiled code
bool list; //!< List files
bool test; //!< Test files (but don't extract)
bool extract; //!< Extract files
Expand Down Expand Up @@ -119,4 +122,6 @@ struct extract_options {

void process_file(const boost::filesystem::path & installer, const extract_options & o);

void dump_compiledcode(std::istream & is, const loader::offsets & offsets, const setup::info & info, const extract_options & o);

#endif // INNOEXTRACT_CLI_EXTRACT_HPP
9 changes: 9 additions & 0 deletions src/cli/main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -144,6 +144,7 @@ int main(int argc, char * argv[]) {
#ifdef DEBUG
("dump-headers", "Dump decompressed setup headers")
#endif
("dump-compiledcode", "Dump decompressed compiled code binary")
;

po::options_description modifiers("Modifiers");
Expand Down Expand Up @@ -442,6 +443,14 @@ int main(int argc, char * argv[]) {
}
#endif

o.dump_compiledcode = (options.count("dump-compiledcode") != 0);
if(o.dump_compiledcode) {
if(explicit_action || o.data_version) {
log_error << "Combining --dump-compiledcode with other options is not allowed";
return ExitUserError;
}
}

o.extract_unknown = (options.count("no-extract-unknown") == 0);

const std::vector<std::string> & files = options["setup-files"]
Expand Down