CBaseObject* CObjectConverterNative::Convert(CObjectSource* pcSource, char* szObjectName)
{
	CChunkFileFile				cFile;
	CBaseObject*				pvObject;
	CFileBasic					cFileBasic;
	CObjectGraphDeserialiser	cGraphDeserialiser;
	CObjectReader*				pcReader;
	CObjectAllocator			cAllocator;

	//This if statement should be calling a virtual method instead.
	pcReader = pcSource->GetReader();
	if (!pcReader)
	{
		return NULL;
	}

	mcDependentObjects.Kill();
	mcDependentObjects.Init();

	cAllocator.Init(&gcObjects);
	cGraphDeserialiser.Init(pcReader, TRUE,  &cAllocator, &mcDependentObjects, gcObjects.GetMemory());
	pvObject = cGraphDeserialiser.Read(szObjectName);
	cGraphDeserialiser.Kill();

	return pvObject;
}
void TestObjectReaderSimpleDeserialised(void)
{
	WriteObjectReaderSimpleFile();

	CObjectReaderSimpleDisk		cReader;
	CObjectGraphDeserialiser	cGraphDeserialiser;
	CPointer					cBase;

	Ptr<CTestNamedString>		cNS1;
	Ptr<CTestNamedString>		cNS2;
	CPointer					cTemp;

	CObjectAllocator			cAllocator;
	CDependentReadObjects		cDependentReadObjects;

	MemoryInit();
	ObjectsInit();

	gcObjects.AddConstructor<CTestNamedString>();

	AssertLongLongInt(0, gcObjects.NumDatabaseObjects());
	AssertLongLongInt(0, gcObjects.NumMemoryIndexes());

	cAllocator.Init(&gcObjects);
	cDependentReadObjects.Init();
	cReader.Init("Output\\ObjectReaderSimple\\Test\\");
	cGraphDeserialiser.Init(&cReader, FALSE, &cAllocator, &cDependentReadObjects, gcObjects.GetMemory());
	cBase = cGraphDeserialiser.Read("Waggy");

	AssertLongLongInt(0, gcObjects.NumDatabaseObjects());
	AssertLongLongInt(2, gcObjects.NumMemoryIndexes());

	cNS1 = gcObjects.Get("Waggy");
	AssertTrue(cNS1.IsNotNull());
	AssertString("NS1", cNS1->mszEmbedded.Text());

	cNS2 = gcObjects.Get("Dog");
	AssertTrue(cNS2.IsNotNull());
	AssertString("NS2", cNS2->mszEmbedded.Text());

	AssertTrue(cBase.IsNotNull());
	AssertString("CTestNamedString", cBase->ClassName());
	AssertPointer(&cNS1, &cBase);

	AssertPointer(&cNS2, &cNS1->mpAnother);
	AssertPointer(NULL, &cNS2->mpAnother);

	cGraphDeserialiser.Kill();
	cDependentReadObjects.Kill();
	cAllocator.Kill();
	cReader.Kill();

	ObjectsKill();
}
void TestObjectReaderChunkedDeserialised(void)
{
	CObjectReaderChunkFileDisk	cReader;
	CObjectGraphDeserialiser	cGraphDeserialiser;
	CPointer					cBase;
	Ptr<CTestWithArray>			cA1;
	Ptr<CTestWithArray>			cA2;
	Ptr<CTestNamedString>		cNS1;
	Ptr<CTestNamedString>		cNS2;
	Ptr<CTestNamedString>		cNS3;
	Ptr<CTestNamedString>		cNS4;
	Ptr<CString>				sz1;
	Ptr<CString>				sz2;
	Ptr<CString>				sz3;
	CPointer					cTemp;
	Ptr<CTestInteger>			cI1;
	Ptr<CTestInteger>			cI2;
	Ptr<CTestInteger>			cI3;
	CObjectAllocator			cAllocator;
	CDependentReadObjects		cDependentReadObjects;
	OIndex						oiI1;
	OIndex						oiI2;
	OIndex						oiI3;
	int							iNumMemoryIndexes;

	gcObjects.AddConstructor<CTestWithArray>();
	gcObjects.AddConstructor<CTestInteger>();
	gcObjects.AddConstructor<CTestNamedString>();
	gcObjects.AddConstructor<CString>();
	gcObjects.AddConstructor<CArrayObject>();

	iNumMemoryIndexes = WriteObjectReaderChunkedFile();

	AssertLongLongInt(0, gcObjects.NumDatabaseObjects());
	AssertLongLongInt(14, iNumMemoryIndexes);

	ObjectsKill();
	ObjectsInit();

	gcObjects.AddConstructor<CTestWithArray>();
	gcObjects.AddConstructor<CTestInteger>();
	gcObjects.AddConstructor<CTestNamedString>();
	gcObjects.AddConstructor<CString>();
	gcObjects.AddConstructor<CArrayObject>();

	AssertLongLongInt(0, gcObjects.NumDatabaseObjects());
	AssertLongLongInt(0, gcObjects.NumMemoryIndexes());

	cAllocator.Init(&gcObjects);
	cDependentReadObjects.Init();
	cReader.Init("Output\\ObjectReaderChunked\\Test\\", "Reader");
	cGraphDeserialiser.Init(&cReader, FALSE, &cAllocator, &cDependentReadObjects, gcObjects.GetMemory());
	cBase = cGraphDeserialiser.Read("Array 1");
	AssertTrue(cBase.IsNotNull());
	AssertString("CTestWithArray", cBase.ClassName());

	AssertLongLongInt(0, gcObjects.NumDatabaseObjects());
	AssertLongLongInt(14, gcObjects.NumMemoryIndexes());

	cA1 = gcObjects.Get("Array 1");
	AssertTrue(cA1.IsNotNull());
	AssertString("CTestWithArray", cA1->ClassName());
	AssertString("Something with One", cA1->mszString.Text());
	AssertInt(1, cA1->mx);

	AssertPointer(cBase.Object(), cA1.Object());

	cA2 = gcObjects.Get("Array X");
	AssertTrue(cA2.IsNotNull());
	AssertString("CTestWithArray", cA2.ClassName());
	AssertString("An with 2", cA2->mszString.Text());
	AssertInt(2, cA2->mx);

	cNS1 = gcObjects.Get("NamedString 1");
	AssertTrue(cNS1.IsNotNull());
	AssertString("CTestNamedString", cNS1->ClassName());
	AssertString("In Named 1", cNS1->mszEmbedded.Text());

	cNS2 = gcObjects.Get("NamedString 2");
	AssertTrue(cNS2.IsNotNull());
	AssertString("CTestNamedString", cNS2->ClassName());
	AssertString("Another in 2", cNS2->mszEmbedded.Text());

	cNS3 = gcObjects.Get("NamedString 3");
	AssertTrue(cNS3.IsNotNull());
	AssertString("CTestNamedString", cNS3->ClassName());
	AssertString("Three", cNS3->mszEmbedded.Text());

	cNS4 = gcObjects.Get("NamedString 4");
	AssertTrue(cNS4.IsNotNull());

	AssertTrue(cBase.IsNotNull());
	AssertString("CTestWithArray", cBase->ClassName());
	AssertPointer(&cA1, &cBase);
	
	AssertNotNull(&cA1->mcArray);
	AssertInt(6, cA1->mcArray->NumElements())
	sz2 = cA1->mcArray->Get(0);
	AssertString("Ye!", sz2->Text());
	cTemp = cA1->mcArray->Get(1);
	AssertPointer(&cNS1, &cTemp);

	oiI1 = cGraphDeserialiser.GetNewIndexFromOld(5LL);
	oiI2 = cGraphDeserialiser.GetNewIndexFromOld(6LL);
	oiI3 = cGraphDeserialiser.GetNewIndexFromOld(7LL);

	cI1 = gcObjects.Get(oiI1);
	AssertString("CTestInteger", cI1->ClassName());
	cI2 = gcObjects.Get(oiI2);
	AssertString("CTestInteger", cI2->ClassName());
	cI3 = gcObjects.Get(oiI3);
	AssertString("CTestInteger", cI3->ClassName());

	AssertInt(3, cI1->mx);
	AssertInt(2, cI1->my);
	AssertInt(1, cI1->mz);

	AssertPointer(&cNS2, &cNS1->mpAnother);
	AssertPointer(&cI1, &cA1->mcArray->Get(2));
	AssertPointer(&cNS3, &cA1->mcArray->Get(3));
	AssertPointer(&cNS1, &cNS3->mpAnother);
	AssertPointer(&cI2, &cA1->mcArray->Get(4));
	AssertPointer(&cA2, &cA1->mcArray->Get(5));

	AssertNotNull(&cA2->mcArray);
	AssertInt(4, cA2->mcArray->NumElements());
	AssertPointer(&cI3, &cA2->mcArray->Get(0));
	AssertPointer(&cNS2, &cA2->mcArray->Get(1));
	AssertPointer(&cNS3, &cNS2->mpAnother);
	AssertPointer(&cI1, &cA2->mcArray->Get(2));
	AssertPointer(&cNS4, &cA2->mcArray->Get(3));
	AssertPointer(NULL, &cNS4->mpAnother);
	AssertPointer(NULL, &cNS4->mszString);

	cGraphDeserialiser.Kill();
	cDependentReadObjects.Kill();
	cAllocator.Kill();
	cReader.Kill();
}
void TestRemappingOfOIs(CObjectWriter* pcWriter, CObjectReader* pcReader)
{
	CFileUtil					cFileUtil;
	Ptr<CTestSaveableObject2>	cBase;
	Ptr<CTestSaveableObject2>	cStart1;
	Ptr<CRoot>					cRoot;
	Ptr<CString>				szOne;
	Ptr<CString>				cString1;
	Ptr<CString>				cString2;
	CObjectGraphSerialiser		cGraphSerialiser;
	CObjectGraphDeserialiser	cGraphDeserialiser;
	Ptr<CTestSaveableObject1>	cShared;
	int							i;
	CObjectAllocator			cAllocator;
	CDependentReadObjects		cDependentReadObjects;

	cFileUtil.MakeDir("Output/GraphDeserialiser/Simple/Remapping");

	ObjectsInit("Output/GraphDeserialiser/Simple/Remapping");
	TestObjectGraphDeserialiserAddConstructors();
	TestObjectGraphDeserialiserBuildGraph1();

	cBase = gcObjects.Get("Ow/Start 1");
	AssertTrue(cBase.IsNotNull());
	AssertLongLongInt(3, cBase->GetOI());
	cString1 = gcObjects.Get(6LL);
	AssertString("Black", cString1->Text());
	AssertLongLongInt(6LL, cString1->GetOI());
	cString2 = gcObjects.Get(7LL);
	AssertString("Jack", cString2->Text());
	AssertLongLongInt(7LL, cString2->GetOI());

	cGraphSerialiser.Init(pcWriter);
	AssertTrue(cGraphSerialiser.Write(&cBase));
	cGraphSerialiser.Kill();
	pcWriter->Kill();

	ObjectsKill();

	ObjectsInit("Output/GraphDeserialiser/Simple/Remapping");
	TestObjectGraphDeserialiserAddConstructors();

	cRoot = ORoot();

	for (i = 0; i < 20; i++)
	{
		szOne = OMalloc(CString);
		szOne->Init("Hello World ");
		szOne->Append(i);
		cRoot->Add(szOne);
		AssertLongLongInt(3+i, szOne->GetOI());
	}

	cAllocator.Init(&gcObjects);
	cDependentReadObjects.Init();
	cGraphDeserialiser.Init(pcReader, FALSE, &cAllocator, &cDependentReadObjects, gcObjects.GetMemory());
	cStart1 = cGraphDeserialiser.Read("Ow/Start 1");
	AssertTrue(cStart1.IsNotNull());
	AssertLongLongInt(23, cStart1->GetOI());

	AssertTrue(cStart1->mp1.IsNotNull());
	AssertString("CTestSaveableObject1", cStart1->mp1->ClassName());
	cShared = cStart1->mp1;
	AssertLongLongInt(25, cShared->GetOI());

	AssertTrue(cStart1->mp2.IsNotNull());
	AssertString("CString", cStart1->mp2->ClassName());
	cString1 = cStart1->mp2;
	AssertLongLongInt(24, cString1->GetOI());
	cGraphDeserialiser.Kill();
	cDependentReadObjects.Kill();
	cAllocator.Kill();

	for (i = 0; i < 20; i++)
	{
		szOne = OMalloc(CString);
		szOne->Init("Hello World ");
		szOne->Append(i + 20);
		cRoot->Add(szOne);
		AssertLongLongInt(26+i, szOne->GetOI());
	}

	pcReader->Kill();

	ObjectsKill();
}
void TestOverwritingOfExistingNamesFromChunkedFiles(void)
{
	CObjectWriterChunked			cWriterStart1;
	CObjectWriterChunked			cWriterStart2;
	CFileUtil						cFileUtil;
	Ptr<CTestSaveableObject2>		cOwStart1;
	Ptr<CTestSaveableObject2>		cOwStart2;
	CObjectGraphSerialiser			cGraphSerialiser;
	CObjectGraphDeserialiser		cGraphDeserialiser;
	CObjectReaderChunkFileDisk		cReaderStart1;
	CObjectReaderChunkFileDisk		cReaderStart2;
	Ptr<CTestSaveableObject1>		cShared;
	Ptr<CRoot>						cRoot;
	CPointer						cObject;
	int								iNumUnknowns;
	int								iNumIndexes;
	int								iNumNames;
	CObjectAllocator				cAllocator;
	CDependentReadObjects			cDependentReadObjects;

	cWriterStart1.Init("Output/GraphDeserialiser/Simple/Remapping", "", "Start1");
	cWriterStart2.Init("Output/GraphDeserialiser/Simple/Remapping", "", "Start2");

	cFileUtil.MakeDir("Output/GraphDeserialiser/Simple/Remapping");

	ObjectsInit("Output/GraphDeserialiser/Simple/Remapping");
	TestObjectGraphDeserialiserAddConstructors();
	TestObjectGraphDeserialiserBuildGraph1();

	cOwStart1 = gcObjects.Get("Ow/Start 1");
	cGraphSerialiser.Init(&cWriterStart1);
	AssertTrue(cGraphSerialiser.Write(&cOwStart1));
	cGraphSerialiser.Kill();
	cWriterStart1.Kill();

	cOwStart2 = gcObjects.Get("Ow/Start 2");
	cGraphSerialiser.Init(&cWriterStart2);
	AssertTrue(cGraphSerialiser.Write(&cOwStart2));
	cGraphSerialiser.Kill();
	cWriterStart2.Kill();

	cShared = gcObjects.Get("Ow/Shared");
	
	AssertInt(3, cShared->NumHeapFroms());
	AssertPointer(&cOwStart1, cShared->GetHeapFrom(0));
	AssertPointer(&cOwStart2, cShared->GetHeapFrom(1));
	AssertPointer(&cShared, cShared->GetHeapFrom(2));  //Remember: cShared->mpObject = cShared;
	AssertInt(2, cOwStart1->NumPointerTos());
	AssertPointer(&cShared, cOwStart1->TestGetPointerTo(0));
	AssertPointer(&cOwStart1->mp1, cOwStart1->TestGetPointerTo(0));
	AssertPointer(&cOwStart1->mp2, cOwStart1->TestGetPointerTo(1));
	AssertInt(2, cOwStart1->GetDistToRoot());
	AssertInt(3, cShared->GetDistToRoot());

	ObjectsKill();

	ObjectsInit("Output/GraphDeserialiser/Simple/Remapping");
	TestObjectGraphDeserialiserAddConstructors();
	cRoot = ORoot();

	cAllocator.Init(&gcObjects);
	cDependentReadObjects.Init();
	cReaderStart1.Init("Output/GraphDeserialiser/Simple/Remapping", "Start1");
	cGraphDeserialiser.Init(&cReaderStart1, FALSE, &cAllocator, &cDependentReadObjects, gcObjects.GetMemory());
	cOwStart1 = cGraphDeserialiser.Read("Ow/Start 1");
	cGraphDeserialiser.Kill();
	cDependentReadObjects.Kill();
	cAllocator.Kill();
	cReaderStart1.Kill();

	AssertInt(-1, cOwStart1->GetDistToRoot());
	
	cRoot->Add(cOwStart1);

	cShared = gcObjects.Get("Ow/Shared");

	//Make sure the pointed 'froms' are correctly setup after loading.
	AssertInt(2, cShared->NumHeapFroms());
	AssertPointer(&cOwStart1, cShared->GetHeapFrom(0));
	AssertPointer(&cShared, cShared->GetHeapFrom(1));
	AssertInt(2, cOwStart1->NumPointerTos());
	AssertPointer(&cShared, cOwStart1->TestGetPointerTo(0));
	AssertPointer(&cOwStart1->mp1, cOwStart1->TestGetPointerTo(0));
	AssertPointer(&cOwStart1->mp2, cOwStart1->TestGetPointerTo(1));
	AssertInt(2, cOwStart1->GetDistToRoot());
	AssertInt(3, cShared->GetDistToRoot());

	AssertInt(89, cOwStart1->mp1->miInt);
	cOwStart1->mp1->miInt = 66;

	iNumUnknowns = gcUnknowns.NumElements();
	iNumIndexes = (int)gcObjects.NumMemoryIndexes();
	iNumNames = gcObjects.NumMemoryNames();

	cAllocator.Init(&gcObjects);
	cDependentReadObjects.Init();
	cReaderStart2.Init("Output/GraphDeserialiser/Simple/Remapping", "Start2");
	cGraphDeserialiser.Init(&cReaderStart2, FALSE, &cAllocator, &cDependentReadObjects, gcObjects.GetMemory());
	cOwStart2 = cGraphDeserialiser.Read("Ow/Start 2");
	cGraphDeserialiser.Kill();
	cDependentReadObjects.Kill();
	cAllocator.Kill();
	cReaderStart2.Kill();

	AssertInt(iNumUnknowns+2, gcUnknowns.NumElements());
	AssertInt(iNumIndexes+2, (int)gcObjects.NumMemoryIndexes());
	AssertInt(iNumNames+1, (int)gcObjects.NumMemoryNames());

	AssertNotNull(cOwStart2.Object());
	cRoot->Add(cOwStart2);

	AssertNotNull(&cOwStart2);
	AssertInt(89, cOwStart2->mp1->miInt);
	AssertLongLongInt(cOwStart1->mp1->GetOI(), cOwStart2->mp1->GetOI());
	AssertInt(89, cOwStart1->mp1->miInt);
	AssertPointer(&cOwStart1->mp1, &cOwStart2->mp1);

	cObject = gcObjects.Get("Ow/Start 2");
	AssertPointer(&cOwStart2, &cObject);

	cObject = gcObjects.Get(cOwStart2->GetOI());
	AssertPointer(&cOwStart2, &cObject);

	ObjectsKill();
}