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)); }
private List <BuiltInPresetEntry> GetBuiltInPresetsList(PresetsCategory category) { if (_builtInPresets.ContainsKey(category)) { return(_builtInPresets[category]); } return(_builtInPresets[category] = new List <BuiltInPresetEntry>(1)); }
public string GetDirectory(PresetsCategory category) { if (category.DirectoryName.IndexOf(':') != -1) { return(category.DirectoryName); } return(base.GetDirectory(category.DirectoryName)); }
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()); }
public string EnsureDirectory(PresetsCategory category) { var directory = GetDirectory(category); if (!Directory.Exists(directory)) { Directory.CreateDirectory(directory); } return(directory); }
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)); }
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)); }
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); }
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); }
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>()); }
public ContentWatcher Watcher(PresetsCategory category) { return(GetWatcher(GetDirectory(category))); }
public string GetPresetFilename(PresetsCategory category, string name) { return(Path.Combine(Instance.GetDirectory(category.DirectoryName), name + category.Extension)); }
private List <BuiltInPresetEntry> GetBuiltInPresetsList(PresetsCategory category) { return(_builtInPresets.TryGetValue(category, out var value) ? value : (_builtInPresets[category] = new List <BuiltInPresetEntry>(10))); }
protected bool Equals(PresetsCategory other) { return(string.Equals(DirectoryName, other.DirectoryName) && string.Equals(Extension, other.Extension)); }
public bool SavePresetUsingDialog([CanBeNull] string key, [NotNull] PresetsCategory category, [CanBeNull] byte[] data, [CanBeNull] string filename) { return(SavePresetUsingDialog(key, category, data, ref filename)); }
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)); }
public void ClearBuiltInPresets(PresetsCategory category) { _builtInPresets.Remove(category); }
public void ClearBuiltInPresets(PresetsCategory category) { GetBuiltInPresetsList(category).Clear(); }
public ISavedPresetEntry GetBuiltInPreset(PresetsCategory category, string filename) { return(_builtInPresets.GetValueOrDefault(category)?.FirstOrDefault(x => FileUtils.ArePathsEqual(x.VirtualFilename, filename))); }
public bool HasBuiltInPreset(PresetsCategory category, string filename) { return(_builtInPresets.GetValueOrDefault(category)?.Any(x => FileUtils.ArePathsEqual(x.VirtualFilename, filename)) == true); }
private IEnumerable <ISavedPresetEntry> GetBuiltInPresets(PresetsCategory category) { return(GetBuiltInPresetsList(category).Concat(GetDataPresets(category))); }