Skip to content

Commit

Permalink
extractor: split up extraction process and allow overriding data di…
Browse files Browse the repository at this point in the history
…r path (#1302)

* extractor: split up extraction process and allow overriding `data` dir path

* lint: formatting

* deps: add CLI11 dependency

* extractor: refactor CLI arg handling
  • Loading branch information
xTVaser authored Apr 15, 2022
1 parent 9168e20 commit ab063bf
Show file tree
Hide file tree
Showing 11 changed files with 9,338 additions and 40 deletions.
7 changes: 7 additions & 0 deletions .vs/launch.vs.json
Original file line number Diff line number Diff line change
Expand Up @@ -125,6 +125,13 @@
"projectTarget" : "dgo_unpacker.exe (bin\\dgo_unpacker.exe)",
"name" : "Run - DGO Unpacker (test)",
"args" : [ "C:\\GameData\\Jak1\\Backup\\DGO-PAL\\GAME", "C:\\GameData\\Jak1\\Backup\\DISC-PAL\\CGO\\GAME.CGO"]
},
{
"type" : "default",
"project" : "CMakeLists.txt",
"projectTarget" : "extractor.exe (bin\\extractor.exe)",
"name" : "Run - Extractor - Extract",
"args" : [ "\"E:\\ISOs\\Jak\\Jak 1.iso\"", "--extract", "-proj-path", "C:\\Users\\xtvas\\Repositories\\opengoal\\launcher\\bundle-test\\data"]
}
]
}
20 changes: 20 additions & 0 deletions CMakePresets.json
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,26 @@
},
"vendor": { "microsoft.com/VisualStudioSettings/CMake/1.0": { "hostOS": [ "Windows" ] } }
},
{
"name": "Release-clang-static",
"displayName": "Windows Release - Static (clang-cl)",
"description": "Target Windows with the Visual Studio development environment.",
"generator": "Ninja",
"binaryDir": "${sourceDir}/out/build/Release",
"architecture": {
"value": "x64",
"strategy": "external"
},
"cacheVariables": {
"CMAKE_BUILD_TYPE": "Release",
"CMAKE_INSTALL_PREFIX": "${sourceDir}/out/install/${presetName}",
"INSTALL_GTEST": "True",
"CMAKE_C_COMPILER": "clang-cl",
"CMAKE_CXX_COMPILER": "clang-cl",
"BUILD_FOR_RELEASE": "true"
},
"vendor": { "microsoft.com/VisualStudioSettings/CMake/1.0": { "hostOS": [ "Windows" ] } }
},
{
"name": "Debug-msvc",
"displayName": "Windows Debug (msvc)",
Expand Down
9 changes: 8 additions & 1 deletion common/util/FileUtil.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -106,11 +106,18 @@ std::optional<std::filesystem::path> try_get_data_dir() {
}
}

