From b8e2f4ba7c3963ce537b7fafbc899d5cdcc3ed0f Mon Sep 17 00:00:00 2001
From: Mooshua <43320783+mooshua@users.noreply.github.com>
Date: Sat, 4 Nov 2023 13:08:10 -0700
Subject: [PATCH] First draft of local storage driver
---
BitMod.sln | 7 +++
.../BitMod.Storage.Local.csproj | 18 +++++++
.../Config/StorageConfigAdapter.cs | 21 ++++++++
.../BitMod.Storage.Local/Host/StorageHost.cs | 51 +++++++++++++++++++
.../BitMod.Launcher/BitMod.Launcher.csproj | 3 ++
.../BitMod.Launcher/configs/storage_local.cfg | 7 +++
6 files changed, 107 insertions(+)
create mode 100644 builtin/BitMod.Storage.Local/BitMod.Storage.Local.csproj
create mode 100644 builtin/BitMod.Storage.Local/Config/StorageConfigAdapter.cs
create mode 100644 builtin/BitMod.Storage.Local/Host/StorageHost.cs
create mode 100644 standalone/BitMod.Launcher/configs/storage_local.cfg
diff --git a/BitMod.sln b/BitMod.sln
index 3d811fe..860fba5 100644
--- a/BitMod.sln
+++ b/BitMod.sln
@@ -42,6 +42,8 @@ Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "BitMod.Provision", "builtin
EndProject
Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "plugins", "plugins", "{40317A0E-F34B-4367-BF2C-C2540A521B28}"
EndProject
+Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "BitMod.Storage.Local", "builtin\BitMod.Storage.Local\BitMod.Storage.Local.csproj", "{7341505C-9BA0-4140-8EE0-1FC8F20E2680}"
+EndProject
Global
GlobalSection(SolutionConfigurationPlatforms) = preSolution
Debug|Any CPU = Debug|Any CPU
@@ -104,6 +106,10 @@ Global
{2FCD0D1B-C58A-406C-83F9-FD2EB3598F9F}.Debug|Any CPU.Build.0 = Debug|Any CPU
{2FCD0D1B-C58A-406C-83F9-FD2EB3598F9F}.Release|Any CPU.ActiveCfg = Release|Any CPU
{2FCD0D1B-C58A-406C-83F9-FD2EB3598F9F}.Release|Any CPU.Build.0 = Release|Any CPU
+ {7341505C-9BA0-4140-8EE0-1FC8F20E2680}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
+ {7341505C-9BA0-4140-8EE0-1FC8F20E2680}.Debug|Any CPU.Build.0 = Debug|Any CPU
+ {7341505C-9BA0-4140-8EE0-1FC8F20E2680}.Release|Any CPU.ActiveCfg = Release|Any CPU
+ {7341505C-9BA0-4140-8EE0-1FC8F20E2680}.Release|Any CPU.Build.0 = Release|Any CPU
EndGlobalSection
GlobalSection(NestedProjects) = preSolution
{20CD73A1-A74C-4205-AD74-9B48EEEFB3EC} = {621F6C25-527F-4BA1-BF59-1D19021A9B88}
@@ -120,5 +126,6 @@ Global
{0DF78B4C-26A7-4FF7-BD07-036981815125} = {CCEA8B6B-5212-4C12-BB31-A70501DE31D0}
{3FB356E0-587B-42EA-A1A0-C10AE6ADA621} = {CCEA8B6B-5212-4C12-BB31-A70501DE31D0}
{2FCD0D1B-C58A-406C-83F9-FD2EB3598F9F} = {CCEA8B6B-5212-4C12-BB31-A70501DE31D0}
+ {7341505C-9BA0-4140-8EE0-1FC8F20E2680} = {CCEA8B6B-5212-4C12-BB31-A70501DE31D0}
EndGlobalSection
EndGlobal
diff --git a/builtin/BitMod.Storage.Local/BitMod.Storage.Local.csproj b/builtin/BitMod.Storage.Local/BitMod.Storage.Local.csproj
new file mode 100644
index 0000000..f81113c
--- /dev/null
+++ b/builtin/BitMod.Storage.Local/BitMod.Storage.Local.csproj
@@ -0,0 +1,18 @@
+
+
+
+ net6.0
+ enable
+ enable
+
+ bitmod_storage_local
+
+ $(BitModDev)/plugins/$(AssemblyName)/
+ $(BitModBuild)/plugins/$(AssemblyName)/
+ latest
+
+
+
+
+
+
diff --git a/builtin/BitMod.Storage.Local/Config/StorageConfigAdapter.cs b/builtin/BitMod.Storage.Local/Config/StorageConfigAdapter.cs
new file mode 100644
index 0000000..3c8a65e
--- /dev/null
+++ b/builtin/BitMod.Storage.Local/Config/StorageConfigAdapter.cs
@@ -0,0 +1,21 @@
+using BitMod.Configuration.Model;
+
+namespace BitMod.Storage.Local.Config;
+
+public class StorageConfigAdapter
+{
+ private const string PATH = "path";
+
+ private IConfigObject _configObject;
+
+ public StorageConfigAdapter(IConfigObject configObject)
+ {
+ _configObject = configObject;
+ }
+
+ public bool HasPath()
+ => _configObject.Get(PATH) != null;
+
+ public string? Path => _configObject.Get(PATH)?.Symbol;
+
+}
diff --git a/builtin/BitMod.Storage.Local/Host/StorageHost.cs b/builtin/BitMod.Storage.Local/Host/StorageHost.cs
new file mode 100644
index 0000000..a8a8d17
--- /dev/null
+++ b/builtin/BitMod.Storage.Local/Host/StorageHost.cs
@@ -0,0 +1,51 @@
+
+using BattleBitAPI.Common;
+
+using BitMod.Attributes.Injects;
+using BitMod.Attributes.Targets;
+using BitMod.Configuration.Model;
+using BitMod.Events.Stats;
+using BitMod.Plugins.Events;
+using BitMod.Storage.Local.Config;
+
+namespace BitMod.Storage.Local.Host;
+
+public class StorageHost
+{
+
+ [Config("storage_local")]
+ private IConfigObject _configObject;
+
+ private StorageConfigAdapter _config => new StorageConfigAdapter(_configObject);
+
+ private string Build(string id)
+ {
+ if (!_config.HasPath() || _config.Path == null)
+ throw new InvalidOperationException("local storage config does not have a valid path!");
+
+ Directory.CreateDirectory(_config.Path);
+ return Path.Combine(_config.Path, id);
+ }
+
+ [BitProducer]
+ public async Task OnGetStats(GetPlayerStatsEventArgs ev)
+ {
+ var path = Build(ev.SteamID.ToString());
+
+ if (!File.Exists(path))
+ return Product.None();
+
+ var bytes = File.ReadAllBytes(path);
+
+ return Product.Produce(new PlayerStats(bytes));
+ }
+
+ [BitEvent]
+ public async Task OnSaveStats(SavingPlayerStatsEventArgs ev)
+ {
+ var path = Build(ev.SteamID.ToString());
+
+ File.WriteAllBytes(path, ev.PlayerStats.SerializeToByteArray());
+ }
+
+}
diff --git a/standalone/BitMod.Launcher/BitMod.Launcher.csproj b/standalone/BitMod.Launcher/BitMod.Launcher.csproj
index 8445363..ea14277 100644
--- a/standalone/BitMod.Launcher/BitMod.Launcher.csproj
+++ b/standalone/BitMod.Launcher/BitMod.Launcher.csproj
@@ -42,6 +42,9 @@
Always
+
+ Always
+
diff --git a/standalone/BitMod.Launcher/configs/storage_local.cfg b/standalone/BitMod.Launcher/configs/storage_local.cfg
new file mode 100644
index 0000000..d6e7466
--- /dev/null
+++ b/standalone/BitMod.Launcher/configs/storage_local.cfg
@@ -0,0 +1,7 @@
+"Storage"
+{
+ // The path where the player stats database will be placed.
+ // This is a naive file-based database for the time being.
+ // The path must either not exist or be a directory.
+ "path" "data/stats"
+}