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;

}

}

}

相关推荐
三只坚果3 小时前
blender制作动画导入unity两种方式
unity·游戏引擎·blender
YF云飞4 小时前
Unity音频管理:打造沉浸式游戏音效
游戏·unity·游戏引擎·游戏程序·个人开发
我好喜欢你~8 小时前
WPF---数据模版
wpf
与火星的孩子对话13 小时前
Unity高级开发:反射原理深入解析与实践指南 C#
java·unity·c#·游戏引擎·lucene·反射
scoone15 小时前
开源游戏引擎Bevy 和 Godot
游戏引擎·godot
霸王•吕布16 小时前
游戏引擎中的粒子系统
游戏引擎·粒子系统·粒子发射盒·粒子物理参数·粒子实例·粒子生命周期·粒子参数
小徐小徐编程不急1 天前
unity实现背包拖拽排序
unity·游戏引擎
hqwest1 天前
C#WPF实战出真汁07--【系统设置】--菜品类型设置
开发语言·c#·wpf·grid设计·stackpanel布局
萘柰奈1 天前
Unity进阶--C#补充知识点--【Unity跨平台的原理】Mono与IL2CPP
unity·c#·游戏引擎
阿赵3D1 天前
Unity引擎播放HLS自适应码率流媒体视频
unity·游戏引擎·音视频·流媒体·hls