| 112 | } |
| 113 | |
| 114 | CreateAssetResult CreateAssetContext::Run(const CreateAssetFunction& callback) |
| 115 | { |
| 116 | ASSERT(callback.IsBinded()); |
| 117 | |
| 118 | // Call action |
| 119 | auto result = callback(*this); |
| 120 | if (result != CreateAssetResult::Ok) |
| 121 | return result; |
| 122 | |
| 123 | // Skip for non-flax assets (eg. json resource or custom asset type) |
| 124 | if (!TargetAssetPath.EndsWith(ASSET_FILES_EXTENSION)) |
| 125 | return CreateAssetResult::Ok; |
| 126 | |
| 127 | // Validate assigned TypeID |
| 128 | if (Data.Header.TypeName.IsEmpty()) |
| 129 | { |
| 130 | LOG(Warning, "Assigned asset TypeName is invalid."); |
| 131 | return CreateAssetResult::InvalidTypeID; |
| 132 | } |
| 133 | |
| 134 | // Add import metadata to the file (if it's empty) |
| 135 | if (!SkipMetadata && Data.Metadata.IsInvalid()) |
| 136 | { |
| 137 | rapidjson_flax::StringBuffer buffer; |
| 138 | CompactJsonWriter writer(buffer); |
| 139 | writer.StartObject(); |
| 140 | { |
| 141 | AddMeta(writer); |
| 142 | } |
| 143 | writer.EndObject(); |
| 144 | Data.Metadata.Copy((const byte*)buffer.GetString(), (uint32)buffer.GetSize()); |
| 145 | } |
| 146 | |
| 147 | // Check if target asset already exists but has different type |
| 148 | AssetInfo targetAssetInfo; |
| 149 | if (Content::GetAssetInfo(TargetAssetPath, targetAssetInfo) && !IsAssetTypeNameMatch(targetAssetInfo.TypeName, Data.Header.TypeName)) |
| 150 | { |
| 151 | // Change path |
| 152 | int32 index = 0; |
| 153 | String newTargetAssetPath; |
| 154 | do |
| 155 | { |
| 156 | newTargetAssetPath = StringUtils::GetDirectoryName(TargetAssetPath); |
| 157 | newTargetAssetPath /= StringUtils::GetFileNameWithoutExtension(TargetAssetPath) + String::Format(TEXT(" ({})."), index++) + FileSystem::GetExtension(TargetAssetPath); |
| 158 | } while (index < 100 && FileSystem::FileExists(newTargetAssetPath)); |
| 159 | TargetAssetPath = newTargetAssetPath; |
| 160 | |
| 161 | // Change id |
| 162 | Data.Header.ID = Guid::New(); |
| 163 | } |
| 164 | |
| 165 | // Save file |
| 166 | result = FlaxStorage::Create(OutputPath, Data) ? CreateAssetResult::CannotSaveFile : CreateAssetResult::Ok; |
| 167 | if (result == CreateAssetResult::Ok) |
| 168 | { |
| 169 | _applyChangesResult = CreateAssetResult::Abort; |
| 170 | INVOKE_ON_MAIN_THREAD(CreateAssetContext, CreateAssetContext::ApplyChanges, this); |
| 171 | result = _applyChangesResult; |
no test coverage detected