1 /* Copyright (C) 2009 Wildfire Games.
2 * This file is part of 0 A.D.
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.
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.
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/>.
18 #include "precompiled.h"
22 #include "CommonConvert.h"
24 #include "FUtils/FUXmlParser.h"
28 Things that are fixed here:
32 3ds Max "file://" image URLs
34 Identifier: /COLLADA/asset/contributor/authoring_tool = "FBX COLLADA exporter"
36 Problem: /COLLADA/library_images/image/init_from = "file://" which crashes some versions of FCollada
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.
43 3ds Max broken material references
45 Identifier: /COLLADA/asset/contributor/authoring_tool = "FBX COLLADA exporter"
47 Problem: /COLLADA/library_visual_scenes/.../instance_material/@target sometimes
48 refers to non-existent material IDs.
50 Fix: Delete the whole bind_material subtree, since we never use it anyway.
56 static xmlNode
* findChildElement(xmlNode
* node
, const char* name
)
58 for (xmlNode
* child
= node
->children
; child
; child
= child
->next
)
60 if (child
->type
== XML_ELEMENT_NODE
&& strcmp((const char*)child
->name
, name
) == 0)
66 static bool applyFBXFixesNode(xmlNode
* node
)
69 for (xmlNode
* child
= node
->children
; child
; child
= child
->next
)
71 if (child
->type
== XML_ELEMENT_NODE
)
73 if (strcmp((const char*)child
->name
, "node") == 0)
75 if (applyFBXFixesNode(child
))
78 else if (strcmp((const char*)child
->name
, "instance_geometry") == 0)
80 xmlNode
* bind_material
= findChildElement(child
, "bind_material");
81 if (! bind_material
) continue;
82 Log(LOG_INFO
, "Found a bind_material to delete");
83 xmlUnlinkNode(bind_material
);
84 xmlFreeNode(bind_material
);
93 static bool applyFBXFixes(xmlNode
* root
)
95 Log(LOG_INFO
, "Applying fixes for 3ds Max exporter");
99 xmlNode
* library_images
= findChildElement(root
, "library_images");
102 Log(LOG_INFO
, "Found library_images to delete");
103 xmlUnlinkNode(library_images
);
104 xmlFreeNode(library_images
);
108 xmlNode
* library_materials
= findChildElement(root
, "library_materials");
109 if (library_materials
)
111 Log(LOG_INFO
, "Found library_materials to delete");
112 xmlUnlinkNode(library_materials
);
113 xmlFreeNode(library_materials
);
117 xmlNode
* library_effects
= findChildElement(root
, "library_effects");
120 Log(LOG_INFO
, "Found library_effects to delete");
121 xmlUnlinkNode(library_effects
);
122 xmlFreeNode(library_effects
);
126 xmlNode
* library_visual_scenes
= findChildElement(root
, "library_visual_scenes");
127 if (library_visual_scenes
) // (Assume there's only one of these)
129 xmlNode
* visual_scene
= findChildElement(library_visual_scenes
, "visual_scene");
130 if (visual_scene
) // (Assume there's only one of these)
132 for (xmlNode
* child
= visual_scene
->children
; child
; child
= child
->next
)
134 if (child
->type
== XML_ELEMENT_NODE
&& strcmp((const char*)child
->name
, "node") == 0)
135 if (applyFBXFixesNode(child
))
144 static bool processDocument(xmlNode
* root
)
146 xmlNode
* asset
= findChildElement(root
, "asset");
147 if (! asset
) return false;
148 xmlNode
* contributor
= findChildElement(asset
, "contributor");
149 if (! contributor
) return false;
150 xmlNode
* authoring_tool
= findChildElement(contributor
, "authoring_tool");
151 if (! authoring_tool
) return false;
153 xmlNode
* authoring_tool_text
= authoring_tool
->children
;
154 if (! authoring_tool_text
) return false;
155 if (authoring_tool_text
->type
!= XML_TEXT_NODE
) return false;
156 xmlChar
* toolname
= authoring_tool_text
->content
;
157 Log(LOG_INFO
, "Authoring tool: %s", toolname
);
158 if (strcmp((const char*)toolname
, "FBX COLLADA exporter") == 0)
159 return applyFBXFixes(root
);
164 void FixBrokenXML(const char* text
, const char** out
, size_t* outSize
)
166 Log(LOG_INFO
, "Running FixBrokenXML");
168 size_t textSize
= strlen(text
);
169 xmlDocPtr doc
= xmlParseMemory(text
, (int)textSize
);
171 xmlNode
* root
= xmlDocGetRootElement(doc
);
172 if (root
&& processDocument(root
))
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
178 xmlDocDumpFormatMemory(doc
, &mem
, &size
, 0);
179 *out
= (const char*)mem
;