TrinityCore
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Modules Pages
ChunkedFile Class Reference

#include <loadlib.h>

Public Member Functions

uint8GetData ()
 
uint32 GetDataSize ()
 
 ChunkedFile ()
 
virtual ~ChunkedFile ()
 
bool prepareLoadedData ()
 
bool loadFile (HANDLE mpq, std::string const &fileName, bool log=true)
 
void free ()
 
void parseChunks ()
 
FileChunkGetChunk (std::string const &name)
 

Public Attributes

uint8data
 
uint32 data_size
 
std::multimap< std::string,
FileChunk * > 
chunks
 

Constructor & Destructor Documentation

ChunkedFile::ChunkedFile ( )
26 {
27  data = 0;
28  data_size = 0;
29 }
uint8 * data
Definition: loadlib.h:83
uint32 data_size
Definition: loadlib.h:84
ChunkedFile::~ChunkedFile ( )
virtual
32 {
33  free();
34 }
void free()
Definition: loadlib.cpp:79

+ Here is the call graph for this function:

Member Function Documentation

void ChunkedFile::free ( )
80 {
81  for (auto chunk : chunks)
82  delete chunk.second;
83 
84  chunks.clear();
85 
86  delete[] data;
87  data = 0;
88  data_size = 0;
89 }
std::multimap< std::string, FileChunk * > chunks
Definition: loadlib.h:96
Definition: adtfile.h:57
uint8 * data
Definition: loadlib.h:83
uint32 data_size
Definition: loadlib.h:84

+ Here is the caller graph for this function:

FileChunk * ChunkedFile::GetChunk ( std::string const name)
141 {
142  auto range = chunks.equal_range(name);
143  if (std::distance(range.first, range.second) == 1)
144  return range.first->second;
145 
146  return NULL;
147 }
std::multimap< std::string, FileChunk * > chunks
Definition: loadlib.h:96
arena_t NULL
Definition: jemalloc_internal.h:624
double distance(double x, double y)
Definition: g3dmath.h:731

+ Here is the call graph for this function:

+ Here is the caller graph for this function:

uint8* ChunkedFile::GetData ( )
inline
86 { return data; }
uint8 * data
Definition: loadlib.h:83

+ Here is the caller graph for this function:

uint32 ChunkedFile::GetDataSize ( )
inline
87 { return data_size; }
uint32 data_size
Definition: loadlib.h:84

+ Here is the caller graph for this function:

bool ChunkedFile::loadFile ( HANDLE  mpq,
std::string const fileName,
bool  log = true 
)
37 {
38  free();
39  HANDLE file;
40  if (!CascOpenFile(mpq, fileName.c_str(), CASC_LOCALE_ALL, 0, &file))
41  {
42  if (log)
43  printf("No such file %s\n", fileName.c_str());
44  return false;
45  }
46 
47  data_size = CascGetFileSize(file, nullptr);
48  data = new uint8[data_size];
49  CascReadFile(file, data, data_size, nullptr/*bytesRead*/);
50  parseChunks();
51  if (prepareLoadedData())
52  {
53  CascCloseFile(file);
54  return true;
55  }
56 
57  printf("Error loading %s\n", fileName.c_str());
58  CascCloseFile(file);
59  free();
60 
61  return false;
62 }
#define CASC_LOCALE_ALL
Definition: CascLib.h:64
void * HANDLE
Definition: CascPort.h:146
void free()
Definition: loadlib.cpp:79
TC_SHARED_API::google::protobuf::internal::ExtensionIdentifier< ::google::protobuf::FieldOptions,::google::protobuf::internal::EnumTypeTraits< ::bgs::protocol::LogOption,::bgs::protocol::LogOption_IsValid >, 14, false > log
void parseChunks()
Definition: loadlib.cpp:112
bool prepareLoadedData()
Definition: loadlib.cpp:64
uint8 * data
Definition: loadlib.h:83
uint32 data_size
Definition: loadlib.h:84
DWORD WINAPI CascGetFileSize(HANDLE hFile, PDWORD pdwFileSizeHigh)
Definition: CascReadFile.cpp:367
bool WINAPI CascCloseFile(HANDLE hFile)
Definition: CascOpenFile.cpp:273
bool WINAPI CascReadFile(HANDLE hFile, void *lpBuffer, DWORD dwToRead, PDWORD pdwRead)
Definition: CascReadFile.cpp:459
uint8_t uint8
Definition: Define.h:152
bool WINAPI CascOpenFile(HANDLE hStorage, const char *szFileName, DWORD dwLocale, DWORD dwFlags, HANDLE *phFile)
Definition: CascOpenFile.cpp:196
void printf(BasicWriter< Char > &w, BasicCStringRef< Char > format, ArgList args)
Definition: format.h:3083

