Пример #1
0
        public void RegisterBuiltInPreset(byte[] data, PresetsCategory category, params string[] localFilename)
        {
            var directory = GetDirectory(category);

            GetBuiltInPresetsList(category).Add(new BuiltInPresetEntry(directory,
                                                                       Path.Combine(directory, Path.Combine(localFilename)) + category.Extension, category.Extension, data));
        }
Пример #2
0
 private List <BuiltInPresetEntry> GetBuiltInPresetsList(PresetsCategory category)
 {
     if (_builtInPresets.ContainsKey(category))
     {
         return(_builtInPresets[category]);
     }
     return(_builtInPresets[category] = new List <BuiltInPresetEntry>(1));
 }
Пример #3
0
 public string GetDirectory(PresetsCategory category)
 {
     if (category.DirectoryName.IndexOf(':') != -1)
     {
         return(category.DirectoryName);
     }
     return(base.GetDirectory(category.DirectoryName));
 }
Пример #4
0
        public byte[] GetBuiltInPresetData(string categoryName, params string[] localFilename)
        {
            var category  = new PresetsCategory(categoryName);
            var directory = GetDirectory(category);
            var filename  = Path.Combine(directory, Path.Combine(localFilename)) + category.Extension;

            return(GetBuiltInPresetsList(category).FirstOrDefault(x => x.VirtualFilename == filename)?.ReadBinaryData());
        }
Пример #5
0
        public string EnsureDirectory(PresetsCategory category)
        {
            var directory = GetDirectory(category);

            if (!Directory.Exists(directory))
            {
                Directory.CreateDirectory(directory);
            }
            return(directory);
        }
Пример #6
0
        public IEnumerable <ISavedPresetEntry> GetSavedPresets(PresetsCategory category)
        {
            var directory = GetDirectory(category);
            var filesList = FileUtils.GetFilesRecursive(directory, @"*" + category.Extension)
                            .Select(x => new SavedPresetEntry(directory, category.Extension, x))
                            .ToList <ISavedPresetEntry>();

            return(filesList.Concat(GetBuiltInPresets(category)
                                    .Where(x => filesList.All(y => x.VirtualFilename != y.VirtualFilename))).OrderBy(x => x.VirtualFilename));
        }
Пример #7
0
        public IEnumerable <ISavedPresetEntry> GetSavedPresets(PresetsCategory category)
        {
            var directory = GetDirectory(category);
            var filesList = FileUtils.GetFilesRecursive(directory)
                            .Where(x => x.ToLowerInvariant().EndsWith(category.Extension))
                            .Select(x => new SavedPresetEntry(directory, category.Extension, x))
                            .ToList <ISavedPresetEntry>();

            return(filesList.Union(GetBuiltInPresetsList(category)
                                   .Where(x => filesList.All(y => x.Filename != y.Filename))).OrderBy(x => x.Filename));
        }
Пример #8
0
        public bool SavePresetUsingDialog([CanBeNull] string key, [NotNull] PresetsCategory category, [CanBeNull] byte[] data,
                                          [CanBeNull] ref string filename)
        {
            if (data == null)
            {
                return(false);
            }

            var presetsDirectory = EnsureDirectory(category);

            if (filename != null && !FileUtils.IsAffectedBy(filename, presetsDirectory))
            {
                filename = Path.Combine(presetsDirectory, Path.GetFileName(filename));
            }

            filename = FileRelatedDialogs.Save(new SaveDialogParams {
                Filters          = { new DialogFilterPiece("Presets", "*" + category.Extension) },
                InitialDirectory = presetsDirectory,
                DetaultExtension = category.Extension,
                CustomPlaces     =
                {
                    new FileDialogCustomPlace(presetsDirectory)
                }
            }, filename);
            if (filename == null)
            {
                return(false);
            }

            Logging.Debug("presetsDirectory=" + presetsDirectory);
            Logging.Debug("filename=" + filename);

            if (!FileUtils.IsAffectedBy(filename, presetsDirectory))
            {
                if (ModernDialog.ShowMessage(ToolsStrings.Presets_ChooseFileInInitialDirectory,
                                             ToolsStrings.Common_CannotDo_Title, MessageBoxButton.OKCancel) == MessageBoxResult.OK)
                {
                    return(SavePresetUsingDialog(key, category, data, ref filename));
                }

                return(false);
            }

            File.WriteAllBytes(filename, data);
            Logging.Debug("Preset saved as " + filename);

            if (key != null)
            {
                PresetSaved?.Invoke(this, new PresetSavedEventArgs(key, filename));
            }

            return(true);
        }
