summaryrefslogtreecommitdiff
path: root/WorldlineKeepers/Assets/Scripts/Data
diff options
context:
space:
mode:
Diffstat (limited to 'WorldlineKeepers/Assets/Scripts/Data')
-rw-r--r--WorldlineKeepers/Assets/Scripts/Data/CSVReader.cs94
-rw-r--r--WorldlineKeepers/Assets/Scripts/Data/DataManager.cs2
-rw-r--r--WorldlineKeepers/Assets/Scripts/Data/DataManager_Load.cs19
3 files changed, 99 insertions, 16 deletions
diff --git a/WorldlineKeepers/Assets/Scripts/Data/CSVReader.cs b/WorldlineKeepers/Assets/Scripts/Data/CSVReader.cs
index 312eba8..207656b 100644
--- a/WorldlineKeepers/Assets/Scripts/Data/CSVReader.cs
+++ b/WorldlineKeepers/Assets/Scripts/Data/CSVReader.cs
@@ -1,4 +1,6 @@
using JetBrains.Annotations;
+using LitJson;
+using Newtonsoft.Json.Serialization;
using System;
using System.Collections;
using System.Collections.Generic;
@@ -16,8 +18,27 @@ namespace WK.Data
private static Dictionary<string/*key*/, int/*index*/> m_KeyMapping = new Dictionary<string, int>();
private static List<List<string>> m_Rows = new List<List<string>>();
+ /// <summary>
+ /// 解析csv表格,并返回列表
+ /// </summary>
+ /// <typeparam name="T"></typeparam>
+ /// <param name="content"></param>
+ /// <returns></returns>
public static List<T> Read<T>(string content) where T : new()
{
+ List<T> result = new List<T>();
+ Read<T>(result, content);
+ return result;
+ }
+
+ /// <summary>
+ /// 解析csv表格,并返回数量
+ /// </summary>
+ /// <typeparam name="T"></typeparam>
+ /// <param name="content"></param>
+ /// <returns></returns>
+ public static int Read<T>(List<T> target, string content) where T : new()
+ {
m_KeyMapping.Clear();
m_Rows.Clear();
@@ -28,7 +49,7 @@ namespace WK.Data
{
m_KeyMapping.Add(keys[i], i);
}
- List<T> result = new List<T>();
+ int count = 0;
Type type = typeof(T);
for (int i = 1; i < m_Rows.Count; ++i)
{
@@ -36,18 +57,79 @@ namespace WK.Data
continue;
List<string> row = m_Rows[i];
T obj = new T();
- foreach(var key in m_KeyMapping)
+ foreach (var key in m_KeyMapping)
{
int index = key.Value;
var fieldInfo = type.GetField(key.Key);
- if(fieldInfo != null)
+ if (fieldInfo != null)
{
- fieldInfo.SetValue(obj, Convert.ChangeType(row[index], fieldInfo.FieldType));
+ 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));
+ }
}
}
- result.Add(obj);
+ target.Add(obj);
+ count++;
}
- return result;
+ return count;
+ }
+
+ /// <summary>
+ /// 解析csv表格,并按key存储到字典里,返回数量
+ /// </summary>
+ /// <typeparam name="TKey"></typeparam>
+ /// <typeparam name="TValue"></typeparam>
+ /// <param name="target"></param>
+ /// <param name="content"></param>
+ /// <param name="keyName"></param>
+ /// <returns></returns>
+ public static int ReadDictionary<TKey, TValue>(Dictionary<TKey, TValue> target, string content, string keyName) where TValue : new()
+ {
+ List<TValue> data = CSVReader.Read<TValue>(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;
}
}
diff --git a/WorldlineKeepers/Assets/Scripts/Data/DataManager.cs b/WorldlineKeepers/Assets/Scripts/Data/DataManager.cs
index 1c148ee..2667736 100644
--- a/WorldlineKeepers/Assets/Scripts/Data/DataManager.cs
+++ b/WorldlineKeepers/Assets/Scripts/Data/DataManager.cs
@@ -17,7 +17,7 @@ namespace WK.Data
private Dictionary<string/*uid*/, BuffMetadata> m_BuffMetadata = new Dictionary<string, BuffMetadata>();
private Dictionary<string/*uid*/, CharacterMetadata> m_CharacterMetadata = new Dictionary<string, CharacterMetadata>();
- private Dictionary<int, MetadataFile> m_Filelist = new Dictionary<int, MetadataFile>();
+ private Dictionary<EFileKey, MetadataFile> m_Filelist = new Dictionary<EFileKey, MetadataFile>();
public CharacterStatsMetadata GetCharacterStats(string uid)
{
diff --git a/WorldlineKeepers/Assets/Scripts/Data/DataManager_Load.cs b/WorldlineKeepers/Assets/Scripts/Data/DataManager_Load.cs
index 40b00a7..75de5f3 100644
--- a/WorldlineKeepers/Assets/Scripts/Data/DataManager_Load.cs
+++ b/WorldlineKeepers/Assets/Scripts/Data/DataManager_Load.cs
@@ -26,8 +26,6 @@ namespace WK.Data
yield return Timing.WaitForSeconds(StaticDefine.IntervalLoadFile);
}
- #region 加载
-
/// <summary>
/// fielist
/// </summary>
@@ -36,15 +34,18 @@ namespace WK.Data
TextAsset text = ResourceManager.Instance.LoadAsset<TextAsset>(StaticDefine.FileList);
string content = text.text;
List<MetadataFile> files = CSVReader.Read<MetadataFile>(content);
- for (int i = 0; i < files.Count; ++i)
- {
- MetadataFile file = files[i];
- int key = (int)Enum.Parse(typeof(EFileKey), file.key);
- m_Filelist.Add(key, file);
- }
+ //for (int i = 0; i < files.Count; ++i)
+ //{
+ // MetadataFile file = files[i];
+ // int key = (int)Enum.Parse(typeof(EFileKey), file.key);
+ // m_Filelist.Add(key, file);
+ //}
+
+ CSVReader.ReadDictionary<EFileKey, MetadataFile>(m_Filelist, content, "key");
+
+ Debug.Log(m_Filelist.Count);
}
- #endregion
}
} \ No newline at end of file