using System; using System.Collections.ObjectModel; using System.IO; using System.Reflection; using Unosquare.WiringPi.Native; namespace Unosquare.WiringPi.Resources { /// /// Provides access to embedded assembly files. /// internal static class EmbeddedResources { /// /// Initializes static members of the class. /// static EmbeddedResources() => ResourceNames = new ReadOnlyCollection(typeof(EmbeddedResources).Assembly.GetManifestResourceNames()); /// /// Gets the resource names. /// /// /// The resource names. /// public static ReadOnlyCollection ResourceNames { get; } /// /// Extracts all the file resources to the specified base path. /// public static void ExtractAll() { String basePath = Path.GetDirectoryName(Assembly.GetEntryAssembly().Location); Int32 executablePermissions = SysCall.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(resourceName)) { using(FileStream outputStream = File.OpenWrite(targetPath)) { stream?.CopyTo(outputStream); } try { _ = SysCall.Chmod(targetPath, (UInt32)executablePermissions); } catch { /* Ignore */ } } } } } }