Beispiel #1
0
        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 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);
        }
        /// <summary>
        /// Tries to register the given asset, returns true and the asset if an asset with the same guid or the same path already exists in case AlwaysImport is false
        /// </summary>
        /// <typeparam name="T"></typeparam>
        /// <param name="baseAsset"></param>
        /// <param name="basePath"></param>
        /// <param name="existingAsset"></param>
        /// <param name="bAlwaysImport"></param>
        /// <returns></returns>
        public bool RequestRegisterAsset <T>(T baseAsset, string basePath, out T existingAsset, bool bAlwaysImport = false) where T : CAsset, new()
        {
            lock (m_registryMutex)
            {
                if (m_assetMap.TryGetValue(baseAsset.Guid, out CAsset foundAsset))
                {
                    existingAsset = (T)foundAsset;
                    return(true);
                }

                basePath = SanitizeAssetPath(basePath);
                string assetFileName = basePath + baseAsset.Name + baseAsset.GetFileExtension();
                if (m_assetFileMap.TryGet(assetFileName, out Guid foundGuid))
                {
                    if (!bAlwaysImport)
                    {
                        baseAsset.Guid = foundGuid;
                        existingAsset  = GetAsset <T>(foundGuid);
                        return(true);
                    }

                    if (FileUtilities.GetNextAvailableAssetFile(assetFileName, m_assetFileMap.ValueToKey, out string foundFileName))
                    {
                        assetFileName  = foundFileName;
                        baseAsset.Name = Path.GetFileNameWithoutExtension(foundFileName);
                    }
                    else
                    {
                        throw new Exception("Couldn't find a valid asset filename");
                    }
                }

                existingAsset = null;
                m_assetFileMap.Add(baseAsset.Guid, assetFileName);
                baseAsset.Path = assetFileName;
                m_assetMap.Add(baseAsset.Guid, baseAsset);

                // todo henning defer this until asset is loaded in case it is not loaded yet
                if (AutoSaveAssets > 0)
                {
                    Task.Run(() =>
                    {
                        string absoluteFilename = ProjectDefinitions.GetAbsolutePath(assetFileName);
                        FileInfo fileInfo       = new FileInfo(absoluteFilename);
                        fileInfo.Directory?.Create();
                        baseAsset.SaveCustomResources(basePath);
                        FileStream fileStream = new FileStream(absoluteFilename, FileMode.Create);
                        CAssetSerializer.Instance.SerializeToStream(baseAsset, fileStream);
                        SaveRegistry();
                    });
                }
                else
                {
                    m_unsavedAssets.Add(baseAsset);
                }
            }

            return(false);
        }
        internal override void RemoveCustomResources()
        {
            base.RemoveCustomResources();
            string imagePath = ProjectDefinitions.GetAbsolutePath(DDSImagePath);

            if (File.Exists(imagePath))
            {
                File.Delete(imagePath);
            }
        }
 public void SaveRegistry()
 {
     lock (m_registryMutex)
     {
         string   json     = CAssetSerializer.Instance.Serialize(m_assetFileMap.KeyToValue);
         string   filename = ProjectDefinitions.GetAbsolutePath(REGISTRY_FILEPATH);
         FileInfo fileInfo = new FileInfo(filename);
         fileInfo.Directory?.Create();
         File.WriteAllText(filename, json);
     }
 }
