diff --git a/common/util/json_util.cpp b/common/util/json_util.cpp index 08661fb076..a1edd3a499 100644 --- a/common/util/json_util.cpp +++ b/common/util/json_util.cpp @@ -1,3 +1,4 @@ +#include "common/log/log.h" #include "common/util/assert.h" #include "json_util.h" @@ -75,8 +76,17 @@ std::string strip_cpp_style_comments(const std::string& input) { * Parse JSON file with comments stripped. Unlike the default comment stripping feature * of nlohmann::json, this allows you to have multiple comments in a row! */ -nlohmann::json parse_commented_json(const std::string& input) { - return nlohmann::json::parse(strip_cpp_style_comments(input)); +nlohmann::json parse_commented_json(const std::string& input, const std::string& source_name) { + try { + return nlohmann::json::parse(strip_cpp_style_comments(input)); + } catch (const std::exception& e) { + if (source_name.empty()) { + lg::error("Error parsing JSON file: {}", e.what()); + } else { + lg::error("Error parsing JSON in file {}: {}", source_name, e.what()); + } + throw; + } } /*! diff --git a/common/util/json_util.h b/common/util/json_util.h index d068e93140..9e3a21f6dd 100644 --- a/common/util/json_util.h +++ b/common/util/json_util.h @@ -5,5 +5,5 @@ #include "third-party/json.hpp" std::string strip_cpp_style_comments(const std::string& input); -nlohmann::json parse_commented_json(const std::string& input); +nlohmann::json parse_commented_json(const std::string& input, const std::string& source_name); Range parse_json_optional_integer_range(const nlohmann::json& json); diff --git a/decompiler/IR2/AtomicOpForm.cpp b/decompiler/IR2/AtomicOpForm.cpp index 53c8d62128..ccf74204d0 100644 --- a/decompiler/IR2/AtomicOpForm.cpp +++ b/decompiler/IR2/AtomicOpForm.cpp @@ -645,6 +645,18 @@ Form* LoadVarOp::get_load_src(FormPool& pool, const Env& env) const { } } } + + if (m_kind == Kind::FLOAT && m_size == 4) { + assert((label.offset % 4) == 0); + const auto& words = env.file->words_by_seg.at(label.target_segment); + if ((int)words.size() > label.offset / 4) { + auto word = words.at(label.offset / 4); + assert(word.kind == LinkedWord::PLAIN_DATA); + float value; + memcpy(&value, &word.data, 4); + return pool.alloc_single_element_form(nullptr, value); + } + } } auto source = pool.alloc_single_element_form(nullptr, m_src, m_my_idx); diff --git a/decompiler/ObjectFile/ObjectFileDB.cpp b/decompiler/ObjectFile/ObjectFileDB.cpp index 98e55a51a6..3e41e6e933 100644 --- a/decompiler/ObjectFile/ObjectFileDB.cpp +++ b/decompiler/ObjectFile/ObjectFileDB.cpp @@ -163,7 +163,7 @@ ObjectFileDB::ObjectFileDB(const std::vector& _dgos, } void ObjectFileDB::load_map_file(const std::string& map_data) { - auto j = parse_commented_json(map_data); + auto j = parse_commented_json(map_data, "ObjectFileDB Map File"); for (auto& x : j) { auto mapped_name = x[0].get(); diff --git a/decompiler/config.cpp b/decompiler/config.cpp index 4485ea7b3e..38b4750399 100644 --- a/decompiler/config.cpp +++ b/decompiler/config.cpp @@ -15,7 +15,7 @@ namespace { nlohmann::json read_json_file_from_config(const nlohmann::json& cfg, const std::string& file_key) { auto file_name = cfg.at(file_key).get(); auto file_txt = file_util::read_text_file(file_util::get_file_path({file_name})); - return parse_commented_json(file_txt); + return parse_commented_json(file_txt, file_name); } } // namespace @@ -25,7 +25,7 @@ nlohmann::json read_json_file_from_config(const nlohmann::json& cfg, const std:: Config read_config_file(const std::string& path_to_config_file) { Config config; auto config_str = file_util::read_text_file(path_to_config_file); - auto cfg = parse_commented_json(config_str); + auto cfg = parse_commented_json(config_str, path_to_config_file); config.game_version = cfg.at("game_version").get(); diff --git a/decompiler/config/jak1_ntsc_black_label/label_types.jsonc b/decompiler/config/jak1_ntsc_black_label/label_types.jsonc index da438a1b1f..09c72893b7 100644 --- a/decompiler/config/jak1_ntsc_black_label/label_types.jsonc +++ b/decompiler/config/jak1_ntsc_black_label/label_types.jsonc @@ -1,15 +1,11 @@ { "gcommon": [ - ["L345", "float", true], ["L346", "uint64", true] ], "gkernel": [ ["L345", "_auto_", true], ["L344", "_auto_", true], - ["L346", "float", true], - ["L347", "float", true], - ["L348", "float", true], ["L289", "_auto_", true], ["L282", "_auto_", true], ["L122", "_lambda_", true], @@ -23,9 +19,7 @@ "gstring": [["L170", "float", true]], "math": [ - ["L41", "float", true], - ["L34", "float", true], - ["L35", "float", true] + ["L41", "float", true] ], "vector-h": [ @@ -43,8 +37,6 @@ ["L2", "_auto_", true] ], - "matrix": [["L60", "float", true]], - "transform": [["L11", "float", true]], "quaternion-h": [["L1", "quaternion", true]], "quaternion": [ @@ -54,62 +46,9 @@ ["L70", "vector", true], ["L71", "vector", true], ["L72", "vector", true], - ["L77", "float", true], - ["L78", "float", true], - ["L79", "float", true], - ["L80", "float", true], - ["L81", "float", true], - ["L82", "float", true], - ["L93", "float", true], - ["L94", "float", true] - ], - - "euler": [["L18", "float", true]], - - "geometry": [ - ["L115", "float", true], - ["L116", "float", true], - ["L117", "float", true], - ["L118", "float", true], - ["L119", "float", true], - ["L120", "float", true], - ["L121", "float", true], - ["L122", "float", true], - ["L123", "float", true], - ["L124", "float", true], - ["L125", "float", true], - ["L126", "float", true], - ["L127", "float", true], - ["L128", "float", true] - ], - - "trigonometry": [ - ["L143", "float", true], - ["L144", "float", true], - ["L145", "float", true], - ["L137", "float", true], - ["L106", "float", true], - ["L134", "float", true], - ["L112", "float", true], - ["L114", "float", true], - ["L135", "float", true], - ["L121", "float", true], - ["L150", "float", true], - ["L147", "float", true], - ["L149", "float", true], - ["L107", "float", true], - ["L129", "float", true], - ["L152", "float", true], - ["L109", "float", true], - ["L138", "float", true], - ["L127", "float", true], - ["L128", "float", true], - ["L110", "float", true], - ["L136", "float", true] + ["L81", "float", true] ], - "timer": [["L20", "float", true]], - "dma": [["L56", "uint64", true]], "dma-bucket": [["L10", "uint64", true]], @@ -117,10 +56,7 @@ "video-h": [["L1", "video-parms", true]], "pad": [ - ["L44", "float", true], - ["L42", "float", true], - ["L43", "float", true], - ["L41", "float", true] + ["L44", "float", true] ], "dma-disasm": [["L148", "(array vif-disasm-element)", true]], @@ -130,8 +66,6 @@ "display-h": [["L6", "rgba", true]], - "loader-h": [["L10", "float", true]], - "level-h": [["L3", "level-group", true]], "level-info": [ @@ -166,63 +100,15 @@ "level-h": [["L3", "_auto_", true]], - "math-camera": [ - ["L51", "float", true], - ["L52", "float", true], - ["L53", "float", true], - ["L55", "float", true], - ["L56", "float", true], - ["L38", "float", true], - ["L37", "float", true], - ["L54", "float", true], - ["L50", "float", true], - ["L49", "float", true], - ["L27", "float", true], - ["L35", "float", true], - ["L34", "float", true], - ["L28", "float", true], - ["L26", "float", true], - ["L34", "float", true], - ["L33", "float", true], - ["L30", "float", true], - ["L44", "float", true], - ["L45", "float", true], - ["L32", "float", true], - ["L31", "float", true], - ["L47", "float", true], - ["L48", "float", true], - ["L46", "float", true], - ["L36", "float", true], - ["L43", "float", true], - ["L42", "float", true], - ["L40", "float", true], - ["L41", "float", true], - ["L39", "float", true], - ["L59", "float", true] - ], - "font-h": [ ["L18", "matrix", true], - ["L19", "float", true], ["L17", "font-work", true] ], "text-h": [["L2", "_auto_", true]], "display": [ - ["L47", "float", true], - ["L42", "float", true], - ["L43", "float", true], - ["L44", "float", true], - ["L45", "float", true], - ["L48", "float", true], - ["L46", "float", true], - ["L50", "float", true], - ["L49", "float", true], ["L87", "float", true], - ["L77", "float", true], - ["L78", "float", true], - ["L79", "float", true], ["L76", "(pointer uint32)", true, 2], ["L51", "uint64", true], ["L52", "uint64", true], @@ -296,29 +182,9 @@ ], "ocean-frames": [["L1", "(pointer uint32)", true, 16384]], - - "sky-h": [ - ["L73", "float", true], - ["L71", "float", true], - ["L72", "float", true] - ], - - "mood-h": [["L3", "float", true]], "time-of-day-h": [["L2", "time-of-day-context", true]], - "merc-h": [ - ["L4", "float", true], - ["L5", "float", true], - ["L6", "float", true], - ["L7", "float", true] - ], - - "shadow-cpu-h": [ - ["L9", "float", true], - ["L10", "float", true] - ], - "game-info-h": [ ["L4", "game-bank", true], ["L3", "game-info", true] @@ -327,13 +193,10 @@ "wind-h": [["L3", "_auto_", true]], "dynamics-h": [ - ["L7", "float", true], ["L6", "_auto_", true] ], "surface-h": [ - ["L72", "float", true], - ["L71", "float", true], ["L68", "_auto_", true], ["L67", "_auto_", true], ["L66", "_auto_", true], @@ -376,29 +239,15 @@ "pat-h": [["L1", "(inline-array pat-mode-info)", true, 4]], "fact-h": [ - ["L35", "_auto_", true], - ["L36", "float", true] + ["L35", "_auto_", true] ], "sync-info": [ ["L78", "float", true], - ["L79", "float", true], - ["L80", "float", true], - ["L81", "float", true], - ["L82", "float", true], - ["L83", "float", true], ["L84", "float", true], - ["L85", "float", true], ["L86", "float", true] ], - "smush-control-h": [ - ["L20", "float", true], - ["L18", "float", true], - ["L19", "float", true], - ["L17", "float", true] - ], - "collide-shape-h": [ ["L27", "float", true], ["L25", "_auto_", true], @@ -410,8 +259,7 @@ "vector": [ ["L114", "float", true], ["L113", "float", true], - ["L112", "float", true], - ["L111", "float", true] + ["L112", "float", true] ], "texture": [ @@ -425,19 +273,7 @@ ["L349", "_auto_", true], ["L369", "uint64", true], ["L373", "uint64", true], - ["L371", "uint64", true], - ["L364", "float", true], - ["L360", "float", true], - ["L362", "float", true], - ["L359", "float", true], - ["L358", "float", true], - ["L361", "float", true] - ], - - "lights": [ - ["L8", "float", true], - ["L9", "float", true], - ["L10", "float", true] + ["L371", "uint64", true] ], "hud-h": [ diff --git a/test/decompiler/FormRegressionTest.cpp b/test/decompiler/FormRegressionTest.cpp index 6d0f0d756d..cb83f70c87 100644 --- a/test/decompiler/FormRegressionTest.cpp +++ b/test/decompiler/FormRegressionTest.cpp @@ -80,7 +80,7 @@ parse_var_json(const std::string& str) { std::vector args; std::unordered_map var_overrides; - auto j = parse_commented_json(str); + auto j = parse_commented_json(str, "Regression Test variable map"); auto arg = j.find("args"); if (arg != j.end()) { diff --git a/test/offline/offline_test_main.cpp b/test/offline/offline_test_main.cpp index 71a0d499e4..f7f37ff39a 100644 --- a/test/offline/offline_test_main.cpp +++ b/test/offline/offline_test_main.cpp @@ -168,7 +168,8 @@ int main(int argc, char** argv) { } // use the all_objs.json file to place them in the correct build order auto j = parse_commented_json( - file_util::read_text_file(file_util::get_file_path({"goal_src", "build", "all_objs.json"}))); + file_util::read_text_file(file_util::get_file_path({"goal_src", "build", "all_objs.json"})), + "all_objs.json"); for (auto& x : j) { auto mapped_name = x[0].get(); for (auto& p : reference_files_rough_order) {