From 4f2a6e8110afcec36af528f9fa14ddeebb613663 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E9=9C=A7=E9=9B=A8=E7=83=A8?= Date: Sat, 15 Jun 2024 14:30:22 +0800 Subject: [PATCH] Split loops and extract functions --- locals/Log.csv | 4 +-- scripts/item/ItemTypeRegister.cs | 61 +++++++++++++++++++------------- 2 files changed, 39 insertions(+), 26 deletions(-) diff --git a/locals/Log.csv b/locals/Log.csv index 78c2114..96c110d 100644 --- a/locals/Log.csv +++ b/locals/Log.csv @@ -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,アイテム情報カタログを開こうとしてエラーが発生しました。 \ No newline at end of file diff --git a/scripts/item/ItemTypeRegister.cs b/scripts/item/ItemTypeRegister.cs index 933492a..3e93c51 100644 --- a/scripts/item/ItemTypeRegister.cs +++ b/scripts/item/ItemTypeRegister.cs @@ -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 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>(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(typeInfo.ScenePath); - var icon = ResourceLoader.Load(typeInfo.IconPath); - var itemType = new ItemType(typeInfo.Id, - () => NodeUtils.InstantiatePackedScene(scene), - icon, typeInfo.MaxStackValue); - ItemTypeManager.Register(itemType); - } + RegisterTypeInfo(typeInfo); } } - + + private static IList 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>(yamlString); + yamlFile.Close(); + return typeInfos; + } + + private static void RegisterTypeInfo(ItemTypeInfo typeInfo) + { + var scene = ResourceLoader.Load(typeInfo.ScenePath); + var icon = ResourceLoader.Load(typeInfo.IconPath); + var itemType = new ItemType(typeInfo.Id, + () => NodeUtils.InstantiatePackedScene(scene), + icon, typeInfo.MaxStackValue); + ItemTypeManager.Register(itemType); + } + //Use for yaml deserialization private record struct ItemTypeInfo(string Id, string ScenePath, string IconPath, int MaxStackValue) { } } \ No newline at end of file