diff --git a/ArcFormats/EmbeddedResource.cs b/ArcFormats/EmbeddedResource.cs new file mode 100644 index 00000000..a26ed958 --- /dev/null +++ b/ArcFormats/EmbeddedResource.cs @@ -0,0 +1,58 @@ +//! \file EmbeddedResource.cs +//! \date 2018 Apr 10 +//! \brief Embedded resource loader. +// +// Copyright (C) 2018 by morkt +// +// Permission is hereby granted, free of charge, to any person obtaining a copy +// of this software and associated documentation files (the "Software"), to +// deal in the Software without restriction, including without limitation the +// rights to use, copy, modify, merge, publish, distribute, sublicense, and/or +// sell copies of the Software, and to permit persons to whom the Software is +// furnished to do so, subject to the following conditions: +// +// The above copyright notice and this permission notice shall be included in +// all copies or substantial portions of the Software. +// +// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR +// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, +// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE +// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER +// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING +// FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS +// IN THE SOFTWARE. +// + +using System; +using System.IO; + +namespace GameRes.Formats +{ + public static class EmbeddedResource + { + /// + /// Open embedded resource as a stream. + /// + public static Stream Open (string name, Type owner) + { + var assembly = owner.Assembly; + string qualified_name = owner.Namespace + '.' + name; + return assembly.GetManifestResourceStream (qualified_name); + } + + /// + /// Load binary embedded resource as a byte array. + /// + public static byte[] Load (string name, Type owner) + { + using (var stream = Open (name, owner)) + { + if (null == stream) + return null; + var res = new byte[stream.Length]; + stream.Read (res, 0, res.Length); + return res; + } + } + } +} diff --git a/ArcFormats/ScrPlayer/ImageI.cs b/ArcFormats/ScrPlayer/ImageI.cs index 1e22c696..16979c98 100644 --- a/ArcFormats/ScrPlayer/ImageI.cs +++ b/ArcFormats/ScrPlayer/ImageI.cs @@ -262,16 +262,7 @@ namespace GameRes.Formats.ScrPlayer static byte[] LoadResource (string name) { - var type = typeof(Img2Reader); - var assembly = type.Assembly; - using (var stream = assembly.GetManifestResourceStream (type.Namespace+'.'+name)) - { - if (null == stream) - return null; - var data = new byte[stream.Length]; - stream.Read (data, 0, data.Length); - return data; - } + return EmbeddedResource.Load (name, typeof(Img2Reader)); } #region IDisposable Members diff --git a/ArcFormats/Softpal/ArcVAFS.cs b/ArcFormats/Softpal/ArcVAFS.cs index 49306995..cb09aa67 100644 --- a/ArcFormats/Softpal/ArcVAFS.cs +++ b/ArcFormats/Softpal/ArcVAFS.cs @@ -368,18 +368,12 @@ namespace GameRes.Formats.Softpal static short[] LoadWaveTable (string name) { - - var assembly = typeof(VafsOpener).Assembly; - using (var stream = assembly.GetManifestResourceStream ("GameRes.Formats.Softpal."+name)) - { - if (null == stream) - return null; - var src = new byte[stream.Length]; - stream.Read (src, 0, src.Length); - var array = new short[src.Length/2]; - Buffer.BlockCopy (src, 0, array, 0, src.Length); - return array; - } + var src = EmbeddedResource.Load (name, typeof(VafsOpener)); + if (null == src) + return null; + var array = new short[src.Length/2]; + Buffer.BlockCopy (src, 0, array, 0, src.Length); + return array; } } diff --git a/ArcFormats/Unity/Asset.cs b/ArcFormats/Unity/Asset.cs index 4996ace0..7ea3bdcb 100644 --- a/ArcFormats/Unity/Asset.cs +++ b/ArcFormats/Unity/Asset.cs @@ -305,25 +305,12 @@ namespace GameRes.Formats.Unity return Binary.GetCString (strings, offset, strings.Length-offset, Encoding.UTF8); } - internal static Stream OpenResource (string name) - { - var qualified_name = ".Unity." + name; - var assembly = Assembly.GetExecutingAssembly(); - var res_name = assembly.GetManifestResourceNames().Single (r => r.EndsWith (qualified_name)); - Stream stream = assembly.GetManifestResourceStream (res_name); - if (null == stream) - throw new FileNotFoundException ("Resource not found.", name); - return stream; - } - internal static byte[] LoadResource (string name) { - using (var stream = OpenResource (name)) - { - var res = new byte[stream.Length]; - stream.Read (res, 0, res.Length); - return res; - } + var res = EmbeddedResource.Load (name, typeof(TypeTree)); + if (null == res) + throw new FileNotFoundException ("Resource not found.", name); + return res; } }