Exemplo n.º 1
0
        private static bool Initialize()
        {
            int count;
            using (BinaryReader reader = new BinaryReader(new FileStream(Config.Instance.Binary, FileMode.Open, FileAccess.Read), Encoding.ASCII))
            {
                count = reader.ReadInt32();
                while (count-- > 0)
                {
                    AbilityData ability = new AbilityData();
                    ability.Load(reader);
                    Dictionary<byte, AbilityData> levels = sAbilities.GetOrDefault(ability.Identifier, null);
                    if (levels == null)
                    {
                        levels = new Dictionary<byte,AbilityData>();
                        sAbilities.Add(ability.Identifier, levels);
                    }
                    levels.Add(ability.Level, ability);
                }

                count = reader.ReadInt32();
                while (count-- > 0)
                {
                    SkillData skill = new SkillData();
                    skill.Load(reader);
                    Dictionary<byte, SkillData> levels = sSkills.GetOrDefault(skill.Identifier, null);
                    if (levels == null)
                    {
                        levels = new Dictionary<byte, SkillData>();
                        sSkills.Add(skill.Identifier, levels);
                    }
                    levels.Add(skill.Level, skill);
                }

                count = reader.ReadInt32();
                while (count-- > 0)
                {
                    NPCData npc = new NPCData();
                    npc.Load(reader);
                    sNPCs.Add(npc.Identifier, npc);
                }

                count = reader.ReadInt32();
                while (count-- > 0)
                {
                    ReactorData reactor = new ReactorData();
                    reactor.Load(reader);
                    sReactors.Add(reactor.Identifier, reactor);
                }

                count = reader.ReadInt32();
                while (count-- > 0)
                {
                    MobData mob = new MobData();
                    mob.Load(reader);
                    sMobs.Add(mob.Identifier, mob);
                }

                count = reader.ReadInt32();
                while (count-- > 0)
                {
                    QuestData quest = new QuestData();
                    quest.Load(reader);
                    sQuests.Add(quest.Identifier, quest);
                }

                count = reader.ReadInt32();
                while (count-- > 0)
                {
                    ItemData item = new ItemData();
                    item.Load(reader);
                    sItems.Add(item.Identifier, item);
                }

                count = reader.ReadInt32();
                while (count-- > 0)
                {
                    MapData map = new MapData();
                    map.Load(reader);
                    sMaps.Add(map.Identifier, map);
                }
            }
            Log.WriteLine(ELogLevel.Info, "[Server] Initialized Data");

            foreach (string scriptPath in Config.Instance.Scripts)
            {
                if (!File.Exists(scriptPath)) continue;
                try { Assembly.LoadFile(Path.GetFullPath(scriptPath)); }
                catch { return false; }
            }
            List<Doublet<InitializerAttribute, InitializerCallback>> initializers = Reflector.FindAllMethods<InitializerAttribute, InitializerCallback>();
            initializers.Sort((p1, p2) => p1.First.Stage.CompareTo(p2.First.Stage));
            if (!initializers.TrueForAll(p => p.Second())) return false;

            sLoginListener = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
            sLoginListener.Bind(new IPEndPoint(IPAddress.Any, Config.Instance.Login.Listener.Port));
            sLoginListener.Listen(Config.Instance.Login.Listener.Backlog);
            Log.WriteLine(ELogLevel.Info, "[Server] Initialized Login Listener");

            sChannelListener = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
            sChannelListener.Bind(new IPEndPoint(IPAddress.Any, Config.Instance.Channel.Listener.Port));
            sChannelListener.Listen(Config.Instance.Channel.Listener.Backlog);
            Log.WriteLine(ELogLevel.Info, "[Server] Initialized Channel Listener");

            BeginLoginListenerAccept(null);
            BeginChannelListenerAccept(null);

            return true;
        }
        public static void Export(BinaryWriter pWriter)
        {
            PerformanceTimer timer = new PerformanceTimer();
            long dataCount = 0;
            timer.Unpause();

            List<AbilityData> datas = new List<AbilityData>();
            using (MySqlConnection connection1 = new MySqlConnection(Program.Database))
            {
                connection1.Open();
                MySqlCommand command1 = connection1.CreateCommand();
                command1.CommandText = "SELECT COUNT(*) FROM skill_mob_data";
                Program.ResetCounter((int)(long)command1.ExecuteScalar());
                command1.CommandText = "SELECT * FROM skill_mob_data ORDER BY skillid ASC,level ASC";
                using (MySqlDataReader reader1 = command1.ExecuteReader())
                {
                    while (reader1.Read())
                    {
                        AbilityData data = new AbilityData();
                        data.Identifier = (byte)Convert.ChangeType(reader1["skillid"], TypeCode.Byte);
                        data.Level = (byte)Convert.ChangeType(reader1["level"], TypeCode.Byte);
                        data.Duration = (ushort)Convert.ChangeType(reader1["time"], TypeCode.UInt16);
                        data.MPCost = (byte)Convert.ChangeType(reader1["mp_cost"], TypeCode.Byte);
                        data.Parameter1 = (int)Convert.ChangeType(reader1["x"], TypeCode.Int32);
                        data.Parameter2 = (int)Convert.ChangeType(reader1["y"], TypeCode.Int32);
                        data.Chance = (byte)Convert.ChangeType(reader1["chance"], TypeCode.Byte);
                        data.TargetCount = (byte)Convert.ChangeType(reader1["target_count"], TypeCode.Byte);
                        data.Cooldown = (ushort)Convert.ChangeType(reader1["cooldown"], TypeCode.UInt16);
                        data.LeftTopX = (short)Convert.ChangeType(reader1["ltx"], TypeCode.Int16);
                        data.LeftTopY = (short)Convert.ChangeType(reader1["lty"], TypeCode.Int16);
                        data.RightBottomX = (short)Convert.ChangeType(reader1["rbx"], TypeCode.Int16);
                        data.RightBottomY = (short)Convert.ChangeType(reader1["rby"], TypeCode.Int16);
                        data.HPLimitPercent = (byte)Convert.ChangeType(reader1["hp_limit_percentage"], TypeCode.Byte);
                        data.SummonLimit = (ushort)Convert.ChangeType(reader1["summon_limit"], TypeCode.UInt16);
                        data.SummonEffect = (byte)Convert.ChangeType(reader1["summon_effect"], TypeCode.Byte);
                        data.SummonIdentifiers = new List<int>();
                        if (data.Identifier == 200)
                        {
                            using (MySqlConnection connection2 = new MySqlConnection(Program.Database))
                            {
                                connection2.Open();
                                MySqlCommand command2 = connection2.CreateCommand();
                                command2.CommandText = "SELECT * FROM skill_mob_summons WHERE level=@level ORDER BY `index` ASC";
                                command2.Parameters.AddWithValue("@level", data.Level);
                                using (MySqlDataReader reader2 = command2.ExecuteReader())
                                {
                                    while (reader2.Read())
                                    {
                                        data.SummonIdentifiers.Add((int)Convert.ChangeType(reader2["mobid"], TypeCode.Int32));
                                    }
                                }
                            }
                        }

                        datas.Add(data);
                        ++dataCount;
                        ++Program.AllDataCounter;
                        Program.IncrementCounter();
                    }
                }
            }

            pWriter.Write(datas.Count);
            datas.ForEach(d => d.Save(pWriter));

            timer.Pause();
            Console.WriteLine("| {0,-24} | {1,-16} | {2,-24} |", "AbilityData", dataCount, timer.Duration);
        }