(EmbeddedResource): new static class.

This commit is contained in:
morkt 2018-04-10 22:16:23 +04:00
parent 742ef02a85
commit ddfc0f2bdb
4 changed files with 69 additions and 39 deletions

View File

@ -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
{
/// <summary>
/// Open embedded resource as a stream.
/// </summary>
public static Stream Open (string name, Type owner)
{
var assembly = owner.Assembly;
string qualified_name = owner.Namespace + '.' + name;
return assembly.GetManifestResourceStream (qualified_name);
}
/// <summary>
/// Load binary embedded resource as a byte array.
/// </summary>
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;
}
}
}
}

View File

@ -262,16 +262,7 @@ namespace GameRes.Formats.ScrPlayer
static byte[] LoadResource (string name) static byte[] LoadResource (string name)
{ {
var type = typeof(Img2Reader); return EmbeddedResource.Load (name, 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;
}
} }
#region IDisposable Members #region IDisposable Members

View File

@ -368,18 +368,12 @@ namespace GameRes.Formats.Softpal
static short[] LoadWaveTable (string name) static short[] LoadWaveTable (string name)
{ {
var src = EmbeddedResource.Load (name, typeof(VafsOpener));
var assembly = typeof(VafsOpener).Assembly; if (null == src)
using (var stream = assembly.GetManifestResourceStream ("GameRes.Formats.Softpal."+name)) return null;
{ var array = new short[src.Length/2];
if (null == stream) Buffer.BlockCopy (src, 0, array, 0, src.Length);
return null; return array;
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;
}
} }
} }

View File

@ -305,25 +305,12 @@ namespace GameRes.Formats.Unity
return Binary.GetCString (strings, offset, strings.Length-offset, Encoding.UTF8); 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) internal static byte[] LoadResource (string name)
{ {
using (var stream = OpenResource (name)) var res = EmbeddedResource.Load (name, typeof(TypeTree));
{ if (null == res)
var res = new byte[stream.Length]; throw new FileNotFoundException ("Resource not found.", name);
stream.Read (res, 0, res.Length); return res;
return res;
}
} }
} }