| 92 | #endif |
| 93 | |
| 94 | bool JsonStorageProxy::ChangeId(const StringView& path, const Guid& newId) |
| 95 | { |
| 96 | #if USE_EDITOR |
| 97 | PROFILE_CPU(); |
| 98 | |
| 99 | // Load file |
| 100 | Array<byte> fileData; |
| 101 | if (File::ReadAllBytes(path, fileData)) |
| 102 | return false; |
| 103 | |
| 104 | // Parse data |
| 105 | rapidjson_flax::Document document; |
| 106 | { |
| 107 | PROFILE_CPU_NAMED("Json.Parse"); |
| 108 | document.Parse((const char*)fileData.Get(), fileData.Count()); |
| 109 | } |
| 110 | if (document.HasParseError()) |
| 111 | { |
| 112 | Log::JsonParseException(document.GetParseError(), document.GetErrorOffset(), path); |
| 113 | return false; |
| 114 | } |
| 115 | |
| 116 | // Get all IDs inside the file |
| 117 | HashSet<Guid> ids; |
| 118 | FindObjectIds(document, document, ids); |
| 119 | |
| 120 | // Remap into a unique IDs |
| 121 | Dictionary<Guid, Guid> remap; |
| 122 | remap.EnsureCapacity(ids.Count()); |
| 123 | for (const auto& id : ids) |
| 124 | remap.Add(id.Item, Guid::New()); |
| 125 | |
| 126 | // Remap asset ID using the provided value |
| 127 | auto idNode = document.FindMember("ID"); |
| 128 | if (idNode == document.MemberEnd()) |
| 129 | return true; |
| 130 | remap[JsonTools::GetGuid(idNode->value)] = newId; |
| 131 | |
| 132 | // Change IDs of asset and objects inside asset |
| 133 | JsonTools::ChangeIds(document, remap); |
| 134 | |
| 135 | // Save to file |
| 136 | rapidjson_flax::StringBuffer buffer; |
| 137 | PrettyJsonWriter writer(buffer); |
| 138 | document.Accept(writer.GetWriter()); |
| 139 | if (File::WriteAllBytes(path, (byte*)buffer.GetString(), (int32)buffer.GetSize())) |
| 140 | return true; |
| 141 | |
| 142 | return false; |
| 143 | #else |
| 144 | LOG(Warning, "Editing cooked content is invalid."); |
| 145 | return true; |
| 146 | #endif |
| 147 | } |
nothing calls this directly
no test coverage detected