Skip to content

Commit

Permalink
Adjust method naming, add more methods to read data from streams, upd…
Browse files Browse the repository at this point in the history
…ate changelog, add more tests
  • Loading branch information
Edvinas01 committed Oct 19, 2023
1 parent 51e904f commit dd0b5b6
Show file tree
Hide file tree
Showing 37 changed files with 992 additions and 366 deletions.
15 changes: 9 additions & 6 deletions Packages/com.chark.game-management/CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -9,19 +9,22 @@ adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.html).

### Added

- `ISerializer` with `GameManager.TryDeserializeValue` and `GameManager.TrySerializeValue` methods, this can be used to serialize and deserialize data using internal `GameManager` serialization utilities.
- `GameManager.DeleteRuntimeValueAsync` which can be used to delete data asynchronously.
- `GameManager.GetResourceAsync` which can be used to retrieve resources from StreamingAssets directory.
- `GameManager.TryDeserializeValue`, `GameManager.TrySerializeValue`, `GameManager.TryDeserializeStream` and `GameManager.TrySerializeStream` methods, these can be used to serialize and deserialize data using internal `GameManager` serialization utilities. You can customize this via `ISerializer` interface (see `CreateSerializer` method on `GameManager`).
- `GameManager.DeleteDataAsync` which can be used to delete data asynchronously.
- `GameManager.ReadResourceAsync` and `GameManager.ReadResourceStreamAsync` methods which can be used to retrieve resources from StreamingAssets directory.
- `GameManager.ReadDataStream` and `GameManager.ReadDataStreamAsync` methods which can be used to read a `Stream` from a file on disk.
- `GameManager.SaveDataStream` and `GameManager.SaveDataStreamAsync` methods which can be used to persist a `Stream` to disk.

### Changed

- Renamed `IResourceLoader` methods to use `Get*` prefix instead of `Load*` so its more consistent with other methods.
- Renamed some `IResourceLoader` methods to use `Get*` prefix instead of `Load*` so its more consistent with other methods. Methods which read from _StreamingAssets_ directory will use `Read*` prefix.
- Renamed `IGameStorage` to `IStorage`.
- Cancellation tokens can now be used in async methods.
- Cancellation tokens can now be used in all async methods.
- Renamed `IStorage` methods to use `Read*` and `Save*` prefixes to emphasise that these methods interact with data on dist.

### Fixed

- `GameStorage.GetValueAsync` not switching back to main thread when no value is found.
- `GameStorage.GetValueAsync` (now `GameStorage.ReadValueAsync`) not switching back to main thread when no value is found.

