예제 #1
0
        private static void DeleteSourceMediaFile(ResourceEventArgs deleteEvent)
        {
            // Determine which source/media path we're going to modify
            string mediaPath = null;

            if (deleteEvent.IsResource)
            {
                Resource res = deleteEvent.Content.Res;
                if (res != null && res.SourcePath != null)
                {
                    mediaPath = res.SourcePath;
                }
                if (!File.Exists(mediaPath))
                {
                    mediaPath = FileImportProvider.SelectSourceFilePath(deleteEvent.Content, Path.GetExtension(mediaPath));
                }
                if (!File.Exists(mediaPath))
                {
                    return;
                }
            }
            else if (deleteEvent.IsDirectory)
            {
                mediaPath = Path.Combine(EditorHelper.SourceMediaDirectory, PathHelper.MakeFilePathRelative(deleteEvent.Path, DualityApp.DataDirectory));
            }

            // Ignore stuff changes without data to modify
            if (mediaPath == null)
            {
                return;
            }

            // Remove now-unused media files
            if (deleteEvent.IsResource)
            {
                if (File.Exists(mediaPath))
                {
                    RecycleBin.SendSilent(mediaPath);
                    PathHelper.DeleteEmptyDirectory(Path.GetDirectoryName(mediaPath), true);
                }
            }
            else if (deleteEvent.IsDirectory)
            {
                if (Directory.Exists(mediaPath))
                {
                    RecycleBin.SendSilent(mediaPath);
                    PathHelper.DeleteEmptyDirectory(Path.GetDirectoryName(mediaPath), true);
                }
            }
        }
예제 #2
0
        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);
                    }
                }
            }
        }