1 /* Copyright (C) 2023 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"
26 #include <libxml/parser.h>
30 Things that are fixed here:
34 3ds Max "file://" image URLs
36 Identifier: /COLLADA/asset/contributor/authoring_tool = "FBX COLLADA exporter"
38 Problem: /COLLADA/library_images/image/init_from = "file://" which crashes some versions of FCollada
40 Fix: Delete the whole library_images subtree, since we never use it anyway.
41 Then delete library_effects and library_materials too, to avoid broken references.
45 3ds Max broken material references
47 Identifier: /COLLADA/asset/contributor/authoring_tool = "FBX COLLADA exporter"
49 Problem: /COLLADA/library_visual_scenes/.../instance_material/@target sometimes
50 refers to non-existent material IDs.
52 Fix: Delete the whole bind_material subtree, since we never use it anyway.
58 static xmlNode
* findChildElement(xmlNode
* node
, const char* name
)
60 for (xmlNode
* child
= node
->children
; child
; child
= child
->next
)
62 if (child
->type
== XML_ELEMENT_NODE
&& strcmp((const char*)child
->name
, name
) == 0)
68 static bool applyFBXFixesNode(xmlNode
* node
)
71 for (xmlNode
* child
= node
->children
; child
; child
= child
->next
)
73 if (child
->type
== XML_ELEMENT_NODE
)
75 if (strcmp((const char*)child
->name
, "node") == 0)
77 if (applyFBXFixesNode(child
))
80 else if (strcmp((const char*)child
->name
, "instance_geometry") == 0)
82 xmlNode
* bind_material
= findChildElement(child
, "bind_material");
83 if (! bind_material
) continue;
84 Log(LOG_INFO
, "Found a bind_material to delete");
85 xmlUnlinkNode(bind_material
);
86 xmlFreeNode(bind_material
);
95 static bool applyFBXFixes(xmlNode
* root
)
97 Log(LOG_INFO
, "Applying fixes for 3ds Max exporter");
101 xmlNode
* library_images
= findChildElement(root
, "library_images");
104 Log(LOG_INFO
, "Found library_images to delete");
105 xmlUnlinkNode(library_images
);
106 xmlFreeNode(library_images
);
110 xmlNode
* library_materials
= findChildElement(root
, "library_materials");
111 if (library_materials
)
113 Log(LOG_INFO
, "Found library_materials to delete");
114 xmlUnlinkNode(library_materials
);
115 xmlFreeNode(library_materials
);
119 xmlNode
* library_effects
= findChildElement(root
, "library_effects");
122 Log(LOG_INFO
, "Found library_effects to delete");
123 xmlUnlinkNode(library_effects
);
124 xmlFreeNode(library_effects
);
128 xmlNode
* library_visual_scenes
= findChildElement(root
, "library_visual_scenes");
129 if (library_visual_scenes
) // (Assume there's only one of these)
131 xmlNode
* visual_scene
= findChildElement(library_visual_scenes
, "visual_scene");
132 if (visual_scene
) // (Assume there's only one of these)
134 for (xmlNode
* child
= visual_scene
->children
; child
; child
= child
->next
)
136 if (child
->type
== XML_ELEMENT_NODE
&& strcmp((const char*)child
->name
, "node") == 0)
137 if (applyFBXFixesNode(child
))
146 static bool processDocument(xmlNode
* root
)
148 xmlNode
* asset
= findChildElement(root
, "asset");
149 if (! asset
) return false;
150 xmlNode
* contributor
= findChildElement(asset
, "contributor");
151 if (! contributor
) return false;
152 xmlNode
* authoring_tool
= findChildElement(contributor
, "authoring_tool");
153 if (! authoring_tool
) return false;
155 xmlNode
* authoring_tool_text
= authoring_tool
->children
;
156 if (! authoring_tool_text
) return false;
157 if (authoring_tool_text
->type
!= XML_TEXT_NODE
) return false;
158 xmlChar
* toolname
= authoring_tool_text
->content
;
159 Log(LOG_INFO
, "Authoring tool: %s", toolname
);
160 if (strcmp((const char*)toolname
, "FBX COLLADA exporter") == 0)
161 return applyFBXFixes(root
);
166 void FixBrokenXML(const char* text
, const char** out
, size_t* outSize
)
168 Log(LOG_INFO
, "Running FixBrokenXML");
170 size_t textSize
= strlen(text
);
171 xmlDocPtr doc
= xmlParseMemory(text
, (int)textSize
);
173 xmlNode
* root
= xmlDocGetRootElement(doc
);
174 if (root
&& processDocument(root
))
176 // Reserialising the document, then parsing it again inside FCollada, is a bit ugly;
177 // but it's the only way I can see to make it work through FCollada's public API
180 xmlDocDumpFormatMemory(doc
, &mem
, &size
, 0);
181 *out
= (const char*)mem
;