Beispiel #1
0
 public void LeaveWorld()
 {
     try {
         this.Player.InWorld = false;
         if (m_player.Group != null)
         {
             if (m_player.IsLeader || m_player.Group.Size < 3)
             {
                 m_player.Group.Destroy();
             }
             else
             {
                 m_player.Group.RemoveMember(m_player.Name);
             }
         }
         ChannelManager.Deconnection(this);
         m_player.SaveAndRemove();
         WorldServer.RemoveClient(this);
         WorldPacket pkg = new WorldPacket(WORLDMSG.PLAYER_LEAVE_WORLD);
         pkg.Write(m_character.ObjectId);
         WorldServer.Send(pkg);
     } catch (Exception exp) {
         DebugLogger.Log("", exp);
     }
 }
Beispiel #2
0
 private static void OnLoginServerData(byte[] data)
 {
     try {
         BinReader read = new BinReader(data);
         read.BaseStream.Position += 4;                 // skip len
         WORLDMSG msgID = (WORLDMSG)read.ReadInt32();
         if (msgID == WORLDMSG.CLIENT_MESSAGE)
         {
             uint        charID = read.ReadUInt32();
             CMSG        cmsg   = (CMSG)read.ReadInt32();
             WorldClient client = GetClientByCharacterID(charID);
             if (client != null)
             {
                 WorldPacketManager.HandlePacket(client, cmsg, read);
             }
             else
             {
                 Console.WriteLine("Client(" + charID + ") was missing when " + cmsg.ToString() + " was received.");
             }
         }
         else if (msgID == WORLDMSG.SCRIPT_MESSAGE)
         {
             int msg = read.ReadInt32();
             Scripts.OnScriptMessage(msg, read);
         }
         else
         {
             WorldPacketManager.HandlePacket(msgID, read);
         }
     } catch (Exception exp) {
         DebugLogger.Log("", exp);
     }
 }
Beispiel #3
0
        private static void mainThread()
        {
            Thread.CurrentThread.CurrentCulture = CultureInfo.CreateSpecificCulture("en-US");
            try
            {
                m_running = true;
                int shutdownstage = 0;
                while (true)
                {
                    if (!processConnection())
                    {
                        // Todo handle this
                        break;
                    }

                    EventManager.CheckEvents();

                    if (m_shutdown == true)
                    {
                        if (shutdownstage == 0)
                        {
                            // save and remove everything
                            ArrayList   clients = new ArrayList(m_clients.Values);
                            IEnumerator e       = clients.GetEnumerator();
                            while (e.MoveNext())
                            {
                                WorldClient client = (WorldClient)e.Current;
                                client.LeaveWorld();
                            }
                            shutdownstage = 1;
                        }
                        if (shutdownstage == 1 && DBManager.CreateRequestsPending() == 0)
                        {
                            Send(new WorldPacket(WORLDMSG.WORLD_SHUTDOWN));
                            shutdownstage = 2;
                        }
                        if (shutdownstage == 2 && m_connection.PendingSendData == false)
                        {
                            break;
                        }
                    }
                    Thread.Sleep(5);
                }
            }
            catch (ThreadAbortException)
            {
            }
            catch (Exception e)
            {
                Console.WriteLine("Unhandled worldserver exception!");
                Console.WriteLine(e.Message);
                Console.WriteLine(e.StackTrace);
                DebugLogger.Log("", e);
            }
            ObjectManager.CheckBeforeShutdown();
            DBManager.CheckBeforeShutdown();
            m_connection.Close("Server shutting down.");
            m_running = false;
        }
Beispiel #4
0
 public static void AddWorldObject(WorldObject obj)
 {
     try {
         m_worldObjects[(int)obj.ObjectType][obj.GUID] = obj;
     } catch (Exception exp) {
         DebugLogger.Log("", exp);
     }
 }
Beispiel #5
0
 public static void RemoveWorldObject(WorldObject obj)
 {
     try {
         m_worldObjects[(int)obj.ObjectType].Remove(obj.GUID);
     } catch (Exception exp) {
         DebugLogger.Log("", exp);
     }
 }
Beispiel #6
0
 internal static void RemoveClient(WorldClient client)
 {
     try {
         m_clients.Remove(client.CharacterID);
     } catch (Exception exp) {
         DebugLogger.Log("", exp);
     }
 }
