Remove packets and databases.

移除数据包和数据库。
This commit is contained in:
Cold-Mint 2024-04-29 22:18:46 +08:00
parent 5bdd68b61f
commit 159d3a7017
Signed by: Cold-Mint
GPG Key ID: C5A9BF8A98E0CE99
23 changed files with 4 additions and 1009 deletions

3
.gitignore vendored
View File

@ -1,4 +1,5 @@
# Godot 4+ specific ignores
.godot/
export_presets.cfg
.idea/
.idea/
.vs/

View File

@ -7,8 +7,4 @@
<RootNamespace>ColdMint</RootNamespace>
<Nullable>enable</Nullable>
</PropertyGroup>
<ItemGroup>
<PackageReference Include="Microsoft.EntityFrameworkCore.Sqlite.Core" Version="6.0.29" />
<PackageReference Include="SQLitePCLRaw.bundle_e_sqlite3" Version="2.1.8" />
</ItemGroup>
</Project>

View File

@ -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
/// <para>公司/创作者名字</para>
/// </summary>
public const string CompanyName = "ColdMint";
/// <summary>
/// <para>An empty namespace</para>
/// <para>空的命名空间</para>
/// </summary>
public const string EmptyNamespace = "Empty";
/// <summary>
/// <para>The default namespace of the packet</para>
/// <para>数据包的默认命名空间</para>
/// </summary>
public const string DefaultNamespace = "traveler";
/// <summary>
/// <para>UserID</para>
@ -119,66 +107,7 @@ public static class Config
///<para>在禁用版本隔离时用的</para>
/// </remarks>
public const string DefaultVersionName = "Default";
public const string DataPackDirectoryName = "DataPacks";
public const string CacheDirectoryName = "Caches";
public const string DataBaseDirectoryName = "DataBases";
/// <summary>
/// <para>The starting path of the item data</para>
/// <para>物品数据的起始路径</para>
/// </summary>
public const string ItemStartPathName = "items";
public const string SpriteStartPathName = "sprites";
/// <summary>
/// <para>The format of the source file inside the packet</para>
/// <para>数据包内的源文件格式</para>
/// </summary>
public const string DataPackSourceFileFomat = ".json";
/// <summary>
/// <para>The path symbol inside the compressed package</para>
/// <para>压缩包内部的路径符号</para>
/// </summary>
public const char ZipPathCharacter = '/';
/// <summary>
/// <para>Gets the packet directory</para>
/// <para>获取数据包目录</para>
/// </summary>
/// <returns></returns>
public static string GetDataPackDirectory()
{
return Path.Join(GetGameDataDirectory(), DataPackDirectoryName);
}
/// <summary>
/// <para>Gets the packet cache directory</para>
/// <para>获取数据包缓存目录</para>
/// </summary>
/// <param name="nameS"></param>
/// <returns></returns>
public static string GetDataPackCacheDirectory(string namespaceStr)
{
var path = Path.Join(GetGameDataDirectory(), CacheDirectoryName, DataPackDirectoryName, namespaceStr);
if (!Directory.Exists(path))
{
Directory.CreateDirectory(path);
}
return path;
}
/// <summary>
/// <para>Get database directory</para>
/// <para>获取数据库目录</para>
/// </summary>
/// <returns></returns>
public static string GetDataBaseDirectory()
{
return Path.Join(GetGameDataDirectory(), DataBaseDirectoryName);
}
/// <summary>
/// <para>GetGameDataDirectory</para>

View File

@ -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;
/// <summary>
/// <para>玩家角色</para>
@ -282,20 +278,6 @@ public partial class Player : CharacterTemplate
var success = PickItem(PickAbleItem);
if (success)
{
//在背包内添加物品
var dataPackDbContext = DataBaseManager.GetRequiredService<DataPackDbContext>();
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)

View File

