private static string[] PreMoveSourceMediaFile(ResourceRenamedEventArgs renameEvent) { if (!renameEvent.IsResource) { return(new string[0]); } else { return(GetSourceMediaPaths(renameEvent.OldContent.Res)); } }
private static string[] PreMoveSourceMediaFile(ResourceRenamedEventArgs renameEvent) { if (!renameEvent.IsResource) { return(new string[0]); } else { return(AssetManager.GetAssetSourceFiles(renameEvent.OldContent)); } }
private static void MoveSourceMediaFile(ResourceRenamedEventArgs renameEvent, string[] oldMediaPaths) { if (renameEvent.IsResource) { string[] newMediaPaths = GetSourceMediaPaths(renameEvent.Content.Res); for (int i = 0; i < oldMediaPaths.Length; i++) { string oldPath = oldMediaPaths[i]; string newPath = newMediaPaths.Length > i ? newMediaPaths[i] : oldPath; // Move the media file to mirror the data files movement if (!PathOp.ArePathsEqual(oldPath, newPath)) { if (File.Exists(oldPath) && !File.Exists(newPath)) { Directory.CreateDirectory(Path.GetDirectoryName(newPath)); File.Move(oldPath, newPath); PathHelper.DeleteEmptyDirectory(Path.GetDirectoryName(oldPath), true); } } } } else if (renameEvent.IsDirectory) { // Determine which source/media directory we're going to move string oldMediaPath = Path.Combine( EditorHelper.SourceMediaDirectory, PathHelper.MakeFilePathRelative(renameEvent.OldPath, DualityApp.DataDirectory)); // Determine where that old source/media directory needs to be moved string newMediaPath = Path.Combine( EditorHelper.SourceMediaDirectory, PathHelper.MakeFilePathRelative(renameEvent.Path, DualityApp.DataDirectory)); // Move the media directory to mirror the data directories movement if (!PathOp.ArePathsEqual(newMediaPath, oldMediaPath)) { if (Directory.Exists(oldMediaPath) && !Directory.Exists(newMediaPath)) { Directory.CreateDirectory(Path.GetDirectoryName(newMediaPath)); Directory.Move(oldMediaPath, newMediaPath); PathHelper.DeleteEmptyDirectory(Path.GetDirectoryName(oldMediaPath), true); } } } }
private void FileEventManager_ResourceRenamed(object sender, ResourceRenamedEventArgs e) { NodeBase node = this.NodeFromPath(e.OldPath); bool scanResFile = false; // Modify existing node if (node != null) { string newDirectory = Path.GetDirectoryName(e.Path); bool moved = !PathOp.ArePathsEqual(Path.GetDirectoryName(e.OldPath), newDirectory); // If its a file, remove and add it again if (File.Exists(e.Path)) { // Remove it this.UnregisterNodeTree(node); node.Parent.Nodes.Remove(node); // Register it scanResFile = true; } // Otherwise: Rename node according to file else { this.UnregisterNodeTree(node); if (moved) { node.Parent.Nodes.Remove(node); Node newParent = this.NodeFromPath(newDirectory) ?? this.folderModel.Root; this.InsertNodeSorted(node, newParent); } node.NodePath = e.Path; node.ApplyPathToName(); this.RegisterNodeTree(node); } } // Register newly detected Resource file else if (this.NodeFromPath(e.Path) == null) { scanResFile = true; } // If neccessary, check if the file is a Resource file and add it, if yes if (scanResFile && Resource.IsResourceFile(e.Path)) { node = this.ScanFile(e.Path); Node parentNode = this.NodeFromPath(Path.GetDirectoryName(e.Path)); if (parentNode == null) parentNode = this.folderModel.Root; this.InsertNodeSorted(node, parentNode); this.RegisterNodeTree(node); node.NotifyVisible(); } // Perform previously scheduled selection this.PerformScheduleSelect(Path.GetFullPath(e.Path)); }
private void DualityEditorApp_ResourceRenamed(object sender, ResourceRenamedEventArgs e) { if (e.Path == Scene.CurrentPath) this.UpdateSceneLabel(); if (!e.IsDirectory && !typeof(Prefab).IsAssignableFrom(e.ContentType)) return; this.UpdatePrefabLinkStatus(true); }
private static void MoveSourceMediaFile(ResourceRenamedEventArgs renameEvent) { // Determine which source/media path we're going to modify string oldMediaPath = null; string mediaPath = null; if (renameEvent.IsResource) { Resource res = renameEvent.Content.Res; if (res.SourcePath != null) { mediaPath = res.SourcePath; } if (!File.Exists(mediaPath)) { mediaPath = FileImportProvider.SelectSourceFilePath(renameEvent.OldContent, Path.GetExtension(mediaPath)); } if (!File.Exists(mediaPath)) { return; } } else if (renameEvent.IsDirectory) { mediaPath = Path.Combine(EditorHelper.SourceMediaDirectory, PathHelper.MakeFilePathRelative(renameEvent.OldPath, DualityApp.DataDirectory)); } oldMediaPath = mediaPath; // Ignore stuff changes without data to modify if (mediaPath == null) { return; } // If media and data file were located in a similar folder structure, keep that structure { string relativeMediaPath = PathHelper.MakeFilePathRelative(mediaPath, EditorHelper.SourceMediaDirectory); string relativeOldDataPath = PathHelper.MakeFilePathRelative(renameEvent.OldPath, DualityApp.DataDirectory); string relativeMediaDir = Path.GetDirectoryName(relativeMediaPath); string relativeOldDataDir = Path.GetDirectoryName(relativeOldDataPath); if (PathHelper.ArePathsEqual(relativeMediaDir, relativeOldDataDir)) { string relativeNewDataDir = Path.GetDirectoryName(PathHelper.MakeFilePathRelative(renameEvent.Path, DualityApp.DataDirectory)); string newMediaDir = Path.Combine(EditorHelper.SourceMediaDirectory, relativeNewDataDir); mediaPath = Path.Combine(newMediaDir, Path.GetFileName(mediaPath)); } } // If media and data file were named similarly, keep that naming scheme if (!PathHelper.ArePathsEqual(Path.GetFileName(renameEvent.OldPath), Path.GetFileName(renameEvent.Path))) { string mediaFileNameWithoutExt = Path.GetFileNameWithoutExtension(mediaPath); string oldDataName = ContentProvider.GetNameFromPath(renameEvent.OldPath); if (PathHelper.ArePathsEqual(mediaFileNameWithoutExt, oldDataName)) { string newDataName = ContentProvider.GetNameFromPath(renameEvent.Path); string newMediaFileName = newDataName + Path.GetExtension(mediaPath); mediaPath = Path.Combine(Path.GetDirectoryName(mediaPath), newMediaFileName); } } if (renameEvent.IsResource) { // Move the media file to mirror the data files movement if (!PathHelper.ArePathsEqual(mediaPath, oldMediaPath)) { if (File.Exists(oldMediaPath) && !File.Exists(mediaPath)) { Directory.CreateDirectory(Path.GetDirectoryName(mediaPath)); File.Move(oldMediaPath, mediaPath); PathHelper.DeleteEmptyDirectory(Path.GetDirectoryName(oldMediaPath), true); } } } else if (renameEvent.IsDirectory) { // Move the media directory to mirror the data files movement if (!PathHelper.ArePathsEqual(mediaPath, oldMediaPath)) { if (Directory.Exists(oldMediaPath) && !Directory.Exists(mediaPath)) { Directory.CreateDirectory(Path.GetDirectoryName(mediaPath)); Directory.Move(oldMediaPath, mediaPath); PathHelper.DeleteEmptyDirectory(Path.GetDirectoryName(oldMediaPath), true); } } } }
private static void ProcessDataDirEvents() { List <ResourceRenamedEventArgs> renameEventBuffer = null; // Process events while (dataDirEventBuffer.Count > 0) { FileSystemEventArgs e = FetchFileSystemEvent(dataDirEventBuffer, DualityApp.DataDirectory); if (e == null) { continue; } // Determine whether we're dealing with a directory bool isDirectory = Directory.Exists(e.FullPath); { // If this is a deletion, nothing exists anymore, rely on metadata instead. DeletedEventArgsExt de = e as DeletedEventArgsExt; if (de != null && de.IsDirectory) { isDirectory = true; } } if (e.ChangeType == WatcherChangeTypes.Changed) { // Ignore stuff saved by the editor itself if (IsPathEditorModified(e.FullPath)) { continue; } if (Resource.IsResourceFile(e.FullPath) || isDirectory) { ContentRef <Resource> resRef = new ContentRef <Resource>(null, e.FullPath); // Unregister outdated resources, if modified outside the editor if (!isDirectory && ContentProvider.HasContent(e.FullPath)) { bool isCurrentScene = resRef.Is <Scene>() && Scene.Current == resRef.Res; if (isCurrentScene || DualityEditorApp.IsResourceUnsaved(e.FullPath)) { DialogResult result = MessageBox.Show( String.Format(Properties.GeneralRes.Msg_ConfirmReloadResource_Text, e.FullPath), Properties.GeneralRes.Msg_ConfirmReloadResource_Caption, MessageBoxButtons.YesNo, MessageBoxIcon.Exclamation); if (result == DialogResult.Yes) { string curScenePath = Scene.CurrentPath; ContentProvider.RemoveContent(e.FullPath); if (isCurrentScene) { Scene.SwitchTo(ContentProvider.RequestContent <Scene>(curScenePath), true); } } } else { ContentProvider.RemoveContent(e.FullPath); } } if (ResourceModified != null) { ResourceModified(null, new ResourceEventArgs(e.FullPath, isDirectory)); } } } else if (e.ChangeType == WatcherChangeTypes.Created) { if (File.Exists(e.FullPath)) { // Register newly detected Resource file if (Resource.IsResourceFile(e.FullPath)) { if (ResourceCreated != null) { ResourceCreated(null, new ResourceEventArgs(e.FullPath, false)); } } // Import non-Resource file else { bool abort = false; if (FileImportProvider.DoesImportOverwriteData(e.FullPath)) { DialogResult result = MessageBox.Show( String.Format(Properties.GeneralRes.Msg_ImportConfirmOverwrite_Text, e.FullPath), Properties.GeneralRes.Msg_ImportConfirmOverwrite_Caption, MessageBoxButtons.YesNo, MessageBoxIcon.Warning); abort = result == DialogResult.No; } if (!abort) { bool importedSuccessfully = FileImportProvider.ImportFile(e.FullPath); if (!importedSuccessfully) { MessageBox.Show( String.Format(Properties.GeneralRes.Msg_CantImport_Text, e.FullPath), Properties.GeneralRes.Msg_CantImport_Caption, MessageBoxButtons.OK, MessageBoxIcon.Error); } abort = !importedSuccessfully; } } } else if (Directory.Exists(e.FullPath)) { // Register newly detected Resource directory if (ResourceCreated != null) { ResourceCreated(null, new ResourceEventArgs(e.FullPath, true)); } } } else if (e.ChangeType == WatcherChangeTypes.Deleted) { // Is it a Resource file or just something else? if (Resource.IsResourceFile(e.FullPath) || isDirectory) { ResourceEventArgs args = new ResourceEventArgs(e.FullPath, isDirectory); // Organize the Source/Media directory accordingly DeleteSourceMediaFile(args); // Unregister no-more existing resources if (isDirectory) { ContentProvider.RemoveContentTree(args.Path); } else { ContentProvider.RemoveContent(args.Path); } if (ResourceDeleted != null) { ResourceDeleted(null, args); } } } else if (e.ChangeType == WatcherChangeTypes.Renamed) { // Is it a Resource file or just something else? RenamedEventArgs re = e as RenamedEventArgs; ResourceRenamedEventArgs args = new ResourceRenamedEventArgs(re.FullPath, re.OldFullPath, isDirectory); if (Resource.IsResourceFile(e.FullPath) || isDirectory) { // Rename content registerations if (isDirectory) { ContentProvider.RenameContentTree(args.OldPath, args.Path); } else { ContentProvider.RenameContent(args.OldPath, args.Path); } // Query skipped paths bool isEmptyDir = isDirectory && !Directory.EnumerateFileSystemEntries(args.Path).Any(); bool isSkippedPath = isEmptyDir; if (!isSkippedPath && BeginGlobalRename != null) { BeginGlobalRenameEventArgs beginGlobalRenameArgs = new BeginGlobalRenameEventArgs(args.Path, args.OldPath, isDirectory); BeginGlobalRename(null, beginGlobalRenameArgs); isSkippedPath = beginGlobalRenameArgs.Cancel; } if (!isSkippedPath) { // Buffer rename event to perform the global rename for all at once. if (renameEventBuffer == null) { renameEventBuffer = new List <ResourceRenamedEventArgs>(); } renameEventBuffer.Add(args); } if (ResourceRenamed != null) { ResourceRenamed(null, args); } if (!isSkippedPath) { // Organize the Source/Media directory accordingly MoveSourceMediaFile(args); } } } } // If required, perform a global rename operation in all existing content if (renameEventBuffer != null) { // Don't do it now - schedule it for the main form event loop so we don't block here. DualityEditorApp.MainForm.BeginInvoke((Action) delegate() { ProcessingBigTaskDialog taskDialog = new ProcessingBigTaskDialog( Properties.GeneralRes.TaskRenameContentRefs_Caption, Properties.GeneralRes.TaskRenameContentRefs_Desc, async_RenameContentRefs, renameEventBuffer); taskDialog.ShowDialog(DualityEditorApp.MainForm); }); } }
public void When_a_non_script_resource_is_renamed_Then_dont_throw() { var resource = new Prefab(); resource.Save("test.prefab.res"); var eventArgs = new ResourceRenamedEventArgs("test2.prefab.res", "test.prefab.res"); Assert.DoesNotThrow(() => _scriptResourceEvents.OnResourceRenamed(this, eventArgs)); }
private ResourceRenamedEventArgs CreateEventArgs(string newPath, string oldPath) { var eventArgs = new ResourceRenamedEventArgs(newPath, oldPath); var fieldInfo = eventArgs.GetType().BaseType.GetField("content", BindingFlags.NonPublic | BindingFlags.Instance); if (fieldInfo != null) fieldInfo.SetValue(eventArgs, CreateContentRef(GetCSScriptText(),newPath)); return eventArgs; }
public void When_non_resource_is_renamed_Then_dont_throw() { var eventArgs = new ResourceRenamedEventArgs("", ""); Assert.DoesNotThrow(() => _scriptResourceEvents.OnResourceRenamed(this, eventArgs)); }
private static string[] PreMoveSourceMediaFile(ResourceRenamedEventArgs renameEvent) { if (!renameEvent.IsResource) return new string[0]; else return GetSourceMediaPaths(renameEvent.OldContent.Res); }
private static void ProcessDataDirEvents() { List<ResourceRenamedEventArgs> renameEventBuffer = null; // Process events while (dataDirEventBuffer.Count > 0) { FileSystemEventArgs e = FetchFileSystemEvent(dataDirEventBuffer, DualityApp.DataDirectory); if (e == null) continue; // Determine whether we're dealing with a directory bool isDirectory = Directory.Exists(e.FullPath); { // If this is a deletion, nothing exists anymore, rely on metadata instead. DeletedEventArgsExt de = e as DeletedEventArgsExt; if (de != null && de.IsDirectory) isDirectory = true; } if (e.ChangeType == WatcherChangeTypes.Changed) { // Ignore stuff saved by the editor itself if (IsPathEditorModified(e.FullPath)) continue; if (Resource.IsResourceFile(e.FullPath) || isDirectory) { ContentRef<Resource> resRef = new ContentRef<Resource>(null, e.FullPath); // Unregister outdated resources, if modified outside the editor if (!isDirectory && ContentProvider.HasContent(e.FullPath)) { bool isCurrentScene = resRef.Is<Scene>() && Scene.Current == resRef.Res; if (isCurrentScene || DualityEditorApp.IsResourceUnsaved(e.FullPath)) { DialogResult result = MessageBox.Show( String.Format(Properties.GeneralRes.Msg_ConfirmReloadResource_Text, e.FullPath), Properties.GeneralRes.Msg_ConfirmReloadResource_Caption, MessageBoxButtons.YesNo, MessageBoxIcon.Exclamation); if (result == DialogResult.Yes) { string curScenePath = Scene.CurrentPath; ContentProvider.RemoveContent(e.FullPath); if (isCurrentScene) Scene.SwitchTo(ContentProvider.RequestContent<Scene>(curScenePath), true); } } else ContentProvider.RemoveContent(e.FullPath); } if (ResourceModified != null) ResourceModified(null, new ResourceEventArgs(e.FullPath, isDirectory)); } } else if (e.ChangeType == WatcherChangeTypes.Created) { if (File.Exists(e.FullPath)) { // Register newly detected Resource file if (Resource.IsResourceFile(e.FullPath)) { if (ResourceCreated != null) ResourceCreated(null, new ResourceEventArgs(e.FullPath, false)); } } else if (Directory.Exists(e.FullPath)) { // Register newly detected Resource directory if (ResourceCreated != null) ResourceCreated(null, new ResourceEventArgs(e.FullPath, true)); } } else if (e.ChangeType == WatcherChangeTypes.Deleted) { // Is it a Resource file or just something else? if (Resource.IsResourceFile(e.FullPath) || isDirectory) { ResourceEventArgs args = new ResourceEventArgs(e.FullPath, isDirectory); // Organize the Source/Media directory accordingly DeleteSourceMediaFile(args); // Unregister no-more existing resources if (isDirectory) ContentProvider.RemoveContentTree(args.Path); else ContentProvider.RemoveContent(args.Path); if (ResourceDeleted != null) ResourceDeleted(null, args); } } else if (e.ChangeType == WatcherChangeTypes.Renamed) { // Is it a Resource file or just something else? RenamedEventArgs re = e as RenamedEventArgs; ResourceRenamedEventArgs args = new ResourceRenamedEventArgs(re.FullPath, re.OldFullPath, isDirectory); if (Resource.IsResourceFile(e.FullPath) || isDirectory) { // Determine which Source / Media files would belong to this Resource - before moving it string[] oldMediaPaths = PreMoveSourceMediaFile(args);; // Rename content registerations if (isDirectory) ContentProvider.RenameContentTree(args.OldPath, args.Path); else ContentProvider.RenameContent(args.OldPath, args.Path); // Query skipped paths bool isEmptyDir = isDirectory && !Directory.EnumerateFileSystemEntries(args.Path).Any(); bool isSkippedPath = isEmptyDir; if (!isSkippedPath && BeginGlobalRename != null) { BeginGlobalRenameEventArgs beginGlobalRenameArgs = new BeginGlobalRenameEventArgs(args.Path, args.OldPath, isDirectory); BeginGlobalRename(null, beginGlobalRenameArgs); isSkippedPath = beginGlobalRenameArgs.Cancel; } if (!isSkippedPath) { // Buffer rename event to perform the global rename for all at once. if (renameEventBuffer == null) renameEventBuffer = new List<ResourceRenamedEventArgs>(); renameEventBuffer.Add(args); } if (ResourceRenamed != null) ResourceRenamed(null, args); if (!isSkippedPath) { // Organize the Source/Media directory accordingly MoveSourceMediaFile(args, oldMediaPaths); } } } } // If required, perform a global rename operation in all existing content if (renameEventBuffer != null) { // Don't do it now - schedule it for the main form event loop so we don't block here. DualityEditorApp.MainForm.BeginInvoke((Action)delegate() { ProcessingBigTaskDialog taskDialog = new ProcessingBigTaskDialog( Properties.GeneralRes.TaskRenameContentRefs_Caption, Properties.GeneralRes.TaskRenameContentRefs_Desc, async_RenameContentRefs, renameEventBuffer); taskDialog.ShowDialog(DualityEditorApp.MainForm); }); } }
private static void ProcessDataDirEvents() { List<ResourceRenamedEventArgs> renameEventBuffer = null; // Process events while (dataDirEventBuffer.Count > 0) { FileSystemEventArgs e = FetchFileSystemEvent(dataDirEventBuffer, dataDirWatcher.Path); if (e == null) continue; if (e.ChangeType == WatcherChangeTypes.Changed) { ResourceEventArgs args = new ResourceEventArgs(e.FullPath); bool justSaved = editorJustSavedRes.Contains(Path.GetFullPath(e.FullPath)); // Ignore stuff saved by the editor itself if (!justSaved && (Resource.IsResourceFile(e.FullPath) || args.IsDirectory)) { // Unregister outdated resources, if modified outside the editor if (!args.IsDirectory && ContentProvider.HasContent(args.Path)) { bool isCurrentScene = args.Content.Is<Scene>() && Scene.Current == args.Content.Res; if (isCurrentScene || DualityEditorApp.IsResourceUnsaved(e.FullPath)) { DialogResult result = MessageBox.Show( String.Format(Properties.GeneralRes.Msg_ConfirmReloadResource_Text, e.FullPath), Properties.GeneralRes.Msg_ConfirmReloadResource_Caption, MessageBoxButtons.YesNo, MessageBoxIcon.Exclamation); if (result == DialogResult.Yes) { string curScenePath = Scene.CurrentPath; ContentProvider.RemoveContent(args.Path); if (isCurrentScene) Scene.SwitchTo(ContentProvider.RequestContent<Scene>(curScenePath), true); } } else ContentProvider.RemoveContent(args.Path); } if (ResourceModified != null) ResourceModified(null, args); } } else if (e.ChangeType == WatcherChangeTypes.Created) { if (File.Exists(e.FullPath)) { // Register newly detected ressource file if (Resource.IsResourceFile(e.FullPath)) { if (ResourceCreated != null) ResourceCreated(null, new ResourceEventArgs(e.FullPath)); } // Import non-ressource file else { bool abort = false; if (FileImportProvider.IsImportFileExisting(e.FullPath)) { DialogResult result = MessageBox.Show( String.Format(Properties.GeneralRes.Msg_ImportConfirmOverwrite_Text, e.FullPath), Properties.GeneralRes.Msg_ImportConfirmOverwrite_Caption, MessageBoxButtons.YesNo, MessageBoxIcon.Warning); abort = result == DialogResult.No; } if (!abort) { bool importedSuccessfully = FileImportProvider.ImportFile(e.FullPath); if (!importedSuccessfully) { MessageBox.Show( String.Format(Properties.GeneralRes.Msg_CantImport_Text, e.FullPath), Properties.GeneralRes.Msg_CantImport_Caption, MessageBoxButtons.OK, MessageBoxIcon.Error); } abort = !importedSuccessfully; } } } else if (Directory.Exists(e.FullPath)) { // Register newly detected ressource directory if (ResourceCreated != null) ResourceCreated(null, new ResourceEventArgs(e.FullPath)); } } else if (e.ChangeType == WatcherChangeTypes.Deleted) { // Is it a Resource file or just something else? ResourceEventArgs args = new ResourceEventArgs(e.FullPath); if (Resource.IsResourceFile(e.FullPath) || args.IsDirectory) { // Unregister no-more existing resources if (args.IsDirectory) ContentProvider.RemoveContentTree(args.Path); else ContentProvider.RemoveContent(args.Path); if (ResourceDeleted != null) ResourceDeleted(null, args); } } else if (e.ChangeType == WatcherChangeTypes.Renamed) { // Is it a Resource file or just something else? RenamedEventArgs re = e as RenamedEventArgs; ResourceRenamedEventArgs args = new ResourceRenamedEventArgs(re.FullPath, re.OldFullPath); if (Resource.IsResourceFile(e.FullPath) || args.IsDirectory) { // Rename content registerations if (args.IsDirectory) ContentProvider.RenameContentTree(args.OldPath, args.Path); else ContentProvider.RenameContent(args.OldPath, args.Path); // Query skipped paths bool isEmptyDir = args.IsDirectory && !Directory.EnumerateFileSystemEntries(args.Path).Any(); bool isSkippedPath = isEmptyDir; if (!isSkippedPath && BeginGlobalRename != null) { BeginGlobalRenameEventArgs beginGlobalRenameArgs = new BeginGlobalRenameEventArgs(args.Path, args.OldPath); BeginGlobalRename(null, beginGlobalRenameArgs); isSkippedPath = beginGlobalRenameArgs.Cancel; } if (!isSkippedPath) { // Buffer rename event to perform the global rename for all at once. if (renameEventBuffer == null) renameEventBuffer = new List<ResourceRenamedEventArgs>(); renameEventBuffer.Add(args); } if (ResourceRenamed != null) ResourceRenamed(null, args); } } } // If required, perform a global rename operation in all existing content if (renameEventBuffer != null) { // Don't do it now - schedule it for the main form event loop so we don't block here. DualityEditorApp.MainForm.BeginInvoke((Action)delegate() { ProcessingBigTaskDialog taskDialog = new ProcessingBigTaskDialog( Properties.GeneralRes.TaskRenameContentRefs_Caption, Properties.GeneralRes.TaskRenameContentRefs_Desc, async_RenameContentRefs, renameEventBuffer); taskDialog.ShowDialog(DualityEditorApp.MainForm); }); } }
private static void ProcessDataDirEvents() { List <ResourceRenamedEventArgs> renameEventBuffer = null; HashSet <string> sourceMediaDeleteSchedule = null; // Process events while (dataDirEventBuffer.Count > 0) { FileSystemEventArgs e = FetchFileSystemEvent(dataDirEventBuffer, DualityApp.DataDirectory); if (e == null) { continue; } // Determine whether we're dealing with a directory bool isDirectory = Directory.Exists(e.FullPath); { // If this is a deletion, nothing exists anymore, rely on metadata instead. DeletedEventArgsExt de = e as DeletedEventArgsExt; if (de != null && de.IsDirectory) { isDirectory = true; } } if (e.ChangeType == WatcherChangeTypes.Changed) { // Ignore stuff saved by the editor itself if (IsPathEditorModified(e.FullPath)) { continue; } if (Resource.IsResourceFile(e.FullPath) || isDirectory) { ContentRef <Resource> resRef = new ContentRef <Resource>(null, e.FullPath); // Unregister outdated resources, if modified outside the editor if (!isDirectory && ContentProvider.HasContent(e.FullPath)) { bool isCurrentScene = resRef.Is <Scene>() && Scene.Current == resRef.Res; if (isCurrentScene || DualityEditorApp.IsResourceUnsaved(e.FullPath)) { DialogResult result = MessageBox.Show( String.Format(Properties.GeneralRes.Msg_ConfirmReloadResource_Text, e.FullPath), Properties.GeneralRes.Msg_ConfirmReloadResource_Caption, MessageBoxButtons.YesNo, MessageBoxIcon.Exclamation); if (result == DialogResult.Yes) { string curScenePath = Scene.CurrentPath; ContentProvider.RemoveContent(e.FullPath); if (isCurrentScene) { Scene.SwitchTo(ContentProvider.RequestContent <Scene>(curScenePath), true); } } } else { ContentProvider.RemoveContent(e.FullPath); } } if (ResourceModified != null) { ResourceModified(null, new ResourceEventArgs(e.FullPath, isDirectory)); } } } else if (e.ChangeType == WatcherChangeTypes.Created) { if (File.Exists(e.FullPath)) { // Register newly detected Resource file if (Resource.IsResourceFile(e.FullPath)) { if (ResourceCreated != null) { ResourceCreated(null, new ResourceEventArgs(e.FullPath, false)); } } } else if (Directory.Exists(e.FullPath)) { // Register newly detected Resource directory if (ResourceCreated != null) { ResourceCreated(null, new ResourceEventArgs(e.FullPath, true)); } } } else if (e.ChangeType == WatcherChangeTypes.Deleted) { // Is it a Resource file or just something else? if (Resource.IsResourceFile(e.FullPath) || isDirectory) { ResourceEventArgs args = new ResourceEventArgs(e.FullPath, isDirectory); // Schedule Source/Media file deletion to keep it organized / synced with Resource Data if (sourceMediaDeleteSchedule == null) { sourceMediaDeleteSchedule = new HashSet <string>(); } GetDeleteSourceMediaFilePaths(args, sourceMediaDeleteSchedule); // Unregister no-more existing resources if (isDirectory) { ContentProvider.RemoveContentTree(args.Path); } else { ContentProvider.RemoveContent(args.Path); } if (ResourceDeleted != null) { ResourceDeleted(null, args); } } } else if (e.ChangeType == WatcherChangeTypes.Renamed) { // Is it a Resource file or just something else? RenamedEventArgs re = e as RenamedEventArgs; ResourceRenamedEventArgs args = new ResourceRenamedEventArgs(re.FullPath, re.OldFullPath, isDirectory); if (Resource.IsResourceFile(e.FullPath) || isDirectory) { // Determine which Source / Media files would belong to this Resource - before moving it string[] oldMediaPaths = PreMoveSourceMediaFile(args);; // Rename content registerations if (isDirectory) { ContentProvider.RenameContentTree(args.OldPath, args.Path); } else { ContentProvider.RenameContent(args.OldPath, args.Path); } // Query skipped paths bool isEmptyDir = isDirectory && !Directory.EnumerateFileSystemEntries(args.Path).Any(); bool isSkippedPath = isEmptyDir; if (!isSkippedPath && BeginGlobalRename != null) { BeginGlobalRenameEventArgs beginGlobalRenameArgs = new BeginGlobalRenameEventArgs(args.Path, args.OldPath, isDirectory); BeginGlobalRename(null, beginGlobalRenameArgs); isSkippedPath = beginGlobalRenameArgs.Cancel; } if (!isSkippedPath) { // Buffer rename event to perform the global rename for all at once. if (renameEventBuffer == null) { renameEventBuffer = new List <ResourceRenamedEventArgs>(); } renameEventBuffer.Add(args); } if (ResourceRenamed != null) { ResourceRenamed(null, args); } if (!isSkippedPath) { // Organize the Source/Media directory accordingly MoveSourceMediaFile(args, oldMediaPaths); } } } } // If we scheduled source / media files for deletion, do it now at once if (sourceMediaDeleteSchedule != null) { // Gather a list of directories from which we're removing HashSet <string> affectedDirectories = new HashSet <string>(); foreach (string file in sourceMediaDeleteSchedule) { affectedDirectories.Add(Path.GetDirectoryName(file)); } // Send all the files to the recycle bin RecycleBin.SendSilent(sourceMediaDeleteSchedule); // Remove directories that are now empty foreach (string dir in affectedDirectories) { PathHelper.DeleteEmptyDirectory(dir, true); } } // If required, perform a global rename operation in all existing content if (renameEventBuffer != null) { // Don't do it now - schedule it for the main form event loop so we don't block here. DualityEditorApp.MainForm.BeginInvoke((Action) delegate() { ProcessingBigTaskDialog taskDialog = new ProcessingBigTaskDialog( Properties.GeneralRes.TaskRenameContentRefs_Caption, Properties.GeneralRes.TaskRenameContentRefs_Desc, async_RenameContentRefs, renameEventBuffer); taskDialog.ShowDialog(DualityEditorApp.MainForm); }); } }