Beispiel #6
0
        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));
            }
        }
        public override bool LoadCustomResources()
        {
            string absolutePath = ProjectDefinitions.GetAbsolutePath(DDSImagePath);

            if (File.Exists(absolutePath))
            {
                ImageSurface = Surface.LoadFromFile(absolutePath);
                ImageSurface.FlipVertically();
                return(true);
            }

            return(true);
        }
        private CAsset DeserializeAsset(string filePath)
        {
            FileStream fileStream  = new FileStream(ProjectDefinitions.GetAbsolutePath(filePath), FileMode.Open);
            CAsset     loadedAsset = (CAsset)CAssetSerializer.Instance.DeserializeFromStream(fileStream);

            loadedAsset.Path = filePath;

            if (loadedAsset.LoadCustomResources())
            {
                loadedAsset.LoadFinished();
            }

            return(loadedAsset);
        }
        public override void SaveCustomResources(string directory)
        {
            DDSImagePath = directory + Name + ".dds";
            using (Compressor compressor = new Compressor())
            {
                compressor.Input.GenerateMipmaps = false;
                compressor.Input.SetData(ImageSurface);
                compressor.Compression.Format = CompressionFormat.DXT1a;
                compressor.Compression.SetBGRAPixelFormat();

                compressor.Process(ProjectDefinitions.GetAbsolutePath(DDSImagePath));
                compressor.Dispose();
            }
        }
        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);
            }
        }
        private void DeserializeAsset <T>(string filePath, T outAsset) where T : CAsset
        {
            FileStream fileStream  = new FileStream(ProjectDefinitions.GetAbsolutePath(filePath), FileMode.Open);
            T          loadedAsset = CAssetSerializer.Instance.DeserializeFromStream <T>(fileStream);

            outAsset.Path = filePath;

            if (loadedAsset.LoadCustomResources())
            {
                outAsset.CopyFrom(loadedAsset);
                outAsset.LoadFinished();
            }
            else
            {
                outAsset.CopyFrom(loadedAsset);
            }
        }
        public void RegisterAsset(CAsset asset, string basePath, bool bOverride)
        {
            lock (m_registryMutex)
            {
                if (!m_assetFileMap.ContainsKey(asset.Guid))
                {
                    basePath = SanitizeAssetPath(basePath);
                    string assetFileName = basePath + asset.Name + asset.GetFileExtension();
                    if (!bOverride)
                    {
                        if (FileUtilities.GetNextAvailableAssetFile(assetFileName, m_assetFileMap.ValueToKey, out string foundFileName))
                        {
                            assetFileName = foundFileName;
                            asset.Name    = Path.GetFileNameWithoutExtension(foundFileName);
                        }
                        else
                        {
                            throw new Exception("Couldn't find a valid asset filename");
                        }
                    }

                    asset.Path = assetFileName;
                    m_assetFileMap.Add(asset.Guid, assetFileName);
                    m_assetMap.Add(asset.Guid, asset);

                    // todo henning defer this until asset is loaded in case it is not loaded yet
                    if (AutoSaveAssets > 0)
                    {
                        Task.Run(() =>
                        {
                            string absoluteFilename = ProjectDefinitions.GetAbsolutePath(assetFileName);
                            FileInfo fileInfo       = new FileInfo(absoluteFilename);
                            fileInfo.Directory?.Create();
                            asset.SaveCustomResources(basePath);
                            FileStream fileStream = new FileStream(absoluteFilename, FileMode.Create);
                            CAssetSerializer.Instance.SerializeToStream(asset, fileStream);
                            SaveRegistry();
                        });
                    }
                    else
                    {
                        m_unsavedAssets.Add(asset);
                    }
                }
            }
        }
        public static void LoadInstance()
        {
            string registryFilename = ProjectDefinitions.GetAbsolutePath(REGISTRY_FILEPATH);

            if (File.Exists(registryFilename))
            {
                Dictionary <Guid, string> loadedFileMap = CAssetSerializer.Instance.DeserializeObject <Dictionary <Guid, string> >(File.ReadAllText(registryFilename));

                // Validate asset entries for their files to exist
                loadedFileMap           = loadedFileMap.Where(pair => File.Exists(ProjectDefinitions.GetAbsolutePath(pair.Value))).ToDictionary(p => p.Key, p => p.Value);
                Instance.m_assetFileMap = new BiDictionary <Guid, string>(loadedFileMap);
            }
            else
            {
                Instance = new CAssetRegistry();
            }
            EngineBaseContentLoader.LoadBaseContent();
        }
 /// <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");
         }
     }
 }
