Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Rename BinaryPrefs to BinaryStorage #34

Merged
merged 5 commits into from
Jun 22, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
36 changes: 18 additions & 18 deletions .BinaryPrefs/Assets/Scripts/GameManager.cs
Original file line number Diff line number Diff line change
@@ -1,58 +1,58 @@
using System.IO;
using UnityEngine;

namespace Appegy.BinaryStorage.Example
namespace Appegy.Storage.Example
{
public class GameManager : MonoBehaviour
{
private BinaryPrefs _prefs;
private BinaryStorage _storage;

private void Awake()
{
Debug.Log("Game started");
_prefs = BinaryPrefs
_storage = BinaryStorage
.Construct(Path.Combine(Application.persistentDataPath, "PlayerPrefs.bin"))
.AddPrimitiveTypes()
.SupportListsOf<int>()
.SupportListsOf<string>()
.EnableAutoSaveOnChange()
.Build();

_prefs.GetListOf<int>("ints");
_prefs.Remove("ints");
_storage.GetListOf<int>("ints");
_storage.Remove("ints");

using (_prefs.MultipleChangeScope())
using (_storage.MultipleChangeScope())
{
var value = _prefs.Get<int>("int_val", 0);
_prefs.Set("int_val", value + 1);
var value = _storage.Get<int>("int_val", 0);
_storage.Set("int_val", value + 1);

using (_prefs.MultipleChangeScope())
using (_storage.MultipleChangeScope())
{
value = _prefs.Get<int>("int_val", 0);
_prefs.Set("int_val", value + 1);
value = _storage.Get<int>("int_val", 0);
_storage.Set("int_val", value + 1);

using (_prefs.MultipleChangeScope())
using (_storage.MultipleChangeScope())
{
value = _prefs.Get<int>("int_val", 0);
_prefs.Set("int_val", value + 1);
value = _storage.Get<int>("int_val", 0);
_storage.Set("int_val", value + 1);
}
}
}
}

private void OnGUI()
{
var value = _prefs.Get<int>("int_val", 0);
var value = _storage.Get<int>("int_val", 0);
if (GUILayout.Button($"INT={value}"))
{
_prefs.Set("int_val", value + 1);
_storage.Set("int_val", value + 1);
}
}

private void OnDestroy()
{
_prefs?.Dispose();
_prefs = null;
_storage?.Dispose();
_storage = null;
}
}
}
2 changes: 1 addition & 1 deletion Editor/Appegy.BinaryStorage.Editor.asmdef
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "Appegy.BinaryStorage.Editor",
"rootNamespace": "Appegy.BinaryStorage",
"rootNamespace": "Appegy.Storage",
"references": [
"Appegy.BinaryStorage"
],
Expand Down
2 changes: 1 addition & 1 deletion Editor/BinaryPrefsExplorer.cs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
using UnityEditor;

