diff --git a/sessionzero/.vscode/settings.json b/sessionzero/.vscode/settings.json index f0e7773..12c7734 100644 --- a/sessionzero/.vscode/settings.json +++ b/sessionzero/.vscode/settings.json @@ -1,3 +1,3 @@ { - "godotTools.editorPath.godot4": "/home/chris/Godot/Godot_v4.4.1-stable_mono_linux_x86_64/Godot_v4.4.1-stable_mono_linux.x86_64" + "godotTools.editorPath.godot4": "/data/OtherApps/godot/Godot_v4.4.1-stable_mono_linux_x86_64/Godot_v4.4.1-stable_mono_linux.x86_64" } \ No newline at end of file diff --git a/sessionzero/AppSettings.tres b/sessionzero/AppSettings.tres index 3f2eeb8..7dffb04 100644 --- a/sessionzero/AppSettings.tres +++ b/sessionzero/AppSettings.tres @@ -1,4 +1,4 @@ -[gd_resource type="Resource" script_class="ApplicationSettingsResource" load_steps=8 format=3 uid="uid://dwhgu0ywrt618"] +[gd_resource type="Resource" script_class="ApplicationSettingsResource" load_steps=7 format=3 uid="uid://dwhgu0ywrt618"] [ext_resource type="Script" uid="uid://ddh7o0nfsuo4k" path="res://src/scripts/custom_resources/ApplicationSettingsResource.cs" id="1_3gmkh"] [ext_resource type="PackedScene" uid="uid://djj22j6g14exe" path="res://src/scenes/main/MainUI.tscn" id="1_688px"] @@ -6,10 +6,9 @@ [ext_resource type="PackedScene" uid="uid://dkrls6nkk3fk4" path="res://src/scenes/main/content_pages/library_page.tscn" id="3_tfier"] [ext_resource type="PackedScene" uid="uid://cymkrj587gxm5" path="res://src/scenes/main/content_pages/datasets_page.tscn" id="4_jyu5u"] [ext_resource type="PackedScene" uid="uid://d2fmlv2jifbvd" path="res://src/scenes/main/content_pages/characters_page.tscn" id="5_biv5l"] -[ext_resource type="PackedScene" uid="uid://cqop2rx4uybvs" path="res://src/scenes/main/content_pages/character_templates_page.tscn" id="6_y30hn"] [resource] script = ExtResource("1_3gmkh") MainUiScene = ExtResource("1_688px") -Pages = Array[PackedScene]([ExtResource("2_tfier"), ExtResource("3_tfier"), ExtResource("4_jyu5u"), ExtResource("5_biv5l"), ExtResource("6_y30hn")]) +Pages = Array[PackedScene]([ExtResource("2_tfier"), ExtResource("3_tfier"), ExtResource("4_jyu5u"), ExtResource("5_biv5l")]) metadata/_custom_type_script = "uid://ddh7o0nfsuo4k" diff --git a/sessionzero/SessionZero.csproj b/sessionzero/SessionZero.csproj index 393cc6a..1a5220f 100644 --- a/sessionzero/SessionZero.csproj +++ b/sessionzero/SessionZero.csproj @@ -4,6 +4,8 @@ true + + \ No newline at end of file diff --git a/sessionzero/project.godot b/sessionzero/project.godot index b87448e..2d77299 100644 --- a/sessionzero/project.godot +++ b/sessionzero/project.godot @@ -13,11 +13,14 @@ config_version=5 config/name="SessionZero" config/tags=PackedStringArray("c#") run/main_scene="uid://bv1ceq4dnkl7l" +config/use_custom_user_dir=true config/features=PackedStringArray("4.4", "C#", "GL Compatibility") boot_splash/bg_color=Color(0.101961, 0.12549, 0.180392, 1) boot_splash/fullsize=false boot_splash/image="uid://qnpvlqg85kx4" -config/icon="res://resources/images/icon.svg" +config/icon="uid://08eaio0bj4c7" +config/macos_native_icon="uid://08eaio0bj4c7" +config/windows_native_icon="res://resources/images/icon.svgs" [autoload] diff --git a/sessionzero/src/scripts/AppManager.cs b/sessionzero/src/scripts/AppManager.cs index f7e3a72..e4d9846 100644 --- a/sessionzero/src/scripts/AppManager.cs +++ b/sessionzero/src/scripts/AppManager.cs @@ -1,11 +1,16 @@ using Godot; +using Microsoft.Data.Sqlite; using System; using System.Collections.Generic; +using System.IO; namespace SessionZeroApp; public partial class AppManager : Node { + + + public static AppManager Instance { get; private set; } public ApplicationSettingsResource AppSettings { get; private set; } @@ -16,11 +21,14 @@ public partial class AppManager : Node public Dictionary Pages { get; private set; } = []; private PackedScene _mainUiScene; + public override void _Ready() { Instance = this; + LocalDatabaseHelper.SetupLocalDatabase(); + try { AppSettings = ResourceLoader.Load("res://AppSettings.tres"); @@ -68,6 +76,8 @@ public partial class AppManager : Node LoadPage("Home"); } + + public void LoadPage(string pageName) { if (!Pages.TryGetValue(pageName, out ContentPageBase value)) diff --git a/sessionzero/src/scripts/data/LocalDatabaseHelper.cs b/sessionzero/src/scripts/data/LocalDatabaseHelper.cs new file mode 100644 index 0000000..843c4fb --- /dev/null +++ b/sessionzero/src/scripts/data/LocalDatabaseHelper.cs @@ -0,0 +1,177 @@ +using System; +using System.Collections.Generic; +using System.IO; +using Godot; +using Microsoft.Data.Sqlite; +using SessionZero.SzfLib.File; +using SessionZero.SzfLib.Helpers; +using SessionZero.SzfLib.Objects; +using SessionZero.SzfLib.Parser; + +namespace SessionZeroApp; + +public static class LocalDatabaseHelper +{ + public const string DATA_PATH = "user://data"; + public const string LOCAL_DATABASE_FILE_PATH = "user://data/SessionZerolocal.db"; + + public static string DataDir = ProjectSettings.GlobalizePath(DATA_PATH); + public static string LocalDatabaseFilePath = ProjectSettings.GlobalizePath(LOCAL_DATABASE_FILE_PATH); + + public static List SzfObjects { get; private set; } = new(); + + public static void SetupLocalDatabase() + { + if (!Directory.Exists(DataDir)) + { + GD.Print($"SZ WARNING: Local Data Directory does not exist, creating it at {DataDir}"); + Directory.CreateDirectory(DataDir); + } + + if (!File.Exists(LocalDatabaseFilePath)) + { + GD.Print($"SZ WARNING: Local Database file does not exist, creating a new one at {LocalDatabaseFilePath}"); + + try + { + using var dbConnection = new SqliteConnection($"Data Source={LocalDatabaseFilePath}"); + dbConnection.Open(); + + SetupLocalDbTables(dbConnection); + + GD.Print($"SZ INFO: Local Database file created at {LocalDatabaseFilePath}"); + } + + catch (SqliteException e) + { + GD.PrintErr($"SZ ERROR: Could not connect local database; {e}"); + return; + } + } + + GD.Print($"SZ INFO: Local Database verified at {LocalDatabaseFilePath}"); + } + + private static void SetupLocalDbTables(SqliteConnection dbConnection) + { + var sql = @"CREATE TABLE szf_data( + name TEXT NOT NULL, + id TEXT NOT NULL, + version FLOAT NOT NULL, + type TEXT NOT NULL, + path TEXT NOT NULL, + PRIMARY KEY (name, id, version) + );"; + + try + { + using var command = new SqliteCommand(sql, dbConnection); + command.ExecuteNonQuery(); + + GD.Print($"SZ INFO: Local database tables initialized"); + } + + catch (SqliteException e) + { + GD.PrintErr($"SZ ERROR: Could not set up local database tables; {e}"); + return; + } + } + + public static ISzfObject? GetSzfObject(string name, string guid, float version) + { + var sql = @"SELECT path FROM szf_data + WHERE name = @name AND guid = @guid AND version = @version;"; + + string selectedPath = ""; + + try + { + using var connection = new SqliteConnection($"Data Source={LocalDatabaseFilePath}"); + connection.Open(); + + using var command = new SqliteCommand(sql, connection); + command.Parameters.AddWithValue("@name", name); + command.Parameters.AddWithValue("@guid", guid); + command.Parameters.AddWithValue("@version", version); + + using var reader = command.ExecuteReader(); + if (reader.HasRows) + { + while(reader.Read()) + { + selectedPath = reader.GetString(0); + } + } + } + catch (SqliteException e) + { + GD.PrintErr(e); + return null; + } + + + try + { + var content = File.ReadAllText(selectedPath); + + SzfParser parser = new(); + var szfObject = parser.Parse(content); + + return szfObject; + } + catch (Exception e) + { + GD.PrintErr(e); + return null; + } + } + + public static List GetAllDatasets() + { + var sql = @"SELECT path FROM szf_data + WHERE type = @type"; + + List datasets = new(); + + try + { + using var connection = new SqliteConnection($"Data Source={LocalDatabaseFilePath}"); + connection.Open(); + + using var command = new SqliteCommand(sql, connection); + command.Parameters.AddWithValue("@type", "dataset"); + + using var reader = command.ExecuteReader(); + if (reader.HasRows) + { + while(reader.Read()) + { + var path = reader.GetString(0); + + try + { + var content = File.ReadAllText(path); + + SzfParser parser = new(); + var szfObject = parser.Parse(content) as SzfDataset; + + datasets.Add(szfObject); + } + catch (Exception e) + { + GD.PrintErr(e); + } + } + } + } + catch (SqliteException e) + { + GD.PrintErr(e); + } + + GD.Print($"Retrieving {datasets.Count} datasets"); + + return datasets; + } +} \ No newline at end of file diff --git a/sessionzero/src/scripts/data/LocalDatabaseHelper.cs.uid b/sessionzero/src/scripts/data/LocalDatabaseHelper.cs.uid new file mode 100644 index 0000000..fd98397 --- /dev/null +++ b/sessionzero/src/scripts/data/LocalDatabaseHelper.cs.uid @@ -0,0 +1 @@ +uid://d2hdpv3n022wx diff --git a/sessionzero/src/scripts/scenes/main/content_pages/DatasetsContentPage.cs b/sessionzero/src/scripts/scenes/main/content_pages/DatasetsContentPage.cs index 4b581ef..382ef73 100644 --- a/sessionzero/src/scripts/scenes/main/content_pages/DatasetsContentPage.cs +++ b/sessionzero/src/scripts/scenes/main/content_pages/DatasetsContentPage.cs @@ -21,7 +21,9 @@ public partial class DatasetsContentPage : ContentPageBase { SetupConnections(); - + Datasets.AddRange(LocalDatabaseHelper.GetAllDatasets()); + + GD.Print(Datasets[0].GetMetadataField("Name")); } private void SetupConnections()