diff options
author | chai <215380520@qq.com> | 2023-05-12 19:00:29 +0800 |
---|---|---|
committer | chai <215380520@qq.com> | 2023-05-12 19:00:29 +0800 |
commit | 6c91f1ed6810a57da08a24dd1359f288c443dd75 (patch) | |
tree | 485096584922b7300e987af1fe9c21f262898a5f /WorldlineKeepers/Assets/Scripts/Data/CSVReader.cs | |
parent | 266370578135dca270729e8a70252e776ed22898 (diff) |
*misc
Diffstat (limited to 'WorldlineKeepers/Assets/Scripts/Data/CSVReader.cs')
-rw-r--r-- | WorldlineKeepers/Assets/Scripts/Data/CSVReader.cs | 55 |
1 files changed, 55 insertions, 0 deletions
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<string/*key*/, int/*index*/> m_KeyMapping = new Dictionary<string, int>(); + private static List<List<string>> m_Rows = new List<List<string>>(); + + public static List<T> Read<T>(string content) where T : new() + { + m_KeyMapping.Clear(); + m_Rows.Clear(); + + m_Rows = CSVParser.LoadFromString(content); + // µÚÒ»ÐÐÊÇkey + List<string> keys = m_Rows[0]; + for (int i = 0; i < keys.Count; ++i) + { + m_KeyMapping.Add(keys[i], i); + } + List<T> result = new List<T>(); + Type type = typeof(T); + for (int i = 1; i < m_Rows.Count; ++i) + { + if (m_Rows[i][0][0] == '#') // ×¢ÊÍ + continue; + List<string> 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 |