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 小时前
宇宙版地球模拟器
游戏·unity·游戏引擎·贴图·模型
魔士于安5 小时前
氛围感游戏场景,天空盒,带地形,附赠一个空要塞
游戏·unity·游戏引擎·贴图
mxwin8 小时前
Unity Shader UI 流光效果完全推导指南
ui·unity·游戏引擎·shader·uv
林鸿群9 小时前
VS2026 编译 Cocos2d-x 项目完整指南:解决兼容性问题
游戏引擎·cocos2d
林鸿群10 小时前
VS2026 编译 Cocos2d-x 老项目完整指南:从崩溃到完美运行
游戏引擎·cocos2d
风酥糖10 小时前
Godot游戏练习01-第15节-敌人生成动画,翻转,碰撞
游戏·游戏引擎·godot
呆呆敲代码的小Y11 小时前
Unity+AI 用一句话制作完整小游戏:飞翔的牛马【AI纯添加-0手工代码】
人工智能·游戏·unity·游戏引擎·游戏制作·unityai·一句话制作游戏
一念春风12 小时前
证件照制作工具(WPF C#)
c#·wpf
mxwin1 天前
Unity Shader FLOWMAP岩浆流动制作案例
unity·游戏引擎·shader·uv
小贺儿开发1 天前
【Arduino与Unity交互探究】01 摇杆模块
科技·unity·游戏引擎·arduino·串口通信·摇杆·硬件交互