2024-04-28 13:55:19 +00:00
|
|
|
|
using System;
|
2024-04-29 13:57:24 +00:00
|
|
|
|
using System.IO;
|
|
|
|
|
using System.IO.Compression;
|
2024-04-28 13:55:19 +00:00
|
|
|
|
using System.Linq;
|
2024-04-29 13:57:24 +00:00
|
|
|
|
using System.Text;
|
2024-04-28 13:55:19 +00:00
|
|
|
|
using System.Threading.Tasks;
|
|
|
|
|
using ColdMint.scripts.database;
|
|
|
|
|
using ColdMint.scripts.database.dataPackEntity;
|
|
|
|
|
using ColdMint.scripts.debug;
|
|
|
|
|
using ColdMint.scripts.serialization;
|
|
|
|
|
using Godot;
|
2024-04-29 13:57:24 +00:00
|
|
|
|
using Microsoft.EntityFrameworkCore;
|
2024-04-28 13:55:19 +00:00
|
|
|
|
|
|
|
|
|
namespace ColdMint.scripts.inventory;
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
/// <para>Local Item</para>
|
|
|
|
|
/// <para>本地Item</para>
|
|
|
|
|
/// </summary>
|
|
|
|
|
public class LocalItem : IItem
|
|
|
|
|
{
|
|
|
|
|
private ItemInfo _itemInfo;
|
|
|
|
|
private int quantity;
|
2024-04-29 13:57:24 +00:00
|
|
|
|
private ImageTexture texture2D;
|
2024-04-28 13:55:19 +00:00
|
|
|
|
|
|
|
|
|
public LocalItem(ItemInfo itemInfo)
|
|
|
|
|
{
|
|
|
|
|
_itemInfo = itemInfo;
|
|
|
|
|
quantity = itemInfo.Quantity;
|
|
|
|
|
}
|
|
|
|
|
|
2024-04-29 13:57:24 +00:00
|
|
|
|
public async Task LoadIcon()
|
|
|
|
|
{
|
|
|
|
|
if (_itemInfo == null)
|
|
|
|
|
{
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
var icon = _itemInfo.Icon;
|
|
|
|
|
if (icon == null)
|
|
|
|
|
{
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
//查找图标路径
|
|
|
|
|
var dataPackDbContext = DataBaseManager.GetRequiredService<DataPackDbContext>();
|
|
|
|
|
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);
|
|
|
|
|
}
|
|
|
|
|
|
2024-04-28 13:55:19 +00:00
|
|
|
|
public string Id => _itemInfo.Id;
|
|
|
|
|
|
|
|
|
|
public int Quantity
|
|
|
|
|
{
|
|
|
|
|
get => quantity;
|
|
|
|
|
set { quantity = value; }
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public int MaxStackQuantity => _itemInfo.MaxStackQuantity;
|
2024-04-29 13:57:24 +00:00
|
|
|
|
public Texture2D Icon => texture2D;
|
2024-04-28 13:55:19 +00:00
|
|
|
|
public string Name => _itemInfo.Name;
|
|
|
|
|
public string Namespace => _itemInfo.Namespace;
|
|
|
|
|
public Action<IItem> OnUse { get; set; }
|
|
|
|
|
public Func<IItem, Node> OnInstantiation { get; set; }
|
|
|
|
|
}
|