示例#1
0
        private bool ValidateScript(ScriptItem scriptItem)
        {
            var scriptName = scriptItem.ScriptName;
            var scriptType = ScriptsBuilder.FindScript(scriptName);

            return(scriptType != null);
        }
        private void LoadScripts(ContentTreeNode parent, string[] files)
        {
            for (int i = 0; i < files.Length; i++)
            {
                var path = StringUtils.NormalizePath(files[i]);

                // Check if node already has that element (skip during init when we want to walk project dir very fast)
                if (_isDuringFastSetup || !parent.Folder.ContainsChild(path))
                {
                    // Create file item
                    ContentItem item;
                    if (path.EndsWith(".cs"))
                    {
                        item = new CSharpScriptItem(path);
                    }
                    else if (path.EndsWith(".cpp") || path.EndsWith(".h"))
                    {
                        item = new CppScriptItem(path);
                    }
                    else if (path.EndsWith(".shader") || path.EndsWith(".hlsl"))
                    {
                        item = new ShaderSourceItem(path);
                    }
                    else
                    {
                        item = new FileItem(path);
                    }

                    // Link
                    item.ParentFolder = parent.Folder;

                    // Fire event
                    if (_enableEvents)
                    {
                        ItemAdded?.Invoke(item);
                        WorkspaceModified?.Invoke();
                        if (!path.EndsWith(".Gen.cs"))
                        {
                            if (item is ScriptItem)
                            {
                                ScriptsBuilder.MarkWorkspaceDirty();
                            }
                            if (item is ScriptItem || item is ShaderSourceItem)
                            {
                                Editor.CodeEditing.SelectedEditor.OnFileAdded(path);
                            }
                        }
                    }
                    _itemsCreated++;
                }
            }
        }
示例#3
0
        /// <summary>
        /// Requests start playing in editor.
        /// </summary>
        public void RequestStartPlay()
        {
            if (Editor.StateMachine.IsEditMode)
            {
                Editor.Log("[PlayMode] Start");

                if (Editor.Options.Options.General.AutoReloadScriptsOnMainWindowFocus)
                {
                    ScriptsBuilder.CheckForCompile();
                }
                _isPlayModeRequested = true;
                Editor.UI.UpdateToolstrip();
            }
        }
示例#4
0
        /// <summary>
        /// Requests start playing in editor.
        /// </summary>
        public void RequestStartPlay()
        {
            // Check if is in edit mode
            if (Editor.StateMachine.IsEditMode)
            {
                Editor.Log("[PlayMode] Start");

                // Request to be compiled
                ScriptsBuilder.CheckForCompile();

                // Set flag
                _isPlayModeRequested = true;

                // Update
                Editor.UI.UpdateToolstrip();
            }
        }
示例#5
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();
            }
        }
示例#6
0
 private void Compile()
 {
     this.Dispatcher.Invoke((Action)(() =>
     {
         if (!ScriptsBuilder.ReloadScripts())
         {
             ErrorDataGrid.ItemsSource = ScriptsBuilder.Logger.Errors;
             if (ErrorDataGrid.Items.Count > 0)
             {
                 ErrorsExpander.IsExpanded = true;
             }
         }
         else
         {
             ErrorDataGrid.ItemsSource = null;
             EditorCommands.CreatePropertyGridView();
         }
     }));
 }
示例#7
0
        private void AddScripts(List <ScriptItem> items)
        {
            var list = new List <ScriptType>(items.Count);

            for (int i = 0; i < items.Count; i++)
            {
                var item       = items[i];
                var scriptName = item.ScriptName;
                var scriptType = ScriptsBuilder.FindScript(scriptName);
                if (scriptType == null)
                {
                    Editor.LogWarning("Invalid script type " + scriptName);
                }
                else
                {
                    list.Add(new ScriptType(scriptType));
                }
            }
            AddScripts(list);
        }
示例#8
0
        private void LoadScripts(ContentTreeNode parent, string directory)
        {
            // Find files
            var files = Directory.GetFiles(directory, ScriptProxy.ExtensionFiler, SearchOption.TopDirectoryOnly);

            // Add them
            bool anyAdded = false;

            for (int i = 0; i < files.Length; i++)
            {
                var path = StringUtils.NormalizePath(files[i]);

                // Check if node already has that element (skip during init when we want to walk project dir very fast)
                if (_isDuringFastSetup || !parent.Folder.ContainsChild(path))
                {
                    // Create item object
                    var item = new ScriptItem(path);

                    // Link
                    item.ParentFolder = parent.Folder;

                    // Fire event
                    if (_enableEvents)
                    {
                        ItemAdded?.Invoke(item);
                        OnWorkspaceModified?.Invoke();
                    }
                    _itemsCreated++;
                    anyAdded = true;
                }
            }

            if (anyAdded && _enableEvents)
            {
                ScriptsBuilder.MarkWorkspaceDirty();
            }
        }
 /// <inheritdoc />
 public void OpenFile(string path, int line)
 {
     ScriptsBuilder.Internal_OpenFile(Type, path, line);
 }
 /// <inheritdoc />
 public void OpenSolution()
 {
     ScriptsBuilder.Internal_OpenSolution(Type);
 }
