예제 #1
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 srcFile = Path.Combine(DualityApp.PluginDirectory, Path.GetFileName(dstFile));
                    if (File.Exists(srcFile))
                    {
                        File.Copy(srcFile, 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 for further operations
            string oldPath = Environment.CurrentDirectory;

            Environment.CurrentDirectory = projFolder;

            // Initialize content
            if (Directory.Exists(DualityApp.DataDirectory))
            {
                // Read content source code data (needed to rename classes / namespaces)
                string oldRootNamespaceNameCore;
                string newRootNamespaceNameCore;
                DualityEditorApp.ReadPluginSourceCodeContentData(out oldRootNamespaceNameCore, out newRootNamespaceNameCore);

                // Rename classes / namespaces
                List <string> resFiles = Resource.GetResourceFiles();
                foreach (string resFile in resFiles)
                {
                    MetaFormatHelper.FilePerformAction(resFile, d => d.ReplaceTypeStrings(oldRootNamespaceNameCore, newRootNamespaceNameCore));
                }
            }

            // Initialize AppData
            DualityAppData data;

            if (File.Exists(DualityApp.AppDataPath))
            {
                try
                {
                    using (FileStream str = File.OpenRead(DualityApp.AppDataPath))
                        using (var formatter = Formatter.Create(str))
                        {
                            data = formatter.ReadObject <DualityAppData>() ?? new DualityAppData();
                        }
                }
                catch (Exception) { data = new DualityAppData(); }
            }
            else
            {
                data = new DualityAppData();
            }
            data.AppName    = projName;
            data.AuthorName = Environment.UserName;
            data.Version    = 0;
            using (FileStream str = File.Open(DualityApp.AppDataPath, FileMode.Create))
                using (var formatter = Formatter.Create(str, FormattingMethod.Binary))
                {
                    formatter.WriteObject(data);
                }

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

            // Compile plugins
            var buildProperties = new Dictionary <string, string>();

            buildProperties["Configuration"] = "Release";
            var buildRequest    = new BuildRequestData(EditorHelper.SourceCodeSolutionFile, buildProperties, null, new string[] { "Build" }, null);
            var buildParameters = new BuildParameters();
            var buildResult     = BuildManager.DefaultBuildManager.Build(buildParameters, buildRequest);

            Environment.CurrentDirectory = oldPath;
            return(Path.Combine(projFolder, "DualityEditor.exe"));
        }
예제 #2
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"));
        }
예제 #3
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);
                    }
                }
            }
        }