Пример #1
0
        private static void LoadPointsOfinterest()
        {
            string query =
                "SELECT ID, PositionX, PositionY, Icon, Flags, Importance, Name " +
                $"FROM {Settings.TDBDatabase}.points_of_interest ORDER BY ID;";

            using (var command = SQLConnector.CreateCommand(query))
            {
                if (command == null)
                {
                    return;
                }
                using (MySqlDataReader reader = command.ExecuteReader())
                {
                    while (reader.Read())
                    {
                        var poiData = new POIData()
                        {
                            ID         = Convert.ToUInt32(reader["ID"]),
                            PositionX  = Convert.ToSingle(reader["PositionX"]),
                            PositionY  = Convert.ToSingle(reader["PositionY"]),
                            Icon       = Convert.ToUInt32(reader["Icon"]),
                            Flags      = Convert.ToUInt32(reader["Flags"]),
                            Importance = Convert.ToUInt32(reader["Importance"]),
                            Name       = Convert.ToString(reader["Name"])
                        };

                        POIs.Add(poiData);
                    }
                }
            }
        }
Пример #2
0
        public static T GetDefaultObject <T>() where T : IDataModel, new()
        {
            var tableName = GetTableName <T>(true);

            //                              0                 1
            var query = $"SELECT `COLUMN_NAME`, `COLUMN_DEFAULT` FROM `information_schema`.`COLUMNS` WHERE `TABLE_SCHEMA`='{Settings.TDBDatabase}' AND `TABLE_NAME`='{tableName}'";

            using (var command = SQLConnector.CreateCommand(query))
            {
                if (command == null)
                {
                    return(default);
Пример #3
0
        public static RowList <T> Get <T>(RowList <T> rowList = null, string database = null)
            where T : IDataModel, new()
        {
            // TODO: Add new config option "Verify data against DB"
            if (!SQLConnector.Enabled)
            {
                return(null);
            }

            if (!SQLUtil.IsTableVisible <T>())
            {
                return(null);
            }

            var result = new RowList <T>();

            using (var command = SQLConnector.CreateCommand(new SQLSelect <T>(rowList, database).Build()))
            {
                if (command == null)
                {
                    return(null);
                }

                var fields      = SQLUtil.GetFields <T>();
                var fieldsCount = fields.Select(f => f.Item3.First().Count).Sum();
                using (MySqlDataReader reader = command.ExecuteReader())
                {
                    while (reader.Read())
                    {
                        var instance = (T)Activator.CreateInstance(typeof(T));
                        var values   = GetValues(reader, fieldsCount);

                        var i = 0;
                        foreach (var field in fields)
                        {
                            SQLUtil.SetFieldValueByDB(instance, field, values, i);
                            i += field.Item3.First().Count;
                        }

                        result.Add(instance);
                    }
                }
            }

            return(result);
        }
Пример #4
0
        /// <summary>
        /// Loads names of the objects from the database.
        /// </summary>
        public static void GrabNameData()
        {
            if (!SQLConnector.Connected())
            {
                throw new DataException("Cannot get DB data without an active DB connection.");
            }

            foreach (var objectType in ObjectTypes)
            {
                using (var command = SQLConnector.CreateCommand($"SELECT `Id`, `Name` FROM `object_names` WHERE `ObjectType`='{objectType}';"))
                {
                    if (command == null)
                    {
                        return;
                    }

                    using (MySqlDataReader reader = command.ExecuteReader())
                    {
                        while (reader.Read())
                        {
                            int    ID   = Convert.ToInt32(reader.GetValue(0));
                            string Name = Convert.ToString(reader.GetValue(1));

                            Dictionary <int, string> names;
                            if (!NameStores.TryGetValue(objectType, out names))
                            {
                                names = new Dictionary <int, string>();
                                NameStores.Add(objectType, names);
                            }

                            if (!names.ContainsKey(ID))
                            {
                                names.Add(ID, Name);
                            }
                        }
                    }
                }
            }
        }
Пример #5
0
        // Returns a dictionary from a DB query with two parameters (e.g <creature_entry, creature_name>)
        // TODO: Drop this and use the GetDict<T, TK> method below
        public static Dictionary <T, TK> GetDict <T, TK>(string query)
        {
            using (var command = SQLConnector.CreateCommand(query))
            {
                if (command == null)
                {
                    return(null);
                }

                var dict = new Dictionary <T, TK>();

                using (MySqlDataReader reader = command.ExecuteReader())
                {
                    while (reader.Read())
                    {
                        dict.Add((T)Convert.ChangeType(reader.GetValue(0), typeof(T)), (TK)Convert.ChangeType(reader.GetValue(1), typeof(TK)));
                    }
                }

                return(dict);
            }
        }
Пример #6
0
        /// <summary>
        /// Loads the broadcast texts form the database.
        /// </summary>
        private static void LoadBroadcastText()
        {
            var    soundFieldName = Settings.TargetedDatabase >= TargetedDatabase.Shadowlands ? "Kit" : "Entries";
            string query          =
                $"SELECT ID, Text, Text1, EmoteID1, EmoteID2, EmoteID3, EmoteDelay1, EmoteDelay2, EmoteDelay3, EmotesID, LanguageID, Flags, ConditionID, Sound{soundFieldName}ID1, Sound{soundFieldName}ID2 " +
                $"FROM {Settings.HotfixesDatabase}.broadcast_text;";

            if (Settings.TargetedDatabase == TargetedDatabase.WrathOfTheLichKing || Settings.TargetedDatabase == TargetedDatabase.Cataclysm)
            {
                query = "SELECT ID, LanguageID, Text, Text1, EmoteID1, EmoteID2, EmoteID3, EmoteDelay1, EmoteDelay2, EmoteDelay3, SoundEntriesID, EmotesID, Flags " +
                        $"FROM {Settings.TDBDatabase}.broadcast_text;";
            }

            using (var command = SQLConnector.CreateCommand(query))
            {
                if (command == null)
                {
                    return;
                }

                using (MySqlDataReader reader = command.ExecuteReader())
                {
                    while (reader.Read())
                    {
                        var id    = Convert.ToInt32(reader["Id"]);
                        var text  = Convert.ToString(reader["Text"]);
                        var text1 = Convert.ToString(reader["Text1"]);

                        if (!BroadcastTexts.ContainsKey(text))
                        {
                            BroadcastTexts[text] = new List <int>();
                        }
                        BroadcastTexts[text].Add(id);

                        if (!BroadcastText1s.ContainsKey(text1))
                        {
                            BroadcastText1s[text1] = new List <int>();
                        }
                        BroadcastText1s[text1].Add(id);

                        if (!Settings.UseDBC)
                        {
                            continue;
                        }

                        var broadcastText = new BroadcastTextEntry()
                        {
                            Text  = text,
                            Text1 = text1,
                        };
                        broadcastText.EmoteID       = new ushort[3];
                        broadcastText.EmoteID[0]    = Convert.ToUInt16(reader["EmoteID1"]);
                        broadcastText.EmoteID[1]    = Convert.ToUInt16(reader["EmoteID2"]);
                        broadcastText.EmoteID[2]    = Convert.ToUInt16(reader["EmoteID3"]);
                        broadcastText.EmoteDelay    = new ushort[3];
                        broadcastText.EmoteDelay[0] = Convert.ToUInt16(reader["EmoteDelay1"]);
                        broadcastText.EmoteDelay[1] = Convert.ToUInt16(reader["EmoteDelay2"]);
                        broadcastText.EmoteDelay[2] = Convert.ToUInt16(reader["EmoteDelay3"]);
                        broadcastText.EmotesID      = Convert.ToUInt16(reader["EmotesID"]);
                        broadcastText.LanguageID    = Convert.ToInt32(reader["LanguageID"]);
                        broadcastText.Flags         = Convert.ToByte(reader["Flags"]);
                        if (Settings.TargetedDatabase == TargetedDatabase.WrathOfTheLichKing || Settings.TargetedDatabase == TargetedDatabase.Cataclysm)
                        {
                            broadcastText.ConditionID       = 0;
                            broadcastText.SoundEntriesID    = new uint[2];
                            broadcastText.SoundEntriesID[0] = Convert.ToUInt32(reader["SoundEntriesID"]);
                            broadcastText.SoundEntriesID[1] = 0;
                        }
                        else
                        {
                            broadcastText.ConditionID       = Convert.ToUInt32(reader["ConditionID"]);
                            broadcastText.SoundEntriesID    = new uint[2];
                            broadcastText.SoundEntriesID[0] = Convert.ToUInt32(reader[$"Sound{soundFieldName}ID1"]);
                            broadcastText.SoundEntriesID[1] = Convert.ToUInt32(reader[$"Sound{soundFieldName}ID2"]);
                        }

                        if (!DBC.DBC.BroadcastText.ContainsKey(id))
                        {
                            DBC.DBC.BroadcastText.Add(id, broadcastText);
                        }
                        else
                        {
                            DBC.DBC.BroadcastText[id] = broadcastText;
                        }
                    }
                }
            }
        }
Пример #7
0
        public static RowList <T> Get <T>(RowList <T> rowList = null, string database = null)
            where T : IDataModel, new()
        {
            // TODO: Add new config option "Verify data against DB"
            if (!SQLConnector.Enabled)
            {
                return(null);
            }

            var result = new RowList <T>();

            using (var command = SQLConnector.CreateCommand(new SQLSelect <T>(rowList, database).Build()))
            {
                if (command == null)
                {
                    return(null);
                }

                var fields      = SQLUtil.GetFields <T>();
                var fieldsCount = fields.Select(f => f.Item3.First().Count).Sum();
                using (MySqlDataReader reader = command.ExecuteReader())
                {
                    while (reader.Read())
                    {
                        var instance = (T)Activator.CreateInstance(typeof(T));
                        var values   = GetValues(reader, fieldsCount);

                        var i = 0;
                        foreach (var field in fields)
                        {
                            if (values[i] is DBNull)
                            {
                                if (field.Item2.FieldType == typeof(string))
                                {
                                    field.Item2.SetValue(instance, string.Empty);
                                }
                                else if (field.Item3.Any(a => a.Nullable))
                                {
                                    field.Item2.SetValue(instance, null);
                                }
                            }
                            else if (field.Item2.FieldType.BaseType == typeof(Enum))
                            {
                                field.Item2.SetValue(instance, Enum.Parse(field.Item2.FieldType, values[i].ToString()));
                            }
                            else if (field.Item2.FieldType.BaseType == typeof(Array))
                            {
                                var arr = Array.CreateInstance(field.Item2.FieldType.GetElementType(), field.Item3.First().Count);

                                for (var j = 0; j < arr.Length; j++)
                                {
                                    var elemType = arr.GetType().GetElementType();

                                    if (elemType.IsEnum)
                                    {
                                        arr.SetValue(Enum.Parse(elemType, values[i + j].ToString()), j);
                                    }
                                    else if (Nullable.GetUnderlyingType(elemType) != null) //is nullable
                                    {
                                        arr.SetValue(Convert.ChangeType(values[i + j], Nullable.GetUnderlyingType(elemType)), j);
                                    }
                                    else
                                    {
                                        arr.SetValue(Convert.ChangeType(values[i + j], elemType), j);
                                    }
                                }
                                field.Item2.SetValue(instance, arr);
                            }
                            else if (field.Item2.FieldType == typeof(bool))
                            {
                                field.Item2.SetValue(instance, Convert.ToBoolean(values[i]));
                            }
                            else if (Nullable.GetUnderlyingType(field.Item2.FieldType) != null) // is nullable
                            {
                                var uType = Nullable.GetUnderlyingType(field.Item2.FieldType);
                                field.Item2.SetValue(instance,
                                                     uType.IsEnum
                                        ? Enum.Parse(uType, values[i].ToString())
                                        : Convert.ChangeType(values[i], uType));
                            }
                            else
                            {
                                field.Item2.SetValue(instance, values[i]);
                            }

                            i += field.Item3.First().Count;
                        }

                        result.Add(instance);
                    }
                }
            }

            return(result);
        }