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;

}

}

}

相关推荐
郝学胜-神的一滴2 小时前
游戏设计:原型与开发 18:Unity入门的第一课
人工智能·程序人生·游戏·ui·unity·游戏引擎
淡海水4 小时前
09-01-内存-Unity资源生命周期全景
unity·c#·游戏引擎·.netcore·gc
Solange__7 小时前
AI世界模型、3D场景生成和游戏引擎怎么选?按5类任务比较交互、物理与导出
开发语言·人工智能·游戏·平面·3d·自然语言处理·游戏引擎
avi91118 小时前
电影开源Jevdo - 源码介绍(vue版)没有android 和 没有wpf
android·wpf·框架·数据爬虫·meego·电影管理
呆呆敲代码的小Y8 小时前
【实战精选面试题】Unity精选面试题详细解答,按模块整理汇总
人工智能·游戏·unity·游戏引擎
地狱为王1 天前
Unity使用VoxLock实现语音隔离
unity·游戏引擎·音频处理·voxlock
解决小子1 天前
【虚幻引擎】【GAS】模块详解
游戏引擎·虚幻·gas
一只大鸽子1 天前
游戏开发笔记0-Godot启动
笔记·游戏引擎·godot
暴走约伯1 天前
【VFX工具链源码拆解 03】告别机械劳动(下):材质字典映射批处理与智能纹理槽识别
unity·游戏引擎·材质
吾与谁归in1 天前
C#脚本工具,支持智能提示自动补全(WPF + Roslyn + AvalonEdit)
开发语言·c#·wpf