151 lines
5.2 KiB
C#
151 lines
5.2 KiB
C#
|
using System.Collections.Generic;
|
|||
|
using System.IO;
|
|||
|
using System.Reflection;
|
|||
|
using Module.Log;
|
|||
|
using UnityEngine;
|
|||
|
|
|||
|
// 编辑器用表格读取工具,用于避开修改自动生成的TableManager类,来读取编辑器表格的功能
|
|||
|
public static class EditorTableManager
|
|||
|
{
|
|||
|
public static List<T> GetTable<T>() where T : class
|
|||
|
{
|
|||
|
// 不缓存表格,可能有动态改表的情况
|
|||
|
object tableList = null;
|
|||
|
const string fileNameField = "TAB_FILE_DATA";
|
|||
|
var fileNameGetInfo = typeof(T).GetField("TAB_FILE_DATA",
|
|||
|
BindingFlags.Public | BindingFlags.Static | BindingFlags.FlattenHierarchy);
|
|||
|
if (fileNameGetInfo == null)
|
|||
|
{
|
|||
|
LogModule.ErrorLog(string.Format("类型{0}不存在表格路径{1}", typeof(T), fileNameField));
|
|||
|
}
|
|||
|
else
|
|||
|
{
|
|||
|
var fileName = (string) fileNameGetInfo.GetValue(null);
|
|||
|
fileName = Path.GetFileName(fileName);
|
|||
|
tableList = LoadTable<T>(fileName);
|
|||
|
}
|
|||
|
return tableList as List<T>;
|
|||
|
}
|
|||
|
|
|||
|
private static List<T> LoadTable<T>(string tableName) where T : class
|
|||
|
{
|
|||
|
var filePath = GetTablePath(tableName);
|
|||
|
if (!File.Exists(filePath))
|
|||
|
{
|
|||
|
LogModule.ErrorLog(string.Format("文件{0}不存在!", filePath));
|
|||
|
}
|
|||
|
else
|
|||
|
{
|
|||
|
ConstructorInfo constructor = null;
|
|||
|
var ctors = typeof(T).GetConstructors();
|
|||
|
for (var i = 0; i < ctors.Length; i++)
|
|||
|
{
|
|||
|
var parameters = ctors[i].GetParameters();
|
|||
|
if (parameters.Length == 1
|
|||
|
&& parameters[0].ParameterType == typeof(string))
|
|||
|
{
|
|||
|
constructor = ctors[i];
|
|||
|
break;
|
|||
|
}
|
|||
|
}
|
|||
|
if (constructor == null)
|
|||
|
{
|
|||
|
LogModule.ErrorLog(string.Format("类型{0}不存在表格形式的构造函数!", typeof(T)));
|
|||
|
}
|
|||
|
else
|
|||
|
{
|
|||
|
var list = new List<T>();
|
|||
|
var lines = File.ReadAllLines(filePath);
|
|||
|
for (var i = 2; i < lines.Length; i++)
|
|||
|
{
|
|||
|
var line = lines[i];
|
|||
|
if (!string.IsNullOrEmpty(line) && !line[0].Equals('#') && !line[0].Equals('\t'))
|
|||
|
{
|
|||
|
var item = (T) constructor.Invoke(new object[] {line});
|
|||
|
list.Add(item);
|
|||
|
}
|
|||
|
}
|
|||
|
return list;
|
|||
|
}
|
|||
|
}
|
|||
|
return null;
|
|||
|
}
|
|||
|
|
|||
|
private static string GetTablePath(string tableName)
|
|||
|
{
|
|||
|
if (!Path.HasExtension(tableName))
|
|||
|
tableName += AssetConst.textExtension;
|
|||
|
return EditorCommonUtility.AssetToFilePath(LoadAssetBundle.tablePath) + tableName;
|
|||
|
}
|
|||
|
|
|||
|
//private static void ReaderPList(string text, SerializableTable _fun, Dictionary<int, List<object>> _hash)
|
|||
|
//{
|
|||
|
// var alldataRow = text.Split('\n');
|
|||
|
// int skip = 0;
|
|||
|
// string[] typeList = null;
|
|||
|
// foreach (string line in alldataRow)
|
|||
|
// {
|
|||
|
// int nKey;
|
|||
|
// if (skip == 1)
|
|||
|
// {
|
|||
|
// typeList = line.Split('\t');
|
|||
|
// _values = new string[typeList.Length];
|
|||
|
// ++skip;
|
|||
|
// continue;
|
|||
|
// }
|
|||
|
// if (++skip < 4) continue;
|
|||
|
// if (string.IsNullOrEmpty(line)) continue;
|
|||
|
// if (line[0] == '#') continue;
|
|||
|
// string szlinetemp = line;
|
|||
|
// if (szlinetemp.Length >= 1)
|
|||
|
// {
|
|||
|
// if (szlinetemp[szlinetemp.Length - 1] == '\r')
|
|||
|
// {
|
|||
|
// szlinetemp = szlinetemp.TrimEnd('\r');
|
|||
|
// }
|
|||
|
// }
|
|||
|
// string[] strCol = MySplit(szlinetemp, typeList, "\t");
|
|||
|
// if (strCol.Length == 0) continue;
|
|||
|
// string skey = strCol[0];
|
|||
|
// string[] valuesList = new string[strCol.Length];
|
|||
|
|
|||
|
// if (string.IsNullOrEmpty(skey) || skey.Equals("--"))
|
|||
|
// {
|
|||
|
// skey = _key;
|
|||
|
// nKey = Int32.Parse(skey);
|
|||
|
// valuesList[0] = skey;
|
|||
|
// for (int i = 1; i < strCol.Length; ++i)
|
|||
|
// {
|
|||
|
// if (String.IsNullOrEmpty(strCol[i]) || strCol[i] == "--")
|
|||
|
// {
|
|||
|
// valuesList[i] = _values[i];
|
|||
|
// }
|
|||
|
// else
|
|||
|
// {
|
|||
|
// valuesList[i] = strCol[i];
|
|||
|
// _values[i] = strCol[i];
|
|||
|
// }
|
|||
|
// }
|
|||
|
// }
|
|||
|
// else
|
|||
|
// {
|
|||
|
// _key = skey;
|
|||
|
// nKey = Int32.Parse(skey);
|
|||
|
// for (int i = 0; i < strCol.Length; ++i)
|
|||
|
// {
|
|||
|
// if (strCol[i] == "--")
|
|||
|
// {
|
|||
|
// valuesList[i] = "0";
|
|||
|
// _values[i] = "0";
|
|||
|
// }
|
|||
|
// else
|
|||
|
// {
|
|||
|
// valuesList[i] = strCol[i];
|
|||
|
// _values[i] = strCol[i];
|
|||
|
// }
|
|||
|
// }
|
|||
|
// }
|
|||
|
// _fun(valuesList, nKey, _hash);
|
|||
|
// }
|
|||
|
//}
|
|||
|
}
|