-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
6 changed files
with
107 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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
21
builtin/BitMod.Storage.Local/Config/StorageConfigAdapter.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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()); | ||
} | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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" | ||
} |