00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00025 #ifndef __DATFILE_H
00026 #define __DATFILE_H
00027
00028 #pragma warning(disable:4786) //warning: identifier was truncated to '255' characters in the browser information
00029
00030 #include <winsock2.h>
00031 #include <list>
00032 #include <algorithm>
00033
00034 #include "Avatar.h"
00035
00036 #define CELLSECSIZE 64
00037 #define PORTALSECSIZE 256
00038 #define NUMFILELOC 0x03F
00039 #define ROOTDIRPTRLOC 0x148
00040
00041 class cPortalDat
00042 {
00043
00044 public:
00045
00046 static void LoadStartingInfo ( cAvatar *pcAvatar );
00047 static void LoadItemModel ( cObject *pcObject, DWORD dwModelID, DWORD dwColorID = 0, double dblColorValue = -1 );
00048 static int CalcPalette ( int numPalettes, double palValue = -1 );
00049
00059 static inline void Load_PortalDat( UCHAR* &buf, char file[9] )
00060 {
00061 FILE *inFile;
00062 int read;
00063 UINT rootDirPtr;
00064 UINT filePos, len;
00065 UINT id;
00066
00067 inFile = fopen("portal.dat", "rb");
00068 if (inFile == NULL)
00069 {
00070 UpdateConsole(" Portal.dat: Open failed.\r\n");
00071
00072 }
00073
00074 read = fseek(inFile, ROOTDIRPTRLOC, SEEK_SET);
00075 if (read != 0)
00076 {
00077 UpdateConsole(" Portal.dat: Read error.\r\n");
00078 fclose(inFile);
00079
00080 }
00081
00082 read = fread(&rootDirPtr, sizeof(UINT), 1, inFile);
00083 if (read != 1)
00084 {
00085 UpdateConsole(" Portal.dat: End of file reached.\r\n");
00086 fclose(inFile);
00087
00088 }
00089
00090 id = strtol(file, NULL, 16);
00091
00092 if (!FetchPortalFilePos(inFile, rootDirPtr, id, &filePos, &len))
00093 {
00094 UpdateConsole(" Portal.dat: File not found.\r\n");
00095
00096 }
00097
00098 buf = (UCHAR *)malloc(len);
00099 if (!FetchPortalFile(inFile, filePos, len, buf))
00100 {
00101 free(buf);
00102 fclose(inFile);
00103
00104 }
00105
00106
00107 fclose(inFile);
00108 }
00109
00110 private:
00111
00121 static inline int FetchPortalFilePos(FILE *inFile, UINT dirPos, UINT id, UINT *filePos, UINT *len)
00122 {
00123
00124 UINT dir[PORTALSECSIZE];
00125 UINT i;
00126 UINT numFiles;
00127 int read;
00128
00129 while (1)
00130 {
00131 if (dirPos == 0)
00132 {
00133 UpdateConsole(" Portal.dat: NULL directory entry found.\r\n");
00134 return 0;
00135 }
00136
00137 read = fseek(inFile, dirPos, SEEK_SET);
00138 if (read != 0)
00139 {
00140 UpdateConsole(" Portal.dat: Sector is beyond end of file.\r\n");
00141 return 0;
00142 }
00143
00144 read = fread(dir, sizeof(UINT), PORTALSECSIZE, inFile);
00145 if (read != PORTALSECSIZE)
00146 {
00147 UpdateConsole(" Portal.dat: Sector doesn't contain enough words.\r\n");
00148 return 0;
00149 }
00150
00151 numFiles = dir[NUMFILELOC];
00152 if (numFiles >= NUMFILELOC)
00153 {
00154 UpdateConsole(" Portal.dat: Number of files exceeds directory entries.\r\n");
00155 return 0;
00156 }
00157
00158 i = 0;
00159 while ((i < numFiles) && (id > dir[i * 3 + NUMFILELOC + 1]))
00160 {
00161 i++;
00162 }
00163 if (i < numFiles)
00164 {
00165 if (id == dir[i * 3 + NUMFILELOC + 1])
00166 {
00167 *filePos = dir[i * 3 + NUMFILELOC + 2];
00168 *len = dir[i * 3 + NUMFILELOC + 3];
00169 return 1;
00170 }
00171 }
00172
00173 if (dir[1] == 0)
00174 {
00175 filePos = 0;
00176 len = 0;
00177 return 0;
00178 }
00179 dirPos = dir[i + 1];
00180 }
00181
00182 return 0;
00183 }
00184
00193 static inline int FetchPortalFile(FILE *inFile, UINT filePos, UINT len, UCHAR *buf)
00194 {
00195 int read, doChain;
00196 UINT sec[PORTALSECSIZE];
00197
00198 if (filePos == 0)
00199 {
00200 UpdateConsole(" Portal.dat: Null file pointer found.\r\n");
00201 return 0;
00202 }
00203
00204 doChain = 1;
00205 while (doChain)
00206 {
00207
00208 read = fseek(inFile, filePos, SEEK_SET);
00209 if (read != 0)
00210 {
00211 UpdateConsole(" Portal.dat: Seek failed.\r\n");
00212 return 0;
00213 }
00214 read = fread(sec, sizeof(UINT), PORTALSECSIZE, inFile);
00215 if (read != PORTALSECSIZE)
00216 {
00217 UpdateConsole(" Portal.dat: Sector doesn't contain enough words.\r\n");
00218 return 0;
00219 }
00220
00221 filePos = sec[0] & 0x7FFFFFFF;
00222
00223 if (len > (PORTALSECSIZE - 1) * sizeof(UINT))
00224 {
00225 memcpy(buf, &sec[1], (PORTALSECSIZE - 1) * sizeof(UINT));
00226 buf += (PORTALSECSIZE - 1) * sizeof(UINT);
00227 len -= (PORTALSECSIZE - 1) * sizeof(UINT);
00228 } else {
00229 memcpy(buf, &sec[1], len);
00230 len = 0;
00231 }
00232
00233 if (filePos == 0)
00234 doChain = 0;
00235 }
00236
00237 return 1;
00238 }
00239 };
00240
00241 #endif // #ifndef __DATFILE_H