소스 검색

Dat_id input before opening file, cmd enter improvement

Ivan Arkhipov 5 년 전
부모
커밋
d907312f54
2개의 변경된 파일90개의 추가작업 그리고 37개의 파일을 삭제
  1. 41 16
      src/Examples/extractor_example.cpp
  2. 49 21
      src/Examples/patcher_example.cpp

+ 41 - 16
src/Examples/extractor_example.cpp

@@ -12,7 +12,6 @@
 
 #include "LotroDat.h"
 using namespace LOTRO_DAT;
-using namespace std;
 
 // Change these variables to true if you want export category to files.
 bool exportTextsToFiles = false;
@@ -80,23 +79,35 @@ void DatStatusChangedHandler(DatStatus::ProgressInfo info) {
 
 int main() {
     std::cout.precision(1);
-    std::cout << fixed;
+    std::cout << std::fixed;
     std::cout << "Gi1dor's LotRO .dat extractor ver. " << LOTRO_DAT_VERSION << std::endl;
 
     std::cout << "Hello! I'm a basic shell version of .dat file extractor. I can open .dat file directly, "
             "if you write path to it (with name of file) in file \"dat_file_path.txt\"\n";
     DatFile file;
 
-    ifstream in("dat_file_path.txt");
+    std::ifstream in("dat_file_path.txt");
     if (!in.fail()) {
         std::string filename;
-        getline(in, filename);
-        std::cout << "Using .dat file from dat_file_path.txt...\n";
-        std::cout << "Opening file " << filename << std::endl;
+        std::getline(in, filename);
+
+        std::string file_id_str;
+        std::getline(in, file_id_str);
+        int file_id = 0;
+        try {
+            file_id = stoi(file_id_str);
+        } catch (std::invalid_argument) {
+            file_id = -1;
+            std::cout << "Invalid file_id on second line of file dat_file_path.txt!\n\n";
+        }
 
-        auto operation = file.Initialise(filename, 0);
-        if (operation.result == false) {
-            std::cout << "Dat initialisation failed. Error message: " << operation.msg << "\n";
+        if (file_id != -1) {
+            std::cout << "Using .dat file from dat_file_path.txt...\n";
+            std::cout << "Opening file " << filename << " with id = " << file_id << std::endl;
+            auto operation = file.Initialise(filename, file_id);
+            if (operation.result == false) {
+                std::cout << "Dat initialisation failed. Error message: " << operation.msg << "\n";
+            }
         }
     }
 
@@ -106,9 +117,20 @@ int main() {
         std::string filename;
         std::getline(std::cin, filename);
 
+        std::cout << "Enter file id: ";
+        std::string file_id_str;
+        std::getline(std::cin, file_id_str);
+        int file_id = 0;
+        try {
+            file_id = stoi(file_id_str);
+        } catch (std::invalid_argument) {
+            std::cout << "Invalid command entered!\n\n";
+            continue;
+        }
+
         std::cout << "Opening file " << filename << std::endl;
 
-        auto operation = file.Initialise(filename, 0);
+        auto operation = file.Initialise(filename, file_id);
         if (operation.result == false) {
             std::cout << "Dat initialisation failed. Error message: " << operation.msg << "\nTo try again enter path to .dat file\n";
         }
@@ -119,17 +141,20 @@ int main() {
     std::cout << "Gathering file information...\n";
     file.GatherInformation("dat_info.log");
 
-    int cmd = 0;
     while (true) {
         std::cout << "Please, choose, what should I do. I can extract .dat file to files (press 1), "
                     "open another .dat file (press 2), or configure, what should I extract. Choose, what to do or exit (press -1)\n";
 
-        if (cmd != 3) {
-            std::cout << "Enter number of command (1-3): ";
-            std::cin >> cmd;
+        std::cout << "Enter number of command (1-3): ";
+        std::string command;
+        std::getline(std::cin, command);
 
-            std::string tmp;
-            std::getline(std::cin, tmp);
+        int cmd = -10;
+        try {
+            cmd = stoi(command);
+        } catch (std::invalid_argument) {
+            std::cout << "Invalid command entered!\n\n";
+            continue;
         }
 
         if (cmd == -1) {

+ 49 - 21
src/Examples/patcher_example.cpp

@@ -12,7 +12,6 @@
 
 #include "LotroDat.h"
 using namespace LOTRO_DAT;
-using namespace std;
 
 void DatStatusChangedHandler(DatStatus::ProgressInfo info) {
     if (info.status == DatStatus::DAT_STATUS::E_FREE) {
@@ -63,7 +62,7 @@ void DatStatusChangedHandler(DatStatus::ProgressInfo info) {
 
 int main() {
     std::cout.precision(1);
-    std::cout << fixed;
+    std::cout << std::fixed;
     std::cout << "Gi1dor's LotRO .dat patcher ver. " << LOTRO_DAT_VERSION << std::endl;
     freopen("patcher_errors.log", "w", stderr);
 
@@ -77,16 +76,29 @@ int main() {
             "if you write path to it (with name of file) in file \"dat_file_path.txt\"\n";
     DatFile file;
 
-    ifstream in("dat_file_path.txt");
+    std::ifstream in("dat_file_path.txt");
     if (!in.fail()) {
         std::string filename;
-        getline(in, filename);
+        std::getline(in, filename);
+
+        std::string file_id_str;
+        std::getline(in, file_id_str);
+        int file_id = 0;
+        try {
+            file_id = stoi(file_id_str);
+        } catch (std::invalid_argument) {
+            file_id = -1;
+            std::cout << "Invalid file_id on second line of file dat_file_path.txt!\n\n";
+        }
 
-        std::cout << "Using .dat file from dat_file_path.txt...\n";
-        std::cout << "Opening file " << filename << std::endl;
-        auto operation = file.Initialise(filename, 0);
-        if (operation.result == ERROR)
-            std::cout << "Cannot initialise dat file " << filename << " \n";
+        if (file_id != -1) {
+            std::cout << "Using .dat file from dat_file_path.txt...\n";
+            std::cout << "Opening file " << filename << " with id = " << file_id << std::endl;
+            auto operation = file.Initialise(filename, file_id);
+            if (operation.result == false) {
+                std::cout << "Dat initialisation failed. Error message: " << operation.msg << "\n";
+            }
+        }
     }
 
     while (!file.Initialized()) {
@@ -95,11 +107,23 @@ int main() {
         std::string filename;
         std::getline(std::cin, filename);
 
+        std::cout << "Enter file id: ";
+        std::string file_id_str;
+        std::getline(std::cin, file_id_str);
+        int file_id = 0;
+        try {
+            file_id = stoi(file_id_str);
+        } catch (std::invalid_argument) {
+            std::cout << "Invalid command entered!\n\n";
+            continue;
+        }
+
         std::cout << "Opening file " << filename << std::endl;
 
-        auto operation = file.Initialise(filename, 0);
-        if (operation.result == ERROR)
-            std::cout << "Cannot initialise dat file " << filename << ", please, try again\n";
+        auto operation = file.Initialise(filename, file_id);
+        if (operation.result == false) {
+            std::cout << "Dat initialisation failed. Error message: " << operation.msg << "\nTo try again enter path to .dat file\n";
+        }
     }
 
     std::cout << "Great! File initialised successfully!\n";
@@ -111,12 +135,16 @@ int main() {
                 "print disabled categories (enter 6), create backup (enter 7), remove backup (enter 8), "
                 "restore .dat file from backup (enter 9), check if backup exists (enter 10) or exit (enter -1)\n";
 
-        int cmd = 0;
         std::cout << "Enter number of command (1-10): ";
-        std::cin >> cmd;
-
-        std::string tmp;
-        std::getline(std::cin, tmp);
+        std::string command;
+        std::getline(std::cin, command);
+        int cmd = -10;
+        try {
+            cmd = stoi(command);
+        } catch (std::invalid_argument) {
+            std::cout << "Invalid command entered!\n\n";
+            continue;
+        }
 
         if (cmd == -1) {
             std::cout << "Exiting. Thanks for using me!\n";
@@ -170,14 +198,14 @@ int main() {
             }
         }
         if (cmd == 2) {
-            std::cout << "Old locale is " << (file.GetLocaleManager().GetCurrentLocale() == DatLocaleManager::PATCHED ? "RU" : "Original") << endl;
+            std::cout << "Old locale is " << (file.GetLocaleManager().GetCurrentLocale() == DatLocaleManager::PATCHED ? "RU" : "Original") << std::endl;
             std::cout << "Changing locale..." << std::endl;
             file.GetLocaleManager().SetLocale(file.GetLocaleManager().GetCurrentLocale() == DatLocaleManager::PATCHED  ? DatLocaleManager::ORIGINAL : DatLocaleManager::PATCHED);
-            std::cout << "New locale is " << (file.GetLocaleManager().GetCurrentLocale() == DatLocaleManager::PATCHED ? "RU" : "Original") << endl;
+            std::cout << "New locale is " << (file.GetLocaleManager().GetCurrentLocale() == DatLocaleManager::PATCHED ? "RU" : "Original") << std::endl;
         }
 
         if (cmd == 3) {
-            std::cout << "Current locale is " << (file.GetLocaleManager().GetCurrentLocale() == DatLocaleManager::PATCHED ? "RU" : "Original") << endl;
+            std::cout << "Current locale is " << (file.GetLocaleManager().GetCurrentLocale() == DatLocaleManager::PATCHED ? "RU" : "Original") << std::endl;
         }
 
         if (cmd == 4) {
@@ -200,7 +228,7 @@ int main() {
             std::cout << "Disabled categories: ";
             for (auto i : file.GetLocaleManager().GetInactiveCategories())
                 std::cout << i << " ";
-            std::cout << endl;
+            std::cout << std::endl;
         }
 
         if (cmd == 7) {