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;

}

}

}

相关推荐
无心水2 小时前
分布式定时任务与SELECT FOR UPDATE:从致命陷阱到优雅解决方案(实战案例+架构演进)
服务器·人工智能·分布式·后端·spring·架构·wpf
LZL_SQ4 小时前
HCCL测试框架中AllReduce边界条件测试设计深度剖析
wpf·cann
小李也疯狂15 小时前
Unity 中的立方体贴图(Cubemaps)
unity·游戏引擎·贴图·cubemap
呆呆敲代码的小Y15 小时前
【Unity工具篇】| 超实用工具LuBan,快速上手使用
游戏·unity·游戏引擎·unity插件·luban·免费游戏·游戏配置表
EQ-雪梨蛋花汤15 小时前
【Unity优化】Unity多场景加载优化与资源释放完整指南:解决Additive加载卡顿、预热、卸载与内存释放问题
unity·游戏引擎
我的offer在哪里16 小时前
用 Unity 从 0 做一个「可以玩的」游戏,需要哪些步骤和流程
游戏·unity·游戏引擎
泡泡茶壶ᐇ16 小时前
Unity游戏开发入门指南:从零开始理解游戏引擎核心概念
unity·游戏引擎
Var_al18 小时前
抖小Unity WebGL分包命令行工具实践指南
unity·游戏引擎·webgl
天人合一peng20 小时前
unity 通过代码修改button及其名字字体的属性
unity·游戏引擎
GLDbalala1 天前
Unity基于自定义管线实现经典经验光照模型
unity·游戏引擎