From 56d3ae4964147255f3cc1327871b528638ab45ba Mon Sep 17 00:00:00 2001 From: Cold-Mint Date: Tue, 6 Aug 2024 00:27:51 +0800 Subject: [PATCH] =?UTF-8?q?Restore=20the=20database.=20=E6=81=A2=E5=A4=8D?= =?UTF-8?q?=E6=95=B0=E6=8D=AE=E5=BA=93=E3=80=82?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- ColdMint.Traveler.csproj | 2 + scripts/Config.cs | 22 +++++---- scripts/database/DataBaseManager.cs | 48 +++++++++++++++++++ scripts/database/GameDbContext.cs | 40 ++++++++++++++++ scripts/database/gameDbTables/ErrorRecord.cs | 31 ++++++++++++ scripts/debug/LogCat.cs | 3 ++ scripts/loader/uiLoader/SplashScreenLoader.cs | 8 ++++ 7 files changed, 145 insertions(+), 9 deletions(-) create mode 100644 scripts/database/DataBaseManager.cs create mode 100644 scripts/database/GameDbContext.cs create mode 100644 scripts/database/gameDbTables/ErrorRecord.cs diff --git a/ColdMint.Traveler.csproj b/ColdMint.Traveler.csproj index d911b96..d395554 100644 --- a/ColdMint.Traveler.csproj +++ b/ColdMint.Traveler.csproj @@ -11,5 +11,7 @@ + + \ No newline at end of file diff --git a/scripts/Config.cs b/scripts/Config.cs index 1895228..516705c 100644 --- a/scripts/Config.cs +++ b/scripts/Config.cs @@ -136,13 +136,7 @@ public static class Config /// 快捷栏上有多少个物品槽 /// public const int HotBarSize = 9; - - - /// - /// UserID - /// 用户ID - /// - public const string UserId = "DefaultUser"; + /// /// Whether version isolation is enabled @@ -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); } } + /// + /// GetDataBaseDirectory + /// 获取数据库文件夹 + /// + /// + public static string GetDataBaseDirectory() + { + return Path.Join(GetGameDataDirectory(), "Databases"); + } + /// /// GetModDataDirectory /// 获取模组文件夹 diff --git a/scripts/database/DataBaseManager.cs b/scripts/database/DataBaseManager.cs new file mode 100644 index 0000000..3414944 --- /dev/null +++ b/scripts/database/DataBaseManager.cs @@ -0,0 +1,48 @@ +using System; +using System.IO; + +namespace ColdMint.scripts.database; + +using Microsoft.EntityFrameworkCore; +using Microsoft.Extensions.DependencyInjection; + +/// +/// Game database manager +/// 游戏数据库管理器 +/// +public class DataBaseManager +{ + private static ServiceProvider? _serviceProvider; + + /// + /// Initialize database + /// 初始化数据库 + /// + public static void InitDataBases(string databasePath) + { + var serviceCollection = new ServiceCollection(); + serviceCollection.AddDbContext(options => + options.UseSqlite($"Data Source={Path.Join(databasePath, Config.SolutionName + ".db")}")); + _serviceProvider = serviceCollection.BuildServiceProvider(); + var dataPackDbContext = GetRequiredService(); + dataPackDbContext.Database.EnsureCreated(); + } + + + /// + /// Get database service + /// 获取数据库服务 + /// + /// + /// + public static T GetRequiredService() where T : notnull + { + if (_serviceProvider == null) + { + throw new NullReferenceException("service provider is null"); + } + + var scope = _serviceProvider.CreateScope(); + return scope.ServiceProvider.GetRequiredService(); + } +} \ No newline at end of file diff --git a/scripts/database/GameDbContext.cs b/scripts/database/GameDbContext.cs new file mode 100644 index 0000000..922d056 --- /dev/null +++ b/scripts/database/GameDbContext.cs @@ -0,0 +1,40 @@ +using ColdMint.scripts.database.gameDbTables; +using Microsoft.EntityFrameworkCore; + +namespace ColdMint.scripts.database; + +/// +/// Game database +/// 游戏数据库 +/// +public class GameDbContext(DbContextOptions options) : DbContext(options) +{ + public DbSet ErrorRecords { get; set; } + + /// + /// Async add error record + /// 异步添加错误信息 + /// + /// + 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(); + } +} \ No newline at end of file diff --git a/scripts/database/gameDbTables/ErrorRecord.cs b/scripts/database/gameDbTables/ErrorRecord.cs new file mode 100644 index 0000000..c38bef4 --- /dev/null +++ b/scripts/database/gameDbTables/ErrorRecord.cs @@ -0,0 +1,31 @@ +using System; +using System.ComponentModel.DataAnnotations; + +namespace ColdMint.scripts.database.gameDbTables; + +/// +/// Error record +/// 错误记录 +/// +public class ErrorRecord +{ + /// + /// Message + /// 错误消息 + /// + [Key] + [MaxLength(255)] + public string? Message { get; set; } + + /// + /// Count + /// 出现次数 + /// + public int Count { get; set; } = 1; + + /// + /// DateTime + /// 时间 + /// + public DateTime LastDateTime { get; set; } = DateTime.Now; +} \ No newline at end of file diff --git a/scripts/debug/LogCat.cs b/scripts/debug/LogCat.cs index 2496159..1fe6e02 100644 --- a/scripts/debug/LogCat.cs +++ b/scripts/debug/LogCat.cs @@ -64,6 +64,7 @@ public static class LogCat /// 日志收集器 /// public const string LogCollector = "LogCollector"; + /// /// Mod Loader /// 模组加载器 @@ -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 diff --git a/scripts/loader/uiLoader/SplashScreenLoader.cs b/scripts/loader/uiLoader/SplashScreenLoader.cs index 31a4549..06568ad 100644 --- a/scripts/loader/uiLoader/SplashScreenLoader.cs +++ b/scripts/loader/uiLoader/SplashScreenLoader.cs @@ -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)