Skip to content

Commit

Permalink
First draft of local storage driver
Browse files Browse the repository at this point in the history
  • Loading branch information
Mooshua committed Nov 4, 2023
1 parent d6cfca3 commit b8e2f4b
Show file tree
Hide file tree
Showing 6 changed files with 107 additions and 0 deletions.
7 changes: 7 additions & 0 deletions BitMod.sln
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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}
Expand All @@ -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
18 changes: 18 additions & 0 deletions builtin/BitMod.Storage.Local/BitMod.Storage.Local.csproj
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
<Project Sdk="Microsoft.NET.Sdk">

<PropertyGroup>
<TargetFramework>net6.0</TargetFramework>
<ImplicitUsings>enable</ImplicitUsings>
<Nullable>enable</Nullable>

<AssemblyName>bitmod_storage_local</AssemblyName>

<OutDir>$(BitModDev)/plugins/$(AssemblyName)/</OutDir>
<PublishDir>$(BitModBuild)/plugins/$(AssemblyName)/</PublishDir>
<LangVersion>latest</LangVersion>
</PropertyGroup>

<ItemGroup>
<ProjectReference Include="..\..\api\BitMod\BitMod.csproj" />
</ItemGroup>
</Project>
21 changes: 21 additions & 0 deletions builtin/BitMod.Storage.Local/Config/StorageConfigAdapter.cs
Original file line number Diff line number Diff line change
@@ -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<IConfigSymbol>(PATH) != null;

public string? Path => _configObject.Get<IConfigSymbol>(PATH)?.Symbol;

}
51 changes: 51 additions & 0 deletions builtin/BitMod.Storage.Local/Host/StorageHost.cs
Original file line number Diff line number Diff line change
@@ -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<Product> 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());
}

}
3 changes: 3 additions & 0 deletions standalone/BitMod.Launcher/BitMod.Launcher.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,9 @@
<None Update="configs\provisions.cfg">
<CopyToOutputDirectory>Always</CopyToOutputDirectory>
</None>
<None Update="configs\storage_local.cfg">
<CopyToOutputDirectory>Always</CopyToOutputDirectory>
</None>
</ItemGroup>

<ItemGroup>
Expand Down
7 changes: 7 additions & 0 deletions standalone/BitMod.Launcher/configs/storage_local.cfg
Original file line number Diff line number Diff line change
@@ -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"
}

0 comments on commit b8e2f4b

Please sign in to comment.