ProjectDDD/Assets/_DDD/_Scripts/GameData/DataSo.cs
2025-08-12 13:58:25 +09:00

95 lines
2.7 KiB
C#

using System;
using System.Collections.Generic;
using System.Linq;
using UnityEngine;
using UnityEngine.Serialization;
namespace DDD
{
public class DataSo<T> : ScriptableObject where T : IId
{
[FormerlySerializedAs("Datas")] [SerializeField] protected List<T> _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<T> newList)
{
_datas = newList;
}
public List<T> GetDataList()
{
return _datas;
}
public int GetDataCount() => _datas.Count;
public static List<string> ParseDelimitedList(string input, List<string> buffer = null, char[] separators = null,
bool distinct = false, bool toLower = false)
{
separators ??= _defaultSeparators;
if (string.IsNullOrWhiteSpace(input))
{
if (buffer == null) return new List<string>(0);
buffer.Clear();
return buffer;
}
IEnumerable<string> 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;
}
/// <summary>
/// 결과 리스트(target)에 직접 채워 넣습니다. (할당 최소화)
/// </summary>
public static void ParseDelimitedListInPlace(string input, List<string> target, char[] separators = null,
bool distinct = false, bool toLower = false)
{
ParseDelimitedList(input, target, separators, distinct, toLower);
}
}
}