Beispiel #7
0
 internal static void AddClient(WorldClient client)
 {
     try {
         m_clients[client.CharacterID] = client;
     } catch (Exception exp) {
         DebugLogger.Log("", exp);
     }
 }
Beispiel #8
0
 public static ArrayList GetAllObjects(OBJECTTYPE type)
 {
     try {
         return(new ArrayList(m_worldObjects[(int)type].Values));
     } catch (Exception exp) {
         DebugLogger.Log("", exp);
     }
     return(null);
 }
 void SearchForClientPacketDelegates(Type type)
 {
     try {
         MethodInfo[] methods = type.GetMethods(BindingFlags.NonPublic | BindingFlags.Public | BindingFlags.Instance | BindingFlags.Static);
         foreach (MethodInfo method in methods)
         {
             WorldPacketDelegate[] attribs = (WorldPacketDelegate[])method.GetCustomAttributes(typeof(WorldPacketDelegate), true);
             if (attribs.Length == 0)
             {
                 continue;
             }
             if (method.IsStatic)
             {
                 foreach (WorldPacketDelegate attrib in attribs)
                 {
                     if (attrib.ClientMessage)
                     {
                         WorldClientPacketDelegate wcpd = (WorldClientPacketDelegate)Delegate.CreateDelegate(typeof(WorldClientPacketDelegate), method);
                         WorldPacketManager.RegisterPacketHandler((CMSG)attrib.MsgID, wcpd);
                         if (m_clientPacketDelegates.Contains(attrib.MsgID))
                         {
                             WorldClientPacketDelegate dele = (WorldClientPacketDelegate)m_clientPacketDelegates[attrib.MsgID];
                             m_clientPacketDelegates[attrib.MsgID] = dele + wcpd;
                         }
                         else
                         {
                             m_clientPacketDelegates[attrib.MsgID] = wcpd;
                         }
                     }
                 }
             }
             else
             {
                 object obj = GetHandlerObject(type);
                 foreach (WorldPacketDelegate attrib in attribs)
                 {
                     if (attrib.ClientMessage)
                     {
                         WorldClientPacketDelegate wcpd = (WorldClientPacketDelegate)Delegate.CreateDelegate(typeof(WorldClientPacketDelegate), obj, method.Name);
                         WorldPacketManager.RegisterPacketHandler((CMSG)attrib.MsgID, wcpd);
                         if (m_clientPacketDelegates.Contains(attrib.MsgID))
                         {
                             WorldClientPacketDelegate dele = (WorldClientPacketDelegate)m_clientPacketDelegates[attrib.MsgID];
                             m_clientPacketDelegates[attrib.MsgID] = dele + wcpd;
                         }
                         else
                         {
                             m_clientPacketDelegates[attrib.MsgID] = wcpd;
                         }
                     }
                 }
             }
         }
     } catch (Exception exp) {
         DebugLogger.Log("", exp);
     }
 }
Beispiel #10
0
 public static WorldObject GetWorldObject(OBJECTTYPE type, ulong guid)
 {
     try {
         return((WorldObject)m_worldObjects[(int)type][guid]);
     } catch (Exception exp) {
         DebugLogger.Log("", exp);
     }
     return(null);
 }
Beispiel #11
0
 public static void Send(WorldPacket pkg)
 {
     try {
         pkg.Set(0, (int)(pkg.BaseStream.Length - 4));
         m_connection.Send(pkg.GetBuffer(), pkg.BaseStream.Length);
     } catch (Exception exp) {
         DebugLogger.Log("", exp);
     }
 }
Beispiel #12
0
 public static WorldClient GetClientByCharacterID(uint id)
 {
     try {
         return((WorldClient)m_clients[id]);
     } catch (Exception exp) {
         DebugLogger.Log("", exp);
     }
     return(null);
 }
Beispiel #13
0
 static void OnAcquireGuids(WORLDMSG msgID, BinReader data)
 {
     try {
         m_guidpool.Enqueue(data.ReadUInt64());
         m_guidpool.Enqueue(data.ReadUInt64());
     } catch (Exception exp) {
         DebugLogger.Log("", exp);
     }
 }
