Clone of PhatAC @ https://github.com/floaterxk/PhatAC

DATDisk.cpp 8.6KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495
  1. #include "StdAfx.h"
  2. #include "DATDisk.h"
  3. DATDisk *DATDisk::pPortal = NULL;
  4. DATDisk *DATDisk::pCell = NULL;
  5. BOOL DATDisk::OpenDisks(const char *portalPath, const char *cellPath)
  6. {
  7. pPortal = new DATDisk(portalPath);
  8. if (!pPortal->Open())
  9. return FALSE;
  10. pCell = new DATDisk(cellPath);
  11. if (!pCell->Open())
  12. return FALSE;
  13. return TRUE;
  14. }
  15. void DATDisk::CloseDisks()
  16. {
  17. if (pPortal)
  18. {
  19. delete pPortal;
  20. pPortal = NULL;
  21. }
  22. if (pCell)
  23. {
  24. delete pCell;
  25. pCell = NULL;
  26. }
  27. }
  28. DATDisk::DATDisk(const char *Path) : m_BTree(&m_BlockLoader)
  29. {
  30. m_FilePath = _strdup(Path);
  31. ZeroMemory(&m_DATHeader, sizeof(DATHeader));
  32. }
  33. DATDisk::~DATDisk()
  34. {
  35. if (m_FilePath)
  36. free(m_FilePath);
  37. }
  38. BOOL DATDisk::Open()
  39. {
  40. if (!m_BlockLoader.Init(m_FilePath, &m_DATHeader))
  41. return FALSE;
  42. if (!m_BTree.Init())
  43. return FALSE;
  44. return TRUE;
  45. }
  46. const DATHeader *DATDisk::GetHeader()
  47. {
  48. return &m_DATHeader;
  49. }
  50. void DATDisk::FindFileIDsWithinRange(DWORD Min, DWORD Max, void(*FileCallback)(void *, DWORD, BTreeEntry *), void(*ProgressCallback)(void *, float), void *CallbackArg)
  51. {
  52. m_BTree.SetFileCallback(FileCallback);
  53. m_BTree.SetProgressCallback(ProgressCallback);
  54. m_BTree.SetCallbackArg(CallbackArg);
  55. m_BTree.FindEntryIDsWithinRange(Min, Max, 0, 100.0);
  56. }
  57. BOOL DATDisk::GetData(DWORD ID, DATEntry *pEntry)
  58. {
  59. BTreeEntry FileInfo;
  60. // Look up the file within the BTree.
  61. if (!m_BTree.Lookup(ID, &FileInfo))
  62. return FALSE;
  63. // Attempt to allocate a file buffer. (Extra DWORD for block loading)
  64. BYTE *Buffer = new BYTE[sizeof(DWORD) + FileInfo.Length];
  65. if (!Buffer)
  66. return FALSE;
  67. if (!m_BlockLoader.LoadData(FileInfo.BlockHead, Buffer, FileInfo.Length))
  68. {
  69. delete[] Buffer;
  70. return FALSE;
  71. }
  72. if (pEntry)
  73. {
  74. pEntry->ID = ID;
  75. pEntry->Data = Buffer;
  76. pEntry->Length = FileInfo.Length;
  77. }
  78. else
  79. delete[] Buffer;
  80. return TRUE;
  81. }
  82. BOOL DATDisk::GetDataEx(DWORD BlockHead, void *Data, DWORD Length)
  83. {
  84. if (!m_BlockLoader.LoadData(BlockHead, Data, Length))
  85. return FALSE;
  86. return TRUE;
  87. }
  88. void(*BTreeNode::m_pfnFileCallback)(void *, DWORD, BTreeEntry *);
  89. void(*BTreeNode::m_pfnProgressCallback)(void *, float);
  90. void *BTreeNode::m_pCallbackArg = NULL;
  91. BTreeNode::BTreeNode(BlockLoader *pBlockLoader)
  92. {
  93. m_pBlockLoader = pBlockLoader;
  94. for (DWORD i = 0; i < 0x3E; i++)
  95. m_Branches[i] = NULL;
  96. }
  97. BTreeNode::~BTreeNode()
  98. {
  99. for (DWORD i = 0; i < 0x3E; i++)
  100. {
  101. if (m_Branches[i])
  102. delete m_Branches[i];
  103. }
  104. }
  105. BOOL BTreeNode::LoadData(DWORD BlockHead)
  106. {
  107. if (!BlockHead)
  108. return FALSE;
  109. if (!m_pBlockLoader->LoadData(BlockHead, &m_TreeData, sizeof(BTreeData) - sizeof(DWORD)))
  110. return FALSE;
  111. m_bLeaf = (!m_TreeData.Branches[0] ? TRUE : FALSE);
  112. return TRUE;
  113. }
  114. DWORD BTreeNode::GetBranchCount() const
  115. {
  116. if (m_bLeaf)
  117. return 0;
  118. return(m_TreeData.EntryCount + 1);
  119. }
  120. void BTreeNode::LoadChildren(void)
  121. {
  122. for (DWORD i = 0; i < GetBranchCount(); i++)
  123. {
  124. if (m_Branches[i])
  125. continue;
  126. m_Branches[i] = new BTreeNode(m_pBlockLoader);
  127. if (!m_Branches[i])
  128. continue;
  129. if (!m_Branches[i]->LoadData(m_TreeData.Branches[i]))
  130. {
  131. delete m_Branches[i];
  132. m_Branches[i] = NULL;
  133. }
  134. }
  135. }
  136. void BTreeNode::LoadChildrenRecursive(void)
  137. {
  138. for (DWORD i = 0; i < GetBranchCount(); i++)
  139. {
  140. if (m_Branches[i])
  141. continue;
  142. m_Branches[i] = new BTreeNode(m_pBlockLoader);
  143. if (!m_Branches[i])
  144. continue;
  145. if (m_Branches[i]->LoadData(m_TreeData.Branches[i]))
  146. m_Branches[i]->LoadChildrenRecursive();
  147. else
  148. {
  149. delete m_Branches[i];
  150. m_Branches[i] = NULL;
  151. }
  152. }
  153. }
  154. void BTreeNode::SetFileCallback(void(*pfnFileCallback)(void *, DWORD, BTreeEntry *))
  155. {
  156. m_pfnFileCallback = pfnFileCallback;
  157. }
  158. void BTreeNode::SetProgressCallback(void(*pfnProgressCallback)(void *, float))
  159. {
  160. m_pfnProgressCallback = pfnProgressCallback;
  161. }
  162. void BTreeNode::SetCallbackArg(void *CallbackArg)
  163. {
  164. m_pCallbackArg = CallbackArg;
  165. }
  166. BTreeNode* BTreeNode::GetBranch(DWORD index)
  167. {
  168. if (!m_Branches[index])
  169. {
  170. if (m_Branches[index] = new BTreeNode(m_pBlockLoader))
  171. {
  172. if (!m_Branches[index]->LoadData(m_TreeData.Branches[index]))
  173. {
  174. delete m_Branches[index];
  175. m_Branches[index] = NULL;
  176. }
  177. }
  178. }
  179. return m_Branches[index];
  180. }
  181. BOOL BTreeNode::Lookup(DWORD ID, BTreeEntry *pEntry)
  182. {
  183. unsigned int i;
  184. for (i = 0; i < m_TreeData.EntryCount; i++)
  185. {
  186. if (ID < m_TreeData.Entries[i].ID)
  187. {
  188. BTreeNode *Branch;
  189. if (!m_bLeaf && (Branch = GetBranch(i)))
  190. return Branch->Lookup(ID, pEntry);
  191. else
  192. return FALSE;
  193. }
  194. if (ID == m_TreeData.Entries[i].ID)
  195. {
  196. if (pEntry)
  197. *pEntry = m_TreeData.Entries[i];
  198. return TRUE;
  199. }
  200. }
  201. BTreeNode *LastBranch;
  202. if (!m_bLeaf && (LastBranch = GetBranch(i)))
  203. return LastBranch->Lookup(ID, pEntry);
  204. else
  205. return FALSE;
  206. }
  207. void BTreeNode::FindEntryIDsWithinRange(DWORD Min, DWORD Max, float Progress, float ProgressDelta)
  208. {
  209. DWORD NumBranches = GetBranchCount();
  210. float BranchDelta = (NumBranches ? ProgressDelta / NumBranches : 0);
  211. float ProgressStart = Progress;
  212. unsigned int i;
  213. for (i = 0; i < m_TreeData.EntryCount; i++)
  214. {
  215. DWORD ID = m_TreeData.Entries[i].ID;
  216. if (ID > Max)
  217. {
  218. BTreeNode *Branch;
  219. if (!m_bLeaf && (Branch = GetBranch(i)))
  220. Branch->FindEntryIDsWithinRange(Min, Max, Progress, BranchDelta);
  221. return;
  222. }
  223. if (ID < Min)
  224. continue;
  225. BTreeNode *Branch;
  226. if (!m_bLeaf && (Branch = GetBranch(i)))
  227. Branch->FindEntryIDsWithinRange(Min, Max, Progress, BranchDelta);
  228. if (m_pfnFileCallback)
  229. m_pfnFileCallback(m_pCallbackArg, ID, &m_TreeData.Entries[i]);
  230. if (!m_bLeaf)
  231. {
  232. Progress += BranchDelta;
  233. if (m_pfnProgressCallback)
  234. m_pfnProgressCallback(m_pCallbackArg, Progress);
  235. }
  236. }
  237. if (!m_bLeaf)
  238. {
  239. BTreeNode *LastBranch;
  240. if (LastBranch = GetBranch(i))
  241. LastBranch->FindEntryIDsWithinRange(Min, Max, Progress, BranchDelta);
  242. }
  243. Progress = ProgressStart + ProgressDelta;
  244. if (m_pfnProgressCallback)
  245. m_pfnProgressCallback(m_pCallbackArg, Progress);
  246. }
  247. BTree::BTree(BlockLoader *pBlockLoader) : BTreeNode(pBlockLoader)
  248. {
  249. }
  250. BTree::~BTree()
  251. {
  252. }
  253. BOOL BTree::Init()
  254. {
  255. if (!LoadData(m_pBlockLoader->GetTreeOrigin()))
  256. return FALSE;
  257. // Preload immediate children. ?
  258. // LoadChildren();
  259. // Preload ALL children. ? (very slow)
  260. LoadChildrenRecursive();
  261. return TRUE;
  262. }
  263. BlockLoader::BlockLoader()
  264. {
  265. m_pHeader = NULL;
  266. }
  267. BlockLoader::~BlockLoader()
  268. {
  269. }
  270. DWORD BlockLoader::GetTreeOrigin()
  271. {
  272. if (!m_pHeader)
  273. return 0;
  274. return m_pHeader->BTree;
  275. }
  276. BOOL BlockLoader::Init(const char *Path, DATHeader *pHeader)
  277. {
  278. m_pHeader = pHeader;
  279. return m_DiskDev.OpenFile(Path, m_pHeader);
  280. }
  281. BOOL BlockLoader::LoadData(DWORD HeadBlock, void *pBuffer, DWORD Length)
  282. {
  283. BYTE* pbBuffer = (BYTE *)pBuffer;
  284. DWORD dwLength = Length;
  285. DWORD dwBlockSize = m_pHeader->BlockSize;
  286. DWORD dwDataPerBlock = dwBlockSize - sizeof(DWORD);
  287. BOOL bLoadOK = TRUE;
  288. if (!dwLength)
  289. return TRUE;
  290. DWORD dwBlock = HeadBlock;
  291. while (dwBlock)
  292. {
  293. if (!bLoadOK)
  294. return FALSE;
  295. DWORD dwOldValue = *((DWORD *)pbBuffer);
  296. if (dwDataPerBlock > dwLength)
  297. {
  298. dwBlockSize = dwLength + sizeof(DWORD);
  299. dwDataPerBlock = dwLength;
  300. }
  301. if (m_DiskDev.SyncRead(pbBuffer, dwBlockSize, dwBlock))
  302. {
  303. dwBlock = *((DWORD *)pbBuffer);
  304. *((DWORD *)pbBuffer) = dwOldValue;
  305. dwLength -= dwDataPerBlock;
  306. pbBuffer += dwDataPerBlock;
  307. if (dwBlock & 0x80000000)
  308. {
  309. dwBlock &= ~0x80000000;
  310. bLoadOK = FALSE;
  311. }
  312. }
  313. else
  314. bLoadOK = FALSE;
  315. if (!dwLength)
  316. break;
  317. }
  318. if (dwLength)
  319. bLoadOK = FALSE;
  320. return bLoadOK;
  321. }
  322. DiskDev::DiskDev()
  323. {
  324. m_hFile = INVALID_HANDLE_VALUE;
  325. }
  326. DiskDev::~DiskDev()
  327. {
  328. CloseFile();
  329. }
  330. BOOL DiskDev::OpenFile(const char* Path, DATHeader *pHeader)
  331. {
  332. CloseFile();
  333. m_hFile = CreateFile(Path, GENERIC_READ, FILE_SHARE_READ, NULL, OPEN_EXISTING, NULL, NULL);
  334. if (m_hFile == INVALID_HANDLE_VALUE)
  335. return FALSE;
  336. if (pHeader)
  337. {
  338. if (!SyncRead(pHeader, sizeof(DATHeader), DAT_HEADER_OFFSET))
  339. return FALSE;
  340. }
  341. return TRUE;
  342. }
  343. void DiskDev::CloseFile()
  344. {
  345. if (m_hFile != INVALID_HANDLE_VALUE)
  346. {
  347. CloseHandle(m_hFile);
  348. m_hFile = INVALID_HANDLE_VALUE;
  349. }
  350. }
  351. BOOL DiskDev::SyncRead(void *pBuffer, DWORD dwLength, DWORD dwPosition)
  352. {
  353. if (INVALID_SET_FILE_POINTER == SetFilePointer(m_hFile, dwPosition, 0, FILE_BEGIN))
  354. return FALSE;
  355. DWORD dwBytesRead;
  356. if (!ReadFile(m_hFile, pBuffer, dwLength, &dwBytesRead, FALSE))
  357. return FALSE;
  358. if (dwBytesRead != dwLength)
  359. return FALSE;
  360. return TRUE;
  361. }
  362. BOOL DiskDev::SyncWrite(void *pBuffer, DWORD dwLength, DWORD dwPosition)
  363. {
  364. if (INVALID_SET_FILE_POINTER == SetFilePointer(m_hFile, dwPosition, 0, FILE_BEGIN))
  365. return FALSE;
  366. DWORD dwBytesWritten;
  367. if (!WriteFile(m_hFile, pBuffer, dwLength, &dwBytesWritten, FALSE))
  368. return FALSE;
  369. if (dwBytesWritten != dwLength)
  370. return FALSE;
  371. return TRUE;
  372. }