|
| 1 | +//! \file ArcSpVM.cs |
| 2 | +//! \date 2018 May 02 |
| 3 | +//! \brief Unity SpVM resources deserializer. |
| 4 | +// |
| 5 | +// Copyright (C) 2018 by morkt |
| 6 | +// |
| 7 | +// Permission is hereby granted, free of charge, to any person obtaining a copy |
| 8 | +// of this software and associated documentation files (the "Software"), to |
| 9 | +// deal in the Software without restriction, including without limitation the |
| 10 | +// rights to use, copy, modify, merge, publish, distribute, sublicense, and/or |
| 11 | +// sell copies of the Software, and to permit persons to whom the Software is |
| 12 | +// furnished to do so, subject to the following conditions: |
| 13 | +// |
| 14 | +// The above copyright notice and this permission notice shall be included in |
| 15 | +// all copies or substantial portions of the Software. |
| 16 | +// |
| 17 | +// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR |
| 18 | +// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, |
| 19 | +// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE |
| 20 | +// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER |
| 21 | +// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING |
| 22 | +// FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS |
| 23 | +// IN THE SOFTWARE. |
| 24 | +// |
| 25 | + |
| 26 | +using System; |
| 27 | +using System.Collections.Generic; |
| 28 | +using System.ComponentModel.Composition; |
| 29 | +using System.IO; |
| 30 | +using System.Linq; |
| 31 | +using System.Runtime.Serialization; |
| 32 | +using System.Runtime.Serialization.Formatters.Binary; |
| 33 | + |
| 34 | +namespace GameRes.Formats.Unity |
| 35 | +{ |
| 36 | + [Export(typeof(ArchiveFormat))] |
| 37 | + public class BytesOpener : ArchiveFormat |
| 38 | + { |
| 39 | + public override string Tag { get { return "BYTES/UNITY"; } } |
| 40 | + public override string Description { get { return "Unity engine resource archive"; } } |
| 41 | + public override uint Signature { get { return 0; } } |
| 42 | + public override bool IsHierarchic { get { return false; } } |
| 43 | + public override bool CanWrite { get { return false; } } |
| 44 | + |
| 45 | + public override ArcFile TryOpen (ArcView file) |
| 46 | + { |
| 47 | + if (!file.Name.EndsWith ("DAT.bytes", StringComparison.OrdinalIgnoreCase)) |
| 48 | + return null; |
| 49 | + var inf_name = file.Name.Substring (0, file.Name.Length - "DAT.bytes".Length); |
| 50 | + inf_name += "INF.bytes"; |
| 51 | + if (!VFS.FileExists (inf_name)) |
| 52 | + return null; |
| 53 | + using (var inf = VFS.OpenStream (inf_name)) |
| 54 | + { |
| 55 | + var bin = new BinaryFormatter { Binder = new SpTypeBinder() }; |
| 56 | + var list = bin.Deserialize (inf) as List<LinkerInfo>; |
| 57 | + if (null == list || 0 == list.Count) |
| 58 | + return null; |
| 59 | + var base_name = Path.GetFileNameWithoutExtension (file.Name); |
| 60 | + string type = ""; |
| 61 | + if (base_name.StartsWith ("WAVE", StringComparison.OrdinalIgnoreCase)) |
| 62 | + type = "audio"; |
| 63 | + else if (base_name.StartsWith ("CG", StringComparison.OrdinalIgnoreCase)) |
| 64 | + type = "image"; |
| 65 | + var dir = list.Select (e => new Entry { |
| 66 | + Name = e.name, Type = type, Offset = e.offset, Size = (uint)e.size |
| 67 | + }).ToList(); |
| 68 | + return new ArcFile (file, this, dir); |
| 69 | + } |
| 70 | + } |
| 71 | + } |
| 72 | + |
| 73 | + [Serializable] |
| 74 | + public class LinkerInfo |
| 75 | + { |
| 76 | + public string name { get; set; } |
| 77 | + public int offset { get; set; } |
| 78 | + public int size { get; set; } |
| 79 | + } |
| 80 | + |
| 81 | + internal class SpTypeBinder : SerializationBinder |
| 82 | + { |
| 83 | + public override Type BindToType (string assemblyName, string typeName) |
| 84 | + { |
| 85 | + if ("Assembly-CSharp" == assemblyName && "SpVM.Library.LinkerInfo" == typeName) |
| 86 | + { |
| 87 | + return typeof(LinkerInfo); |
| 88 | + } |
| 89 | + if (assemblyName.StartsWith ("mscorlib,") && typeName.StartsWith ("System.Collections.Generic.List`1[[SpVM.Library.LinkerInfo")) |
| 90 | + { |
| 91 | + return typeof(List<LinkerInfo>); |
| 92 | + } |
| 93 | + return null; |
| 94 | + } |
| 95 | + } |
| 96 | +} |
0 commit comments