/// <summary>Converts a dictionary to a MetadataKVP array.</summary> public static IList <MetadataKVP> DictionaryToArray(Dictionary <string, List <string> > dictionary) { Debug.Assert(dictionary != null); var list = new List <MetadataKVP>(); foreach (var kvp in dictionary) { if (kvp.Value == null) { MetadataKVP newKVP = new MetadataKVP() { key = kvp.Key, value = null, }; list.Add(newKVP); } else { foreach (var stringValue in kvp.Value) { MetadataKVP newKVP = new MetadataKVP() { key = kvp.Key, value = stringValue, }; list.Add(newKVP); } } } return(list); }
public static MetadataKVP[] DictionaryToArray(Dictionary <string, string> metaDictionary) { var array = new MetadataKVP[metaDictionary.Count]; int index = 0; foreach (var kvp in metaDictionary) { MetadataKVP newKVP = new MetadataKVP() { key = kvp.Key, value = kvp.Value }; array[index++] = newKVP; } return(array); }
/// <summary>Converts a dictionary to a MetadataKVP array.</summary> public static MetadataKVP[] DictionaryToArray(Dictionary <string, string> dictionary) { Debug.Assert(dictionary != null); var array = new MetadataKVP[dictionary.Count]; int index = 0; foreach (var kvp in dictionary) { MetadataKVP newKVP = new MetadataKVP() { key = kvp.Key, value = kvp.Value }; array[index++] = newKVP; } return(array); }
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); } }
/// <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(); } }