import re import codecs path = 'Source/MetaCoreEditor/Private/MetaCoreBuiltinCoreServicesModule.cpp' with codecs.open(path, 'r', 'utf-8') as f: text = f.read() if '#include ' not in text and '#include "cgltf.h"' not in text: text = text.replace('#include ', '#include \r\n#include "cgltf.h"') pattern = re.compile(r'\[\[nodiscard\]\] MetaCoreModelAssetDocument MetaCoreBuildImportedGltfAssetDocument\([\s\S]*?\)\s*\{[\s\S]*?\n\}\n\n\[\[nodiscard\]\] MetaCoreModelAssetDocument MetaCoreBuildImportedGenericModelAssetDocument') replacement = '''[[nodiscard]] MetaCoreModelAssetDocument MetaCoreBuildImportedGltfAssetDocument( const std::filesystem::path& absoluteSourcePath, const std::filesystem::path& relativeSourcePath, std::uint64_t sourceHash ) { MetaCoreModelAssetDocument document; document.AssetType = "model"; document.ImporterId = "GltfModelImporter"; document.SourcePath = relativeSourcePath; document.SourceHash = sourceHash; document.ModelKind = MetaCoreModelAssetKind::Gltf; document.SourceFormat = absoluteSourcePath.extension() == ".glb" ? "glb" : "gltf"; cgltf_options options = {}; cgltf_data* data = nullptr; cgltf_result result = cgltf_parse_file(&options, absoluteSourcePath.string().c_str(), &data); if (result != cgltf_result_success) { MetaCoreImportedGltfNodeDocument rootNode; rootNode.Name = absoluteSourcePath.stem().string(); document.Nodes.push_back(std::move(rootNode)); return document; } result = cgltf_load_buffers(&options, data, absoluteSourcePath.string().c_str()); if (result != cgltf_result_success) { cgltf_free(data); MetaCoreImportedGltfNodeDocument rootNode; rootNode.Name = absoluteSourcePath.stem().string(); document.Nodes.push_back(std::move(rootNode)); return document; } if (data->images_count > 0) { for (std::size_t i = 0; i < data->images_count; ++i) { const cgltf_image& image = data->images[i]; MetaCoreImportedGltfTextureDocument textureDoc; textureDoc.SourcePath = image.uri ? std::filesystem::path(image.uri).lexically_normal() : std::filesystem::path{}; textureDoc.UsageHint = "Unknown"; MetaCoreTextureAssetDocument genTex; genTex.AssetGuid = MetaCoreMakeDeterministicGeneratedAssetGuid(relativeSourcePath, "texture", image.name ? image.name : ("tex_" + std::to_string(i)), i); genTex.Name = image.name ? image.name : ("Texture_" + std::to_string(i)); genTex.SourcePath = textureDoc.SourcePath; genTex.UsageHint = "Unknown"; document.Textures.push_back(std::move(textureDoc)); document.GeneratedTextureAssets.push_back(std::move(genTex)); } } if (data->materials_count > 0) { for (std::size_t i = 0; i < data->materials_count; ++i) { const cgltf_material& mat = data->materials[i]; MetaCoreImportedGltfMaterialDocument matDoc; matDoc.Name = mat.name ? mat.name : ("Material_" + std::to_string(i)); matDoc.DoubleSided = mat.double_sided; MetaCoreMaterialAssetDocument genMat; genMat.AssetGuid = MetaCoreMakeDeterministicGeneratedAssetGuid(relativeSourcePath, "material", mat.name ? mat.name : ("mat_" + std::to_string(i)), i); genMat.Name = matDoc.Name; genMat.DoubleSided = mat.double_sided; genMat.AlphaCutoff = mat.alpha_cutoff; genMat.AlphaMode = mat.alpha_mode == cgltf_alpha_mode_blend ? MetaCoreMaterialAlphaMode::Blend : (mat.alpha_mode == cgltf_alpha_mode_mask ? MetaCoreMaterialAlphaMode::Mask : MetaCoreMaterialAlphaMode::Opaque); if (mat.has_pbr_metallic_roughness) { genMat.BaseColor = glm::vec3(mat.pbr_metallic_roughness.base_color_factor[0], mat.pbr_metallic_roughness.base_color_factor[1], mat.pbr_metallic_roughness.base_color_factor[2]); genMat.Metallic = mat.pbr_metallic_roughness.metallic_factor; genMat.Roughness = mat.pbr_metallic_roughness.roughness_factor; if (mat.pbr_metallic_roughness.base_color_texture.texture) { const std::int32_t texIdx = static_cast(mat.pbr_metallic_roughness.base_color_texture.texture->image - data->images); matDoc.TextureIndices.push_back(texIdx); genMat.BaseColorTexture = document.GeneratedTextureAssets[texIdx].AssetGuid; } } document.Materials.push_back(std::move(matDoc)); document.GeneratedMaterialAssets.push_back(std::move(genMat)); } } if (data->meshes_count > 0) { for (std::size_t i = 0; i < data->meshes_count; ++i) { const cgltf_mesh& mesh = data->meshes[i]; MetaCoreImportedGltfMeshDocument meshDoc; meshDoc.Name = mesh.name ? mesh.name : ("Mesh_" + std::to_string(i)); meshDoc.PrimitiveCount = static_cast(mesh.primitives_count); MetaCoreMeshAssetDocument genMesh; genMesh.AssetGuid = MetaCoreMakeDeterministicGeneratedAssetGuid(relativeSourcePath, "mesh", mesh.name ? mesh.name : ("mesh_" + std::to_string(i)), i); genMesh.Name = meshDoc.Name; for (std::size_t p = 0; p < mesh.primitives_count; ++p) { const cgltf_primitive& prim = mesh.primitives[p]; std::int32_t matIdx = -1; if (prim.material) { matIdx = static_cast(prim.material - data->materials); meshDoc.MaterialSlots.push_back(matIdx); } MetaCoreMeshSubMeshDocument subMesh; subMesh.Name = "Primitive_" + std::to_string(p); subMesh.MaterialSlotIndex = matIdx; genMesh.SubMeshes.push_back(std::move(subMesh)); } document.Meshes.push_back(std::move(meshDoc)); document.GeneratedMeshAssets.push_back(std::move(genMesh)); } } if (data->nodes_count > 0) { for (std::size_t i = 0; i < data->nodes_count; ++i) { const cgltf_node& node = data->nodes[i]; MetaCoreImportedGltfNodeDocument nodeDoc; nodeDoc.Name = node.name ? node.name : ("Node_" + std::to_string(i)); nodeDoc.ParentIndex = node.parent ? static_cast(node.parent - data->nodes) : -1; nodeDoc.MeshIndex = node.mesh ? static_cast(node.mesh - data->meshes) : -1; document.Nodes.push_back(std::move(nodeDoc)); } } else { MetaCoreImportedGltfNodeDocument rootNode; rootNode.Name = absoluteSourcePath.stem().string(); document.Nodes.push_back(std::move(rootNode)); } cgltf_free(data); return document; } [[nodiscard]] MetaCoreModelAssetDocument MetaCoreBuildImportedGenericModelAssetDocument''' new_text = pattern.sub(replacement, text) if text == new_text: print('Failed to replace.') else: print('Successfully replaced.') with codecs.open(path, 'w', 'utf-8') as f: f.write(new_text)