RaspberryIO_26/Unosquare.WiringPi/Resources/EmbeddedResources.cs

56 lines
1.8 KiB
C#
Raw Normal View History

2019-12-06 21:09:52 +01:00
using System;
using System.Collections.ObjectModel;
using System.IO;
using System.Reflection;
using Unosquare.WiringPi.Native;
namespace Unosquare.WiringPi.Resources {
/// <summary>
/// Provides access to embedded assembly files.
/// </summary>
internal static class EmbeddedResources {
2019-12-04 18:57:18 +01:00
/// <summary>
2019-12-06 21:09:52 +01:00
/// Initializes static members of the <see cref="EmbeddedResources"/> class.
2019-12-04 18:57:18 +01:00
/// </summary>
2019-12-06 21:09:52 +01:00
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 = 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 */
}
}
}
}
}
2019-12-04 18:57:18 +01:00
}