wpf3d游戏引擎下的AssetRegister.cs实现

1.AssetRegister.cs

using PrimalEditor.Common;

using PrimalEditor.Utilities;

using System;

using System.Collections.Generic;

using System.Collections.ObjectModel;

using System.Diagnostics;

using System.IO;

using System.Linq;

using System.Text;

using System.Threading.Tasks;

using System.Windows;

namespace PrimalEditor.Content

{

static class AssetRegistry

{

private static readonly DelayEventTimer _refreshTimer = new DelayEventTimer(TimeSpan.FromMicroseconds(250));

private static readonly Dictionary<string, AssetInfo> _assetDictionary = new Dictionary<string, AssetInfo>();

private static readonly ObservableCollection<AssetInfo> _assets = new ObservableCollection<AssetInfo>();

private static readonly FileSystemWatcher _contentWatcher = new FileSystemWatcher()

{

IncludeSubdirectories = true,

Filter = "",

NotifyFilter = NotifyFilters.CreationTime |

NotifyFilters.DirectoryName |

NotifyFilters.FileName |

NotifyFilters.LastWrite

};

public static ReadOnlyObservableCollection<AssetInfo> Assets { get; } = new ReadOnlyObservableCollection<AssetInfo>(_assets);

private static void RegisterAllAssets(string path)

{

Debug.Assert(Directory.Exists(path));

foreach (var entry in Directory.GetFileSystemEntries(path))

{

if (ContentHelper.IsDirectory(entry))

{

RegisterAllAssets(entry);

}

else

{

RegisterAsset(entry);

}

}

}

private static void RegisterAsset(string file)

{

Debug.Assert(File.Exists(file));

try

{

var fileInfo = new FileInfo(file);

if (!_assetDictionary.ContainsKey(file) ||

_assetDictionary[file].RegisterTime.IsOlder(fileInfo.LastWriteTime))

{

var info = Asset.GetAssetInfo(file);

Debug.Assert(info != null);

info.RegisterTime = DateTime.Now;

_assetDictionary[file] = info;

Debug.Assert(_assetDictionary.ContainsKey(file));

_assets.Add(_assetDictionary[file]);

}

}catch (Exception ex)

{

Debug.WriteLine(ex.Message);

}

}

private static void UnRegisterAsset(string file)

{

if (_assetDictionary.ContainsKey(file))

{

_assets.Remove(_assetDictionary[file]);

_assetDictionary.Remove(file);

}

}

private static async void OnContentModified(object sender, FileSystemEventArgs e)

{

if (Path.GetExtension(e.FullPath) != Asset.AssetFileExtension) return;

await Application.Current.Dispatcher.BeginInvoke(new Action(() => {

_refreshTimer.Trigger();

}));

}

private static void Refresh(object? sender, DelayEventTimerArgs e)

{

foreach (var item in e.Data)

{

if (!(item is FileSystemEventArgs eventArgs)) continue;

if (eventArgs.ChangeType == WatcherChangeTypes.Deleted)

{

UnRegisterAsset(eventArgs.FullPath);

}

else

{

RegisterAsset(eventArgs.FullPath);

if (eventArgs.ChangeType == WatcherChangeTypes.Renamed)

{

_assetDictionary.Keys.Where(key=>!File.Exists(key)).ToList().ForEach(file => UnRegisterAsset(file));

}

}

}

}

public static void Clear()

{

_contentWatcher.EnableRaisingEvents = false;

_assetDictionary.Clear();

_assets.Clear();

}

public static void Reset(string contentFolder)

{

Clear();

Debug.Assert(Directory.Exists(contentFolder));

RegisterAllAssets(contentFolder);

_contentWatcher.Path = contentFolder;

_contentWatcher.EnableRaisingEvents = true;

}

public static AssetInfo GetAssetInfo(string file) => _assetDictionary.ContainsKey(file) ? _assetDictionary[file] : null;

public static AssetInfo GetAssetinfo(Guid guid) => _assets.FirstOrDefault(x => x.Guid == guid);

static AssetRegistry()

{

_contentWatcher.Changed += OnContentModified;

_contentWatcher.Created += OnContentModified;

_contentWatcher.Deleted += OnContentModified;

_contentWatcher.Renamed += OnContentModified;

_refreshTimer.Triggered += Refresh;

}

}

}

相关推荐
code bean30 分钟前
【WPF】WPF 自定义控件之依赖属性
wpf
上元星如雨10 小时前
WPF 加载和显示 GIF 图片的完整指南
wpf
erxij15 小时前
【游戏引擎之路】登神长阶(十八):3天制作Galgame引擎《Galplayer》——无敌之道心
游戏引擎
lizz3117 小时前
GAMES101 lec2-数学基础1(线性代数)
线性代数·游戏引擎·图形渲染
微小冷21 小时前
WPF中ListView控件详解
c#·wpf·数据绑定·listview·bingding
X-mj1 天前
Unity URP + XR 自定义 Skybox 在真机变黑问题全解析与解决方案(支持 Pico、Quest 等一体机)
unity·游戏引擎·xr
erxij1 天前
【游戏引擎之路】登神长阶(十七):Humanoid动画——长风破浪会有时,直挂云帆济沧海
游戏引擎
erxij1 天前
【游戏引擎之路】登神长阶(十九):3D物理引擎——岁不寒,无以知松柏;事不难,无以知君子
3d·游戏引擎
心疼你的一切2 天前
Unity 多人游戏框架学习系列一
学习·游戏·unity·c#·游戏引擎
示申○言舌2 天前
Unity沉浸式/360View/全景渲染
unity·游戏引擎·沉浸式·360view·全景视图·全景渲染