Пример #1
0
        public static float GetCellValueFloat(object data, int splitPosition, string refObjectId)
        {
            if (data == null)
            {
                return(0f);
            }

            string value = string.Empty;

            try
            {
                value = data.ToString().Split(',')[splitPosition];
            }
            catch { }

            if (string.IsNullOrEmpty(value.ToString()))
            {
                return(0f);
            }

            try
            {
                return(Convert.ToSingle(value, System.Globalization.CultureInfo.InvariantCulture));
            }
            catch
            {
                MetaDataImportExeption.Log("Invalid value or cast to float.", refObjectId != "" ? refObjectId : data.ToString());
                return(0f);
            }
        }
Пример #2
0
        public static int GetCellValueInt(object data, int splitPosition, string refObjectId = "Undefined")
        {
            if (data == null)
            {
                return(0);
            }

            if (!string.IsNullOrEmpty(data.ToString()) && splitPosition == 1)
            {
                bool breakehre = true;
            }

            string value = string.Empty;

            try
            {
                value = data.ToString().Split(',')[splitPosition];
            }
            catch { }

            if (string.IsNullOrEmpty(data.ToString()))
            {
                return(0);
            }

            try
            {
                return(Convert.ToInt32(value, System.Globalization.CultureInfo.InvariantCulture));
            }
            catch
            {
                MetaDataImportExeption.Log("Invalid value or cast to int.", refObjectId != "Undefined" ? refObjectId : data.ToString());
                return(0);
            }
        }
        private static RewardData ImportGenericRewardItems(DataTable data, DataRow row, bool updateDatabase)
        {
            RewardData rewardData = new RewardData();

            if (updateDatabase)
            {
                DataLayer.Instance.Connection.Insert(rewardData);
            }

            string itemColumnName   = "Reward{0}_Item";
            string amountColumnName = "Reward{0}_Amount";

            int i = 1;

            while (data.Columns.Contains(string.Format(itemColumnName, i.ToString())))
            {
                string item   = row[string.Format(itemColumnName, i.ToString())].ToString();
                int    Amount = DataConvert.GetCellValueInt(row[string.Format(amountColumnName, i.ToString())].ToString());

                if (!string.IsNullOrEmpty(item) && Amount > 0)
                {
                    Consumable consumable = ObjectsMemoryCache[typeof(Consumable).Name].Cast <Consumable>().ToList().Where(y => y.Name == item).SingleOrDefault();

                    if (consumable != null)
                    {
                        RewardDataItem rewardDataItem = new RewardDataItem();
                        rewardDataItem.Consumable_Id = consumable.Id;
                        rewardDataItem.DropRate      = 100;
                        rewardDataItem.Amount        = Amount;
                        rewardDataItem.Reward_Id     = rewardData.Id;
                        if (updateDatabase)
                        {
                            DataLayer.Instance.Connection.Insert(rewardDataItem);
                        }
                    }
                    else
                    {
                        MetaDataImportExeption.Log("ImportGenericRewardItems could not find Consumable.", item);
                    }
                }
                i++;
            }

            return(rewardData);
        }
