Exemplo n.º 1
0
        private void LoadAssets(ContentTreeNode parent, string directory)
        {
            // Find files
            var files = Directory.GetFiles(directory, "*.*", SearchOption.TopDirectoryOnly);

            // Add them
            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))
                {
                    // It can be any type of asset: binary, text, cooked, package, etc.
                    // The best idea is to just ask Flax.
                    // Flax isn't John Snow. Flax knows something :)
                    // Also Flax Content Layer is using smart caching so this query gonna be fast.

                    ContentItem item = null;

                    // Try using in-build asset
                    string typeName;
                    Guid   id;
                    if (FlaxEngine.Content.GetAssetInfo(path, out typeName, out id))
                    {
                        var proxy = GetAssetProxy(typeName, path);
                        item = proxy?.ConstructItem(path, typeName, ref id);
                    }

                    // Generic file
                    if (item == null)
                    {
                        item = new FileItem(path);
                    }

                    // Link
                    item.ParentFolder = parent.Folder;

                    // Fire event
                    if (_enableEvents)
                    {
                        ItemAdded?.Invoke(item);
                        OnWorkspaceModified?.Invoke();
                    }
                    _itemsCreated++;
                }
            }
        }
Exemplo n.º 2
0
        /// <inheritdoc />
        public override void OnUpdate()
        {
            while (_dirtyNodes.Count > 0)
            {
                // Get node
                var node = _dirtyNodes.Dequeue();

                // Refresh
                loadFolder(node, true);

                // Fire event
                if (_enableEvents)
                {
                    OnWorkspaceModified?.Invoke();
                }
            }
        }
Exemplo n.º 3
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();
            }
        }
Exemplo n.º 4
0
        private void loadFolder(ContentTreeNode node, bool checkSubDirs)
        {
            if (node == null)
            {
                return;
            }

            // Temporary data
            var folder = node.Folder;
            var path   = folder.Path;

            // Check for missing files/folders (skip it during fast tree setup)
            if (!_isDuringFastSetup)
            {
                for (int i = 0; i < folder.Children.Count; i++)
                {
                    var child = folder.Children[i];
                    if (!child.Exists)
                    {
                        // Send info
                        Editor.Log(string.Format($"Content item \'{child.Path}\' has been removed"));

                        // Destroy it
                        Delete(child);

                        i--;
                    }
                }
            }

            // Find elements (use separate path for scripts and assets - perf stuff)
            if (node.CanHaveAssets)
            {
                LoadAssets(node, path);
            }
            if (node.CanHaveScripts)
            {
                LoadScripts(node, path);
            }

            // Get child directories
            var childFolders = Directory.GetDirectories(path);

            // Load child folders
            bool sortChildren = false;

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

                // Check if node already has that element (skip during init when we want to walk project dir very fast)
                ContentFolder childFolderNode = _isDuringFastSetup ? null : node.Folder.FindChild(childPath) as ContentFolder;
                if (childFolderNode == null)
                {
                    // Create node
                    ContentTreeNode n = new ContentTreeNode(node, childPath);
                    if (!_isDuringFastSetup)
                    {
                        sortChildren = true;
                    }

                    // Load child folder
                    loadFolder(n, true);

                    // Fire event
                    if (_enableEvents)
                    {
                        ItemAdded?.Invoke(n.Folder);
                        OnWorkspaceModified?.Invoke();
                    }
                    _itemsCreated++;
                }
                else if (checkSubDirs)
                {
                    // Update child folder
                    loadFolder(childFolderNode.Node, true);
                }
            }
            if (sortChildren)
            {
                node.SortChildren();
            }
        }
