2020-08-22 23:30:17 -04:00
|
|
|
#include "config.h"
|
2022-06-22 23:37:46 -04:00
|
|
|
|
2022-10-01 11:58:36 -04:00
|
|
|
#include "common/log/log.h"
|
2020-09-10 20:03:31 -04:00
|
|
|
#include "common/util/FileUtil.h"
|
2021-03-13 16:10:39 -05:00
|
|
|
#include "common/util/json_util.h"
|
2022-06-22 23:37:46 -04:00
|
|
|
|
2021-03-27 15:18:59 -04:00
|
|
|
#include "decompiler/util/config_parsers.h"
|
2020-08-22 23:30:17 -04:00
|
|
|
|
2022-06-22 23:37:46 -04:00
|
|
|
#include "third-party/fmt/core.h"
|
|
|
|
#include "third-party/json.hpp"
|
|
|
|
|
2021-01-06 20:04:15 -05:00
|
|
|
namespace decompiler {
|
2020-08-22 23:30:17 -04:00
|
|
|
|
2020-12-26 11:09:59 -05:00
|
|
|
namespace {
|
|
|
|
/*!
|
|
|
|
* Read an entry from cfg containing the name of a json file, and parse that file.
|
|
|
|
* Relative to jak-project directory.
|
|
|
|
*/
|
|
|
|
nlohmann::json read_json_file_from_config(const nlohmann::json& cfg, const std::string& file_key) {
|
|
|
|
auto file_name = cfg.at(file_key).get<std::string>();
|
|
|
|
auto file_txt = file_util::read_text_file(file_util::get_file_path({file_name}));
|
2021-05-11 21:57:05 -04:00
|
|
|
return parse_commented_json(file_txt, file_name);
|
2020-12-26 11:09:59 -05:00
|
|
|
}
|
|
|
|
} // namespace
|
|
|
|
|
|
|
|
/*!
|
2021-05-11 20:49:54 -04:00
|
|
|
* Parse the main config file and return decompiler config.
|
2020-12-26 11:09:59 -05:00
|
|
|
*/
|
2022-07-29 20:10:11 -04:00
|
|
|
Config read_config_file(const fs::path& path_to_config_file, const std::string& override_json) {
|
2021-05-11 20:49:54 -04:00
|
|
|
Config config;
|
2020-09-10 20:03:31 -04:00
|
|
|
auto config_str = file_util::read_text_file(path_to_config_file);
|
2022-07-29 20:10:11 -04:00
|
|
|
auto cfg = parse_commented_json(config_str, path_to_config_file.string());
|
2022-10-01 11:58:36 -04:00
|
|
|
lg::info("Config Overide: '{}'\n", override_json);
|
2022-07-29 20:10:11 -04:00
|
|
|
auto cfg_override = parse_commented_json(override_json, "");
|
|
|
|
cfg.update(cfg_override);
|
2022-02-12 17:48:50 -05:00
|
|
|
|
2022-06-06 17:58:49 -04:00
|
|
|
int version_int = cfg.at("game_version").get<int>();
|
|
|
|
ASSERT(version_int == 1 || version_int == 2);
|
|
|
|
config.game_version = (GameVersion)version_int;
|
2021-12-26 16:43:16 +00:00
|
|
|
config.text_version = cfg.at("text_version").get<GameTextVersion>();
|
2022-02-04 23:37:48 +00:00
|
|
|
config.game_name = cfg.at("game_name").get<std::string>();
|
2022-02-12 17:48:50 -05:00
|
|
|
if (cfg.contains("expected_elf_name")) {
|
|
|
|
config.expected_elf_name = cfg.at("expected_elf_name").get<std::string>();
|
|
|
|
}
|
2022-06-06 17:58:49 -04:00
|
|
|
config.all_types_file = cfg.at("all_types_file").get<std::string>();
|
2020-08-22 23:30:17 -04:00
|
|
|
|
2021-05-11 20:49:54 -04:00
|
|
|
auto inputs_json = read_json_file_from_config(cfg, "inputs_file");
|
|
|
|
config.dgo_names = inputs_json.at("dgo_names").get<std::vector<std::string>>();
|
|
|
|
config.object_file_names = inputs_json.at("object_file_names").get<std::vector<std::string>>();
|
|
|
|
config.str_file_names = inputs_json.at("str_file_names").get<std::vector<std::string>>();
|
2021-07-18 19:28:56 -04:00
|
|
|
config.audio_dir_file_name = inputs_json.at("audio_dir_file_name").get<std::string>();
|
|
|
|
config.streamed_audio_file_names =
|
|
|
|
inputs_json.at("streamed_audio_file_names").get<std::vector<std::string>>();
|
2020-10-24 22:51:40 -04:00
|
|
|
|
2023-01-15 11:33:39 -05:00
|
|
|
if (cfg.contains("art_group_dump_file")) {
|
|
|
|
auto json_data = file_util::read_text_file(
|
|
|
|
file_util::get_file_path({cfg.at("art_group_dump_file").get<std::string>()}));
|
|
|
|
std::unordered_map<std::string, std::unordered_map<int, std::string>> serialized =
|
|
|
|
parse_commented_json(json_data, "art_group_dump_file");
|
|
|
|
config.art_group_info_dump = serialized;
|
|
|
|
}
|
|
|
|
|
2021-05-11 20:49:54 -04:00
|
|
|
if (cfg.contains("obj_file_name_map_file")) {
|
|
|
|
config.obj_file_name_map_file = cfg.at("obj_file_name_map_file").get<std::string>();
|
2020-10-24 22:51:40 -04:00
|
|
|
}
|
2021-05-11 20:49:54 -04:00
|
|
|
config.disassemble_code = cfg.at("disassemble_code").get<bool>();
|
|
|
|
config.decompile_code = cfg.at("decompile_code").get<bool>();
|
|
|
|
config.write_hex_near_instructions = cfg.at("write_hex_near_instructions").get<bool>();
|
|
|
|
config.write_scripts = cfg.at("write_scripts").get<bool>();
|
|
|
|
config.disassemble_data = cfg.at("disassemble_data").get<bool>();
|
|
|
|
config.process_tpages = cfg.at("process_tpages").get<bool>();
|
|
|
|
config.process_game_text = cfg.at("process_game_text").get<bool>();
|
|
|
|
config.process_game_count = cfg.at("process_game_count").get<bool>();
|
2022-05-20 02:30:14 +01:00
|
|
|
config.process_art_groups = cfg.at("process_art_groups").get<bool>();
|
2023-01-04 18:26:59 -05:00
|
|
|
config.dump_art_group_info = cfg.at("dump_art_group_info").get<bool>();
|
2021-05-11 20:49:54 -04:00
|
|
|
config.hexdump_code = cfg.at("hexdump_code").get<bool>();
|
|
|
|
config.hexdump_data = cfg.at("hexdump_data").get<bool>();
|
2022-06-08 18:34:52 -04:00
|
|
|
config.find_functions = cfg.at("find_functions").get<bool>();
|
2021-05-11 20:49:54 -04:00
|
|
|
config.dump_objs = cfg.at("dump_objs").get<bool>();
|
2021-05-24 19:52:19 -04:00
|
|
|
config.print_cfgs = cfg.at("print_cfgs").get<bool>();
|
2021-05-31 10:43:25 -04:00
|
|
|
config.generate_symbol_definition_map = cfg.at("generate_symbol_definition_map").get<bool>();
|
2021-10-15 23:17:51 +01:00
|
|
|
config.is_pal = cfg.at("is_pal").get<bool>();
|
2022-08-05 12:25:35 -04:00
|
|
|
config.rip_levels = cfg.at("rip_levels").get<bool>();
|
2022-04-25 21:53:23 -04:00
|
|
|
config.extract_collision = cfg.at("extract_collision").get<bool>();
|
2022-06-25 21:26:15 -04:00
|
|
|
config.generate_all_types = cfg.at("generate_all_types").get<bool>();
|
|
|
|
if (cfg.contains("old_all_types_file")) {
|
|
|
|
config.old_all_types_file = cfg.at("old_all_types_file").get<std::string>();
|
|
|
|
}
|
2021-02-01 20:41:37 -05:00
|
|
|
auto allowed = cfg.at("allowed_objects").get<std::vector<std::string>>();
|
|
|
|
for (const auto& x : allowed) {
|
2021-05-11 20:49:54 -04:00
|
|
|
config.allowed_objects.insert(x);
|
2021-02-01 20:41:37 -05:00
|
|
|
}
|
2021-09-21 23:40:38 +01:00
|
|
|
auto banned = cfg.at("banned_objects").get<std::vector<std::string>>();
|
|
|
|
for (const auto& x : banned) {
|
|
|
|
config.banned_objects.insert(x);
|
|
|
|
}
|
2021-02-01 20:41:37 -05:00
|
|
|
|
2021-03-13 16:10:39 -05:00
|
|
|
auto type_casts_json = read_json_file_from_config(cfg, "type_casts_file");
|
|
|
|
for (auto& kv : type_casts_json.items()) {
|
2020-12-17 15:48:07 -05:00
|
|
|
auto& function_name = kv.key();
|
2021-03-13 16:10:39 -05:00
|
|
|
auto& casts = kv.value();
|
|
|
|
for (auto& cast : casts) {
|
2021-06-04 13:43:19 -04:00
|
|
|
if (cast.at(0).is_string()) {
|
|
|
|
auto cast_name = cast.at(0).get<std::string>();
|
|
|
|
if (cast_name == "_stack_") {
|
|
|
|
// it's a stack var cast
|
|
|
|
StackTypeCast stack_cast;
|
|
|
|
stack_cast.stack_offset = cast.at(1).get<int>();
|
|
|
|
stack_cast.type_name = cast.at(2).get<std::string>();
|
|
|
|
config.stack_type_casts_by_function_by_stack_offset[function_name]
|
|
|
|
[stack_cast.stack_offset] = stack_cast;
|
|
|
|
} else {
|
|
|
|
throw std::runtime_error(fmt::format("Unknown cast type: {}", cast_name));
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
auto idx_range = parse_json_optional_integer_range(cast.at(0));
|
|
|
|
for (auto idx : idx_range) {
|
|
|
|
RegisterTypeCast type_cast;
|
|
|
|
type_cast.atomic_op_idx = idx;
|
2021-06-18 21:10:00 -04:00
|
|
|
type_cast.reg = Register(cast.at(1).get<std::string>());
|
2021-06-04 13:43:19 -04:00
|
|
|
type_cast.type_name = cast.at(2).get<std::string>();
|
|
|
|
config.register_type_casts_by_function_by_atomic_op_idx[function_name][idx].push_back(
|
|
|
|
type_cast);
|
|
|
|
}
|
2020-12-17 15:48:07 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2020-12-26 11:09:59 -05:00
|
|
|
|
|
|
|
auto anon_func_json = read_json_file_from_config(cfg, "anonymous_function_types_file");
|
|
|
|
for (auto& kv : anon_func_json.items()) {
|
|
|
|
auto& obj_file_name = kv.key();
|
|
|
|
auto& anon_types = kv.value();
|
|
|
|
for (auto& anon_type : anon_types) {
|
|
|
|
auto id = anon_type.at(0).get<int>();
|
|
|
|
const auto& type_name = anon_type.at(1).get<std::string>();
|
2021-05-11 20:49:54 -04:00
|
|
|
config.anon_function_types_by_obj_by_id[obj_file_name][id] = type_name;
|
2020-12-26 11:09:59 -05:00
|
|
|
}
|
|
|
|
}
|
2021-02-09 20:59:14 -05:00
|
|
|
auto var_names_json = read_json_file_from_config(cfg, "var_names_file");
|
|
|
|
for (auto& kv : var_names_json.items()) {
|
|
|
|
auto& function_name = kv.key();
|
|
|
|
auto arg = kv.value().find("args");
|
|
|
|
if (arg != kv.value().end()) {
|
|
|
|
for (auto& x : arg.value()) {
|
2021-05-11 20:49:54 -04:00
|
|
|
config.function_arg_names[function_name].push_back(x);
|
2021-02-09 20:59:14 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
auto var = kv.value().find("vars");
|
|
|
|
if (var != kv.value().end()) {
|
2021-03-13 16:10:39 -05:00
|
|
|
for (auto& vkv : var->get<std::unordered_map<std::string, nlohmann::json>>()) {
|
|
|
|
LocalVarOverride override;
|
|
|
|
if (vkv.second.is_string()) {
|
|
|
|
override.name = vkv.second.get<std::string>();
|
|
|
|
} else if (vkv.second.is_array()) {
|
|
|
|
override.name = vkv.second[0].get<std::string>();
|
|
|
|
override.type = vkv.second[1].get<std::string>();
|
|
|
|
} else {
|
|
|
|
throw std::runtime_error("Invalid function var override.");
|
|
|
|
}
|
2021-05-11 20:49:54 -04:00
|
|
|
config.function_var_overrides[function_name][vkv.first] = override;
|
2021-02-09 20:59:14 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2021-02-20 11:42:46 -05:00
|
|
|
|
|
|
|
auto label_types_json = read_json_file_from_config(cfg, "label_types_file");
|
|
|
|
for (auto& kv : label_types_json.items()) {
|
|
|
|
auto& obj_name = kv.key();
|
|
|
|
auto& types = kv.value();
|
|
|
|
for (auto& x : types) {
|
|
|
|
const auto& name = x.at(0).get<std::string>();
|
|
|
|
const auto& type_name = x.at(1).get<std::string>();
|
2021-08-29 11:13:06 -04:00
|
|
|
bool is_val = false;
|
|
|
|
std::optional<int> array_size;
|
|
|
|
if (x.size() > 2) {
|
|
|
|
if (x.at(2).is_boolean()) {
|
|
|
|
is_val = x.at(2).get<bool>();
|
|
|
|
} else {
|
|
|
|
array_size = x.at(2).get<int>();
|
|
|
|
}
|
2021-02-27 14:40:18 -05:00
|
|
|
}
|
2021-08-29 11:13:06 -04:00
|
|
|
auto& config_entry = config.label_types[obj_name][name];
|
|
|
|
config_entry = {is_val, type_name, array_size};
|
2021-02-20 11:42:46 -05:00
|
|
|
}
|
|
|
|
}
|
2021-03-27 15:18:59 -04:00
|
|
|
|
2021-06-04 13:43:19 -04:00
|
|
|
auto stack_structures_json = read_json_file_from_config(cfg, "stack_structures_file");
|
|
|
|
for (auto& kv : stack_structures_json.items()) {
|
2021-03-27 15:18:59 -04:00
|
|
|
auto& func_name = kv.key();
|
2021-06-04 13:43:19 -04:00
|
|
|
auto& stack_structures = kv.value();
|
|
|
|
config.stack_structure_hints_by_function[func_name] =
|
|
|
|
parse_stack_structure_hints(stack_structures);
|
2021-03-27 15:18:59 -04:00
|
|
|
}
|
2021-05-11 20:49:54 -04:00
|
|
|
|
|
|
|
auto hacks_json = read_json_file_from_config(cfg, "hacks_file");
|
|
|
|
config.hacks.hint_inline_assembly_functions =
|
|
|
|
hacks_json.at("hint_inline_assembly_functions").get<std::unordered_set<std::string>>();
|
|
|
|
config.hacks.asm_functions_by_name =
|
|
|
|
hacks_json.at("asm_functions_by_name").get<std::unordered_set<std::string>>();
|
|
|
|
config.hacks.pair_functions_by_name =
|
|
|
|
hacks_json.at("pair_functions_by_name").get<std::unordered_set<std::string>>();
|
|
|
|
config.hacks.no_type_analysis_functions_by_name =
|
|
|
|
hacks_json.at("no_type_analysis_functions_by_name").get<std::unordered_set<std::string>>();
|
|
|
|
config.hacks.types_with_bad_inspect_methods =
|
|
|
|
hacks_json.at("types_with_bad_inspect_methods").get<std::unordered_set<std::string>>();
|
2021-05-30 19:57:11 -04:00
|
|
|
config.hacks.reject_cond_to_value = hacks_json.at("aggressively_reject_cond_to_value_rewrite")
|
|
|
|
.get<std::unordered_set<std::string>>();
|
2021-07-05 16:07:07 -04:00
|
|
|
config.hacks.blocks_ending_in_asm_branch_by_func_name =
|
|
|
|
hacks_json.at("blocks_ending_in_asm_branch")
|
|
|
|
.get<std::unordered_map<std::string, std::unordered_set<int>>>();
|
2021-09-07 01:35:03 +01:00
|
|
|
config.hacks.format_ops_with_dynamic_string_by_func_name =
|
|
|
|
hacks_json.at("dynamic_format_arg_counts")
|
|
|
|
.get<std::unordered_map<std::string, std::vector<std::vector<int>>>>();
|
2021-09-11 20:52:35 -04:00
|
|
|
config.hacks.mips2c_functions_by_name =
|
|
|
|
hacks_json.at("mips2c_functions_by_name").get<std::unordered_set<std::string>>();
|
2022-01-28 21:32:03 -05:00
|
|
|
config.hacks.mips2c_jump_table_functions =
|
|
|
|
hacks_json.at("mips2c_jump_table_functions")
|
|
|
|
.get<std::unordered_map<std::string, std::vector<int>>>();
|
2021-05-24 19:52:19 -04:00
|
|
|
|
|
|
|
for (auto& entry : hacks_json.at("cond_with_else_max_lengths")) {
|
|
|
|
auto func_name = entry.at(0).get<std::string>();
|
|
|
|
auto cond_name = entry.at(1).get<std::string>();
|
|
|
|
auto max_len = entry.at(2).get<int>();
|
|
|
|
config.hacks.cond_with_else_len_by_func_name[func_name].max_length_by_start_block[cond_name] =
|
|
|
|
max_len;
|
|
|
|
}
|
2021-07-01 21:38:19 -04:00
|
|
|
|
2021-12-04 12:33:18 -05:00
|
|
|
for (auto& entry : hacks_json.at("missing_textures")) {
|
|
|
|
int tpage = entry.at(1).get<int>();
|
|
|
|
int idx = entry.at(2).get<int>();
|
|
|
|
config.hacks.missing_textures_by_level[entry.at(0).get<std::string>()].emplace_back(tpage, idx);
|
|
|
|
}
|
|
|
|
|
2021-07-01 21:38:19 -04:00
|
|
|
config.bad_format_strings =
|
|
|
|
hacks_json.at("bad_format_strings").get<std::unordered_map<std::string, int>>();
|
2022-01-05 01:32:34 +00:00
|
|
|
|
|
|
|
auto merged = hacks_json.at("expected_merged_objs").get<std::vector<std::string>>();
|
|
|
|
for (const auto& x : merged) {
|
|
|
|
config.merged_objects.insert(x);
|
|
|
|
}
|
|
|
|
|
2022-02-04 23:37:48 +00:00
|
|
|
config.levels_to_extract = inputs_json.at("levels_to_extract").get<std::vector<std::string>>();
|
|
|
|
config.levels_extract = cfg.at("levels_extract").get<bool>();
|
2022-03-10 19:25:01 -05:00
|
|
|
|
2022-05-20 02:30:14 +01:00
|
|
|
auto art_info_json = read_json_file_from_config(cfg, "art_info_file");
|
|
|
|
config.art_groups_by_file =
|
|
|
|
art_info_json.at("files").get<std::unordered_map<std::string, std::string>>();
|
|
|
|
config.art_groups_by_function =
|
|
|
|
art_info_json.at("functions").get<std::unordered_map<std::string, std::string>>();
|
|
|
|
|
2022-05-23 18:53:02 -04:00
|
|
|
auto import_deps = read_json_file_from_config(cfg, "import_deps_file");
|
|
|
|
config.import_deps_by_file =
|
|
|
|
import_deps.get<std::unordered_map<std::string, std::vector<std::string>>>();
|
|
|
|
|
2022-06-22 05:16:34 +01:00
|
|
|
config.write_patches = cfg.at("write_patches").get<bool>();
|
|
|
|
config.apply_patches = cfg.at("apply_patches").get<bool>();
|
|
|
|
const auto& object_patches = cfg.at("object_patches");
|
|
|
|
for (auto& [obj, pch] : object_patches.items()) {
|
|
|
|
ObjectPatchInfo new_pch;
|
|
|
|
new_pch.crc = (u32)std::stoull(pch.at("crc32").get<std::string>(), nullptr, 16);
|
|
|
|
new_pch.target_file = pch.at("in").get<std::string>();
|
|
|
|
new_pch.patch_file = pch.at("out").get<std::string>();
|
|
|
|
config.object_patches.insert({obj, new_pch});
|
|
|
|
}
|
|
|
|
|
2021-05-11 20:49:54 -04:00
|
|
|
return config;
|
2020-10-15 18:59:30 -06:00
|
|
|
}
|
2021-03-27 15:18:59 -04:00
|
|
|
|
2021-09-07 01:35:03 +01:00
|
|
|
} // namespace decompiler
|