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;

}

}

}

相关推荐
危险库几秒前
【UE4/UE5】在虚幻引擎中创建控制台指令的几种方法
c++·ue5·游戏引擎·ue4·虚幻
大Mod_abfun5 小时前
Unity游戏基础-4(人物移动、相机移动、UI事件处理 代码详解)
游戏·ui·unity·游戏引擎
大Mod_abfun8 小时前
Unity游戏基础-3(UI层)
游戏·ui·unity·游戏引擎
大有数据可视化16 小时前
告别传统监控:基于Unity+IoT打造沉浸式数字孪生车间
物联网·unity·游戏引擎
雪下的新火18 小时前
爆炸特效:Unity+Blender-02-火焰
unity·游戏引擎·blender·特效制作·笔记分享
雪下的新火18 小时前
Unity+Blender-03-输出制作Flipbook
游戏·unity·游戏引擎·blender·资源·笔记分享
大Mod_abfun21 小时前
Unity游戏基础-1(安装~工作区构建)
游戏·unity·游戏引擎·大学课程·数媒
qq_428639611 天前
虚幻基础:角色攻击
游戏引擎·虚幻
希望PZM1 天前
Unity实现UV的中心缩放
unity·游戏引擎·uv
FuckPatience1 天前
WPF 具有跨线程功能的UI元素
wpf