Skip to content

Commit f5eb141

Browse files
[Packer] Added type name baking into resource packing;
1 parent 59bb93b commit f5eb141

File tree

6 files changed

+38
-59
lines changed

6 files changed

+38
-59
lines changed

Engine/Core/MessagePackGenerated/MessagePackGenerated.cs

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2118,10 +2118,11 @@ public void Serialize(ref global::MessagePack.MessagePackWriter writer, global::
21182118
}
21192119

21202120
global::MessagePack.IFormatterResolver formatterResolver = options.Resolver;
2121-
writer.WriteArrayHeader(3);
2121+
writer.WriteArrayHeader(4);
21222122
writer.Write(value.guid);
21232123
formatterResolver.GetFormatterWithVerify<string>().Serialize(ref writer, value.path, options);
21242124
writer.Write(value.size);
2125+
formatterResolver.GetFormatterWithVerify<string>().Serialize(ref writer, value.typeName, options);
21252126
}
21262127

21272128
public global::Staple.Internal.ResourcePak.Entry Deserialize(ref global::MessagePack.MessagePackReader reader, global::MessagePack.MessagePackSerializerOptions options)
@@ -2149,6 +2150,9 @@ public void Serialize(ref global::MessagePack.MessagePackWriter writer, global::
21492150
case 2:
21502151
____result.size = reader.ReadInt64();
21512152
break;
2153+
case 3:
2154+
____result.typeName = formatterResolver.GetFormatterWithVerify<string>().Deserialize(ref reader, options);
2155+
break;
21522156
default:
21532157
reader.Skip();
21542158
break;

Engine/Core/Rendering/Animation/SkinnedMeshAnimatorSystem.cs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -65,6 +65,7 @@ private void SetState(SkinnedMeshAnimator animator, string name)
6565
animator.animation = state.animation;
6666
animator.playTime = 0;
6767
animator.currentState = state;
68+
animator.evaluator = null;
6869
}
6970

7071
public void Prepare()

Engine/Core/Resources/ResourcePak.cs

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,9 @@ public class Entry
3333
[Key(2)]
3434
public long size;
3535

36+
[Key(3)]
37+
public string typeName;
38+
3639
[IgnoreMember]
3740
public Stream stream;
3841
}
@@ -147,6 +150,7 @@ public class FileInfo
147150
public string guid;
148151
public string path;
149152
public long size;
153+
public string typeName;
150154
}
151155

152156
private List<Entry> entries = new();
@@ -162,7 +166,7 @@ public void Clear()
162166
entries.Clear();
163167
}
164168

165-
public void AddEntry(string guid, string path, Stream stream)
169+
public void AddEntry(string guid, string path, string typeName, Stream stream)
166170
{
167171
if(Guid.TryParse(guid, out var _guid) == false)
168172
{
@@ -174,6 +178,7 @@ public void AddEntry(string guid, string path, Stream stream)
174178
guid = _guid.ToByteArray(),
175179
path = path,
176180
size = stream.Length,
181+
typeName = typeName,
177182
stream = stream,
178183
};
179184

@@ -327,9 +332,10 @@ internal bool Deserialize(Stream reader)
327332
guid = new Guid(entry.guid).ToString(),
328333
path = entry.path,
329334
size = entry.size,
335+
typeName = entry.typeName,
330336
});
331337

332-
reader.Position = reader.Position + entry.size;
338+
reader.Position += entry.size;
333339
}
334340
}
335341
catch (Exception e)

Engine/Core/Serialization/Asset/AssetDatabase.cs

Lines changed: 1 addition & 52 deletions
Original file line numberDiff line numberDiff line change
@@ -78,60 +78,9 @@ public static void Reload()
7878
guid = file.guid,
7979
path = file.path,
8080
name = Path.GetFileNameWithoutExtension(file.path.Replace(".meta", "")),
81+
typeName = file.typeName,
8182
};
8283

83-
var extension = Path.GetExtension(file.path);
84-
85-
//TODO: Do this better
86-
switch (extension)
87-
{
88-
case ".stsh":
89-
90-
asset.typeName = typeof(Shader).FullName;
91-
92-
break;
93-
94-
case ".mat":
95-
96-
asset.typeName = typeof(Material).FullName;
97-
98-
break;
99-
100-
case ".stsc":
101-
102-
asset.typeName = typeof(Scene).FullName;
103-
104-
break;
105-
106-
case ".stpr":
107-
108-
asset.typeName = typeof(Prefab).FullName;
109-
110-
break;
111-
112-
default:
113-
114-
if(extension != null && extension.Length > 0)
115-
{
116-
var shortExtension = extension.Substring(1);
117-
118-
if (AssetSerialization.TextureExtensions.Contains(shortExtension))
119-
{
120-
asset.typeName = typeof(Texture).FullName;
121-
}
122-
else if (AssetSerialization.AudioExtensions.Contains(shortExtension))
123-
{
124-
asset.typeName = typeof(AudioClip).FullName;
125-
}
126-
else if (AssetSerialization.MeshExtensions.Contains(shortExtension))
127-
{
128-
asset.typeName = typeof(Mesh).FullName;
129-
}
130-
}
131-
132-
break;
133-
}
134-
13584
assets.Add(asset);
13685
}
13786
}