namespace Appegy.BinaryStorage
namespace Appegy.Storage
{
public class BinaryPrefsExplorer : EditorWindow
{
Expand Down
2 changes: 1 addition & 1 deletion Runtime/Appegy.BinaryStorage.asmdef
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "Appegy.BinaryStorage",
"rootNamespace": "Appegy.BinaryStorage",
"rootNamespace": "Appegy.Storage",
"references": [],
"includePlatforms": [],
"excludePlatforms": [],
Expand Down
2 changes: 1 addition & 1 deletion Runtime/BinarySection.cs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
using System.IO;

namespace Appegy.BinaryStorage
namespace Appegy.Storage
{
internal abstract class BinarySection
{
Expand Down
24 changes: 12 additions & 12 deletions Runtime/BinaryPrefs.Builder.cs → Runtime/BinaryStorage.Builder.cs
Original file line number Diff line number Diff line change
Expand Up @@ -3,20 +3,20 @@
using System.IO;
using System.Linq;

namespace Appegy.BinaryStorage
namespace Appegy.Storage
{
public partial class BinaryPrefs
public partial class BinaryStorage
{
static partial void LockFilePathInEditor(string filePath);
static partial void ThrowIfFilePathLocked(string filePath);
static partial void UnlockFilePathInEditor(string filePath);

/// <summary>
/// Creates and configures a new instance of <see cref="BinaryPrefs"/> with default settings.
/// Creates and configures a new instance of <see cref="BinaryStorage"/> with default settings.
/// </summary>
/// <param name="filePath">The file path for the storage.</param>
/// <returns>A configured <see cref="BinaryPrefs"/> instance.</returns>
public static BinaryPrefs Get(string filePath)
/// <returns>A configured <see cref="BinaryStorage"/> instance.</returns>
public static BinaryStorage Get(string filePath)
{
return Construct(filePath)
.AddPrimitiveTypes()
Expand All @@ -25,10 +25,10 @@ public static BinaryPrefs Get(string filePath)
}

/// <summary>
/// Begins the construction of a new <see cref="BinaryPrefs"/> instance.
/// Begins the construction of a new <see cref="BinaryStorage"/> instance.
/// </summary>
/// <param name="filePath">The file path for the storage.</param>
/// <returns>A <see cref="Builder"/> for configuring the <see cref="BinaryPrefs"/> instance.</returns>
/// <returns>A <see cref="Builder"/> for configuring the <see cref="BinaryStorage"/> instance.</returns>
public static Builder Construct(string filePath)
{
ThrowIfFilePathLocked(filePath);
Expand All @@ -49,7 +49,7 @@ internal static void Delete(string storagePath)
}

/// <summary>
/// Provides a fluent interface for configuring and building a <see cref="BinaryPrefs"/> instance.
/// Provides a fluent interface for configuring and building a <see cref="BinaryStorage"/> instance.
/// </summary>
public class Builder
{
Expand Down Expand Up @@ -215,15 +215,15 @@ public Builder SupportDictionariesOf<TKey, TValue>()
}

/// <summary>
/// Builds and returns the configured <see cref="BinaryPrefs"/> instance.
/// Builds and returns the configured <see cref="BinaryStorage"/> instance.
/// </summary>
/// <returns>The configured <see cref="BinaryPrefs"/> instance.</returns>
/// <returns>The configured <see cref="BinaryStorage"/> instance.</returns>
/// <exception cref="Exception">Thrown if the storage fails to load data from disk.</exception>
public BinaryPrefs Build()
public BinaryStorage Build()
{
try
{
var storage = new BinaryPrefs(_filePath, _serializers);
var storage = new BinaryStorage(_filePath, _serializers);
storage.AutoSave = _autoSave;
storage.LoadDataFromDisk();
return storage;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,9 @@
using System.IO;
using System.Linq;

namespace Appegy.BinaryStorage
namespace Appegy.Storage
{
public partial class BinaryPrefs
public partial class BinaryStorage
{
private static readonly HashSet<string> _lockedFiles = new();

Expand Down
File renamed without changes.
17 changes: 8 additions & 9 deletions Runtime/BinaryPrefs.cs → Runtime/BinaryStorage.cs
Original file line number Diff line number Diff line change
Expand Up @@ -5,15 +5,14 @@
using UnityEngine;
using UnityEngine.Pool;

namespace Appegy.BinaryStorage
namespace Appegy.Storage
{
/// <summary>
/// Manages a binary storage system for saving, retrieving, and managing records of various types.
/// </summary>
public partial class BinaryPrefs : IDisposable
public partial class BinaryStorage : IDisposable
{
private readonly string _storageFilePath;
private readonly bool _autoSave;
private readonly IReadOnlyList<BinarySection> _supportedTypes;
private readonly Dictionary<string, Record> _data = new();
private int _changeScopeCounter;
Expand All @@ -34,11 +33,11 @@ public partial class BinaryPrefs : IDisposable
public bool IsDisposed { get; private set; }

/// <summary>
/// Initializes a new instance of the <see cref="BinaryPrefs"/> class.
/// Initializes a new instance of the <see cref="BinaryStorage"/> class.
/// </summary>
/// <param name="storageFilePath">The file path for storing data.</param>
/// <param name="supportedTypes">The list of supported types for storage.</param>
internal BinaryPrefs(string storageFilePath, IReadOnlyList<BinarySection> supportedTypes)
internal BinaryStorage(string storageFilePath, IReadOnlyList<BinarySection> supportedTypes)
{
_storageFilePath = storageFilePath;
_supportedTypes = supportedTypes;
Expand Down Expand Up @@ -400,7 +399,7 @@ private void DecreaseCounter()
{
if (_changeScopeCounter == 0)
{
Debug.LogError($"{nameof(BinaryPrefs)}: Unexpected behaviour - MultipleChangeScope counter is already zero");
Debug.LogError($"{nameof(BinaryStorage)}: Unexpected behaviour - MultipleChangeScope counter is already zero");
return;
}
_changeScopeCounter--;
Expand Down Expand Up @@ -460,7 +459,7 @@ private void ThrowIfCollection<T>()
/// <summary>
/// Finalizer
/// </summary>
~BinaryPrefs()
~BinaryStorage()
{
Dispose(false);
}
Expand Down Expand Up @@ -512,7 +511,7 @@ private void Dispose(bool disposing)
private void LoadDataFromDisk()
{
ThrowIfDisposed();
BinaryPrefsIO.LoadDataFromDisk(_storageFilePath, _supportedTypes, _data);
BinaryStorageIO.LoadDataFromDisk(_storageFilePath, _supportedTypes, _data);
foreach (var rc in _data.Values.Select(c => c.Object).OfType<IReactiveCollection>())
{
rc.OnChanged += MarkChanged;
Expand All @@ -525,7 +524,7 @@ private void LoadDataFromDisk()
private void SaveDataFromDisk()
{
ThrowIfDisposed();
BinaryPrefsIO.SaveDataOnDisk(_storageFilePath, _supportedTypes, _data);
BinaryStorageIO.SaveDataOnDisk(_storageFilePath, _supportedTypes, _data);
IsDirty = false;
}

Expand Down
File renamed without changes.
4 changes: 2 additions & 2 deletions Runtime/BinaryPrefsIO.cs → Runtime/BinaryStorageIO.cs
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,9 @@
using System.Text;
using UnityEngine;

namespace Appegy.BinaryStorage
namespace Appegy.Storage
{
internal static class BinaryPrefsIO
internal static class BinaryStorageIO
{
internal static void SaveDataOnDisk(string storageFilePath, IReadOnlyList<BinarySection> sections, IReadOnlyDictionary<string, Record> data)
{
Expand Down
File renamed without changes.
2 changes: 1 addition & 1 deletion Runtime/Collections/IReactiveCollection.cs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
using System;

namespace Appegy.BinaryStorage
namespace Appegy.Storage
{
internal interface IReactiveCollection : IDisposable
{
Expand Down
2 changes: 1 addition & 1 deletion Runtime/Collections/ReactiveDictionary.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
using System.Collections;
using System.Collections.Generic;

namespace Appegy.BinaryStorage
namespace Appegy.Storage
{
public class ReactiveDictionary<TKey, TValue> : IReactiveCollection, IDictionary<TKey, TValue>, IReadOnlyDictionary<TKey, TValue>
{
Expand Down
2 changes: 1 addition & 1 deletion Runtime/Collections/ReactiveList.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
using System.Collections;
using System.Collections.Generic;

namespace Appegy.BinaryStorage
namespace Appegy.Storage
{
public class ReactiveList<T> : IReactiveCollection, IList<T>, IReadOnlyList<T>
{
Expand Down
2 changes: 1 addition & 1 deletion Runtime/Collections/ReactiveSet.cs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
using System.Collections.Generic;
using JetBrains.Annotations;

namespace Appegy.BinaryStorage
namespace Appegy.Storage
{
public class ReactiveSet<T> : IReactiveCollection, ISet<T>, IReadOnlyCollection<T>
{
Expand Down
4 changes: 2 additions & 2 deletions Runtime/Exceptions/CantSupportCollectionOfException.cs
Original file line number Diff line number Diff line change
@@ -1,13 +1,13 @@
using System;

namespace Appegy.BinaryStorage
namespace Appegy.Storage
{
public class CantSupportCollectionOfException : Exception
{
public Type ItemType { get; }

public CantSupportCollectionOfException(Type itemType)
: base($"Add serializer for {itemType.FullName} using {nameof(BinaryPrefs.Builder.AddTypeSerializer)} before adding support for List<{itemType.Name}>.")
: base($"Add serializer for {itemType.FullName} using {nameof(BinaryStorage.Builder.AddTypeSerializer)} before adding support for List<{itemType.Name}>.")
{
ItemType = itemType;
}
Expand Down
2 changes: 1 addition & 1 deletion Runtime/Exceptions/CollectionDisposedException.cs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
using System;

namespace Appegy.BinaryStorage
namespace Appegy.Storage
{
public class CollectionDisposedException : Exception
{
Expand Down
2 changes: 1 addition & 1 deletion Runtime/Exceptions/DuplicateTypeSerializerException.cs
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
using System;
using System.IO;

namespace Appegy.BinaryStorage
namespace Appegy.Storage
{
public class DuplicateTypeSerializerException : Exception
{
Expand Down
2 changes: 1 addition & 1 deletion Runtime/Exceptions/IncorrectUsageOfCollectionException.cs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
using System;

namespace Appegy.BinaryStorage
namespace Appegy.Storage
{
public class IncorrectUsageOfCollectionException : Exception
{
Expand Down
2 changes: 1 addition & 1 deletion Runtime/Exceptions/StorageDisposedException.cs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
using System;

namespace Appegy.BinaryStorage
namespace Appegy.Storage
{
public class StorageDisposedException : Exception
{
Expand Down
2 changes: 1 addition & 1 deletion Runtime/Exceptions/UnexpectedTypeException.cs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
using System;

namespace Appegy.BinaryStorage
namespace Appegy.Storage
{
public class UnexpectedTypeException : Exception
{
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
using System;

namespace Appegy.BinaryStorage
namespace Appegy.Storage
{
public class UnexpectedUnderlyingEnumTypeException : Exception
{
Expand Down
2 changes: 1 addition & 1 deletion Runtime/Exceptions/UnregisteredTypeException.cs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
using System;

namespace Appegy.BinaryStorage
namespace Appegy.Storage
{
public class UnregisteredTypeException : Exception
{
Expand Down
2 changes: 1 addition & 1 deletion Runtime/PackageInfo.cs
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@

[assembly: InternalsVisibleTo("Appegy.BinaryStorage.Tests")]

namespace Appegy.BinaryStorage
namespace Appegy.Storage
{
public static class PackageInfo
{
Expand Down
2 changes: 1 addition & 1 deletion Runtime/Record.cs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
using System;

namespace Appegy.BinaryStorage
namespace Appegy.Storage
{
internal abstract class Record
{
Expand Down
Loading