Example #1
0
        /// <summary>
        /// Runs the projects generation.
        /// </summary>
        public void Run()
        {
            if (IsActive)
            {
                return;
            }

            try
            {
                OnStart();
                var customArgs = Editor.Instance.CodeEditing.SelectedEditor.GenerateProjectCustomArgs;
                ScriptsBuilder.GenerateProject(customArgs);
            }
            finally
            {
                OnEnd();
            }
        }
        /// <summary>
        /// Performs the plugin exporting.
        /// </summary>
        /// <param name="options">The export options.</param>
        /// <returns>The error message or null if not failed.</returns>
        public static string DoExport(ref ExportOptions options)
        {
            var startTime = DateTime.Now;

            Editor.Log("Exporting plugin " + options.Description.Name);

            // Validate data
            if (string.IsNullOrEmpty(options.ShortName))
            {
                return("Missing short name.");
            }
            if (options.ShortName == "Assembly")
            {
                return("Invalid short name. Don't use restricted names.");
            }
            if (options.ShortName.Contains(' ') || options.ShortName.Contains('\n') || options.ShortName.Contains('\r') || options.ShortName.Contains('\t'))
            {
                return("Invalid short name. It cannot contain whitespace characters.");
            }
            if (Utilities.Utils.HasInvalidPathChar(options.ShortName))
            {
                return("Invalid short name. It must be a valid path name.");
            }

            // Prepare
            var assemblyName         = options.ShortName;
            var solutionPath         = ScriptsBuilder.SolutionPath;
            var assembliesOutputPath = Path.Combine(Globals.ProjectCacheFolder, "bin");

            // Remove previous compilation artifacts (if user changes plugin name it may leave some old name assemblies)
            try
            {
                Editor.Log("Cleanup assemblies output directory");

                var files = Directory.GetFiles(assembliesOutputPath, "*.dll").ToList();
                RemoveStartsWith(files, "FlaxEngine");
                RemoveStartsWith(files, "FlaxEditor");
                RemoveStartsWith(files, "Newtonsoft.Json");
                Remove(files, "Assembly");
                Remove(files, "Assembly.Editor");
                Remove(files, assemblyName);
                Remove(files, assemblyName + ".Editor");

                for (int i = 0; i < files.Count; i++)
                {
                    Editor.Log("Removing " + files[i]);
                    File.Delete(files[i]);
                }
            }
            catch (Exception ex)
            {
                Editor.LogWarning(ex);
                return("Failed to cleanup assemblies output directory.");
            }

            // Compile project scripts as a plugin
            if (ScriptsBuilder.GeneratePluginProject(assemblyName))
            {
                return("Failed to generate plugin solution and project files.");
            }
            if (ScriptsBuilder.Compile(solutionPath, options.Configuration))
            {
                return("Scripts compilation failed. See Debug Console or log file to learn more.");
            }
            if (ScriptsBuilder.GenerateProject(true, true))
            {
                return("Failed to generate restore solution and project files.");
            }

            // Setup output directory
            var outputPath = options.OutputPath;

            try
            {
                Editor.Log("Setup output directory");
                if (Directory.Exists(outputPath))
                {
                    Directory.Delete(outputPath, true);
                }
                Directory.CreateDirectory(outputPath);
            }
            catch (Exception ex)
            {
                Editor.LogWarning(ex);
                return("Failed to setup output directory.");
            }

            // Copy plugin binaries
            try
            {
                Editor.Log("Exporting plugin binaries");

                var files = Directory.GetFiles(assembliesOutputPath).ToList();

                // Don't copy Flax files
                RemoveStartsWith(files, "FlaxEngine");
                RemoveStartsWith(files, "FlaxEditor");
                RemoveStartsWith(files, "Newtonsoft.Json");

                // Don't copy pdb files if release mode is checked
                if (options.Configuration == BuildMode.Release)
                {
                    RemoveEndsWith(files, ".pdb");
                }

                // Don't copy game assembly if plugin is editor-only and vice versa
                if (options.GamePlugin == null)
                {
                    Remove(files, assemblyName);
                }
                else if (options.EditorPlugin == null)
                {
                    Remove(files, assemblyName + ".Editor");
                }

                // Don't copy previous game assembly
                Remove(files, "Assembly");
                Remove(files, "Assembly.Editor");

                Editor.Log(files.Count + " files");
                for (int i = 0; i < files.Count; i++)
                {
                    var src      = files[i];
                    var filename = Path.GetFileName(src);
                    var dst      = Path.Combine(outputPath, filename);
                    File.Copy(src, dst);
                }
            }
            catch (Exception ex)
            {
                Editor.LogWarning(ex);
                return("Failed to copy plugin binaries.");
            }

            // Copy content
            try
            {
                if (options.IncludeContent)
                {
                    Editor.Log("Exporting plugin content");

                    // Copy all assets
                    var src = Globals.ContentFolder;
                    var dst = Path.Combine(outputPath, "Content");
                    Utilities.Utils.DirectoryCopy(src, dst);

                    // Remove some unwanted data
                    Utilities.Utils.RemoveFileIfExists(Path.Combine(dst, "GameSettings.json"));
                }
            }
            catch (Exception ex)
            {
                Editor.LogWarning(ex);
                return("Failed to copy plugin content.");
            }

            // Copy plugin icon
            try
            {
                if (options.Icon)
                {
                    Editor.Log("Exporting plugin icon");

                    var src = options.Icon.Path;
                    var dst = Path.Combine(outputPath, assemblyName + ".Icon.flax");
                    File.Copy(src, dst);
                }
            }
            catch (Exception ex)
            {
                Editor.LogWarning(ex);
                return("Failed to copy plugin icon.");
            }

            // Generate plugin description json file
            try
            {
                Editor.Log("Exporting plugin description");

                var dst = Path.Combine(outputPath, assemblyName + ".json");
                if (Editor.SaveJsonAsset(dst, options.Description))
                {
                    throw new FlaxException("Failed to save json asset.");
                }
            }
            catch (Exception ex)
            {
                Editor.LogWarning(ex);
                return("Failed to generate plugin description.");
            }

            // Done!
            var endTime = DateTime.Now;

            Editor.Log(string.Format("Plugin exported in {0}s", Math.Max(1, (int)(endTime - startTime).TotalSeconds)));
            return(null);
        }