예제 #1
0
        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);
                    }
                }
            }
        }
예제 #2
0
        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);
        }