Пример #9
0
        public bool SavePresetUsingDialog([CanBeNull] string key, [NotNull] PresetsCategory category, [CanBeNull] string data,
                                          [CanBeNull] ref string filename)
        {
            if (data == null)
            {
                return(false);
            }

            var presetsDirectory = EnsureDirectory(category);

            var dialog = new SaveFileDialog {
                InitialDirectory = presetsDirectory,
                Filter           = string.Format(ToolsStrings.Presets_FileFilter, category.Extension),
                DefaultExt       = category.Extension,
                CustomPlaces     =
                {
                    new FileDialogCustomPlace(presetsDirectory)
                }
            };

            if (filename != null)
            {
                dialog.InitialDirectory = Path.GetDirectoryName(filename);
                dialog.FileName         = Path.GetFileNameWithoutExtension(filename);
            }

            if (dialog.ShowDialog() != true)
            {
                return(false);
            }

            filename = dialog.FileName;
            if (!filename.StartsWith(presetsDirectory))
            {
                if (ModernDialog.ShowMessage(ToolsStrings.Presets_ChooseFileInInitialDirectory,
                                             ToolsStrings.Common_CannotDo_Title, MessageBoxButton.OKCancel) == MessageBoxResult.OK)
                {
                    return(SavePresetUsingDialog(key, category, data, ref filename));
                }

                return(false);
            }

            File.WriteAllText(filename, data);
            Logging.Debug("Preset saved as " + filename);

            if (key != null)
            {
                PresetSaved?.Invoke(this, new PresetSavedEventArgs(key, filename));
            }

            return(true);
        }
Пример #10
0
        private IEnumerable <ISavedPresetEntry> GetDataPresets(PresetsCategory category)
        {
            if (category.DirectoryName.IndexOf(':') != -1)
            {
                return(new BuiltInPresetEntry[0]);
            }
            var directory        = FilesStorage.Instance.GetDirectory(FilesStorage.DataDirName, ContentCategory.BuiltInPresets, category.DirectoryName);
            var virtualDirectory = GetDirectory(category);

            return(FileUtils.GetFilesRecursive(directory, @"*" + category.Extension)
                   .TryToSelect(x => {
                var relative = FileUtils.GetPathWithin(x, directory);
                return relative == null
                                        ? throw new Exception($"Can’t find relative path from “{x}” to “{directory}”")
                                        : new DataBuiltInPresetEntry(virtualDirectory, category.Extension, Path.Combine(virtualDirectory, relative), x);
            }, e => Logging.Error(e))
                   .ToList <ISavedPresetEntry>());
        }
Пример #11
0
 public ContentWatcher Watcher(PresetsCategory category)
 {
     return(GetWatcher(GetDirectory(category)));
 }
Пример #12
0
 public string GetPresetFilename(PresetsCategory category, string name)
 {
     return(Path.Combine(Instance.GetDirectory(category.DirectoryName), name + category.Extension));
 }
Пример #13
0
 private List <BuiltInPresetEntry> GetBuiltInPresetsList(PresetsCategory category)
 {
     return(_builtInPresets.TryGetValue(category, out var value) ? value
             : (_builtInPresets[category] = new List <BuiltInPresetEntry>(10)));
 }
Пример #14
0
 protected bool Equals(PresetsCategory other)
 {
     return(string.Equals(DirectoryName, other.DirectoryName) && string.Equals(Extension, other.Extension));
 }
Пример #15
0
 public bool SavePresetUsingDialog([CanBeNull] string key, [NotNull] PresetsCategory category, [CanBeNull] byte[] data,
                                   [CanBeNull] string filename)
 {
     return(SavePresetUsingDialog(key, category, data, ref filename));
 }
Пример #16
0
 public bool SavePresetUsingDialog([CanBeNull] string key, [NotNull] PresetsCategory category, [CanBeNull] string data,
                                   [CanBeNull] string filename)
 {
     return(SavePresetUsingDialog(key, category, data == null ? null : Encoding.UTF8.GetBytes(data), filename));
 }
Пример #17
0
 public void ClearBuiltInPresets(PresetsCategory category)
 {
     _builtInPresets.Remove(category);
 }
Пример #18
0
 public void ClearBuiltInPresets(PresetsCategory category)
 {
     GetBuiltInPresetsList(category).Clear();
 }
Пример #19
0
 public ISavedPresetEntry GetBuiltInPreset(PresetsCategory category, string filename)
 {
     return(_builtInPresets.GetValueOrDefault(category)?.FirstOrDefault(x => FileUtils.ArePathsEqual(x.VirtualFilename, filename)));
 }
Пример #20
0
 public bool HasBuiltInPreset(PresetsCategory category, string filename)
 {
     return(_builtInPresets.GetValueOrDefault(category)?.Any(x => FileUtils.ArePathsEqual(x.VirtualFilename, filename)) == true);
 }
Пример #21
0
 private IEnumerable <ISavedPresetEntry> GetBuiltInPresets(PresetsCategory category)
 {
     return(GetBuiltInPresetsList(category).Concat(GetDataPresets(category)));
 }