private static CREATE_FUNC GetCreateFunc() { var tType = typeof(T); var method = tType.GetMethods() .Where((methodInfo) => methodInfo.Name.Equals("CreateFromBinary")) .Where((methodInfo) => methodInfo.IsStatic && methodInfo.IsPublic) .Where((methodInfo) => methodInfo.GetParameters().Length == 2) .Where((methodInfo) => methodInfo.GetParameters()[0].ParameterType.Equals(typeof(MutagenFrame))) .Where((methodInfo) => methodInfo.GetParameters()[1].ParameterType.Equals(typeof(RecordTypeConverter))) .FirstOrDefault(); if (method == null) { throw new ArgumentException(); } if (method.ReturnType == tType) { var wrap = LoquiBinaryTranslation <T> .CREATE; return(async(MutagenFrame frame, RecordTypeConverter? recConv) => { return wrap(frame, recConv); }); } else { return(DelegateBuilder.BuildDelegate <CREATE_FUNC>(method)); } }
private static (Type ReturnType, Type GeneratorType, Delegate Delegate) GetGenerators(MethodInfo methodInfo) { Delegate @delegate; if (methodInfo.IsStatic) { @delegate = DelegateBuilder.BuildDelegate(methodInfo, null); } else { var declaringType = methodInfo.DeclaringType; if (declaringType == null) { throw new InvalidOperationException(); } if (!_instances.TryGetValue(declaringType, out var instance)) { try { instance = Activator.CreateInstance(declaringType); } catch (Exception exception) { Console.WriteLine(exception); } _instances[declaringType] = instance; } @delegate = instance == null ? null : DelegateBuilder.BuildDelegate(methodInfo, instance); } return(methodInfo.GetReturnType(), methodInfo.DeclaringType, @delegate); }
public ClientSession(ServerConfiguration configuration, IEnumerable <IPacketController> packetControllers, IMapInstanceProvider mapInstanceProvider, IExchangeProvider exchangeProvider, ILogger logger) : base(logger) { _logger = logger; if (configuration is WorldConfiguration worldConfiguration) { WorldConfiguration = worldConfiguration; _mapInstanceProvider = mapInstanceProvider; _exchangeProvider = exchangeProvider; _isWorldClient = true; } foreach (var controller in packetControllers) { controller.RegisterSession(this); foreach (var methodInfo in controller.GetType().GetMethods().Where(x => typeof(PacketDefinition).IsAssignableFrom(x.GetParameters().FirstOrDefault()?.ParameterType))) { var type = methodInfo.GetParameters().FirstOrDefault()?.ParameterType; var packetheader = (PacketHeaderAttribute)Array.Find(type?.GetCustomAttributes(true), ca => ca.GetType() == typeof(PacketHeaderAttribute)); _headerMethod.Add(packetheader, new Tuple <IPacketController, Type>(controller, type)); _controllerMethods.Add(packetheader, DelegateBuilder.BuildDelegate <Action <object, object> >(methodInfo)); } } }
public static void Main() { Type type = typeof(MyType); var mi = type.GetMethod("SetValue"); var obj1 = new MyType(1); var obj2 = new MyType(2); var action = DelegateBuilder.BuildDelegate <Action <object, int> >(mi); action(obj1, 3); action(obj2, 4); Console.WriteLine(obj1.Value); Console.WriteLine(obj2.Value); // Sample passing a default value for the 2nd param of SetSumValue. var mi2 = type.GetMethod("SetSumValue"); var action2 = DelegateBuilder.BuildDelegate <Action <object, int> >(mi2, 10); action2(obj1, 3); action2(obj2, 4); Console.WriteLine(obj1.Value); Console.WriteLine(obj2.Value); // Sample without passing a default value for the 2nd param of SetSumValue. // It will just use the default int value that is 0. var action3 = DelegateBuilder.BuildDelegate <Action <object, int> >(mi2); action3(obj1, 3); action3(obj2, 4); Console.WriteLine(obj1.Value); Console.WriteLine(obj2.Value); }
private static CreateFunc GetCreateFunc() { var regis = LoquiRegistration.GetRegister(typeof(T)); if (regis == null) { throw new ArgumentException(); } var className = $"{regis.Namespace}.Internals.{regis.Name}BinaryOverlay"; var tType = regis.ClassType.Assembly.GetType(className) !; var method = tType.GetMethods() .Where((methodInfo) => methodInfo.Name.Equals($"{regis.Name}Factory")) .Where((methodInfo) => methodInfo.IsStatic && methodInfo.IsPublic) .Where((methodInfo) => { var param = methodInfo.GetParameters(); if (param.Length != 3) { return(false); } if (!param[0].ParameterType.Equals(typeof(OverlayStream))) { return(false); } if (!param[1].ParameterType.Equals(typeof(BinaryOverlayFactoryPackage))) { return(false); } if (!param[2].ParameterType.Equals(typeof(RecordTypeConverter))) { return(false); } return(true); }) .FirstOrDefault(); if (method != null) { return(DelegateBuilder.BuildDelegate <CreateFunc>(method)); } else { throw new ArgumentException(); } }
public Test(TestMethod testMethod) { var method = testMethod.Method; var type = method.DeclaringType; if (type == null) { throw new ArgumentException("Null declaring type", nameof(testMethod)); } _instance = method.IsStatic ? null : Activator.CreateInstance(type); _delegate = DelegateBuilder.BuildDelegate(method, _instance); _parameterInfos = method.GetParameters(); _isolatedThread = testMethod.TestAttribute.IsolatedThread; _preferredGenerators = testMethod.PreferredGenerators; Name = method.Name; _repeat = testMethod.TestAttribute.Repeat; }
public ClientSession(IChannel channel, bool isWorldClient) : base(channel) { _isWorldClient = isWorldClient; foreach (var controller in PacketControllerFactory.GenerateControllers()) { controller.RegisterSession(this); foreach (var methodInfo in controller.GetType().GetMethods().Where(x => x.GetParameters().FirstOrDefault()?.ParameterType.BaseType == typeof(PacketDefinition))) { var type = methodInfo.GetParameters().FirstOrDefault()?.ParameterType; var packetheader = (PacketHeaderAttribute)Array.Find(type?.GetCustomAttributes(true), ca => ca.GetType() == typeof(PacketHeaderAttribute)); _headerMethod.Add(packetheader, new Tuple <IPacketController, Type>(controller, type)); _controllerMethods.Add(packetheader, DelegateBuilder.BuildDelegate <Action <object, object> >(methodInfo)); } } }
public ClientSession(GameServerConfiguration configuration, IEnumerable <IPacketController> packetControllers) { _isWorldClient = configuration is WorldConfiguration; foreach (var controller in packetControllers) { controller.RegisterSession(this); foreach (var methodInfo in controller.GetType().GetMethods().Where(x => x.GetParameters().FirstOrDefault()?.ParameterType.BaseType == typeof(PacketDefinition))) { var type = methodInfo.GetParameters().FirstOrDefault()?.ParameterType; var packetheader = (PacketHeaderAttribute)Array.Find(type?.GetCustomAttributes(true), ca => ca.GetType() == typeof(PacketHeaderAttribute)); _headerMethod.Add(packetheader, new Tuple <IPacketController, Type>(controller, type)); _controllerMethods.Add(packetheader, DelegateBuilder.BuildDelegate <Action <object, object> >(methodInfo)); } } }
public ClientSession(IChannel channel, bool isWorldClient) : base(channel) { // set last received lastPacketReceive = DateTime.Now.Ticks; _random = new Random((int)ClientId); _isWorldClient = isWorldClient; foreach (var controller in PacketControllerFactory.GenerateControllers()) { controller.RegisterSession(this); foreach (MethodInfo methodInfo in controller.GetType().GetMethods().Where(x => x.GetParameters().FirstOrDefault()?.ParameterType.BaseType == typeof(PacketDefinition))) { var type = methodInfo.GetParameters().FirstOrDefault()?.ParameterType; PacketHeaderAttribute packetheader = (PacketHeaderAttribute)Array.Find(type.GetCustomAttributes(true), ca => ca.GetType().Equals(typeof(PacketHeaderAttribute))); HeaderMethod.Add(packetheader, new Tuple <IPacketController, Type>(controller, type)); ControllerMethods.Add(packetheader, DelegateBuilder.BuildDelegate <Action <object, object> >(methodInfo)); } } }
static Dictionary <string, Func <WBProjectOptions, WBSolver> > LazyFactory() { return(AppDomain.CurrentDomain .GetAssemblies() .SelectMany(ass => ass.GetTypes()) .Where(t => t.GetCustomAttributes(typeof(SolversFactoryAttribute), false).Length > 0) .SelectMany(t => t.GetMethods()) .Where(m => m.GetCustomAttributes(typeof(SolverGeneratorMethodAttribute), false).Length > 0) .ToDictionary( mi => { var attr = (SolverGeneratorMethodAttribute)mi.GetCustomAttributes(typeof(SolverGeneratorMethodAttribute), false)[0]; return attr.Name; }, mi => { return DelegateBuilder.BuildDelegate <Func <WBProjectOptions, WBSolver> >(mi); } )); }
private static CREATE_FUNC GetCreateFunc() { var tType = typeof(T); var method = tType.GetMethods() .Where((methodInfo) => methodInfo.Name.Equals("CreateFromBinary")) .Where((methodInfo) => methodInfo.IsStatic && methodInfo.IsPublic) .Where((methodInfo) => methodInfo.ReturnType.Equals(tType)) .Where((methodInfo) => methodInfo.GetParameters().Length == 2) .Where((methodInfo) => methodInfo.GetParameters()[0].ParameterType.Equals(typeof(MutagenFrame))) .Where((methodInfo) => methodInfo.GetParameters()[1].ParameterType.Equals(typeof(RecordTypeConverter))) .FirstOrDefault(); if (method != null) { return(DelegateBuilder.BuildDelegate <CREATE_FUNC>(method)); } else { throw new ArgumentException(); } }
private static CREATE_FUNC GetCreateFunc() { var tType = typeof(T); var options = tType.GetMethods() .Where((methodInfo) => methodInfo.Name.Equals("CreateFromXmlFolder")) .Where((methodInfo) => methodInfo.IsStatic && methodInfo.IsPublic) .Where((methodInfo) => methodInfo.ReturnType.Equals(typeof(Task <T>))) .Where((methodInfo) => methodInfo.GetParameters().Length == 4) .Where((methodInfo) => methodInfo.GetParameters()[0].ParameterType.Equals(typeof(XElement))) .Where((methodInfo) => methodInfo.GetParameters()[1].ParameterType.Equals(typeof(string))) .Where((methodInfo) => methodInfo.GetParameters()[2].ParameterType.Equals(typeof(ErrorMaskBuilder))) .Where((methodInfo) => methodInfo.GetParameters()[3].ParameterType.Equals(typeof(TranslationCrystal))) .ToArray(); var method = options .FirstOrDefault(); if (method != null) { return(DelegateBuilder.BuildDelegate <CREATE_FUNC>(method)); } else { return(async(node, path, errorMask, translMask) => { if (LoquiXmlTranslation <T> .Instance.Parse( node !, out var item, errorMask: errorMask, translationMask: translMask)) { return item; } throw new NotImplementedException(); }); } }
private void GenerateHandlerReferences(Type type) { //iterate thru each type in the given assembly, the IPacketHandler is expected in the same dll foreach (Type handlerType in type.Assembly.GetTypes().Where(p => !p.IsInterface && type.GetInterfaces().FirstOrDefault().IsAssignableFrom(p))) { object handler = Activator.CreateInstance(handlerType, new object[] { this }); foreach (MethodInfo methodInfo in handlerType.GetMethods().Where(x => x.GetCustomAttributes(false).OfType <Packet>().Any())) { Packet packetAttribute = methodInfo.GetCustomAttributes(false).OfType <Packet>().FirstOrDefault(); if (packetAttribute != null) { HandlerMethods.Add(packetAttribute, new Tuple <Action <object, string>, object>(DelegateBuilder.BuildDelegate <Action <object, string> >(methodInfo), handler)); } } } }
private void GenerateHandlerReferences(Type type, bool isWorldServer) { IEnumerable <Type> handlerTypes = !isWorldServer?type.Assembly.GetTypes().Where(t => t.Name.Equals("LoginPacketHandler")) //shitty but it works : type.Assembly.GetTypes().Where(p => !p.IsInterface && type.GetInterfaces().FirstOrDefault().IsAssignableFrom(p)); //iterate thru each type in the given assembly, the IPacketHandler is expected in the same dll foreach (Type handlerType in handlerTypes) { object handler = Activator.CreateInstance(handlerType, new object[] { this }); foreach (MethodInfo methodInfo in handlerType.GetMethods().Where(x => x.GetCustomAttributes(false).OfType <PacketAttribute>().Any())) { PacketAttribute Packet = methodInfo.GetCustomAttributes(false).OfType <PacketAttribute>().FirstOrDefault(); if (Packet != null) { HandlerMethods.Add(Packet, new Tuple <Action <object, string>, object>(DelegateBuilder.BuildDelegate <Action <object, string> >(methodInfo), handler)); } } } }
private void GenerateHandlerReferences(Type type, bool isWorldServer) { IEnumerable <Type> handlerTypes = !isWorldServer?type.Assembly.GetTypes().Where(t => t.Name.Equals("LoginPacketHandler")) // shitty but it works : type.Assembly.GetTypes().Where(p => { Type interfaceType = type.GetInterfaces().FirstOrDefault(); return(interfaceType != null && !p.IsInterface && interfaceType.IsAssignableFrom(p)); }); // iterate thru each type in the given assembly foreach (Type handlerType in handlerTypes) { IPacketHandler handler = (IPacketHandler)Activator.CreateInstance(handlerType, this); // include PacketDefinition foreach (MethodInfo methodInfo in handlerType.GetMethods().Where(x => x.GetCustomAttributes(false).OfType <PacketAttribute>().Any() || x.GetParameters().FirstOrDefault()?.ParameterType.BaseType == typeof(PacketDefinition))) { List <PacketAttribute> packetAttributes = methodInfo.GetCustomAttributes(false).OfType <PacketAttribute>().ToList(); // assume PacketDefinition based handler method if (!packetAttributes.Any()) { HandlerMethodReference methodReference = new HandlerMethodReference(DelegateBuilder.BuildDelegate <Action <object, object> >(methodInfo), handler, methodInfo.GetParameters().FirstOrDefault()?.ParameterType); HandlerMethods.Add(methodReference.Identification, methodReference); } else { // assume string based handler method foreach (PacketAttribute packetAttribute in packetAttributes) { HandlerMethodReference methodReference = new HandlerMethodReference(DelegateBuilder.BuildDelegate <Action <object, object> >(methodInfo), handler, packetAttribute); HandlerMethods.Add(methodReference.Identification, methodReference); } } } } }
private void GenerateHandlerReferences(IEnumerable <IPacketHandler> packetDictionary) { // iterate thru each type in the given assembly foreach (IPacketHandler handlerType in packetDictionary) { IPacketHandler handler = (IPacketHandler)Activator.CreateInstance(handlerType.GetType(), this); // include PacketDefinition foreach (MethodInfo methodInfo in handlerType.GetType().GetMethods().Where(x => x.GetParameters().FirstOrDefault()?.ParameterType.BaseType == typeof(PacketDefinition))) { HandlerMethodReference methodReference = new HandlerMethodReference(DelegateBuilder.BuildDelegate <Action <object, object> >(methodInfo), handler, methodInfo.GetParameters().FirstOrDefault()?.ParameterType); HandlerMethods.Add(methodReference.Identification, methodReference); } } }