if ( QFile::exists( "categories.db" ) && !QFile::remove( "categories.db" ) ) { socket->sysMessage( "Unable to remove existing categories.db." ); return; }  cSQLiteDriver driver; driver.setDatabaseName( "categories.db" );  if ( !driver.open() ) { socket->sysMessage( tr( "Unable to open categories.db in your wolfpack directory." ) ); return; }  Console::instance()->log( LOG_MESSAGE, tr( "Exporting definitions to %1.\n" ).arg( "categories.db" ) );  try {  driver.exec( "CREATE TABLE items (\ id INTEGER PRIMARY KEY,\ name varchar(255) NULL,\ parent int NOT NULL,\ artid int,\ color int,\ addid varchar(255)\ );" );  driver.exec( "CREATE TABLE categories (\ id INTEGER PRIMARY KEY,\ name varchar(255) NULL,\ parent int NOT NULL,\ type int\ );" );  driver.exec( "CREATE TABLE locationcategories (\ id INTEGER PRIMARY KEY,\ name varchar(255) NULL,\ parent int NOT NULL,\ type int\ );" );  driver.exec( "CREATE TABLE locations (\ id INTEGER PRIMARY KEY,\ name varchar(255) NULL,\ parent INT NOT NULL,\ posx INT NOT NULL,\ posy INT NOT NULL,\ posz INT NOT NULL,\ posmap INT NOT NULL,\ location varchar(255)\ );" );  driver.exec( "CREATE TABLE npcs (\ id INTEGER PRIMARY KEY,\ name varchar(255) NULL,\ parent int NOT NULL,\ bodyid int,\ skin int,\ addid varchar(255)\ );" );  driver.exec( "CREATE TABLE npccategories (\ id INTEGER PRIMARY KEY,\ name varchar(255) NULL,\ parent int NOT NULL,\ type int\ );" );  driver.exec( "CREATE TABLE npcequipment (\ id int NOT NULL,\ artid int NOT NULL,\ layer int NOT NULL,\ color int NOT NULL\ );" );  driver.exec( "CREATE TABLE multis (\ id INTEGER PRIMARY KEY,\ name varchar(255) NULL,\ addid varchar(255) NULL,\ multiid int NOT NULL\ );" );  unsigned int lastcategory = 0; QMap<QString, unsigned int> categories; QMap<QString, unsigned int>::iterator categoriesIt;  QStringList sections = Definitions::instance()->getSections( WPDT_ITEM ); QStringList::const_iterator sectionIt; QMap<QCString, QString> item;  for ( sectionIt = sections.begin(); sectionIt != sections.end(); ++sectionIt ) { const cElement *element = Definitions::instance()->getDefinition( WPDT_ITEM, *sectionIt );  item.clear(); item.insert( "name", QString::null ); item.insert( "color", "0" ); item.insert( "dispid", "0" ); item.insert( "category", "0" ); item.insert( "categoryname", QString::null );  processItem( item, element );  QString category = item["categoryname"];  if ( category.isNull() ) { continue; }   int pos = category.findRev( '\\' ); if ( pos != -1 ) { category = category.left( pos ); }   if ( !categories.contains( category ) ) { ensureCategory( categories, lastcategory, category ); item["category"] = QString::number( lastcategory ); } else { item["category"] = QString::number( categories[category] ); }   QString categoryname = item["categoryname"]; if ( pos != -1 ) { item["name"] = categoryname.right( categoryname.length() - ( pos + 1 ) ); } else { item["name"] = categoryname; }   QString section = *sectionIt; QString sql = QString( "INSERT INTO items VALUES(NULL,'%1',%2,%3,%4,'%5');" ) .arg( item["name"].replace( "'", "''" ) ) .arg( item["category"] ) .arg( item["dispid"] ) .arg( item["color"] ) .arg( section.replace( "'", "''" ) ); driver.exec( sql.utf8() ); }   for ( categoriesIt = categories.begin(); categoriesIt != categories.end(); ++categoriesIt ) { unsigned int parent = 0; int pos = categoriesIt.key().findRev( '\\' ); if ( pos != -1 ) { QString parentName = categoriesIt.key().left( pos ); if ( categories.contains( parentName ) ) { parent = categories[parentName]; } }  QString name = categoriesIt.key(); name = name.right( name.length() - ( pos + 1 ) );  QString sql = QString( "INSERT INTO categories VALUES(%1,'%2',%3,0);" ) .arg( categoriesIt.data() ) .arg( name.replace( "'", "''" ) ) .arg( parent ); driver.exec( sql.utf8() ); }  categories.clear(); lastcategory = 0;  sections = Definitions::instance()->getSections( WPDT_LOCATION ); for ( sectionIt = sections.begin(); sectionIt != sections.end(); ++sectionIt ) { const cElement *element = Definitions::instance()->getDefinition( WPDT_LOCATION, *sectionIt );  QString category = element->getAttribute( "category" ); if ( category.isNull() ) { continue; }  int pos = category.findRev( '\\' );  if ( pos == -1 ) { continue; }  QString name = category.right( category.length() - ( pos + 1 ) ); category = category.left( pos );   unsigned int categoryId; if ( !categories.contains( category ) ) { ensureCategory( categories, lastcategory, category ); categoryId = lastcategory; } else { categoryId = categories[category]; }  Coord coord; parseCoordinates( element->text(), coord ); QString id = *sectionIt;  QString sql = QString( "INSERT INTO locations VALUES(NULL,'%1',%2,%3,%4,%5,%6,'%7');" ) .arg( name.replace( "'", "''" ) ) .arg( categoryId ) .arg( coord.x ) .arg( coord.y ) .arg( coord.z ) .arg( coord.map ) .arg( id.replace( "'", "''" ) );  driver.exec( sql.utf8() ); }   for ( categoriesIt = categories.begin(); categoriesIt != categories.end(); ++categoriesIt ) { unsigned int parent = 0; int pos = categoriesIt.key().findRev( '\\' ); if ( pos != -1 ) { QString parentName = categoriesIt.key().left( pos ); if ( categories.contains( parentName ) ) { parent = categories[parentName]; } }  QString name = categoriesIt.key(); name = name.right( name.length() - ( pos + 1 ) );  QString sql = QString( "INSERT INTO locationcategories VALUES(%1,'%2',%3,0);" ) .arg( categoriesIt.data() ) .arg( name.replace( "'", "''" ) ) .arg( parent ); driver.exec( sql.utf8() ); }   lastcategory = 0; categories.clear(); sections = Definitions::instance()->getSections( WPDT_NPC );  EquipmentContainer equipment;  for ( sectionIt = sections.begin(); sectionIt != sections.end(); ++sectionIt ) { const cElement *element = Definitions::instance()->getDefinition( WPDT_NPC, *sectionIt );  equipment.clear(); item.clear(); item.insert( "name", QString::null ); item.insert( "skin", "0" ); item.insert( "bodyid", "0" ); item.insert( "category", "0" ); item.insert( "categoryname", QString::null );  processNpc( item, element, equipment );  QString category = item["categoryname"];  if ( category.isNull() ) { continue; }   int pos = category.findRev( '\\' ); if ( pos != -1 ) { category = category.left( pos ); }   if ( !categories.contains( category ) ) { ensureCategory( categories, lastcategory, category ); item["category"] = QString::number( lastcategory ); } else { item["category"] = QString::number( categories[category] ); }  QString categoryname = item["categoryname"]; if ( pos != -1 ) { item["name"] = categoryname.right( categoryname.length() - ( pos + 1 ) ); } else { item["name"] = categoryname; }   QString section = *sectionIt; QString sql = QString( "INSERT INTO npcs VALUES(NULL,'%1',%2,%3,%4,'%5');" ) .arg( item["name"].replace( "'", "''" ) ) .arg( item["category"] ) .arg( item["bodyid"] ) .arg( item["skin"] ) .arg( section.replace( "'", "''" ) ); driver.exec( sql.utf8() );  int lastInsertId = driver.lastInsertId();  EquipmentContainer::iterator eIt; for ( eIt = equipment.begin(); eIt != equipment.end(); ++eIt ) { QString sql = QString( "INSERT INTO npcequipment VALUES(%1,%2,%3,%4);" ) .arg( lastInsertId ) .arg( eIt.data()["anim"].toInt() ) .arg( eIt.key() ) .arg( eIt.data()["color"].toInt() ); driver.exec( sql.utf8() ); } }   for ( categoriesIt = categories.begin(); categoriesIt != categories.end(); ++categoriesIt ) { unsigned int parent = 0; int pos = categoriesIt.key().findRev( '\\' ); if ( pos != -1 ) { QString parentName = categoriesIt.key().left( pos ); if ( categories.contains( parentName ) ) { parent = categories[parentName]; } }  QString name = categoriesIt.key(); name = name.right( name.length() - ( pos + 1 ) );  QString sql = QString( "INSERT INTO npccategories VALUES(%1,'%2',%3,0);" ) .arg( categoriesIt.data() ) .arg( name.replace( "'", "''" ) ) .arg( parent ); driver.exec( sql.utf8() ); }  sections = Definitions::instance()->getSections( WPDT_MULTI ); for ( sectionIt = sections.begin(); sectionIt != sections.end(); ++sectionIt ) { const cElement *element = Definitions::instance()->getDefinition( WPDT_MULTI, *sectionIt );  item.clear(); item.insert( "name", QString::null ); item.insert( "dispid", "0" );  processMulti( item, element );  if ( item["name"].isNull() || item["dispid"].toInt() < 0x4000 ) { continue; }   QString section = *sectionIt; QString sql = QString( "INSERT INTO multis VALUES(NULL,'%1','%2',%3);" ) .arg( item["name"].replace( "'", "''" ) ) .arg( section.replace( "'", "''" ) ) .arg( item["dispid"].toInt() ); driver.exec( sql.utf8() ); }  socket->sysMessage( "Finished exporting definitions to categories.db." ); } catch ( const QString& e ) { socket->sysMessage( e ); } catch ( const wpException& e ) { socket->sysMessage( e.error() ); }  driver.close();
cDefinitionExporter exporter; exporter.setSocket(socket); exporter.generate("categories.db"); return;
void commandExportDefinitions( cUOSocket* socket, const QString& /*command*/, const QStringList& /*args*/ ) throw(){	if ( QFile::exists( "categories.db" ) && !QFile::remove( "categories.db" ) )	{		socket->sysMessage( "Unable to remove existing categories.db." );		return;	}	cSQLiteDriver driver;	driver.setDatabaseName( "categories.db" );	if ( !driver.open() )	{		socket->sysMessage( tr( "Unable to open categories.db in your wolfpack directory." ) );		return;	}	Console::instance()->log( LOG_MESSAGE, tr( "Exporting definitions to %1.\n" ).arg( "categories.db" ) );	try	{		// Create Tables		driver.exec( "CREATE TABLE items (\			id INTEGER PRIMARY KEY,\			name varchar(255) NULL,\			parent int NOT NULL,\			artid int,\			color int,\			addid varchar(255)\		);" );		driver.exec( "CREATE TABLE categories (\			id INTEGER PRIMARY KEY,\			name varchar(255) NULL,\			parent int NOT NULL,\			type int\		);" );		driver.exec( "CREATE TABLE locationcategories (\			id INTEGER PRIMARY KEY,\			name varchar(255) NULL,\			parent int NOT NULL,\			type int\		);" );		driver.exec( "CREATE TABLE locations (\			id INTEGER PRIMARY KEY,\			name varchar(255) NULL,\			parent INT NOT NULL,\			posx INT NOT NULL,\			posy INT NOT NULL,\			posz INT NOT NULL,\			posmap INT NOT NULL,\			location varchar(255)\		);" );		driver.exec( "CREATE TABLE npcs (\			id INTEGER PRIMARY KEY,\			name varchar(255) NULL,\			parent int NOT NULL,\			bodyid int,\			skin int,\			addid varchar(255)\		);" );		driver.exec( "CREATE TABLE npccategories (\			id INTEGER PRIMARY KEY,\			name varchar(255) NULL,\			parent int NOT NULL,\			type int\		);" );		driver.exec( "CREATE TABLE npcequipment (\			id int NOT NULL,\			artid int NOT NULL,\			layer int NOT NULL,\			color int NOT NULL\		);" );		driver.exec( "CREATE TABLE multis (\			id INTEGER PRIMARY KEY,\			name varchar(255) NULL,\			addid varchar(255) NULL,\			multiid int NOT NULL\		);" );		unsigned int lastcategory = 0;		QMap<QString, unsigned int> categories;		QMap<QString, unsigned int>::iterator categoriesIt;		QStringList sections = Definitions::instance()->getSections( WPDT_ITEM );		QStringList::const_iterator sectionIt;		QMap<QCString, QString> item;		for ( sectionIt = sections.begin(); sectionIt != sections.end(); ++sectionIt )		{			const cElement *element = Definitions::instance()->getDefinition( WPDT_ITEM, *sectionIt );			item.clear();			item.insert( "name", QString::null );			item.insert( "color", "0" );			item.insert( "dispid", "0" );			item.insert( "category", "0" );			item.insert( "categoryname", QString::null );			processItem( item, element );			QString category = item["categoryname"];			if ( category.isNull() )			{				continue;			}			// Strip out the portion after the last slash			int pos = category.findRev( '\\' );			if ( pos != -1 )			{				category = category.left( pos );			}			// Create an id for the category			if ( !categories.contains( category ) )			{				ensureCategory( categories, lastcategory, category );				item["category"] = QString::number( lastcategory );			}			else			{				item["category"] = QString::number( categories[category] );			}			// See if there has been a custom name definition			QString categoryname = item["categoryname"];			if ( pos != -1 )			{				item["name"] = categoryname.right( categoryname.length() - ( pos + 1 ) );			}			else			{				item["name"] = categoryname;			}			// Insert the item into the table.			QString section = *sectionIt;			QString sql = QString( "INSERT INTO items VALUES(NULL,'%1',%2,%3,%4,'%5');" )			.arg( item["name"].replace( "'", "''" ) )			.arg( item["category"] )			.arg( item["dispid"] )			.arg( item["color"] )			.arg( section.replace( "'", "''" ) );			driver.exec( sql.utf8() );		}		// Ensure that all categories are in the list		for ( categoriesIt = categories.begin(); categoriesIt != categories.end(); ++categoriesIt )		{			unsigned int parent = 0;			int pos = categoriesIt.key().findRev( '\\' );			if ( pos != -1 )			{				QString parentName = categoriesIt.key().left( pos );				if ( categories.contains( parentName ) )				{					parent = categories[parentName];				}			}			QString name = categoriesIt.key();			name = name.right( name.length() - ( pos + 1 ) );			QString sql = QString( "INSERT INTO categories VALUES(%1,'%2',%3,0);" )			.arg( categoriesIt.data() )			.arg( name.replace( "'", "''" ) )			.arg( parent );			driver.exec( sql.utf8() );		}		categories.clear();		lastcategory = 0;		sections = Definitions::instance()->getSections( WPDT_LOCATION );		for ( sectionIt = sections.begin(); sectionIt != sections.end(); ++sectionIt )		{			const cElement *element = Definitions::instance()->getDefinition( WPDT_LOCATION, *sectionIt );			QString category = element->getAttribute( "category" );			if ( category.isNull() )			{				continue;			}			int pos = category.findRev( '\\' );			if ( pos == -1 )			{				continue;			}			QString name = category.right( category.length() - ( pos + 1 ) );			category = category.left( pos );			// Create an id for the category			unsigned int categoryId;			if ( !categories.contains( category ) )			{				ensureCategory( categories, lastcategory, category );				categoryId = lastcategory;			}			else			{				categoryId = categories[category];			}			Coord coord;			parseCoordinates( element->text(), coord );			QString id = *sectionIt;			QString sql = QString( "INSERT INTO locations VALUES(NULL,'%1',%2,%3,%4,%5,%6,'%7');" )			.arg( name.replace( "'", "''" ) )			.arg( categoryId )			.arg( coord.x )			.arg( coord.y )			.arg( coord.z )			.arg( coord.map )			.arg( id.replace( "'", "''" ) );			driver.exec( sql.utf8() );		}		// Ensure that all categories are in the list		for ( categoriesIt = categories.begin(); categoriesIt != categories.end(); ++categoriesIt )		{			unsigned int parent = 0;			int pos = categoriesIt.key().findRev( '\\' );			if ( pos != -1 )			{				QString parentName = categoriesIt.key().left( pos );				if ( categories.contains( parentName ) )				{					parent = categories[parentName];				}			}			QString name = categoriesIt.key();			name = name.right( name.length() - ( pos + 1 ) );			QString sql = QString( "INSERT INTO locationcategories VALUES(%1,'%2',%3,0);" )			.arg( categoriesIt.data() )			.arg( name.replace( "'", "''" ) )			.arg( parent );			driver.exec( sql.utf8() );		}		// Process NPCS		lastcategory = 0;		categories.clear();		sections = Definitions::instance()->getSections( WPDT_NPC );		EquipmentContainer equipment;		for ( sectionIt = sections.begin(); sectionIt != sections.end(); ++sectionIt )		{			const cElement *element = Definitions::instance()->getDefinition( WPDT_NPC, *sectionIt );			equipment.clear();			item.clear();			item.insert( "name", QString::null );			item.insert( "skin", "0" );			item.insert( "bodyid", "0" );			item.insert( "category", "0" );			item.insert( "categoryname", QString::null );			processNpc( item, element, equipment );			QString category = item["categoryname"];			if ( category.isNull() )			{				continue;			}			// Strip out the portion after the last slash			int pos = category.findRev( '\\' );			if ( pos != -1 )			{				category = category.left( pos );			}			// Create an id for the category			if ( !categories.contains( category ) )			{				ensureCategory( categories, lastcategory, category );				item["category"] = QString::number( lastcategory );			}			else			{				item["category"] = QString::number( categories[category] );			}			QString categoryname = item["categoryname"];			if ( pos != -1 )			{				item["name"] = categoryname.right( categoryname.length() - ( pos + 1 ) );			}			else			{				item["name"] = categoryname;			}			// Insert the item into the table.			QString section = *sectionIt;			QString sql = QString( "INSERT INTO npcs VALUES(NULL,'%1',%2,%3,%4,'%5');" )			.arg( item["name"].replace( "'", "''" ) )			.arg( item["category"] )			.arg( item["bodyid"] )			.arg( item["skin"] )			.arg( section.replace( "'", "''" ) );			driver.exec( sql.utf8() );			int lastInsertId = driver.lastInsertId();			EquipmentContainer::iterator eIt;			for ( eIt = equipment.begin(); eIt != equipment.end(); ++eIt )			{				QString sql = QString( "INSERT INTO npcequipment VALUES(%1,%2,%3,%4);" )				.arg( lastInsertId )				.arg( eIt.data()["anim"].toInt() )				.arg( eIt.key() )				.arg( eIt.data()["color"].toInt() );				driver.exec( sql.utf8() );			}		}		// Ensure that all categories are in the list		for ( categoriesIt = categories.begin(); categoriesIt != categories.end(); ++categoriesIt )		{			unsigned int parent = 0;			int pos = categoriesIt.key().findRev( '\\' );			if ( pos != -1 )			{				QString parentName = categoriesIt.key().left( pos );				if ( categories.contains( parentName ) )				{					parent = categories[parentName];				}			}			QString name = categoriesIt.key();			name = name.right( name.length() - ( pos + 1 ) );			QString sql = QString( "INSERT INTO npccategories VALUES(%1,'%2',%3,0);" )			.arg( categoriesIt.data() )			.arg( name.replace( "'", "''" ) )			.arg( parent );			driver.exec( sql.utf8() );		}		sections = Definitions::instance()->getSections( WPDT_MULTI );		for ( sectionIt = sections.begin(); sectionIt != sections.end(); ++sectionIt )		{			const cElement *element = Definitions::instance()->getDefinition( WPDT_MULTI, *sectionIt );			item.clear();			item.insert( "name", QString::null );			item.insert( "dispid", "0" );			processMulti( item, element );			if ( item["name"].isNull() || item["dispid"].toInt() < 0x4000 )			{				continue;			}			// Insert the item into the table.			QString section = *sectionIt;			QString sql = QString( "INSERT INTO multis VALUES(NULL,'%1','%2',%3);" )			.arg( item["name"].replace( "'", "''" ) )			.arg( section.replace( "'", "''" ) )			.arg( item["dispid"].toInt() );			driver.exec( sql.utf8() );		}		socket->sysMessage( "Finished exporting definitions to categories.db." );	}	catch ( const QString& e )	{		socket->sysMessage( e );	}	catch ( const wpException& e )	{		socket->sysMessage( e.error() );	}	driver.close();}