From 5a5d88eb1738e6451b76db632074dbbd9a79b999 Mon Sep 17 00:00:00 2001 From: Tyler Wilding Date: Sun, 25 Oct 2020 02:26:35 -0400 Subject: [PATCH] Amalgamate FileIO and remove duplicate Timer header Closes #8 --- common/util/FileUtil.cpp | 43 ++++++++++++++ common/util/FileUtil.h | 5 ++ decompiler/CMakeLists.txt | 3 +- decompiler/ObjectFile/ObjectFileDB.cpp | 17 +++--- decompiler/config.cpp | 1 - decompiler/main.cpp | 14 +++-- decompiler/util/FileIO.cpp | 47 ---------------- decompiler/util/FileIO.h | 15 ----- game/system/SystemThread.h | 2 +- game/system/Timer.h | 77 -------------------------- 10 files changed, 67 insertions(+), 157 deletions(-) delete mode 100644 decompiler/util/FileIO.cpp delete mode 100644 decompiler/util/FileIO.h delete mode 100644 game/system/Timer.h diff --git a/common/util/FileUtil.cpp b/common/util/FileUtil.cpp index f7b1642526..31bfc0b9b0 100644 --- a/common/util/FileUtil.cpp +++ b/common/util/FileUtil.cpp @@ -108,3 +108,46 @@ std::string file_util::read_text_file(const std::string& path) { bool file_util::is_printable_char(char c) { return c >= ' ' && c <= '~'; } + +std::string file_util::combine_path(const std::string& parent, const std::string& child) { + return parent + "/" + child; +} + +std::string file_util::base_name(const std::string& filename) { + size_t pos = 0; + assert(!filename.empty()); + for (size_t i = filename.size() - 1; i-- > 0;) { + if (filename.at(i) == '/') { + pos = (i + 1); + break; + } + } + + return filename.substr(pos); +} + +static bool sInitCrc = false; +static uint32_t crc_table[0x100]; + +void file_util::init_crc() { + for (uint32_t i = 0; i < 0x100; i++) { + uint32_t n = i << 24u; + for (uint32_t j = 0; j < 8; j++) + n = n & 0x80000000 ? (n << 1u) ^ 0x04c11db7u : (n << 1u); + crc_table[i] = n; + } + sInitCrc = true; +} + +uint32_t file_util::crc32(const uint8_t* data, size_t size) { + assert(sInitCrc); + uint32_t crc = 0; + for (size_t i = size; i != 0; i--, data++) { + crc = crc_table[crc >> 24u] ^ ((crc << 8u) | *data); + } + return ~crc; +} + +uint32_t file_util::crc32(const std::vector& data) { + return crc32(data.data(), data.size()); +} diff --git a/common/util/FileUtil.h b/common/util/FileUtil.h index d2a2b68abc..c20237390c 100644 --- a/common/util/FileUtil.h +++ b/common/util/FileUtil.h @@ -10,4 +10,9 @@ void write_text_file(const std::string& file_name, const std::string& text); std::vector read_binary_file(const std::string& filename); std::string read_text_file(const std::string& path); bool is_printable_char(char c); +std::string combine_path(const std::string& parent, const std::string& child); +std::string base_name(const std::string& filename); +void init_crc(); +uint32_t crc32(const uint8_t* data, size_t size); +uint32_t crc32(const std::vector& data); } // namespace file_util diff --git a/decompiler/CMakeLists.txt b/decompiler/CMakeLists.txt index c7156e2421..289c6611a1 100644 --- a/decompiler/CMakeLists.txt +++ b/decompiler/CMakeLists.txt @@ -9,7 +9,6 @@ add_executable(decompiler ObjectFile/LinkedObjectFile.cpp Function/Function.cpp Function/TypeAnalysis.cpp - util/FileIO.cpp config.cpp util/DecompilerTypeSystem.cpp Function/BasicBlocks.cpp @@ -27,4 +26,4 @@ target_link_libraries(decompiler common_util type_system spdlog - fmt) \ No newline at end of file + fmt) diff --git a/decompiler/ObjectFile/ObjectFileDB.cpp b/decompiler/ObjectFile/ObjectFileDB.cpp index a5eef6d7bc..e5865e0ca9 100644 --- a/decompiler/ObjectFile/ObjectFileDB.cpp +++ b/decompiler/ObjectFile/ObjectFileDB.cpp @@ -14,7 +14,6 @@ #include "decompiler/config.h" #include "third-party/minilzo/minilzo.h" #include "common/util/BinaryReader.h" -#include "decompiler/util/FileIO.h" #include "common/util/Timer.h" #include "common/util/FileUtil.h" #include "decompiler/Function/BasicBlocks.h" @@ -247,7 +246,7 @@ void ObjectFileDB::get_objs_from_dgo(const std::string& filename) { BinaryReader reader(dgo_data); auto header = reader.read(); - auto dgo_base_name = base_name(filename); + auto dgo_base_name = file_util::base_name(filename); assert(header.name == dgo_base_name); assert_string_empty_after(header.name, 60); @@ -286,7 +285,7 @@ void ObjectFileDB::add_obj_from_dgo(const std::string& obj_name, stats.total_obj_files++; assert(obj_size > 128); uint16_t version = *(uint16_t*)(obj_data + 8); - auto hash = crc32(obj_data, obj_size); + auto hash = file_util::crc32(obj_data, obj_size); bool duplicated = false; // first, check to see if we already got it... @@ -486,7 +485,7 @@ void ObjectFileDB::write_object_file_words(const std::string& output_dir, bool d for_each_obj([&](ObjectFileData& obj) { if (obj.linked_data.segments == 3 || !dump_v3_only) { auto file_text = obj.linked_data.print_words(); - auto file_name = combine_path(output_dir, obj.to_unique_name() + ".txt"); + auto file_name = file_util::combine_path(output_dir, obj.to_unique_name() + ".txt"); total_bytes += file_text.size(); file_util::write_text_file(file_name, file_text); total_files++; @@ -516,10 +515,11 @@ void ObjectFileDB::write_disassembly(const std::string& output_dir, if (obj.linked_data.has_any_functions() || disassemble_objects_without_functions) { auto file_text = obj.linked_data.print_disassembly(); asm_functions += obj.linked_data.print_asm_function_disassembly(obj.to_unique_name()); - auto file_name = combine_path(output_dir, obj.to_unique_name() + ".func"); + auto file_name = file_util::combine_path(output_dir, obj.to_unique_name() + ".func"); auto json_asm_text = obj.linked_data.to_asm_json(obj.to_unique_name()); - auto json_asm_file_name = combine_path(output_dir, obj.to_unique_name() + "_asm.json"); + auto json_asm_file_name = + file_util::combine_path(output_dir, obj.to_unique_name() + "_asm.json"); file_util::write_text_file(json_asm_file_name, json_asm_text); total_bytes += file_text.size() + json_asm_text.size(); @@ -530,7 +530,8 @@ void ObjectFileDB::write_disassembly(const std::string& output_dir, total_bytes += asm_functions.size(); total_files++; - file_util::write_text_file(combine_path(output_dir, "asm_functions.func"), asm_functions); + file_util::write_text_file(file_util::combine_path(output_dir, "asm_functions.func"), + asm_functions); spdlog::info("Wrote functions dumps:"); spdlog::info(" Total {} files", total_files); @@ -600,7 +601,7 @@ void ObjectFileDB::find_and_write_scripts(const std::string& output_dir) { } }); - auto file_name = combine_path(output_dir, "all_scripts.lisp"); + auto file_name = file_util::combine_path(output_dir, "all_scripts.lisp"); file_util::write_text_file(file_name, all_scripts); spdlog::info("Found scripts:"); diff --git a/decompiler/config.cpp b/decompiler/config.cpp index 1af62c9b11..6887427969 100644 --- a/decompiler/config.cpp +++ b/decompiler/config.cpp @@ -1,6 +1,5 @@ #include "config.h" #include "third-party/json.hpp" -#include "util/FileIO.h" #include "common/util/FileUtil.h" Config gConfig; diff --git a/decompiler/main.cpp b/decompiler/main.cpp index 58dadadc40..bb6e5a70d6 100644 --- a/decompiler/main.cpp +++ b/decompiler/main.cpp @@ -3,7 +3,6 @@ #include #include "ObjectFile/ObjectFileDB.h" #include "config.h" -#include "util/FileIO.h" #include "third-party/spdlog/include/spdlog/spdlog.h" #include "third-party/spdlog/include/spdlog/sinks/basic_file_sink.h" #include "common/util/FileUtil.h" @@ -16,7 +15,7 @@ int main(int argc, char** argv) { spdlog::set_default_logger(lu); spdlog::flush_on(spdlog::level::info); - init_crc(); + file_util::init_crc(); init_opcode_info(); if (argc != 4) { @@ -30,12 +29,14 @@ int main(int argc, char** argv) { std::vector dgos; for (const auto& dgo_name : get_config().dgo_names) { - dgos.push_back(combine_path(in_folder, dgo_name)); + dgos.push_back(file_util::combine_path(in_folder, dgo_name)); } ObjectFileDB db(dgos, get_config().obj_file_name_map_file); - file_util::write_text_file(combine_path(out_folder, "dgo.txt"), db.generate_dgo_listing()); - file_util::write_text_file(combine_path(out_folder, "obj.txt"), db.generate_obj_listing()); + file_util::write_text_file(file_util::combine_path(out_folder, "dgo.txt"), + db.generate_dgo_listing()); + file_util::write_text_file(file_util::combine_path(out_folder, "obj.txt"), + db.generate_obj_listing()); db.process_link_data(); db.find_code(); @@ -58,7 +59,8 @@ int main(int argc, char** argv) { // todo print type summary // printf("%s\n", get_type_info().get_summary().c_str()); - file_util::write_text_file(combine_path(out_folder, "all-syms.gc"), db.dts.dump_symbol_types()); + file_util::write_text_file(file_util::combine_path(out_folder, "all-syms.gc"), + db.dts.dump_symbol_types()); spdlog::info("Disassembly has completed successfully."); return 0; } diff --git a/decompiler/util/FileIO.cpp b/decompiler/util/FileIO.cpp deleted file mode 100644 index c92bb85524..0000000000 --- a/decompiler/util/FileIO.cpp +++ /dev/null @@ -1,47 +0,0 @@ -#include "FileIO.h" -#include -#include -#include - -std::string combine_path(const std::string& parent, const std::string& child) { - return parent + "/" + child; -} - -std::string base_name(const std::string& filename) { - size_t pos = 0; - assert(!filename.empty()); - for (size_t i = filename.size() - 1; i-- > 0;) { - if (filename.at(i) == '/') { - pos = (i + 1); - break; - } - } - - return filename.substr(pos); -} - -static bool sInitCrc = false; -static uint32_t crc_table[0x100]; - -void init_crc() { - for (uint32_t i = 0; i < 0x100; i++) { - uint32_t n = i << 24u; - for (uint32_t j = 0; j < 8; j++) - n = n & 0x80000000 ? (n << 1u) ^ 0x04c11db7u : (n << 1u); - crc_table[i] = n; - } - sInitCrc = true; -} - -uint32_t crc32(const uint8_t* data, size_t size) { - assert(sInitCrc); - uint32_t crc = 0; - for (size_t i = size; i != 0; i--, data++) { - crc = crc_table[crc >> 24u] ^ ((crc << 8u) | *data); - } - return ~crc; -} - -uint32_t crc32(const std::vector& data) { - return crc32(data.data(), data.size()); -} diff --git a/decompiler/util/FileIO.h b/decompiler/util/FileIO.h deleted file mode 100644 index 74354ce4ca..0000000000 --- a/decompiler/util/FileIO.h +++ /dev/null @@ -1,15 +0,0 @@ -#pragma once - -#ifndef JAK_V2_FILEIO_H -#define JAK_V2_FILEIO_H - -#include -#include - -std::string combine_path(const std::string& parent, const std::string& child); -std::string base_name(const std::string& filename); -void init_crc(); -uint32_t crc32(const uint8_t* data, size_t size); -uint32_t crc32(const std::vector& data); - -#endif // JAK_V2_FILEIO_H diff --git a/game/system/SystemThread.h b/game/system/SystemThread.h index a2ab512829..b05958c111 100644 --- a/game/system/SystemThread.h +++ b/game/system/SystemThread.h @@ -15,7 +15,7 @@ #include #include -#include "Timer.h" +#include "common/util/Timer.h" constexpr int MAX_SYSTEM_THREADS = 16; diff --git a/game/system/Timer.h b/game/system/Timer.h deleted file mode 100644 index cc7242bdad..0000000000 --- a/game/system/Timer.h +++ /dev/null @@ -1,77 +0,0 @@ -#pragma once - -#ifndef RUNTIME_TIMER_H -#define RUNTIME_TIMER_H - -#ifdef _WIN32 -#include -#endif - -#include -#include -#include - -class Timer { - public: - explicit Timer() { start(); } - -#ifdef _WIN32 -#define MS_PER_SEC 1000ULL // MS = milliseconds -#define US_PER_MS 1000ULL // US = microseconds -#define HNS_PER_US 10ULL // HNS = hundred-nanoseconds (e.g., 1 hns = 100 ns) -#define NS_PER_US 1000ULL - -#define HNS_PER_SEC (MS_PER_SEC * US_PER_MS * HNS_PER_US) -#define NS_PER_HNS (100ULL) // NS = nanoseconds -#define NS_PER_SEC (MS_PER_SEC * US_PER_MS * NS_PER_US) - int Timer::clock_gettime_monotonic(struct timespec* tv) { - static LARGE_INTEGER ticksPerSec; - LARGE_INTEGER ticks; - double seconds; - - if (!ticksPerSec.QuadPart) { - QueryPerformanceFrequency(&ticksPerSec); - if (!ticksPerSec.QuadPart) { - errno = ENOTSUP; - return -1; - } - } - - QueryPerformanceCounter(&ticks); - - seconds = (double)ticks.QuadPart / (double)ticksPerSec.QuadPart; - tv->tv_sec = (time_t)seconds; - tv->tv_nsec = (long)((ULONGLONG)(seconds * NS_PER_SEC) % NS_PER_SEC); - - return 0; - } -#endif - - void start() { -#ifdef _WIN32 - clock_gettime_monotonic(&_startTime); -#elif __linux__ - clock_gettime(CLOCK_MONOTONIC, &_startTime); -#endif - } - - double getMs() { return (double)getNs() / 1.e6; } - - int64_t getNs() { - struct timespec now; -#ifdef _WIN32 - clock_gettime_monotonic(&now); -#elif __linux__ - clock_gettime(CLOCK_MONOTONIC, &now); -#endif - - return (int64_t)(now.tv_nsec - _startTime.tv_nsec) + - 1000000000 * (now.tv_sec - _startTime.tv_sec); - } - - double getSeconds() { return (double)getNs() / 1.e9; } - - struct timespec _startTime; -}; - -#endif // RUNTIME_TIMER_H