Beispiel #14
0
 static void OnInitGuids(WORLDMSG msgID, BinReader data)
 {
     try {
         m_currentGUID = data.ReadUInt64();
         m_currentMax  = data.ReadUInt64();
         m_guidpool.Enqueue(data.ReadUInt64());
         m_guidpool.Enqueue(data.ReadUInt64());
     } catch (Exception exp) {
         DebugLogger.Log("", exp);
     }
 }
Beispiel #15
0
        public override void FireEvent()
        {
            // Mob has changed target
            if (this.currenttarget != mob.Target)
            {
                client = (WorldClient)WorldServer.GetClientByCharacterID((uint)this.mob.Target);
                if (client == null)
                {
                    this.mob.StopCombat();
                }
                else
                {
                    this.currenttarget = mob.Target;
                }
            }
            //Console.WriteLine("ChasingEvent.FireEvent (" + (client != null ? client.Player.Name : "") + "): " + mob.Name);

            if (client != null && client.Player.InWorld && !client.Player.Dead && !mob.Dead)
            {
                try
                {
                    float distance = client.Player.Position.Distance(mob.Position);
                    //				if(distance > 4)
                    //				{
                    int          time = (int)((distance / mob.RunningSpeed) * 1000);
                    ServerPacket pkg  = new ServerPacket(SMSG.MONSTER_MOVE);
                    pkg.Write(mob.GUID);
                    pkg.WriteVector(mob.Position);
                    pkg.Write(mob.Facing);
                    pkg.Write((byte)0);
                    pkg.Write(0x100);
                    pkg.Write(time);
                    pkg.Write(1);
                    if (client != null && client.Player != null)
                    {
                        mob.Position.Translate(client.Player.Position, 1.5f - (client.Player.Position.Distance(client.Player.LastPosition) * 0.5f));
                    }

                    pkg.Write(mob.Position.X);
                    pkg.Write(mob.Position.Y);
                    pkg.Write(mob.Position.Z);
                    pkg.Finish();
                    mob.MapTile.SendSurrounding(pkg);
                    mob.UpdateData();
                    client.Player.UpdateData();
                }
                catch (Exception exp)
                {
                    DebugLogger.Log("", exp);
                }
                //				eventTime = DateTime.Now.Add(TimeSpan.FromSeconds(1));
                //				EventManager.AddEvent(this);
            }
        }
 internal void OnScriptMessage(int msgID, BinReader data)
 {
     try {
         ScriptPacketDelegate handler = (ScriptPacketDelegate)m_scriptPacketHandlers[msgID];
         if (handler != null)
         {
             handler(msgID, data);
         }
     } catch (Exception exp) {
         DebugLogger.Log("", exp);
     }
 }
Beispiel #17
0
 public void Send(SMSG msgID, byte[] data, int index, int count)
 {
     try {
         ServerPacket pkg = new ServerPacket(msgID);
         pkg.Write(data, index, count);
         pkg.Finish();
         pkg.AddDestination(m_character.ObjectId);
         WorldServer.Send(pkg);
     } catch (Exception exp) {
         DebugLogger.Log("", exp);
     }
 }
Beispiel #18
0
 public void Send(SMSG msgID, BinWriter data)
 {
     try {
         ServerPacket pkg = new ServerPacket(msgID);
         pkg.Write(data.GetBuffer(), 0, (int)data.BaseStream.Length);
         pkg.Finish();
         pkg.AddDestination(m_character.ObjectId);
         WorldServer.Send(pkg);
     } catch (Exception exp) {
         DebugLogger.Log("", exp);
     }
 }
Beispiel #19
0
 public static void CheckBeforeShutdown()
 {
     try {
         for (int i = 0; i < (int)OBJECTTYPE.MAX; i++)
         {
             if (m_worldObjects[i].Count > 0)
             {
                 Console.WriteLine("Still " + m_worldObjects[i].Count + " " + ((OBJECTTYPE)i).ToString() + " in ObjectManager before shutting down.");
             }
         }
     } catch (Exception exp) {
         DebugLogger.Log("", exp);
     }
 }
Beispiel #20
0
 public static WorldObject GetWorldObjectByGUID(ulong guid)
 {
     try {
         for (int i = 0; i < (int)OBJECTTYPE.MAX; i++)
         {
             if (((WorldObject)m_worldObjects[i][guid]) != null)
             {
                 return((WorldObject)m_worldObjects[i][guid]);
             }
         }
     } catch (Exception exp) {
         DebugLogger.Log("", exp);
     }
     return(null);
 }
