diff --git a/client_files/export/main.cpp b/client_files/export/main.cpp index 34e137726..e90e6c76c 100644 --- a/client_files/export/main.cpp +++ b/client_files/export/main.cpp @@ -29,7 +29,10 @@ #include "../../common/content/world_content_service.h" #include "../../common/zone_store.h" #include "../../common/path_manager.h" +#include "../../common/repositories/base_data_repository.h" +#include "../../common/repositories/db_str_repository.h" #include "../../common/repositories/skill_caps_repository.h" +#include "../../common/repositories/spells_new_repository.h" #include "../../common/file.h" #include "../../common/events/player_event_logs.h" #include "../../common/skill_caps.h" @@ -99,25 +102,22 @@ int main(int argc, char **argv) ->LoadLogDatabaseSettings() ->StartFileLogs(); - std::string arg_1; + std::string export_type; if (argv[1]) { - arg_1 = argv[1]; + export_type = argv[1]; } - if (arg_1 == "spells") { + if (Strings::EqualFold(export_type, "spells")) { ExportSpells(&content_db); return 0; - } - if (arg_1 == "skills") { + } else if (Strings::EqualFold(export_type, "skills")) { ExportSkillCaps(&content_db); return 0; - } - if (arg_1 == "basedata") { + } else if (Strings::EqualFold(export_type, "basedata") || Strings::EqualFold(export_type, "base_data")) { ExportBaseData(&content_db); return 0; - } - if (arg_1 == "dbstring") { + } else if (Strings::EqualFold(export_type, "dbstr") || Strings::EqualFold(export_type, "dbstring")) { ExportDBStrings(&database); return 0; } @@ -132,180 +132,79 @@ int main(int argc, char **argv) return 0; } -void ExportSpells(SharedDatabase *db) +void ExportSpells(SharedDatabase* db) { - LogInfo("Exporting Spells"); - - std::string file = fmt::format("{}/export/spells_us.txt", path.GetServerPath()); - FILE *f = fopen(file.c_str(), "w"); - if (!f) { + std::ofstream file(fmt::format("{}/export/spells_us.txt", path.GetServerPath())); + if (!file || !file.is_open()) { LogError("Unable to open export/spells_us.txt to write, skipping."); return; } - const std::string query = "SELECT * FROM spells_new ORDER BY id"; - auto results = db->QueryDatabase(query); + const auto& lines = SpellsNewRepository::GetSpellFileLines(*db); - if (results.Success()) { - for (auto row = results.begin(); row != results.end(); ++row) { - std::string line; - unsigned int fields = results.ColumnCount(); - for (unsigned int i = 0; i < fields; ++i) { - if (i != 0) { - line.push_back('^'); - } + const std::string& file_string = Strings::Implode("\n", lines); - if (row[i] != nullptr) { - line += row[i]; - } - } + file << file_string; - fprintf(f, "%s\n", line.c_str()); - } - } - else { - } + file.close(); - fclose(f); -} - -bool SkillUsable(SharedDatabase* db, int skill_id, int class_id) -{ - const auto& l = SkillCapsRepository::GetWhere( - *db, - fmt::format( - "`class_id` = {} AND `skill_id` = {} ORDER BY `cap` DESC LIMIT 1", - class_id, - skill_id - ) - ); - - return !l.empty(); -} - -uint32 GetSkill(SharedDatabase* db, int skill_id, int class_id, int level) -{ - const auto& l = SkillCapsRepository::GetWhere( - *db, - fmt::format( - "`class_id` = {} AND `skill_id` = {} AND `level` = {}", - class_id, - skill_id, - level - ) - ); - - if (l.empty()) { - return 0; - } - - auto e = l.front(); - - return e.cap; + LogInfo("Exported [{}] Spell{}", lines.size(), lines.size() != 1 ? "s" : ""); } void ExportSkillCaps(SharedDatabase* db) { - LogInfo("Exporting Skill Caps"); - std::ofstream file(fmt::format("{}/export/SkillCaps.txt", path.GetServerPath())); if (!file || !file.is_open()) { LogError("Unable to open export/SkillCaps.txt to write, skipping."); return; } - for (uint8 class_id = Class::Warrior; class_id <= Class::Berserker; class_id++) { - for (uint8 skill_id = EQ::skills::Skill1HBlunt; skill_id <= EQ::skills::Skill2HPiercing; skill_id++) { - if (SkillUsable(db, skill_id, class_id)) { - uint32 previous_cap = 0; + const auto& lines = SkillCapsRepository::GetSkillCapFileLines(*db); - for ( - uint8 level = 1; - level <= SkillCaps::GetSkillCapMaxLevel(class_id, static_cast(skill_id)); - level++ - ) { - uint32 cap = GetSkill(db, skill_id, class_id, level); - if (cap < previous_cap) { - cap = previous_cap; - } + const std::string& file_string = Strings::Implode("\n", lines); - file << fmt::format("{}^{}^{}^{}^0", class_id, skill_id, level, cap) << std::endl; - - previous_cap = cap; - } - } - } - } + file << file_string; file.close(); + + LogInfo("Exported [{}] Skill Cap{}", lines.size(), lines.size() != 1 ? "s" : ""); } void ExportBaseData(SharedDatabase *db) { - LogInfo("Exporting Base Data"); - - std::string file = fmt::format("{}/export/BaseData.txt", path.GetServerPath()); - FILE *f = fopen(file.c_str(), "w"); - if (!f) { + std::ofstream file(fmt::format("{}/export/BaseData.txt", path.GetServerPath())); + if (!file || !file.is_open()) { LogError("Unable to open export/BaseData.txt to write, skipping."); return; } - const std::string query = "SELECT * FROM base_data ORDER BY level, class"; - auto results = db->QueryDatabase(query); - if (results.Success()) { - for (auto row = results.begin(); row != results.end(); ++row) { - std::string line; - unsigned int fields = results.ColumnCount(); - for (unsigned int rowIndex = 0; rowIndex < fields; ++rowIndex) { - if (rowIndex != 0) { - line.push_back('^'); - } + const auto& lines = BaseDataRepository::GetBaseDataFileLines(*db); - if (row[rowIndex] != nullptr) { - line += row[rowIndex]; - } - } + const std::string& file_string = Strings::Implode("\n", lines); - fprintf(f, "%s\n", line.c_str()); - } - } + file << file_string; - fclose(f); + file.close(); + + LogInfo("Exported [{}] Base Data Entr{}", lines.size(), lines.size() != 1 ? "ies" : "y"); } void ExportDBStrings(SharedDatabase *db) { - LogInfo("Exporting DB Strings"); - - std::string file = fmt::format("{}/export/dbstr_us.txt", path.GetServerPath()); - FILE *f = fopen(file.c_str(), "w"); - if (!f) { + std::ofstream file(fmt::format("{}/export/dbstr_us.txt", path.GetServerPath())); + if (!file || !file.is_open()) { LogError("Unable to open export/dbstr_us.txt to write, skipping."); return; } - fprintf(f, "Major^Minor^String(New)\n"); - const std::string query = "SELECT * FROM db_str ORDER BY id, type"; - auto results = db->QueryDatabase(query); - if (results.Success()) { - for (auto row = results.begin(); row != results.end(); ++row) { - std::string line; - unsigned int fields = results.ColumnCount(); - for (unsigned int rowIndex = 0; rowIndex < fields; ++rowIndex) { - if (rowIndex != 0) { - line.push_back('^'); - } + const auto& lines = DbStrRepository::GetDBStrFileLines(*db); - if (row[rowIndex] != nullptr) { - line += row[rowIndex]; - } - } + const std::string& file_string = Strings::Implode("\n", lines); - fprintf(f, "%s\n", line.c_str()); - } - } + file << file_string; - fclose(f); + file.close(); + + LogInfo("Exported [{}] Database String{}", lines.size(), lines.size() != 1 ? "s" : ""); } diff --git a/common/repositories/base_data_repository.h b/common/repositories/base_data_repository.h index 85e696b04..fc9cc584b 100644 --- a/common/repositories/base_data_repository.h +++ b/common/repositories/base_data_repository.h @@ -44,7 +44,24 @@ public: */ // Custom extended repository methods here + static std::vector GetBaseDataFileLines(Database& db) + { + std::vector lines; + auto results = db.QueryDatabase( + fmt::format( + "SELECT CONCAT_WS('^', {}) FROM {} ORDER BY `level`, `class` ASC", + ColumnsRaw(), + TableName() + ) + ); + + for (auto row : results) { + lines.emplace_back(row[0]); + } + + return lines; + } }; #endif //EQEMU_BASE_DATA_REPOSITORY_H diff --git a/common/repositories/db_str_repository.h b/common/repositories/db_str_repository.h index 1ea5550ac..9eeb21587 100644 --- a/common/repositories/db_str_repository.h +++ b/common/repositories/db_str_repository.h @@ -44,7 +44,24 @@ public: */ // Custom extended repository methods here + static std::vector GetDBStrFileLines(Database& db) + { + std::vector lines; + auto results = db.QueryDatabase( + fmt::format( + "SELECT CONCAT(CONCAT_WS('^', {}), '^0') FROM {} ORDER BY `id`, `type` ASC", + ColumnsRaw(), + TableName() + ) + ); + + for (auto row : results) { + lines.emplace_back(row[0]); + } + + return lines; + } }; #endif //EQEMU_DB_STR_REPOSITORY_H diff --git a/common/repositories/skill_caps_repository.h b/common/repositories/skill_caps_repository.h index 6388bacc2..05be76b0b 100644 --- a/common/repositories/skill_caps_repository.h +++ b/common/repositories/skill_caps_repository.h @@ -44,7 +44,23 @@ public: */ // Custom extended repository methods here + static std::vector GetSkillCapFileLines(Database& db) + { + std::vector lines; + auto results = db.QueryDatabase( + fmt::format( + "SELECT CONCAT_WS('^', `class_id`, `skill_id`, `level`, `cap`, `class_`) FROM {} ORDER BY `class_id`, `skill_id`, `level` ASC", + TableName() + ) + ); + + for (auto row : results) { + lines.emplace_back(row[0]); + } + + return lines; + } }; #endif //EQEMU_SKILL_CAPS_REPOSITORY_H diff --git a/common/repositories/spells_new_repository.h b/common/repositories/spells_new_repository.h index cafbad33f..0e25e9c6e 100644 --- a/common/repositories/spells_new_repository.h +++ b/common/repositories/spells_new_repository.h @@ -44,7 +44,25 @@ public: */ // Custom extended repository methods here + static std::vector GetSpellFileLines(Database& db) + { + std::vector lines; + auto results = db.QueryDatabase( + fmt::format( + "SELECT CONCAT_WS('^', {}) FROM {} ORDER BY {} ASC", + ColumnsRaw(), + TableName(), + PrimaryKey() + ) + ); + + for (auto row : results) { + lines.emplace_back(row[0]); + } + + return lines; + } }; #endif //EQEMU_SPELLS_NEW_REPOSITORY_H