(UnityObject): added Type property.

This commit is contained in:
morkt 2018-10-04 07:54:36 +04:00
parent f376104f47
commit 1c049dc6ca
4 changed files with 37 additions and 32 deletions

View File

@ -84,7 +84,7 @@ namespace GameRes.Formats.Unity
{ {
reader.SetupReaders (obj.Asset); reader.SetupReaders (obj.Asset);
var tex = new Texture2D(); var tex = new Texture2D();
tex.Load (reader); tex.Load (reader, obj.Asset.Tree.Version);
if (0 == tex.m_DataLength) if (0 == tex.m_DataLength)
{ {
reader.Dispose(); reader.Dispose();
@ -131,6 +131,7 @@ namespace GameRes.Formats.Unity
} }
m_disposed = true; m_disposed = true;
} }
base.Dispose (disposing);
} }
#endregion #endregion
} }

View File

@ -45,7 +45,7 @@ namespace GameRes.Formats.Unity
public UnityFSOpener () public UnityFSOpener ()
{ {
Extensions = new string[] { "", "unity3d" }; Extensions = new string[] { "", "unity3d", "asset" };
} }
public override ArcFile TryOpen (ArcView file) public override ArcFile TryOpen (ArcView file)
@ -122,7 +122,7 @@ namespace GameRes.Formats.Unity
public override IImageDecoder OpenImage (ArcFile arc, Entry entry) public override IImageDecoder OpenImage (ArcFile arc, Entry entry)
{ {
var aent = entry as AssetEntry; var aent = entry as AssetEntry;
if (null == aent || aent.AssetObject.Type != "Texture2D") if (null == aent || aent.AssetObject.TypeName != "Texture2D")
return base.OpenImage (arc, entry); return base.OpenImage (arc, entry);
var uarc = (UnityBundle)arc; var uarc = (UnityBundle)arc;
var obj = aent.AssetObject; var obj = aent.AssetObject;
@ -131,24 +131,21 @@ namespace GameRes.Formats.Unity
var reader = new AssetReader (input, entry.Name); var reader = new AssetReader (input, entry.Name);
reader.SetupReaders (obj.Asset); reader.SetupReaders (obj.Asset);
Texture2D tex = null; Texture2D tex = null;
if (obj.Asset.Tree.TypeTrees.ContainsKey (obj.TypeId)) var type = obj.Type;
if (type != null && type.Children.Any (t => t.Type == "StreamingInfo"))
{ {
var type = obj.Asset.Tree.TypeTrees[obj.TypeId]; var fields = obj.Deserialize (reader);
if (type.Children.Any (t => t.Type == "StreamingInfo")) tex = new Texture2D();
tex.Import (fields);
var info = fields["m_StreamData"] as StreamingInfo;
if (info != null)
{ {
var fields = obj.Deserialize (reader); var bundle = uarc.Bundles.FirstOrDefault (b => VFS.IsPathEqualsToFileName (info.Path, b.Name));
tex = new Texture2D(); if (bundle != null)
tex.Import (fields);
var info = fields["m_StreamData"] as StreamingInfo;
if (info != null)
{ {
var bundle = uarc.Bundles.FirstOrDefault (b => VFS.IsPathEqualsToFileName (info.Path, b.Name)); tex.m_DataLength = (int)info.Size;
if (bundle != null) input = new StreamRegion (bundles, bundle.Offset+info.Offset, info.Size);
{ reader = new AssetReader (input, entry.Name);
tex.m_DataLength = (int)info.Size;
input = new StreamRegion (bundles, bundle.Offset+info.Offset, info.Size);
reader = new AssetReader (input, entry.Name);
}
} }
} }
} }
@ -269,8 +266,6 @@ namespace GameRes.Formats.Unity
{ {
if (bundle.Name.HasAnyOfExtensions (".resource", ".resS")) if (bundle.Name.HasAnyOfExtensions (".resource", ".resS"))
continue; continue;
var res_s_name = bundle.Name + ".resS";
var res_s = m_bundles.FirstOrDefault (b => b.Name == res_s_name);
using (var asset_stream = new StreamRegion (stream, bundle.Offset, bundle.Size, true)) using (var asset_stream = new StreamRegion (stream, bundle.Offset, bundle.Size, true))
using (var reader = new AssetReader (asset_stream, bundle.Name)) using (var reader = new AssetReader (asset_stream, bundle.Name))
{ {
@ -305,6 +300,8 @@ namespace GameRes.Formats.Unity
foreach (var obj in asset.Objects) foreach (var obj in asset.Objects)
{ {
var entry = ReadAsset (file, obj); var entry = ReadAsset (file, obj);
if (null == entry)
continue;
if (null == entry.Bundle) if (null == entry.Bundle)
entry.Bundle = bundle; entry.Bundle = bundle;
string name; string name;
@ -319,7 +316,7 @@ namespace GameRes.Formats.Unity
AssetEntry ReadAsset (Stream file, UnityObject obj) AssetEntry ReadAsset (Stream file, UnityObject obj)
{ {
string type = obj.Type; string type = obj.TypeName;
if ("AudioClip" == type) if ("AudioClip" == type)
return ReadAudioClip (file, obj); return ReadAudioClip (file, obj);
else if ("TextAsset" == type) else if ("TextAsset" == type)
@ -366,8 +363,8 @@ namespace GameRes.Formats.Unity
string GetObjectName (Stream input, UnityObject obj) string GetObjectName (Stream input, UnityObject obj)
{ {
TypeTree type; var type = obj.Type;
if (obj.Asset.Tree.TypeTrees.TryGetValue (obj.TypeId, out type) && type.Children.Count > 0) if (type != null && type.Children.Count > 0)
{ {
var first_field = type.Children[0]; var first_field = type.Children[0];
if ("m_Name" == first_field.Name && "string" == first_field.Type) if ("m_Name" == first_field.Name && "string" == first_field.Type)
@ -385,8 +382,7 @@ namespace GameRes.Formats.Unity
AssetEntry ReadTextAsset (Stream input, UnityObject obj) AssetEntry ReadTextAsset (Stream input, UnityObject obj)
{ {
var type_def = obj.Asset.Tree.TypeTrees[obj.TypeId]; var script = obj.Type.Children.FirstOrDefault (f => f.Name == "m_Script");
var script = type_def.Children.FirstOrDefault (f => f.Name == "m_Script");
if (null == script) if (null == script)
return null; return null;
using (var reader = obj.Open (input)) using (var reader = obj.Open (input))

View File

@ -198,15 +198,23 @@ namespace GameRes.Formats.Unity
reader.ReadByte(); reader.ReadByte();
} }
public string Type { public string TypeName {
get { get {
var type_tree = Asset.Tree.TypeTrees; var type = this.Type;
if (type_tree.ContainsKey (TypeId)) if (type != null)
return type_tree[TypeId].Type; return type.Type;
return string.Format ("[TypeId:{0}]", TypeId); return string.Format ("[TypeId:{0}]", TypeId);
} }
} }
public TypeTree Type {
get {
TypeTree type;
Asset.Tree.TypeTrees.TryGetValue (TypeId, out type);
return type;
}
}
public override string ToString () public override string ToString ()
{ {
return string.Format ("<{0} {1}>", Type, ClassId); return string.Format ("<{0} {1}>", Type, ClassId);
@ -260,6 +268,8 @@ namespace GameRes.Formats.Unity
} }
else if ("int" == node.Type) else if ("int" == node.Type)
obj = input.ReadInt32(); obj = input.ReadInt32();
else if ("unsigned int" == node.Type)
obj = input.ReadUInt32();
else if ("bool" == node.Type) else if ("bool" == node.Type)
obj = input.ReadBool(); obj = input.ReadBool();
else else

View File

@ -61,11 +61,9 @@ namespace GameRes.Formats.Unity
case 28: // Texture2D case 28: // Texture2D
{ {
var tex = new Texture2D(); var tex = new Texture2D();
tex.Load (input); tex.Load (input, asset.Tree.Version);
if (0 == tex.m_DataLength) if (0 == tex.m_DataLength)
{ {
if (asset.Tree.Version.StartsWith ("2017.")) // "2017.2.0f3" || "2017.1.1p1"
input.ReadInt64();
var stream_data = new StreamingInfo(); var stream_data = new StreamingInfo();
stream_data.Load (input); stream_data.Load (input);
if (!string.IsNullOrEmpty (stream_data.Path)) if (!string.IsNullOrEmpty (stream_data.Path))