87 lines
2.0 KiB
C#
87 lines
2.0 KiB
C#
using System;
|
|
using System.Collections.Generic;
|
|
using System.Data;
|
|
using System.Linq;
|
|
using System.Text;
|
|
using System.Threading.Tasks;
|
|
|
|
|
|
using ServerCore; using ServerBase;
|
|
|
|
|
|
namespace ServerBase;
|
|
|
|
public partial class Initializers
|
|
{
|
|
private string m_contents_name = string.Empty;
|
|
private readonly List<IInitializer> m_initializers = new();
|
|
|
|
public async Task<Result> init(string contentsName)
|
|
{
|
|
var result = new Result();
|
|
|
|
m_contents_name = contentsName;
|
|
|
|
foreach (var each in m_initializers)
|
|
{
|
|
result = await each.onInit();
|
|
if (result.isFail())
|
|
{
|
|
Log.getLogger().error($"Failed to init of Initializers - {each.getTypeName()}, ContentsName:{contentsName}");
|
|
return result;
|
|
}
|
|
}
|
|
|
|
Log.getLogger().debug($"Success Initializers.init() - {m_contents_name}");
|
|
|
|
return result;
|
|
}
|
|
|
|
public bool appendInitializer(IInitializer initializer)
|
|
{
|
|
if (initializer == null)
|
|
{
|
|
return false;
|
|
}
|
|
|
|
if (null != m_initializers.Find(x => x.Equals(initializer)))
|
|
{
|
|
Log.getLogger().fatal($"Failed to append Initializer, cause Duplicated Initializer !!! - {initializer.getTypeName()}");
|
|
return false;
|
|
}
|
|
|
|
m_initializers.Add(initializer);
|
|
return true;
|
|
}
|
|
|
|
public bool tryConvert<T>(out List<T>? founds)
|
|
where T : class
|
|
{
|
|
founds = null;
|
|
|
|
foreach (var each in m_initializers)
|
|
{
|
|
var converted = each as T;
|
|
if (converted != null)
|
|
{
|
|
founds = founds ?? new List<T>();
|
|
founds.Add(converted);
|
|
}
|
|
}
|
|
|
|
return founds != null && 0 < founds.Count;
|
|
}
|
|
|
|
public List<T>? tryConvert<T>() where T : class
|
|
{
|
|
if (tryConvert<T>(out var founds))
|
|
{
|
|
return founds;
|
|
}
|
|
else
|
|
{
|
|
return new List<T>();
|
|
}
|
|
}
|
|
}
|