Core/Storage: Add some sanity checks and better define some SQL and DBC

storage format field types.
* Also LOTS of whitespace cleanup
* Some Trinity code style applied
* Some if / else if -> switch / case
* Original ideal for patch by Vinolentus
This commit is contained in:
runningnak3d
2011-03-18 09:36:59 -06:00
parent 3384dc3a24
commit 171268a565
3 changed files with 201 additions and 124 deletions
+102 -68
View File
@@ -41,7 +41,7 @@ bool DBCFileLoader::Load(const char *filename, const char *fmt)
if (!f)
return false;
if (fread(&header,4,1,f) != 1) // Number of records
if (fread(&header, 4, 1, f) != 1) // Number of records
{
fclose(f);
return false;
@@ -56,7 +56,7 @@ bool DBCFileLoader::Load(const char *filename, const char *fmt)
return false;
}
if (fread(&recordCount,4,1,f) != 1) // Number of records
if (fread(&recordCount, 4, 1, f) != 1) // Number of records
{
fclose(f);
return false;
@@ -64,7 +64,7 @@ bool DBCFileLoader::Load(const char *filename, const char *fmt)
EndianConvert(recordCount);
if (fread(&fieldCount,4,1,f) != 1) // Number of fields
if (fread(&fieldCount, 4, 1, f) != 1) // Number of fields
{
fclose(f);
return false;
@@ -72,7 +72,7 @@ bool DBCFileLoader::Load(const char *filename, const char *fmt)
EndianConvert(fieldCount);
if (fread(&recordSize,4,1,f) != 1) // Size of a record
if (fread(&recordSize, 4, 1, f) != 1) // Size of a record
{
fclose(f);
return false;
@@ -80,7 +80,7 @@ bool DBCFileLoader::Load(const char *filename, const char *fmt)
EndianConvert(recordSize);
if (fread(&stringSize,4,1,f) != 1) // String size
if (fread(&stringSize, 4, 1, f) != 1) // String size
{
fclose(f);
return false;
@@ -90,19 +90,19 @@ bool DBCFileLoader::Load(const char *filename, const char *fmt)
fieldsOffset = new uint32[fieldCount];
fieldsOffset[0] = 0;
for (uint32 i = 1; i < fieldCount; i++)
for (uint32 i = 1; i < fieldCount; ++i)
{
fieldsOffset[i] = fieldsOffset[i - 1];
if (fmt[i - 1] == 'b' || fmt[i - 1] == 'X') // byte fields
fieldsOffset[i] += 1;
fieldsOffset[i] += sizeof(uint8);
else // 4 byte fields (int32/float/strings)
fieldsOffset[i] += 4;
fieldsOffset[i] += sizeof(uint32);
}
data = new unsigned char[recordSize*recordCount+stringSize];
data = new unsigned char[recordSize * recordCount + stringSize];
stringTable = data + recordSize*recordCount;
if (fread(data,recordSize*recordCount+stringSize,1,f) != 1)
if (fread(data, recordSize * recordCount + stringSize, 1, f) != 1)
{
fclose(f);
return false;
@@ -125,33 +125,41 @@ DBCFileLoader::~DBCFileLoader()
DBCFileLoader::Record DBCFileLoader::getRecord(size_t id)
{
assert(data);
return Record(*this, data + id*recordSize);
return Record(*this, data + id * recordSize);
}
uint32 DBCFileLoader::GetFormatRecordSize(const char * format,int32* index_pos)
uint32 DBCFileLoader::GetFormatRecordSize(const char * format, int32* index_pos)
{
uint32 recordsize = 0;
int32 i = -1;
for (uint32 x=0; format[x]; ++x)
for (uint32 x = 0; format[x]; ++x)
{
switch(format[x])
{
case FT_FLOAT:
recordsize += sizeof(float);
break;
case FT_INT:
recordsize+=4;
recordsize += sizeof(uint32);
break;
case FT_STRING:
recordsize+=sizeof(char*);
recordsize += sizeof(char*);
break;
case FT_SORT:
i=x;
i = x;
break;
case FT_IND:
i=x;
recordsize+=4;
i = x;
recordsize += sizeof(uint32);
break;
case FT_BYTE:
recordsize += 1;
recordsize += sizeof(uint8);
break;
case FT_LOGIC:
assert(false && "Attempted to load DBC files that do not have field types that match what is in the core. Check DBCfmt.h or your DBC files.");
break;
default:
assert(false && "Unknown field format character in DBCfmt.h");
break;
}
}
@@ -181,16 +189,16 @@ char* DBCFileLoader::AutoProduceData(const char* format, uint32& records, char**
//get struct size and index pos
int32 i;
uint32 recordsize=GetFormatRecordSize(format,&i);
uint32 recordsize = GetFormatRecordSize(format, &i);
if (i>=0)
if (i >= 0)
{
uint32 maxi=0;
uint32 maxi = 0;
//find max index
for (uint32 y=0; y<recordCount; y++)
for (uint32 y = 0; y < recordCount; ++y)
{
uint32 ind=getRecord(y).getUInt (i);
if(ind>maxi)maxi=ind;
uint32 ind = getRecord(y).getUInt (i);
if (indi > maxi)maxi = ind;
}
// If higher index avalible from sql - use it instead of dbcs
@@ -198,47 +206,57 @@ char* DBCFileLoader::AutoProduceData(const char* format, uint32& records, char**
maxi = sqlHighestIndex;
++maxi;
records=maxi;
indexTable=new ptr[maxi];
memset(indexTable,0,maxi*sizeof(ptr));
records = maxi;
indexTable = new ptr[maxi];
memset(indexTable, 0, imaxi * sizeof(ptr));
}
else
{
records = recordCount + sqlRecordCount;
indexTable = new ptr[recordCount+ sqlRecordCount];
indexTable = new ptr[recordCount + sqlRecordCount];
}
char* dataTable= new char[(recordCount + sqlRecordCount)*recordsize];
char* dataTable = new char[(recordCount + sqlRecordCount) * recordsize];
uint32 offset=0;
uint32 offset = 0;
for (uint32 y =0; y<recordCount; ++y)
for (uint32 y = 0; y < recordCount; ++y)
{
if(i>=0)
indexTable[getRecord(y).getUInt(i)]=&dataTable[offset];
if (i >= 0)
indexTable[getRecord(y).getUInt(i)] = &dataTable[offset];
else
indexTable[y]=&dataTable[offset];
indexTable[y] = &dataTable[offset];
for (uint32 x=0; x<fieldCount; x++)
for (uint32 x=0; x < fieldCount; ++x)
{
switch(format[x])
{
case FT_FLOAT:
*((float*)(&dataTable[offset]))=getRecord(y).getFloat(x);
offset+=4;
*((float*)(&dataTable[offset])) = getRecord(y).getFloat(x);
offset += sizeof(float);
break;
case FT_IND:
case FT_INT:
*((uint32*)(&dataTable[offset]))=getRecord(y).getUInt(x);
offset+=4;
*((uint32*)(&dataTable[offset])) = getRecord(y).getUInt(x);
offset += sizeof(uint32);
break;
case FT_BYTE:
*((uint8*)(&dataTable[offset]))=getRecord(y).getUInt8(x);
offset+=1;
*((uint8*)(&dataTable[offset])) = getRecord(y).getUInt8(x);
offset += sizeof(uint8);
break;
case FT_STRING:
*((char**)(&dataTable[offset]))=NULL; // will be replaces non-empty or "" strings in AutoProduceStrings
offset+=sizeof(char*);
*((char**)(&dataTable[offset])) = NULL; // will replace non-empty or "" strings in AutoProduceStrings
offset += sizeof(char*);
break;
case FT_LOGIC:
assert(false && "Attempted to load DBC files that do not have field types that match what is in the core. Check DBCfmt.h or your DBC files.");
break;
case FT_NA:
case FT_NA_BYTE:
case FT_SORT:
break;
default:
assert(false && "Unknown field format character in DBCfmt.h");
break;
}
}
@@ -251,37 +269,53 @@ char* DBCFileLoader::AutoProduceData(const char* format, uint32& records, char**
char* DBCFileLoader::AutoProduceStrings(const char* format, char* dataTable)
{
if (strlen(format)!=fieldCount)
if (strlen(format) != fieldCount)
return NULL;
char* stringPool= new char[stringSize];
memcpy(stringPool,stringTable,stringSize);
char* stringPool = new char[stringSize];
memcpy(stringPool, stringTable, stringSize);
uint32 offset=0;
uint32 offset = 0;
for (uint32 y =0; y<recordCount; y++)
for (uint32 y = 0; y < recordCount; ++y)
{
for (uint32 x=0; x<fieldCount; x++)
switch(format[x])
for (uint32 x = 0; x < fieldCount; ++x)
{
case FT_FLOAT:
case FT_IND:
case FT_INT:
offset+=4;
break;
case FT_BYTE:
offset+=1;
break;
case FT_STRING:
// fill only not filled entries
char** slot = (char**)(&dataTable[offset]);
if(!*slot || !**slot)
switch(format[x])
{
case FT_FLOAT:
offset += sizeof(float);
break;
case FT_IND:
case FT_INT:
offset += sizeof(uint32);
break;
case FT_BYTE:
offset += sizeof(uint8);
break;
case FT_STRING:
{
const char * st = getRecord(y).getString(x);
*slot=stringPool+(st-(const char*)stringTable);
}
offset+=sizeof(char*);
break;
// fill only not filled entries
char** slot = (char**)(&dataTable[offset]);
if (!*slot || !**slot)
{
const char * st = getRecord(y).getString(x);
*slot=stringPool+(st-(const char*)stringTable);
}
offset += sizeof(char*);
break;
}
case FT_LOGIC:
assert(false && "Attempted to load DBC files that do not have field types that match what is in the core. Check DBCfmt.h or your DBC files.");
break;
case FT_NA:
case FT_NA_BYTE:
case FT_SORT:
break;
default:
assert(false && "Unknown field format character in DBCfmt.h");
break;
}
}
}