void CopyXMLNode(csRef<iDocumentNode> source, csRef<iDocumentNode> target, int mode)
{
    if (mode == 0)
    {
        target->RemoveNodes();
        target->RemoveAttributes();
    }
    
    // copy nodes
    csRef<iDocumentNodeIterator> nodeIter = source->GetNodes();
    while (nodeIter->HasNext())
    {
        csRef<iDocumentNode> child = nodeIter->Next();
        csRef<iDocumentNode> targetChild = target->GetNode(child->GetValue());
        if (targetChild==NULL || mode==3)  // Mode 3 means don't merge tags but just insert multiples, so we create a new one here every time
        {
            targetChild = target->CreateNodeBefore(child->GetType());
            if (targetChild == NULL)
                assert(!"failed to create XML node, you are probably using wrong XML parser (xmlread instead of xmltiny)");
            targetChild->SetValue(child->GetValue());
        }
        CopyXMLNode(child, targetChild, mode);
    }
    
    // copy attributes
    csRef <iDocumentAttributeIterator> attrIter = source->GetAttributes();
    while (attrIter->HasNext())
    {
        csRef<iDocumentAttribute> attr = attrIter->Next();
        const char* attrName = attr->GetName();
        if (mode==1  ||  !target->GetAttribute(attrName))
            target->SetAttribute(attrName, attr->GetValue());
    }
}
void OptimiseData::ParseMeshObj(csArray<csString>& libsNeeded, csArray<csString>& materialsNeeded,
                                csString& mapInPath, csRef<iDocumentNode> meshobj)
{
  csRef<iDocumentNodeIterator> meshobjs = meshobj->GetNodes("meshobj");
  while(meshobjs->HasNext())
  {
    ParseMeshObj(libsNeeded, materialsNeeded, mapInPath, meshobjs->Next());
  }

  if(meshobj->GetNode("params"))
  {
    meshobj = meshobj->GetNode("params");
    if(meshobj->GetNode("factory"))
    {
      bool found = false;
      for(size_t i=0; i<meshFactsOut.GetSize(); i++)
      {
        if(csString(meshFactsOut[i]->GetRoot()->GetNode("library")->GetNode("meshfact")->GetAttributeValue("name")).Compare(meshobj->GetNode("factory")->GetContentsValue()))
        {
          libsNeeded.PushSmart(meshobj->GetNode("factory")->GetContentsValue());
          found = true;
        }
      }

      if(!found)
      {
        // Print error and mark data as incorrect.
        csFPrintf(stderr, "ERROR: Mesh object %s uses mesh factory %s but there is no factory data!\n",
          meshobj->GetAttributeValue("name"), meshobj->GetNode("factory")->GetContentsValue());
      }
    }
    if(meshobj->GetNode("material"))
    {
      bool found = false;
      for(size_t i=0; i<materials.GetSize(); i++)
      {
        if(csString(materials[i]->GetAttributeValue("name")).Compare(meshobj->GetNode("material")->GetContentsValue()))
        {
          materialsNeeded.PushSmart(meshobj->GetNode("material")->GetContentsValue());
          found = true;
        }
      }

      if(!found)
      {
        // Print error and mark data as incorrect.
        csFPrintf(stderr, "ERROR: Mesh object %s uses material %s but there is no such material declaration!\n",
          meshobj->GetAttributeValue("name"), meshobj->GetNode("material")->GetContentsValue());
      }
    }
  }
  else if(meshobj->GetNode("paramsfile"))
  {
    csString paramsPath = mapInPath + "/" + meshobj->GetNode("paramsfile")->GetContentsValue();
    csRef<iFile> file = vfs->Open(paramsPath, VFS_FILE_READ);
    csRef<iDocument> paramsDoc = docSys->CreateDocument();
    paramsDoc->Parse(file);
    meshobj = paramsDoc->GetRoot()->GetNode("params");
    if(meshobj->GetNode("factory"))
    {
      bool found = false;
      for(size_t i=0; i<meshFactsOut.GetSize(); i++)
      {
        if(csString(meshFactsOut[i]->GetRoot()->GetNode("library")->GetNode("meshfact")->GetAttributeValue("name")).Compare(meshobj->GetNode("factory")->GetContentsValue()))
        {
          libsNeeded.PushSmart(meshobj->GetNode("factory")->GetContentsValue());
          found = true;
        }
      }

      if(!found)
      {
        // Print error and mark data as incorrect.
        csFPrintf(stderr, "ERROR: Mesh object %s uses mesh factory %s but there is no factory data!\n",
          meshobj->GetParent()->GetAttributeValue("name"), meshobj->GetNode("factory")->GetContentsValue());
      }
    }
    if(meshobj->GetNode("material"))
    {
      bool found = false;
      for(size_t i=0; i<materials.GetSize(); i++)
      {
        if(csString(materials[i]->GetAttributeValue("name")).Compare(meshobj->GetNode("material")->GetContentsValue()))
        {
          materialsNeeded.PushSmart(meshobj->GetNode("material")->GetContentsValue());
          found = true;
        }
      }

      if(!found)
      {
        // Print error and mark data as incorrect.
        csFPrintf(stderr, "ERROR: Mesh object %s uses material %s but there is no such material declaration!\n",
          meshobj->GetParent()->GetAttributeValue("name"), meshobj->GetNode("material")->GetContentsValue());
      }
    }
  }
}
void OptimiseData::ParseMeshFact(csRef<iDocumentNode>& meshFact, csRef<iDocumentNode>& tempDocRoot,
                                 csRefArray<iDocumentNode>& tempMats)
{
  csRef<iDocumentNode> params = meshFact->GetNode("params");
  if(params.IsValid())
  {
    bool first = true;
    csRef<iDocumentNodeIterator> submeshes = params->GetNodes("submesh");
    if(!submeshes->HasNext())
    {
      submeshes = params->GetNodes("curve");
    }

    while((first && params->GetNode("material")) || submeshes->HasNext())
    {
      csString materialName;
      if(first && params->GetNode("material"))
      {
        materialName = params->GetNode("material")->GetContentsValue();
        first = false;
      }
      else if(submeshes->HasNext())
      {
        csRef<iDocumentNode> submesh = submeshes->Next();
        if(submesh->GetNode("material"))
        {
          materialName = submesh->GetNode("material")->GetContentsValue();
        }
        else
        {
          continue;
        }
      }

      bool hasMaterialDecl = false;

      csRef<iDocumentNode> material;
      for(size_t j=0; j<materials.GetSize(); j++)
      {
        material = materials[j];
        if(materialName.Compare(material->GetAttributeValue("name")))
        {
          hasMaterialDecl = true;
          break;
        }
      }

      if(!hasMaterialDecl)
      {
        // Print error and mark data as incorrect.
        csFPrintf(stderr, "ERROR: Meshfact %s uses material %s but there is no material declaration!\n",
          meshFact->GetAttributeValue("name"), materialName.GetData());
      }

      tempMats.PushSmart(material);
    }
  }

  csRef<iDocumentNodeIterator> undermeshes = meshFact->GetNodes("meshfact");
  while(undermeshes->HasNext())
  {
    csRef<iDocumentNode> next = undermeshes->Next();
    ParseMeshFact(next, tempDocRoot, tempMats);
  }
}