|
@@ -13,13 +13,27 @@ namespace LOTRO_DAT {
|
|
|
DatExporter::DatExporter(DatFile *datFilePtr) : dat(datFilePtr) {
|
|
|
}
|
|
|
|
|
|
+ /*!
|
|
|
+ * \author Gi1dor
|
|
|
+ * \date 05.07.2018
|
|
|
+ * Извлечение в папку всех файлов, имеющих заданный тип
|
|
|
+ * \param[in] type Тип файлов
|
|
|
+ * \param[in] output_directory_path путь к папке, куда будут записаны файлы. Имя экспортируемого файла - file_id.ext, где file_id - id файла, ext - его расширение
|
|
|
+ *
|
|
|
+ * \returns DatOperationResult.value - количество извлечённых файлов
|
|
|
+ */
|
|
|
+
|
|
|
+
|
|
|
DatOperationResult<int> DatExporter::ExtractAllFilesByType(const FILE_TYPE &type, std::string output_directory_path) {
|
|
|
int success_exported = 0;
|
|
|
|
|
|
auto operation = dat->getFileSystem().PerformOperationOnAllFiles([&success_exported, this, type, &output_directory_path](std::shared_ptr<SubFile>& file) -> void {
|
|
|
- if (file->FileType() != type || !dat->getFileSystem().CheckCorrectSubfile(file))
|
|
|
+ if (file->FileType() != type)
|
|
|
+ return;
|
|
|
+ SubfileData export_file = file->PrepareForExport(dat->getFileSystem().GetFileData(file->file_id(), 8).value);
|
|
|
+
|
|
|
+ if (export_file.Empty())
|
|
|
return;
|
|
|
- SubfileData export_file = file->PrepareForExport(dat->getFileSystem().GetFileData(file->file_id(), 8));
|
|
|
|
|
|
bool result;
|
|
|
|
|
@@ -34,6 +48,15 @@ namespace LOTRO_DAT {
|
|
|
return DatOperationResult<int>(success_exported, SUCCESS);
|
|
|
}
|
|
|
|
|
|
+
|
|
|
+ /*!
|
|
|
+ * \author Gi1dor
|
|
|
+ * \date 05.07.2018
|
|
|
+ * Извлечение файла по его id
|
|
|
+ * \param[in] file_id id файла
|
|
|
+ * \param[in] output_filename - имя файла, куда будет записаны данные.
|
|
|
+ */
|
|
|
+
|
|
|
DatOperationResult<> DatExporter::ExtractFileById(long long file_id, std::string output_filename) {
|
|
|
auto operation_GetFileData = dat->getFileSystem().GetFileData(file_id, 8);
|
|
|
if (operation_GetFileData.result == ERROR)
|
|
@@ -44,27 +67,45 @@ namespace LOTRO_DAT {
|
|
|
if (operation_GetFileData.result == ERROR)
|
|
|
return DatOperationResult<>(ERROR, "EXTRACTFILEBYID: Can't get pointer for id = " + std::to_string(file_id));
|
|
|
|
|
|
- SubfileData export_data = operation_GetFilePointer.value->PrepareForExport(operation_GetFileData.value);
|
|
|
+ std::shared_ptr<SubFile> file = operation_GetFilePointer.value;
|
|
|
+
|
|
|
+ SubfileData export_data = file->PrepareForExport(operation_GetFileData.value);
|
|
|
|
|
|
- if (export_data.Empty())
|
|
|
- return DatOperationResult<>(ERROR, "EXTRACTFILEBYID: Export data is empty for id = " + std::to_string(file_id));
|
|
|
+ bool result;
|
|
|
|
|
|
- if (!export_data.binary_data.WriteToFile(output_filename + export_data.options["ext"].as<std::string>()))
|
|
|
- return DatOperationResult<>(ERROR, "EXTRACTFILEBYID: Cannot write to file" + output_filename);
|
|
|
+ if (file->FileType() == TEXT)
|
|
|
+ result = WriteStringToFile(export_data.text_data, output_filename + file->Extension()).result;
|
|
|
+ else
|
|
|
+ result = export_data.binary_data.WriteToFile(output_filename + file->Extension());
|
|
|
+
|
|
|
+ if (!result)
|
|
|
+ return DatOperationResult<>(ERROR, "EXTRACTFILEBYID: Cannot write to file" + output_filename + file->Extension());
|
|
|
|
|
|
return DatOperationResult<>(SUCCESS);
|
|
|
}
|
|
|
|
|
|
+
|
|
|
+ /*!
|
|
|
+ * \author Gi1dor
|
|
|
+ * \date 05.07.2018
|
|
|
+ * Извлечение в базу данных всех файлов, имеющих заданный тип
|
|
|
+ * \param[in] type Тип файлов
|
|
|
+ * \param[in] db Указатель на объект базы данных
|
|
|
+ *
|
|
|
+ * \returns DatOperationResult.value - количество извлечённых файлов
|
|
|
+ */
|
|
|
+
|
|
|
+
|
|
|
DatOperationResult<int> DatExporter::ExtractAllFilesByType(const FILE_TYPE &type, Database *db) {
|
|
|
if (!db)
|
|
|
return DatOperationResult<int>(0, ERROR, "EXTRACTALLBYTYPETODB: database is nullptr");
|
|
|
|
|
|
int success_exported = 0;
|
|
|
auto operation = dat->getFileSystem().PerformOperationOnAllFiles([&success_exported, this, type, db](std::shared_ptr<SubFile>& file) {
|
|
|
- if (!dat->getFileSystem().CheckCorrectSubfile(file))
|
|
|
+ if (file->FileType() != type)
|
|
|
return;
|
|
|
|
|
|
- SubfileData export_file = file->PrepareForExport(dat->getFileSystem().GetFileData(file->file_id(), 8));
|
|
|
+ SubfileData export_file = file->PrepareForExport(dat->getFileSystem().GetFileData(file->file_id(), 8).value);
|
|
|
|
|
|
bool result = db->PushFile(export_file);
|
|
|
|
|
@@ -74,13 +115,25 @@ namespace LOTRO_DAT {
|
|
|
return DatOperationResult<int>(success_exported, SUCCESS);
|
|
|
}
|
|
|
|
|
|
+
|
|
|
+ /*!
|
|
|
+ * \author Gi1dor
|
|
|
+ * \date 05.07.2018
|
|
|
+ * Запись строки в формате UTF-16 в файл
|
|
|
+ * \param[in] str Строка для записи в файл
|
|
|
+ * \param[in] path Путь к файлу, в который строка будет записана
|
|
|
+ *
|
|
|
+ * \warning Путь к файлу должен быть корректным (папки должны существовать). Если и сам файл существует, то он будет перезаписан
|
|
|
+ */
|
|
|
+
|
|
|
DatOperationResult<> DatExporter::WriteStringToFile(const std::u16string &str, const std::string &path) {
|
|
|
- FILE* file = fopen(path.c_str(), "w");
|
|
|
- if (!file)
|
|
|
+ std::basic_ofstream<char16_t> output_stream(path, std::ios::out);
|
|
|
+ if (!output_stream.is_open())
|
|
|
return DatOperationResult<>(ERROR, "WRITESTRINGTOFILE: cant open file " + path);
|
|
|
|
|
|
- fprintf(file, "%s", (char *) str.c_str());
|
|
|
- fclose(file);
|
|
|
+ output_stream << str;
|
|
|
+ output_stream.close();
|
|
|
+
|
|
|
return DatOperationResult<>();
|
|
|
}
|
|
|
|