ProjectDDD/Assets/_DDD/_Scripts/GameData/DataSo.cs

95 lines
2.7 KiB
C#
Raw Normal View History

2025-08-12 04:58:25 +00:00
using System;
2025-07-15 04:01:35 +00:00
using System.Collections.Generic;
using System.Linq;
using UnityEngine;
2025-08-11 18:27:18 +00:00
using UnityEngine.Serialization;
2025-07-15 04:01:35 +00:00
namespace DDD
{
public class DataSo<T> : ScriptableObject where T : IId
{
2025-08-11 18:27:18 +00:00
[FormerlySerializedAs("Datas")] [SerializeField] protected List<T> _datas = new();
2025-07-15 04:01:35 +00:00
2025-08-12 04:58:25 +00:00
private static readonly char[] _defaultSeparators = { ',', '|' };
2025-08-11 18:27:18 +00:00
private void OnEnable()
{
Initialize();
}
private void OnValidate()
{
Initialize();
}
protected virtual void Initialize() { }
public T GetDataById(string id) => _datas.FirstOrDefault(x => x.Id == id);
2025-07-25 07:58:53 +00:00
2025-08-11 18:27:18 +00:00
public bool ContainsData(string id) => _datas.Any(x => x.Id == id);
2025-08-06 02:45:08 +00:00
2025-07-25 07:58:53 +00:00
public bool TryGetDataById(string id, out T data)
{
2025-08-11 18:27:18 +00:00
data = _datas.FirstOrDefault(x => x.Id == id);
2025-07-25 07:58:53 +00:00
return data != null;
}
2025-07-15 04:01:35 +00:00
public void SetDataList(List<T> newList)
{
2025-08-11 18:27:18 +00:00
_datas = newList;
2025-07-15 04:01:35 +00:00
}
public List<T> GetDataList()
{
2025-08-11 18:27:18 +00:00
return _datas;
2025-07-15 04:01:35 +00:00
}
2025-08-11 18:27:18 +00:00
public int GetDataCount() => _datas.Count;
2025-08-12 04:58:25 +00:00
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);
}
2025-07-15 04:01:35 +00:00
}
}