Skip to content

Commit

Permalink
remove MonoGame/Nez dependency for all Persistence code
Browse files Browse the repository at this point in the history
  • Loading branch information
prime31 committed Mar 3, 2019
1 parent 3aa19cd commit ed47779
Show file tree
Hide file tree
Showing 6 changed files with 15 additions and 36 deletions.
15 changes: 13 additions & 2 deletions Nez.Persistence/BINARY_README.md
Original file line number Diff line number Diff line change
Expand Up @@ -68,13 +68,20 @@ dataStore.Load( "the-filename.bin", myObj );

## Usage: KeyValueDataStore

The `KeyValueDataStore` is for storing small bits of data. It contains a default instance accessible via the `Default` ivar. An important bit to know is that `KeyValueDataStore.Default` will use the `FileDataStore` that is present in the `GameServiceContainer`. If none is there, it will create and add one that saves to `Utils.GetStorageRoot()`. The `Default` `KeyValueDataStore` will automatically call `Load` the first time it is accessed so data will always be available.
The `KeyValueDataStore` is for storing small bits of data. It contains a default instance accessible via the `Default` ivar. Be sure to call `Load` before using it the first time, preferably at application init. Calling `Flush` at application shutdown will save the data.

You can also create your own instances via the constructor passing in the filename of your choosing. You have the option of saving/loading from any `FileDataStore` by passing the `FileDataStore` to `Load` or `Flush`. If you do not pass a `FileDataStore` into these methods the default `FileDataStore` will be used as in the case above.
You can also create your own instances via the constructor passing in the filename of your choosing. You have the option of saving/loading from any `FileDataStore` by passing the `FileDataStore` to `Load` or `Flush`.

Useage example:

```csharp
// fetch the FileDataStore from your service container
var fileDataStore = Core.services.GetOrAddService<FileDataStore>().

// load data
KeyValueDataStore.Load( fileDataStore );


// setting data
KeyValueDataStore.Default.Set( "the-key", true ); // values can be of type string, bool, int or float.
Expand All @@ -88,4 +95,8 @@ if( KeyValueDataStore.Default.ContainsBoolKey( "the-key" ) )
// deleting a keys data
KeyValueDataStore.Default.DeleteBoolKey( "the-key" );


// saving data
KeyValueDataStore.Flush( fileDataStore );
```
23 changes: 0 additions & 23 deletions Nez.Persistence/Binary/DataStore/KeyValueDataStore.cs
Original file line number Diff line number Diff line change
Expand Up @@ -31,9 +31,6 @@ public class KeyValueDataStore : IPersistable
public KeyValueDataStore( string filename )
{
_filename = filename;

if( _filename == kDefaultFileName )
Load();
}


Expand Down Expand Up @@ -63,26 +60,6 @@ public void Load( FileDataStore dataStore )
dataStore.Load( _filename, this );
}

/// <summary>
/// flushes the data to disk either using a FileDataStore from the GameServiceContainer or creating and adding
/// one if one is not present.
/// </summary>
public void Flush()
{
Core.services.GetOrAddService<FileDataStore>().Save( _filename, this );
}

/// <summary>
/// restores the data from disk if it is available either using a FileDataStore from the GameServiceContainer or creating and adding
/// one if one is not present.
/// </summary>
public void Load()
{
// guard for when running in unit test
if( Core.instance != null )
Core.services.GetOrAddService<FileDataStore>().Load( _filename, this );
}

#region IPersistable

void IPersistable.Persist( IPersistableWriter writer )
Expand Down
7 changes: 1 addition & 6 deletions Nez.Persistence/Nez.Persistence.csproj
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
<Project>
<Project ToolsVersion="15.0">
<PropertyGroup>
<BaseIntermediateOutputPath>obj\$(MSBuildProjectName)</BaseIntermediateOutputPath>
</PropertyGroup>
Expand All @@ -21,10 +21,5 @@
<DefaultItemExcludes>$(DefaultItemExcludes);Tests\**\*.*</DefaultItemExcludes>
</PropertyGroup>

<ItemGroup>
<ProjectReference Include="../Nez.Portable/Nez.csproj" />
<PackageReference Include="MonoGame.Framework.Portable" Version="3.7.1.189" />
</ItemGroup>

<Import Sdk="Microsoft.NET.Sdk" Project="Sdk.targets" />
</Project>
2 changes: 1 addition & 1 deletion Nez.Persistence/Tests/Json/TypeConverterTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ public override void WriteJson( IJsonEncoder encoder, Doodle value )
public override void OnFoundCustomData( Doodle instance, string key, object value )
{
instance.totalOrphanedKeys++;
Debug.log( $"field name: {key}, value: {value}" );
System.Console.WriteLine( $"field name: {key}, value: {value}" );
}
}

Expand Down
2 changes: 0 additions & 2 deletions Nez.Persistence/Tests/Nez.Persistence.Tests.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -25,12 +25,10 @@
<PlatformTarget>anycpu</PlatformTarget>
</PropertyGroup>
<ItemGroup>
<ProjectReference Include="../../Nez.Portable/Nez.csproj" />
<ProjectReference Include="../Nez.Persistence.csproj" />
</ItemGroup>

<ItemGroup>
<PackageReference Include="MonoGame.Framework.Portable" Version="3.7.1.189" />
<PackageReference Include="NUnit" Version="3.11.0" />
</ItemGroup>
</Project>
2 changes: 0 additions & 2 deletions Nez.Persistence/Tests/Nez.Persistence.Tests.sln
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,6 @@ Microsoft Visual Studio Solution File, Format Version 12.00
# Visual Studio 15
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Nez.Persistence.Tests", "Nez.Persistence.Tests.csproj", "{7F2D4561-4F7F-423D-B259-5C199FD9E439}"
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Nez", "..\..\Nez.Portable\Nez.csproj", "{8F2D4561-4F7F-423D-B259-5C199FD9E439}"
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Nez.Persistence", "..\Nez.Persistence.csproj", "{9F2D4561-4F7F-423D-B259-5C199FD9E439}"
EndProject
Global
Expand Down

0 comments on commit ed47779

Please sign in to comment.