Beispiel #21
0
 public static void WriteDataUpdate(BinWriter w, BinaryTree values, object obj, bool clear, bool isClient)
 {
     try {
         //Console.WriteLine("Type: "+obj.GetType());
         UpdateObjectInfo info = (UpdateObjectInfo)m_updateObjectInfos[obj.GetType()];
         if (info == null)
         {
             throw new ObjectUpdateManagerException("UpdateObjectInfo is missing for type " + obj.GetType());
         }
         if (!isClient && obj.GetType() == typeof(PlayerObject))
         {
             info.MaxFields = (int)PLAYERFIELDS.MAX_NOTCLIENT;
             info.BlockSize = (byte)((info.MaxFields + 31) / 32);
         }
         w.Write(info.BlockSize);
         byte[]      mask    = new byte[info.BlockSize * 4];
         IEnumerator e       = values.GetEnumerator();
         Hashtable   tbl     = info.tbl;
         long        maskPos = w.BaseStream.Position;
         w.BaseStream.Position += mask.Length;
         while (e.MoveNext())
         {
             if ((!isClient && ((int)e.Current <= (int)PLAYERFIELDS.MAX_NOTCLIENT)) || (isClient))
             {
                 IUpdateValue updater = (IUpdateValue)tbl[(int)e.Current];
                 if (updater == null)
                 {
                     throw new ObjectUpdateManagerException("UpdateValue Handler is missing for field " + e.Current + " in " + obj.GetType().ToString());
                 }
                 updater.WriteValue(obj, w, mask);
             }
         }
         long pos = w.BaseStream.Position;
         w.BaseStream.Position = maskPos;
         w.Write(mask, 0, mask.Length);
         w.BaseStream.Position = pos;
         if (clear)
         {
             values.Clear();
         }
         else
         {
             e.Reset();
         }
     } catch (Exception exp) {
         DebugLogger.Log("", exp);
     }
 }
Beispiel #22
0
        public static ulong NextGUID()
        {
            ulong guid = m_currentGUID++;

            try {
                if (m_currentGUID == m_currentMax)
                {
                    m_currentGUID = (ulong)m_guidpool.Dequeue();
                    m_currentMax  = (ulong)m_guidpool.Dequeue();
                    WorldServer.Send(new WorldPacket(WORLDMSG.ACQUIRE_GUIDS));
                }
            } catch (Exception exp) {
                DebugLogger.Log("", exp);
            }
            return(guid);
        }
Beispiel #23
0
 public static WorldClient GetClientByName(string name)
 {
     try {
         int numclient = m_clients.Count;
         System.Collections.IDictionaryEnumerator ienum = m_clients.GetEnumerator();
         while (ienum.MoveNext())
         {
             if (((WorldClient)ienum.Value).Player.Name == name)
             {
                 return((WorldClient)ienum.Value);
             }
         }
     } catch (Exception exp) {
         DebugLogger.Log("", exp);
     }
     return(null);
 }
 public static void HandlePacket(WorldClient client, CMSG msgID, BinReader data)
 {
     try {
         IWorldClientPacketHandler handler = (IWorldClientPacketHandler)worldClientHandlers[msgID];
         if (handler != null)
         {
             handler.HandlePacket(client, msgID, data);
         }
         WorldClientPacketDelegate wcpd = (WorldClientPacketDelegate)worldClientDelegates[(int)msgID];
         if (wcpd != null)
         {
             wcpd(client, msgID, data);
         }
     } catch (Exception exp) {
         DebugLogger.Log("", exp);
     }
 }
 public static void HandlePacket(WORLDMSG msgID, BinReader data)
 {
     try {
         IWorldServerPacketHandler handler = (IWorldServerPacketHandler)worldServerHandlers[msgID];
         if (handler != null)
         {
             handler.HandlePacket(msgID, data);
         }
         WorldServerPacketDelegate wspd = (WorldServerPacketDelegate)worldServerDelegates[(int)msgID];
         if (wspd != null)
         {
             wspd(msgID, data);
         }
     } catch (Exception exp) {
         DebugLogger.Log("", exp);
     }
 }
