Split loops and extract functions
This commit is contained in:
parent
efc289b3ad
commit
4f2a6e8110
|
@ -28,6 +28,6 @@ log_not_within_the_loot_spawn_range,给定的数值{0}没有在战利品{1}的
|
|||
log_loot_data_quantity,有{0}个战利品数据被返回。,{0} loot data was returned.,{0}個の戦利品データが返されます。
|
||||
|
||||
log_start_item_register_from_file,开始从文件注册物品信息,Start registering item information from files,アイテム情報をファイルから登録開始
|
||||
log_item_register_from_file,从文件{0}中注册物品信息,Registering item information from file {0},ファイル{0}からアイテム情報を登録する
|
||||
log_item_register_find_item_in_file,注册发现的物品{0},Register discovered item {0},見つかったアイテム{0}を登録
|
||||
log_found_files,找到{0}个文件,Found {0} files,{0}ファイルが見つかりました
|
||||
log_found_item_types,从文件中找到{0}个物品类型,Found {0} item types in files,ファイルから{0}個のアイテム・タイプが見つかった
|
||||
log_error_when_open_item_regs_dir,尝试打开物品信息目录时发生错误,Error when opening itemRegs dir,アイテム情報カタログを開こうとしてエラーが発生しました。
|
|
|
@ -1,4 +1,6 @@
|
|||
using System.Collections.Generic;
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
|
||||
using ColdMint.scripts.debug;
|
||||
using ColdMint.scripts.utils;
|
||||
|
@ -36,32 +38,43 @@ public static class ItemTypeRegister
|
|||
LogCat.LogError("error_when_open_item_regs_dir");
|
||||
}
|
||||
|
||||
// traverse the dir, find files to register
|
||||
foreach (var file in itemRegsDir.GetFiles())
|
||||
// find files
|
||||
var files = itemRegsDir.GetFiles();
|
||||
LogCat.LogWithFormat("found_files", files.Length);
|
||||
|
||||
// parse files to item type infos
|
||||
IEnumerable<ItemTypeInfo> typeInfos =
|
||||
files.SelectMany(file => ParseFile(deserializer, $"{itemRegsDirPath}/{file}")).ToList();
|
||||
LogCat.LogWithFormat("found_item_types", typeInfos.Count());
|
||||
|
||||
// traverse type infos and register them.
|
||||
foreach (var typeInfo in typeInfos)
|
||||
{
|
||||
if (file is null) continue;
|
||||
LogCat.LogWithFormat("item_register_from_file", file);
|
||||
|
||||
// read file, parse to an IEnumerable of type infos
|
||||
var yamlFile = FileAccess.Open($"{itemRegsDirPath}/{file}", FileAccess.ModeFlags.Read);
|
||||
var yamlString = yamlFile.GetAsText();
|
||||
var typeInfos = deserializer.Deserialize<IEnumerable<ItemTypeInfo>>(yamlString);
|
||||
yamlFile.Close();
|
||||
|
||||
// traverse type infos and register them.
|
||||
foreach (var typeInfo in typeInfos)
|
||||
{
|
||||
LogCat.LogWithFormat("item_register_find_item_in_file", typeInfo.Id);
|
||||
var scene = ResourceLoader.Load<PackedScene>(typeInfo.ScenePath);
|
||||
var icon = ResourceLoader.Load<Texture2D>(typeInfo.IconPath);
|
||||
var itemType = new ItemType(typeInfo.Id,
|
||||
() => NodeUtils.InstantiatePackedScene<Packsack>(scene),
|
||||
icon, typeInfo.MaxStackValue);
|
||||
ItemTypeManager.Register(itemType);
|
||||
}
|
||||
RegisterTypeInfo(typeInfo);
|
||||
}
|
||||
}
|
||||
|
||||
private static IList<ItemTypeInfo> ParseFile(IDeserializer deserializer, string filePath)
|
||||
{
|
||||
string itemRegsDirPath;
|
||||
string file;
|
||||
var yamlFile = FileAccess.Open(filePath, FileAccess.ModeFlags.Read);
|
||||
var yamlString = yamlFile.GetAsText();
|
||||
var typeInfos = deserializer.Deserialize<IList<ItemTypeInfo>>(yamlString);
|
||||
yamlFile.Close();
|
||||
return typeInfos;
|
||||
}
|
||||
|
||||
private static void RegisterTypeInfo(ItemTypeInfo typeInfo)
|
||||
{
|
||||
var scene = ResourceLoader.Load<PackedScene>(typeInfo.ScenePath);
|
||||
var icon = ResourceLoader.Load<Texture2D>(typeInfo.IconPath);
|
||||
var itemType = new ItemType(typeInfo.Id,
|
||||
() => NodeUtils.InstantiatePackedScene<Packsack>(scene),
|
||||
icon, typeInfo.MaxStackValue);
|
||||
ItemTypeManager.Register(itemType);
|
||||
}
|
||||
|
||||
//Use for yaml deserialization
|
||||
private record struct ItemTypeInfo(string Id, string ScenePath, string IconPath, int MaxStackValue) { }
|
||||
}
|
Loading…
Reference in New Issue
Block a user