Clone of UAS2 @ https://github.com/drudgedance/uas2

DatFile.h 5.9KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241
  1. /*
  2. * This file is part of UAS2.
  3. *
  4. * UAS2 is free software; you can redistribute it and/or modify
  5. * it under the terms of the GNU General Public License as published by
  6. * the Free Software Foundation; either version 2 of the License, or
  7. * (at your option) any later version.
  8. *
  9. * UAS2 is distributed in the hope that it will be useful,
  10. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  12. * GNU General Public License for more details.
  13. * You should have received a copy of the GNU General Public License
  14. * along with UASv1; if not, write to the Free Software
  15. * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
  16. */
  17. /* Special thanks to David Simpson for his work on the cell.dat and portal.dat extraction code */
  18. /**
  19. * @file DatFile.h
  20. */
  21. #ifndef __DATFILE_H
  22. #define __DATFILE_H
  23. #pragma warning(disable:4786) //warning: identifier was truncated to '255' characters in the browser information
  24. #include <winsock2.h>
  25. #include <list>
  26. #include <algorithm>
  27. #include "Avatar.h"
  28. #define CELLSECSIZE 64
  29. #define PORTALSECSIZE 256
  30. #define NUMFILELOC 0x03F
  31. #define ROOTDIRPTRLOC 0x148
  32. class cPortalDat
  33. {
  34. public:
  35. static void LoadStartingInfo ( cAvatar *pcAvatar );
  36. static void LoadItemModel ( cObject *pcObject, DWORD dwModelID, DWORD dwColorID = 0, double dblColorValue = -1 );
  37. static int CalcPalette ( int numPalettes, double palValue = -1 );
  38. /**
  39. * Loads the portal.dat information for the specified file.
  40. *
  41. * Calls FetchPortalFilePos to find a file's position.
  42. * Calls FetchPortalFile to fetch a file from its found position.
  43. *
  44. * @param &buf - The address of the buffer that should receive the file information.
  45. * @param file - The file to be loaded from the portal.dat.
  46. */
  47. static inline void Load_PortalDat( UCHAR* &buf, char file[9] )
  48. {
  49. FILE *inFile;
  50. int read;
  51. UINT rootDirPtr;
  52. UINT filePos, len;
  53. UINT id;
  54. inFile = fopen("portal.dat", "rb");
  55. if (inFile == NULL)
  56. {
  57. UpdateConsole(" Portal.dat: Open failed.\r\n");
  58. // return -1;
  59. }
  60. read = fseek(inFile, ROOTDIRPTRLOC, SEEK_SET);
  61. if (read != 0)
  62. {
  63. UpdateConsole(" Portal.dat: Read error.\r\n");
  64. fclose(inFile);
  65. // return -1;
  66. }
  67. read = fread(&rootDirPtr, sizeof(UINT), 1, inFile);
  68. if (read != 1)
  69. {
  70. UpdateConsole(" Portal.dat: End of file reached.\r\n");
  71. fclose(inFile);
  72. // return 0;
  73. }
  74. id = strtol(file, NULL, 16);
  75. if (!FetchPortalFilePos(inFile, rootDirPtr, id, &filePos, &len))
  76. {
  77. UpdateConsole(" Portal.dat: File not found.\r\n");
  78. // return -1;
  79. }
  80. buf = (UCHAR *)malloc(len);
  81. if (!FetchPortalFile(inFile, filePos, len, buf))
  82. {
  83. free(buf);
  84. fclose(inFile);
  85. // return -1;
  86. }
  87. //free(buf);
  88. fclose(inFile);
  89. }
  90. private:
  91. /**
  92. * Finds the location of a file in the portal.dat.
  93. *
  94. * @param &inFile - The address of the file/directory to search (the portal.dat).
  95. * @param disPos - The beginning position of the file pointer (in BYTEs).
  96. * @param id - The file to be loaded from the portal.dat.
  97. * @param *filePos - A pointer to the variable that should receive the file's position.
  98. * @param *len - A pointer to the variable that should receive the file's length.
  99. */
  100. static inline int FetchPortalFilePos(FILE *inFile, UINT dirPos, UINT id, UINT *filePos, UINT *len)
  101. {
  102. UINT dir[PORTALSECSIZE];
  103. UINT i;
  104. UINT numFiles;
  105. int read;
  106. while (1)
  107. {
  108. if (dirPos == 0)
  109. {
  110. UpdateConsole(" Portal.dat: NULL directory entry found.\r\n");
  111. return 0;
  112. }
  113. read = fseek(inFile, dirPos, SEEK_SET);
  114. if (read != 0)
  115. {
  116. UpdateConsole(" Portal.dat: Sector is beyond end of file.\r\n");
  117. return 0;
  118. }
  119. read = fread(dir, sizeof(UINT), PORTALSECSIZE, inFile);
  120. if (read != PORTALSECSIZE)
  121. {
  122. UpdateConsole(" Portal.dat: Sector doesn't contain enough words.\r\n");
  123. return 0;
  124. }
  125. numFiles = dir[NUMFILELOC];
  126. if (numFiles >= NUMFILELOC)
  127. {
  128. UpdateConsole(" Portal.dat: Number of files exceeds directory entries.\r\n");
  129. return 0;
  130. }
  131. i = 0;
  132. while ((i < numFiles) && (id > dir[i * 3 + NUMFILELOC + 1]))
  133. {
  134. i++;
  135. }
  136. if (i < numFiles)
  137. {
  138. if (id == dir[i * 3 + NUMFILELOC + 1])
  139. {
  140. *filePos = dir[i * 3 + NUMFILELOC + 2];
  141. *len = dir[i * 3 + NUMFILELOC + 3];
  142. return 1;
  143. }
  144. }
  145. if (dir[1] == 0)
  146. {
  147. filePos = 0;
  148. len = 0;
  149. return 0;
  150. }
  151. dirPos = dir[i + 1];
  152. }
  153. return 0;
  154. }
  155. /**
  156. * Fetches a file from the portal.dat.
  157. *
  158. * @param *inFile - A pointer to the file/directory to search (the portal.dat).
  159. * @param filePos - The variable that should receive the file's position.
  160. * @param len - The variable that should receive the file's length.
  161. * *@param &buf - A pointer to the buffer that should receive the file information.
  162. */
  163. static inline int FetchPortalFile(FILE *inFile, UINT filePos, UINT len, UCHAR *buf)
  164. {
  165. int read, doChain;
  166. UINT sec[PORTALSECSIZE];
  167. if (filePos == 0)
  168. {
  169. UpdateConsole(" Portal.dat: Null file pointer found.\r\n");
  170. return 0;
  171. }
  172. doChain = 1;
  173. while (doChain)
  174. {
  175. read = fseek(inFile, filePos, SEEK_SET);
  176. if (read != 0)
  177. {
  178. UpdateConsole(" Portal.dat: Seek failed.\r\n");
  179. return 0;
  180. }
  181. read = fread(sec, sizeof(UINT), PORTALSECSIZE, inFile);
  182. if (read != PORTALSECSIZE)
  183. {
  184. UpdateConsole(" Portal.dat: Sector doesn't contain enough words.\r\n");
  185. return 0;
  186. }
  187. filePos = sec[0] & 0x7FFFFFFF;
  188. if (len > (PORTALSECSIZE - 1) * sizeof(UINT))
  189. {
  190. memcpy(buf, &sec[1], (PORTALSECSIZE - 1) * sizeof(UINT));
  191. buf += (PORTALSECSIZE - 1) * sizeof(UINT);
  192. len -= (PORTALSECSIZE - 1) * sizeof(UINT);
  193. } else {
  194. memcpy(buf, &sec[1], len);
  195. len = 0;
  196. }
  197. if (filePos == 0)
  198. doChain = 0;
  199. }
  200. return 1;
  201. }
  202. };
  203. #endif // #ifndef __DATFILE_H