VGearsLGPArchiveSerializer.cpp 3.6 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495
  1. /*
  2. * Copyright (C) 2022 The V-Gears Team
  3. *
  4. * This file is part of V-Gears
  5. *
  6. * V-Gears is free software: you can redistribute it and/or modify it under
  7. * terms of the GNU General Public License as published by the Free Software
  8. * Foundation, version 3.0 (GPLv3) of the License.
  9. *
  10. * V-Gears is distributed in the hope that it will be useful,
  11. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  12. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  13. * GNU General Public License for more details.
  14. */
  15. #include <OgreLogManager.h>
  16. #include <OgreException.h>
  17. #include "data/VGearsLGPArchiveSerializer.h"
  18. namespace VGears{
  19. LGPArchiveSerializer::LGPArchiveSerializer() : Serializer(){}
  20. LGPArchiveSerializer::~LGPArchiveSerializer(){}
  21. void LGPArchiveSerializer::ImportLGPArchive(
  22. Ogre::DataStreamPtr &stream, LGPArchive* dest
  23. ){
  24. ReadFileHeader(stream);
  25. uint32 file_count(0);
  26. ReadUInt32(stream, file_count);
  27. FileList& files(dest->GetFiles());
  28. ReadVector(stream, files, file_count);
  29. ReadFolders(stream, files);
  30. FileList::iterator it(files.begin());
  31. FileList::const_iterator it_end(files.end());
  32. while (it != it_end){
  33. stream->seek(it->file_offset);
  34. it->datafile_name = readString(stream, FILE_NAME_LENGTH);
  35. ReadUInt32(stream, it->data_size);
  36. it->data_offset = stream->tell();
  37. ++ it;
  38. }
  39. }
  40. void LGPArchiveSerializer::ReadFolders(Ogre::DataStreamPtr &stream, FileList& files){
  41. // After the file table there's a lookup table of 30 * 30 entries, then the conflict tables.
  42. // Each conflict table lists the folders where files of the same name are, with the index
  43. // of the file in the file table.
  44. static const size_t LOOKUP_TABLE_SIZE = 30 * 30 * 4;
  45. static const size_t FOLDER_NAME_LENGTH = 128;
  46. if (stream->size() < stream->tell() + LOOKUP_TABLE_SIZE + 2) return;
  47. stream->skip(LOOKUP_TABLE_SIZE);
  48. uint16 table_count(0);
  49. ReadUInt16(stream, table_count);
  50. for (uint16 t = 0; t < table_count; ++ t){
  51. if (stream->eof()) return;
  52. uint16 entry_count(0);
  53. ReadUInt16(stream, entry_count);
  54. for (uint16 e = 0; e < entry_count; ++ e){
  55. if (stream->size() < stream->tell() + FOLDER_NAME_LENGTH + 2) return;
  56. String folder(readString(stream, FOLDER_NAME_LENGTH));
  57. uint16 index(0);
  58. ReadUInt16(stream, index);
  59. if (index < files.size()) files[index].folder = folder;
  60. }
  61. }
  62. }
  63. void LGPArchiveSerializer::ReadFileHeader(Ogre::DataStreamPtr &stream){
  64. stream->skip(2); // unused
  65. String magic_string(readString(stream, MAGIC_STRING_LENGTH));
  66. }
  67. void LGPArchiveSerializer::readObject(
  68. Ogre::DataStreamPtr &stream, FileEntry& file_entry
  69. ){
  70. file_entry.file_name = readString(stream, FILE_NAME_LENGTH);
  71. ReadUInt32(stream, file_entry.file_offset);
  72. stream->read(&file_entry.unknown1, sizeof(file_entry.unknown1));
  73. ReadShort(stream, file_entry.unknown2);
  74. if (
  75. (file_entry.unknown1 != 14 && file_entry.unknown1 != 11)
  76. || file_entry.unknown2 != 0
  77. ){
  78. Ogre::LogManager::getSingleton().stream()
  79. << "file_name: " << file_entry.file_name
  80. << " file_offset: " << file_entry.file_offset
  81. << " unknown1: " << (uint16)file_entry.unknown1
  82. << " unknown2: " << file_entry.unknown2;
  83. }
  84. }
  85. }