/// <summary> /// Performs a rebuild of the names item database /// This includes saving to disk /// This is mostly asynchronous, as it returns the total amount of names it will be pulling /// from the API, for progress-bar purposes. /// </summary> /// <param name="incrementProgressAction">Action to perform when the progress continues</param> /// <param name="cancelToken">Cancellation token in case the user wishes to cancel the task</param> /// <returns>Returns the total amount of requests that will be performed</returns> public int RebuildItemDatabase(CultureInfo culture, Action incrementProgressAction, Action rebuildCompleteAction, CancellationToken cancelToken) { var itemService = GW2.V2.Items.ForCulture(culture); var itemIds = itemService.Discover(); // We'll split this up into multiple requests int requestSize = 200; // maybe tweak this int totalRequests = (itemIds.Count / requestSize) + 1; Task.Factory.StartNew(() => { Dictionary <int, ItemDBEntry> itemsDb = new Dictionary <int, ItemDBEntry>(); for (int i = 0; i < totalRequests; i++) { if (cancelToken.IsCancellationRequested) { return; } var items = itemService.FindPage(i, requestSize); foreach (var item in items) { var entry = new ItemDBEntry(item.ItemId, item.Name, (ItemRarity)item.Rarity, item.Level); itemsDb.Add(item.ItemId, entry); } incrementProgressAction.Invoke(); } var dbString = JsonConvert.SerializeObject(itemsDb); File.WriteAllText(this.GetFilePath(CultureInfo.CurrentUICulture.TwoLetterISOLanguageName), dbString); rebuildCompleteAction.Invoke(); }, cancelToken); return(totalRequests); }
/// <summary> /// Performs a rebuild of the names item database /// This includes saving to disk /// This is mostly asynchronous, as it returns the total amount of names it will be pulling /// from the API, for progress-bar purposes. /// </summary> /// <param name="incrementProgressAction">Action to perform when the progress continues</param> /// <param name="cancelToken">Cancellation token in case the user wishes to cancel the task</param> /// <returns>Returns the total amount of requests that will be performed</returns> public int RebuildItemDatabase(CultureInfo culture, Action incrementProgressAction, Action rebuildCompleteAction, CancellationToken cancelToken) { var itemService = GW2.V2.Items.ForCulture(culture); var itemIds = itemService.Discover(); // We'll split this up into multiple requests int requestSize = 200; // maybe tweak this int totalRequests = (itemIds.Count / requestSize) + 1; Task.Factory.StartNew(() => { Dictionary<int, ItemDBEntry> itemsDb = new Dictionary<int, ItemDBEntry>(); for (int i = 0; i < totalRequests; i++) { if (cancelToken.IsCancellationRequested) { return; } var items = itemService.FindPage(i, requestSize); foreach (var item in items) { var entry = new ItemDBEntry(item.ItemId, item.Name, (ItemRarity)item.Rarity, item.Level); itemsDb.Add(item.ItemId, entry); } incrementProgressAction.Invoke(); } var dbString = JsonConvert.SerializeObject(itemsDb); File.WriteAllText(this.GetFilePath(CultureInfo.CurrentUICulture.TwoLetterISOLanguageName), dbString); rebuildCompleteAction.Invoke(); }, cancelToken); return totalRequests; }