Tools/Packer/Packer.cs

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -156,6 +156,7 @@ public static void Main(string[] args)
156156
var filePaths = new List<string>();
157157
var localFilePaths = new List<string>();
158158
var fileGuids = new List<string>();
159+
var fileTypes = new List<string>();
159160
var fileStreams = new List<Stream>();
160161

161162
void Recursive(string basePath, string current)
@@ -176,7 +177,7 @@ void Recursive(string basePath, string current)
176177
}
177178

178179
var localPath = Path.GetRelativePath(basePath, file).Replace(Path.DirectorySeparatorChar, '/');
179-
var guid = PackerUtils.ExtractGuid(file) ?? Guid.NewGuid().ToString();
180+
var guid = PackerUtils.ExtractGuid(file, out var typeName) ?? Guid.NewGuid().ToString();
180181

181182
if(fileGuids.Any(x => x == guid))
182183
{
@@ -185,6 +186,7 @@ void Recursive(string basePath, string current)
185186

186187
filePaths.Add(file);
187188
localFilePaths.Add(localPath);
189+
fileTypes.Add(typeName);
188190
fileStreams.Add(new FileStream(file, FileMode.Open));
189191
fileGuids.Add(guid);
190192
}
@@ -215,7 +217,7 @@ void Recursive(string basePath, string current)
215217

216218
for(var i = 0; i < filePaths.Count; i++)
217219
{
218-
resourcePak.AddEntry(fileGuids[i], localFilePaths[i], fileStreams[i]);
220+
resourcePak.AddEntry(fileGuids[i], localFilePaths[i], fileTypes[i], fileStreams[i]);
219221
}
220222

221223
try

Tools/Packer/PackerUtils.cs

Lines changed: 19 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
using MessagePack;
2+
using Staple;
23
using Staple.Internal;
34
using System;
45
using System.IO;
@@ -8,10 +9,14 @@ namespace Packer;
89

910
static class PackerUtils
1011
{
11-
public static string ExtractGuid(string path)
12+
public static string ExtractGuid(string path, out string typeName)
1213
{
13-
if(path.EndsWith(".mat"))
14+
typeName = "Unknown";
15+
16+
if (path.EndsWith(".mat"))
1417
{
18+
typeName = typeof(Material).FullName;
19+
1520
try
1621
{
1722
var data = File.ReadAllBytes(path);
@@ -39,6 +44,8 @@ public static string ExtractGuid(string path)
3944
}
4045
else if(path.EndsWith(".stsc"))
4146
{
47+
typeName = typeof(Scene).FullName;
48+
4249
try
4350
{
4451
var data = File.ReadAllBytes(path);
@@ -66,6 +73,8 @@ public static string ExtractGuid(string path)
6673
}
6774
else if(path.EndsWith(".stsh"))
6875
{
76+
typeName = typeof(Shader).FullName;
77+
6978
try
7079
{
7180
var data = File.ReadAllBytes(path);
@@ -109,6 +118,8 @@ public static string ExtractGuid(string path)
109118

110119
var value = MessagePackSerializer.Deserialize<SerializableStapleAsset>(stream);
111120

121+
typeName = value.typeName;
122+
112123
return value.guid;
113124
}
114125
catch (Exception e)
@@ -120,6 +131,8 @@ public static string ExtractGuid(string path)
120131
}
121132
else if(AssetSerialization.TextureExtensions.Any(x => path.EndsWith($".{x}")))
122133
{
134+
typeName = typeof(Texture).FullName;
135+
123136
try
124137
{
125138
var data = File.ReadAllBytes(path);
@@ -147,6 +160,8 @@ public static string ExtractGuid(string path)
147160
}
148161
else if (AssetSerialization.AudioExtensions.Any(x => path.EndsWith($".{x}")))
149162
{
163+
typeName = typeof(AudioClip).FullName;
164+
150165
try
151166
{
152167
var data = File.ReadAllBytes(path);
@@ -174,6 +189,8 @@ public static string ExtractGuid(string path)
174189
}
175190
else if (AssetSerialization.MeshExtensions.Any(x => path.EndsWith($".{x}")))
176191
{
192+
typeName = typeof(Mesh).FullName;
193+
177194
try
178195
{
179196
var data = File.ReadAllBytes(path);

0 commit comments

Comments
 (0)