RaspberryIO/Unosquare.RaspberryIO/Resources/EmbeddedResources.cs
2019-12-03 18:43:54 +01:00

55 lines
1.8 KiB
C#

using Unosquare.RaspberryIO.Native;
using Unosquare.Swan;
using System;
using System.Collections.ObjectModel;
using System.IO;
namespace Unosquare.RaspberryIO.Resources {
/// <summary>
/// Provides access to embedded assembly files
/// </summary>
internal static class EmbeddedResources {
/// <summary>
/// Initializes static members of the <see cref="EmbeddedResources"/> class.
/// </summary>
static EmbeddedResources() => ResourceNames = new ReadOnlyCollection<String>(typeof(EmbeddedResources).Assembly().GetManifestResourceNames());
/// <summary>
/// Gets the resource names.
/// </summary>
/// <value>
/// The resource names.
/// </value>
public static ReadOnlyCollection<String> ResourceNames {
get;
}
/// <summary>
/// Extracts all the file resources to the specified base path.
/// </summary>
public static void ExtractAll() {
String basePath = Runtime.EntryAssemblyDirectory;
Int32 executablePermissions = Standard.StringToInteger("0777", IntPtr.Zero, 8);
foreach(String resourceName in ResourceNames) {
String filename = resourceName.Substring($"{typeof(EmbeddedResources).Namespace}.".Length);
String targetPath = Path.Combine(basePath, filename);
if(File.Exists(targetPath)) {
return;
}
using(Stream stream = typeof(EmbeddedResources).Assembly().GetManifestResourceStream($"{typeof(EmbeddedResources).Namespace}.{filename}")) {
using(FileStream outputStream = File.OpenWrite(targetPath)) {
stream?.CopyTo(outputStream);
}
try {
_ = Standard.Chmod(targetPath, (UInt32)executablePermissions);
} catch {
/* Ignore */
}
}
}
}
}
}