示例#1
0
        internal static GameTable <T> Read <T>(string fileName) where T : new()
        {
            GameTable <T> storage = new GameTable <T>();

            if (!File.Exists(CliDB.DataPath + fileName))
            {
                Log.outError(LogFilter.ServerLoading, "File {0} not found.", fileName);
                return(storage);
            }
            using (var reader = new StreamReader(CliDB.DataPath + fileName))
            {
                string headers = reader.ReadLine();
                if (headers.IsEmpty())
                {
                    Log.outError(LogFilter.ServerLoading, "GameTable file {0} is empty.", fileName);
                    return(storage);
                }

                List <T> data = new List <T>();
                data.Add(new T()); // row id 0, unused

                string line;
                while (!(line = reader.ReadLine()).IsEmpty())
                {
                    var values = new StringArray(line, '\t');
                    if (values.IsEmpty())
                    {
                        break;
                    }

                    var obj    = new T();
                    var fields = obj.GetType().GetFields();
                    for (int fieldIndex = 0, valueIndex = 1; fieldIndex < fields.Length && valueIndex < values.Length; ++fieldIndex, ++valueIndex)
                    {
                        var field = fields[fieldIndex];
                        if (field.FieldType.IsArray)
                        {
                            Array array = (Array)field.GetValue(obj);
                            for (var i = 0; i < array.Length; ++i)
                            {
                                array.SetValue(float.Parse(values[valueIndex++]), i);
                            }
                        }
                        else
                        {
                            fields[fieldIndex].SetValue(obj, float.Parse(values[valueIndex]));
                        }
                    }

                    data.Add(obj);
                }

                storage.SetData(data);
            }

            CliDB.LoadedFileCount++;
            return(storage);
        }
示例#2
0
        public bool LoadFromDB(SQLFields fields)
        {
            MarketID    = fields.Read <uint>(0);
            SellerNPC   = fields.Read <uint>(1);
            Item        = new ItemInstance();
            Item.ItemID = fields.Read <uint>(2);
            Quantity    = fields.Read <uint>(3);
            MinBid      = fields.Read <ulong>(4);
            Duration    = fields.Read <uint>(5);
            Chance      = fields.Read <float>(6);

            var         bonusListIDsTok = new StringArray(fields.Read <string>(7), ' ');
            List <uint> bonusListIDs    = new();

            if (!bonusListIDsTok.IsEmpty())
            {
                foreach (string token in bonusListIDsTok)
                {
                    if (uint.TryParse(token, out uint id))
                    {
                        bonusListIDs.Add(id);
                    }
                }
            }

            if (!bonusListIDs.Empty())
            {
                Item.ItemBonus.HasValue           = true;
                Item.ItemBonus.Value.BonusListIDs = bonusListIDs;
            }

            if (Global.ObjectMgr.GetCreatureTemplate(SellerNPC) == null)
            {
                Log.outError(LogFilter.Misc, "Black market template {0} does not have a valid seller. (Entry: {1})", MarketID, SellerNPC);
                return(false);
            }

            if (Global.ObjectMgr.GetItemTemplate(Item.ItemID) == null)
            {
                Log.outError(LogFilter.Misc, "Black market template {0} does not have a valid item. (Entry: {1})", MarketID, Item.ItemID);
                return(false);
            }

            return(true);
        }