From 6c91f1ed6810a57da08a24dd1359f288c443dd75 Mon Sep 17 00:00:00 2001 From: chai <215380520@qq.com> Date: Fri, 12 May 2023 19:00:29 +0800 Subject: *misc --- WorldlineKeepers/Assets/Scripts/Data/CSVReader.cs | 55 ++++++++++++++++++++++ .../Assets/Scripts/Data/CSVReader.cs.meta | 11 +++++ .../Assets/Scripts/Data/DataManager.cs | 19 +++++++- 3 files changed, 83 insertions(+), 2 deletions(-) create mode 100644 WorldlineKeepers/Assets/Scripts/Data/CSVReader.cs create mode 100644 WorldlineKeepers/Assets/Scripts/Data/CSVReader.cs.meta (limited to 'WorldlineKeepers/Assets/Scripts/Data') diff --git a/WorldlineKeepers/Assets/Scripts/Data/CSVReader.cs b/WorldlineKeepers/Assets/Scripts/Data/CSVReader.cs new file mode 100644 index 0000000..312eba8 --- /dev/null +++ b/WorldlineKeepers/Assets/Scripts/Data/CSVReader.cs @@ -0,0 +1,55 @@ +using JetBrains.Annotations; +using System; +using System.Collections; +using System.Collections.Generic; +using System.Net.Mime; +using System.Reflection; +using UnityEngine; +using yutokun; +using static UnityEngine.Rendering.DebugUI; + +namespace WK.Data +{ + + public class CSVReader + { + private static Dictionary m_KeyMapping = new Dictionary(); + private static List> m_Rows = new List>(); + + public static List Read(string content) where T : new() + { + m_KeyMapping.Clear(); + m_Rows.Clear(); + + m_Rows = CSVParser.LoadFromString(content); + // µÚÒ»ÐÐÊÇkey + List keys = m_Rows[0]; + for (int i = 0; i < keys.Count; ++i) + { + m_KeyMapping.Add(keys[i], i); + } + List result = new List(); + Type type = typeof(T); + for (int i = 1; i < m_Rows.Count; ++i) + { + if (m_Rows[i][0][0] == '#') // ×¢ÊÍ + continue; + List row = m_Rows[i]; + T obj = new T(); + foreach(var key in m_KeyMapping) + { + int index = key.Value; + var fieldInfo = type.GetField(key.Key); + if(fieldInfo != null) + { + fieldInfo.SetValue(obj, Convert.ChangeType(row[index], fieldInfo.FieldType)); + } + } + result.Add(obj); + } + return result; + } + + } + +} \ No newline at end of file diff --git a/WorldlineKeepers/Assets/Scripts/Data/CSVReader.cs.meta b/WorldlineKeepers/Assets/Scripts/Data/CSVReader.cs.meta new file mode 100644 index 0000000..52670ce --- /dev/null +++ b/WorldlineKeepers/Assets/Scripts/Data/CSVReader.cs.meta @@ -0,0 +1,11 @@ +fileFormatVersion: 2 +guid: f6b35022c04390a46beb2b27711a7950 +MonoImporter: + externalObjects: {} + serializedVersion: 2 + defaultReferences: [] + executionOrder: 0 + icon: {instanceID: 0} + userData: + assetBundleName: + assetBundleVariant: diff --git a/WorldlineKeepers/Assets/Scripts/Data/DataManager.cs b/WorldlineKeepers/Assets/Scripts/Data/DataManager.cs index e56c80b..a3d1257 100644 --- a/WorldlineKeepers/Assets/Scripts/Data/DataManager.cs +++ b/WorldlineKeepers/Assets/Scripts/Data/DataManager.cs @@ -9,8 +9,8 @@ namespace WK.Data public class DataManager : Singleton { - private Dictionary m_CharacterStatsMetadata; - private Dictionary m_BuffMetadata; + private Dictionary m_CharacterStatsMetadata = new Dictionary(); + private Dictionary m_BuffMetadata = new Dictionary(); public CharacterStatsMetadata GetCharacterStats(string uid) { @@ -32,6 +32,21 @@ namespace WK.Data return null; } + public void Load() + { + LoadDefaultStats(); + } + + private void LoadDefaultStats() + { + TextAsset text = ResourceManager.Instance.LoadAsset(StaticDefine.StatsFilePath); + List stats = CSVReader.Read(text.text); + for(int i = 0; i < stats.Count; ++i) + { + m_CharacterStatsMetadata.Add(stats[i].uid, stats[i]); + } + } + } } \ No newline at end of file -- cgit v1.1-26-g67d0