/// <summary> /// Fetches the Tf2 Item schema. /// </summary> /// <param name="apiKey">The API key.</param> /// <returns>A deserialized instance of the Item Schema.</returns> /// <remarks> /// The schema will be cached for future use if it is updated. /// </remarks> public static Schema Init(string apiKey, string schemaLang = null) { if (_schema != null) { return(_schema); } var url = SchemaApiUrlBase + apiKey; if (schemaLang != null) { url += "&language=" + schemaLang; } // Get Schema URL Regex regex = new Regex("https.*txt"); DateTime schemaLastModified; using (HttpWebResponse response = new SteamWeb().Request(url, "GET")) { schemaLastModified = response.LastModified; string result; using (var reader = new StreamReader(response.GetResponseStream())) { result = reader.ReadToEnd(); } url = regex.Match(result).Value; } _schema = GetSchema(url, schemaLastModified); return(_schema); }
// Gets the schema from the web or from the cached file. private static Schema GetSchema(string url, DateTime schemaLastModified) { bool mustUpdateCache = !File.Exists(Cachefile) || schemaLastModified > File.GetCreationTime(Cachefile); var items = ParseLocalSchema(); List <string> lines = new List <string>(); Dictionary <string, Item> newItems = new Dictionary <string, Item>(); if (mustUpdateCache) { using (HttpWebResponse response = new SteamWeb().Request(url, "GET")) { using (var reader = new StreamReader(response.GetResponseStream())) { while (!reader.EndOfStream) { lines.Add(reader.ReadLine()); } } } newItems = ParseWebSchema(VDFConvert.ToJson(lines.ToArray())); } if (items == null) { items = newItems; } else { foreach (var item in newItems) { if (!items.ContainsKey(item.Key)) { items[item.Key] = item.Value; } } } var schema = new Schema() { _items = items }; System.IO.Directory.CreateDirectory(Path.GetDirectoryName(Cachefile) ?? ""); File.WriteAllText(Cachefile, new JavaScriptSerializer { MaxJsonLength = 62914560 }.Serialize(schema.GetItems())); return(schema); }