Beispiel #26
0
        internal void CreatePlayerObject()
        {
            try {
                // at enter world
                BinWriter w = new BinWriter();
                w.Write(0);
                w.Write((byte)0);                 //A9 Fix by Phaze
                w.Set(0, m_player.Inventory.AddCreateInventory(w, true) + 1);
                m_player.AddCreateObject(w, true, true);
                BinWriter pkg = new BinWriter();
                pkg.Write((int)w.BaseStream.Length);

                pkg.Write(ZLib.Compress(w.GetBuffer(), 0, (int)w.BaseStream.Length));
                Send(SMSG.COMPRESSED_UPDATE_OBJECT, pkg);

                m_player.updateTime();
            } catch (Exception exp) {
                DebugLogger.Log("", exp);
            }
        }
Beispiel #27
0
 public static void SearchAssemblyForUpdateObjects(Assembly assembly)
 {
     try {
         foreach (Type type in assembly.GetTypes())
         {
             if (type.IsClass == false)
             {
                 continue;
             }
             if (type.IsAbstract == true)
             {
                 continue;
             }
             if (type.IsDefined(typeof(UpdateObjectAttribute), true))
             {
                 RegisterUpdateObject(type);
             }
         }
     } catch (Exception exp) {
         DebugLogger.Log("", exp);
     }
 }
Beispiel #28
0
 private static bool processConnection()
 {
     try {
         if (m_connection.PendingSendData)
         {
             m_connection.SendWork();
         }
         if (m_connection.Connected == false)
         {
             return(false);
         }
         byte[] data;
         // Hmm, bah! if it is not able to handle all the data there's something wrong but not here:)
         while ((data = m_connection.GetNextPacketData()) != null)
         {
             OnLoginServerData(data);
         }
         return(m_connection.Connected);
     } catch (Exception exp) {
         DebugLogger.Log("", exp);
     }
     return(false);
 }
Beispiel #29
0
        public static void RegisterUpdateObject(Type type)
        {
            try {
                object[] tmp = type.GetCustomAttributes(typeof(UpdateObjectAttribute), true);
                if (tmp.Length == 0)
                {
                    throw new ObjectUpdateManagerException(type.ToString() + " has no UpdateObjectAttribute");
                }
                UpdateObjectAttribute updateObjectAttribute = (UpdateObjectAttribute)tmp[0];
                if (updateObjectAttribute.MaxFields == -1)
                {
                    throw new ObjectUpdateManagerException("MaxFields is not set in UpdateObjectAttribute on " + type.ToString());
                }

                Hashtable tbl = new Hashtable();
                foreach (MemberValue value in MemberValue.GetMemberValues(type, typeof(UpdateValueAttribute), true, false))
                {
                    UpdateValueAttribute attrib = (UpdateValueAttribute)value.Attribute;
                    if (!checkType(attrib, type))
                    {
                        continue;
                    }
                    Type valueType = value.GetValueType();
                    if (valueType.IsArray == false)
                    {
                        if (!IsSubClass(valueType))
                        {
                            if (attrib.Field == -1)
                            {
                                throw new ObjectUpdateManagerException("Field was not set on " + type.Name + value.GetName());
                            }
                            if (value is IndexMemberValue)
                            {
                                IndexMemberValue indexValue = value as IndexMemberValue;
                                if (valueType == typeof(ulong) || valueType == typeof(long))
                                {
                                    AddUpdateValue(tbl, value, attrib.Field + indexValue.Index * 2);
                                }
                                else
                                {
                                    AddUpdateValue(tbl, value, attrib.Field + indexValue.Index);
                                }
                            }
                            else
                            {
                                AddUpdateValue(tbl, value, attrib.Field);
                            }
                        }
                        else
                        {
                            GetSubClassUpdateValues(tbl, value, attrib.Field == -1 ? 0 : attrib.Field, valueType, type);
                        }
                    }
                    else
                    {
                        if (attrib.ArraySize == -1)
                        {
                            throw new ObjectUpdateManagerException("ArraySize was not set on " + type.Name + "." + value.GetName());
                        }
                        if (attrib.Field == -1)
                        {
                            throw new ObjectUpdateManagerException("Field was not set on the array " + type.Name + "." + value.GetName());
                        }
                        if (attrib.NumSubFields == -1)
                        {
                            throw new ObjectUpdateManagerException("NumFields was not set on the subclass array " + type.Name + "." + value.GetName());
                        }
                        valueType = valueType.GetElementType();
                        for (int i = 0; i < attrib.ArraySize; i++)
                        {
                            IndexMemberValue indexValue = new IndexMemberValue(value, i);
                            GetSubClassUpdateValues(tbl, indexValue, attrib.Field + i * attrib.NumSubFields, valueType, type);
                        }
                    }
                }

                if (tbl.Count == 0)
                {
                    throw new ObjectUpdateManagerException("No update values in " + type.ToString());
                }
                UpdateObjectInfo uoi = new UpdateObjectInfo(tbl, updateObjectAttribute.MaxFields);
                m_updateObjectInfos[type] = uoi;
            } catch (Exception exp) {
                DebugLogger.Log("", exp);
            }
        }
