using System.IO;
using ColdMint.scripts.database.dataPackEntity;
using ColdMint.scripts.debug;
using Microsoft.EntityFrameworkCore;
using Microsoft.Extensions.DependencyInjection;
namespace ColdMint.scripts.database;
///
/// Game database manager
/// 游戏数据库管理器
///
public static class DataBaseManager
{
private static ServiceProvider serviceProvider;
///
/// Initialize database
/// 初始化数据库
///
public static void InitDataBases(string databasePath)
{
if (!Directory.Exists(databasePath))
{
return;
}
var serviceCollection = new ServiceCollection();
serviceCollection.AddDbContext(options =>
options.UseSqlite($"Data Source={Path.Join(databasePath, "DataPack.db")}"));
serviceProvider = serviceCollection.BuildServiceProvider();
var dataPackDbContext = GetRequiredService();
dataPackDbContext.Database.EnsureCreated();
}
///
/// Get database service
/// 获取数据库服务
///
///
///
public static T GetRequiredService() where T : notnull
{
var scope = serviceProvider.CreateScope();
return scope.ServiceProvider.GetRequiredService();
}
}