Skip to content

Commit

Permalink
add ignore key load behaviour
Browse files Browse the repository at this point in the history
  • Loading branch information
imurashka committed Oct 6, 2024
1 parent 80ba616 commit d29e380
Show file tree
Hide file tree
Showing 3 changed files with 13 additions and 4 deletions.
11 changes: 8 additions & 3 deletions Runtime/BinaryStorageIO.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
using System.Collections.Generic;
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;
Expand Down Expand Up @@ -89,6 +90,7 @@ internal static void SaveDataOnDisk(string storageFilePath, IReadOnlyList<Binary
/// <param name="storageFilePath"> Path to the storage file </param>
/// <param name="sections"> List of sections </param>
/// <param name="data"> Dictionary to store data </param>
/// <param name="keyLoadFailedBehaviour">Specify behaviour for broken keys</param>
/// <exception cref="IOException"> An I/O error occurred </exception>
internal static void LoadDataFromDisk(string storageFilePath, IReadOnlyList<BinarySection> sections, IDictionary<string, Record> data, KeyLoadFailedBehaviour keyLoadFailedBehaviour)
{
Expand Down Expand Up @@ -167,13 +169,16 @@ internal static void LoadDataFromDisk(string storageFilePath, IReadOnlyList<Bina

void failedToLoadKey(string reason)
{
// move stream position to the next record
stream.Position = Math.Min(position + entrySize, stream.Length);

switch (keyLoadFailedBehaviour)
{
case KeyLoadFailedBehaviour.ThrowException:
throw new KeyLoadFailedException(key, sectionsNames[typeIndex], entrySize, reason);
case KeyLoadFailedBehaviour.Ignore:
break;
case KeyLoadFailedBehaviour.IgnoreWithWarning:
// skip key by moving stream position to next record
stream.Position = position + entrySize;
Debug.LogWarning($"Failed to load key {key} of type {sectionsNames[typeIndex]} with size {entrySize}b. Reason: {reason}");
break;
default:
Expand Down
5 changes: 5 additions & 0 deletions Runtime/Settings/KeyLoadFailedBehaviour.cs
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,11 @@ public enum KeyLoadFailedBehaviour
/// </summary>
ThrowException,

/// <summary>
/// Ignores the failure and continue.
/// </summary>
Ignore,

/// <summary>
/// Ignores the failure and logs a warning.
/// </summary>
Expand Down
1 change: 0 additions & 1 deletion Tests/PrefsSaveLoadTests.cs
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
using System;
using System.IO;
using FluentAssertions;
using NSubstitute.ExceptionExtensions;
using NUnit.Framework;
using UnityEngine;

Expand Down

0 comments on commit d29e380

Please sign in to comment.