Files
ProjectQuint/Assets/ProjectQuint/Runtime/Unity/Library/DifficultyTableLibrarySource.cs
T

73 lines
3.0 KiB
C#

using System;
using System.Collections;
namespace ProjectQuint.Library
{
internal sealed class DifficultyTableLibraryData
{
public ExternalDataResult<BmsDifficultyTableIndex> Catalog { get; set; }
public ExternalDataResult<AeryCourseIndex> Courses { get; set; }
}
internal sealed class DifficultyTableLibrarySource
{
private readonly string _providerId;
private readonly string _displayName;
private readonly string _headerUrl;
private readonly string _headerCachePath;
private readonly string _dataCachePath;
public DifficultyTableLibrarySource(BmsDifficultyTableDefinition table, BmsDifficultyTableRegistry registry)
{
// Snapshot registration values so a later selection cannot redirect an in-flight load.
_providerId = table?.Id ?? string.Empty;
_displayName = table?.DisplayName ?? string.Empty;
_headerUrl = table?.HeaderUrl ?? string.Empty;
_headerCachePath = table == null ? string.Empty : registry.HeaderCachePathFor(table);
_dataCachePath = table == null ? string.Empty : registry.DataCachePathFor(table);
}
public bool IsConfigured => !string.IsNullOrWhiteSpace(_headerUrl);
public DifficultyTableLibraryData LoadLocal()
{
var catalog = IsConfigured
? BmsDifficultyTableIndex.LoadLocal(_providerId, _headerCachePath, _dataCachePath)
: new ExternalDataResult<BmsDifficultyTableIndex> { Status = "No difficulty table configured" };
return WithCourses(catalog);
}
public IEnumerator Refresh(Action<DifficultyTableLibraryData> completed)
{
DifficultyTableRequest request;
try
{
request = new DifficultyTableRequest(_providerId, _headerUrl, _headerCachePath, _dataCachePath);
}
catch (ArgumentException exception)
{
completed?.Invoke(WithCourses(new ExternalDataResult<BmsDifficultyTableIndex> { Status = exception.Message }));
yield break;
}
ExternalDataResult<BmsDifficultyTableIndex> catalog = null;
yield return BmsDifficultyTableIndex.LoadOnline(request, result => catalog = result);
catalog ??= new ExternalDataResult<BmsDifficultyTableIndex>
{
Status = "Difficulty table request did not complete",
};
completed?.Invoke(WithCourses(catalog));
}
private DifficultyTableLibraryData WithCourses(ExternalDataResult<BmsDifficultyTableIndex> catalog)
{
return new DifficultyTableLibraryData
{
Catalog = catalog,
Courses = IsConfigured
? AeryCourseIndex.LoadTableLocal(_providerId, catalog.Data?.DisplayName ?? _displayName, _headerCachePath)
: new ExternalDataResult<AeryCourseIndex> { Status = "NO BMSTABLE COURSE DATA CONFIGURED" },
};
}
}
}