Пример #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());
        }
        /// <summary>Calculates changes made to a mod profile and submits them to the servers.</summary>
        private void SubmitModChanges_Internal(ModProfile profile)
        {
            // early outs
            if (profile == null)
            {
                this.SubmissionError_Local("Profile parameter passed to ModManager_SubmitModOperation.SubmitModChanges_Internal"
                                           + " was null. This was an unexpected error, please try submitting the mod again.");
                return;
            }
            if (profile.id == ModProfile.NULL_ID)
            {
                this.SubmissionError_Local("Profile parameter passed to ModManager_SubmitModOperation.SubmitModChanges_Internal"
                                           + " has a NULL_ID. This was an unexpected error, please try submitting the mod again.");
                return;
            }

            // --- Collect Submission Information ---
            this.modId = profile.id;

            // - Media -
            if (this.eModProfile.logoLocator.isDirty &&
                !string.IsNullOrEmpty(this.eModProfile.logoLocator.value.url))
            {
                this.logoPath = this.eModProfile.logoLocator.value.url;
            }

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

                this.addedImageFilePaths = new List <string>();
                foreach (var locator in this.eModProfile.galleryImageLocators.value)
                {
                    this.addedImageFilePaths.Add(locator.url);
                }
                foreach (var locator in profile.media.galleryImageLocators)
                {
                    this.addedImageFilePaths.Remove(locator.GetURL());
                }
            }

            if (this.eModProfile.sketchfabURLs.isDirty)
            {
                this.removedSketchfabURLs = new List <string>(profile.media.sketchfabURLs);
                foreach (string url in this.eModProfile.sketchfabURLs.value)
                {
                    this.removedSketchfabURLs.Remove(url);
                }

                this.addedSketchfabURLs = new List <string>(this.eModProfile.sketchfabURLs.value);
                foreach (string url in profile.media.sketchfabURLs)
                {
                    this.addedSketchfabURLs.Remove(url);
                }
            }

            if (this.eModProfile.youTubeURLs.isDirty)
            {
                this.removedYouTubeURLs = new List <string>(profile.media.youTubeURLs);
                foreach (string url in this.eModProfile.youTubeURLs.value)
                {
                    this.removedYouTubeURLs.Remove(url);
                }

                this.addedYouTubeURLs = new List <string>(this.eModProfile.youTubeURLs.value);
                foreach (string url in profile.media.youTubeURLs)
                {
                    this.addedYouTubeURLs.Remove(url);
                }
            }

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

                this.addedTags = new List <string>(this.eModProfile.tags.value);
                foreach (string tag in profile.tagNames)
                {
                    this.addedTags.Remove(tag);
                }
            }

            // - Metadata KVP -
            if (this.eModProfile.metadataKVPs.isDirty)
            {
                this.removedKVPs = MetadataKVP.ArrayToDictionary(profile.metadataKVPs);
                foreach (MetadataKVP kvp in this.eModProfile.metadataKVPs.value)
                {
                    string profileValue;

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

                this.addedKVPs = MetadataKVP.ArrayToDictionary(this.eModProfile.metadataKVPs.value);
                foreach (MetadataKVP kvp in profile.metadataKVPs)
                {
                    string editValue;

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

            // - Start submission chain -
            if (this.logoPath != null)
            {
                DataStorage.ReadFile(this.logoPath, this.SubmitModChanges_Internal_OnReadLogo);
            }
            else
            {
                this.SubmitModChanges_Internal_ZipImages();
            }
        }