Exemplo n.º 5
0
        /// <summary>
        /// Deletes the specified item.
        /// </summary>
        /// <param name="item">The item.</param>
        public void Delete(ContentItem item)
        {
            if (item == null)
            {
                throw new ArgumentNullException();
            }

            // Fire events
            if (_enableEvents)
            {
                ItemRemoved?.Invoke(item);
            }
            item.OnDelete();
            _itemsDeleted++;

            var path = item.Path;

            // Special case for folders
            if (item is ContentFolder folder)
            {
                // TODO: maybe don't remove folders recursive but at once?

                // Delete all children
                if (folder.Children.Count > 0)
                {
                    var children = folder.Children.ToArray();
                    for (int i = 0; i < children.Length; i++)
                    {
                        Delete(children[0]);
                    }
                }

                // Remove directory
                if (Directory.Exists(path))
                {
                    try
                    {
                        Directory.Delete(path, true);
                    }
                    catch (Exception ex)
                    {
                        // Error
                        Editor.LogWarning(ex);
                        Editor.LogWarning(string.Format("Cannot remove folder \'{0}\'", path));
                        return;
                    }
                }

                // Unlink from the parent
                item.ParentFolder = null;

                // Delete tree node
                folder.Node.Dispose();
            }
            else
            {
                // Check if it's an asset
                if (item.IsAsset)
                {
                    // Delete asset by using content pool
                    FlaxEngine.Content.DeleteAsset(path);
                }
                else
                {
                    // Delete file
                    if (File.Exists(path))
                    {
                        File.Delete(path);
                    }
                }

                // Unlink from the parent
                item.ParentFolder = null;

                // Delete item
                item.Dispose();
            }

            if (_enableEvents)
            {
                OnWorkspaceModified?.Invoke();
            }
        }
Exemplo n.º 6
0
        /// <summary>
        /// Moves the specified item to the different location. Handles moving whole directories and single assets.
        /// </summary>
        /// <param name="item">The item.</param>
        /// <param name="newPath">The new path.</param>
        public void Move(ContentItem item, string newPath)
        {
            if (item == null || string.IsNullOrEmpty(newPath))
            {
                throw new ArgumentNullException();
            }

            if (item.IsFolder && Directory.Exists(newPath))
            {
                // Error
                MessageBox.Show("Cannot move folder. Target location already exists.");
                return;
            }
            if (!item.IsFolder && File.Exists(newPath))
            {
                // Error
                MessageBox.Show("Cannot move file. Target location already exists.");
                return;
            }

            // Find target parent
            var newDirPath = Path.GetDirectoryName(newPath);
            var newParent  = Find(newDirPath) as ContentFolder;

            if (newParent == null)
            {
                // Error
                MessageBox.Show("Cannot move item. Missing target location.");
                return;
            }

            // Perform renaming
            {
                string oldPath = item.Path;

                // Special case for folders
                if (item.IsFolder)
                {
                    // Cache data
                    var folder = (ContentFolder)item;

                    // Create new folder
                    try
                    {
                        Directory.CreateDirectory(newPath);
                    }
                    catch (Exception ex)
                    {
                        // Error
                        Editor.LogWarning(ex);
                        Editor.LogError(string.Format("Cannot move folder \'{0}\' to \'{1}\'", oldPath, newPath));
                        return;
                    }

                    // Change path
                    item.UpdatePath(newPath);

                    // Rename all child elements
                    for (int i = 0; i < folder.Children.Count; i++)
                    {
                        UpdateAssetNewNameTree(folder.Children[i]);
                    }

                    // Delete old folder
                    try
                    {
                        Directory.Delete(oldPath, true);
                    }
                    catch (Exception ex)
                    {
                        // Error
                        Editor.LogWarning(ex);
                        Editor.LogWarning(string.Format("Cannot remove folder \'{0}\'", oldPath));
                        return;
                    }
                }
                else
                {
                    RenameAsset(item, ref newPath);
                }

                if (item.ParentFolder != null)
                {
                    item.ParentFolder.Node.SortChildren();
                }
            }

            // Link item
            item.ParentFolder = newParent;

            if (_enableEvents)
            {
                OnWorkspaceModified?.Invoke();
            }
        }