#include "tool.hpp" #include #include #include 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 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(); std::string conv_str = callingConventionToString(func.calling_convention); size_t numParams = 0; if (!func.parameter_types.empty()) { std::string params = func.parameter_types; std::stringstream ss(params); std::string param; while (std::getline(ss, param, ';')) { if (!param.empty()) { numParams++; } } } if (func.parameter_types == "void") numParams = 0; if (conv_str == "stdcall") { output_stream << "HOOK_S" << numParams << "(0x" << func.address << ", " << func.name << ") // " << filename << std::endl; } else { output_stream << "HOOK(0x" << func.address << ", " << func.name << ") // " << filename << std::endl; } spdlog::debug("Added hook: {} {} {} from {}", func.address, func.name, conv_str, 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"); } }); }