public bool RenameAsset(CAsset assetToRename, string newName) { lock (m_registryMutex) { if (m_assetFileMap.TryGet(assetToRename.Guid, out string assetPath)) { string oldFullPath = ProjectDefinitions.GetAbsolutePath(assetPath); string newFullPath = Path.GetDirectoryName(oldFullPath) + "/" + newName + assetToRename.GetFileExtension(); if (File.Exists(newFullPath)) { return(false); } string newRelativePath = ProjectDefinitions.GetRelativePath(newFullPath); assetToRename.Name = newName; assetToRename.Path = newRelativePath; m_assetFileMap[assetToRename.Guid] = newRelativePath; if (File.Exists(oldFullPath)) { File.Delete(oldFullPath); SaveAsset(assetToRename); } SaveRegistry(); return(true); } } return(false); }
private void OnAddFolder(object e) { string absoluteFolderPath = ProjectDefinitions.GetAbsolutePath(Path); string newFolderName = "NewFolder"; string newFolderPath = System.IO.Path.Combine(absoluteFolderPath, newFolderName); if (Directory.Exists(newFolderPath)) { bool bFoundName = false; for (int i = 0; i < 1000; i++) { string folderName = newFolderName + i; newFolderPath = System.IO.Path.Combine(absoluteFolderPath, folderName); if (!Directory.Exists(newFolderPath)) { bFoundName = true; break; } } if (!bFoundName) { return; } } Directory.CreateDirectory(newFolderPath); SubDirectories.Add(new CDirectoryEntry(ProjectDefinitions.GetRelativePath(newFolderPath), m_viewModel, this)); }
public void UpdateSubDirectories() { SubDirectories.Clear(); string absolutePath = ProjectDefinitions.GetAbsolutePath(Path); var subDirectories = Directory.GetDirectories(absolutePath); foreach (string subDirectory in subDirectories) { SubDirectories.Add(new CDirectoryEntry(ProjectDefinitions.GetRelativePath(subDirectory), m_viewModel, this)); } }
internal override void MoveCustomResources(string newFolder) { base.MoveCustomResources(newFolder); newFolder = CAssetRegistry.SanitizeAssetPath(newFolder); string currentImagePath = ProjectDefinitions.GetAbsolutePath(DDSImagePath); string newAbsoluteImagePath = ProjectDefinitions.GetAbsolutePath(newFolder + Name + ".dds"); if (currentImagePath != newAbsoluteImagePath) { if (File.Exists(currentImagePath)) { File.Move(currentImagePath, newAbsoluteImagePath); } DDSImagePath = ProjectDefinitions.GetRelativePath(newAbsoluteImagePath); } }
/// <summary> /// Save modifications made to the given asset, only needed if an asset is modified after registration, does not work for non registered assets use RequestRegisterAsset instead /// </summary> /// <param name="assetToSave"></param> public void SaveAsset(CAsset assetToSave) { lock (m_registryMutex) { if (m_assetFileMap.TryGet(assetToSave.Guid, out string assetFilename)) { string absoluteFilename = ProjectDefinitions.GetAbsolutePath(assetFilename); FileInfo fileInfo = new FileInfo(absoluteFilename); string relativeDirectory = ProjectDefinitions.GetRelativePath(fileInfo.DirectoryName) + '/'; assetToSave.SaveCustomResources(relativeDirectory); FileStream fileStream = new FileStream(absoluteFilename, FileMode.Create); CAssetSerializer.Instance.SerializeToStream(assetToSave, fileStream); } else { throw new Exception("Tried to save an asset that is not registered in the registry, make sure to register assets with RequestRegisterAsset"); } } }
public bool RenameFolder(string folderPath, string newFolderName) { System.Diagnostics.Debug.Assert(!Path.HasExtension(folderPath)); lock (m_registryMutex) { // Can't rename project root if (string.IsNullOrWhiteSpace(folderPath)) { return(false); } folderPath = SanitizeAssetPath(folderPath); string absoluteCurrent = ProjectDefinitions.GetAbsolutePath(folderPath); DirectoryInfo dirInfo = new DirectoryInfo(absoluteCurrent); if (dirInfo.Parent != null) { string absoluteTarget = Path.Combine(dirInfo.Parent.FullName, newFolderName); if (!Directory.Exists(absoluteTarget)) { Directory.CreateDirectory(absoluteTarget); } List <FileInfo> movedFiles = new List <FileInfo>(); List <DirectoryInfo> movedFolders = new List <DirectoryInfo>(); try { foreach (FileInfo file in dirInfo.EnumerateFiles()) { file.MoveTo(Path.Combine(absoluteTarget, file.Name)); movedFiles.Add(file); } foreach (DirectoryInfo directory in dirInfo.EnumerateDirectories()) { directory.MoveTo(absoluteTarget); movedFolders.Add(directory); } } catch (IOException e) { LogUtility.Log("Could not rename folder " + e.Message); // Revert already moved files foreach (FileInfo movedFile in movedFiles) { movedFile.MoveTo(Path.Combine(absoluteCurrent, movedFile.Name)); } foreach (DirectoryInfo movedFolder in movedFolders) { movedFolder.MoveTo(absoluteCurrent); } return(false); } try { dirInfo.Delete(); } catch (IOException) { } string targetPath = SanitizeAssetPath(ProjectDefinitions.GetRelativePath(absoluteTarget)); // Afterwards we fix the file paths in the registry List <(Guid, string)> keysToChange = new List <(Guid, string)>(); foreach (var guidPathPair in m_assetFileMap.KeyToValue) { if (guidPathPair.Value.StartsWith(folderPath)) { string newPath = targetPath + guidPathPair.Value.Substring(folderPath.Length); keysToChange.Add((guidPathPair.Key, newPath)); } } foreach (var change in keysToChange) { if (m_assetMap.TryGetValue(change.Item1, out CAsset changedAsset)) { changedAsset.Path = change.Item2; changedAsset.MoveCustomResources(targetPath); } m_assetFileMap[change.Item1] = change.Item2; } SaveRegistry(); return(true); } return(false); } }