datexportapi.cpp 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139
  1. #include "datexportapi.h"
  2. #include <EasyLogging++/easylogging++.h>
  3. void ReportWinAPIError(std::string error_msg) {
  4. int error_code = GetLastError();
  5. LOG(ERROR) << error_msg << "Error code: " << error_code;
  6. error_msg = error_msg +
  7. " Error code: " +
  8. std::to_string(error_code) +
  9. "\n\n"
  10. "Please, contact #support at LotRO Legacy Discord Channel (https://discord.gg/j25MdKR)\n"
  11. "or write an e-mail to developer (me@endevir.ru) with this error description!";
  12. MessageBox(
  13. NULL,
  14. error_msg.c_str(),
  15. "Endevir's LotroDat library error!",
  16. MB_ICONERROR | MB_OK
  17. );
  18. exit(1);
  19. }
  20. DatExportApi::DatExportApi() {
  21. datexport_dll_ = LoadLibraryA("datexport");
  22. if (datexport_dll_ == NULL) {
  23. ReportWinAPIError("Error while opening runtime library!");
  24. }
  25. /* get pointer to the function in the dll*/
  26. open_dat_file_addr_ = GetProcAddress(datexport_dll_, "OpenDatFileEx2");
  27. if (open_dat_file_addr_ == NULL) {
  28. ReportWinAPIError("Error while parsing runtime library funciton 0x1!");
  29. }
  30. open_dat_file_func_ = DatExportApiPrivate::OpenDatFileFunc(open_dat_file_addr_);
  31. get_num_subfiles_addr_ = GetProcAddress(datexport_dll_, "GetNumSubfiles");
  32. if (get_num_subfiles_addr_ == NULL) {
  33. ReportWinAPIError("Error while parsing runtime library funciton 0x2!");
  34. }
  35. get_num_subfiles_func_ = DatExportApiPrivate::GetNumSubfilesFunc(get_num_subfiles_addr_);
  36. get_subfile_sizes_addr_ = GetProcAddress(datexport_dll_, "GetSubfileSizes");
  37. if (get_subfile_sizes_addr_ == NULL) {
  38. ReportWinAPIError("Error while parsing runtime library funciton 0x3!");
  39. }
  40. get_subfile_sizes_func_ = DatExportApiPrivate::GetSubFileSizesFunc(get_subfile_sizes_addr_);
  41. get_subfile_version_addr_ = GetProcAddress(datexport_dll_, "GetSubfileVersion");
  42. if (get_subfile_version_addr_ == NULL) {
  43. ReportWinAPIError("Error while parsing runtime library funciton 0x4!");
  44. }
  45. get_subfile_version_func_ = DatExportApiPrivate::GetSubFileVersionFunc(get_subfile_version_addr_);
  46. get_subfile_data_addr_ = GetProcAddress(datexport_dll_, "GetSubfileData");
  47. if (get_subfile_data_addr_ == NULL) {
  48. ReportWinAPIError("Error while parsing runtime library funciton 0x5!");
  49. }
  50. get_subfile_data_func_ = DatExportApiPrivate::GetSubfileDataFunc(get_subfile_data_addr_);
  51. close_dat_file_addr_ = GetProcAddress(datexport_dll_, "CloseDatFile");
  52. if (close_dat_file_addr_ == NULL) {
  53. ReportWinAPIError("Error while parsing runtime library funciton 0x6!");
  54. }
  55. close_dat_file_func_ = DatExportApiPrivate::CloseDatFileFunc(close_dat_file_addr_);
  56. purge_subfile_data_addr_ = GetProcAddress(datexport_dll_, "PurgeSubfileData");
  57. if (purge_subfile_data_addr_ == NULL) {
  58. ReportWinAPIError("Error while parsing runtime library funciton 0x7!");
  59. }
  60. purge_subfile_data_func_ = DatExportApiPrivate::PurgeSubFileDataFunc(purge_subfile_data_addr_);
  61. put_subfile_data_addr_ = GetProcAddress(datexport_dll_, "PutSubfileData");
  62. if (open_dat_file_addr_ == NULL) {
  63. ReportWinAPIError("Error while parsing runtime library funciton 0x8!");
  64. }
  65. put_subfile_data_func_ = DatExportApiPrivate::PutSubFileDataFunc(put_subfile_data_addr_);
  66. flush_addr_ = GetProcAddress(datexport_dll_, "Flush");
  67. if (open_dat_file_addr_ == NULL) {
  68. ReportWinAPIError("Error while parsing runtime library funciton 0x9!");
  69. }
  70. flush_func_ = DatExportApiPrivate::FlushFunc(flush_func_);
  71. }
  72. int DatExportApi::OpenDatFile(int handle, const char* filename, unsigned int flags) {
  73. int did_master_map = 0;
  74. int block_size = 0;
  75. int vnum_dat_file = 0;
  76. int vnum_game_data = 0;
  77. unsigned long dat_file_id = 0;
  78. void* dat_id_stamp = malloc(64);
  79. void* first_iter_guid = malloc(64);
  80. int result = open_dat_file_func_(handle, filename, flags, &did_master_map, &block_size,
  81. &vnum_dat_file, &vnum_game_data, &dat_file_id, dat_id_stamp, first_iter_guid);
  82. free(dat_id_stamp);
  83. free(first_iter_guid);
  84. return result;
  85. }
  86. int DatExportApi::GetNumSubfiles(int handle) {
  87. return get_num_subfiles_func_(handle);
  88. }
  89. void DatExportApi::GetSubfileSizes(int handle, int* file_ids, int* size, int* iteration, int offset, int count) {
  90. get_subfile_sizes_func_(handle, file_ids, size, iteration, offset, count);
  91. }
  92. int DatExportApi::GetSubfileVersion(int handle, int file_id) {
  93. return get_subfile_version_func_(handle, file_id);
  94. }
  95. int DatExportApi::GetSubfileData(int handle, int file_id, void* target_buf, int& version) {
  96. return get_subfile_data_func_(handle, file_id, target_buf, 0, &version); // returns size of file in bytes
  97. }
  98. void DatExportApi::CloseDatFile(int handle) {
  99. close_dat_file_func_(handle);
  100. }
  101. int DatExportApi::PurgeSubfileData(int handle, int file_id) {
  102. return purge_subfile_data_func_(handle, file_id); // returns 0 on successfull
  103. }
  104. int DatExportApi::PutSubfileData(int handle, int file_id, void* data, int offset, int size, int version, int iteration, bool compress) {
  105. return put_subfile_data_func_(handle, file_id, data, offset, size, version, iteration, compress);
  106. }
  107. void DatExportApi::Flush(int handle) {
  108. flush_func_(handle);
  109. }
  110. DatExportApi::~DatExportApi() {
  111. FreeLibrary(datexport_dll_);
  112. }