Line data Source code
1 : /* Copyright (C) 2025 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 "DLL.h" 23 : 24 : #include <cstring> 25 : #include <libxml/parser.h> 26 : #include <libxml/tree.h> 27 : #include <libxml/xmlstring.h> 28 : 29 : /* 30 : 31 : Things that are fixed here: 32 : 33 : ---- 34 : 35 : 3ds Max "file://" image URLs 36 : 37 : Identifier: /COLLADA/asset/contributor/authoring_tool = "FBX COLLADA exporter" 38 : 39 : Problem: /COLLADA/library_images/image/init_from = "file://" which crashes some versions of FCollada 40 : 41 : Fix: Delete the whole library_images subtree, since we never use it anyway. 42 : Then delete library_effects and library_materials too, to avoid broken references. 43 : 44 : ---- 45 : 46 : 3ds Max broken material references 47 : 48 : Identifier: /COLLADA/asset/contributor/authoring_tool = "FBX COLLADA exporter" 49 : 50 : Problem: /COLLADA/library_visual_scenes/.../instance_material/@target sometimes 51 : refers to non-existent material IDs. 52 : 53 : Fix: Delete the whole bind_material subtree, since we never use it anyway. 54 : 55 : ---- 56 : 57 : */ 58 : 59 20 : static xmlNode* findChildElement(xmlNode* node, const char* name) 60 : { 61 68 : for (xmlNode* child = node->children; child; child = child->next) 62 : { 63 67 : if (child->type == XML_ELEMENT_NODE && strcmp((const char*)child->name, name) == 0) 64 19 : return child; 65 : } 66 : return NULL; 67 : } 68 : 69 2 : static bool applyFBXFixesNode(xmlNode* node) 70 : { 71 2 : bool changed = false; 72 22 : for (xmlNode* child = node->children; child; child = child->next) 73 : { 74 20 : if (child->type == XML_ELEMENT_NODE) 75 : { 76 9 : if (strcmp((const char*)child->name, "node") == 0) 77 : { 78 1 : if (applyFBXFixesNode(child)) 79 2 : changed = true; 80 : } 81 8 : else if (strcmp((const char*)child->name, "instance_geometry") == 0) 82 : { 83 1 : xmlNode* bind_material = findChildElement(child, "bind_material"); 84 1 : if (! bind_material) continue; 85 1 : Log(LOG_INFO, "Found a bind_material to delete"); 86 1 : xmlUnlinkNode(bind_material); 87 1 : xmlFreeNode(bind_material); 88 : 89 1 : changed = true; 90 : } 91 : } 92 : } 93 2 : return changed; 94 : } 95 : 96 1 : static bool applyFBXFixes(xmlNode* root) 97 : { 98 1 : Log(LOG_INFO, "Applying fixes for 3ds Max exporter"); 99 : 100 1 : bool changed = false; 101 : 102 1 : xmlNode* library_images = findChildElement(root, "library_images"); 103 1 : if (library_images) 104 : { 105 1 : Log(LOG_INFO, "Found library_images to delete"); 106 1 : xmlUnlinkNode(library_images); 107 1 : xmlFreeNode(library_images); 108 1 : changed = true; 109 : } 110 : 111 1 : xmlNode* library_materials = findChildElement(root, "library_materials"); 112 1 : if (library_materials) 113 : { 114 1 : Log(LOG_INFO, "Found library_materials to delete"); 115 1 : xmlUnlinkNode(library_materials); 116 1 : xmlFreeNode(library_materials); 117 1 : changed = true; 118 : } 119 : 120 1 : xmlNode* library_effects = findChildElement(root, "library_effects"); 121 1 : if (library_effects) 122 : { 123 1 : Log(LOG_INFO, "Found library_effects to delete"); 124 1 : xmlUnlinkNode(library_effects); 125 1 : xmlFreeNode(library_effects); 126 1 : changed = true; 127 : } 128 : 129 1 : xmlNode* library_visual_scenes = findChildElement(root, "library_visual_scenes"); 130 1 : if (library_visual_scenes) // (Assume there's only one of these) 131 : { 132 1 : xmlNode* visual_scene = findChildElement(library_visual_scenes, "visual_scene"); 133 1 : if (visual_scene) // (Assume there's only one of these) 134 : { 135 4 : for (xmlNode* child = visual_scene->children; child; child = child->next) 136 : { 137 3 : if (child->type == XML_ELEMENT_NODE && strcmp((const char*)child->name, "node") == 0) 138 1 : if (applyFBXFixesNode(child)) 139 3 : changed = true; 140 : } 141 : } 142 : } 143 : 144 1 : return changed; 145 : } 146 : 147 5 : static bool processDocument(xmlNode* root) 148 : { 149 5 : xmlNode* asset = findChildElement(root, "asset"); 150 5 : if (! asset) return false; 151 5 : xmlNode* contributor = findChildElement(asset, "contributor"); 152 5 : if (! contributor) return false; 153 4 : xmlNode* authoring_tool = findChildElement(contributor, "authoring_tool"); 154 4 : if (! authoring_tool) return false; 155 : 156 4 : xmlNode* authoring_tool_text = authoring_tool->children; 157 4 : if (! authoring_tool_text) return false; 158 4 : if (authoring_tool_text->type != XML_TEXT_NODE) return false; 159 4 : xmlChar* toolname = authoring_tool_text->content; 160 4 : Log(LOG_INFO, "Authoring tool: %s", toolname); 161 4 : if (strcmp((const char*)toolname, "FBX COLLADA exporter") == 0) 162 1 : return applyFBXFixes(root); 163 : else 164 : return false; 165 : } 166 : 167 6 : void FixBrokenXML(const char* text, const char** out, size_t* outSize) 168 : { 169 6 : Log(LOG_INFO, "Running FixBrokenXML"); 170 : 171 6 : size_t textSize = strlen(text); 172 6 : xmlDocPtr doc = xmlParseMemory(text, (int)textSize); 173 : 174 6 : xmlNode* root = xmlDocGetRootElement(doc); 175 6 : if (root && processDocument(root)) 176 : { 177 : // Reserialising the document, then parsing it again inside FCollada, is a bit ugly; 178 : // but it's the only way I can see to make it work through FCollada's public API 179 1 : xmlChar* mem = NULL; 180 1 : int size = -1; 181 1 : xmlDocDumpFormatMemory(doc, &mem, &size, 0); 182 1 : *out = (const char*)mem; 183 1 : *outSize = size; 184 : } 185 : else 186 : { 187 5 : *out = text; 188 5 : *outSize = textSize; 189 : } 190 : 191 6 : xmlFreeDoc(doc); 192 6 : }