using System.Reflection;
namespace VPet.House.Resources;
///
/// 本地资源
///
internal class NativeResources
{
#region Resources
public const string Wall = ResourcePath + "Wall.png";
public const string Floor = ResourcePath + "Floor.png";
public const string Chair = ResourcePath + "Chair.png";
public const string Table = ResourcePath + "Table.png";
public const string Bed = ResourcePath + "Bed.png";
public const string OakPlanks = ResourcePath + "oak_planks.png";
public const string Stone = ResourcePath + "stone.png";
#endregion Resources
///
/// 资源基路径
///
public const string ResourcePath = $"{nameof(VPet)}.{nameof(House)}.{nameof(Resources)}.";
#region Native
private static readonly Assembly _assembly = Assembly.GetExecutingAssembly();
///
/// 获取资源流
///
/// 资源名
/// 资源流
public static Stream GetStream(string resourceName) =>
_assembly.GetManifestResourceStream(resourceName)!;
///
/// 尝试获取资源流
///
/// 资源名
/// 资源流
/// 成功为 失败为
public static bool TryGetStream(string resourceName, out Stream resourceStream)
{
resourceStream = null;
if (_assembly.GetManifestResourceStream(resourceName) is not Stream stream)
return false;
resourceStream = stream;
return true;
}
///
/// 将流保存至文件
///
/// 资源名
/// 文件路径
/// 成功为 失败为
public static bool SaveTo(string resourceName, string path)
{
if (_assembly.GetManifestResourceStream(resourceName) is not Stream stream)
return false;
using var sr = new StreamReader(stream);
using var sw = new StreamWriter(path);
sr.BaseStream.CopyTo(sw.BaseStream);
return true;
}
#endregion Native
}