// Copyright (c) Nate McMaster.
// Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information.
using System;
using System.Diagnostics;
using System.IO;
namespace BTCPayServer.Plugins.Dotnet.LibraryModel
{
///
/// Represents an unmanaged library, such as `libsqlite3`, which may need to be loaded
/// for P/Invoke to work.
///
[DebuggerDisplay("{Name} = {AdditionalProbingPath}")]
public class NativeLibrary
{
private NativeLibrary(string name, string appLocalPath, string additionalProbingPath)
{
Name = name ?? throw new ArgumentNullException(nameof(name));
AppLocalPath = appLocalPath ?? throw new ArgumentNullException(nameof(appLocalPath));
AdditionalProbingPath = additionalProbingPath ?? throw new ArgumentNullException(nameof(additionalProbingPath));
}
///
/// Name of the native library. This should match the name of the P/Invoke call.
///
/// For example, if specifying `[DllImport("sqlite3")]`, should be sqlite3.
/// This may not match the exact file name as loading will attempt variations on the name according
/// to OS convention. On Windows, P/Invoke will attempt to load `sqlite3.dll`. On macOS, it will
/// attempt to find `sqlite3.dylib` and `libsqlite3.dylib`. On Linux, it will attempt to find
/// `sqlite3.so` and `libsqlite3.so`.
///
///
public string Name { get; private set; }
///
/// Contains path to file within a deployed, framework-dependent application
///
/// For example, runtimes/linux-x64/native/libsqlite.so
///
///
public string AppLocalPath { get; private set; }
///
/// Contains path to file within an additional probing path root. This is typically a combination
/// of the NuGet package ID (lowercased), version, and path within the package.
///
/// For example, sqlite/3.13.3/runtimes/linux-x64/native/libsqlite.so
///
///
public string AdditionalProbingPath { get; private set; }
///
/// Create an instance of from a NuGet package.
///
/// The name of the package.
/// The version of the package.
/// The path within the NuGet package.
///
public static NativeLibrary CreateFromPackage(string packageId, string packageVersion, string assetPath)
{
return new NativeLibrary(
Path.GetFileNameWithoutExtension(assetPath),
assetPath,
Path.Combine(packageId.ToLowerInvariant(), packageVersion, assetPath)
);
}
}
}