Example #1
0
        private static bool IsUsingSourceFile(IFileImporter importer, ContentRef <Resource> resourceRef, string srcFilePath, string srcFileExt)
        {
            // Does the Resource or Importer recall to use this file?
            if (importer.IsUsingSrcFile(resourceRef, srcFilePath))
            {
                return(true);
            }

            // Does the system suggest that the Resource would use that file if it was opened for editing?
            string resourceSourcePath = SelectSourceFilePath(resourceRef, srcFileExt);

            if (PathHelper.ArePathsEqual(resourceSourcePath, srcFilePath))
            {
                return(true);
            }

            // Nope.
            return(false);
        }
Example #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);
                    }
                }
            }
        }