Files
Fishing2/Assets/Scripts/Configs~/ConfigAssets.Parse.cs
2025-10-10 17:57:36 +08:00

68 lines
1.9 KiB
C#

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;
}
}
}