diff --git a/Articulate-Network.csproj b/Articulate-Network.csproj
index 7340b71..b21d3b6 100644
--- a/Articulate-Network.csproj
+++ b/Articulate-Network.csproj
@@ -41,6 +41,7 @@
+
diff --git a/Attributes/PacketIgnore.cs b/Attributes/PacketIgnore.cs
new file mode 100644
index 0000000..dbbd96d
--- /dev/null
+++ b/Attributes/PacketIgnore.cs
@@ -0,0 +1,13 @@
+using System;
+using System.Collections.Generic;
+using System.Linq;
+using System.Text;
+using System.Threading.Tasks;
+
+namespace Articulate_Network.Attributes
+{
+ public class PacketIgnore : System.Attribute
+ {
+
+ }
+}
diff --git a/Interfaces/IManager.cs b/Interfaces/IManager.cs
index eaadacd..bb79ee7 100644
--- a/Interfaces/IManager.cs
+++ b/Interfaces/IManager.cs
@@ -6,7 +6,7 @@ using System.Threading.Tasks;
namespace Articulate_Network.Interfaces
{
- interface IManager
+ public interface IManager
{
void Initialize();
}
diff --git a/Managers/PacketManager.cs b/Managers/PacketManager.cs
index 1bae597..caf4001 100644
--- a/Managers/PacketManager.cs
+++ b/Managers/PacketManager.cs
@@ -1,6 +1,8 @@
-using Articulate_Network.Interfaces;
+using Articulate_Network.Attributes;
+using Articulate_Network.Interfaces;
using System;
using System.Collections.Generic;
+using System.IO;
using System.Linq;
using System.Reflection;
using System.Text;
@@ -10,11 +12,75 @@ namespace Articulate_Network.Managers
{
public class PacketManager : IManager
{
- List packets = new List();
+ Dictionary> packetInfo = new Dictionary>();
public void Initialize()
{
- Console.WriteLine(Assembly.GetEntryAssembly());
+
+ }
+
+ public void LoadPacketInfo(IPacket packet)
+ {
+ packetInfo.Add(packet.GetType(), packet.GetType().GetProperties().Where(f => !Attribute.IsDefined(f, typeof(PacketIgnore))).OrderBy(f => f.Name).ToList());
+ }
+
+ public T Send(IPacket packet)
+ {
+ if (!packetInfo.ContainsKey(packet.GetType()))
+ LoadPacketInfo(packet);
+
+
+ //send packet
+
+ var serialized = SerializePacket(packet);
+
+ var deserialized = DeserializePacket(serialized);
+
+ return (T)deserialized;
+ }
+
+ public MemoryStream SerializePacket(IPacket packet)
+ {
+ var fields = packetInfo[packet.GetType()];
+
+ MemoryStream memoryStream = new MemoryStream();
+ BinaryWriter binaryWriter = new BinaryWriter(memoryStream);
+
+ foreach (var field in fields)
+ {
+ if (field.PropertyType == typeof(bool))
+ binaryWriter.Write((bool)field.GetValue(packet));
+
+ if (field.PropertyType == typeof(int))
+ binaryWriter.Write((int)field.GetValue(packet));
+
+ if (field.PropertyType == typeof(string))
+ binaryWriter.Write((string)field.GetValue(packet));
+ }
+
+ return memoryStream;
+ }
+
+ public T DeserializePacket(Stream stream)
+ {
+ IPacket packet = (IPacket)Activator.CreateInstance(typeof(T));
+
+ var fields = packetInfo[typeof(T)];
+ BinaryReader reader = new BinaryReader(stream);
+
+ foreach (var field in fields)
+ {
+ if (field.PropertyType == typeof(bool))
+ field.SetValue(packet, reader.ReadBoolean());
+
+ if (field.PropertyType == typeof(int))
+ field.SetValue(packet, reader.ReadInt32());
+
+ if (field.PropertyType == typeof(string))
+ field.SetValue(packet, reader.ReadString());
+ }
+
+ return (T)packet;
}
}
}
diff --git a/Server.cs b/Server.cs
index 3fc8c33..045873b 100644
--- a/Server.cs
+++ b/Server.cs
@@ -1,9 +1,11 @@
using Articulate_Network.Interfaces;
+using Articulate_Network.Managers;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Net.Sockets;
+using System.Reflection;
using System.Text;
using System.Threading.Tasks;
@@ -15,11 +17,18 @@ namespace Articulate_Network
TcpListener tcp;
+ public Server()
+ {
+ InitializeManagers();
+
+ Console.WriteLine($"Initialized {managers.Count} manager{(managers.Count > 1 ? "s" : "")}");
+ }
+
public async Task Start(string ip, int port)
{
tcp = new TcpListener(IPAddress.Parse(ip), port);
- bool connected = await Task.Run(() =>
+ bool connected = await Task.Run(async () =>
{
try
{
@@ -42,5 +51,29 @@ namespace Articulate_Network
{
return false;
}
+
+ public void InitializeManagers()
+ {
+ var @interface = typeof(IManager);
+
+ foreach (Assembly assembly in AppDomain.CurrentDomain.GetAssemblies())
+ {
+ var managersFound = assembly.GetTypes().Where(t => @interface.IsAssignableFrom(t) && !t.IsAbstract);
+
+ foreach (var m in managersFound)
+ {
+ var manager = (IManager)Activator.CreateInstance(m);
+
+ manager.Initialize();
+
+ managers.Add(manager);
+ }
+ }
+ }
+
+ public T Get()
+ {
+ return (T)managers.SingleOrDefault(t => typeof(T) == t.GetType());
+ }
}
}