diff --git a/scripts/Config.cs b/scripts/Config.cs index 7c6b15e..3dbcd58 100644 --- a/scripts/Config.cs +++ b/scripts/Config.cs @@ -121,6 +121,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"; /// @@ -153,6 +154,22 @@ public static class Config 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 /// 获取数据库目录 diff --git a/scripts/character/Player.cs b/scripts/character/Player.cs index 8f58828..ec15f35 100644 --- a/scripts/character/Player.cs +++ b/scripts/character/Player.cs @@ -12,6 +12,7 @@ using ColdMint.scripts.debug; using ColdMint.scripts.inventory; using ColdMint.scripts.utils; using ColdMint.scripts.weapon; +using Microsoft.EntityFrameworkCore; /// /// 玩家角色 @@ -287,10 +288,11 @@ public partial class Player : CharacterTemplate var query = from itemInfoData in itemInfoDbSet where itemInfoData.Id == "staffOfTheDead" && itemInfoData.Namespace == Config.DefaultNamespace select itemInfoData; - var itemInfo = query.FirstOrDefault(); + var itemInfo = await query.FirstOrDefaultAsync(); if (itemInfo != null) { var item = new LocalItem(itemInfo); + await item.LoadIcon(); GameSceneNodeHolder.HotBar.AddItem(item); } diff --git a/scripts/dataPack/entryLoader/DataPackManifestLoader.cs b/scripts/dataPack/entryLoader/DataPackManifestLoader.cs index 250462e..33899f6 100644 --- a/scripts/dataPack/entryLoader/DataPackManifestLoader.cs +++ b/scripts/dataPack/entryLoader/DataPackManifestLoader.cs @@ -7,6 +7,7 @@ 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; @@ -55,7 +56,7 @@ public class DataPackManifestLoader : IEntryLoader var dataPackQuery = from dataPack in dataPackInfoDbSet where dataPack.ZipFileName == zipFileName select dataPack; - var oldDataPackInfo = dataPackQuery.FirstOrDefault(); + var oldDataPackInfo = await dataPackQuery.FirstOrDefaultAsync(); if (oldDataPackInfo == null) { //There was no list to record before, create one. diff --git a/scripts/dataPack/local/LocalDataPack.cs b/scripts/dataPack/local/LocalDataPack.cs index 3a01494..1846230 100644 --- a/scripts/dataPack/local/LocalDataPack.cs +++ b/scripts/dataPack/local/LocalDataPack.cs @@ -14,6 +14,7 @@ using ColdMint.scripts.debug; using ColdMint.scripts.serialization; using ColdMint.scripts.utils; using Godot; +using Microsoft.EntityFrameworkCore; namespace ColdMint.scripts.dataPack.local; @@ -50,7 +51,7 @@ public class LocalDataPack : IDataPack var query = from zipFileInfo in zipFileInfoDbSet where zipFileInfo.ZipFileName == zipFileName select zipFileInfo; - var oldZipFileInfo = query.FirstOrDefault(); + var oldZipFileInfo = await query.FirstOrDefaultAsync(); if (oldZipFileInfo == null || oldZipFileInfo.ZipFileMd5 != md5) { //Get the list file GetEntry internal Dictionary based implementation, very fast @@ -82,7 +83,7 @@ public class LocalDataPack : IDataPack .Where(item => item.ZipFileName == zipFileName) .ToList(); dataPackDbContext.ItemInfo.RemoveRange(itemsToDelete); - + var spritesToDelete = dataPackDbContext.SpriteInfo .Where(sprite => sprite.ZipFileName == zipFileName) .ToList(); @@ -174,7 +175,7 @@ public class LocalDataPack : IDataPack select dataPack; if (dataPackInfo != null) { - manifest = dataPackInfo.FirstOrDefault(); + manifest = await dataPackInfo.FirstOrDefaultAsync(); } } diff --git a/scripts/inventory/IItem.cs b/scripts/inventory/IItem.cs index 3b2f71b..f893cc6 100644 --- a/scripts/inventory/IItem.cs +++ b/scripts/inventory/IItem.cs @@ -27,7 +27,7 @@ public interface IItem /// Items can be set with Icon /// 物品可以设置图标 /// - Texture2D Icon { get; set; } + Texture2D Icon { get; } /// /// Item has a name diff --git a/scripts/inventory/LocalItem.cs b/scripts/inventory/LocalItem.cs index d3774e7..531b81b 100644 --- a/scripts/inventory/LocalItem.cs +++ b/scripts/inventory/LocalItem.cs @@ -1,11 +1,15 @@ 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; @@ -17,6 +21,7 @@ public class LocalItem : IItem { private ItemInfo _itemInfo; private int quantity; + private ImageTexture texture2D; public LocalItem(ItemInfo itemInfo) { @@ -24,6 +29,51 @@ public class LocalItem : IItem 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 @@ -33,7 +83,7 @@ public class LocalItem : IItem } public int MaxStackQuantity => _itemInfo.MaxStackQuantity; - public Texture2D Icon { get; set; } + public Texture2D Icon => texture2D; public string Name => _itemInfo.Name; public string Namespace => _itemInfo.Namespace; public Action OnUse { get; set; }