Browse Source

Added additional checks of correctness for DatFile::current_locale() and DatFile::SetLocale()

Ivan Arkhipov 7 years ago
parent
commit
49279a0747
4 changed files with 40 additions and 35 deletions
  1. 1 1
      include/DatFile.h
  2. BIN
      lib/libLotroDat.dll.a
  3. BIN
      lib/libLotroDat_static.a
  4. 39 34
      src/DatFile.cpp

+ 1 - 1
include/DatFile.h

@@ -76,7 +76,7 @@ namespace LOTRO_DAT {
 
         DAT_STATE DatFileState() const;
 
-        virtual ~DatFile();
+        ~DatFile();
 
         bool ExtractFile(long long file_id, const std::string &path = "");
 

BIN
lib/libLotroDat.dll.a


BIN
lib/libLotroDat_static.a


+ 39 - 34
src/DatFile.cpp

@@ -37,14 +37,7 @@ namespace LOTRO_DAT {
             OpenDatFile(filename.c_str());
             ReadSuperBlock();
             MakeDirectories();
-
-            try {
-                MakeDictionary();
-            } catch (std::exception &e) {
-                fprintf(stderr, "Caught %s exception.", e.what());
-                fprintf(stderr, "Unable to make dictionary!! Unable to init DatFile!!!");
-                return false;
-            }
+            MakeDictionary();
 
             InitLocale(PATCHED, (filename + "patched.dbgm").c_str());
             InitLocale(ORIGINAL, (filename + "original.dbgm").c_str());
@@ -67,7 +60,8 @@ namespace LOTRO_DAT {
                 throw DatException("Bad DatFile initialization! Not all init states were successfully passed!",
                                    INIT_EXCEPTION);
         } catch (std::exception &e) {
-            fprintf(stderr, "Bad DatFile::InitDatFile() - caught exception %s\n", e.what());
+            fprintf(stderr, "Bad DatFile::InitDatFile() - caught exception %s. File closed\n", e.what());
+            CloseDatFile();
             return false;
         }
         return true;
@@ -672,36 +666,43 @@ namespace LOTRO_DAT {
     }
 
     void DatFile::SetLocale(LOCALE locale) {
-        if (current_locale_ == locale) {
-            return;
-        }
-        dat_state_ = UPDATED;
-        auto dict = GetLocaleDictReference(locale);
-        for (auto file : *dict) {
-            if (dictionary_[file.first] == nullptr) {
-                fprintf(stderr, "WARNING: In locale dictionary there is file with file_id = %lld, which is not in .dat "
-                        "file! Passing it and removing from locale dictionary\n", file.first);
-                dict->erase(file.first);
-                continue;
+        try {
+            if (current_locale_ == locale) {
+                return;
             }
-            if (dictionary_[file.first]->MakeHeaderData().CutData(8, 16) == file.second->MakeHeaderData().CutData(8, 16))
-                continue;
+            dat_state_ = UPDATED;
+            auto dict = GetLocaleDictReference(locale);
+            for (auto file : *dict) {
+                if (dictionary_[file.first] == nullptr) {
+                    fprintf(stderr,
+                            "WARNING: In locale dictionary there is file with file_id = %lld, which is not in .dat "
+                                    "file! Passing it and removing from locale dictionary\n", file.first);
+                    dict->erase(file.first);
+                    continue;
+                }
+                if (dictionary_[file.first]->MakeHeaderData().CutData(8, 16) ==
+                    file.second->MakeHeaderData().CutData(8, 16))
+                    continue;
 
-            long long file_id = file.first;
-            Subfile* new_file = file.second;
+                long long file_id = file.first;
+                Subfile *new_file = file.second;
 
-            dictionary_[file_id]->file_offset_ = new_file->file_offset_;
-            dictionary_[file_id]->file_size_ = new_file->file_size_;
-            dictionary_[file_id]->block_size_= new_file->block_size_;
-            dictionary_[file_id]->timestamp_ = new_file->timestamp_;
-            dictionary_[file_id]->version_ = new_file->version_;
+                dictionary_[file_id]->file_offset_ = new_file->file_offset_;
+                dictionary_[file_id]->file_size_ = new_file->file_size_;
+                dictionary_[file_id]->block_size_ = new_file->block_size_;
+                dictionary_[file_id]->timestamp_ = new_file->timestamp_;
+                dictionary_[file_id]->version_ = new_file->version_;
 
-            patched_list.insert(file.first);
-            dat_state_ = UPDATED;
-        }
+                patched_list.insert(file.first);
+                dat_state_ = UPDATED;
+            }
 
-        current_locale_ = locale;
-        CommitChanges();
+            current_locale_ = locale;
+            CommitChanges();
+        } catch (std::exception &e) {
+            fprintf(stderr, "Bad DatFile::SetLocale() - caught exception %s. Locale wasn't set.\n");
+            return;
+        }
     }
 
     void DatFile::SaveLocale(LOCALE locale, const char *filename) {
@@ -727,6 +728,10 @@ namespace LOTRO_DAT {
     }
 
     LOCALE DatFile::current_locale() {
+        if (dat_state_ < READY) {
+            fprintf(stderr, "Bad DatFile::current_locale() - dat_file is in incorrect state!\n");
+            return ORIGINAL;
+        }
         if (current_locale_ != PATCHED && current_locale_ != ORIGINAL) {
             fprintf(stderr, "Bad DatFile::current_locale() - locale has incorrect value. Setting it to original\n");
             current_locale_ = ORIGINAL;