123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585 |
- // Copyright (c) rAthena Dev Teams - Licensed under GNU GPL
- // For more information, see LICENCE in the main folder
- #include "yamlupgrade.hpp"
- using namespace rathena::tool_yamlupgrade;
- static bool upgrade_achievement_db(std::string file, const uint32 source_version);
- static bool upgrade_item_db(std::string file, const uint32 source_version);
- static bool upgrade_job_stats(std::string file, const uint32 source_version);
- static bool upgrade_status_db(std::string file, const uint32 source_version);
- static bool upgrade_map_drops_db(std::string file, const uint32 source_version);
- static bool upgrade_enchantgrade_db( std::string file, const uint32 source_version );
- static bool upgrade_item_group_db( std::string file, const uint32 source_version );
- static bool upgrade_laphine_upgrade( std::string file, const uint32 source_version );
- template<typename Func>
- bool process(const std::string &type, uint32 version, const std::vector<std::string> &paths, const std::string &name, Func lambda) {
- for (const std::string &path : paths) {
- const std::string name_ext = name + ".yml";
- const std::string from = path + "/" + name_ext;
- const std::string to = path + "/" + name + "-upgrade.yml";
- inNode.reset();
- if (fileExists(from)) {
- inNode = YAML::LoadFile(from);
- uint32 source_version = getHeaderVersion(inNode);
- if (source_version >= version) {
- continue;
- }
- if (!askConfirmation("Found the file \"%s\", which requires an upgrade.\nDo you want to convert it now? (Y/N)\n", from.c_str())) {
- continue;
- }
- if (fileExists(to)) {
- if (!askConfirmation("The file \"%s\" already exists.\nDo you want to replace it? (Y/N)\n", to.c_str())) {
- continue;
- }
- }
- ShowNotice("Upgrade process has begun.\n");
- std::ofstream outFile;
- body.~Emitter();
- new (&body) YAML::Emitter();
- outFile.open(to);
- if (!outFile.is_open()) {
- ShowError("Can not open file \"%s\" for writing.\n", to.c_str());
- return false;
- }
- prepareHeader(outFile, type, version, name);
- if (inNode["Body"].IsDefined()) {
- prepareBody();
- if (!lambda(path, name_ext, source_version)) {
- outFile.close();
- return false;
- }
- finalizeBody();
- outFile << body.c_str();
- }
- prepareFooter(outFile);
- // Make sure there is an empty line at the end of the file for git
- outFile << "\n";
- outFile.close();
- }
- }
- return true;
- }
- bool YamlUpgradeTool::initialize( int argc, char* argv[] ){
- const std::string path_db = std::string(db_path);
- const std::string path_db_mode = path_db + "/" + DBPATH;
- const std::string path_db_import = path_db + "/" + DBIMPORT;
- // Loads required conversion constants
- if (fileExists(item_db.getDefaultLocation())) {
- item_db.load();
- } else {
- parse_item_constants_txt((path_db_mode + "item_db.txt").c_str());
- parse_item_constants_txt((path_db_import + "item_db.txt").c_str());
- }
- if (fileExists(mob_db.getDefaultLocation())) {
- mob_db.load();
- } else {
- sv_readdb(path_db_mode.c_str(), "mob_db.txt", ',', 31 + 2 * MAX_MVP_DROP + 2 * MAX_MOB_DROP, 31 + 2 * MAX_MVP_DROP + 2 * MAX_MOB_DROP, -1, &parse_mob_constants_txt, false);
- sv_readdb(path_db_import.c_str(), "mob_db.txt", ',', 31 + 2 * MAX_MVP_DROP + 2 * MAX_MOB_DROP, 31 + 2 * MAX_MVP_DROP + 2 * MAX_MOB_DROP, -1, &parse_mob_constants_txt, false);
- }
- if (fileExists(skill_db.getDefaultLocation())) {
- skill_db.load();
- } else {
- sv_readdb(path_db_mode.c_str(), "skill_db.txt", ',', 18, 18, -1, parse_skill_constants_txt, false);
- sv_readdb(path_db_import.c_str(), "skill_db.txt", ',', 18, 18, -1, parse_skill_constants_txt, false);
- }
- // Load constants
- #define export_constant_npc(a) export_constant(a)
- #include <map/script_constants.hpp>
- std::vector<std::string> root_paths = {
- path_db,
- path_db_mode,
- path_db_import
- };
- if (!process("ACHIEVEMENT_DB", 2, root_paths, "achievement_db", [](const std::string &path, const std::string &name_ext, uint32 source_version) -> bool {
- return upgrade_achievement_db(path + name_ext, source_version);
- })) {
- return false;
- }
- if (!process("ITEM_DB", 3, root_paths, "item_db", [](const std::string& path, const std::string& name_ext, uint32 source_version) -> bool {
- return upgrade_item_db(path + name_ext, source_version);
- })) {
- return false;
- }
- if (!process("JOB_STATS", 2, root_paths, "job_stats", [](const std::string& path, const std::string& name_ext, uint32 source_version) -> bool {
- return upgrade_job_stats(path + name_ext, source_version);
- })) {
- return false;
- }
-
- if (!process("STATUS_DB", 3, root_paths, "status", [](const std::string& path, const std::string& name_ext, uint32 source_version) -> bool {
- return upgrade_status_db(path + name_ext, source_version);
- })) {
- return false;
- }
-
- if (!process("MAP_DROP_DB", 2, root_paths, "map_drops", [](const std::string& path, const std::string& name_ext, uint32 source_version) -> bool {
- return upgrade_map_drops_db(path + name_ext, source_version);
- })) {
- return 0;
- }
- if( !process( "ENCHANTGRADE_DB", 3, root_paths, "enchantgrade", []( const std::string& path, const std::string& name_ext, uint32 source_version ) -> bool {
- return upgrade_enchantgrade_db( path + name_ext, source_version );
- } ) ){
- return false;
- }
- if( !process( "ITEM_GROUP_DB", 3, root_paths, "item_group_db", []( const std::string& path, const std::string& name_ext, uint32 source_version ) -> bool {
- return upgrade_item_group_db( path + name_ext, source_version );
- } ) ){
- return false;
- }
- if( !process( "LAPHINE_UPGRADE_DB", 2, root_paths, "laphine_upgrade", []( const std::string& path, const std::string& name_ext, uint32 source_version ) -> bool {
- return upgrade_laphine_upgrade( path + name_ext, source_version );
- } ) ){
- return false;
- }
- return true;
- }
- // Implementation of the upgrade functions
- static bool upgrade_achievement_db(std::string file, const uint32 source_version) {
- size_t entries = 0;
- for (const auto &input : inNode["Body"]) {
- body << YAML::BeginMap;
- body << YAML::Key << "Id" << YAML::Value << input["ID"];
- std::string constant = input["Group"].as<std::string>();
- constant.erase(0, 3); // Remove "AG_"
- if (constant.compare("Chat") == 0) // Chat -> Chatting
- constant.insert(4, "ting");
- else if (constant.compare("Hear") == 0 || constant.compare("See") == 0)
- constant = "Chatting"; // Aegis treats these as general "Talk to NPC" achievements.
- else if (constant.compare("Refine") == 0) { // Refine -> Enchant
- constant.erase(0, 6);
- constant = "Enchant" + constant;
- }
- body << YAML::Key << "Group" << YAML::Value << name2Upper(constant);
- body << YAML::Key << "Name" << YAML::Value << input["Name"];
- if (input["Target"].IsDefined()) {
- body << YAML::Key << "Targets";
- body << YAML::BeginSeq;
- for (const auto &it : input["Target"]) {
- body << YAML::BeginMap;
- body << YAML::Key << "Id" << YAML::Value << it["Id"];
- if (it["MobID"].IsDefined()) {
- uint16 mob_id = it["MobID"].as<uint16>();
- std::string *mob_name = util::umap_find(aegis_mobnames, mob_id);
- if (mob_name == nullptr) {
- ShowWarning("mob_avail reading: Invalid mob-class %hu, Mob not read.\n", mob_id);
- return false;
- }
- body << YAML::Key << "Mob" << YAML::Value << *mob_name;
- }
- if (it["Count"].IsDefined() && it["Count"].as<int32>() > 1)
- body << YAML::Key << "Count" << YAML::Value << it["Count"];
- body << YAML::EndMap;
- }
- body << YAML::EndSeq;
- }
- if (input["Condition"].IsDefined())
- body << YAML::Key << "Condition" << YAML::Value << input["Condition"];
- if (input["Map"].IsDefined())
- body << YAML::Key << "Map" << YAML::Value << input["Map"];
- if (input["Dependent"].IsDefined()) {
- body << YAML::Key << "Dependents";
- body << YAML::BeginMap;
- for (const auto &it : input["Dependent"]) {
- body << YAML::Key << it["Id"] << YAML::Value << true;
- }
- body << YAML::EndMap;
- }
- /**
- * Example usage for adding label at specific version.
- if (source_version < ?) {
- body << YAML::Key << "CustomLabel" << YAML::Value << "Unique";
- }
- */
- if (input["Reward"].IsDefined()) {
- body << YAML::Key << "Rewards";
- body << YAML::BeginMap;
- if (input["Reward"]["ItemID"].IsDefined()) {
- t_itemid item_id = input["Reward"]["ItemID"].as<t_itemid>();
- std::string *item_name = util::umap_find(aegis_itemnames, item_id);
- if (item_name == nullptr) {
- ShowError("Reward item name for item ID %u is not known.\n", item_id);
- return false;
- }
- body << YAML::Key << "Item" << YAML::Value << *item_name;
- }
- if (input["Reward"]["Amount"].IsDefined() && input["Reward"]["Amount"].as<uint16>() > 1)
- body << YAML::Key << "Amount" << YAML::Value << input["Reward"]["Amount"];
- if (input["Reward"]["Script"].IsDefined())
- body << YAML::Key << "Script" << YAML::Value << input["Reward"]["Script"];
- if (input["Reward"]["TitleID"].IsDefined())
- body << YAML::Key << "TitleId" << YAML::Value << input["Reward"]["TitleID"];
- body << YAML::EndMap;
- }
- body << YAML::Key << "Score" << YAML::Value << input["Score"];
- body << YAML::EndMap;
- entries++;
- }
- ShowStatus("Done converting/upgrading '" CL_WHITE "%zu" CL_RESET "' achievements in '" CL_WHITE "%s" CL_RESET "'.\n", entries, file.c_str());
- return true;
- }
- static bool upgrade_item_db(std::string file, const uint32 source_version) {
- size_t entries = 0;
- for( auto input : inNode["Body"] ){
- // If under version 2
- if( source_version < 2 ){
- // Add armor level to all equipments
- if( input["Type"].IsDefined() && input["Type"].as<std::string>() == "Armor" ){
- input["ArmorLevel"] = 1;
- }
- }
- // TODO: this currently converts all scripts using Literal syntax to normal double quote strings
- body << input;
- entries++;
- }
- ShowStatus("Done converting/upgrading '" CL_WHITE "%zu" CL_RESET "' items in '" CL_WHITE "%s" CL_RESET "'.\n", entries, file.c_str());
- return true;
- }
- static bool upgrade_job_stats(std::string file, const uint32 source_version) {
- size_t entries = 0;
- for (auto input : inNode["Body"]) {
- // If under version 2
- if (source_version < 2) {
- // Field name changes
- if (input["HPFactor"].IsDefined()) {
- input["HpFactor"] = input["HPFactor"].as<uint32>();
- input.remove("HPFactor");
- }
- if (input["HpMultiplicator"].IsDefined()) {
- input["HpIncrease"] = input["HpMultiplicator"].as<uint32>();
- input.remove("HpMultiplicator");
- }
- if (input["HPMultiplicator"].IsDefined()) {
- input["HpIncrease"] = input["HPMultiplicator"].as<uint32>();
- input.remove("HPMultiplicator");
- }
- if (input["SpFactor"].IsDefined()) {
- input["SpIncrease"] = input["SpFactor"].as<uint32>();
- input.remove("SpFactor");
- }
- if (input["SPFactor"].IsDefined()) {
- input["SpIncrease"] = input["SPFactor"].as<uint32>();
- input.remove("SPFactor");
- }
- }
- body << input;
- entries++;
- }
- ShowStatus("Done converting/upgrading '" CL_WHITE "%zu" CL_RESET "' job stats in '" CL_WHITE "%s" CL_RESET "'.\n", entries, file.c_str());
- return true;
- }
- static bool upgrade_status_db(std::string file, const uint32 source_version) {
- size_t entries = 0;
- for (auto input : inNode["Body"]) {
- // If under version 3
- if (source_version < 3) {
- // Rename End to EndOnStart
- if (input["End"].IsDefined()) {
- input["EndOnStart"] = input["End"];
- input.remove("End");
- }
- }
- body << input;
- entries++;
- }
- ShowStatus("Done converting/upgrading '" CL_WHITE "%zu" CL_RESET "' statuses in '" CL_WHITE "%s" CL_RESET "'.\n", entries, file.c_str());
- return true;
- }
- static bool upgrade_map_drops_db(std::string file, const uint32 source_version) {
- size_t entries = 0;
- for( auto input : inNode["Body"] ){
- // If under version 2, adjust the rates from n/10000 to n/100000
- if( source_version < 2 ){
- if (input["GlobalDrops"].IsDefined()) {
- for( auto GlobalDrops : input["GlobalDrops"] ){
- if (GlobalDrops["Rate"].IsDefined()) {
- uint32 val = GlobalDrops["Rate"].as<uint32>() * 10;
- GlobalDrops["Rate"] = val;
- }
- }
- }
- if (input["SpecificDrops"].IsDefined()) {
- for( auto SpecificDrops : input["SpecificDrops"] ){
- if (SpecificDrops["Drops"].IsDefined()) {
- for( auto Drops : SpecificDrops["Drops"] ){
- if (Drops["Rate"].IsDefined()) {
- uint32 val = Drops["Rate"].as<uint32>() * 10;
- Drops["Rate"] = val;
- }
- }
- }
- }
- }
- }
- body << input;
- entries++;
- }
- ShowStatus("Done converting/upgrading '" CL_WHITE "%zu" CL_RESET "' rates in '" CL_WHITE "%s" CL_RESET "'.\n", entries, file.c_str());
- return true;
- }
- static bool upgrade_enchantgrade_db( std::string file, const uint32 source_version ){
- size_t entries = 0;
- for( auto input : inNode["Body"] ){
- // If under version 3
- if( source_version < 3 ){
- if( input["Levels"].IsDefined() ){
- for( auto levelNode : input["Levels"] ){
- if( levelNode["Grades"].IsDefined() ){
- for( auto gradeNode : levelNode["Grades"] ){
- // Convert Refine + Chance to a Chances array
- if( gradeNode["Refine"].IsDefined() && !gradeNode["Chance"].IsDefined() ){
- ShowError( "Cannot upgrade automatically, because Refine is specified, but Chance is missing" );
- return false;
- }
- if( gradeNode["Chance"].IsDefined() && !gradeNode["Refine"].IsDefined() ){
- ShowError( "Cannot upgrade automatically, because Chance is specified, but Refine is missing" );
- return false;
- }
- uint16 refine = gradeNode["Refine"].as<uint16>();
- uint16 chance = gradeNode["Chance"].as<uint16>();
- auto chancesNode = gradeNode["Chances"];
- for( int i = refine, j = 0; i <= MAX_REFINE; i++, j++ ){
- auto chanceNode = chancesNode[j];
- chanceNode["Refine"] = i;
- chanceNode["Chance"] = chance;
- }
- // Remove the existing Refine entry
- gradeNode.remove( "Refine" );
- // Remove the existing Chance entry
- gradeNode.remove( "Chance" );
- }
- }
- }
- }
- }
- body << input;
- entries++;
- }
- ShowStatus( "Done converting/upgrading '" CL_WHITE "%zu" CL_RESET "' entries in '" CL_WHITE "%s" CL_RESET "'.\n", entries, file.c_str() );
- return true;
- }
- static bool upgrade_item_group_db( std::string file, const uint32 source_version ){
- size_t entries = 0;
- for( const auto input : inNode["Body"] ){
- // If under version 3
- if( source_version < 3 ){
- body << YAML::BeginMap;
- body << YAML::Key << "Group" << YAML::Value << input["Group"];
- if( input["SubGroups"].IsDefined() ){
- body << YAML::Key << "SubGroups";
- body << YAML::BeginSeq;
- for (const auto &it : input["SubGroups"]) {
- body << YAML::BeginMap;
- if( it["SubGroup"].IsDefined() ){
- body << YAML::Key << "SubGroup" << YAML::Value << it["SubGroup"];
- }
- if( it["List"].IsDefined() )
- body << YAML::Key << "List";{
- body << YAML::BeginSeq;
- uint32 index = 0;
- for( auto ListNode : it["List"] ){
- if( !ListNode["Item"].IsDefined() ){
- ShowError( "Cannot upgrade automatically, Item is missing" );
- return false;
- }
- body << YAML::BeginMap;
- body << YAML::Key << "Index" << YAML::Value << index;
- body << YAML::Key << "Item" << YAML::Value << ListNode["Item"];
- if( ListNode["Rate"].IsDefined() )
- body << YAML::Key << "Rate" << YAML::Value << ListNode["Rate"];
- if( ListNode["Amount"].IsDefined() )
- body << YAML::Key << "Amount" << YAML::Value << ListNode["Amount"];
- if( ListNode["Duration"].IsDefined() )
- body << YAML::Key << "Duration" << YAML::Value << ListNode["Duration"];
- if( ListNode["Announced"].IsDefined() )
- body << YAML::Key << "Announced" << YAML::Value << ListNode["Announced"];
- if( ListNode["UniqueId"].IsDefined() )
- body << YAML::Key << "UniqueId" << YAML::Value << ListNode["UniqueId"];
- if( ListNode["Stacked"].IsDefined() )
- body << YAML::Key << "Stacked" << YAML::Value << ListNode["Stacked"];
- if( ListNode["Named"].IsDefined() )
- body << YAML::Key << "Named" << YAML::Value << ListNode["Named"];
- if( ListNode["Bound"].IsDefined() )
- body << YAML::Key << "Bound" << YAML::Value << ListNode["Bound"];
- if( ListNode["RandomOptionGroup"].IsDefined() )
- body << YAML::Key << "RandomOptionGroup" << YAML::Value << ListNode["RandomOptionGroup"];
- if( ListNode["RefineMinimum"].IsDefined() )
- body << YAML::Key << "RefineMinimum" << YAML::Value << ListNode["RefineMinimum"];
- if( ListNode["RefineMaximum"].IsDefined() )
- body << YAML::Key << "RefineMaximum" << YAML::Value << ListNode["RefineMaximum"];
- if( ListNode["Clear"].IsDefined() )
- body << YAML::Key << "Clear" << YAML::Value << ListNode["Clear"];
- index++;
- body << YAML::EndMap;
- }
- body << YAML::EndSeq;
- }
- if( it["Clear"].IsDefined() )
- body << YAML::Key << "Clear" << YAML::Value << it["Clear"];
- body << YAML::EndMap;
- }
- body << YAML::EndSeq;
- }
- body << YAML::EndMap;
- }
- entries++;
- }
- ShowStatus( "Done converting/upgrading '" CL_WHITE "%zu" CL_RESET "' entries in '" CL_WHITE "%s" CL_RESET "'.\n", entries, file.c_str() );
- return true;
- }
- static bool upgrade_laphine_upgrade( std::string file, const uint32 source_version ){
- size_t entries = 0;
- for( auto input : inNode["Body"] ){
- if( source_version < 2 ){
- if( input["ResultRefine"].IsDefined() ){
- // Convert ResultRefine to a ResultRefine array
- uint16 refine_level = input["ResultRefine"].as<uint16>();
- // Remove the existing Refine entry
- input.remove( "ResultRefine" );
- // Add the ResultRefine array
- auto RatesNode = input["ResultRefine"];
- auto RateNode = RatesNode[0];
- RateNode["Level"] = refine_level;
- }
- // Convert the values between ResultRefineMinimum and ResultRefineMaximum to a ResultRefine array
- if( input["ResultRefineMinimum"].IsDefined() || input["ResultRefineMaximum"].IsDefined() ){
- uint16 refine_level_min = 0, refine_level_max = MAX_REFINE;
- // Save data and remove the existing ResultRefineMinimum/ResultRefineMaximum entry
- if( input["ResultRefineMinimum"].IsDefined() ) {
- refine_level_min = input["ResultRefineMinimum"].as<uint16>();
- input.remove( "ResultRefineMinimum" );
- }
- if( input["ResultRefineMaximum"].IsDefined() ) {
- refine_level_max = input["ResultRefineMaximum"].as<uint16>();
- input.remove( "ResultRefineMaximum" );
- }
- // Remove existing ResultRefine entry (shouldn't happen)
- if( input["ResultRefine"].IsDefined() )
- input.remove( "ResultRefine" );
- // Add the ResultRefine array
- auto RatesNode = input["ResultRefine"];
- for( int i = refine_level_min, j = 0; i <= refine_level_max; i++, j++ ){
- auto RateNode = RatesNode[j];
- RateNode["Level"] = i;
- }
- }
- }
- body << input;
- entries++;
- }
- ShowStatus( "Done converting/upgrading '" CL_WHITE "%zu" CL_RESET "' entries in '" CL_WHITE "%s" CL_RESET "'.\n", entries, file.c_str() );
- return true;
- }
- int main( int argc, char *argv[] ){
- return main_core<YamlUpgradeTool>( argc, argv );
- }
|