+ Here is the call graph for this function:

+ Here is the caller graph for this function:

void ChunkedFile::parseChunks ( )
113 {
114  uint8* ptr = GetData();
115  // Make sure there's enough data to read u_map_fcc struct and the uint32 size after it
116  while (ptr <= GetData() + GetDataSize() - 8)
117  {
118  u_map_fcc header = *(u_map_fcc*)ptr;
119  if (IsInterestingChunk(header))
120  {
121  uint32 size = *(uint32*)(ptr + 4);
122  if (size <= data_size)
123  {
124  std::swap(header.fcc_txt[0], header.fcc_txt[3]);
125  std::swap(header.fcc_txt[1], header.fcc_txt[2]);
126 
127  FileChunk* chunk = new FileChunk(ptr, size);
128  chunk->parseSubChunks();
129  chunks.insert({ std::string(header.fcc_txt, 4), chunk });
130  }
131 
132  // move to next chunk
133  ptr += size + 8;
134  }
135  else
136  ++ptr;
137  }
138 }
Definition: loadlib.h:38
bool IsInterestingChunk(u_map_fcc const &fcc)
Definition: loadlib.cpp:103
std::multimap< std::string, FileChunk * > chunks
Definition: loadlib.h:96
uint32 GetDataSize()
Definition: loadlib.h:87
Definition: adtfile.h:57
void parseSubChunks()
Definition: loadlib.cpp:157
uint32_t uint32
Definition: Define.h:150
uint32 data_size
Definition: loadlib.h:84
Definition: loadlib.h:64
uint8_t uint8
Definition: Define.h:152
char fcc_txt[4]
Definition: loadlib.h:40
uint8 * GetData()
Definition: loadlib.h:86

+ Here is the call graph for this function:

+ Here is the caller graph for this function:

bool ChunkedFile::prepareLoadedData ( )
65 {
66  FileChunk* chunk = GetChunk("MVER");
67  if (!chunk)
68  return false;
69 
70  // Check version
71  file_MVER* version = chunk->As<file_MVER>();
72  if (version->fcc != MverMagic.fcc)
73  return false;
74  if (version->ver != FILE_FORMAT_VERSION)
75  return false;
76  return true;
77 }
Definition: loadlib.h:47
FileChunk * GetChunk(std::string const &name)
Definition: loadlib.cpp:140
Definition: adtfile.h:57
uint32 ver
Definition: loadlib.h:54
T * As()
Definition: loadlib.h:74
u_map_fcc MverMagic
Definition: loadlib.cpp:23
uint32 fcc
Definition: loadlib.h:41
Definition: loadlib.h:64
uint32 fcc
Definition: loadlib.h:50
#define FILE_FORMAT_VERSION
Definition: loadlib.h:34

+ Here is the call graph for this function:

+ Here is the caller graph for this function:

Member Data Documentation

std::multimap<std::string, FileChunk*> ChunkedFile::chunks
uint8* ChunkedFile::data
uint32 ChunkedFile::data_size

The documentation for this class was generated from the following files: