Restore the database.

恢复数据库。
This commit is contained in:
Cold-Mint 2024-08-06 00:27:51 +08:00
parent cceda0bd1f
commit 56d3ae4964
Signed by: Cold-Mint
GPG Key ID: C5A9BF8A98E0CE99
7 changed files with 145 additions and 9 deletions

View File

@ -11,5 +11,7 @@
<ItemGroup>
<PackageReference Include="JetBrains.Annotations" Version="2023.3.0" />
<PackageReference Include="YamlDotNet" Version="15.1.6" />
<PackageReference Include="Microsoft.EntityFrameworkCore.Sqlite.Core" Version="6.0.29" />
<PackageReference Include="SQLitePCLRaw.bundle_e_sqlite3" Version="2.1.8" />
</ItemGroup>
</Project>

View File

@ -136,13 +136,7 @@ public static class Config
/// <para>快捷栏上有多少个物品槽</para>
/// </summary>
public const int HotBarSize = 9;
/// <summary>
/// <para>UserID</para>
/// <para>用户ID</para>
/// </summary>
public const string UserId = "DefaultUser";
/// <summary>
/// <para>Whether version isolation is enabled</para>
@ -322,17 +316,27 @@ public static class Config
if (EnableVersionIsolation())
{
return Path.Join(Environment.GetFolderPath(Environment.SpecialFolder.ApplicationData), CompanyName,
ProjectSettings.GetSetting("application/config/name").AsString(), UserId,
ProjectSettings.GetSetting("application/config/name").AsString(),
ProjectSettings.GetSetting("application/config/version").AsString());
}
else
{
return Path.Join(Environment.GetFolderPath(Environment.SpecialFolder.ApplicationData), CompanyName,
ProjectSettings.GetSetting("application/config/name").AsString(), UserId,
ProjectSettings.GetSetting("application/config/name").AsString(),
DefaultVersionName);
}
}
/// <summary>
/// <para>GetDataBaseDirectory</para>
/// <para>获取数据库文件夹</para>
/// </summary>
/// <returns></returns>
public static string GetDataBaseDirectory()
{
return Path.Join(GetGameDataDirectory(), "Databases");
}
/// <summary>
/// <para>GetModDataDirectory</para>
/// <para>获取模组文件夹</para>

View File

@ -0,0 +1,48 @@
using System;
using System.IO;
namespace ColdMint.scripts.database;
using Microsoft.EntityFrameworkCore;
using Microsoft.Extensions.DependencyInjection;
/// <summary>
/// <para>Game database manager</para>
/// <para>游戏数据库管理器</para>
/// </summary>
public class DataBaseManager
{
private static ServiceProvider? _serviceProvider;
/// <summary>
/// <para>Initialize database</para>
/// <para>初始化数据库</para>
/// </summary>
public static void InitDataBases(string databasePath)
{
var serviceCollection = new ServiceCollection();
serviceCollection.AddDbContext<GameDbContext>(options =>
options.UseSqlite($"Data Source={Path.Join(databasePath, Config.SolutionName + ".db")}"));
_serviceProvider = serviceCollection.BuildServiceProvider();
var dataPackDbContext = GetRequiredService<GameDbContext>();
dataPackDbContext.Database.EnsureCreated();
}
/// <summary>
/// <para>Get database service</para>
/// <para>获取数据库服务</para>
/// </summary>
/// <typeparam name="T"></typeparam>
/// <returns></returns>
public static T GetRequiredService<T>() where T : notnull
{
if (_serviceProvider == null)
{
throw new NullReferenceException("service provider is null");
}
var scope = _serviceProvider.CreateScope();
return scope.ServiceProvider.GetRequiredService<T>();
}
}

View File

@ -0,0 +1,40 @@
using ColdMint.scripts.database.gameDbTables;
using Microsoft.EntityFrameworkCore;
namespace ColdMint.scripts.database;
/// <summary>
/// <para>Game database</para>
/// <para>游戏数据库</para>
/// </summary>
public class GameDbContext(DbContextOptions<GameDbContext> options) : DbContext(options)
{
public DbSet<ErrorRecord> ErrorRecords { get; set; }
/// <summary>
/// <para>Async add error record</para>
/// <para>异步添加错误信息</para>
/// </summary>
/// <param name="errorRecord"></param>
public async void AddOrUpdateErrorRecordAsync(ErrorRecord errorRecord)
{
if (errorRecord.Message == null)
{
return;
}
var oldErrorRecord = await ErrorRecords.FindAsync(errorRecord.Message);
if (oldErrorRecord == null)
{
ErrorRecords.Add(errorRecord);
}
else
{
oldErrorRecord.Count++;
oldErrorRecord.LastDateTime = errorRecord.LastDateTime;
ErrorRecords.Update(oldErrorRecord);
}
await SaveChangesAsync();
}
}

View File

@ -0,0 +1,31 @@
using System;
using System.ComponentModel.DataAnnotations;
namespace ColdMint.scripts.database.gameDbTables;
/// <summary>
/// <para>Error record</para>
/// <para>错误记录</para>
/// </summary>
public class ErrorRecord
{
/// <summary>
/// <para>Message</para>
/// <para>错误消息</para>
/// </summary>
[Key]
[MaxLength(255)]
public string? Message { get; set; }
/// <summary>
/// <para>Count</para>
/// <para>出现次数</para>
/// </summary>
public int Count { get; set; } = 1;
/// <summary>
/// <para>DateTime</para>
/// <para>时间</para>
/// </summary>
public DateTime LastDateTime { get; set; } = DateTime.Now;
}

View File

@ -64,6 +64,7 @@ public static class LogCat
/// <para>日志收集器</para>
/// </summary>
public const string LogCollector = "LogCollector";
/// <summary>
/// <para>Mod Loader</para>
/// <para>模组加载器</para>
@ -237,6 +238,8 @@ public static class LogCat
return;
}
//If you need to upload logs, you can upload logs.
//如果需要上传日志,并且能够上传日志。
if (LogCollector.CanUploadLog && upload)
{
var logData = new LogData

View File

@ -3,6 +3,7 @@ using System.Text;
using System.Threading.Tasks;
using ColdMint.scripts.camp;
using ColdMint.scripts.contribute;
using ColdMint.scripts.database;
using ColdMint.scripts.deathInfo;
using ColdMint.scripts.debug;
using ColdMint.scripts.inventory;
@ -96,6 +97,13 @@ public partial class SplashScreenLoader : UiLoaderTemplate
Directory.CreateDirectory(dataPath);
}
var databasePath = Config.GetDataBaseDirectory();
if (!Directory.Exists(databasePath))
{
Directory.CreateDirectory(databasePath);
}
DataBaseManager.InitDataBases(databasePath);
//Registered camp
//注册阵营
var defaultCamp = new Camp(Config.CampId.Default)