MCPcopy Create free account
hub / github.com/ddnet/ddnet / GetData

Method GetData

src/engine/shared/datafile.cpp:191–300  ·  view source on GitHub ↗

Source from the content-addressed store, hash-verified

189 }
190
191 void *GetData(int Index, bool Swap) const
192 {
193 // Invalid data indices may appear in map items
194 if(Index < 0 || Index >= m_Header.m_NumRawData)
195 {
196 return nullptr;
197 }
198
199 // Data already loaded
200 if(m_ppDataPtrs[Index] != nullptr)
201 {
202 return m_ppDataPtrs[Index];
203 }
204
205 // Don't try to load the data again if it previously failed
206 if(m_pDataSizes[Index] < 0)
207 {
208 return nullptr;
209 }
210
211 const unsigned DataSize = GetFileDataSize(Index);
212 if(m_Info.m_pDataSizes != nullptr)
213 {
214 // v4 has compressed data
215 const unsigned OriginalUncompressedSize = m_Info.m_pDataSizes[Index];
216 log_trace("datafile", "loading data. index=%d size=%d uncompressed=%d", Index, DataSize, OriginalUncompressedSize);
217 if(OriginalUncompressedSize == 0)
218 {
219 log_error("datafile", "data size invalid. data will be ignored. index=%d size=%d uncompressed=%d", Index, DataSize, OriginalUncompressedSize);
220 m_ppDataPtrs[Index] = nullptr;
221 m_pDataSizes[Index] = -1;
222 return nullptr;
223 }
224
225 // read the compressed data
226 void *pCompressedData = malloc(DataSize);
227 if(pCompressedData == nullptr)
228 {
229 log_error("datafile", "out of memory. could not allocate memory for compressed data. index=%d size=%d", Index, DataSize);
230 m_ppDataPtrs[Index] = nullptr;
231 m_pDataSizes[Index] = -1;
232 return nullptr;
233 }
234 unsigned ActualDataSize = 0;
235 if(io_seek(m_File, m_DataStartOffset + m_Info.m_pDataOffsets[Index], IOSEEK_START) == 0)
236 {
237 ActualDataSize = io_read(m_File, pCompressedData, DataSize);
238 }
239 if(DataSize != ActualDataSize)
240 {
241 log_error("datafile", "truncation error. could not read all compressed data. index=%d wanted=%d got=%d", Index, DataSize, ActualDataSize);
242 free(pCompressedData);
243 m_ppDataPtrs[Index] = nullptr;
244 m_pDataSizes[Index] = -1;
245 return nullptr;
246 }
247
248 // decompress the data

Callers

nothing calls this directly

Calls 4

io_seekFunction · 0.85
io_readFunction · 0.85
uncompressFunction · 0.85
SwapEndianInPlaceFunction · 0.85

Tested by

no test coverage detected