Add library
Embed the library into the executable and extract at runtime Cleanup after executing the program
This commit is contained in:
parent
132894e924
commit
c51e02fa05
1
.gitignore
vendored
1
.gitignore
vendored
@ -1,3 +1,4 @@
|
||||
.vscode/
|
||||
obj/
|
||||
bin/
|
||||
Libraries/
|
||||
|
@ -2,6 +2,7 @@
|
||||
|
||||
<ItemGroup>
|
||||
<ProjectReference Include="..\Chksum.Tests\Chksum.Tests.csproj" />
|
||||
<EmbeddedResource Include="Libraries/libe_sqlite3.so" />
|
||||
</ItemGroup>
|
||||
|
||||
<PropertyGroup>
|
||||
|
@ -17,6 +17,8 @@ public class Program {
|
||||
|
||||
utils.getBaseDir();
|
||||
|
||||
utils.ExtractEmbeddedLibrary();
|
||||
|
||||
Console.ForegroundColor = ConsoleColor.Green;
|
||||
switch (args[0]) {
|
||||
case "checksum":
|
||||
@ -64,6 +66,8 @@ public class Program {
|
||||
PrintAvailableOptions();
|
||||
break;
|
||||
}
|
||||
|
||||
utils.cleanup();
|
||||
}
|
||||
|
||||
static void PrintAvailableOptions() {
|
||||
|
@ -1,3 +1,4 @@
|
||||
using System.Reflection;
|
||||
using Microsoft.Data.Sqlite;
|
||||
namespace Chksum.Utils;
|
||||
public class ChksumUtils {
|
||||
@ -17,11 +18,30 @@ public class ChksumUtils {
|
||||
// return parentFolder;
|
||||
// }
|
||||
|
||||
public string DatabaseRoot { get; set; }
|
||||
public string DatabaseRoot { get; set; } = string.Empty;
|
||||
public void getBaseDir() {
|
||||
DatabaseRoot = AppDomain.CurrentDomain.BaseDirectory;
|
||||
}
|
||||
|
||||
public string libraryPath { get; set; } = string.Empty;
|
||||
public void ExtractEmbeddedLibrary() {
|
||||
libraryPath = Path.Combine(DatabaseRoot, "libe_sqlite3.so");
|
||||
|
||||
using (Stream? resourceStream = Assembly.GetExecutingAssembly().GetManifestResourceStream("Chksum.Libraries.libe_sqlite3.so")) {
|
||||
if (resourceStream != null) {
|
||||
byte[] buffer = new byte[resourceStream.Length];
|
||||
resourceStream.Read(buffer, 0, buffer.Length);
|
||||
File.WriteAllBytes(libraryPath, buffer);
|
||||
} else {
|
||||
throw new Exception(libraryPath + " could not be loaded");
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
public void cleanup() {
|
||||
File.Delete(libraryPath);
|
||||
}
|
||||
|
||||
public void initializeDB() {
|
||||
if (!File.Exists("chksum.db")) {
|
||||
using (var connection = new SqliteConnection("Data Source=chksum.db")) {
|
||||
|
Loading…
Reference in New Issue
Block a user