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;

}

}

}

相关推荐
步、步、为营7 小时前
.NET 9 RC1 正式发布
.net·wpf
21 小时前
3D碰撞检测系统 基于SAT算法+Burst优化(Unity)
算法·3d·unity·c#·游戏引擎·sat
dzj20211 天前
Unity是如何把3D场景显示到屏幕上的——Unity的渲染过程
3d·unity·游戏引擎·渲染·图形学
界面开发小八哥2 天前
界面组件DevExpress WPF中文教程:Grid - 如何过滤节点?
.net·wpf·界面控件·devexpress·ui开发
I'mSQL2 天前
C#与WPF使用mvvm简单案例点击按钮触发弹窗
开发语言·c#·wpf
不绝1913 天前
ARPG开发流程第一章——方法合集
算法·游戏·unity·游戏引擎
贵州晓智信息科技3 天前
Unity 性能优化全攻略
unity·性能优化·游戏引擎
百锦再3 天前
WPF依赖属性深度解析:从原理到高级应用
wpf·依赖·绑定·验证·net·强制
unicrom_深圳市由你创科技3 天前
Unity 的UI动画调节
ui·unity·游戏引擎
✎ ﹏梦醒͜ღ҉繁华落℘3 天前
WPF高级学习(一)
学习·wpf