wpf游戏引擎content/Asset.cs

1.Asset.cs

using System;

using System.Collections.Generic;

using System.Diagnostics;

using System.IO;

using System.Linq;

using System.Text;

using System.Threading.Tasks;

namespace PrimalEditor.Content

{

enum AssetType

{

Unknown,

Animation,

Audio,

Material,

Mesh,

Sleleton,

Texture

}

sealed class AssetInfo {

public AssetType Type { get; set; }

public byte[] Icon { get; set; }

public string FullPath { get; set; }

public string FileName => Path.GetFileNameWithoutExtension(FullPath);

public string SourcePath { get; set; }

public DateTime RegisterTime { get; set; }

public DateTime ImportDate { get; set; }

public Guid Guid { get; set; }

public byte[] Hash { get; set; }

//public byte[] Hash { get; protected set; }

// public abstract IEnumerable<string> Save(string file);

}

abstract class Asset : ViewModelBase

{

public static string AssetFileExtension => ".asset";

public AssetType Type { get; private set; }

public byte[] Icon { get; protected set; }

public string SourcePath { get; protected set; }

private string _fullPath;

public string FullPath

{

get => _fullPath;

set

{

if (_fullPath != value)

{

_fullPath = value;

OnPropertyChanged(nameof(FullPath));

OnPropertyChanged(nameof(FileName));

}

}

}

public string FileName => Path.GetFileNameWithoutExtension(FullPath);

public Guid Guid { get; protected set; }

public DateTime ImportDate { get; protected set; }

public byte[] Hash { get; protected set; }

public abstract IEnumerable<string> Save(string file);

private static AssetInfo GetAssetInfo(BinaryReader reader)

{

reader.BaseStream.Position = 0;

var info = new AssetInfo();

info.Type = (AssetType)reader.ReadInt32();

var idSize = reader.ReadInt32();

info.Guid = new Guid(reader.ReadBytes(idSize));

info.ImportDate = DateTime.FromBinary(reader.ReadInt64());

var hashSize = reader.ReadInt32();

if (hashSize > 0)

{

info.Hash = reader.ReadBytes(hashSize);

}

info.SourcePath = reader.ReadString();

var iconSize = reader.ReadInt32();

info.Icon = reader.ReadBytes(iconSize);

return info;

}

public static AssetInfo GetAssetInfo(string file)

{

Debug.Assert(File.Exists(file) && Path.GetExtension(file) == AssetFileExtension);

try

{

using var reader = new BinaryReader(File.Open(file,FileMode.Open,FileAccess.Read));

var info = GetAssetInfo(reader);

info.FullPath = file;

return info;

} catch (Exception ex)

{

Debug.WriteLine(ex.Message);

}

return null;

}

protected void WriteAssetFileHeader(BinaryWriter writer)

{

var id = Guid.ToByteArray();

var importDate = DateTime.Now.ToBinary();

writer.Write((int)Type);

writer.Write(id.Length);

writer.Write(id);

writer.Write(importDate);

//asset hash is optional

if (Hash?.Length > 0)

{

writer.Write(Hash.Length);

writer.Write(Hash);

}

else

{

writer.Write(0);

}

writer.Write(SourcePath ?? "");

writer.Write(Icon.Length);

writer.Write(Icon);

}

protected void ReadAssetFileHeader(BinaryReader reader)

{

var info = GetAssetInfo(reader);

Debug.Assert(Type == info.Type);

Guid = info.Guid;

ImportDate = info.ImportDate;

Hash = info.Hash;

SourcePath = info.SourcePath;

Icon = info.Icon;

}

public Asset(AssetType type)

{

Debug.Assert(type != AssetType.Unknown);

Type = type;

}

}

}

相关推荐
SunflowerCoder2 小时前
WPF迁移avalonia之触发器
c#·wpf·avalonia
主宰者2 小时前
【WPF+Prism】日常开发问题总结
wpf
九章云极AladdinEdu4 小时前
绿色算力技术栈:AI集群功耗建模与动态调频系统
人工智能·pytorch·深度学习·unity·游戏引擎·transformer·gpu算力
~空中楼阁8 小时前
WPF WriteableBitmap 高性能双缓冲图片显示方案
wpf
伽蓝_游戏9 小时前
UGUI源码剖析(15):Slider的运行时逻辑与编辑器实现
游戏·ui·unity·性能优化·c#·游戏引擎·.net
m0_4972141520 小时前
unity中通过拖拽,自定义scroll view中子物体顺序
unity·游戏引擎
I'mSQL1 天前
WPF资源字典合并报错
wpf
one9961 天前
WPF应用程序中的异常处理
c#·.net·wpf
郝学胜-神的一滴2 天前
基于OpenGL封装摄像机类:视图矩阵与透视矩阵的实现
c++·qt·线性代数·矩阵·游戏引擎·图形渲染
EQ-雪梨蛋花汤2 天前
【Unity笔记】Unity 编辑器扩展:打造一个可切换 Config.assets 的顶部菜单插件
unity·编辑器·游戏引擎