diff --git a/.gitignore b/.gitignore index 2100bbd..04ad2a9 100644 --- a/.gitignore +++ b/.gitignore @@ -1,4 +1,5 @@ # Godot 4+ specific ignores .godot/ export_presets.cfg -.idea/ \ No newline at end of file +.idea/ +.vs/ \ No newline at end of file diff --git a/ColdMint.Traveler.csproj b/ColdMint.Traveler.csproj index 724a02b..81e4509 100644 --- a/ColdMint.Traveler.csproj +++ b/ColdMint.Traveler.csproj @@ -7,8 +7,4 @@ ColdMint enable - - - - \ No newline at end of file diff --git a/scripts/Config.cs b/scripts/Config.cs index 3dbcd58..1bb80c4 100644 --- a/scripts/Config.cs +++ b/scripts/Config.cs @@ -1,6 +1,5 @@ using System; using System.IO; -using ColdMint.scripts.dataPack; using Godot; using Environment = System.Environment; @@ -85,18 +84,7 @@ public static class Config /// 公司/创作者名字 /// public const string CompanyName = "ColdMint"; - - /// - /// An empty namespace - /// 空的命名空间 - /// - public const string EmptyNamespace = "Empty"; - - /// - /// The default namespace of the packet - /// 数据包的默认命名空间 - /// - public const string DefaultNamespace = "traveler"; + /// /// UserID @@ -119,66 +107,7 @@ public static class Config ///在禁用版本隔离时用的 /// public const string DefaultVersionName = "Default"; - - public const string DataPackDirectoryName = "DataPacks"; - public const string CacheDirectoryName = "Caches"; - public const string DataBaseDirectoryName = "DataBases"; - - /// - /// The starting path of the item data - /// 物品数据的起始路径 - /// - public const string ItemStartPathName = "items"; - - public const string SpriteStartPathName = "sprites"; - - /// - /// The format of the source file inside the packet - /// 数据包内的源文件格式 - /// - public const string DataPackSourceFileFomat = ".json"; - - /// - /// The path symbol inside the compressed package - /// 压缩包内部的路径符号 - /// - public const char ZipPathCharacter = '/'; - - /// - /// Gets the packet directory - /// 获取数据包目录 - /// - /// - public static string GetDataPackDirectory() - { - return Path.Join(GetGameDataDirectory(), DataPackDirectoryName); - } - - /// - /// Gets the packet cache directory - /// 获取数据包缓存目录 - /// - /// - /// - public static string GetDataPackCacheDirectory(string namespaceStr) - { - var path = Path.Join(GetGameDataDirectory(), CacheDirectoryName, DataPackDirectoryName, namespaceStr); - if (!Directory.Exists(path)) - { - Directory.CreateDirectory(path); - } - return path; - } - - /// - /// Get database directory - /// 获取数据库目录 - /// - /// - public static string GetDataBaseDirectory() - { - return Path.Join(GetGameDataDirectory(), DataBaseDirectoryName); - } + /// /// GetGameDataDirectory diff --git a/scripts/character/Player.cs b/scripts/character/Player.cs index ec15f35..46a1085 100644 --- a/scripts/character/Player.cs +++ b/scripts/character/Player.cs @@ -7,12 +7,8 @@ using System.Threading.Tasks; using ColdMint.scripts; using ColdMint.scripts.character; using ColdMint.scripts.damage; -using ColdMint.scripts.database; -using ColdMint.scripts.debug; -using ColdMint.scripts.inventory; using ColdMint.scripts.utils; using ColdMint.scripts.weapon; -using Microsoft.EntityFrameworkCore; /// /// 玩家角色 @@ -282,20 +278,6 @@ public partial class Player : CharacterTemplate var success = PickItem(PickAbleItem); if (success) { - //在背包内添加物品 - var dataPackDbContext = DataBaseManager.GetRequiredService(); - var itemInfoDbSet = dataPackDbContext.ItemInfo; - var query = from itemInfoData in itemInfoDbSet - where itemInfoData.Id == "staffOfTheDead" && itemInfoData.Namespace == Config.DefaultNamespace - select itemInfoData; - var itemInfo = await query.FirstOrDefaultAsync(); - if (itemInfo != null) - { - var item = new LocalItem(itemInfo); - await item.LoadIcon(); - GameSceneNodeHolder.HotBar.AddItem(item); - } - PickAbleItem = null; TotalNumberOfPickups--; if (FloatLabel != null) diff --git a/scripts/dataPack/DataPackManager.cs b/scripts/dataPack/DataPackManager.cs deleted file mode 100644 index b41fe52..0000000 --- a/scripts/dataPack/DataPackManager.cs +++ /dev/null @@ -1,81 +0,0 @@ -using System; -using System.Collections.Generic; -using System.IO; -using System.Linq; -using System.Threading.Tasks; -using System.Transactions; -using ColdMint.scripts.database; -using ColdMint.scripts.database.dataPackEntity; -using ColdMint.scripts.dataPack.local; -using ColdMint.scripts.debug; -using ColdMint.scripts.inventory; -using Microsoft.EntityFrameworkCore.Storage; -using Array = System.Array; - -namespace ColdMint.scripts.dataPack; - -/// -/// Packet manager -/// 数据包管理器 -/// -public static class DataPackManager -{ - /// - /// When a packet is scanned - /// 当一个数据包被扫描到时 - /// - public static Action? OnScanComplete; - - - /// - /// Load all packets in a directory - /// 加载某个目录下的所有数据包 - /// - /// - public static async Task ScanAllDataPack(string path) - { - if (!Directory.Exists(path)) - { - return Array.Empty(); - } - - var dataPacks = new List(); - var files = Directory.GetFiles(path); - foreach (var file in files) - { - var dataPack = await ScanSingleDataPack(file); - if (dataPack == null) - { - continue; - } - - dataPacks.Add(dataPack); - } - - return dataPacks.ToArray(); - } - - /// - /// Load a single packet - /// 加载单个数据包 - /// - /// - /// - public async static Task ScanSingleDataPack(string path) - { - if (!File.Exists(path)) - { - return null; - } - - var dataPack = new LocalDataPack(path); - await dataPack.BuildIndex(); - await dataPack.LoadManifest(); - if (OnScanComplete != null) - { - OnScanComplete.Invoke(dataPack); - } - - return dataPack; - } -} diff --git a/scripts/dataPack/EmptyManifest.cs b/scripts/dataPack/EmptyManifest.cs deleted file mode 100644 index 3dbf1f2..0000000 --- a/scripts/dataPack/EmptyManifest.cs +++ /dev/null @@ -1,40 +0,0 @@ -using Godot; - -namespace ColdMint.scripts.dataPack; - -public class EmptyManifest : IDataPackManifest -{ - - private EmptyManifest() - { - - } - - public string? ID { get; set; } - public string? Name { get; set; } - public string? Description { get; set; } - public string? VersionName { get; set; } - public int? VersionCode { get; set; } - public string? Author { get; set; } - public string? Namespace { get; set; } - - /// - /// Create an empty manifest file - /// 创建空的清单文件 - /// - /// - /// - public static EmptyManifest CreateEmptyManifest(string id) - { - var emptyManifest = new EmptyManifest(); - var unknown = TranslationServer.Translate("unknown"); - emptyManifest.ID = id; - emptyManifest.Author = unknown; - emptyManifest.Name = unknown; - emptyManifest.Description = unknown; - emptyManifest.VersionName = unknown; - emptyManifest.Namespace = Config.EmptyNamespace; - emptyManifest.VersionCode = 0; - return emptyManifest; - } -} \ No newline at end of file diff --git a/scripts/dataPack/IDataPack.cs b/scripts/dataPack/IDataPack.cs deleted file mode 100644 index 68a5ca4..0000000 --- a/scripts/dataPack/IDataPack.cs +++ /dev/null @@ -1,17 +0,0 @@ -namespace ColdMint.scripts.dataPack; - -/// -/// DataPack -/// 数据包 -/// -public interface IDataPack -{ - IDataPackManifest Manifest { get; } - - /// - /// Get the item's data - /// 获取物品的数据 - /// - /// - string GetItemsData(); -} \ No newline at end of file diff --git a/scripts/dataPack/IDataPackManifest.cs b/scripts/dataPack/IDataPackManifest.cs deleted file mode 100644 index 09d6020..0000000 --- a/scripts/dataPack/IDataPackManifest.cs +++ /dev/null @@ -1,16 +0,0 @@ -namespace ColdMint.scripts.dataPack; - -/// -/// DataPackManifest -/// 数据包清单文件 -/// -public interface IDataPackManifest -{ - string? ID { get; set; } - string? Name { get; set; } - string? Description { get; set; } - string? VersionName { get; set; } - int? VersionCode { get; set; } - string? Author { get; set; } - string? Namespace { get; set; } -} \ No newline at end of file diff --git a/scripts/dataPack/entryLoader/DataPackManifestLoader.cs b/scripts/dataPack/entryLoader/DataPackManifestLoader.cs deleted file mode 100644 index 33899f6..0000000 --- a/scripts/dataPack/entryLoader/DataPackManifestLoader.cs +++ /dev/null @@ -1,95 +0,0 @@ -using System; -using System.IO; -using System.IO.Compression; -using System.Linq; -using System.Threading.Tasks; -using ColdMint.scripts.database; -using ColdMint.scripts.database.dataPackEntity; -using ColdMint.scripts.dataPack.local; -using ColdMint.scripts.serialization; -using Microsoft.EntityFrameworkCore; - -namespace ColdMint.scripts.dataPack.entryLoader; - -/// -/// Load the manifest file in the zip package and write it to the data table -/// 在zip包内加载清单文件,将其写入数据表 -/// -public class DataPackManifestLoader : IEntryLoader -{ - public string Namespace => _namespace; - - //清单文件的命名空间 - private string _namespace; - - public bool NeedLoad(ZipArchiveEntry archiveEntry) - { - return archiveEntry.FullName == Config.DataPackManifestName; - } - - public async Task ExecutionLoad(string namespaceString, string zipFileName, DataPackDbContext dataPackDbContext, - ZipArchiveEntry archiveEntry) - { - //Do not use namespaceString within the DataPackManifestLoader's ExecutionLoad method, as this value is assigned in the following code. - //不要在DataPackManifestLoader的ExecutionLoad方法内使用namespaceString,因为这个值是在下面的代码内赋值的。 - var nowDateTime = DateTime.Now; - IDataPackManifest? dataPackManifest = null; - //When the manifest file is obtained, load the file information - //在获取到清单文件时,加载文件信息 - await using (var stream = archiveEntry.Open()) - { - var localDataPackManifest = - await JsonSerialization.ReadJsonFileToObj(stream); - if (localDataPackManifest == null) - { - dataPackManifest = EmptyManifest.CreateEmptyManifest(zipFileName); - } - else - { - dataPackManifest = localDataPackManifest; - } - } - - if (dataPackManifest != null) - { - var dataPackInfoDbSet = dataPackDbContext.DataPackInfo; - var dataPackQuery = from dataPack in dataPackInfoDbSet - where dataPack.ZipFileName == zipFileName - select dataPack; - var oldDataPackInfo = await dataPackQuery.FirstOrDefaultAsync(); - if (oldDataPackInfo == null) - { - //There was no list to record before, create one. - //之前没有清单记录,创建一份。 - await dataPackDbContext.DataPackInfo.AddAsync(new DataPackInfo - { - ID = dataPackManifest.ID, - Author = dataPackManifest.Author, - Description = dataPackManifest.Description, - Name = dataPackManifest.Name, - Namespace = dataPackManifest.Namespace, - VersionCode = dataPackManifest.VersionCode, - VersionName = dataPackManifest.VersionName, - ZipFileName = zipFileName, - UpdateTime = nowDateTime, - CrateTime = nowDateTime - }); - } - else - { - //It's already on the record. Update. - //已经有记录了,更新。 - oldDataPackInfo.Name = dataPackManifest.Name; - oldDataPackInfo.Author = dataPackManifest.Author; - oldDataPackInfo.Description = dataPackManifest.Description; - oldDataPackInfo.Namespace = dataPackManifest.Namespace; - oldDataPackInfo.VersionCode = dataPackManifest.VersionCode; - oldDataPackInfo.VersionName = dataPackManifest.VersionName; - oldDataPackInfo.UpdateTime = nowDateTime; - dataPackDbContext.DataPackInfo.Update(oldDataPackInfo); - } - - _namespace = dataPackManifest.Namespace ?? string.Empty; - } - } -} \ No newline at end of file diff --git a/scripts/dataPack/entryLoader/IEntryLoader.cs b/scripts/dataPack/entryLoader/IEntryLoader.cs deleted file mode 100644 index 5ca6bd5..0000000 --- a/scripts/dataPack/entryLoader/IEntryLoader.cs +++ /dev/null @@ -1,31 +0,0 @@ -using System.IO.Compression; -using System.Threading.Tasks; -using ColdMint.scripts.database; - -namespace ColdMint.scripts.dataPack.entryLoader; - -public interface IEntryLoader -{ - /// - /// Whether to load - /// 是否需要加载 - /// - /// - /// - bool NeedLoad(ZipArchiveEntry archiveEntry); - - - /// - /// Execution load - /// 执行加载 - /// - /// - ///It is only necessary to add or update data to dataPackDbContext in this method. When the scan is completed, the upper layer code will be uniformly submitted to the database - ///仅需要在此方法内将数据add或者update到dataPackDbContext内,当扫描结束后,上层代码会统一提交到数据库 - /// Do not query the existence of the old project from the database within this method, because the save request is also submitted to the database. - /// 不要在此方法内从数据库查询旧的项目是否存在,因为还为向数据库提交保存请求。 - /// - /// - Task ExecutionLoad(string namespaceString, string zipFileName, DataPackDbContext dataPackDbContext, - ZipArchiveEntry archiveEntry); -} \ No newline at end of file diff --git a/scripts/dataPack/entryLoader/ItemLoader.cs b/scripts/dataPack/entryLoader/ItemLoader.cs deleted file mode 100644 index 65d6435..0000000 --- a/scripts/dataPack/entryLoader/ItemLoader.cs +++ /dev/null @@ -1,66 +0,0 @@ -using System; -using System.Collections.Generic; -using System.IO.Compression; -using System.Linq; -using System.Threading.Tasks; -using ColdMint.scripts.database; -using ColdMint.scripts.database.dataPackEntity; -using ColdMint.scripts.debug; -using ColdMint.scripts.serialization; - -namespace ColdMint.scripts.dataPack.entryLoader; - -/// -/// Load item information into the data table in the manifest file -/// 在清单文件内加载物品信息到数据表 -/// -public class ItemLoader : IEntryLoader -{ - private HashSet _itemIdSet = new HashSet(); - - public bool NeedLoad(ZipArchiveEntry archiveEntry) - { - return archiveEntry.FullName.StartsWith(Config.ItemStartPathName) && - archiveEntry.FullName.EndsWith(Config.DataPackSourceFileFomat); - } - - public async Task ExecutionLoad(string namespaceString, string zipFileName, DataPackDbContext dataPackDbContext, - ZipArchiveEntry archiveEntry) - { - await using var stream = archiveEntry.Open(); - //从文件中读取物品信息 - var itemInfo = await JsonSerialization.ReadJsonFileToObj(stream); - if (itemInfo == null) - { - return; - } - - if (_itemIdSet.Contains(itemInfo.Id)) - { - LogCat.LogErrorWithFormat("duplicate_at_path_id", zipFileName, archiveEntry.FullName, itemInfo.Id); - return; - } - - if (itemInfo.MaxStackQuantity <= 0 || itemInfo.MaxStackQuantity > Config.MaxStackQuantity) - { - itemInfo.MaxStackQuantity = Config.MaxStackQuantity; - } - - if (itemInfo.Quantity <= 0) - { - itemInfo.Quantity = 1; - } - - if (itemInfo.Quantity > Config.MaxStackQuantity) - { - itemInfo.Quantity = Config.MaxStackQuantity; - } - - itemInfo.Namespace = namespaceString; - var itemDbSet = dataPackDbContext.ItemInfo; - itemInfo.ZipFileName = zipFileName; - itemInfo.CrateTime = DateTime.Now; - await itemDbSet.AddAsync(itemInfo); - _itemIdSet.Add(itemInfo.Id); - } -} \ No newline at end of file diff --git a/scripts/dataPack/entryLoader/SpriteLoader.cs b/scripts/dataPack/entryLoader/SpriteLoader.cs deleted file mode 100644 index 10109b8..0000000 --- a/scripts/dataPack/entryLoader/SpriteLoader.cs +++ /dev/null @@ -1,45 +0,0 @@ -using System; -using System.Collections.Generic; -using System.IO; -using System.IO.Compression; -using System.Linq; -using System.Threading.Tasks; -using ColdMint.scripts.database; -using ColdMint.scripts.database.dataPackEntity; -using ColdMint.scripts.debug; - -namespace ColdMint.scripts.dataPack.entryLoader; - -public class SpriteLoader : IEntryLoader -{ - private HashSet _spriteNameSet = new HashSet(); - - public bool NeedLoad(ZipArchiveEntry archiveEntry) - { - return archiveEntry.FullName.StartsWith(Config.SpriteStartPathName); - } - - public async Task ExecutionLoad(string namespaceString, string zipFileName, DataPackDbContext dataPackDbContext, - ZipArchiveEntry archiveEntry) - { - var fileName = Path.GetFileNameWithoutExtension(archiveEntry.FullName); - if (_spriteNameSet.Contains(fileName)) - { - LogCat.LogErrorWithFormat("duplicate_at_path_id", zipFileName, archiveEntry.FullName, fileName); - return; - } - - var spriteDbSet = dataPackDbContext.SpriteInfo; - //如果没有记录,创建一份。 - var spriteInfo = new SpriteInfo - { - FileName = fileName, - Namespace = namespaceString, - FullName = archiveEntry.FullName, - ZipFileName = zipFileName, - CrateTime = DateTime.Now - }; - await spriteDbSet.AddAsync(spriteInfo); - _spriteNameSet.Add(fileName); - } -} \ No newline at end of file diff --git a/scripts/dataPack/local/LocalDataPack.cs b/scripts/dataPack/local/LocalDataPack.cs deleted file mode 100644 index 1846230..0000000 --- a/scripts/dataPack/local/LocalDataPack.cs +++ /dev/null @@ -1,194 +0,0 @@ -using System; -using System.Collections.Generic; -using System.Diagnostics.CodeAnalysis; -using System.IO; -using System.IO.Compression; -using System.Linq; -using System.Security.Cryptography; -using System.Text; -using System.Threading.Tasks; -using ColdMint.scripts.database; -using ColdMint.scripts.database.dataPackEntity; -using ColdMint.scripts.dataPack.entryLoader; -using ColdMint.scripts.debug; -using ColdMint.scripts.serialization; -using ColdMint.scripts.utils; -using Godot; -using Microsoft.EntityFrameworkCore; - -namespace ColdMint.scripts.dataPack.local; - -/// -/// LocalDataPack -/// 本地数据包 -/// -public class LocalDataPack : IDataPack -{ - private string zipFilePath; - private string zipFileName; - private IDataPackManifest? manifest; - - public LocalDataPack(string zipFilePath) - { - this.zipFilePath = zipFilePath; - zipFileName = Path.GetFileName(zipFilePath); - } - - - /// - /// Create index information for packets in the database - /// 在数据库内为数据包创建索引信息 - /// - public async Task BuildIndex() - { - //我们首先根据文件名在数据表内查找对应的Md5值,在判断Md5值是否发生变化。 - var entryLoaders = new List(); - entryLoaders.Add(new ItemLoader()); - entryLoaders.Add(new SpriteLoader()); - var md5 = Md5Utils.GetFileMd5(zipFilePath); - var dataPackDbContext = DataBaseManager.GetRequiredService(); - var zipFileInfoDbSet = dataPackDbContext.ZipFileInfo; - var query = from zipFileInfo in zipFileInfoDbSet - where zipFileInfo.ZipFileName == zipFileName - select zipFileInfo; - var oldZipFileInfo = await query.FirstOrDefaultAsync(); - if (oldZipFileInfo == null || oldZipFileInfo.ZipFileMd5 != md5) - { - //Get the list file GetEntry internal Dictionary based implementation, very fast - //获取清单文件 GetEntry内部基于Dictionary实现,速度很快 - //If there is no manifest file, we do not scan the zip and only save the Md5 value for next check - //如果没有清单文件,我们不扫描zip,仅保存Md5值,以便下次检查 - using var archive = ZipFile.Open(zipFilePath, ZipArchiveMode.Read, Encoding.GetEncoding("GBK")); - var manifestEntry = archive.GetEntry(Config.DataPackManifestName); - if (manifestEntry == null) - { - LogCat.LogErrorWithFormat("no_manifest_file", zipFilePath); - } - else - { - var dataPackManifestLoader = new DataPackManifestLoader(); - await dataPackManifestLoader.ExecutionLoad(string.Empty, zipFileName, dataPackDbContext, manifestEntry); - LogCat.LogWithFormat("build_an_index", zipFilePath); - var zipEntryInfoDbSet = dataPackDbContext.ZipEntryInfo; - if (oldZipFileInfo != null) - { - //Delete old records that are outdated - //删除过时的旧记录 - var entriesToDelete = zipEntryInfoDbSet - .Where(entry => entry.FileName == zipFileName) - .ToList(); - dataPackDbContext.ZipEntryInfo.RemoveRange(entriesToDelete); - - var itemsToDelete = dataPackDbContext.ItemInfo - .Where(item => item.ZipFileName == zipFileName) - .ToList(); - dataPackDbContext.ItemInfo.RemoveRange(itemsToDelete); - - var spritesToDelete = dataPackDbContext.SpriteInfo - .Where(sprite => sprite.ZipFileName == zipFileName) - .ToList(); - dataPackDbContext.SpriteInfo.RemoveRange(spritesToDelete); - await dataPackDbContext.SaveChangesAsync(); - } - - foreach (var entry in archive.Entries) - { - if (entry.FullName.EndsWith(Config.ZipPathCharacter)) - { - //Ignore folders - //忽略文件夹 - continue; - } - - var nowDateTime = DateTime.Now; - foreach (var entryLoader in entryLoaders) - { - var needLoad = entryLoader.NeedLoad(entry); - if (needLoad) - { - await entryLoader.ExecutionLoad(dataPackManifestLoader.Namespace, zipFileName, - dataPackDbContext, entry); - } - } - - var zipEntryInfo = new ZipEntryInfo - { - FullName = entry.FullName, - FileName = zipFileName, - CrateTime = nowDateTime - }; - LogCat.LogWithFormat("add_file_index", entry.FullName); - await zipEntryInfoDbSet.AddAsync(zipEntryInfo); - } - } - - if (oldZipFileInfo == null) - { - //创建一份 - var zipNowDateTime = DateTime.Now; - var zipFileInfo = new ZipFileInfo - { - ZipFileName = zipFileName, - ZipFileMd5 = md5, - EntryCount = archive.Entries.Count, - CrateTime = zipNowDateTime, - UpdateTime = zipNowDateTime - }; - dataPackDbContext.ZipFileInfo.Add(zipFileInfo); - } - else - { - var zipNowDateTime = DateTime.Now; - oldZipFileInfo.UpdateTime = zipNowDateTime; - oldZipFileInfo.ZipFileMd5 = md5; - oldZipFileInfo.EntryCount = archive.Entries.Count; - dataPackDbContext.Update(oldZipFileInfo); - } - - try - { - await dataPackDbContext.SaveChangesAsync(); - } - catch (Exception e) - { - LogCat.LogError(e); - } - - LogCat.LogWithFormat("index_updated", zipFilePath); - return; - } - - //没有变化什么也不做 - LogCat.LogWithFormat("index_is_up_to_date", zipFilePath); - } - - /// - /// Load manifest file - /// 加载清单文件 - /// - public async Task LoadManifest() - { - var dataPackDbContext = DataBaseManager.GetRequiredService(); - var dataPackInfoDbSet = dataPackDbContext.DataPackInfo; - var dataPackInfo = from dataPack in dataPackInfoDbSet - where dataPack.ZipFileName == zipFileName - select dataPack; - if (dataPackInfo != null) - { - manifest = await dataPackInfo.FirstOrDefaultAsync(); - } - } - - - public IDataPackManifest Manifest => manifest ?? EmptyManifest.CreateEmptyManifest(zipFileName); - - /// - /// Get the item's data - /// 获取物品的数据 - /// - /// - public string GetItemsData() - { - return Path.Join(zipFilePath, "items"); - } -} \ No newline at end of file diff --git a/scripts/dataPack/local/LocalDataPackManifest.cs b/scripts/dataPack/local/LocalDataPackManifest.cs deleted file mode 100644 index 1799c1c..0000000 --- a/scripts/dataPack/local/LocalDataPackManifest.cs +++ /dev/null @@ -1,18 +0,0 @@ -using System.IO; -using System.Linq; -using System.Text.Json; -using System.Threading.Tasks; -using ColdMint.scripts.serialization; - -namespace ColdMint.scripts.dataPack.local; - -public class LocalDataPackManifest: IDataPackManifest -{ - public string? ID { get; set; } - public string? Name { get; set; } - public string? Description { get; set; } - public string? VersionName { get; set; } - public int? VersionCode { get; set; } - public string? Author { get; set; } - public string? Namespace { get; set; } -} \ No newline at end of file diff --git a/scripts/database/DataBaseManager.cs b/scripts/database/DataBaseManager.cs deleted file mode 100644 index 9d6e86c..0000000 --- a/scripts/database/DataBaseManager.cs +++ /dev/null @@ -1,48 +0,0 @@ -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(); - } -} \ No newline at end of file diff --git a/scripts/database/DataPackDbContext.cs b/scripts/database/DataPackDbContext.cs deleted file mode 100644 index b87560c..0000000 --- a/scripts/database/DataPackDbContext.cs +++ /dev/null @@ -1,21 +0,0 @@ -using ColdMint.scripts.database.dataPackEntity; -using ColdMint.scripts.debug; -using Microsoft.EntityFrameworkCore; -using Microsoft.Extensions.DependencyInjection; - -namespace ColdMint.scripts.database; - -public class DataPackDbContext : DbContext -{ - public DbSet DataPackInfo { get; set; } - - public DbSet ZipEntryInfo { get; set; } - public DbSet ZipFileInfo { get; set; } - - public DbSet ItemInfo { get; set; } - public DbSet SpriteInfo { get; set; } - - public DataPackDbContext(DbContextOptions options) : base(options) - { - } -} \ No newline at end of file diff --git a/scripts/database/dataPackEntity/DataPackInfo.cs b/scripts/database/dataPackEntity/DataPackInfo.cs deleted file mode 100644 index 81c9597..0000000 --- a/scripts/database/dataPackEntity/DataPackInfo.cs +++ /dev/null @@ -1,27 +0,0 @@ -using System; -using System.ComponentModel.DataAnnotations; -using ColdMint.scripts.dataPack; - -namespace ColdMint.scripts.database.dataPackEntity; - -public class DataPackInfo : IDataPackManifest -{ - [Key] public string? ID { get; set; } - public string? Name { get; set; } - public string? Description { get; set; } - public string? VersionName { get; set; } - public int? VersionCode { get; set; } - public string? Author { get; set; } - public string? Namespace { get; set; } - - /// - /// Whether the status is enabled - /// 是否为启用状态 - /// - public bool IsEnabled { get; set; } - - public string ZipFileName { get; set; } - - public DateTime CrateTime { get; set; } - public DateTime UpdateTime { get; set; } -} \ No newline at end of file diff --git a/scripts/database/dataPackEntity/ItemInfo.cs b/scripts/database/dataPackEntity/ItemInfo.cs deleted file mode 100644 index 23d9f02..0000000 --- a/scripts/database/dataPackEntity/ItemInfo.cs +++ /dev/null @@ -1,26 +0,0 @@ -using System; -using System.ComponentModel.DataAnnotations; -using System.ComponentModel.DataAnnotations.Schema; -using ColdMint.scripts.inventory; -using Godot; - -namespace ColdMint.scripts.database.dataPackEntity; - -public class ItemInfo -{ - [Key] - [DatabaseGenerated(DatabaseGeneratedOption.Identity)] - public int Index { get; set; } - - public string Id { get; set; } - public int Quantity { get; set; } - public int MaxStackQuantity { get; set; } - public string? Icon { get; set; } - public string Name { get; set; } - public string? Description { get; set; } - public string ZipFileName { get; set; } - - public string Namespace { get; set; } - - public DateTime CrateTime { get; set; } -} \ No newline at end of file diff --git a/scripts/database/dataPackEntity/SpriteInfo.cs b/scripts/database/dataPackEntity/SpriteInfo.cs deleted file mode 100644 index 53a5bfe..0000000 --- a/scripts/database/dataPackEntity/SpriteInfo.cs +++ /dev/null @@ -1,26 +0,0 @@ -using System; -using System.ComponentModel.DataAnnotations; -using System.ComponentModel.DataAnnotations.Schema; - -namespace ColdMint.scripts.database.dataPackEntity; - -public class SpriteInfo -{ - [Key] - [DatabaseGenerated(DatabaseGeneratedOption.Identity)] - public int Index { get; set; } - - /// - /// The file name is the Id of the Sprite - /// 文件名就是精灵的Id - /// - public string FileName { get; set; } - - public string FullName { get; set; } - - public string ZipFileName { get; set; } - - public string Namespace { get; set; } - - public DateTime CrateTime { get; set; } -} \ No newline at end of file diff --git a/scripts/database/dataPackEntity/ZipEntryInfo.cs b/scripts/database/dataPackEntity/ZipEntryInfo.cs deleted file mode 100644 index 58d8853..0000000 --- a/scripts/database/dataPackEntity/ZipEntryInfo.cs +++ /dev/null @@ -1,35 +0,0 @@ -using System; -using System.ComponentModel.DataAnnotations; -using System.ComponentModel.DataAnnotations.Schema; - -namespace ColdMint.scripts.database.dataPackEntity; - -/// -/// entry table in Zip file -/// Zip文件内的entry表 -/// -public class ZipEntryInfo -{ - /// - /// Primary key, auto increment - /// 主键,自动递增 - /// - [Key] - [DatabaseGenerated(DatabaseGeneratedOption.Identity)] - public int id { get; set; } - - /// - /// This record is from that zip file - /// 这段记录是来源于那个zip文件的 - /// - public string FileName { get; set; } - - /// - /// The path within the zip file - /// 位于zip文件内的路径 - /// - public string FullName { get; set; } - - public DateTime CrateTime { get; set; } - -} \ No newline at end of file diff --git a/scripts/database/dataPackEntity/ZipFileInfo.cs b/scripts/database/dataPackEntity/ZipFileInfo.cs deleted file mode 100644 index 35997bf..0000000 --- a/scripts/database/dataPackEntity/ZipFileInfo.cs +++ /dev/null @@ -1,15 +0,0 @@ -using System; -using System.ComponentModel.DataAnnotations; - -namespace ColdMint.scripts.database.dataPackEntity; - -public class ZipFileInfo -{ - [Key] public string ZipFileName { get; set; } - - public string ZipFileMd5 { get; set; } - - public int EntryCount { get; set; } - public DateTime CrateTime { get; set; } - public DateTime UpdateTime { get; set; } -} \ No newline at end of file diff --git a/scripts/inventory/LocalItem.cs b/scripts/inventory/LocalItem.cs deleted file mode 100644 index 531b81b..0000000 --- a/scripts/inventory/LocalItem.cs +++ /dev/null @@ -1,91 +0,0 @@ -using System; -using System.IO; -using System.IO.Compression; -using System.Linq; -using System.Text; -using System.Threading.Tasks; -using ColdMint.scripts.database; -using ColdMint.scripts.database.dataPackEntity; -using ColdMint.scripts.debug; -using ColdMint.scripts.serialization; -using Godot; -using Microsoft.EntityFrameworkCore; - -namespace ColdMint.scripts.inventory; - -/// -/// Local Item -/// 本地Item -/// -public class LocalItem : IItem -{ - private ItemInfo _itemInfo; - private int quantity; - private ImageTexture texture2D; - - public LocalItem(ItemInfo itemInfo) - { - _itemInfo = itemInfo; - quantity = itemInfo.Quantity; - } - - public async Task LoadIcon() - { - if (_itemInfo == null) - { - return; - } - - var icon = _itemInfo.Icon; - if (icon == null) - { - return; - } - - //查找图标路径 - var dataPackDbContext = DataBaseManager.GetRequiredService(); - var spriteDbSet = dataPackDbContext.SpriteInfo; - //在同一个命名空间下查找 - var query = from sprite in spriteDbSet - where sprite.FileName == icon && sprite.Namespace == _itemInfo.Namespace - select sprite; - var spriteInfo = await query.FirstOrDefaultAsync(); - if (spriteInfo == null) - { - return; - } - - var zipFilePath = Path.Join(Config.GetDataPackDirectory(), spriteInfo.ZipFileName); - using var archive = ZipFile.Open(zipFilePath, ZipArchiveMode.Read, Encoding.GetEncoding("GBK")); - var zipArchiveEntry = archive.GetEntry(spriteInfo.FullName); - if (zipArchiveEntry == null) - { - return; - } - - var outPath = Path.Join(Config.GetDataPackCacheDirectory(_itemInfo.Namespace), spriteInfo.FileName+".jpg"); - zipArchiveEntry.ExtractToFile(outPath); - var image = Image.LoadFromFile(outPath); - if (image == null) - { - LogCat.Log("无法加载"+outPath); - return; - } - texture2D = ImageTexture.CreateFromImage(image); - } - - public string Id => _itemInfo.Id; - - public int Quantity - { - get => quantity; - set { quantity = value; } - } - - public int MaxStackQuantity => _itemInfo.MaxStackQuantity; - public Texture2D Icon => texture2D; - public string Name => _itemInfo.Name; - public string Namespace => _itemInfo.Namespace; - public Action OnUse { get; set; } - public Func OnInstantiation { get; set; } -} \ No newline at end of file diff --git a/scripts/loader/uiLoader/MainMenuLoader.cs b/scripts/loader/uiLoader/MainMenuLoader.cs index 388ad5d..155e306 100644 --- a/scripts/loader/uiLoader/MainMenuLoader.cs +++ b/scripts/loader/uiLoader/MainMenuLoader.cs @@ -2,11 +2,8 @@ using System; using System.IO; using System.Text; using ColdMint.scripts.camp; -using ColdMint.scripts.database; -using ColdMint.scripts.dataPack; using ColdMint.scripts.debug; using Godot; -using SQLitePCL; namespace ColdMint.scripts.loader.uiLoader; @@ -35,24 +32,6 @@ public partial class MainMenuLoader : UiLoaderTemplate Directory.CreateDirectory(dataPath); } - //创建数据库文件夹 - var dataBasePath = Config.GetDataBaseDirectory(); - if (!Directory.Exists(dataBasePath)) - { - Directory.CreateDirectory(dataBasePath); - } - DataBaseManager.InitDataBases(dataBasePath); - - //创建数据包文件夹 - var dataPackPath = Config.GetDataPackDirectory(); - if (!Directory.Exists(dataPackPath)) - { - Directory.CreateDirectory(dataPackPath); - } - await DataPackManager.ScanAllDataPack(dataPackPath); - - - //Registered camp //注册阵营 var defaultCamp = new Camp(Config.CampId.Default);