/// <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) { WorkspaceModified?.Invoke(); } }
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); WorkspaceModified?.Invoke(); } _itemsCreated++; } else if (checkSubDirs) { // Update child folder loadFolder(childFolderNode.Node, true); } } if (sortChildren) { node.SortChildren(); } }
/// <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) { WorkspaceModified?.Invoke(); } }