Beispiel #30
0
        public void DealDamage(PlayerObject pobj, int damage)
        {
            if (!this.Dead)
            {
                if (m_agrotable[pobj.CharacterID] == null)
                {
                    m_agrotable.Add(pobj.CharacterID, damage);
                }
                else
                {
                    int damagedealt = (int)m_agrotable[pobj.CharacterID];
                    damagedealt += damage;
                    m_agrotable[pobj.CharacterID] = damagedealt;
                }

                this.Health -= damage;
                if (this.Health < 1)
                {
                    this.Roaming = false;
                    EventManager.AddEvent(new CorpseDespawnEvent((UnitBase)this));
                    this.Health       = 0;
                    this.StandState   = UNITSTANDSTATE.DEAD;
                    this.Dead         = true;
                    this.DynamicFlags = 1;                     // If set to 1 there will be loot.
                    this.LootOwner    = pobj.GUID;
                    this.UpdateData();

                    try {
                        // Give xp to the right people.
                        int       exp             = StatManager.CalculateExp(this, pobj);
                        int       maxdamage       = 0;
                        Hashtable expdividergroup = new Hashtable();
                        Hashtable expdividersolo  = new Hashtable();
                        foreach (DictionaryEntry de in m_agrotable)
                        {
                            maxdamage += (int)de.Value;
                            WorldClient aclient = WorldServer.GetClientByCharacterID((uint)de.Key);
                            if (aclient.Player.Group == null)
                            {
                                expdividersolo.Add(aclient.Player.CharacterID, (int)de.Value);
                            }
                            else
                            {
                                if (expdividergroup[aclient.Player.Group.LeaderGUID] == null)
                                {
                                    expdividergroup.Add(aclient.Player.Group.LeaderGUID, (int)de.Value);
                                }
                                else
                                {
                                    int damagedealt = (int)expdividergroup[aclient.Player.Group.LeaderGUID];
                                    damagedealt += (int)de.Value;
                                    expdividergroup[aclient.Player.Group.LeaderGUID] = damagedealt;
                                }
                            }
                        }

                        // Solo players
                        foreach (DictionaryEntry de in expdividersolo)
                        {
                            float       expbit  = ((int)de.Value / (float)maxdamage);
                            WorldClient aclient = WorldServer.GetClientByCharacterID((uint)de.Key);
                            if (aclient != null)
                            {
                                aclient.Player.GainXp(Convert.ToInt32(exp * expbit), this.GUID);
                            }
                        }

                        // Group players
                        foreach (DictionaryEntry de in expdividergroup)
                        {
                            WorldClient aclient = WorldServer.GetClientByCharacterID(Convert.ToUInt32((ulong)de.Key));
                            if (aclient != null)
                            {
                                GroupObject gobj   = aclient.Player.Group;
                                float       expbit = (((int)de.Value / (float)maxdamage) / (float)gobj.Size);
                                for (int i = 0; i < gobj.Size; i++)
                                {
                                    gobj.Members[i].GainXp(Convert.ToInt32(exp * expbit), this.GUID);
                                }
                            }
                        }
                    } catch (Exception exp) {
                        DebugLogger.Log("", exp);
                    }
                }
                else
                {
                    this.UpdateData();
                    if (this.MobsRegenEvent == null)
                    {
                        this.MobsRegenEvent = new MobsRegenEvent(this);
                        EventManager.AddEvent(this.MobsRegenEvent);
                    }
                }
            }
        }