bool CManager::DeInit()
{
	DELETEOBJECTPARAMS dop;

	// Call DeleteObject on every object we manage from every group
	MAPGROUPLISTS::iterator mglIter;
	for (mglIter = m_GroupLists.begin(); mglIter != m_GroupLists.end(); ++mglIter)
	{
		IDTOOBJECTMAP *ntom;

		// loop through all the objects in this group
		ntom = mglIter->second;
		IDTOOBJECTMAP::iterator ntomIter = ntom->begin();
		while (ntomIter != ntom->end())
		{
			IObject *delObject;
			delObject = ntomIter->second;
			dop.name = delObject->GetName();
			static DWORD msgHash_DeleteObject = CHashString(_T("DeleteObject")).GetUniqueID();
			m_ToolBox->SendMessage(msgHash_DeleteObject, sizeof( DELETEOBJECTPARAMS ), &dop, NULL, NULL );
			ntomIter = ntom->begin();
		}

		// no more objects in the list.
		ntom->clear();
	}

	return true;
}
DWORD CDatabaseManager::OnFindDatabaseByFileName(DWORD size, void *params)
{
	DBFINDDATABASE *dbInfo;
	
	// verify the message we have received is the the right size
	VERIFY_MESSAGE_SIZE(size, sizeof(DBFINDDATABASE));

	// cast params to a DATABASEINFO
	dbInfo = (DBFINDDATABASE *)params;
	if( dbInfo->m_DatabaseFileName == NULL )
	{
		return MSG_ERROR;
	}

	IObject* tmpobj = GetDatabaseByFile( dbInfo->m_DatabaseFileName );

	if( tmpobj == NULL )
	{
		dbInfo->m_bIsFound = false;
		dbInfo->m_DatabaseObjName = NULL;
		return MSG_HANDLED_PROCEED;
	}
	
	dbInfo->m_bIsFound = true;
	dbInfo->m_DatabaseObjName = tmpobj->GetName();

	return MSG_HANDLED_PROCEED;	
}
// add a gui element to the page list
DWORD CGUIPage::OnAddGUIElementToPage(DWORD size, void *params)
{
	OBJECTLIST::iterator olIter;
	GUIPAGEMESSAGE *gpm;
	IObject *guiObject;
	static CHashString managerName(_T("CGUIManager"));
	CGUIManager *manager;

	// make sure message is valid
	VERIFY_MESSAGE_SIZE(size, sizeof(GUIPAGEMESSAGE));

	// downcast parameters to struct we expect
	gpm = (GUIPAGEMESSAGE *)params;

	// ask gui manager for the object to add
	manager = dynamic_cast<CGUIManager*>(EngineGetToolBox()->GetComponent( &managerName ));
	if (!manager)
	{
		EngineGetToolBox()->SetErrorValue(ERR_NULL_POINTER);
		EngineGetToolBox()->Log(LOGERROR, _T("could not cast IComponent from EngineToolBox to CGUIManager\n")); 
		return MSG_ERROR;
	}
	guiObject = manager->GetObject(gpm->name, gpm->compType);
	if(guiObject == NULL)
	{
		EngineGetToolBox()->SetErrorValue(ERR_NULL_POINTER);
		EngineGetToolBox()->Log(LOGERROR, _T("could not get the gui object from the gui manager: %s object not found\n"), gpm->name);
		return MSG_ERROR;
	}

	// check if object is already on list
	for(olIter = m_ObjectList.begin(); olIter != m_ObjectList.end(); olIter++)
	{
		if ( (*olIter)->GetName()->GetUniqueID() == guiObject->GetName()->GetUniqueID() )
		{
			// object is already on page
			return MSG_NOT_HANDLED;
		}
	}
	// now add object to list
	m_ObjectList.push_back(guiObject);
	m_SortedMap.clear();
	IGUIElement *guiElement = dynamic_cast<IGUIElement*>(guiObject);
	guiElement->SetWidthRatio(m_fWidthRatio);
	guiElement->SetHeightRatio(m_fHeightRatio);
	guiElement->SetZoomFactor(m_fZoomFactor);
	guiElement->SetPageOffset(m_iPageOffsetX, m_iPageOffsetY);
	return MSG_HANDLED_STOP;
}
DWORD Cal3DModelManager::OnSaveAllCalCubes(DWORD size, void *param)
{
	static CHashString modelType = _T("Cal3DRenderObject");
	IDTOOBJECTMAP *calMap = GetObjectMap(&modelType);
	if (calMap)
	{
		IDTOOBJECTMAP::iterator objIter = calMap->begin();
		while (objIter != calMap->end())
		{
			IObject *calObj = objIter->second;
			IHashString *objName = calObj->GetName();
			static DWORD msgHash_SaveCalCubes = CHashString(_T("SaveCalCubes")).GetUniqueID();
			m_ToolBox->SendMessage(msgHash_SaveCalCubes, 0, 0, objName, &modelType );
			objIter++;
		}
	}
	return MSG_HANDLED_STOP;
}
bool CVisitNodeDWORD::Visit(IComponent *component, bool bVisitEnter)
{
	IObject *theObject;
	IHashString *objName;

	theObject = dynamic_cast<IObject *>(component);

	// if the object is invalid
	if (!theObject)
	{
		// log error
		return false;
	}

	// get the name of the object
	objName = theObject->GetName();

	// add name to set
	m_DWORDSet->Add(objName);

	return true;
}
DWORD CCoordinateToolManager::OnDeleteObject(DWORD size, void *param)
{
	// This message has its priority raised so that it gets called before the
  	// parent entity's delete object. We MUST to remove coordinate tool
  	// for entity, before its deletion. Otherwise physics will crash.
  	// Is there any better solution, except to catch all DeleteObject messages?
  
  	VERIFY_MESSAGE_SIZE(sizeof(DELETEOBJECTPARAMS), size);
  	DELETEOBJECTPARAMS* dop = (DELETEOBJECTPARAMS*)param;
  
  	IObject * object = FindByParent(dop->name);
  	if (object != NULL)
  	{
  		// Remove coordinate tool for this parent object.
  		DELETEOBJECTPARAMS dop;
  		dop.name = object->GetName();
  		static DWORD msgHash_DeleteObject = CHashString(_T("DeleteObject")).GetUniqueID();
  		m_ToolBox->SendMessage(msgHash_DeleteObject, sizeof(DELETEOBJECTPARAMS), &dop);

		m_CoordinateToolName = NULL;
  	}

	return MSG_HANDLED_PROCEED;
}
bool CWorldVisitor::Visit( IComponent * component, bool bVisitEnter )
{
	IObject *theObject;
	IHashString *name;
	IHashString *parentName;
	IHashString *type;

	StdString parentType;
	StdString childType;

	std::string str;

	theObject = dynamic_cast<IObject *>(component);
	// This shouldn't happen but it does for some odd reason....
	assert(theObject);
	if( theObject == NULL )
	{
		return false;
	}
	name = theObject->GetName();
	parentName = theObject->GetParentName();
	type = theObject->GetComponentType();

	//Check to see if it is a valid object (for object exclusion)
	if( !CheckObject( type->GetString() ) )
	{
		return true;
	}

	else
	{
		if ( name == NULL )
		{
			name = &CHashString(_T("NULL"));
		}	

		if( bVisitEnter == true )
		{
			//if( (m_pArchiver != NULL) && ( _tcscmp( type->GetString(), _T("CPhysicsObject") ) != 0 ) )
			if( (m_pArchiver != NULL) )
			{
				// Start the Node
				m_pArchiver->StartClass( type->GetString() );
				
				// Write out the Unique ID aka Name
				m_pArchiver->Write( name->GetString(), _T("Name") );
				theObject->Serialize( *m_pArchiver );
			}

			// Removal of CPhysShape and changes to CPhysicsObject
			if( _tcscmp( type->GetString(), _T("CPhysicsObject") ) == 0 )
			{
				
				CPhysObjectStruct tmpCPhysObject;
				StdString CPhyShapeFile;
				StdString CPhyShapeFileOld;
				// if it's parent is not a CTerrainSector Object
				if( _tcsstr( name->GetString(), _T("Terrain") ) == NULL )
				{
					static DWORD msgHash_GetModelFileName = CHashString(_T("GetModelFileName")).GetUniqueID();
					m_ToolBox->SendMessage(msgHash_GetModelFileName, sizeof(StdString*), &CPhyShapeFile, parentName, NULL );
					CPhyShapeFile += _T(".psl");
				}
				// CTerrainSector Object
				else
				{
					CPhyShapeFile = _T("maps\\terrain.psl");
				}

				IArchive *PhysObjectIn;
				IArchive *PhysObjectRead;
				CHashString memTypePhysObjectIn(_T("Memory"));
				
				int PhysObjectInMemSize = 1024 * 1024 * sizeof(char);
				char* PhysObjectInMemChunk = new char[PhysObjectInMemSize];
				memset( PhysObjectInMemChunk, 0, PhysObjectInMemSize );

				CREATEARCHIVE caPhysObjectIn;
				caPhysObjectIn.mode = STREAM_MODE_READ;
				caPhysObjectIn.streamData = PhysObjectInMemChunk;
				caPhysObjectIn.streamSize = PhysObjectInMemSize;
				caPhysObjectIn.streamType = &memTypePhysObjectIn;
				static DWORD msgHash_CreateArchive = CHashString(_T("CreateArchive")).GetUniqueID();
				if (m_ToolBox->SendMessage(msgHash_CreateArchive, sizeof(CREATEARCHIVE), &caPhysObjectIn) != MSG_HANDLED)
				{
					return true;
				}
				PhysObjectIn = caPhysObjectIn.archive;

				CREATESTREAM csPhysObjectIn;
				csPhysObjectIn.streamData = caPhysObjectIn.streamData;
				csPhysObjectIn.streamSize = caPhysObjectIn.streamSize;
				csPhysObjectIn.mode = STREAM_MODE_WRITE;
				static DWORD msgHash_CreateStream_Memory = CHashString(_T("CreateStream_Memory")).GetUniqueID();
				if (m_ToolBox->SendMessage(msgHash_CreateStream_Memory, sizeof(CREATESTREAM), &csPhysObjectIn) != MSG_HANDLED)
				{
					return true;
				}
				PhysObjectIn->Init(csPhysObjectIn.openStream);		    

				SERIALIZEOBJECTPARAMS sop;
				sop.name = name;
				sop.archive = PhysObjectIn;
				static DWORD msgHash_SerializeObject = CHashString(_T("SerializeObject")).GetUniqueID();
				m_ToolBox->SendMessage(msgHash_SerializeObject, sizeof(SERIALIZEOBJECTPARAMS), &sop, NULL, NULL);
				PhysObjectIn->Close();
				
				CREATEARCHIVE caPhysObjectRead;
				caPhysObjectRead.mode = STREAM_MODE_WRITE;
				caPhysObjectRead.streamData = PhysObjectInMemChunk;
				caPhysObjectRead.streamSize = PhysObjectInMemSize;
				caPhysObjectRead.streamType = &memTypePhysObjectIn;
				if (m_ToolBox->SendMessage(msgHash_CreateArchive, sizeof(CREATEARCHIVE), &caPhysObjectRead) != MSG_HANDLED)
				{
					return true;
				}
				PhysObjectRead = caPhysObjectRead.archive;

				CREATESTREAM csPhysObjectRead;
				csPhysObjectRead.streamData = caPhysObjectRead.streamData;
				csPhysObjectRead.streamSize = caPhysObjectRead.streamSize;
				csPhysObjectRead.mode = STREAM_MODE_READ;
				if (m_ToolBox->SendMessage(msgHash_CreateStream_Memory, sizeof(CREATESTREAM), &csPhysObjectRead) != MSG_HANDLED)
				{
					return true;
				}
				PhysObjectRead->Init(csPhysObjectRead.openStream);
				PhysObjectRead->Read( tmpCPhysObject.vPosition, _T("pos") );
				PhysObjectRead->Read( tmpCPhysObject.vRotation, _T("rot") );
				PhysObjectRead->Read( tmpCPhysObject.vScale, _T("scale") );
				PhysObjectRead->Read( tmpCPhysObject.fMass, _T("mass") );
				PhysObjectRead->Read( tmpCPhysObject.szDynamic, _T("dynamics") );
				PhysObjectRead->Read( CPhyShapeFileOld, _T("shapeFile") );
				PhysObjectRead->Close();
                
				// Archive the Data Back In
				IArchive *MemArchivePhysObject;	
				CHashString memTypePhysObject(_T("Memory"));
				//int sizePhysObject = 1024 * 1024;
				//char* memchunkPhysObject = new char[sizePhysObject];

				CREATEARCHIVE caPhysObject;
				caPhysObject.mode = STREAM_MODE_WRITE;
				caPhysObject.streamData = PhysObjectInMemChunk;
				caPhysObject.streamSize = PhysObjectInMemSize;
				caPhysObject.streamType = &memTypePhysObject;
				if (m_ToolBox->SendMessage(msgHash_CreateArchive, sizeof(CREATEARCHIVE), &caPhysObject) != MSG_HANDLED)
				{
					return true;
				}
				MemArchivePhysObject = caPhysObject.archive;
				MemArchivePhysObject->Write( tmpCPhysObject.vPosition, _T("pos") );
				MemArchivePhysObject->Write( tmpCPhysObject.vRotation, _T("rot") );
				MemArchivePhysObject->Write( tmpCPhysObject.vScale, _T("scale") );
				MemArchivePhysObject->Write( tmpCPhysObject.fMass, _T("mass") );
				MemArchivePhysObject->Write( tmpCPhysObject.szDynamic, _T("dynamics") );
				MemArchivePhysObject->Write( CPhyShapeFile, _T("shapeFile") );
				
				CREATESTREAM csPhysObject;
				csPhysObject.streamData = caPhysObject.streamData;
				csPhysObject.streamSize = caPhysObject.streamSize;
				csPhysObject.mode = STREAM_MODE_READ;
				if (m_ToolBox->SendMessage(msgHash_CreateStream_Memory, sizeof(CREATESTREAM), &csPhysObject) != MSG_HANDLED)
				{
					return true;
				}
				MemArchivePhysObject->Init(csPhysObject.openStream);

				SERIALIZEOBJECTPARAMS sopPhysObject;
				sopPhysObject.name = name;
				sopPhysObject.archive = MemArchivePhysObject;
				m_ToolBox->SendMessage(msgHash_SerializeObject, sizeof(SERIALIZEOBJECTPARAMS), &sopPhysObject, NULL, NULL);

				MemArchivePhysObject->Close();
				//delete [] memchunkPhysObject;
				//memchunkPhysObject = NULL;
				delete [] PhysObjectInMemChunk;
				PhysObjectInMemChunk = NULL;
			}

			// Model Rename Changes
			if( (m_pReporter != NULL) && ( _tcscmp( type->GetString(), _T("CV3ORenderObject") ) == 0 ) )
			{
				IArchive *MemArchive;
				IArchive *MemArchive2;
				CHashString memType(_T("Memory"));
				memset( m_pMemChunk, '\0', m_iMemSize );

				CREATEARCHIVE ca;
				ca.mode = STREAM_MODE_READ;
				ca.streamData = m_pMemChunk;
				ca.streamSize = m_iMemSize;
				ca.streamType = &memType;
				static DWORD msgHash_CreateArchive = CHashString(_T("CreateArchive")).GetUniqueID();
				if (m_ToolBox->SendMessage(msgHash_CreateArchive, sizeof(CREATEARCHIVE), &ca) != MSG_HANDLED)
				{
					return true;
				}
				MemArchive = ca.archive;

				CREATESTREAM cs;
				cs.streamData = ca.streamData;
				cs.streamSize = ca.streamSize;
				cs.mode = STREAM_MODE_WRITE;
				static DWORD msgHash_CreateStream_Memory = CHashString(_T("CreateStream_Memory")).GetUniqueID();
				if (m_ToolBox->SendMessage(msgHash_CreateStream_Memory, sizeof(CREATESTREAM), &cs) != MSG_HANDLED)
				{
					return true;
				}
				MemArchive->Init(cs.openStream);		    

				SERIALIZEOBJECTPARAMS sop;
				sop.name = name;
				sop.archive = MemArchive;
				static DWORD msgHash_SerializeObject = CHashString(_T("SerializeObject")).GetUniqueID();
				m_ToolBox->SendMessage(msgHash_SerializeObject, sizeof(SERIALIZEOBJECTPARAMS), &sop, NULL, NULL);
				MemArchive->Close();
				
				CREATEARCHIVE ca2;
				ca2.mode = STREAM_MODE_WRITE;
				ca2.streamData = m_pMemChunk;
				ca2.streamSize = m_iMemSize;
				ca2.streamType = &memType;
				if (m_ToolBox->SendMessage(msgHash_CreateArchive, sizeof(CREATEARCHIVE), &ca2) != MSG_HANDLED)
				{
					return true;
				}
				MemArchive2 = ca2.archive;

				CREATESTREAM cs2;
				cs2.streamData = ca2.streamData;
				cs2.streamSize = ca2.streamSize;
				cs2.mode = STREAM_MODE_READ;
				if (m_ToolBox->SendMessage(msgHash_CreateStream_Memory, sizeof(CREATESTREAM), &cs2) != MSG_HANDLED)
				{
					return true;
				}
				MemArchive2->Init(cs2.openStream);

				StdString wszModelFileName;
				Vec3 Position;
				Vec3 Rotation;
				Vec3 Scale;

				MemArchive2->Read( wszModelFileName );
				MemArchive2->Read( Position );
				MemArchive2->Read( Rotation );
				MemArchive2->Read( Scale );
				MemArchive2->Close();
				
				MODELMAP::iterator itr;
				bool bReArchive = false;
				while( 1 )
				{		
					itr = m_vModelReference.find( wszModelFileName );
					// If the Model Refernce already exists
					if( itr != m_vModelReference.end() )
					{
						// If its newfilename is different and it isn't an empty string
						// We Change the file name and set the archive flag
						if( (wszModelFileName != itr->second.m_wszNewFileName) &&
							(itr->second.m_wszNewFileName != StdString("")) )
						{
							wszModelFileName = itr->second.m_wszNewFileName;
							bReArchive = true;
						}
						
						// We've reached a file that has the same exact newfilename, no change neccisary
						else
						{
							break;
						}
					}
					
					
					// We change model name first (up above) and then make sure to add it as a reference and break out
					itr = m_vModelReference.find( wszModelFileName );
					if( itr == m_vModelReference.end() )
					{
						/*
						MODELINFO tmpModelInfo;
						tmpModelInfo.m_wszNewFileName = wszModelFileName;
						m_vModelReference[wszModelFileName] = tmpModelInfo;
						m_iNewEntries++;
						*/
						break;
					}
					
				}
				
				m_szLastV3OFileName = wszModelFileName;

				// Archive the object back out
				if( bReArchive == true )
				{
					IArchive *MemArchiveRE;	
					CHashString memTypeRE(_T("Memory"));
					int sizeRE = 1024 + sizeof(Vec3) * 3;
					char* memchunkRE = new char[sizeRE];

					CREATEARCHIVE caRE;
					caRE.mode = STREAM_MODE_WRITE;
					caRE.streamData = memchunkRE;
					caRE.streamSize = sizeRE;
					caRE.streamType = &memTypeRE;
					static DWORD msgHash_CreateArchive = CHashString(_T("CreateArchive")).GetUniqueID();
					if (m_ToolBox->SendMessage(msgHash_CreateArchive, sizeof(CREATEARCHIVE), &caRE) != MSG_HANDLED)
					{
						return true;
					}
					MemArchiveRE = caRE.archive;
					MemArchiveRE->Write( wszModelFileName );
					MemArchiveRE->Write( Position );
					MemArchiveRE->Write( Rotation );
					MemArchiveRE->Write( Scale );

					CREATESTREAM csRE;
					csRE.streamData = caRE.streamData;
					csRE.streamSize = caRE.streamSize;
					csRE.mode = STREAM_MODE_READ;
					static DWORD msgHash_CreateStream_Memory = CHashString(_T("CreateStream_Memory")).GetUniqueID();
					if (m_ToolBox->SendMessage(msgHash_CreateStream_Memory, sizeof(CREATESTREAM), &csRE) != MSG_HANDLED)
					{
						return true;
					}
					MemArchiveRE->Init(csRE.openStream);

					SERIALIZEOBJECTPARAMS sopRE;
					sopRE.name = name;
					sopRE.archive = MemArchiveRE;
					static DWORD msgHash_SerializeObject = CHashString(_T("SerializeObject")).GetUniqueID();
					m_ToolBox->SendMessage(msgHash_SerializeObject, sizeof(SERIALIZEOBJECTPARAMS), &sopRE, NULL, NULL);

					MemArchiveRE->Close();
					delete [] memchunkRE;
					memchunkRE = NULL;
					m_iV3ONameEdits++;
				}
			}
		}

		// bVisitEnter == false
		else
		{
			if( m_pArchiver != NULL )
			{
				m_pArchiver->EndClass();
			}
		}
	}
	
	return true;
}