@ -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;
/// <summary>
/// <para>Packet manager</para>
/// <para>数据包管理器</para>
/// </summary>
public static class DataPackManager
{
/// <summary>
/// <para>When a packet is scanned</para>
/// <para>当一个数据包被扫描到时</para>
/// </summary>
public static Action<IDataPack>? OnScanComplete;
/// <summary>
/// <para>Load all packets in a directory</para>
/// <para>加载某个目录下的所有数据包</para>
/// </summary>
/// <param name="path"></param>
public static async Task<IDataPack[]> ScanAllDataPack(string path)
{
if (!Directory.Exists(path))
{
return Array.Empty<IDataPack>();
}
var dataPacks = new List<IDataPack>();
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();
}
/// <summary>
/// <para>Load a single packet</para>
/// <para>加载单个数据包</para>
/// </summary>
/// <param name="path"></param>
/// <returns></returns>
public async static Task<IDataPack?> 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;
}
}

View File

@ -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; }
/// <summary>
/// <para>Create an empty manifest file</para>
/// <para>创建空的清单文件</para>
/// </summary>
/// <param name="id"></param>
/// <returns></returns>
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;
}
}

View File

@ -1,17 +0,0 @@
namespace ColdMint.scripts.dataPack;
/// <summary>
/// <para>DataPack</para>
/// <para>数据包</para>
/// </summary>
public interface IDataPack
{
IDataPackManifest Manifest { get; }
/// <summary>
/// <para>Get the item's data</para>
/// <para>获取物品的数据</para>
/// </summary>
/// <returns></returns>
string GetItemsData();
}

View File

@ -1,16 +0,0 @@
namespace ColdMint.scripts.dataPack;
/// <summary>
/// <para>DataPackManifest</para>
/// <para>数据包清单文件</para>
/// </summary>
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; }
}

View File

@ -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;
/// <summary>
/// <para>Load the manifest file in the zip package and write it to the data table</para>
/// <para>在zip包内加载清单文件将其写入数据表</para>
/// </summary>
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<LocalDataPackManifest>(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;
}
}
}

View File

@ -1,31 +0,0 @@
using System.IO.Compression;
using System.Threading.Tasks;
using ColdMint.scripts.database;
namespace ColdMint.scripts.dataPack.entryLoader;
public interface IEntryLoader
{
/// <summary>
/// <para>Whether to load</para>
/// <para>是否需要加载</para>
/// </summary>
/// <param name="archiveEntry"></param>
/// <returns></returns>
bool NeedLoad(ZipArchiveEntry archiveEntry);
/// <summary>
/// <para>Execution load</para>
/// <para>执行加载</para>
/// </summary>
///<remarks>
///<para>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</para>
///<para>仅需要在此方法内将数据add或者update到dataPackDbContext内当扫描结束后上层代码会统一提交到数据库</para>
/// <para>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.</para>
/// <para>不要在此方法内从数据库查询旧的项目是否存在,因为还为向数据库提交保存请求。</para>
/// </remarks>
/// <param name="archiveEntry"></param>
Task ExecutionLoad(string namespaceString, string zipFileName, DataPackDbContext dataPackDbContext,
ZipArchiveEntry archiveEntry);
}

View File

@ -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;
/// <summary>
/// <para>Load item information into the data table in the manifest file</para>
/// <para>在清单文件内加载物品信息到数据表</para>
/// </summary>
public class ItemLoader : IEntryLoader
{
private HashSet<string> _itemIdSet = new HashSet<string>();
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<ItemInfo>(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);
}
}

View File

@ -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<string> _spriteNameSet = new HashSet<string>();
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);
}
}

View File

