Add library

Embed the library into the executable and extract at runtime
Cleanup after executing the program
This commit is contained in:
ProfessionalUwU 2023-06-25 21:12:42 +02:00
parent 132894e924
commit c51e02fa05
Signed by: ProfessionalUwU
GPG Key ID: 9F28CB1645C4BFB5
4 changed files with 27 additions and 1 deletions

1
.gitignore vendored
View File

@ -1,3 +1,4 @@
.vscode/
obj/
bin/
Libraries/

View File

@ -2,6 +2,7 @@
<ItemGroup>
<ProjectReference Include="..\Chksum.Tests\Chksum.Tests.csproj" />
<EmbeddedResource Include="Libraries/libe_sqlite3.so" />
</ItemGroup>
<PropertyGroup>

View File

@ -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() {

View File

@ -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")) {