Exemplo n.º 1
0
        public static bool RequestItemData(GBAPIItemData item)
        {
            var handler = new GBAPIRequestHandler();

            handler.ProcessItemData(item);
            string request  = handler.Build();
            string response = new WebClient().DownloadString(request);

            return(handler.ParseResponse(response, item));
        }
Exemplo n.º 2
0
            public void ProcessItemData(GBAPIItemData item)
            {
                ItemType = item.ItemType;
                ItemID   = item.ItemID;
                APIType  = GBAPIRequestType.COREITEMDATA;

                var type = item.GetType();

                foreach (var field in type.GetFields())
                {
                    var GBField = (GBAPIField)field.GetCustomAttributes(typeof(GBAPIField), true).FirstOrDefault();
                    if (GBField != null)
                    {
                        Fields.Add(new KeyValuePair <string, FieldInfo>(GBField.FieldName, field));
                    }
                }
            }
Exemplo n.º 3
0
            /// <summary>
            /// Parses the response data(XML) and writes all data into parameter "item".
            /// </summary>
            /// <param name="response">The Response data from GameBanana API in XML string format</param>
            /// <param name="item">Reference to a GBAPIItemData to write the data to</param>
            /// <returns>If Parse completed with no errors</returns>
            public bool ParseResponse(string response, GBAPIItemData item)
            {
                var responseXML = XDocument.Parse(response).Root;
                var elements    = responseXML.Elements().ToList();

                if (elements.Count() != Fields.Count)
                {
                    var list = Fields.Select(t => t.Key).ToList();
                    list.Insert(0, response);
                    MainForm.AddMessage("Exception thrown while Parsing a GBAPI Response!",
                                        new Exception("GameBanana Returned less values than requested! Parsing must abort!"), list.ToArray());
                    return(false);
                }

                foreach (var field in Fields)
                {
                    var element    = elements.First();
                    var GBFieldKey = (GBAPIFieldKeyArray)field.Value.GetCustomAttributes(typeof(GBAPIFieldKeyArray), true).FirstOrDefault();
                    if (GBFieldKey != null)
                    {
                        var arrayElements = element.Elements().ToArray();
                        var array         = Array.CreateInstance(field.Value.FieldType.GetElementType(), arrayElements.Length);
                        var keyInfo       = field.Value.FieldType.GetElementType().GetField(GBFieldKey.KeyName);
                        var arrayInfo     = field.Value.FieldType.GetElementType().GetField(GBFieldKey.ArrayName);
                        for (int i = 0; i < array.Length; ++i)
                        {
                            object obj = Activator.CreateInstance(field.Value.FieldType.GetElementType());
                            // Key
                            object value = TryConvert(arrayElements[i].Attribute("key").Value, keyInfo.FieldType);
                            if (value != null)
                            {
                                keyInfo.SetValue(obj, value);
                            }
                            // Array
                            var subElements = arrayElements[i].Elements().ToArray();
                            var array2      = Array.CreateInstance(arrayInfo.FieldType.GetElementType(), subElements.Length);
                            for (int ii = 0; ii < array2.Length; ++ii)
                            {
                                array2.SetValue(XMLtoObject(arrayInfo.FieldType.GetElementType(), subElements[ii]), ii);
                            }
                            arrayInfo.SetValue(obj, array2);


                            array.SetValue(obj, i);
                        }
                        field.Value.SetValue(item, array);
                    }
                    else if (element.Name == "value")
                    {
                        field.Value.SetValue(item, XMLtoObject(field.Value.FieldType, elements.First()));
                    }
                    else
                    {
                        var arrayElements = element.Elements().ToArray();
                        var array         = Array.CreateInstance(field.Value.FieldType.GetElementType(), arrayElements.Length);
                        for (int i = 0; i < array.Length; ++i)
                        {
                            array.SetValue(XMLtoObject(field.Value.FieldType.GetElementType(), arrayElements[i]), i);
                        }
                        field.Value.SetValue(item, array);
                    }
                    elements.RemoveAt(0);
                }
                return(true);
            }