示例#11
0
        /// <inheritdoc />
        public override void OnDelete()
        {
            ScriptsBuilder.MarkWorkspaceDirty();

            base.OnDelete();
        }
示例#12
0
 /// <summary>
 /// Opens the file.
 /// </summary>
 /// <param name="path">The source file path.</param>
 public void OpenFile(string path)
 {
     ScriptsBuilder.OpenFile(path);
 }
示例#13
0
        /// <summary>
        /// Renames the specified item.
        /// </summary>
        /// <param name="item">The item.</param>
        /// <param name="newShortName">New name (without extension, just the filename).</param>
        public void Rename(ContentItem item, string newShortName)
        {
            if (item == null)
            {
                throw new ArgumentNullException();
            }

            // Check if can rename this item
            if (!item.CanRename)
            {
                // Cannot
                MessageBox.Show("Cannot rename this item.", "Cannot rename", MessageBoxButtons.OK, MessageBoxIcon.Error);
                return;
            }

            // Renaming a file to an extension it already has
            if (!item.IsFolder && StringUtils.NormalizeExtension(Path.GetExtension(newShortName)) == StringUtils.NormalizeExtension(Path.GetExtension(item.Path)))
            {
                newShortName = StringUtils.GetPathWithoutExtension(newShortName);
            }

            // Check if name is valid
            if (!Editor.ContentEditing.IsValidAssetName(item, newShortName, out string hint))
            {
                // Invalid name
                MessageBox.Show("Given asset name is invalid. " + hint,
                                "Invalid name",
                                MessageBoxButtons.OK,
                                MessageBoxIcon.Error);
                return;
            }

            // Ensure has parent
            if (item.ParentFolder == null)
            {
                // Error
                Editor.LogWarning("Cannot rename root items. " + item.Path);
                return;
            }

            // Cache data
            string extension = Path.GetExtension(item.Path);
            var    newPath   = StringUtils.CombinePaths(item.ParentFolder.Path, newShortName + extension);

            // Check if was renaming mock element
            // Note: we create `_newElement` and then rename it to create new asset
            var itemFolder = item.ParentFolder;
            Action <ContentItem> endEvent = null;

            if (_newElement == item)
            {
                try
                {
                    endEvent = (Action <ContentItem>)_newElement.Tag;

                    // Create new asset
                    var proxy = _newElement.Proxy;
                    Editor.Log(string.Format("Creating asset {0} in {1}", proxy.Name, newPath));
                    proxy.Create(newPath, _newElement.Argument);
                }
                catch (Exception ex)
                {
                    Editor.LogWarning(ex);
                    Editor.LogError("Failed to create asset.");
                }
            }
            else
            {
                // Validate state
                Assert.IsNull(_newElement);

                // Rename asset
                Editor.Log(string.Format("Renaming asset {0} to {1}", item.Path, newShortName));
                Editor.ContentDatabase.Move(item, newPath);
            }

            if (_newElement != null)
            {
                // Trigger compilation if need to
                if (_newElement.Proxy is ScriptProxy && Editor.Instance.Options.Options.General.AutoReloadScriptsOnMainWindowFocus)
                {
                    ScriptsBuilder.MarkWorkspaceDirty();
                }

                // Destroy mock control
                _newElement.ParentFolder = null;
                _newElement.Dispose();
                _newElement = null;

                // Focus content window
                Focus();
                RootWindow?.Focus();
            }

            // Refresh database and view now
            Editor.ContentDatabase.RefreshFolder(itemFolder, true);
            RefreshView();

            if (endEvent != null)
            {
                var newItem = itemFolder.FindChild(newPath);
                if (newItem != null)
                {
                    endEvent(newItem);
                }
                else
                {
                    Editor.LogWarning("Failed to find the created new item.");
                }
            }
        }
示例#14
0
        /// <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);
        }
示例#15
0
 public GameBuilder()
 {
     _scriptsBuilder = new ScriptsBuilder();
     _assetsBuilder  = new AssetsBuilder();
 }
示例#16
0
        /// <inheritdoc />
        public override void OnPathChanged()
        {
            ScriptsBuilder.MarkWorkspaceDirty();

            base.OnPathChanged();
        }