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;

}

}

}

相关推荐
淡海水3 分钟前
【节点】[RandomRange节点]原理解析与实际应用
unity·游戏引擎·shadergraph·图形·randomrange
bugcome_com15 小时前
WPF样式进阶实战:外置样式+MVVM主题切换+样式优先级全解析
c#·.net·wpf
weixin_4242946715 小时前
在Unity中,摄像机移动时出现“残影”或“闪烁”是常见问题,主要原因和处理方法。
unity·游戏引擎
孟无岐16 小时前
【Laya】Browser 使用说明
typescript·游戏引擎·游戏程序·laya
天人合一peng16 小时前
unity 3d 通过游戏对象的名子查到其对象
游戏·unity·游戏引擎
__water21 小时前
RHK《Unity接入DeepSeek问答》
unity·游戏引擎·智能问答·deepseek接入·deepseekapikey
lalala_Zou1 天前
场景题:电商平台订单未支付过期如何实现自动关闭订单?
wpf
czhc11400756631 天前
wpf 16
wpf
在路上看风景1 天前
1.4 Unity运行时路径
unity·游戏引擎
郝学胜-神的一滴1 天前
Qt OpenGL 生成Mipmap技术详解
开发语言·c++·qt·系统架构·游戏引擎·图形渲染·unreal engine