Beispiel #1
0
        private static void SubmitModProfileComponents(ModProfile profile,
                                                       EditableModProfile modEdits,
                                                       Action <ModProfile> modSubmissionSucceeded,
                                                       Action <WebRequestError> modSubmissionFailed)
        {
            List <Action>       submissionActions      = new List <Action>();
            int                 nextActionIndex        = 0;
            Action <APIMessage> doNextSubmissionAction = (m) =>
            {
                if (nextActionIndex < submissionActions.Count)
                {
                    submissionActions[nextActionIndex++]();
                }
            };

            // - Media -
            if (modEdits.logoLocator.isDirty ||
                modEdits.youtubeURLs.isDirty ||
                modEdits.sketchfabURLs.isDirty ||
                modEdits.galleryImageLocators.isDirty)
            {
                var addMediaParameters    = new AddModMediaParameters();
                var deleteMediaParameters = new DeleteModMediaParameters();

                if (modEdits.logoLocator.isDirty &&
                    File.Exists(modEdits.logoLocator.value.url))
                {
                    addMediaParameters.logo = BinaryUpload.Create(Path.GetFileName(modEdits.logoLocator.value.url),
                                                                  File.ReadAllBytes(modEdits.logoLocator.value.url));
                }

                if (modEdits.youtubeURLs.isDirty)
                {
                    var addedYouTubeLinks = new List <string>(modEdits.youtubeURLs.value);
                    foreach (string youtubeLink in profile.media.youtubeURLs)
                    {
                        addedYouTubeLinks.Remove(youtubeLink);
                    }
                    addMediaParameters.youtube = addedYouTubeLinks.ToArray();

                    var removedTags = new List <string>(profile.media.youtubeURLs);
                    foreach (string youtubeLink in modEdits.youtubeURLs.value)
                    {
                        removedTags.Remove(youtubeLink);
                    }
                    deleteMediaParameters.youtube = addedYouTubeLinks.ToArray();
                }

                if (modEdits.sketchfabURLs.isDirty)
                {
                    var addedSketchfabLinks = new List <string>(modEdits.sketchfabURLs.value);
                    foreach (string sketchfabLink in profile.media.sketchfabURLs)
                    {
                        addedSketchfabLinks.Remove(sketchfabLink);
                    }
                    addMediaParameters.sketchfab = addedSketchfabLinks.ToArray();

                    var removedTags = new List <string>(profile.media.sketchfabURLs);
                    foreach (string sketchfabLink in modEdits.sketchfabURLs.value)
                    {
                        removedTags.Remove(sketchfabLink);
                    }
                    deleteMediaParameters.sketchfab = addedSketchfabLinks.ToArray();
                }

                if (modEdits.galleryImageLocators.isDirty)
                {
                    var addedImageFilePaths = new List <string>();
                    foreach (var locator in modEdits.galleryImageLocators.value)
                    {
                        if (File.Exists(locator.url))
                        {
                            addedImageFilePaths.Add(locator.url);
                        }
                    }
                    // - Create Images.Zip -
                    if (addedImageFilePaths.Count > 0)
                    {
                        string galleryZipLocation
                            = Application.temporaryCachePath + "/modio/imageGallery_" + DateTime.Now.ToFileTime() + ".zip";

                        try
                        {
                            Directory.CreateDirectory(Path.GetDirectoryName(galleryZipLocation));

                            using (var zip = new Ionic.Zip.ZipFile())
                            {
                                foreach (string imageFilePath in addedImageFilePaths)
                                {
                                    zip.AddFile(imageFilePath);
                                }
                                zip.Save(galleryZipLocation);
                            }

                            var imageGalleryUpload = BinaryUpload.Create("images.zip",
                                                                         File.ReadAllBytes(galleryZipLocation));

                            addMediaParameters.galleryImages = imageGalleryUpload;
                        }
                        catch (Exception e)
                        {
                            Debug.LogError("[mod.io] Unable to zip image gallery prior to uploading.\n\n"
                                           + Utility.GenerateExceptionDebugString(e));
                        }
                    }

                    var removedImageFileNames = new List <string>();
                    foreach (var locator in profile.media.galleryImageLocators)
                    {
                        removedImageFileNames.Add(locator.fileName);
                    }
                    foreach (var locator in modEdits.galleryImageLocators.value)
                    {
                        removedImageFileNames.Remove(locator.fileName);
                    }

                    if (removedImageFileNames.Count > 0)
                    {
                        deleteMediaParameters.images = removedImageFileNames.ToArray();
                    }
                }

                if (addMediaParameters.stringValues.Count > 0 ||
                    addMediaParameters.binaryData.Count > 0)
                {
                    submissionActions.Add(() =>
                    {
                        APIClient.AddModMedia(profile.id,
                                              addMediaParameters,
                                              doNextSubmissionAction, modSubmissionFailed);
                    });
                }
                if (deleteMediaParameters.stringValues.Count > 0)
                {
                    submissionActions.Add(() =>
                    {
                        APIClient.DeleteModMedia(profile.id,
                                                 deleteMediaParameters,
                                                 () => doNextSubmissionAction(null),
                                                 modSubmissionFailed);
                    });
                }
            }

            // - Tags -
            if (modEdits.tags.isDirty)
            {
                var removedTags = new List <string>(profile.tagNames);
                foreach (string tag in modEdits.tags.value)
                {
                    removedTags.Remove(tag);
                }
                var addedTags = new List <string>(modEdits.tags.value);
                foreach (string tag in profile.tagNames)
                {
                    addedTags.Remove(tag);
                }

                if (removedTags.Count > 0)
                {
                    submissionActions.Add(() =>
                    {
                        var parameters      = new DeleteModTagsParameters();
                        parameters.tagNames = removedTags.ToArray();
                        APIClient.DeleteModTags(profile.id, parameters,
                                                doNextSubmissionAction, modSubmissionFailed);
                    });
                }
                if (addedTags.Count > 0)
                {
                    submissionActions.Add(() =>
                    {
                        var parameters      = new AddModTagsParameters();
                        parameters.tagNames = addedTags.ToArray();
                        APIClient.AddModTags(profile.id, parameters,
                                             doNextSubmissionAction, modSubmissionFailed);
                    });
                }
            }

            // - Metadata KVP -
            if (modEdits.metadataKVPs.isDirty)
            {
                var removedKVPs = MetadataKVP.ArrayToDictionary(profile.metadataKVPs);
                var addedKVPs   = MetadataKVP.ArrayToDictionary(modEdits.metadataKVPs.value);

                foreach (MetadataKVP kvp in modEdits.metadataKVPs.value)
                {
                    string profileValue;

                    // if edited kvp is exact match it's not removed
                    if (removedKVPs.TryGetValue(kvp.key, out profileValue) &&
                        profileValue == kvp.value)
                    {
                        removedKVPs.Remove(kvp.key);
                    }
                }

                foreach (MetadataKVP kvp in profile.metadataKVPs)
                {
                    string editValue;

                    // if profile kvp is exact match it's not new
                    if (addedKVPs.TryGetValue(kvp.key, out editValue) &&
                        editValue == kvp.value)
                    {
                        addedKVPs.Remove(kvp.key);
                    }
                }

                if (removedKVPs.Count > 0)
                {
                    submissionActions.Add(() =>
                    {
                        var parameters          = new DeleteModKVPMetadataParameters();
                        parameters.metadataKeys = removedKVPs.Keys.ToArray();
                        APIClient.DeleteModKVPMetadata(profile.id, parameters,
                                                       doNextSubmissionAction,
                                                       modSubmissionFailed);
                    });
                }

                if (addedKVPs.Count > 0)
                {
                    string[] addedKVPStrings = AddModKVPMetadataParameters.ConvertMetadataKVPsToAPIStrings(MetadataKVP.DictionaryToArray(addedKVPs));

                    submissionActions.Add(() =>
                    {
                        var parameters      = new AddModKVPMetadataParameters();
                        parameters.metadata = addedKVPStrings;
                        APIClient.AddModKVPMetadata(profile.id, parameters,
                                                    doNextSubmissionAction,
                                                    modSubmissionFailed);
                    });
                }
            }

            // - Get Updated Profile -
            submissionActions.Add(() => APIClient.GetMod(profile.id,
                                                         modSubmissionSucceeded,
                                                         modSubmissionFailed));

            // - Start submission chain -
            doNextSubmissionAction(new APIMessage());
        }
        private void SubmitNextParameter(object o)
        {
            // - Media -
            if ((this.removedImageFileNames != null && this.removedImageFileNames.Count > 0) ||
                (this.removedSketchfabURLs != null && this.removedSketchfabURLs.Count > 0) ||
                (this.removedYouTubeURLs != null && this.removedYouTubeURLs.Count > 0))
            {
                var parameters = new DeleteModMediaParameters();

                if (this.removedImageFileNames != null)
                {
                    parameters.images = this.removedImageFileNames.ToArray();
                }
                if (this.removedSketchfabURLs != null)
                {
                    parameters.sketchfab = this.removedSketchfabURLs.ToArray();
                }
                if (this.removedYouTubeURLs != null)
                {
                    parameters.youtube = this.removedYouTubeURLs.ToArray();
                }

                APIClient.DeleteModMedia(this.modId, parameters,
                                         this.SubmitNextParameter,
                                         this.onError);

                this.removedImageFileNames = null;
                this.removedSketchfabURLs  = null;
                this.removedYouTubeURLs    = null;
            }
            else if ((this.logoData != null) ||
                     (this.imageArchiveData != null) ||
                     (this.addedSketchfabURLs != null && this.addedSketchfabURLs.Count > 0) ||
                     (this.addedYouTubeURLs != null && this.addedYouTubeURLs.Count > 0))
            {
                var parameters = new AddModMediaParameters();

                if (this.logoData != null)
                {
                    parameters.logo = BinaryUpload.Create(Path.GetFileName(this.logoPath), this.logoData);
                }
                if (this.imageArchiveData != null)
                {
                    parameters.galleryImages = BinaryUpload.Create("images.zip", this.imageArchiveData);
                }
                if (this.addedSketchfabURLs != null && this.addedSketchfabURLs.Count > 0)
                {
                    parameters.sketchfab = this.addedSketchfabURLs.ToArray();
                }
                if (this.addedYouTubeURLs != null && this.addedYouTubeURLs.Count > 0)
                {
                    parameters.youtube = this.addedYouTubeURLs.ToArray();
                }

                APIClient.AddModMedia(this.modId, parameters,
                                      this.SubmitNextParameter,
                                      this.onError);

                this.logoData           = null;
                this.imageArchiveData   = null;
                this.addedSketchfabURLs = null;
                this.addedYouTubeURLs   = null;
            }
            // - Tags -
            else if (this.removedTags != null && this.removedTags.Count > 0)
            {
                var parameters = new DeleteModTagsParameters();
                parameters.tagNames = this.removedTags.ToArray();

                APIClient.DeleteModTags(this.modId, parameters,
                                        this.SubmitNextParameter,
                                        this.onError);

                this.removedTags = null;
            }
            else if (this.addedTags != null && this.addedTags.Count > 0)
            {
                var parameters = new AddModTagsParameters();
                parameters.tagNames = this.addedTags.ToArray();

                APIClient.AddModTags(this.modId, parameters,
                                     this.SubmitNextParameter,
                                     this.onError);

                this.addedTags = null;
            }
            // - KVPs -
            else if (this.removedKVPs != null && this.removedKVPs.Count > 0)
            {
                var parameters = new DeleteModKVPMetadataParameters();
                parameters.metadataKeys = this.removedKVPs.Keys.ToArray();

                APIClient.DeleteModKVPMetadata(this.modId, parameters,
                                               this.SubmitNextParameter,
                                               this.onError);
                this.removedKVPs = null;
            }
            else if (this.addedKVPs != null && this.addedKVPs.Count > 0)
            {
                string[] addedKVPStrings = AddModKVPMetadataParameters.ConvertMetadataKVPsToAPIStrings(MetadataKVP.DictionaryToArray(this.addedKVPs));

                var parameters = new AddModKVPMetadataParameters();
                parameters.metadata = addedKVPStrings;

                APIClient.AddModKVPMetadata(this.modId, parameters,
                                            this.SubmitNextParameter,
                                            this.onError);

                this.addedKVPs = null;
            }
            // - FINALIZE -
            else if (o != null && o is ModProfile && ((ModProfile)o).id == this.modId)
            {
                if (this.onSuccess != null)
                {
                    this.onSuccess.Invoke((ModProfile)o);
                }
            }
            else
            {
                APIClient.GetMod(this.modId, this.onSuccess, this.onError);
            }
        }