bool setup_project_path() {
bool setup_project_path(std::optional<std::filesystem::path> project_path_override) {
if (gFilePathInfo.initialized) {
return true;
}

if (project_path_override) {
gFilePathInfo.path_to_data = *project_path_override;
gFilePathInfo.initialized = true;
fmt::print("Using explicitly set project path: {}\n", project_path_override->string());
return true;
}

auto data_path = try_get_data_dir();
if (data_path) {
gFilePathInfo.path_to_data = *data_path;
Expand Down
2 changes: 1 addition & 1 deletion common/util/FileUtil.h
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ std::filesystem::path get_jak_project_dir();

bool create_dir_if_needed(const std::string& path);
bool create_dir_if_needed_for_file(const std::string& path);
bool setup_project_path();
bool setup_project_path(std::optional<std::filesystem::path> project_path_override);
std::string get_file_path(const std::vector<std::string>& path);
void write_binary_file(const std::string& name, const void* data, size_t size);
void write_rgba_png(const std::string& name, void* data, int w, int h);
Expand Down
135 changes: 102 additions & 33 deletions decompiler/extractor/main.cpp
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
#include "third-party/CLI11.hpp"
#include "third-party/fmt/core.h"
#include "common/util/FileUtil.h"
#include "decompiler/Disasm/OpcodeInfo.h"
Expand All @@ -7,47 +8,33 @@
#include "goalc/compiler/Compiler.h"
#include "common/util/read_iso_file.h"

void setup_global_decompiler_stuff() {
void setup_global_decompiler_stuff(std::optional<std::filesystem::path> project_path_override) {
file_util::init_crc();
decompiler::init_opcode_info();
file_util::setup_project_path();
file_util::setup_project_path(project_path_override);
}

int main(int argc, char** argv) {
using namespace decompiler;
fmt::print("OpenGOAL Level Extraction Tool\n");
if (argc != 2) {
fmt::print(" usage: extractor <path-to-jak1-files>\n");
return 1;
}
void extract_files(std::filesystem::path data_dir_path, std::filesystem::path extracted_iso_path) {
fmt::print("Note: input isn't a folder, assuming it's an ISO file...\n");

// todo: print revision here.
setup_global_decompiler_stuff();
std::filesystem::create_directories(extracted_iso_path);

std::filesystem::path jak1_input_files(argv[1]);
// make sure the input looks right
if (!std::filesystem::exists(jak1_input_files)) {
fmt::print("Error: input folder {} does not exist\n", jak1_input_files.string());
return 1;
}

if (!std::filesystem::is_directory(jak1_input_files)) {
fmt::print("Note: input isn't a folder, assuming it's an ISO file...\n");
auto path_to_iso_files = file_util::get_jak_project_dir() / "extracted_iso";
std::filesystem::create_directories(path_to_iso_files);

auto fp = fopen(jak1_input_files.string().c_str(), "rb");
ASSERT_MSG(fp, "failed to open input ISO file\n");
unpack_iso_files(fp, path_to_iso_files);
fclose(fp);
jak1_input_files = path_to_iso_files;
}
auto fp = fopen(data_dir_path.string().c_str(), "rb");
ASSERT_MSG(fp, "failed to open input ISO file\n");
unpack_iso_files(fp, extracted_iso_path);
fclose(fp);
}

if (!std::filesystem::exists(jak1_input_files / "DGO")) {
int validate(std::filesystem::path path_to_iso_files) {
if (!std::filesystem::exists(path_to_iso_files / "DGO")) {
fmt::print("Error: input folder doesn't have a DGO folder. Is this the right input?\n");
return 1;
}
return 0;
}

void decompile(std::filesystem::path jak1_input_files) {
using namespace decompiler;
Config config = read_config_file(
(file_util::get_jak_project_dir() / "decompiler" / "config" / "jak1_ntsc_black_label.jsonc")
.string(),
Expand Down Expand Up @@ -123,15 +110,97 @@ int main(int argc, char** argv) {
extract_from_level(db, tex_db, lev, config.hacks, config.rip_levels);
}
}
}

// Compile!
void compile(std::filesystem::path extracted_iso_path) {
Compiler compiler;
compiler.make_system().set_constant("*iso-data*", absolute(jak1_input_files).string());
compiler.make_system().set_constant("*iso-data*", absolute(extracted_iso_path).string());
compiler.make_system().set_constant("*use-iso-data-path*", true);

compiler.make_system().load_project_file(
(file_util::get_jak_project_dir() / "goal_src" / "game.gp").string());
compiler.run_front_end_on_string("(mi)");
}

void launch_game() {
system((file_util::get_jak_project_dir() / "../gk").string().c_str());
}
}

int main(int argc, char** argv) {
std::filesystem::path data_dir_path;
std::filesystem::path project_path_override;
bool flag_runall = false;
bool flag_extract = false;
bool flag_validate = false;
bool flag_decompile = false;
bool flag_compile = false;
bool flag_play = false;

CLI::App app{"OpenGOAL Level Extraction Tool"};
app.add_option("game-files-path", data_dir_path,
"The path to the folder with the ISO extracted or the ISO itself")
->check(CLI::ExistingPath)
->required();
app.add_option("--proj-path", project_path_override,
"Explicitly set the location of the 'data/' folder")
->check(CLI::ExistingPath);
app.add_flag("-a,--all", flag_runall, "Run all steps, from extraction to playing the game");
app.add_flag("-e,--extract", flag_extract, "Extract the ISO");
app.add_flag("-v,--validate", flag_validate, "Validate the ISO / game files");
app.add_flag("-d,--decompile", flag_decompile, "Decompile the game data");
app.add_flag("-c,--compile", flag_compile, "Compile the game");
app.add_flag("-p,--play", flag_play, "Play the game");
app.validate_positionals();

CLI11_PARSE(app, argc, argv);

fmt::print("Working Directory - {}\n", std::filesystem::current_path().string());

// If no flag is set, we default to running everything
if (!flag_extract && !flag_validate && !flag_decompile && !flag_compile && !flag_play) {
fmt::print("Running all steps, no flags provided!\n");
flag_runall = true;
}

// todo: print revision here.
if (!project_path_override.empty()) {
setup_global_decompiler_stuff(std::make_optional(project_path_override));
} else {
setup_global_decompiler_stuff(std::nullopt);
}

auto path_to_iso_files = file_util::get_jak_project_dir() / "extracted_iso";

// make sure the input looks right
if (!std::filesystem::exists(data_dir_path)) {
fmt::print("Error: input folder {} does not exist\n", data_dir_path.string());
return 1;
}

if (flag_runall || flag_extract) {
if (!std::filesystem::is_directory(path_to_iso_files)) {
extract_files(data_dir_path, path_to_iso_files);
}
}

if (flag_runall || flag_validate) {
auto ok = validate(path_to_iso_files);
if (ok != 0) {
return ok;
}
}

if (flag_runall || flag_decompile) {
decompile(path_to_iso_files);
}

if (flag_runall || flag_compile) {
compile(path_to_iso_files);
}

if (flag_runall || flag_play) {
launch_game();
}

return 0;
}
2 changes: 1 addition & 1 deletion decompiler/main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@
int main(int argc, char** argv) {
fmt::print("[Mem] Top of main: {} MB\n", get_peak_rss() / (1024 * 1024));
using namespace decompiler;
if (!file_util::setup_project_path()) {
if (!file_util::setup_project_path(std::nullopt)) {
return 1;
}
lg::set_file(file_util::get_file_path({"log/decompiler.txt"}));
Expand Down
7 changes: 6 additions & 1 deletion game/main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@ int main(int argc, char** argv) {

bool verbose = false;
bool disable_avx2 = false;
std::optional<std::filesystem::path> project_path_override = std::nullopt;
for (int i = 1; i < argc; i++) {
if (std::string("-v") == argv[i]) {
verbose = true;
Expand All @@ -48,9 +49,13 @@ int main(int argc, char** argv) {
if (std::string("-no-avx2") == argv[i]) {
disable_avx2 = true;
}

if (std::string("-proj-path") == argv[i] && i + 1 < argc) {
project_path_override = std::make_optional(std::filesystem::path(argv[i + 1]));
}
}

if (!file_util::setup_project_path()) {
if (!file_util::setup_project_path(project_path_override)) {
return 1;
}

Expand Down
2 changes: 1 addition & 1 deletion goalc/main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ void setup_logging(bool verbose) {
int main(int argc, char** argv) {
(void)argc;
(void)argv;
if (!file_util::setup_project_path()) {
if (!file_util::setup_project_path(std::nullopt)) {
return 1;
}
std::string argument;
Expand Down
2 changes: 1 addition & 1 deletion test/offline/offline_test_main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -347,7 +347,7 @@ bool compile(Decompiler& dc,
int main(int argc, char* argv[]) {
fmt::print("Offline Decompiler Test 2\n");
lg::initialize();
if (!file_util::setup_project_path()) {
if (!file_util::setup_project_path(std::nullopt)) {
return 1;
}

Expand Down
2 changes: 1 addition & 1 deletion test/test_main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@
int main(int argc, char** argv) {
// hopefully get a debug print on github actions
setup_cpu_info();
file_util::setup_project_path();
file_util::setup_project_path(std::nullopt);
lg::initialize();

::testing::InitGoogleTest(&argc, argv);
Expand Down
Loading

0 comments on commit ab063bf

Please sign in to comment.