using System; using System.Collections.Generic; using System.Linq; using UnityEngine; using UnityEngine.Serialization; namespace DDD { public class DataSo : ScriptableObject where T : IId { [FormerlySerializedAs("Datas")] [SerializeField] protected List _datas = new(); private static readonly char[] _defaultSeparators = { ',', '|' }; private void OnEnable() { Initialize(); } private void OnValidate() { Initialize(); } protected virtual void Initialize() { } public T GetDataById(string id) => _datas.FirstOrDefault(x => x.Id == id); public bool ContainsData(string id) => _datas.Any(x => x.Id == id); public bool TryGetDataById(string id, out T data) { data = _datas.FirstOrDefault(x => x.Id == id); return data != null; } public void SetDataList(List newList) { _datas = newList; } public List GetDataList() { return _datas; } public int GetDataCount() => _datas.Count; public static List ParseDelimitedList(string input, List buffer = null, char[] separators = null, bool distinct = false, bool toLower = false) { separators ??= _defaultSeparators; if (string.IsNullOrWhiteSpace(input)) { if (buffer == null) return new List(0); buffer.Clear(); return buffer; } IEnumerable query = input .Split(separators, StringSplitOptions.RemoveEmptyEntries) .Select(s => s.Trim()) .Where(s => !string.IsNullOrWhiteSpace(s)); // 소문자 정규화가 필요하면 먼저 적용 if (toLower) { query = query.Select(s => s.ToLowerInvariant()); } // Distinct if (distinct) { query = query.Distinct(StringComparer.Ordinal); } if (buffer == null) return query.ToList(); buffer.Clear(); buffer.AddRange(query); return buffer; } /// /// 결과 리스트(target)에 직접 채워 넣습니다. (할당 최소화) /// public static void ParseDelimitedListInPlace(string input, List target, char[] separators = null, bool distinct = false, bool toLower = false) { ParseDelimitedList(input, target, separators, distinct, toLower); } } }