/// <summary>
        /// Processes one data source.
        /// </summary>
        /// <typeparam name="T">Type of data.</typeparam>
        /// <param name="dataList">List where should data be added.</param>
        /// <param name="url">URL to data.</param>
        /// <param name="index">Internal identifier for creating temporary folders.</param>
        /// <param name="urlsLength">Count of data feeds to process.</param>
        private static void ProcessData <T>(IList <IDataFeed> dataList, string url, int index = 0, int urlsLength = 1) where T : IDataFeed
        {
            try
            {
                DataProcessing?.Invoke($"Trying to download data from URL { url }.");

                Downloader.GetDataFeed($"{ index }_temp_data/", url, index);

                LoadingProgress?.Invoke("Part of the data successfully downloaded.", 20 / urlsLength);

                DataProcessing?.Invoke($"Data from URL { url } downloaded successfully.");

                DataProcessing?.Invoke($"Trying to parse data downloaded from { url }.");

                IDataFeed data = (T)Activator.CreateInstance(typeof(T), (string)$"{ index }_temp_data/");

                lock (dataList)
                {
                    dataList.Add(data);
                }

                LoadingProgress?.Invoke("Part of the data successfully parsed.", 40 / urlsLength);

                DataProcessing?.Invoke($"The data downloaded from { url } parsed successfully.");
            }
            catch (Exception ex)
            {
                if (ex is System.Reflection.TargetInvocationException)
                {
                    ex = ex.InnerException;
                }

                if (ex is UriFormatException)
                {
                    DataProcessing?.Invoke($"The URL { url } is invalid.");
                }

                else if (ex is System.Net.WebException)
                {
                    DataProcessing?.Invoke($"There was a problem downloading file { url }. Server may be inacessible or you are missing internet connection.");
                }

                else if (ex is FormatException)
                {
                    DataProcessing?.Invoke($"The data downloaded from { url } are not well-formed.");
                }
#if !DEBUG
                else
                {
                    DataProcessing?.Invoke($@"Parsing of data located in { url } ended with an unknown error.
Error: { ex.Message } Type of { ex.GetType() }.");
                }
#endif
                throw;
            }
        }
Пример #2
0
        private FSDirectory createKeywordsFrom(CardRepository repository)
        {
            if (!repository.IsLoadingComplete)
            {
                throw new InvalidOperationException($"{nameof(CardRepository)} must be loaded first");
            }

            var keywordsList = new List <CardKeywords>(repository.Cards.Count);

            foreach (var set in repository.SetsByCode.Values)
            {
                if (!FilterSet(set))
                {
                    continue;
                }

                var setKeywords = new CardKeywords[set.Cards.Count];

                IndexUtils.For(0, set.Cards.Count, i => setKeywords[i] = set.Cards[i].GetAllKeywords());

                keywordsList.AddRange(setKeywords);

                SetsCount++;
                LoadingProgress?.Invoke();
            }

            _version.CreateDirectory();

            var fsIndex           = FSDirectory.Open(_version.Directory);
            var indexWriterConfig = IndexUtils.CreateWriterConfig(new LowercaseKeywordAnalyzer());

            using (var writer = new IndexWriter(fsIndex, indexWriterConfig))
            {
                IndexUtils.ForEach(keywordsList,
                                   keyword =>
                {
                    var doc = keyword.ToDocument();
                    // ReSharper disable once AccessToDisposedClosure
                    writer.AddDocument(doc);
                });
            }

            _version.SetIsUpToDate();

            return(fsIndex);
        }
        public static void DownloadUpdates(string[] updates)
        {
            int i = 0;
            int n = updates.Length;

            using (WebClient wc = new WebClient())
            {
                for (int u = updates.Length - 1; u >= 0; u--)
                {
                    i++;
                    Application.Current.Dispatcher.Invoke(() => LoadingProgress?.Invoke(i, n));
                    UnzipFromStream(wc.OpenRead(GetUpdateFileLink(updates[u])), FormExecution.path);
                }
            }

            Application.Current.Dispatcher.Invoke(() => UpdateCompleted?.Invoke());
            FormExecution.ClientConfig.CardsStuffVersion = FormExecution.GetLastVersion(updates);
            FormExecution.SaveConfig();
        }
        /// <summary>
        /// Downloads and creates data feed.
        /// </summary>
        public static void GetAndTransformDataFeed <T>(params string[] urls) where T : IDataFeed
        {
            LoadingProgress?.Invoke("Preparing for data download.", 0);

            List <IDataFeed> dataList = new List <IDataFeed>(urls.Length);                               // Data are added into the list only if they are (downloaded and) parsed successfully.

            Parallel.For(0, urls.Length, (int i) => ProcessData <T>(dataList, urls[i], i, urls.Length)); // Try to process the data in parallel mode.

            if (dataList.Count == 0)
            {
                DataProcessing?.Invoke($"There are no data to process.");
                return;
            }

            DataProcessing?.Invoke($"Trying to merge the data.");

            IDataFeed mergedData = MergeMultipleDataFeeds(dataList);

            LoadingProgress?.Invoke("Data merged successfully.", 5);

            DataProcessing?.Invoke($"Data merged successfully.");

            DataProcessing?.Invoke($"Trying to create new data feed.");

            mergedData.CreateDataFeed("data/");

            mergedData.CreateBasicData("basic/");

            LoadingProgress?.Invoke($"Data feed created successfully.", 30);

            DataProcessing?.Invoke($"Data feed created successfully.");

            for (int i = 0; i < urls.Length; i++)
            {
                Downloader.DeleteTrash($"{ i }_temp_data/");
            }
        }
Пример #5
0
 public override void OnLoadingProgress(ILevelInfo pLevel, int progressAmount)
 {
     LoadingProgress?.Invoke(new EventArgs <ILevelInfo, int>(pLevel, progressAmount));
 }
        public static bool LoadCDB(string dir, bool overwrite, bool clearData = false)
        {
            logger.Info("Start LOAD CDB {0}", dir);
            LoadPicsFile();

            if (!File.Exists(dir))
            {
                return(false);
            }

            if (clearData)
            {
                CardData.Clear();
            }

            SQLiteConnection connection = new SQLiteConnection("Data Source=" + dir);
            List <string[]>  datas      = new List <string[]>();
            List <string[]>  texts      = new List <string[]>();

            try
            {
                connection.Open();
                datas = SQLiteCommands.LoadData(connection);
                texts = SQLiteCommands.LoadText(connection);
                connection.Close();
            }
            catch (Exception ex)
            {
                logger.Error("LOADCDB :" + ex.ToString());
                connection.Close();
                return(false);
            }

            progress = 0;
            total    = datas.Count + texts.Count;
            foreach (string[] row in datas)
            {
                if (overwrite)
                {
                    CardManager.UpdateOrAddCard(new CardInfos(row));
                }
                else
                {
                    if (!CardManager.ContainsCard(int.Parse(row[0])))
                    {
                        CardManager.UpdateOrAddCard(new CardInfos(row));
                    }
                }

                if (!CheckPicsLoaded(Convert.ToInt32(row[0])))
                {
                    DownloadPics(row[0]);
                }
                else
                {
                    progress++;
                    Application.Current.Dispatcher.Invoke(() => LoadingProgress?.Invoke(progress, total));
                }
            }
            foreach (string[] row in texts)
            {
                progress++;
                Application.Current.Dispatcher.Invoke(() => LoadingProgress?.Invoke(progress, total));

                if (CardManager.ContainsCard(int.Parse(row[0])))
                {
                    CardManager.GetCard(int.Parse(row[0])).SetCardText(row);
                }
            }

            if (File.Exists(Path.Combine(FormExecution.path, "BattleCityAlpha", "strings.conf")))
            {
                LoadSetCodesFromFile(CreateFileStreamFromString(File.ReadAllText(Path.Combine(FormExecution.path, "BattleCityAlpha", "strings.conf"))));
            }
            SetCodesStringInit();

            while (progress != total)
            {
                Thread.Sleep(500);
            }
            Application.Current.Dispatcher.Invoke(() => LoadingFinished?.Invoke());

            return(true);
        }
 private static void cardDownloaded(object sender, System.ComponentModel.AsyncCompletedEventArgs e)
 {
     progress++;
     Application.Current.Dispatcher.Invoke(() => LoadingProgress?.Invoke(progress, total));
 }