예제 #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)));
 }