From c51e02fa05bc3f60fccf6f5a1458e8be6750e381 Mon Sep 17 00:00:00 2001 From: ProfessionalUwU Date: Sun, 25 Jun 2023 21:12:42 +0200 Subject: [PATCH] Add library Embed the library into the executable and extract at runtime Cleanup after executing the program --- .gitignore | 1 + src/Chksum/Chksum.csproj | 1 + src/Chksum/Program.cs | 4 ++++ src/Chksum/chksum.cs | 22 +++++++++++++++++++++- 4 files changed, 27 insertions(+), 1 deletion(-) diff --git a/.gitignore b/.gitignore index e68f0aa..7576ac2 100644 --- a/.gitignore +++ b/.gitignore @@ -1,3 +1,4 @@ .vscode/ obj/ bin/ +Libraries/ diff --git a/src/Chksum/Chksum.csproj b/src/Chksum/Chksum.csproj index 6db87ee..fc84e00 100644 --- a/src/Chksum/Chksum.csproj +++ b/src/Chksum/Chksum.csproj @@ -2,6 +2,7 @@ + diff --git a/src/Chksum/Program.cs b/src/Chksum/Program.cs index 67459a6..dde1d7c 100644 --- a/src/Chksum/Program.cs +++ b/src/Chksum/Program.cs @@ -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() { diff --git a/src/Chksum/chksum.cs b/src/Chksum/chksum.cs index 9cf007e..dfc5c07 100644 --- a/src/Chksum/chksum.cs +++ b/src/Chksum/chksum.cs @@ -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")) {