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

Amalgamate FileIO and remove duplicate Timer header #96

Merged
merged 1 commit into from
Oct 25, 2020
Merged
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
43 changes: 43 additions & 0 deletions common/util/FileUtil.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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<uint8_t>& data) {
return crc32(data.data(), data.size());
}
5 changes: 5 additions & 0 deletions common/util/FileUtil.h
Original file line number Diff line number Diff line change
Expand Up @@ -10,4 +10,9 @@ void write_text_file(const std::string& file_name, const std::string& text);
std::vector<uint8_t> 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<uint8_t>& data);
} // namespace file_util
3 changes: 1 addition & 2 deletions decompiler/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -27,4 +26,4 @@ target_link_libraries(decompiler
common_util
type_system
spdlog
fmt)
fmt)
17 changes: 9 additions & 8 deletions decompiler/ObjectFile/ObjectFileDB.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down Expand Up @@ -247,7 +246,7 @@ void ObjectFileDB::get_objs_from_dgo(const std::string& filename) {
BinaryReader reader(dgo_data);
auto header = reader.read<DgoHeader>();

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);

Expand Down Expand Up @@ -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...
Expand Down Expand Up @@ -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++;
Expand Down Expand Up @@ -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();
Expand All @@ -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);
Expand Down Expand Up @@ -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:");
Expand Down
1 change: 0 additions & 1 deletion decompiler/config.cpp
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
#include "config.h"
#include "third-party/json.hpp"
#include "util/FileIO.h"
#include "common/util/FileUtil.h"

Config gConfig;
Expand Down
14 changes: 8 additions & 6 deletions decompiler/main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@
#include <vector>
#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"
Expand All @@ -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) {
Expand All @@ -30,12 +29,14 @@ int main(int argc, char** argv) {

std::vector<std::string> 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();
Expand All @@ -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;
}
47 changes: 0 additions & 47 deletions decompiler/util/FileIO.cpp

This file was deleted.

15 changes: 0 additions & 15 deletions decompiler/util/FileIO.h

This file was deleted.

2 changes: 1 addition & 1 deletion game/system/SystemThread.h
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@
#include <thread>
#include <condition_variable>

#include "Timer.h"
#include "common/util/Timer.h"

constexpr int MAX_SYSTEM_THREADS = 16;

Expand Down
77 changes: 0 additions & 77 deletions game/system/Timer.h

This file was deleted.