From dbf4ea119100f571b3710568dfdc2e09dcec2a61 Mon Sep 17 00:00:00 2001
From: chai <215380520@qq.com>
Date: Wed, 31 May 2023 09:21:51 +0800
Subject: *misc
---
WorldlineKeepers/Assets/Scripts/Tools/CSVReader.cs | 133 +++++++++++++++++++++
1 file changed, 133 insertions(+)
create mode 100644 WorldlineKeepers/Assets/Scripts/Tools/CSVReader.cs
(limited to 'WorldlineKeepers/Assets/Scripts/Tools/CSVReader.cs')
diff --git a/WorldlineKeepers/Assets/Scripts/Tools/CSVReader.cs b/WorldlineKeepers/Assets/Scripts/Tools/CSVReader.cs
new file mode 100644
index 0000000..0ce796e
--- /dev/null
+++ b/WorldlineKeepers/Assets/Scripts/Tools/CSVReader.cs
@@ -0,0 +1,133 @@
+using JetBrains.Annotations;
+using LitJson;
+using Newtonsoft.Json.Serialization;
+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;
+
+public class CSVReader
+{
+ private static Dictionary m_KeyMapping = new Dictionary();
+ private static List> m_Rows = new List>();
+
+ ///
+ /// 解析csv表格,并返回列表
+ ///
+ ///
+ ///
+ ///
+ public static List Read(string content) where T : new()
+ {
+ List result = new List();
+ Read(result, content);
+ return result;
+ }
+
+ ///
+ /// 解析csv表格,并返回数量
+ ///
+ ///
+ ///
+ ///
+ public static int Read(List target, 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);
+ }
+ int count = 0;
+ 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)
+ {
+ Type fieldType = fieldInfo.FieldType;
+ if (fieldType.IsEnum) // 如果是枚举,先转成int
+ {
+ int value = int.Parse(row[index]);
+ fieldInfo.SetValue(obj, value);
+ }
+ else
+ {
+ fieldInfo.SetValue(obj, Convert.ChangeType(row[index], fieldInfo.FieldType));
+ }
+ }
+ }
+ target.Add(obj);
+ count++;
+ }
+ return count;
+ }
+
+ ///
+ /// 解析csv表格,并按key存储到字典里,返回数量
+ ///
+ ///
+ ///
+ ///
+ ///
+ ///
+ ///
+ public static int ReadDictionary(Dictionary target, string content, string keyName) where TValue : new()
+ {
+ List data = CSVReader.Read(content);
+ if (data == null || data.Count == 0)
+ return 0;
+ Type type_key = typeof(TKey);
+ Type type_value = typeof(TValue);
+ FieldInfo field = type_value.GetField(keyName);
+ Type type_field = field.FieldType;
+ int count = 0;
+ for (int i = 0; i < data.Count; ++i)
+ {
+ TValue d = data[i];
+
+ TKey key = default(TKey);
+ if (type_key.IsEnum)
+ {
+ if(type_field == typeof(string))
+ {
+ key = (TKey)Enum.Parse(type_key, field.GetValue(d).ToString());
+ }
+ else if(type_field == typeof(int))
+ {
+ key = (TKey)field.GetValue(d);
+ }
+ }
+ else
+ {
+ key = (TKey)field.GetValue(d);
+ }
+ if (key == null)
+ {
+ LogHelper.LogError("CSVReader.ReadDictionary(): key is null");
+ continue;
+ }
+
+ target.Add(key, d);
+ count++;
+ }
+ return count;
+ }
+
+}
+
--
cgit v1.1-26-g67d0