@ -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;
/// <summary>
/// <para>LocalDataPack</para>
/// <para>本地数据包</para>
/// </summary>
public class LocalDataPack : IDataPack
{
private string zipFilePath;
private string zipFileName;
private IDataPackManifest? manifest;
public LocalDataPack(string zipFilePath)
{
this.zipFilePath = zipFilePath;
zipFileName = Path.GetFileName(zipFilePath);
}
/// <summary>
/// <para>Create index information for packets in the database</para>
/// <para>在数据库内为数据包创建索引信息</para>
/// </summary>
public async Task BuildIndex()
{
//我们首先根据文件名在数据表内查找对应的Md5值在判断Md5值是否发生变化。
var entryLoaders = new List<IEntryLoader>();
entryLoaders.Add(new ItemLoader());
entryLoaders.Add(new SpriteLoader());
var md5 = Md5Utils.GetFileMd5(zipFilePath);
var dataPackDbContext = DataBaseManager.GetRequiredService<DataPackDbContext>();
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);
}
/// <summary>
/// <para>Load manifest file</para>
/// <para>加载清单文件</para>
/// </summary>
public async Task LoadManifest()
{
var dataPackDbContext = DataBaseManager.GetRequiredService<DataPackDbContext>();
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);
/// <summary>
/// <para>Get the item's data</para>
/// <para>获取物品的数据</para>
/// </summary>
/// <returns></returns>
public string GetItemsData()
{
return Path.Join(zipFilePath, "items");
}
}

View File

@ -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; }
}

View File

@ -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;
/// <summary>
/// <para>Game database manager</para>
/// <para>游戏数据库管理器</para>
/// </summary>
public static class DataBaseManager
{
private static ServiceProvider serviceProvider;
/// <summary>
/// <para>Initialize database</para>
/// <para>初始化数据库</para>
/// </summary>
public static void InitDataBases(string databasePath)
{
if (!Directory.Exists(databasePath))
{
return;
}
var serviceCollection = new ServiceCollection();
serviceCollection.AddDbContext<DataPackDbContext>(options =>
options.UseSqlite($"Data Source={Path.Join(databasePath, "DataPack.db")}"));
serviceProvider = serviceCollection.BuildServiceProvider();
var dataPackDbContext = GetRequiredService<DataPackDbContext>();
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
{
var scope = serviceProvider.CreateScope();
return scope.ServiceProvider.GetRequiredService<T>();
}
}

View File

@ -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> DataPackInfo { get; set; }
public DbSet<ZipEntryInfo> ZipEntryInfo { get; set; }
public DbSet<ZipFileInfo> ZipFileInfo { get; set; }
public DbSet<ItemInfo> ItemInfo { get; set; }
public DbSet<SpriteInfo> SpriteInfo { get; set; }
public DataPackDbContext(DbContextOptions<DataPackDbContext> options) : base(options)
{
}
}

View File

@ -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; }
/// <summary>
/// <para>Whether the status is enabled</para>
/// <para>是否为启用状态</para>
/// </summary>
public bool IsEnabled { get; set; }
public string ZipFileName { get; set; }
public DateTime CrateTime { get; set; }
public DateTime UpdateTime { get; set; }
}

View File

@ -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; }
}

View File

@ -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; }
/// <summary>
/// <para>The file name is the Id of the Sprite</para>
/// <para>文件名就是精灵的Id</para>
/// </summary>
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; }
}

View File

@ -1,35 +0,0 @@
using System;
using System.ComponentModel.DataAnnotations;
using System.ComponentModel.DataAnnotations.Schema;
namespace ColdMint.scripts.database.dataPackEntity;
/// <summary>
/// <para>entry table in Zip file</para>
/// <para>Zip文件内的entry表</para>
/// </summary>
public class ZipEntryInfo
{
/// <summary>
/// <para>Primary key, auto increment</para>
/// <para>主键,自动递增</para>
/// </summary>
[Key]
[DatabaseGenerated(DatabaseGeneratedOption.Identity)]
public int id { get; set; }
/// <summary>
/// <para>This record is from that zip file</para>
/// <para>这段记录是来源于那个zip文件的</para>
/// </summary>
public string FileName { get; set; }
/// <summary>
/// <para>The path within the zip file</para>
/// <para>位于zip文件内的路径</para>
/// </summary>
public string FullName { get; set; }
public DateTime CrateTime { get; set; }
}

View File

@ -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; }
}

View File

@ -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;
/// <summary>
/// <para>Local Item</para>
/// <para>本地Item</para>
/// </summary>
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<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);
}
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<IItem> OnUse { get; set; }
public Func<IItem, Node> OnInstantiation { get; set; }
}

View File

@ -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);