Beispiel #1
0
        /// <summary>
        /// Loads the database, then fills the table with the UserData
        /// </summary>
        /// <param name="filePath">Path to the import database file</param>
        private void LoadDatabase(string filePath)
        {
            string dbError = App.ResMan.GetString("DbError");

            try
            {
                var didExpand = ImportExportHelper.Expand(filePath);
                if (!didExpand)
                {
                    return;
                }
                var dbPath = @$ "{App.AssetDirPath}\Import.db";
                if (!File.Exists(dbPath))
                {
                    return;
                }
                using (var db = new LiteDatabase($"Filename={dbPath};Password={App.ImportExportDbKey}"))
                {
                    IEnumerable <UserDataGames> dbUserData =
                        db.GetCollection <UserDataGames>(DbUserData.UserData_Games.ToString()).FindAll().ToArray();
                    var addList = new List <UserDataGames>();
                    foreach (var item in dbUserData)
                    {
                        var result = _validator.Validate(item);
                        foreach (var error in result.Errors)
                        {
                            item.SetError(error.PropertyName, error.ErrorMessage);
                        }

                        addList.Add(item);
                    }


                    UserDataGamesCollection.AddRange(addList);
                    File.Copy(@$ "{App.AssetDirPath}\Import.db", Path.Combine(App.ConfigDirPath, @"database\Import.db"));
                }

                DatabaseName = Path.GetFileName(filePath);
            }
            catch (IOException)
            {
                var errorStr = App.ResMan.GetString("DbIsLockedProc");
                _windowManager.ShowMessageBox(errorStr, dbError, MessageBoxButton.OK, MessageBoxImage.Exclamation);
            }
            catch (Exception ex)
            {
                App.Logger.Warning(ex, "Failed to load database for import");
                SentryHelper.SendException(ex, null, SentryLevel.Error);
                _windowManager.ShowMessageBox($"{App.ResMan.GetString("ImportInvalidDb")}\n{Path.GetFileName(filePath)}",
                                              $"{App.ResMan.GetString("ImportInvalidDbTitle")}", MessageBoxButton.OK, MessageBoxImage.Exclamation);
            }
        }