表格重构
This commit is contained in:
68
Assets/Scripts/Configs~/ConfigAssets.Parse.cs
Normal file
68
Assets/Scripts/Configs~/ConfigAssets.Parse.cs
Normal file
@@ -0,0 +1,68 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using NBC;
|
||||
using Newtonsoft.Json.Linq;
|
||||
using UnityEngine;
|
||||
|
||||
namespace NBF
|
||||
{
|
||||
public partial class ConfigAssets
|
||||
{
|
||||
private static readonly Type CustomType = typeof(ICustomParse);
|
||||
|
||||
private static List<T> ParseLine<T>(JToken[] arr, TableNameAttribute tableNameAttribute) where T : ConfigBase
|
||||
{
|
||||
List<T> list = new List<T>();
|
||||
var type = typeof(T);
|
||||
foreach (var jToken in arr)
|
||||
{
|
||||
T instance = null;
|
||||
try
|
||||
{
|
||||
if (CustomType.IsAssignableFrom(type)) //自定义解析
|
||||
{
|
||||
instance = Activator.CreateInstance<T>();
|
||||
}
|
||||
else
|
||||
{
|
||||
instance = jToken.ToObject<T>();
|
||||
}
|
||||
}
|
||||
catch (Exception e)
|
||||
{
|
||||
Log.Error(e);
|
||||
}
|
||||
|
||||
if (instance != null)
|
||||
{
|
||||
var key = jToken[tableNameAttribute.Key].ToInt();
|
||||
if (key < 1)
|
||||
{
|
||||
if (instance.id > 0)
|
||||
{
|
||||
key = instance.id;
|
||||
}
|
||||
}
|
||||
|
||||
try
|
||||
{
|
||||
if (key < 1) continue;
|
||||
if (instance is ICustomParse customParse)
|
||||
{
|
||||
customParse.Parse(jToken);
|
||||
}
|
||||
|
||||
instance.id = key;
|
||||
list.Add(instance);
|
||||
}
|
||||
catch (Exception e)
|
||||
{
|
||||
Log.Error(e);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
return list;
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user