Hooks tool

This commit is contained in:
Guus Waals 2025-05-31 23:38:21 +08:00
parent 2c305c7a03
commit 76915ede69
4 changed files with 92 additions and 0 deletions

View File

@ -0,0 +1,56 @@
#include "tool.hpp"
#include <CLI11.hpp>
#include <fstream>
#include <filesystem>
static std::string output_file = "hooks.def";
bool generateHooksFile(const std::string &output_filepath) {
Options &options = Options::get();
try {
DatabaseManager db(options.db_path);
std::vector<FunctionInfo> fix_functions = db.getFunctionsByType(FileType::Fix);
std::ofstream output_stream(output_filepath);
if (!output_stream.is_open()) {
spdlog::error("Could not open output file {}", output_filepath);
return false;
}
spdlog::info("Generating hooks file: {}", output_filepath);
for (const auto &func : fix_functions) {
// Extract just the filename from the full path
std::string filename = std::filesystem::path(func.filepath).filename().string();
output_stream << "HOOK(0x" << func.address << ", " << func.name << ") // " << filename << std::endl;
spdlog::debug("Added hook: {} {} from {}", func.address, func.name, filename);
}
output_stream.close();
spdlog::info("Generated {} hooks in {}", fix_functions.size(), output_filepath);
return true;
} catch (const std::exception &e) {
spdlog::error("Error generating hooks file: {}", e.what());
return false;
}
}
void register_cmd_hooks(CLI::App &app) {
auto cmd = app.add_subcommand("hooks", "Generate hooks file for Fix-type functions");
cmd->add_option("-o,--output", output_file, "Output file for hooks")
->default_val("hooks.def");
cmd->final_callback([]() {
spdlog::info("=== Generating hooks file: {} ===", output_file);
if (generateHooksFile(output_file)) {
spdlog::info("Successfully generated hooks file");
} else {
spdlog::error("Failed to generate hooks file");
}
});
}

View File

@ -220,3 +220,36 @@ bool DatabaseManager::checkDuplicateNames() {
return found_duplicates;
}
std::vector<FunctionInfo> DatabaseManager::getFunctionsByType(FileType type) {
std::vector<FunctionInfo> functions;
const char *sql = R"(
SELECT name, address, filepath
FROM Functions
WHERE type = ? AND address != ''
ORDER BY address;
)";
sqlite3_stmt *stmt;
if (sqlite3_prepare_v2(db, sql, -1, &stmt, nullptr) != SQLITE_OK) {
spdlog::error("Failed to prepare getFunctionsByType query: {}", sqlite3_errmsg(db));
return functions;
}
sqlite3_bind_int(stmt, 1, static_cast<int>(type));
while (sqlite3_step(stmt) == SQLITE_ROW) {
FunctionInfo func;
func.name = (const char *)sqlite3_column_text(stmt, 0);
func.address = (const char *)sqlite3_column_text(stmt, 1);
func.filepath = (const char *)sqlite3_column_text(stmt, 2);
func.type = type;
func.is_import = false; // Functions table contains non-imports
functions.push_back(func);
}
sqlite3_finalize(stmt);
return functions;
}

View File

@ -7,6 +7,7 @@
void register_cmd_scan(CLI::App &app);
void register_cmd_dump(CLI::App &app);
void register_cmd_verify(CLI::App &app);
void register_cmd_hooks(CLI::App &app);
int main(int argc, char *argv[]) {
// Initialize spdlog
@ -39,6 +40,7 @@ int main(int argc, char *argv[]) {
register_cmd_scan(app);
register_cmd_dump(app);
register_cmd_verify(app);
register_cmd_hooks(app);
CLI11_PARSE(app, argc, argv);
return 0;

View File

@ -68,6 +68,7 @@ public:
void rollbackTransaction();
bool checkDuplicateAddresses();
bool checkDuplicateNames();
std::vector<FunctionInfo> getFunctionsByType(FileType type);
};
// File processing functions