private static void PrepareImportFilePaths(string filePath, out string srcFilePath, out string targetName, out string targetDir)
        {
            srcFilePath = PathHelper.MakeFilePathRelative(filePath, DualityApp.DataDirectory);
            if (srcFilePath.Contains(".."))
            {
                srcFilePath = Path.GetFileName(srcFilePath);
            }

            targetDir  = Path.GetDirectoryName(Path.Combine(DualityApp.DataDirectory, srcFilePath));
            targetName = Path.GetFileNameWithoutExtension(filePath);

            srcFilePath = PathHelper.GetFreePath(
                Path.Combine(EditorHelper.SourceMediaDirectory, Path.GetFileNameWithoutExtension(srcFilePath)),
                Path.GetExtension(srcFilePath));
        }
Ejemplo n.º 2
0
        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);
                    }
                }
            }
        }
Ejemplo n.º 3
0
 private static void GetDeleteSourceMediaFilePaths(FileEvent deleteEvent, ICollection <string> deletePathSchedule)
 {
     if (!deleteEvent.IsDirectory)
     {
         IList <string> mediaPaths = AssetManager.GetAssetSourceFiles(new ContentRef <Resource>(deleteEvent.Path));
         for (int i = 0; i < mediaPaths.Count; i++)
         {
             if (File.Exists(mediaPaths[i]))
             {
                 deletePathSchedule.Add(mediaPaths[i]);
             }
         }
     }
     else
     {
         string mediaPath = Path.Combine(
             EditorHelper.ImportDirectory,
             PathHelper.MakeFilePathRelative(deleteEvent.Path, DualityApp.DataDirectory));
         if (Directory.Exists(mediaPath))
         {
             deletePathSchedule.Add(mediaPath);
         }
     }
 }
Ejemplo n.º 4
0
        public static string CreateNewProject(string projName, string projFolder, ProjectTemplateInfo template)
        {
            // Create project folder
            projFolder = Path.Combine(projFolder, projName);
            if (!Directory.Exists(projFolder))
            {
                Directory.CreateDirectory(projFolder);
            }

            // Extract template
            if (template.SpecialTag == ProjectTemplateInfo.SpecialInfo.None)
            {
                template.ExtractTo(projFolder);

                // Update main directory
                foreach (string srcFile in Directory.GetFiles(Environment.CurrentDirectory, "*", SearchOption.TopDirectoryOnly))
                {
                    if (Path.GetFileName(srcFile) == "appdata.dat")
                    {
                        continue;
                    }
                    if (Path.GetFileName(srcFile) == "defaultuserdata.dat")
                    {
                        continue;
                    }
                    string dstFile = Path.Combine(projFolder, Path.GetFileName(srcFile));
                    File.Copy(srcFile, dstFile, true);
                }

                // Update plugin directory
                foreach (string dstFile in Directory.GetFiles(Path.Combine(projFolder, DualityApp.PluginDirectory), "*", SearchOption.AllDirectories))
                {
                    string srcFileWorking = Path.Combine(DualityApp.PluginDirectory, Path.GetFileName(dstFile));
                    string srcFileExec    = Path.Combine(PathHelper.ExecutingAssemblyDir, DualityApp.PluginDirectory, Path.GetFileName(dstFile));
                    if (File.Exists(srcFileWorking))
                    {
                        File.Copy(srcFileWorking, dstFile, true);
                    }
                    else if (File.Exists(srcFileExec))
                    {
                        File.Copy(srcFileExec, dstFile, true);
                    }
                }
            }
            else if (template.SpecialTag == ProjectTemplateInfo.SpecialInfo.Current)
            {
                DualityEditorApp.SaveAllProjectData();
                PathHelper.CopyDirectory(Environment.CurrentDirectory, projFolder, true, delegate(string path)
                {
                    bool isDir      = Directory.Exists(path);
                    string fullPath = Path.GetFullPath(path);
                    if (isDir)
                    {
                        return(fullPath != Path.GetFullPath(EditorHelper.BackupDirectory));
                    }
                    else
                    {
                        return(true);
                    }
                });
            }
            else
            {
                PathHelper.CopyDirectory(Environment.CurrentDirectory, projFolder, true, delegate(string path)
                {
                    bool isDir      = Directory.Exists(path);
                    string fullPath = Path.GetFullPath(path);
                    if (isDir)
                    {
                        return
                        (fullPath != Path.GetFullPath(DualityApp.DataDirectory) &&
                         fullPath != Path.GetFullPath(EditorHelper.SourceDirectory) &&
                         fullPath != Path.GetFullPath(EditorHelper.BackupDirectory));
                    }
                    else
                    {
                        string fileName = Path.GetFileName(fullPath);
                        return(fileName != "appdata.dat" && fileName != "defaultuserdata.dat" && fileName != "designtimedata.dat");
                    }
                });
            }

            // Adjust current directory and perform init operations in the new project folder
            string oldPath = Environment.CurrentDirectory;

            Environment.CurrentDirectory = projFolder;
            try
            {
                // Initialize AppData
                DualityAppData data;
                data            = Serializer.TryReadObject <DualityAppData>(DualityApp.AppDataPath) ?? new DualityAppData();
                data.AppName    = projName;
                data.AuthorName = Environment.UserName;
                data.Version    = 0;
                Serializer.WriteObject(data, DualityApp.AppDataPath, typeof(XmlSerializer));

                // Read content source code data (needed to rename classes / namespaces)
                string oldRootNamespaceNameCore;
                string newRootNamespaceNameCore;
                DualityEditorApp.ReadPluginSourceCodeContentData(out oldRootNamespaceNameCore, out newRootNamespaceNameCore);

                // Initialize source code
                DualityEditorApp.InitPluginSourceCode();                 // Force re-init to update namespaces, etc.
                DualityEditorApp.UpdatePluginSourceCode();

                // Add SerializeErrorHandler class to handle renamed Types
                if (Directory.Exists(DualityApp.DataDirectory))
                {
                    // Add error handler source file to project
                    XDocument coreProject         = XDocument.Load(SourceCodeProjectCorePluginFile);
                    string    relErrorHandlerPath = PathHelper.MakeFilePathRelative(
                        SourceCodeErrorHandlerFile,
                        Path.GetDirectoryName(SourceCodeProjectCorePluginFile));
                    if (!coreProject.Descendants("Compile", true).Any(c => string.Equals(c.GetAttributeValue("Include"), relErrorHandlerPath)))
                    {
                        XElement compileElement    = coreProject.Descendants("Compile", true).FirstOrDefault();
                        XElement newCompileElement = new XElement(
                            XName.Get("Compile", compileElement.Name.NamespaceName),
                            new XAttribute("Include", relErrorHandlerPath));
                        compileElement.AddAfterSelf(newCompileElement);
                    }
                    coreProject.Save(SourceCodeProjectCorePluginFile);

                    // Generate and save error handler source code
                    File.WriteAllText(
                        EditorHelper.SourceCodeErrorHandlerFile,
                        EditorHelper.GenerateErrorHandlersSrcFile(oldRootNamespaceNameCore, newRootNamespaceNameCore));
                }

                // Compile plugins
                BuildHelper.BuildSolutionFile(EditorHelper.SourceCodeSolutionFile, "Release");
            }
            finally
            {
                Environment.CurrentDirectory = oldPath;
            }
            return(Path.Combine(projFolder, "DualityEditor.exe"));
        }
