초기커밋

This commit is contained in:
2025-05-01 07:20:41 +09:00
commit 98bb2e3c5c
2747 changed files with 646947 additions and 0 deletions

View File

@@ -0,0 +1,37 @@
using System.Reflection;
using ServerCore; using ServerBase;
namespace ServerBase;
public class MetaValidator
{
public void validate<T>(IReadOnlyList<T> list, ValidatorErrorCollection errors)
{
var thisAssembly = Assembly.GetExecutingAssembly();
var methods = thisAssembly.GetTypes()
.SelectMany(x => x.GetMethods(BindingFlags.Public | BindingFlags.Static))
.Where(x => x.GetCustomAttributes<MetaValidatorAttribute>().Any())
.Where(x => x.GetParameters()?[0]?.ParameterType == typeof(T))
.ToList();
if (methods.Count == 0)
{
Log.getLogger().error($"not found validateFunc. type: {typeof(T).Name}");
return;
}
var validateFunc = MethodInfoExtentions.compile<Action<T, ValidatorErrorCollection>>(methods[0]);
errors.CurrentName = typeof(T).Name;
// var invokeParams = new object[1];
foreach (var (item, i) in list.Select((value, i) => (value, i)))
{
errors.CurrentArrayIndex = i;
validateFunc.Invoke(item!, errors);
}
}
}

View File

@@ -0,0 +1,11 @@

namespace ServerBase;
[AttributeUsage(AttributeTargets.Method)]
public class MetaValidatorAttribute : Attribute
{
}

View File

@@ -0,0 +1,41 @@
using System.Linq.Expressions;
using System.Reflection;
namespace ServerBase;
public static class MethodInfoExtentions
{
public static TDelegate compile<TDelegate>(MethodInfo mi)
{
ParameterExpression? @this = null;
if (!mi.IsStatic)
{
@this = Expression.Parameter(mi.DeclaringType!, "this");
}
var parameters = new List<ParameterExpression>();
if (@this != null)
{
parameters.Add(@this);
}
foreach (var parameter in mi.GetParameters())
{
parameters.Add(Expression.Parameter(parameter.ParameterType, parameter.Name));
}
Expression? call = null;
if (@this != null)
{
call = Expression.Call(@this, mi, parameters.Skip(1));
}
else
{
call = Expression.Call(mi, parameters);
}
return Expression.Lambda<TDelegate>(call, parameters).Compile();
}
}

View File

@@ -0,0 +1,69 @@
using System.Text;
using ServerCore;
namespace ServerBase;
public class ValidattionError
{
public string Name { get; set; } = "";
public int ArrayIndex { get; set; }
public string Message { get; set; } = "";
public string toDetailString()
{
return $"[{Name}, array: {ArrayIndex}] {Message}";
}
public string toSimpleString()
{
return $"[array: {ArrayIndex}] {Message}";
}
}
public class ValidatorErrorCollection
{
public string CurrentName { get; set; } = "";
public int CurrentArrayIndex { get; set; }
public Dictionary<string, List<ValidattionError>> Errors = new();
public bool HasError => Errors.Count > 0;
public void add(string message)
{
Errors.TryGetValue(CurrentName, out var list);
if (list == null)
{
list = new List<ValidattionError>();
Errors.Add(CurrentName, list);
}
list.Add(new ValidattionError
{
Name = CurrentName,
ArrayIndex = CurrentArrayIndex,
Message = message
});
}
public void log()
{
foreach (var item in Errors)
{
StringBuilder sb = new();
sb.AppendLine($"[{item.Key}]");
foreach (var error in item.Value)
{
sb.AppendLine($"\t {error.toSimpleString()}");
}
sb.AppendLine($"[{item.Key}] validaton error. errorCount: {item.Value.Count}");
Log.getLogger().error(sb.ToString());
}
}
}