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
findChildElement(xmlNode * node,const char * name)56 static xmlNode* findChildElement(xmlNode* node, const char* name)
57 {
58 for (xmlNode* child = node->children; child; child = child->next)
59 {
60 if (child->type == XML_ELEMENT_NODE && strcmp((const char*)child->name, name) == 0)
61 return child;
62 }
63 return NULL;
64 }
65
applyFBXFixesNode(xmlNode * node)66 static bool applyFBXFixesNode(xmlNode* node)
67 {
68 bool changed = false;
69 for (xmlNode* child = node->children; child; child = child->next)
70 {
71 if (child->type == XML_ELEMENT_NODE)
72 {
73 if (strcmp((const char*)child->name, "node") == 0)
74 {
75 if (applyFBXFixesNode(child))
76 changed = true;
77 }
78 else if (strcmp((const char*)child->name, "instance_geometry") == 0)
79 {
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);
85
86 changed = true;
87 }
88 }
89 }
90 return changed;
91 }
92
applyFBXFixes(xmlNode * root)93 static bool applyFBXFixes(xmlNode* root)
94 {
95 Log(LOG_INFO, "Applying fixes for 3ds Max exporter");
96
97 bool changed = false;
98
99 xmlNode* library_images = findChildElement(root, "library_images");
100 if (library_images)
101 {
102 Log(LOG_INFO, "Found library_images to delete");
103 xmlUnlinkNode(library_images);
104 xmlFreeNode(library_images);
105 changed = true;
106 }
107
108 xmlNode* library_materials = findChildElement(root, "library_materials");
109 if (library_materials)
110 {
111 Log(LOG_INFO, "Found library_materials to delete");
112 xmlUnlinkNode(library_materials);
113 xmlFreeNode(library_materials);
114 changed = true;
115 }
116
117 xmlNode* library_effects = findChildElement(root, "library_effects");
118 if (library_effects)
119 {
120 Log(LOG_INFO, "Found library_effects to delete");
121 xmlUnlinkNode(library_effects);
122 xmlFreeNode(library_effects);
123 changed = true;
124 }
125
126 xmlNode* library_visual_scenes = findChildElement(root, "library_visual_scenes");
127 if (library_visual_scenes) // (Assume there's only one of these)
128 {
129 xmlNode* visual_scene = findChildElement(library_visual_scenes, "visual_scene");
130 if (visual_scene) // (Assume there's only one of these)
131 {
132 for (xmlNode* child = visual_scene->children; child; child = child->next)
133 {
134 if (child->type == XML_ELEMENT_NODE && strcmp((const char*)child->name, "node") == 0)
135 if (applyFBXFixesNode(child))
136 changed = true;
137 }
138 }
139 }
140
141 return changed;
142 }
143
processDocument(xmlNode * root)144 static bool processDocument(xmlNode* root)
145 {
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;
152
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);
160 else
161 return false;
162 }
163
FixBrokenXML(const char * text,const char ** out,size_t * outSize)164 void FixBrokenXML(const char* text, const char** out, size_t* outSize)
165 {
166 Log(LOG_INFO, "Running FixBrokenXML");
167
168 size_t textSize = strlen(text);
169 xmlDocPtr doc = xmlParseMemory(text, (int)textSize);
170
171 xmlNode* root = xmlDocGetRootElement(doc);
172 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 xmlChar* mem = NULL;
177 int size = -1;
178 xmlDocDumpFormatMemory(doc, &mem, &size, 0);
179 *out = (const char*)mem;
180 *outSize = size;
181 }
182 else
183 {
184 *out = text;
185 *outSize = textSize;
186 }
187
188 xmlFreeDoc(doc);
189 }
190