Пример #4
0
        public static int GetCellValueInt(object data, string refObjectId = "Undefined")
        {
            if (data == null)
            {
                return(0);
            }
            if (string.IsNullOrEmpty(data.ToString()))
            {
                return(0);
            }
            string value = data.ToString();

            try
            {
                return(Convert.ToInt32(value, System.Globalization.CultureInfo.InvariantCulture));
            }
            catch
            {
                MetaDataImportExeption.Log("Invalid value or cast to int.", refObjectId != "Undefined" ? refObjectId : data.ToString());
                return(0);
            }
        }
        public static void ImportAutoGeneratedObjects(DataSet dataset, List <Type> allTypes, bool updateDatabase)
        {
            foreach (Type type in allTypes)
            {
                if (updateDatabase)
                {
                    ImportDataStatus.WriteStatus(string.Format("Importing {0}...", type.Name));
                }

                if (!ObjectsMemoryCache.ContainsKey(type.Name))
                {
                    ObjectsMemoryCache.Add(type.Name, new List <object>());
                }

                MetaAutoGenerated autoMetaItem = (MetaAutoGenerated)type.GetCustomAttribute(typeof(MetaAutoGenerated));
                if (autoMetaItem != null)
                {
                    string tableName = type.Name;
                    if (!string.IsNullOrEmpty(autoMetaItem.Name))
                    {
                        tableName = autoMetaItem.Name;
                    }

                    bool isRewardObject = type.IsSubclassOf(typeof(RewardObject));

                    bool isCostObject = type.IsSubclassOf(typeof(CostObject));

                    bool isCostAndRewardObject = type.IsSubclassOf(typeof(CostAndRewardObject));

                    var fields = type.GetProperties(BindingFlags.Public | BindingFlags.Instance).ToList();

                    var ignoreFields = fields.Where(y => y.GetCustomAttribute(typeof(PrimaryKeyAttribute)) != null ||
                                                    y.GetCustomAttribute(typeof(IgnoreAttribute)) != null ||
                                                    y.GetCustomAttribute(typeof(IgnoreCodeFirst)) != null ||
                                                    y.GetCustomAttribute(typeof(MetaAutoGeneratedAllowNull)) != null ||
                                                    y.GetCustomAttribute(typeof(MetaAutoGenerated)) != null).ToList();



                    ignoreFields.ForEach(y => fields.Remove(y));


                    if (dataset.Tables.Contains(tableName))
                    {
                        foreach (DataRow row in dataset.Tables[tableName].Rows)
                        {
                            var newInstance = Activator.CreateInstance(type);

                            foreach (var field in fields)
                            {
                                string fieldName    = field.Name;
                                Type   propertyType = field.PropertyType;

                                MetaDependency dependency     = (MetaDependency)field.GetCustomAttribute(typeof(MetaDependency));
                                object         valueToCompare = null;

                                if (dataset.Tables[tableName].Columns.Contains(fieldName))
                                {
                                    if (dependency != null && dependency.CreateRelation)
                                    {
                                        if (propertyType != typeof(int))
                                        {
                                            MetaDataImportExeption.Log(string.Format("Field {0} must be declared as int to create Relationship.", fieldName), type.ToString());
                                        }



                                        valueToCompare = row[fieldName];
                                    }
                                    else
                                    {
                                        if (propertyType == typeof(string))
                                        {
                                            string value = row[fieldName].ToString();
                                            field.SetValue(newInstance, value);
                                            valueToCompare = value;
                                        }

                                        if (propertyType == typeof(int))
                                        {
                                            int value = DataConvert.GetCellValueInt(row[fieldName], type.Name);
                                            field.SetValue(newInstance, value);
                                            valueToCompare = value;
                                        }

                                        if (propertyType == typeof(float))
                                        {
                                            float value = DataConvert.GetCellValueFloat(row[fieldName], type.Name);
                                            field.SetValue(newInstance, value);
                                            valueToCompare = value;
                                        }

                                        if (propertyType == typeof(bool))
                                        {
                                            float value = DataConvert.GetCellValueInt(row[fieldName], type.Name);
                                            field.SetValue(newInstance, value == 1 ? true : false);
                                            valueToCompare = value;
                                        }

                                        if (propertyType.IsEnum)
                                        {
                                            string value = row[fieldName].ToString();
                                            if (!string.IsNullOrEmpty(value))
                                            {
                                                try
                                                {
                                                    var result = Enum.Parse(propertyType, value);
                                                    field.SetValue(newInstance, result);
                                                    valueToCompare = result;
                                                }
                                                catch
                                                {
                                                    MetaDataImportExeption.Log(string.Format("Could not cast {0} into {1} for object {2}.", value, propertyType.Name, field.Name), tableName);
                                                }
                                            }
                                        }
                                    }


                                    if (dependency != null)
                                    {
                                        PropertyInfo p = null;

                                        foreach (var pInfo in dependency.Type.GetProperties(BindingFlags.Public | BindingFlags.Instance).ToList())
                                        {
                                            if (p == null && pInfo.GetCustomAttribute <MetaUniqueField>() != null)
                                            {
                                                p = pInfo;
                                            }
                                            else if (p != null && pInfo.GetCustomAttribute <MetaUniqueField>() != null)
                                            {
                                                MetaDataImportExeption.Log("Found multiple MetaUniqueField attribute for Type.", dependency.Type.Name);
                                            }
                                        }

                                        if (p != null)
                                        {
                                            var relationValue = ObjectsMemoryCache[dependency.Type.Name].Where(y => p.GetValue(y).Equals(valueToCompare)).SingleOrDefault();

                                            if (valueToCompare.ToString() != string.Empty)
                                            {
                                                if (relationValue == null)
                                                {
                                                    MetaDataImportExeption.Log(String.Format("Could not find value of {0} in all objects of Type {1}.", valueToCompare.ToString(), dependency.Type.ToString()), type.Name);
                                                }
                                                else
                                                {
                                                    if (dependency.CreateRelation)
                                                    {
                                                        field.SetValue(newInstance, ((IMetaDataObject)relationValue).Id);
                                                    }
                                                }
                                            }
                                        }
                                        else
                                        {
                                            MetaDataImportExeption.Log("Could not find MetaUniqueField attribute for Type.", dependency.Type.Name);
                                        }
                                    }
                                }
                                else
                                {
                                    MetaDataImportExeption.Log(string.Format("Could not find field {0} in Excel sheet {1}.", fieldName, tableName), type.Name);
                                }
                            }

                            ObjectsMemoryCache[type.Name].Add(newInstance);

                            if (isRewardObject || isCostAndRewardObject)
                            {
                                ((RewardObject)newInstance).RewardData_Id = ImportGenericRewardItems(dataset.Tables[tableName], row, updateDatabase).Id;
                            }


                            if (isCostObject || isCostAndRewardObject)
                            {
                                ((ICostObject)newInstance).ConsumableCost_Id = ImportGenericCostItems(dataset.Tables[tableName], row, updateDatabase).Id;
                            }

                            if (updateDatabase)
                            {
                                DataLayer.Instance.Connection.Insert(newInstance);
                            }
                        }
                    }
                    else
                    {
                        MetaDataImportExeption.Log(string.Format("Could not find table {0} in Excel sheet.", tableName), type.Name);
                    }
                }
            }
        }