초기커밋
This commit is contained in:
69
ServerBase/Meta/IMetaHelper.cs
Normal file
69
ServerBase/Meta/IMetaHelper.cs
Normal file
@@ -0,0 +1,69 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using System.Text;
|
||||
using System.Threading.Tasks;
|
||||
|
||||
|
||||
using Amazon.DynamoDBv2.Model;
|
||||
|
||||
|
||||
using ServerCore; using ServerBase;
|
||||
|
||||
|
||||
namespace ServerBase;
|
||||
|
||||
/*
|
||||
public static partial class MetaHelper
|
||||
{
|
||||
public abstract class MetaHelperBase
|
||||
{
|
||||
private bool m_is_initialized = false;
|
||||
|
||||
public MetaHelperBase()
|
||||
{
|
||||
|
||||
}
|
||||
|
||||
public bool init()
|
||||
{
|
||||
if (m_is_initialized == true)
|
||||
{
|
||||
return true;
|
||||
}
|
||||
|
||||
m_is_initialized = true;
|
||||
|
||||
return onInit();
|
||||
}
|
||||
|
||||
protected abstract bool onInit();
|
||||
|
||||
public bool isInitialized()
|
||||
{
|
||||
return m_is_initialized;
|
||||
}
|
||||
|
||||
public string? getMetaHelperName()
|
||||
{
|
||||
return GetType().FullName;
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
public abstract class MetaHelperBase<T>
|
||||
: MetaHelperBase where T : class, IMetaData
|
||||
{
|
||||
public MetaHelperBase()
|
||||
: base()
|
||||
{
|
||||
}
|
||||
|
||||
//protected T MetaDatas
|
||||
//{
|
||||
// get { return TableData.It.getMetaDatas<T>(); }
|
||||
//}
|
||||
|
||||
}//IHelper<T>
|
||||
}
|
||||
*/
|
||||
51
ServerBase/Meta/MetaAssets/ContentLoader.cs
Normal file
51
ServerBase/Meta/MetaAssets/ContentLoader.cs
Normal file
@@ -0,0 +1,51 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.IO;
|
||||
using System.Text;
|
||||
|
||||
|
||||
namespace ServerBase;
|
||||
|
||||
public static class ContentLoader
|
||||
{
|
||||
public static T? loadFile<T>(string dataDir, string fileName) where T : ContentTableBase<T>
|
||||
{
|
||||
try
|
||||
{
|
||||
string exactPath = Path.GetFullPath(dataDir);
|
||||
|
||||
string data = File.ReadAllText(Path.Combine(dataDir, fileName));
|
||||
return Newtonsoft.Json.JsonConvert.DeserializeObject<T>(data);
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
throw new Exception($"content load fail. dataDir: {dataDir}, fileName: {fileName}", ex);
|
||||
}
|
||||
}
|
||||
|
||||
public static T? loadMultipleFiles<T>(string dataDir, string filePattern) where T : ContentTableBase<T>
|
||||
{
|
||||
var files = Directory.GetFiles(dataDir, filePattern, SearchOption.TopDirectoryOnly);
|
||||
|
||||
List<T> tables = new List<T>();
|
||||
foreach (string file in files)
|
||||
{
|
||||
string data = File.ReadAllText(file);
|
||||
T? json = Newtonsoft.Json.JsonConvert.DeserializeObject<T>(data);
|
||||
|
||||
if (json != null)
|
||||
tables.Add(json);
|
||||
}
|
||||
|
||||
T? oneTable = null;
|
||||
foreach (var table in tables)
|
||||
{
|
||||
if (oneTable == null)
|
||||
oneTable = table;
|
||||
else
|
||||
oneTable.merge(table);
|
||||
}
|
||||
|
||||
return oneTable;
|
||||
}
|
||||
}
|
||||
11
ServerBase/Meta/MetaAssets/ContentTableBase.cs
Normal file
11
ServerBase/Meta/MetaAssets/ContentTableBase.cs
Normal file
@@ -0,0 +1,11 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Text;
|
||||
|
||||
|
||||
namespace ServerBase;
|
||||
|
||||
public class ContentTableBase<T>
|
||||
{
|
||||
public virtual void merge(T table) { }
|
||||
}
|
||||
8
ServerBase/Meta/MetaAssets/IMetaData.cs
Normal file
8
ServerBase/Meta/MetaAssets/IMetaData.cs
Normal file
@@ -0,0 +1,8 @@
|
||||
|
||||
|
||||
namespace ServerBase;
|
||||
|
||||
public interface IMetaData
|
||||
{
|
||||
string toBasicString();
|
||||
}
|
||||
37
ServerBase/Meta/MetaValidation/MetaValidator.cs
Normal file
37
ServerBase/Meta/MetaValidation/MetaValidator.cs
Normal 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);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
11
ServerBase/Meta/MetaValidation/MetaValidatorAttribute.cs
Normal file
11
ServerBase/Meta/MetaValidation/MetaValidatorAttribute.cs
Normal file
@@ -0,0 +1,11 @@
|
||||
|
||||
|
||||
|
||||
|
||||
namespace ServerBase;
|
||||
|
||||
|
||||
[AttributeUsage(AttributeTargets.Method)]
|
||||
public class MetaValidatorAttribute : Attribute
|
||||
{
|
||||
}
|
||||
41
ServerBase/Meta/MetaValidation/MethodInfoExtentions.cs
Normal file
41
ServerBase/Meta/MetaValidation/MethodInfoExtentions.cs
Normal 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();
|
||||
}
|
||||
}
|
||||
|
||||
69
ServerBase/Meta/MetaValidation/ValidattionErrorCollection.cs
Normal file
69
ServerBase/Meta/MetaValidation/ValidattionErrorCollection.cs
Normal 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());
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user