| 12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273 |
- using System;
- using System.Collections.Generic;
- using System.Reflection;
- using System.Threading.Tasks;
- using Common.Base;
- using Common.Helper;
- namespace Model
- {
- public class MessageComponent: Component<World>, IAssemblyLoader
- {
- private Dictionary<Opcode, Func<byte[], byte[]>> events;
- private Dictionary<Opcode, Func<byte[], Task<byte[]>>> eventsAsync;
- public void Load(Assembly assembly)
- {
- this.events = new Dictionary<Opcode, Func<byte[], byte[]>>();
- this.eventsAsync = new Dictionary<Opcode, Func<byte[], Task<byte[]>>>();
- ServerType serverType = World.Instance.Options.ServerType;
- Type[] types = assembly.GetTypes();
- foreach (Type t in types)
- {
- object[] attrs = t.GetCustomAttributes(typeof (MessageAttribute), false);
- if (attrs.Length == 0)
- {
- continue;
- }
- MessageAttribute messageAttribute = (MessageAttribute)attrs[0];
- if (!messageAttribute.Contains(serverType))
- {
- continue;
- }
-
- object obj = Activator.CreateInstance(t);
- IRegister iRegister = obj as IRegister;
- iRegister?.Register();
- throw new Exception($"message handler not inherit IRegister interface: {obj.GetType().FullName}");
- }
- }
- public void Register<T, R>(Func<T, R> func)
- {
- Opcode opcode = EnumHelper.FromString<Opcode>(typeof (T).Name);
- events.Add(opcode, messageBytes =>
- {
- T t = MongoHelper.FromBson<T>(messageBytes, 6);
- R k = func(t);
- return MongoHelper.ToBson(k);
- });
- }
- public async Task<byte[]> RunAsync(Opcode opcode, byte[] messageBytes)
- {
- Func<byte[], byte[]> func = null;
- if (this.events.TryGetValue(opcode, out func))
- {
- return func(messageBytes);
- }
- Func<byte[], Task<byte[]>> funcAsync = null;
- if (this.eventsAsync.TryGetValue(opcode, out funcAsync))
- {
- return await funcAsync(messageBytes);
- }
- throw new GameException($"not found opcode handler: {opcode}");
- }
- }
- }
|