Beispiel #15
0
        public CDirectoryEntry(string directoryPath, CAssetBrowserViewModel viewModel, CDirectoryEntry parent = null)
        {
            ParentDirectory = parent;
            m_viewModel     = viewModel;
            Path            = directoryPath;
            string        absolutePath = ProjectDefinitions.GetAbsolutePath(directoryPath);
            DirectoryInfo dirInfo      = new DirectoryInfo(absolutePath);

            Name     = string.IsNullOrWhiteSpace(directoryPath) ? "Project" : dirInfo.Name;
            EditName = Name;

            UpdateSubDirectories();

            SelectCommand       = new CRelayCommand(OnDirectoryClicked);
            DragEnterCommand    = new CRelayCommand(OnDragEnter);
            DragOverCommand     = new CRelayCommand(OnDragOver);
            DropCommand         = new CRelayCommand(OnDrop);
            AddFolderCommand    = new CRelayCommand(OnAddFolder);
            DeleteFolderCommand = new CRelayCommand(OnDeleteFolder);
        }
        public bool MoveAssetFile(CAsset assetToMove, string targetPath)
        {
            lock (m_registryMutex)
            {
                if (m_assetFileMap.TryGet(assetToMove.Guid, out string currentPath))
                {
                    string assetFileName      = Path.GetFileName(currentPath);
                    string targetRelativePath = SanitizeAssetPath(Path.Combine(targetPath, assetFileName));
                    string absoluteCurrent    = ProjectDefinitions.GetAbsolutePath(currentPath);
                    string absoluteTarget     = ProjectDefinitions.GetAbsolutePath(targetRelativePath);

                    File.Move(absoluteCurrent, absoluteTarget);
                    m_assetFileMap[assetToMove.Guid] = targetRelativePath;
                    assetToMove.Path = targetRelativePath;
                    assetToMove.MoveCustomResources(targetRelativePath);
                    SaveRegistry();
                    return(true);
                }
            }

            return(false);
        }
        public bool RemoveFolder(string folderPath)
        {
            folderPath = SanitizeAssetPath(folderPath);
            string        absolutePath = ProjectDefinitions.GetAbsolutePath(folderPath);
            DirectoryInfo folderInfo   = new DirectoryInfo(absolutePath);

            lock (m_registryMutex)
            {
                if (folderInfo.Exists)
                {
                    folderInfo.Delete(true);
                    List <Guid> guidsToRemove = new List <Guid>();
                    foreach (var guidPathPair in m_assetFileMap.KeyToValue)
                    {
                        if (guidPathPair.Value.StartsWith(folderPath))
                        {
                            guidsToRemove.Add(guidPathPair.Key);
                        }
                    }

                    foreach (var guid in guidsToRemove)
                    {
                        if (m_assetMap.TryGetValue(guid, out CAsset asset))
                        {
                            asset.RemoveCustomResources();
                            m_assetMap.Remove(guid);
                        }

                        m_assetFileMap.Remove(guid);
                    }

                    SaveRegistry();
                    return(true);
                }
            }

            return(false);
        }
        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);
            }
        }
        public bool MoveAssetFolder(string folderPath, string targetPath)
        {
            System.Diagnostics.Debug.Assert(!Path.HasExtension(folderPath));
            System.Diagnostics.Debug.Assert(!Path.HasExtension(targetPath));
            folderPath = SanitizeAssetPath(folderPath);
            targetPath = SanitizeAssetPath(targetPath);
            string absoluteCurrent = ProjectDefinitions.GetAbsolutePath(folderPath);

            if (Directory.Exists(absoluteCurrent))
            {
                DirectoryInfo current = new DirectoryInfo(absoluteCurrent);
                targetPath = SanitizeAssetPath(targetPath + current.Name);

                if (targetPath == folderPath)
                {
                    return(false);
                }

                // We first move the assets on disk
                string absoluteTarget = ProjectDefinitions.GetAbsolutePath(targetPath);
                lock (m_registryMutex)
                {
                    try
                    {
                        if (!Directory.Exists(absoluteTarget))
                        {
                            Directory.Move(absoluteCurrent, absoluteTarget);
                        }
                        else
                        {
                            DirectoryInfo targetInfo = new DirectoryInfo(absoluteTarget);
                            foreach (FileInfo file in targetInfo.EnumerateFiles())
                            {
                                file.MoveTo(Path.Combine(absoluteTarget, file.Name));
                            }

                            foreach (DirectoryInfo directory in targetInfo.EnumerateDirectories())
                            {
                                directory.MoveTo(absoluteTarget);
                            }
                        }
                    }
                    catch (IOException e)
                    {
                        LogUtility.Log("Could not move folder " + e.Message);
                        return(false);
                    }

                    // 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);
        }
        public bool TryGetAsset <T>(Guid assetId, out T outAsset) where T : CAsset
        {
            lock (m_registryMutex)
            {
                if (m_assetMap.TryGetValue(assetId, out CAsset asset))
                {
                    outAsset = (T)asset;
                    return(true);
                }
                else if (m_assetFileMap.TryGet(assetId, out string filename))
                {
                    T loadedAsset = CAssetSerializer.Instance.DeserializeObject <T>(File.ReadAllText(ProjectDefinitions.GetAbsolutePath(filename)));
                    m_assetMap.Add(assetId, loadedAsset);

                    if (loadedAsset.LoadCustomResources())
                    {
                        loadedAsset.LoadFinished();
                    }

                    outAsset = loadedAsset;
                    return(true);
                }
            }

            outAsset = null;
            return(false);
        }