--------------------------------- PackageWriter::Write Write the listed files to the data vector
| 115 | // Write the listed files to the data vector |
| 116 | // |
| 117 | void PackageWriter::Write(std::vector<uint8>& data) |
| 118 | { |
| 119 | // we do a first pass where we figure out the relative offset for all files |
| 120 | //--------------------------- |
| 121 | uint64 offset = 0u; |
| 122 | |
| 123 | // as we go we can figure out the header |
| 124 | core::PkgHeader header; |
| 125 | header.numEntries = static_cast<uint64>(m_Files.size()); |
| 126 | offset += static_cast<uint64>(sizeof(core::PkgHeader)); |
| 127 | |
| 128 | // and the size that our central directory will be |
| 129 | offset += static_cast<uint64>(sizeof(core::PkgFileInfo)) * header.numEntries; |
| 130 | |
| 131 | // as we go we can fill in the entry information |
| 132 | std::vector<core::PkgFileInfo> fileInfos; |
| 133 | for (FileEntryInfo& entryFile : m_Files) |
| 134 | { |
| 135 | core::PkgFileInfo info; |
| 136 | info.fileId = entryFile.entry.fileId; |
| 137 | info.offset = offset; |
| 138 | |
| 139 | // we already know the offset so we can add the file entry |
| 140 | fileInfos.emplace_back(info); |
| 141 | |
| 142 | // accumulate our offset |
| 143 | core::PkgEntry& entry = entryFile.entry; |
| 144 | offset += sizeof(core::PkgEntry) + static_cast<uint64>(entry.nameLength) + entry.size; |
| 145 | } |
| 146 | |
| 147 | // now we know the total size of the package file will be our offset, so we can initialize our data vector starting with the right size |
| 148 | //--------------------------- |
| 149 | data = std::vector<uint8>(static_cast<size_t>(offset), 0u); |
| 150 | uint8* raw = data.data(); |
| 151 | |
| 152 | // finally we can write our data to the vector |
| 153 | //--------------------------- |
| 154 | |
| 155 | // header |
| 156 | memcpy(raw, &header, sizeof(core::PkgHeader)); |
| 157 | offset = sizeof(core::PkgHeader); // we can reuse it now that our vector has bin initialized |
| 158 | |
| 159 | // central dir |
| 160 | for (core::PkgFileInfo const& info : fileInfos) |
| 161 | { |
| 162 | memcpy(raw + offset, &info, sizeof(core::PkgFileInfo)); |
| 163 | offset += sizeof(core::PkgFileInfo); |
| 164 | } |
| 165 | |
| 166 | // files |
| 167 | for (size_t entryIndex = 0u; entryIndex < m_Files.size(); ++entryIndex) |
| 168 | { |
| 169 | FileEntryInfo& entryFile = m_Files[entryIndex]; |
| 170 | |
| 171 | if (offset != fileInfos[entryIndex].offset) |
| 172 | { |
| 173 | LOG("PackageWriter::Write > Entry offset doesn't match expected offset - " + entryFile.relName, core::LogLevel::Error); |
| 174 | } |
no test coverage detected