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

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;
}
}
}

View File

@@ -45,22 +45,44 @@ SQLStorage sInstanceTemplate(InstanceTemplatesrcfmt, InstanceTemplatedstfmt, "ma
void SQLStorage::Free ()
{
uint32 offset=0;
for (uint32 x=0; x<iNumFields; x++)
if (dst_format[x]==FT_STRING)
uint32 offset = 0;
for (uint32 x = 0; x < iNumFields; ++x)
{
switch(dst_format[x])
{
for (uint32 y=0; y<MaxEntry; y++)
if(pIndex[y])
delete [] *(char**)((char*)(pIndex[y])+offset);
case FT_BYTE:
offset += sizeof(char);
break;
case FT_FLOAT:
offset += sizeof(float);
break;
case FT_LOGIC:
offset += sizeof(bool);
break;
case FT_INT:
offset += sizeof(uint32);
break;
case FT_STRING:
{
for (uint32 y = 0; y < MaxEntry; ++y)
if (pIndex[y])
delete [] *(char**)((char*)(pIndex[y])+offset);
offset += sizeof(char*);
offset += sizeof(char*);
break;
}
case FT_NA:
case FT_NA_BYTE:
break;
case FT_IND:
case FT_SORT:
assert(false && "SQL storage has a field type that does not match what is in the core. Check SQLStorage.cpp or your database.");
break;
default:
assert(false && "Unknown field format character in SQLStorage.cpp");
break;
}
else if (dst_format[x]==FT_LOGIC)
offset += sizeof(bool);
else if (dst_format[x]==FT_BYTE)
offset += sizeof(char);
else
offset += 4;
}
delete [] pIndex;
delete [] data;

View File

@@ -29,7 +29,7 @@ void SQLStorageLoaderBase<T>::convert(uint32 /*field_pos*/, S src, D &dst)
template<class T>
void SQLStorageLoaderBase<T>::convert_str_to_str(uint32 /*field_pos*/, char *src, char *&dst)
{
if(!src)
if (!src)
{
dst = new char[1];
*dst = 0;
@@ -66,24 +66,32 @@ void SQLStorageLoaderBase<T>::storeValue(V value, SQLStorage &store, char *p, in
{
case FT_LOGIC:
subclass->convert(x, value, *((bool*)(&p[offset])) );
offset+=sizeof(bool);
offset += sizeof(bool);
break;
case FT_BYTE:
subclass->convert(x, value, *((char*)(&p[offset])) );
offset+=sizeof(char);
offset += sizeof(char);
break;
case FT_INT:
subclass->convert(x, value, *((uint32*)(&p[offset])) );
offset+=sizeof(uint32);
offset += sizeof(uint32);
break;
case FT_FLOAT:
subclass->convert(x, value, *((float*)(&p[offset])) );
offset+=sizeof(float);
offset += sizeof(float);
break;
case FT_STRING:
subclass->convert_to_str(x, value, *((char**)(&p[offset])) );
offset+=sizeof(char*);
offset += sizeof(char*);
break;
case FT_NA:
case FT_NA_BYTE:
break;
case FT_SORT:
assert(false && "SQL storage has a field type that does not match what is in the core. Check SQLStorage.cpp or your database.");
break;
default:
assert(false && "Unknown field format character in SQLStorage.cpp");
}
}
@@ -95,24 +103,32 @@ void SQLStorageLoaderBase<T>::storeValue(char * value, SQLStorage &store, char *
{
case FT_LOGIC:
subclass->convert_from_str(x, value, *((bool*)(&p[offset])) );
offset+=sizeof(bool);
offset += sizeof(bool);
break;
case FT_BYTE:
subclass->convert_from_str(x, value, *((char*)(&p[offset])) );
offset+=sizeof(char);
offset += sizeof(char);
break;
case FT_INT:
subclass->convert_from_str(x, value, *((uint32*)(&p[offset])) );
offset+=sizeof(uint32);
offset += sizeof(uint32);
break;
case FT_FLOAT:
subclass->convert_from_str(x, value, *((float*)(&p[offset])) );
offset+=sizeof(float);
offset += sizeof(float);
break;
case FT_STRING:
subclass->convert_str_to_str(x, value, *((char**)(&p[offset])) );
offset+=sizeof(char*);
offset += sizeof(char*);
break;
case FT_NA:
case FT_NA_BYTE:
break;
case FT_SORT:
assert(false && "SQL storage has a field type that does not match what is in the core. Check SQLStorage.cpp or your database.");
break;
default:
assert(false && "Unknown field format character in SQLStorage.cpp");
}
}
@@ -121,17 +137,17 @@ void SQLStorageLoaderBase<T>::Load(SQLStorage &store)
{
uint32 maxi;
Field *fields;
QueryResult result = WorldDatabase.PQuery("SELECT MAX(%s) FROM %s", store.entry_field, store.table);
QueryResult result = WorldDatabase.PQuery("SELECT MAX(%s) FROM %s", store.entry_field, store.table);
if(!result)
{
sLog->outError("Error loading %s table (not exist?)\n", store.table);
exit(1); // Stop server at loading non exited table or not accessable table
}
maxi = (*result)[0].GetUInt32()+1;
maxi = (*result)[0].GetUInt32() + 1;
result = WorldDatabase.PQuery("SELECT COUNT(*) FROM %s", store.table);
if(result)
if (result)
{
fields = result->Fetch();
store.RecordCount = fields[0].GetUInt32();
@@ -141,7 +157,7 @@ void SQLStorageLoaderBase<T>::Load(SQLStorage &store)
result = WorldDatabase.PQuery("SELECT * FROM %s", store.table);
if(!result)
if (!result)
{
sLog->outError("%s table is empty!\n", store.table);
store.RecordCount = 0;
@@ -151,54 +167,59 @@ void SQLStorageLoaderBase<T>::Load(SQLStorage &store)
uint32 recordsize = 0;
uint32 offset = 0;
if(store.iNumFields != result->GetFieldCount())
if (store.iNumFields != result->GetFieldCount())
{
store.RecordCount = 0;
sLog->outError("Error in %s table, probably sql file format was updated (there should be %d fields in sql).\n", store.table, store.iNumFields);
exit(1); // Stop server at loading broken or non-compatible table.
}
//get struct size
uint32 sc=0;
uint32 bo=0;
uint32 bb=0;
for (uint32 x=0; x< store.iNumFields; x++)
if(store.dst_format[x]==FT_STRING)
++sc;
else if (store.dst_format[x]==FT_LOGIC)
++bo;
else if (store.dst_format[x]==FT_BYTE)
++bb;
recordsize=(store.iNumFields-sc-bo-bb)*4+sc*sizeof(char*)+bo*sizeof(bool)+bb*sizeof(char);
char** newIndex = new char*[maxi];
memset(newIndex, 0, maxi * sizeof(char*));
char** newIndex=new char*[maxi];
memset(newIndex,0,maxi*sizeof(char*));
char * _data= new char[store.RecordCount *recordsize];
uint32 count=0;
char * _data = new char[store.RecordCount *recordsize];
uint32 count = 0;
do
{
fields = result->Fetch();
char *p=(char*)&_data[recordsize*count];
newIndex[fields[0].GetUInt32()]=p;
char *p = (char*)&_data[recordsize*count];
newIndex[fields[0].GetUInt32()] = p;
offset=0;
for (uint32 x = 0; x < store.iNumFields; x++)
offset = 0;
for (uint32 x = 0; x < store.iNumFields; ++x)
switch(store.src_format[x])
{
case FT_LOGIC:
storeValue((bool)(fields[x].GetUInt32() > 0), store, p, x, offset); break;
storeValue((bool)(fields[x].GetUInt32() > 0), store, p, x, offset);
break;
case FT_BYTE:
storeValue((char)fields[x].GetUInt8(), store, p, x, offset); break;
storeValue((char)fields[x].GetUInt8(), store, p, x, offset);
break;
case FT_INT:
storeValue((uint32)fields[x].GetUInt32(), store, p, x, offset); break;
storeValue((uint32)fields[x].GetUInt32(), store, p, x, offset);
break;
case FT_FLOAT:
storeValue((float)fields[x].GetFloat(), store, p, x, offset); break;
storeValue((float)fields[x].GetFloat(), store, p, x, offset);
break;
case FT_STRING:
storeValue((char*)fields[x].GetCString(), store, p, x, offset); break;
storeValue((char*)fields[x].GetCString(), store, p, x, offset);
break;
case FT_NA:
case FT_NA_BYTE:
break;
case FT_IND:
case FT_SORT:
assert(false && "SQL storage has a field type that does not match what is in the core. Check SQLStorage.cpp or your database.");
break;
default:
assert(false && "Unknown field format character in SQLStorage.cpp");
}
++count;
}while( result->NextRow() );
}
while( result->NextRow() );
delete result;
store.pIndex = newIndex;
store.MaxEntry = maxi;