Ejemplo n.º 5
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);
                    }
                }
            }
        }
Ejemplo n.º 6
0
        private static void MoveSourceMediaFile(FileEvent renameEvent, string[] oldMediaPaths)
        {
            if (!renameEvent.IsDirectory)
            {
                string[] newMediaPaths = AssetManager.GetAssetSourceFiles(new ContentRef <Resource>(renameEvent.Path));
                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));
                            try
                            {
                                File.Move(oldPath, newPath);
                            }
                            catch (IOException exception)
                            {
                                Logs.Editor.WriteWarning(
                                    "Unable to move source media file '{0}' to '{1}' ({2}). Copying the file instead.",
                                    oldPath,
                                    newPath,
                                    exception.Message);
                                File.Copy(oldPath, newPath);
                            }
                            PathHelper.DeleteEmptyDirectory(Path.GetDirectoryName(oldPath), true);
                        }
                    }
                }
            }
            else
            {
                // Determine which source/media directory we're going to move
                string oldMediaPath = Path.Combine(
                    EditorHelper.ImportDirectory,
                    PathHelper.MakeFilePathRelative(renameEvent.OldPath, DualityApp.DataDirectory));

                // Determine where that old source/media directory needs to be moved
                string newMediaPath = Path.Combine(
                    EditorHelper.ImportDirectory,
                    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));
                        try
                        {
                            Directory.Move(oldMediaPath, newMediaPath);
                        }
                        catch (IOException exception)
                        {
                            Logs.Editor.WriteWarning(
                                "Unable to move source media directory '{0}' to '{1}' ({2}). Copying the directory instead.",
                                oldMediaPath,
                                newMediaPath,
                                exception.Message);
                            PathHelper.CopyDirectory(oldMediaPath, newMediaPath);
                        }
                        PathHelper.DeleteEmptyDirectory(Path.GetDirectoryName(oldMediaPath), true);
                    }
                }
            }
        }