private void DeleteAssetInternal(Asset asset, bool silent = false) { GuidAssetTable.Remove(asset.Guid); GuidPathTable.Remove(asset.Guid); GuidHashTable.Remove(asset.Guid); if (!silent) { AssetDeleted?.Invoke(asset.Path); } }
private void AddAssetInternal(Asset asset, bool silent = false) { var guid = AssetGuidManager.GetGuid(asset.Path); if (guid != default(Guid)) { asset.SetGuid(guid); } GuidAssetTable.Add(asset.Guid, asset); GuidPathTable.Add(asset.Guid, asset.Path); GuidHashTable.Add(asset.Guid, asset.Hash); AssetGuidManager.AddNewGuid(asset.Guid, asset.Path, asset.Hash); if (!IsEditingAssets) { AssetGuidManager.Save(); } if (!silent) { AssetCreated?.Invoke(asset.Path); } }
public void Refresh() { Profiler.Start("AssetManager_Refresh"); BeginAssetEditing(); var paths = Paths.GetAllFilePaths(); var assetsOnDisk = paths.Select(path => new Asset(path)); // Detect renamed assets if application was closed, and assets were renamed via file system foreach (var pair in AssetGuidManager.Paths.ToList()) { var path = pair.Value; if (!File.Exists(path)) { var guid = pair.Key; var hash = AssetGuidManager.GetHash(guid); var assetOnDiskWithSameHashButNotKnownPath = assetsOnDisk.FirstOrDefault( a => a.Hash == hash && !AssetGuidManager.ContainsValue(a.Path)); // If this asset on disk is found, update old guid to new path, since best prediction is that it was renamed if (assetOnDiskWithSameHashButNotKnownPath != null) { AssetGuidManager.AddNewGuid(guid, assetOnDiskWithSameHashButNotKnownPath.Path, hash); Logger.Log(LogType.Log, "Asset '" + assetOnDiskWithSameHashButNotKnownPath.Name + "' was recognized as renamed asset"); } } } // Detect Rename for assets in memory (while keeping existing asset references) foreach (var assetInMemory in Assets.ToList()) { if (!File.Exists(assetInMemory.Path)) { // if known path does not exist on disk anymore but some other asset with same hash exists on disk, it must have been renamed var assetWithSameHashAndNotInDbYet = assetsOnDisk.FirstOrDefault(asset => asset.Hash == assetInMemory.Hash && !GuidPathTable.ContainsValue(asset.Path)); if (assetWithSameHashAndNotInDbYet != null) { RenameAssetInternal(assetInMemory.Path, assetWithSameHashAndNotInDbYet.Path); Logger.Log(LogType.Log, "Asset '" + assetInMemory.Name + "' was renamed to '" + assetWithSameHashAndNotInDbYet.Name + "'"); } else { DeleteAssetInternal(assetInMemory); Logger.Log(LogType.Log, "Asset was deleted: '" + assetInMemory.Name + "'"); } } } // Add new assets and detect modifications foreach (var assetOnDisk in assetsOnDisk) { var isHashKnown = GuidHashTable.ContainsValue(assetOnDisk.Hash); var isPathKnown = GuidPathTable.ContainsValue(assetOnDisk.Path); // We know the path, but hash has changed, must have been modified if (!isHashKnown && isPathKnown) { UpdateAssetInternal(GetAsset(assetOnDisk.Path)); Logger.Log(LogType.Log, "Asset was modified: '" + assetOnDisk.Name + "'"); } // New file added else if (!isPathKnown) { AddAssetInternal(assetOnDisk); } } EndAssetEditing(); Profiler.Stop("AssetManager_Refresh"); StatusManager.Add("AssetManager", 10, new Status(null, "Asset Refresh Finished", StandardColors.Default)); // Logger.Log(LogType.Log, "Asset refresh finished"); RefreshFinished?.Invoke(); }