LCOV - code coverage report
Current view: top level - source/collada - XMLFix.cpp (source / functions) Hit Total Coverage
Test: 0 A.D. test coverage report Lines: 79 80 98.8 %
Date: 2023-01-19 00:18:29 Functions: 5 5 100.0 %

          Line data    Source code
       1             : /* Copyright (C) 2009 Wildfire Games.
       2             :  * This file is part of 0 A.D.
       3             :  *
       4             :  * 0 A.D. is free software: you can redistribute it and/or modify
       5             :  * it under the terms of the GNU General Public License as published by
       6             :  * the Free Software Foundation, either version 2 of the License, or
       7             :  * (at your option) any later version.
       8             :  *
       9             :  * 0 A.D. is distributed in the hope that it will be useful,
      10             :  * but WITHOUT ANY WARRANTY; without even the implied warranty of
      11             :  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
      12             :  * GNU General Public License for more details.
      13             :  *
      14             :  * You should have received a copy of the GNU General Public License
      15             :  * along with 0 A.D.  If not, see <http://www.gnu.org/licenses/>.
      16             :  */
      17             : 
      18             : #include "precompiled.h"
      19             : 
      20             : #include "XMLFix.h"
      21             : 
      22             : #include "CommonConvert.h"
      23             : 
      24             : #include "FUtils/FUXmlParser.h"
      25             : 
      26             : /*
      27             : 
      28             : Things that are fixed here:
      29             : 
      30             : ----
      31             : 
      32             : 3ds Max "file://" image URLs
      33             : 
      34             : Identifier: /COLLADA/asset/contributor/authoring_tool = "FBX COLLADA exporter"
      35             : 
      36             : Problem: /COLLADA/library_images/image/init_from = "file://" which crashes some versions of FCollada
      37             : 
      38             : Fix: Delete the whole library_images subtree, since we never use it anyway.
      39             : Then delete library_effects and library_materials too, to avoid broken references.
      40             : 
      41             : ----
      42             : 
      43             : 3ds Max broken material references
      44             : 
      45             : Identifier: /COLLADA/asset/contributor/authoring_tool = "FBX COLLADA exporter"
      46             : 
      47             : Problem: /COLLADA/library_visual_scenes/.../instance_material/@target sometimes
      48             : refers to non-existent material IDs.
      49             : 
      50             : Fix: Delete the whole bind_material subtree, since we never use it anyway.
      51             : 
      52             : ----
      53             : 
      54             : */
      55             : 
      56          18 : static xmlNode* findChildElement(xmlNode* node, const char* name)
      57             : {
      58          60 :     for (xmlNode* child = node->children; child; child = child->next)
      59             :     {
      60          60 :         if (child->type == XML_ELEMENT_NODE && strcmp((const char*)child->name, name) == 0)
      61          18 :             return child;
      62             :     }
      63           0 :     return NULL;
      64             : }
      65             : 
      66           2 : static bool applyFBXFixesNode(xmlNode* node)
      67             : {
      68           2 :     bool changed = false;
      69          22 :     for (xmlNode* child = node->children; child; child = child->next)
      70             :     {
      71          20 :         if (child->type == XML_ELEMENT_NODE)
      72             :         {
      73           9 :             if (strcmp((const char*)child->name, "node") == 0)
      74             :             {
      75           1 :                 if (applyFBXFixesNode(child))
      76           1 :                     changed = true;
      77             :             }
      78           8 :             else if (strcmp((const char*)child->name, "instance_geometry") == 0)
      79             :             {
      80           1 :                 xmlNode* bind_material = findChildElement(child, "bind_material");
      81           1 :                 if (! bind_material) continue;
      82           1 :                 Log(LOG_INFO, "Found a bind_material to delete");
      83           1 :                 xmlUnlinkNode(bind_material);
      84           1 :                 xmlFreeNode(bind_material);
      85             : 
      86           1 :                 changed = true;
      87             :             }
      88             :         }
      89             :     }
      90           2 :     return changed;
      91             : }
      92             : 
      93           1 : static bool applyFBXFixes(xmlNode* root)
      94             : {
      95           1 :     Log(LOG_INFO, "Applying fixes for 3ds Max exporter");
      96             : 
      97           1 :     bool changed = false;
      98             : 
      99           1 :     xmlNode* library_images = findChildElement(root, "library_images");
     100           1 :     if (library_images)
     101             :     {
     102           1 :         Log(LOG_INFO, "Found library_images to delete");
     103           1 :         xmlUnlinkNode(library_images);
     104           1 :         xmlFreeNode(library_images);
     105           1 :         changed = true;
     106             :     }
     107             : 
     108           1 :     xmlNode* library_materials = findChildElement(root, "library_materials");
     109           1 :     if (library_materials)
     110             :     {
     111           1 :         Log(LOG_INFO, "Found library_materials to delete");
     112           1 :         xmlUnlinkNode(library_materials);
     113           1 :         xmlFreeNode(library_materials);
     114           1 :         changed = true;
     115             :     }
     116             : 
     117           1 :     xmlNode* library_effects = findChildElement(root, "library_effects");
     118           1 :     if (library_effects)
     119             :     {
     120           1 :         Log(LOG_INFO, "Found library_effects to delete");
     121           1 :         xmlUnlinkNode(library_effects);
     122           1 :         xmlFreeNode(library_effects);
     123           1 :         changed = true;
     124             :     }
     125             : 
     126           1 :     xmlNode* library_visual_scenes = findChildElement(root, "library_visual_scenes");
     127           1 :     if (library_visual_scenes) // (Assume there's only one of these)
     128             :     {
     129           1 :         xmlNode* visual_scene = findChildElement(library_visual_scenes, "visual_scene");
     130           1 :         if (visual_scene) // (Assume there's only one of these)
     131             :         {
     132           4 :             for (xmlNode* child = visual_scene->children; child; child = child->next)
     133             :             {
     134           3 :                 if (child->type == XML_ELEMENT_NODE && strcmp((const char*)child->name, "node") == 0)
     135           1 :                     if (applyFBXFixesNode(child))
     136           1 :                         changed = true;
     137             :             }
     138             :         }
     139             :     }
     140             : 
     141           1 :     return changed;
     142             : }
     143             : 
     144           4 : static bool processDocument(xmlNode* root)
     145             : {
     146           4 :     xmlNode* asset = findChildElement(root, "asset");
     147           4 :     if (! asset) return false;
     148           4 :     xmlNode* contributor = findChildElement(asset, "contributor");
     149           4 :     if (! contributor) return false;
     150           4 :     xmlNode* authoring_tool = findChildElement(contributor, "authoring_tool");
     151           4 :     if (! authoring_tool) return false;
     152             : 
     153           4 :     xmlNode* authoring_tool_text = authoring_tool->children;
     154           4 :     if (! authoring_tool_text) return false;
     155           4 :     if (authoring_tool_text->type != XML_TEXT_NODE) return false;
     156           4 :     xmlChar* toolname = authoring_tool_text->content;
     157           4 :     Log(LOG_INFO, "Authoring tool: %s", toolname);
     158           4 :     if (strcmp((const char*)toolname, "FBX COLLADA exporter") == 0)
     159           1 :         return applyFBXFixes(root);
     160             :     else
     161           3 :         return false;
     162             : }
     163             : 
     164           5 : void FixBrokenXML(const char* text, const char** out, size_t* outSize)
     165             : {
     166           5 :     Log(LOG_INFO, "Running FixBrokenXML");
     167             : 
     168           5 :     size_t textSize = strlen(text);
     169           5 :     xmlDocPtr doc = xmlParseMemory(text, (int)textSize);
     170             : 
     171           5 :     xmlNode* root = xmlDocGetRootElement(doc);
     172           5 :     if (root && processDocument(root))
     173             :     {
     174             :         // Reserialising the document, then parsing it again inside FCollada, is a bit ugly;
     175             :         // but it's the only way I can see to make it work through FCollada's public API
     176           1 :         xmlChar* mem = NULL;
     177           1 :         int size = -1;
     178           1 :         xmlDocDumpFormatMemory(doc, &mem, &size, 0);
     179           1 :         *out = (const char*)mem;
     180           1 :         *outSize = size;
     181             :     }
     182             :     else
     183             :     {
     184           4 :         *out = text;
     185           4 :         *outSize = textSize;
     186             :     }
     187             : 
     188           5 :     xmlFreeDoc(doc);
     189           5 : }

Generated by: LCOV version 1.13