## [v0.0.2](https://github.com/chark/game-management/compare/v0.0.1...v0.0.2) - 2023-10-06

Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
using System.Collections.Generic;
using System.IO;
using System.Threading;
using System.Threading.Tasks;
using UnityEngine;
Expand All @@ -8,24 +9,46 @@ namespace CHARK.GameManagement.Assets
public interface IResourceLoader
{
/// <returns>
/// Enumerable of resources retrieved from given <paramref name="path"/>.
/// Resources of type <see cref="TResource"/> retrieved from given <paramref name="path"/>.
/// </returns>
/// <remarks>
/// <paramref name="path"/> is relative to <i>Resources</i> directory.
/// </remarks>
public IEnumerable<TResource> GetResources<TResource>(string path = null)
where TResource : Object;

/// <returns>
/// <c>true</c> if <paramref name="resource"/> is retrieved from given
/// <c>true</c> if resource of type <see cref="TResource"/> is retrieved from given
/// <paramref name="path"/> or <c>false</c> otherwise.
/// </returns>
/// <remarks>
/// <paramref name="path"/> is relative to <i>Resources</i> directory.
/// </remarks>
public bool TryGetResource<TResource>(string path, out TResource resource)
where TResource : Object;

/// <returns>
/// Asset loaded asynchronously from given <paramref name="path"/>.
/// <p/>
/// <b>Note</b>, <paramref name="path"/> is relative to StreamingAssets directory.
/// Resource of type <see cref="TResource"/> retrieved from given <paramref name="path"/>.
/// </returns>
public Task<TResource> GetResourceAsync<TResource>(
/// <remarks>
/// <paramref name="path"/> is relative to <i>StreamingAssets</i> directory.
/// </remarks>
/// <exception cref="System.Exception">
/// if <see cref="TResource"/> could not be retrieved.
/// </exception>
public Task<TResource> ReadResourceAsync<TResource>(
string path,
CancellationToken cancellationToken = default
);

/// <returns>
/// Resource <see cref="Stream"/> retrieved from given <paramref name="path"/>. If something
/// fails, an empty stream is be returned.
/// </returns>
/// <remarks>
/// <paramref name="path"/> is relative to <i>StreamingAssets</i> directory.
/// </remarks>
public Task<Stream> ReadResourceStreamAsync(
string path,
CancellationToken cancellationToken = default
);
Expand Down
Original file line number Diff line number Diff line change
@@ -1,8 +1,10 @@
using System.Collections.Generic;
using System;
using System.Collections.Generic;
using System.IO;
using System.Threading;
using System.Threading.Tasks;
using CHARK.GameManagement.Serialization;
using CHARK.GameManagement.Utilities;
using UnityEngine;
using Object = UnityEngine.Object;

Expand Down Expand Up @@ -46,11 +48,16 @@ public bool TryGetResource<TResource>(string path, out TResource resource) where
return false;
}

public async Task<TResource> GetResourceAsync<TResource>(
public async Task<TResource> ReadResourceAsync<TResource>(
string path,
CancellationToken cancellationToken
)
{
if (string.IsNullOrWhiteSpace(path))
{
throw new ArgumentException($"{path} cannot be null or empty");
}

var actualPath = Path.Combine(Application.streamingAssetsPath, path);

#if UNITY_ANDROID
Expand All @@ -73,7 +80,51 @@ await Cysharp.Threading.Tasks.UnityAsyncExtensions.ToUniTask(
return value;
}

return default;
throw new Exception($"Could not retrieve resource from path: {actualPath}");
}

#pragma warning disable CS1998
public async Task<Stream> ReadResourceStreamAsync(
#pragma warning restore CS1998
string path,
CancellationToken cancellationToken = default
)
{
if (string.IsNullOrWhiteSpace(path))
{
return Stream.Null;
}

var actualPath = Path.Combine(Application.streamingAssetsPath, path);

try
{
#if UNITY_ANDROID
var request = UnityEngine.Networking.UnityWebRequest.Get(actualPath);
var operation = request.SendWebRequest();

await Cysharp.Threading.Tasks.UnityAsyncExtensions.ToUniTask(
operation,
cancellationToken: cancellationToken
);

var handler = request.downloadHandler;
var data = handler.data;
if (data == null || data.Length == 0)
{
return Stream.Null;
}

return new MemoryStream(data);
#else
return File.OpenRead(actualPath);
#endif
}
catch (Exception exception)
{
Logging.LogException(exception, GetType());
return Stream.Null;
}
}
}
}
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
using System.Collections.Generic;
using System;
using System.Collections.Generic;

namespace CHARK.GameManagement.Entities
{
Expand All @@ -10,23 +11,24 @@ public interface IEntityManager
public IReadOnlyList<object> Entities { get; }

/// <summary>
/// Add a <paramref name="entity"/> to the Entity Manager.
/// Add a <paramref name="entity"/> of type <see cref="TEntity"/> to the Entity Manager.
/// </summary>
/// <returns>
/// <c>true</c> if given <paramref name="entity"/> was added or <c>false</c> otherwise.
/// </returns>
public bool AddEntity<TEntity>(TEntity entity) where TEntity : class;

/// <summary>
/// Remove a <paramref name="entity"/> from the Entity Manager.
/// Remove an <paramref name="entity"/> if type <see cref="TEntity"/> from the Entity Manager.
/// </summary>
/// <returns>
/// <c>true</c> if given <paramref name="entity"/> was removed or <c>false</c> otherwise.
/// </returns>
public bool RemoveEntity<TEntity>(TEntity entity) where TEntity : class;

/// <returns>
/// <c>true</c> if <paramref name="entity"/> is retrieved or <c>false</c> otherwise.
/// <c>true</c> if <paramref name="entity"/> of type <see cref="TEntity"/> is retrieved or
/// <c>false</c> otherwise.
/// </returns>
public bool TryGetEntity<TEntity>(out TEntity entity);

Expand All @@ -36,9 +38,11 @@ public interface IEntityManager
public IEnumerable<TEntity> GetEntities<TEntity>();

/// <returns>
/// Entity of type <see cref="TEntity"/> or throws <see cref="System.Exception"/> if entity
/// is not added to the manager.
/// Entity of type <see cref="TEntity"/>.
/// </returns>
/// <exception cref="Exception">
/// if system of type <see cref="TEntity"/> is not found.
/// </exception>
public TEntity GetEntity<TEntity>();
}
}
Loading

0 comments on commit dd0b5b6

Please sign in to comment.