-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathAssetDataUtil.cs
202 lines (190 loc) · 7.6 KB
/
AssetDataUtil.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
using AssetsTools.NET;
using AssetsTools.NET.Extra;
using AssetStudio;
using AssetStudioExporter.AssetTypes;
using AssetStudioExporter.Internal;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace AssetStudio;
public static class AssetDataUtil
{
/// <summary>
/// 根据资源路径、大小和偏移量读取AssetBundle中的数据
/// </summary>
/// <param name="inst">AssetsFileInstance</param>
/// <param name="path">资源路径</param>
/// <param name="size">大小</param>
/// <param name="offset">偏移量</param>
/// <returns>资源的原始二进制数据</returns>
/// <exception cref="FileNotFoundException">找不到对应的资源</exception>
public static byte[] GetAssetData(this AssetsFileInstance inst, string path, uint size, long offset = 0)
{
byte[] data = Array.Empty<byte>();
if (path != null && inst.parentBundle != null)
{
string text = path;
if (path.StartsWith("archive:/"))
{
text = text[9..];
}
text = Path.GetFileName(text);
AssetBundleFile file = inst.parentBundle.file;
AssetsFileReader dataReader = file.DataReader;
var directoryInfos = file.BlockAndDirInfo.DirectoryInfos;
bool foundFile = false;
foreach (AssetBundleDirectoryInfo assetBundleDirectoryInfo in directoryInfos)
{
if (assetBundleDirectoryInfo.Name == text)
{
dataReader.Position = assetBundleDirectoryInfo.Offset + offset;
data = dataReader.ReadBytes((int)size);
foundFile = true;
break;
}
}
if (!foundFile)
{
throw new FileNotFoundException("resS was detected but no file was found in bundle");
}
}
return data;
}
public static UnityVersion GetVersion(this AssetsFileInstance inst)
{
//return VersionCache.GetVersion(inst);
return new UnityVersion(inst.file.Metadata.UnityVersion);
}
/// <summary>
/// 获取AssetBundle或ResourceManager的Container
/// </summary>
/// <param name="inst">AssetsFileInstance</param>
/// <returns>Container字典</returns>
public static Dictionary<string, AssetPPtr> GetContainers(this AssetsFileInstance inst, AssetsManager am)
{
var isAB = true;
var ab = inst.file.AssetInfos.FirstOrDefault(f => f.TypeId == (uint)AssetClassID.AssetBundle);
if (ab is null)
{
ab = inst.file.AssetInfos.FirstOrDefault(f => f.TypeId == (uint)AssetClassID.ResourceManager);
isAB = false;
if (ab is null)
{
return new Dictionary<string, AssetPPtr>();
}
}
var bf = am.GetBaseField(inst, ab);
var version = VersionCache.GetVersion(inst);
if (isAB)
{
var assetBundle = AssetBundle.Read(bf, version);
return assetBundle.m_Container
.Select(p => new KeyValuePair<string, AssetPPtr>(p.Key, p.Value.asset))
.ToDictionary(p => p.Key, p => p.Value);
} else
{
var resourceManager = ResourceManager.Read(bf, version);
return resourceManager.m_Container
.Select(p => new KeyValuePair<string, AssetPPtr>(p.Key, p.Value))
.ToDictionary(p => p.Key, p => p.Value);
}
}
public static object? DeserializeMonoBehaviour(AssetTypeValueField monoBehaviour) =>
monoBehaviour.Value switch
{
AssetTypeValue v => v.ValueType switch
{
AssetValueType.Bool => v.AsBool,
AssetValueType.Int8 => v.AsByte,
AssetValueType.UInt8 => v.AsSByte,
AssetValueType.Int16 => v.AsShort,
AssetValueType.UInt16 => v.AsUShort,
AssetValueType.Int32 => v.AsInt,
AssetValueType.UInt32 => v.AsUInt,
AssetValueType.Int64 => v.AsLong,
AssetValueType.UInt64 => v.AsULong,
AssetValueType.Float => v.AsFloat,
AssetValueType.Double => v.AsDouble,
AssetValueType.String => v.AsString,
AssetValueType.Array => monoBehaviour.Children
.Select(DeserializeMonoBehaviour)
.ToArray(),
AssetValueType.ByteArray => v.AsByteArray,
_ => null
},
null => monoBehaviour.Children
.Aggregate(new Dictionary<string, dynamic?>(), (obj, c) =>
{
var key = c.TemplateField.Name;
var value = DeserializeMonoBehaviour(c);
obj[key] = value;
return obj;
})
};
public static AssetTypeValueField SerializeMonoBehaviour(object? obj, AssetTypeTemplateField templateField)
{
var ret = new AssetTypeValueField();
ret.TemplateField = templateField;
ret.Value = obj switch
{
null => new AssetTypeValue(AssetValueType.None, null),
bool b => new AssetTypeValue(AssetValueType.Bool, b),
sbyte int8 => new AssetTypeValue(AssetValueType.Int8, int8),
byte uint8 => new AssetTypeValue(AssetValueType.UInt8, uint8),
short int16 => new AssetTypeValue(AssetValueType.Int16, int16),
ushort uint16 => new AssetTypeValue(AssetValueType.UInt16, uint16),
int int32 => new AssetTypeValue(AssetValueType.Int32, int32),
uint uint32 => new AssetTypeValue(AssetValueType.UInt32, uint32),
long int64 => new AssetTypeValue(AssetValueType.Int64, int64),
ulong uint64 => new AssetTypeValue(AssetValueType.UInt64, uint64),
float f => new AssetTypeValue(AssetValueType.Float, f),
double d => new AssetTypeValue(AssetValueType.Double, d),
string s => new AssetTypeValue(AssetValueType.String, s),
IEnumerable<byte> barr => new AssetTypeValue(
AssetValueType.ByteArray,
barr.ToArray()),
_ => null
};
if (ret.Value is not null)
{
return ret;
}
if (obj is IDictionary<string, dynamic> dict)
{
ret.Value = null;
ret.Children = dict.Select(p =>
{
var temp = templateField.Children.First(f => f.Name == p.Key);
AssetTypeValueField child = SerializeMonoBehaviour(p.Value, temp);
return child;
}).ToList();
}
else if (obj is IList<dynamic> arr)
{
var array = new AssetTypeArrayInfo(arr.Count);
ret.Children =arr
.Select(x =>
{
var temp = templateField.Children[1];// [0]为size
return (AssetTypeValueField)SerializeMonoBehaviour(x, temp);
})
.ToList();
ret.Value = new AssetTypeValue(AssetValueType.Array, array);
}
else
{
ret.Value = null;
Type type = obj!.GetType();
ret.Children = type.GetFields().Select(f =>
{
var key = f.Name;
var temp = templateField.Children.First(f => f.Name == key);
AssetTypeValueField child = SerializeMonoBehaviour(f.GetValue(obj), temp);
return child;
}).ToList();
}
return ret;
}
}