// Copyright (c) 2026 GeWuYou
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
using GFramework.Core.Abstractions.storage;
using GFramework.Core.extensions;
using GFramework.Core.utility;
using GFramework.Game.Abstractions.data;
using GFramework.Game.Abstractions.data.events;
namespace GFramework.Game.data;
///
/// 数据仓库类,用于管理游戏数据的存储和读取
///
/// 存储接口实例
/// 数据仓库配置选项
public class DataRepository(IStorage? storage, DataRepositoryOptions? options = null)
: AbstractContextUtility, IDataRepository
{
private readonly DataRepositoryOptions _options = options ?? new DataRepositoryOptions();
private IStorage? _storage = storage;
private IStorage Storage => _storage ??
throw new InvalidOperationException(
"Failed to initialize storage. No IStorage utility found in context.");
///
/// 异步加载指定类型的数据
///
/// 要加载的数据类型,必须实现IData接口
/// 加载的数据对象
public async Task LoadAsync() where T : class, IData, new()
{
var key = GetKey();
T result;
// 检查存储中是否存在指定键的数据
if (await Storage.ExistsAsync(key))
result = await Storage.ReadAsync(key);
else
result = new T();
// 如果启用事件功能,则发送数据加载完成事件
if (_options.EnableEvents)
this.SendEvent(new DataLoadedEvent(result));
return result;
}
///
/// 异步加载指定类型的数据(通过Type参数)
///
/// 要加载的数据类型
/// 加载的数据对象
public async Task LoadAsync(Type type)
{
if (!typeof(IData).IsAssignableFrom(type))
throw new ArgumentException($"{type.Name} does not implement IData");
if (!type.IsClass || type.GetConstructor(Type.EmptyTypes) == null)
throw new ArgumentException($"{type.Name} must be a class with parameterless constructor");
var key = GetKey(type);
IData result;
// 检查存储中是否存在指定键的数据
if (await Storage.ExistsAsync(key))
result = await Storage.ReadAsync(key);
else
result = (IData)Activator.CreateInstance(type)!;
// 如果启用事件功能,则发送数据加载完成事件
if (_options.EnableEvents)
this.SendEvent(new DataLoadedEvent(result));
return result;
}
///
/// 异步保存指定类型的数据
///
/// 要保存的数据类型
/// 要保存的数据对象
public async Task SaveAsync(T data) where T : class, IData
{
var key = GetKey();
// 自动备份
if (_options.AutoBackup && await Storage.ExistsAsync(key))
{
var backupKey = $"{key}.backup";
var existing = await Storage.ReadAsync(key);
await Storage.WriteAsync(backupKey, existing);
}
await Storage.WriteAsync(key, data);
if (_options.EnableEvents)
this.SendEvent(new DataSavedEvent(data));
}
///
/// 检查指定类型的数据是否存在
///
/// 要检查的数据类型
/// 如果数据存在返回true,否则返回false
public async Task ExistsAsync() where T : class, IData
{
var key = GetKey();
return await Storage.ExistsAsync(key);
}
///
/// 异步删除指定类型的数据
///
/// 要删除的数据类型
public async Task DeleteAsync() where T : class, IData
{
var key = GetKey();
await Storage.DeleteAsync(key);
if (_options.EnableEvents)
this.SendEvent(new DataDeletedEvent(typeof(T)));
}
///
/// 批量异步保存多个数据对象
///
/// 要保存的数据对象集合
public async Task SaveAllAsync(IEnumerable dataList)
{
var list = dataList.ToList();
foreach (var data in list)
{
var type = data.GetType();
var key = GetKey(type);
await Storage.WriteAsync(key, data);
}
if (_options.EnableEvents)
this.SendEvent(new DataBatchSavedEvent(list));
}
protected override void OnInit()
{
_storage ??= this.GetUtility()!;
}
///
/// 根据类型生成存储键
///
/// 数据类型
/// 生成的存储键
protected virtual string GetKey() where T : IData
{
return GetKey(typeof(T));
}
///
/// 根据类型生成存储键
///
/// 数据类型
/// 生成的存储键
protected virtual string GetKey(Type type)
{
var fileName = type.FullName!;
return string.IsNullOrEmpty(_options.BasePath) ? fileName : $"{_options.BasePath}/{fileName}";
}
}